/[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.9 by kusanagi, Wed Aug 25 10:14:26 2004 UTC revision 1.10 by kusanagi, Tue Sep 21 20:24:33 2004 UTC
# Line 1  Line 1 
1  /** @file  /** @file
2   * $Author: kusanagi $   * $Author: kusanagi $
3   * $Date: 2004/08/24 13:19:59 $   * $Date: 2004/08/25 10:14:26 $
4   * $Revision: 1.8 $   * $Revision: 1.9 $
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::CalAlarm,        new CalAlarmReader()));
78      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::AcAlarm,         new AcAlarmReader()));
79      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkAlarm,        new TrkAlarmReader()));
80      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgAlarm,        new TrgAlarmReader()));
81      TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofAlarm,        new TofAlarmReader()));
82  }  }
83    
84  /**  /**
# Line 75  EventReader::EventReader(void): Line 86  EventReader::EventReader(void):
86   */   */
87  std::string EventReader::GetVersionInfo(void) const {  std::string EventReader::GetVersionInfo(void) const {
88    return    return
89      "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.8 2004/08/24 13:19:59 kusanagi Exp $\n";      "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.9 2004/08/25 10:14:26 kusanagi Exp $\n";
90  }  }
91    
92  /**  /**
# Line 83  std::string EventReader::GetVersionInfo( Line 94  std::string EventReader::GetVersionInfo(
94   * event reader routines for all packet types.   * event reader routines for all packet types.
95   */   */
96  void EventReader::Init(PamelaRun *run) {  void EventReader::Init(PamelaRun *run) {
97        std::stringstream oss;
98        logger->debug(_T("Initialize"));
99      SetInputStream(run);      SetInputStream(run);
100            
101      //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 104  void EventReader::Init(PamelaRun *run) {
104      //Create the xxx.root in it's specific directory      //Create the xxx.root in it's specific directory
105      for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin();      for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin();
106         i != TechmodelAlgorithmMap.end(); i++) {         i != TechmodelAlgorithmMap.end(); i++) {
107         cat  <<  log4cpp::Priority::DEBUG         oss.flush();
108              <<  " Initializing algo " << i->second->GetAlgorithmName()         oss << "Initializing algo " << i->second->GetAlgorithmName();
109              <<  "\n " << log4cpp::CategoryStream::ENDLINE;         //cout << "Initializing algo " << i->second->GetAlgorithmName();
110           logger->debug(oss.str().c_str());
111         i->second->Init(run);         i->second->Init(run);
112      }      }
113      Run = dynamic_cast<TechmodelPamelaRun*>(run);      Run = dynamic_cast<TechmodelPamelaRun*>(run);
# Line 112  static void SkipToNextHeader(ifstream *) Line 126  static void SkipToNextHeader(ifstream *)
126  // EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later....  // EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later....
127  // 15 June 2004 ---note------------------  // 15 June 2004 ---note------------------
128  void EventReader::RunEvent(int EventNumber) {  void EventReader::RunEvent(int EventNumber) {
     
   cat <<  log4cpp::Priority::INFO <<  "Start Unpacking........"  
       <<  "\n " << log4cpp::CategoryStream::ENDLINE;  
129    //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
130    //to insert a GetPacketStart()    //to insert a GetPacketStart()
131    while (!InputFile->eof()){    stringstream oss;
132      int step = 0;
133      while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){
134      try {      try {
135        if (FindStart()) {        if (FindStart()) {
136          if(UnpackPscuHeader()){          if(UnpackPscuHeader()){
# Line 129  void EventReader::RunEvent(int EventNumb Line 142  void EventReader::RunEvent(int EventNumb
142                  Run->FillTrees(type);                  Run->FillTrees(type);
143                  Header->GetCounter()->Increment(type);                  Header->GetCounter()->Increment(type);
144              } else {              } else {
145              cat <<  log4cpp::Priority::ERROR                  oss.flush();
146                  <<  "No way to read events of type" << type->GetName()                  oss << "No way to read events of type" << type->GetName();
147                  <<  "\n " << log4cpp::CategoryStream::ENDLINE;                  logger->info(oss.str().c_str());
148              throw Exception("No way to read events of type " + type->GetName());              throw Exception("No way to read events of type " + type->GetName());
149          }          }
150        } //else {        } //else {
# Line 141  void EventReader::RunEvent(int EventNumb Line 154  void EventReader::RunEvent(int EventNumb
154       }       }
155      } catch (...) {      } catch (...) {
156        //This have to be more detailed. More exceptions type are needed.        //This have to be more detailed. More exceptions type are needed.
157        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;  
158      }      }
159        if ((step%1000) == 0) std::cout << step << "K \n";
160    }    }
161    Header->GetCounter()->PrintCounters();      Header->GetCounter()->PrintCounters();
162  }  }
163    
164  static void SkipToNextHeader(ifstream* TechmodelFile) {  static void SkipToNextHeader(ifstream* TechmodelFile) {
# Line 158  static void SkipToNextHeader(ifstream* T Line 171  static void SkipToNextHeader(ifstream* T
171   * Unpack the PSCU header from a file into the structure.   * Unpack the PSCU header from a file into the structure.
172   */   */
173  int EventReader::UnpackPscuHeader(void) throw (std::exception) {  int EventReader::UnpackPscuHeader(void) throw (std::exception) {
174      stringstream oss;
175    int response;    int response;
176    char buff[16];    char buff[16];
177    InputFile->read(buff, sizeof(buff));    InputFile->read(buff, sizeof(buff));
# Line 172  int EventReader::UnpackPscuHeader(void) Line 186  int EventReader::UnpackPscuHeader(void)
186    unsigned char FileOffset   = buff[15];    unsigned char FileOffset   = buff[15];
187    
188    if (Counter < prevPckCounter){    if (Counter < prevPckCounter){
189      cat <<  log4cpp::Priority::WARN      oss.flush();
190          <<  " Packet counter is less than before of " << (prevPckCounter - Counter)      oss << "Packet counter is less than before of " << (prevPckCounter - Counter);
191          <<  " " << log4cpp::CategoryStream::ENDLINE;      logger->info(oss.str().c_str());
192    }    }
193    
194    if (OrbitalTime < prevPckOBT){    if (OrbitalTime < prevPckOBT){
195        cat <<  log4cpp::Priority::WARN      oss.flush();
196            <<  " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime)      oss << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime);
197            <<  " " << log4cpp::CategoryStream::ENDLINE;      logger->info(oss.str().c_str());
198    }    }
199    
200    
# Line 203  int EventReader::UnpackPscuHeader(void) Line 217  int EventReader::UnpackPscuHeader(void)
217      FindStart();      FindStart();
218      finalPos =  (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght()));      finalPos =  (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght()));
219          if(finalPos == 0){          if(finalPos == 0){
220              cat <<  log4cpp::Priority::DEBUG <<  " Correct packet length"              logger->debug(_T("Correct packet length"));
                 <<  " " << log4cpp::CategoryStream::ENDLINE;  
221              }              }
222          if (finalPos > 0 && finalPos < 64) {          if (finalPos > 0 && finalPos < 64) {
223              cat <<  log4cpp::Priority::WARN              oss.flush();
224                  <<  " Correct packet length: Padded of " << finalPos << " bytes"              oss << " Correct packet length: Padded of " << finalPos << " bytes";
225                  <<  " " << log4cpp::CategoryStream::ENDLINE;              logger->warn(oss.str().c_str());
226          }          }
227          if (finalPos > 64){          if (finalPos > 64){
228              cat <<  log4cpp::Priority::ERROR <<  " Wrong packet length? (because of wrong padding?) "              logger->warn(_T(" Wrong packet length? (because of wrong padding?)"));
                 <<  " " << log4cpp::CategoryStream::ENDLINE;  
229          }          }
230          InputFile->seekg(initPos, std::ios::beg);          InputFile->seekg(initPos, std::ios::beg);
231          response =  true;          response =  true;
232    } else {    } else {
233      cat <<  log4cpp::Priority::ERROR <<  " WRONG CRC FOR HEADER "      logger->warn(_T(" WRONG CRC FOR HEADER "));
         <<  " " << log4cpp::CategoryStream::ENDLINE;  
234      InputFile->seekg(-(13), std::ios::cur);      InputFile->seekg(-(13), std::ios::cur);
235      response =  false;      response =  false;
236    }    }
# Line 233  int EventReader::UnpackPscuHeader(void) Line 244  int EventReader::UnpackPscuHeader(void)
244      sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght());      sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght());
245      sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset());      sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset());
246      sprintf(tmpCRC, "%02X", CRC);      sprintf(tmpCRC, "%02X", CRC);
247      cat <<  log4cpp::Priority::INFO        oss.flush();
248          <<  "\n Packet Counter (decimal) : "  <<  Counter      oss << "\n Packet Counter (decimal) : "  <<  Counter
249          <<  "\n Id1 - Id2                : "  <<  tmpId1 <<  " - " <<  tmpId2          <<  "\n Id1 - Id2                : "  <<  tmpId1 <<  " - " <<  tmpId2
250          <<  "\n Orbital Time (decimal)   : "  <<  OrbitalTime          <<  "\n Orbital Time (decimal)   : "  <<  OrbitalTime
251          <<  "\n Lenght                   : "  <<  tmpLength          <<  "\n Lenght                   : "  <<  tmpLength
252          <<  "\n CRC                      : "  <<  tmpCRC          <<  "\n CRC                      : "  <<  tmpCRC
253          <<  "\n Header Start Position    : "  <<  tmpStart          <<  "\n Header Start Position    : "  <<  tmpStart;
254          <<  "\n " << log4cpp::CategoryStream::ENDLINE;      logger->info(oss.str().c_str());
255    return response;    return response;
256  }  }
257    
# Line 267  int EventReader::FindStart(void) throw ( Line 278  int EventReader::FindStart(void) throw (
278  //------------------------------------------  //------------------------------------------
279    int subIndex = 0;    int subIndex = 0;
280    char dataByte;    char dataByte;
281    /*InputFile->read(fileByte, 1);  
282      if ( *fileByte ==  *(subSign+subIndex)){    int  buffSize = 100;
283         if (subIndex++ == (subSignDim-1)) {    int  index = 0;
284          InputFile->seekg(-(subIndex), std::ios::cur);    int  loop = 0;
285          return 1;    char buffer[buffSize];
286        }  
287        }*/    /* original version
288    while (!InputFile->eof()) {    while (!InputFile->eof()) {
289      InputFile->get(dataByte);      InputFile->get(dataByte);
290      if (dataByte == (char)(*(subSign+subIndex))){      if (dataByte == (char)(*(subSign+subIndex))){
# Line 286  int EventReader::FindStart(void) throw ( Line 297  int EventReader::FindStart(void) throw (
297          throw Exception("Extra bytes over the last packets");          throw Exception("Extra bytes over the last packets");
298        } else {        } else {
299          subIndex = 0;          subIndex = 0;
         //return 0;  
300        }        }
       //InputFile->seekg(-(subIndex+1), std::ios::cur);  
       //subIndex = 0;  
301      }      }
302    }    } */
303      while (!InputFile->eof()) {
304        InputFile->read(buffer, sizeof(buffer));
305        index = 0;
306        ++loop;
307        while (index < buffSize){
308            dataByte = buffer[index++];
309            if (dataByte == (char)(*(subSign+subIndex))){
310                if (subIndex++ == (subSignDim-1)) {
311                    InputFile->seekg( (index - subIndex - (buffSize*loop)), std::ios::cur);
312                return 1;
313                }
314            } else {
315                subIndex = 0;
316            }
317        }
318      }
319    return 0;    return 0;
320  }  }
321    

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.23