/[PAMELA software]/yoda/techmodel/EventReader.cpp
ViewVC logotype

Diff of /yoda/techmodel/EventReader.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.8 by kusanagi, Tue Aug 24 13:19:59 2004 UTC revision 2.2 by kusanagi, Fri Sep 24 11:57:49 2004 UTC
# Line 1  Line 1 
1  /** @file  /** @file
2   * $Author: kusanagi $   * $Author: kusanagi $
3   * $Date: 2004/08/20 15:01:42 $   * $Date: 2004/09/22 13:14:03 $
4   * $Revision: 1.7 $   * $Revision: 2.1 $
5   *   *
6   * Implementation of the functions of a sample Algorithm class.   * Implementation of the functions of a sample Algorithm class.
7   * This file can be used as a templace to develop your own algorithm.   * This file can be used as a templace to develop your own algorithm.
8   */   */
9    
10  #include <log4cpp/Category.hh>  #include <log4cxx/logger.h>
11  #include <fstream>  #include <fstream>
12  #include "TechmodelAlgorithm.h"  #include "TechmodelAlgorithm.h"
13  #include "EventReader.h"  #include "EventReader.h"
# Line 23  extern "C" { Line 23  extern "C" {
23  using namespace pamela;  using namespace pamela;
24  using namespace pamela::techmodel;  using namespace pamela::techmodel;
25    
26  static log4cpp::Category& cat = log4cpp::Category::getInstance("pamela.techmodel.EventReader");  static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.EventReader"));
27    
28    unsigned int EventReader::maxPackets     = 0;
29  unsigned int EventReader::prevPckCounter = 0;  unsigned int EventReader::prevPckCounter = 0;
30  unsigned int EventReader::prevPckOBT     = 0;  unsigned int EventReader::prevPckOBT     = 0;
31    
32    
33  /**  /**
34   * Constructor.   * Constructor.
35   */   */
36  EventReader::EventReader(void):  EventReader::EventReader(int packetsLimit = -1):
37    TechmodelAlgorithm(0, "TechmodelEventReader") {    TechmodelAlgorithm(0, "TechmodelEventReader"){
38    cat <<  log4cpp::Priority::DEBUG  //EventReader::EventReader(void):
39        <<  "Constructor "  //  TechmodelAlgorithm(0, "TechmodelEventReader") {
40        <<  "\n " << log4cpp::CategoryStream::ENDLINE;    EventReader::maxPackets = packetsLimit;
41      logger->debug(_T("Constructor"));
42    Header = new EventHeader();    Header = new EventHeader();
43    
44    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun,      new PhysEndRunReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun,      new PhysEndRunReader()));
# Line 56  EventReader::EventReader(void): Line 61  EventReader::EventReader(void):
61    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitTrailer,     new InitTrailerReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitTrailer,     new InitTrailerReader()));
62    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::EventTrk,        new EventTrkReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::EventTrk,        new EventTrkReader()));
63    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTrk,         new TestTrkReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTrk,         new TestTrkReader()));
64      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTof,         new TestTofReader()));
65    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log,             new LogReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log,             new LogReader()));
66    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump,         new VarDumpReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump,         new VarDumpReader()));
67    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump,         new ArrDumpReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump,         new ArrDumpReader()));
# Line 68  EventReader::EventReader(void): Line 74  EventReader::EventReader(void):
74    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit,         new TrkInitReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit,         new TrkInitReader()));
75    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit,         new TofInitReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit,         new TofInitReader()));
76    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit,         new TrgInitReader()));    TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit,         new TrgInitReader()));
77      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit,         new NdInitReader()));
78      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalAlarm,        new CalAlarmReader()));
79      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::AcAlarm,         new AcAlarmReader()));
80      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkAlarm,        new TrkAlarmReader()));
81      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgAlarm,        new TrgAlarmReader()));
82      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofAlarm,        new TofAlarmReader()));
83  }  }
84    
85  /**  /**
# Line 75  EventReader::EventReader(void): Line 87  EventReader::EventReader(void):
87   */   */
88  std::string EventReader::GetVersionInfo(void) const {  std::string EventReader::GetVersionInfo(void) const {
89    return    return
90      "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.7 2004/08/20 15:01:42 kusanagi Exp $\n";      "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 2.1 2004/09/22 13:14:03 kusanagi Exp $\n";
91  }  }
92    
93  /**  /**
# Line 83  std::string EventReader::GetVersionInfo( Line 95  std::string EventReader::GetVersionInfo(
95   * event reader routines for all packet types.   * event reader routines for all packet types.
96   */   */
97  void EventReader::Init(PamelaRun *run) {  void EventReader::Init(PamelaRun *run) {
98        std::stringstream oss;
99        logger->debug(_T("Initialize"));
100      SetInputStream(run);      SetInputStream(run);
101            
102      //Create the structure of directories and create xxx.Header.root files      //Create the structure of directories and create xxx.Header.root files
# Line 91  void EventReader::Init(PamelaRun *run) { Line 105  void EventReader::Init(PamelaRun *run) {
105      //Create the xxx.root in it's specific directory      //Create the xxx.root in it's specific directory
106      for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin();      for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin();
107         i != TechmodelAlgorithmMap.end(); i++) {         i != TechmodelAlgorithmMap.end(); i++) {
108         cat  <<  log4cpp::Priority::DEBUG         oss.flush();
109              <<  " Initializing algo " << i->second->GetAlgorithmName()         oss << "Initializing algo " << i->second->GetAlgorithmName();
110              <<  "\n " << log4cpp::CategoryStream::ENDLINE;         //cout << "Initializing algo " << i->second->GetAlgorithmName();
111           logger->debug(oss.str().c_str());
112         i->second->Init(run);         i->second->Init(run);
113      }      }
114      Run = dynamic_cast<TechmodelPamelaRun*>(run);      Run = dynamic_cast<TechmodelPamelaRun*>(run);
# Line 112  static void SkipToNextHeader(ifstream *) Line 127  static void SkipToNextHeader(ifstream *)
127  // EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later....  // EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later....
128  // 15 June 2004 ---note------------------  // 15 June 2004 ---note------------------
129  void EventReader::RunEvent(int EventNumber) {  void EventReader::RunEvent(int EventNumber) {
     
   cat <<  log4cpp::Priority::INFO <<  "Start Unpacking........"  
       <<  "\n " << log4cpp::CategoryStream::ENDLINE;  
130    //for now i'll suppose that the raw file starts immediately with the right bytes    //for now i'll suppose that the raw file starts immediately with the right bytes
131    //to insert a GetPacketStart()    //to insert a GetPacketStart()
132    while (!InputFile->eof()){    stringstream oss;
133      int step = 0;
134      while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){
135      try {      try {
136        if (FindStart()) {        if (FindStart()) {
137          if(UnpackPscuHeader()){          if(UnpackPscuHeader()){
# Line 129  void EventReader::RunEvent(int EventNumb Line 143  void EventReader::RunEvent(int EventNumb
143                  Run->FillTrees(type);                  Run->FillTrees(type);
144                  Header->GetCounter()->Increment(type);                  Header->GetCounter()->Increment(type);
145              } else {              } else {
146              cat <<  log4cpp::Priority::ERROR                  oss.flush();
147                  <<  "No way to read events of type" << type->GetName()                  oss << "No way to read events of type" << type->GetName();
148                  <<  "\n " << log4cpp::CategoryStream::ENDLINE;                  logger->info(oss.str().c_str());
149              throw Exception("No way to read events of type " + type->GetName());              throw Exception("No way to read events of type " + type->GetName());
150          }          }
151        } //else {        } //else {
# Line 141  void EventReader::RunEvent(int EventNumb Line 155  void EventReader::RunEvent(int EventNumb
155       }       }
156      } catch (...) {      } catch (...) {
157        //This have to be more detailed. More exceptions type are needed.        //This have to be more detailed. More exceptions type are needed.
158        cat <<  log4cpp::Priority::ERROR <<  "Couldn't read the event. Skipping to the next header."        logger->error("Couldn't read the event. Skipping to the next header.");
           <<  "\n " << log4cpp::CategoryStream::ENDLINE;  
159      }      }
160        if ((step%1000) == 0) std::cout << step/1000 << "K \n";
161    }    }
162    Header->GetCounter()->PrintCounters();      Header->GetCounter()->PrintCounters();
163  }  }
164    
165  static void SkipToNextHeader(ifstream* TechmodelFile) {  static void SkipToNextHeader(ifstream* TechmodelFile) {
# Line 158  static void SkipToNextHeader(ifstream* T Line 172  static void SkipToNextHeader(ifstream* T
172   * Unpack the PSCU header from a file into the structure.   * Unpack the PSCU header from a file into the structure.
173   */   */
174  int EventReader::UnpackPscuHeader(void) throw (std::exception) {  int EventReader::UnpackPscuHeader(void) throw (std::exception) {
175      stringstream oss;
176    int response;    int response;
177    char buff[16];    char buff[16];
178    InputFile->read(buff, sizeof(buff));    InputFile->read(buff, sizeof(buff));
# Line 169  int EventReader::UnpackPscuHeader(void) Line 184  int EventReader::UnpackPscuHeader(void)
184    unsigned int  OrbitalTime  = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) +  (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF);    unsigned int  OrbitalTime  = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) +  (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF);
185    unsigned int  PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) +  (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF);    unsigned int  PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) +  (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF);
186    unsigned char CRC          = buff[15];    unsigned char CRC          = buff[15];
187      unsigned char FileOffset   = buff[15];
188    
189    if (Counter < prevPckCounter){    if (Counter < prevPckCounter){
190      cat <<  log4cpp::Priority::WARN      oss.flush();
191          <<  " Packet counter is less than before of " << (prevPckCounter - Counter)      oss << "Packet counter is less than before of " << (prevPckCounter - Counter);
192          <<  " " << log4cpp::CategoryStream::ENDLINE;      logger->info(oss.str().c_str());
193    }    }
194    
195    if (OrbitalTime < prevPckOBT){    if (OrbitalTime < prevPckOBT){
196        cat <<  log4cpp::Priority::WARN      oss.flush();
197            <<  " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime)      oss << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime);
198            <<  " " << log4cpp::CategoryStream::ENDLINE;      logger->info(oss.str().c_str());
199    }    }
200    
201    
# Line 195  int EventReader::UnpackPscuHeader(void) Line 211  int EventReader::UnpackPscuHeader(void)
211      //plus the CRC legth (which varies for each type of packet)      //plus the CRC legth (which varies for each type of packet)
212      Header->GetPscuHeader()->SetPacketLenght(PacketLenght);      Header->GetPscuHeader()->SetPacketLenght(PacketLenght);
213      Header->GetPscuHeader()->SetCRC(CRC);      Header->GetPscuHeader()->SetCRC(CRC);
214        Header->GetPscuHeader()->SetFileOffset(((long int)(InputFile->tellg()) - 16));
215            
216      //commented out because of the above test code      //commented out because of the above test code
217      InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur);      InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur);
218      FindStart();      FindStart();
219      finalPos =  (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght()));      finalPos =  (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght()));
220          if(finalPos == 0){          if(finalPos == 0){
221              cat <<  log4cpp::Priority::DEBUG <<  " Correct packet length"              logger->debug(_T("Correct packet length"));
                 <<  " " << log4cpp::CategoryStream::ENDLINE;  
222              }              }
223          if (finalPos > 0 && finalPos < 64) {          if (finalPos > 0 && finalPos < 64) {
224              cat <<  log4cpp::Priority::WARN              oss.flush();
225                  <<  " Correct packet length: Padded of " << finalPos << " bytes"              oss << " Correct packet length: Padded of " << finalPos << " bytes";
226                  <<  " " << log4cpp::CategoryStream::ENDLINE;              logger->warn(oss.str().c_str());
227          }          }
228          if (finalPos > 64){          if (finalPos > 64){
229              cat <<  log4cpp::Priority::ERROR <<  " Wrong packet length? (because of wrong padding?) "              logger->warn(_T(" Wrong packet length? (because of wrong padding?)"));
                 <<  " " << log4cpp::CategoryStream::ENDLINE;  
230          }          }
231          InputFile->seekg(initPos, std::ios::beg);          InputFile->seekg(initPos, std::ios::beg);
232          response =  true;          response =  true;
233    } else {    } else {
234      cat <<  log4cpp::Priority::ERROR <<  " WRONG CRC FOR HEADER "      logger->warn(_T(" WRONG CRC FOR HEADER "));
         <<  " " << log4cpp::CategoryStream::ENDLINE;  
235      InputFile->seekg(-(13), std::ios::cur);      InputFile->seekg(-(13), std::ios::cur);
236      response =  false;      response =  false;
237    }    }
# Line 229  int EventReader::UnpackPscuHeader(void) Line 243  int EventReader::UnpackPscuHeader(void)
243      sprintf(tmpId1, "%02X", PacketId1);      sprintf(tmpId1, "%02X", PacketId1);
244      sprintf(tmpId2, "%02X", PacketId2);      sprintf(tmpId2, "%02X", PacketId2);
245      sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght());      sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght());
246      sprintf(tmpStart, "%X", ((long int)(InputFile->tellg()) - 16));      sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset());
247      sprintf(tmpCRC, "%02X", CRC);      sprintf(tmpCRC, "%02X", CRC);
248      cat <<  log4cpp::Priority::INFO        oss.flush();
249          <<  "\n Packet Counter (decimal) : "  <<  Counter      oss << "\n Packet Counter (decimal) : "  <<  Counter
250          <<  "\n Id1 - Id2                : "  <<  tmpId1 <<  " - " <<  tmpId2          <<  "\n Id1 - Id2                : "  <<  tmpId1 <<  " - " <<  tmpId2
251          <<  "\n Orbital Time (decimal)   : "  <<  OrbitalTime          <<  "\n Orbital Time (decimal)   : "  <<  OrbitalTime
252          <<  "\n Lenght                   : "  <<  tmpLength          <<  "\n Lenght                   : "  <<  tmpLength
253          <<  "\n CRC                      : "  <<  tmpCRC          <<  "\n CRC                      : "  <<  tmpCRC
254          <<  "\n Header Start Position    : "  <<  tmpStart          <<  "\n Header Start Position    : "  <<  tmpStart;
255          <<  "\n " << log4cpp::CategoryStream::ENDLINE;      logger->info(oss.str().c_str());
256    return response;    return response;
257  }  }
258    
# Line 265  int EventReader::FindStart(void) throw ( Line 279  int EventReader::FindStart(void) throw (
279  //------------------------------------------  //------------------------------------------
280    int subIndex = 0;    int subIndex = 0;
281    char dataByte;    char dataByte;
282    /*InputFile->read(fileByte, 1);  
283      if ( *fileByte ==  *(subSign+subIndex)){    int  buffSize = 100;
284         if (subIndex++ == (subSignDim-1)) {    int  index = 0;
285          InputFile->seekg(-(subIndex), std::ios::cur);    int  loop = 0;
286          return 1;    char buffer[buffSize];
287        }  
288        }*/    /* original version
289    while (!InputFile->eof()) {    while (!InputFile->eof()) {
290      InputFile->get(dataByte);      InputFile->get(dataByte);
291      if (dataByte == (char)(*(subSign+subIndex))){      if (dataByte == (char)(*(subSign+subIndex))){
# Line 284  int EventReader::FindStart(void) throw ( Line 298  int EventReader::FindStart(void) throw (
298          throw Exception("Extra bytes over the last packets");          throw Exception("Extra bytes over the last packets");
299        } else {        } else {
300          subIndex = 0;          subIndex = 0;
         //return 0;  
301        }        }
       //InputFile->seekg(-(subIndex+1), std::ios::cur);  
       //subIndex = 0;  
302      }      }
303    }    } */
304      while (!InputFile->eof()) {
305        InputFile->read(buffer, sizeof(buffer));
306        index = 0;
307        ++loop;
308        while (index < buffSize){
309            dataByte = buffer[index++];
310            if (dataByte == (char)(*(subSign+subIndex))){
311                if (subIndex++ == (subSignDim-1)) {
312                    InputFile->seekg( (index - subIndex - (buffSize*loop)), std::ios::cur);
313                return 1;
314                }
315            } else {
316                subIndex = 0;
317            }
318        }
319      }
320    return 0;    return 0;
321  }  }
322    

Legend:
Removed from v.1.8  
changed lines
  Added in v.2.2

  ViewVC Help
Powered by ViewVC 1.1.23