DiracReader Class Reference

#include <DiracReader.hh>

Inheritance diagram for DiracReader:

Inheritance graph
[legend]
Collaboration diagram for DiracReader:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 DiracReader (Run &aRun, FILE *aFile, ui32 firstEventId)
 ~DiracReader ()
int getNextEvent (Event &eventToFill)

Static Public Member Functions

static const char * type (const int subtype=0)

Protected Attributes

unsigned short CRC
int nbDifs
std::vector< DiracDifConfigdifConfigs
i64 bcId_AbsOrg
int acqDifId
DiracTriggerInfo trigInfos [8]

Private Member Functions

void reset ()
int getConfigData ()
int getAcqData ()
int storeHits (Event &event)
ChannelHitnewHit (Event &event, const DiracDifConfig &difConfig, const DiracTriggerInfo &trigInfo, const int chipNum, const int chNum)
void storeDifInfo (const int difId)

Detailed Description

CLASS DATAREADER //

Definition at line 108 of file DiracReader.hh.


Constructor & Destructor Documentation

DiracReader::DiracReader ( Run aRun,
FILE *  aFile,
ui32  firstEventId 
)

Definition at line 228 of file DiracReader.cpp.

00228                                                                    : DifReader(aRun, aFile, _firstEventId) {
00229     reset();
00230 }

DiracReader::~DiracReader (  ) 

Definition at line 234 of file DiracReader.cpp.

00234                           {
00235 } // ~DiracReader


Member Function Documentation

int DiracReader::getNextEvent ( Event eventToFill  )  [virtual]

Implements AcquisitionParser.

Definition at line 524 of file DiracReader.cpp.

00524                                           {
00525     // Start data reconstruction
00526     Level(0);
00527 
00528                 i16 dataFormat = run.getDataFormat();
00529     if (dataFormat == -1) {
00530         // first read of the file
00531         try
00532         {
00533             bcIdDifAbsOrgMap.clear();
00534         }
00535 
00536         catch(exception & e) {
00537             FILE_LOG(logERROR) << "bcIdDifAbsOrgMap.clear():" << e.what() << endl;
00538         }
00539         // read file header
00540         dataFormat = getBinData(1);
00541                                 run.setDataFormat(dataFormat);
00542         if (fileError == EOF) {
00543             FILE_LOG(logDEBUG) << "end of file found" << endl;
00544             reset();
00545             return (LAST_FILE_EVENT);
00546         }
00547 
00548         if (dataFormat != 1) {
00549             // unknown format
00550             FILE_LOG(logERROR) << "DIRAC data format " << dataFormat << " is not implemented" << endl;
00551             reset();
00552             return (LAST_FILE_EVENT);
00553         }
00554 
00555         nbDifs = getBinData(1);
00556         display(+1);
00557         FILE_LOG(logDEBUG) << "DIRAC data format : " << dataFormat << ", nb Dif = " << nbDifs << endl;
00558 
00559         for (int difNum = 0; difNum < nbDifs; difNum++) {
00560             // read DIF configuration
00561             DiracDifConfig difConfig;
00562             unsigned char buf[21];
00563             getBinData(20, buf);
00564             display();
00565 
00566             difConfig.read(buf);
00567             difConfigs.push_back(difConfig);
00568 
00569             // store data into Run info
00570             storeDifInfo(difConfig.id);
00571 
00572             // store info for the whole run
00573             run.setInjectedCharge(difConfigs[0].charge);
00574             run.setWidthCtest(difConfigs[0].ctestWidth);
00575             run.setPeriodCtest(difConfigs[0].ctestPeriod);
00576             run.setDelayCtest(difConfigs[0].ctestDelay);
00577             run.setDelayTrigger(difConfigs[0].triggerDelay);
00578         }
00579         display(-1);
00580 
00581         // get unix timestamp for the whole file (seconds since jan 1st, 1970)
00582         globalTime = getBinData(4);
00583         display();
00584         FILE_LOG(logDEBUG1) << "global time : " << globalTime << endl;
00585         return NEW_CONFIG;
00586     }
00587 
00588     for (unsigned int nbHits = 0; !nbHits;) {
00589         unsigned char header = getBinData(1);
00590         if (fileError == EOF) {
00591             FILE_LOG(logDEBUG) << "  END OF DATA FILE" << endl;
00592             reset();
00593             return (LAST_FILE_EVENT);
00594         }
00595 
00596         if (header == 0xB1) {
00597             // store all configuration data
00598             display(+1);
00599             getConfigData();
00600             if (getBinData(1) != 0xA1) {
00601                 display(-1);
00602                 FILE_LOG(logERROR) << "    frame trailer A1 not found" << endl;
00603                 break;
00604             }
00605             display(-1);
00606         } else if (header == 0xB0) {
00607             // store all acquisition data
00608             display(+1);
00609             getAcqData();
00610             if (getBinData(1) != 0xA0) {
00611                 display(-1);
00612                 FILE_LOG(logERROR) << "    frame trailer A0 not found" << endl;
00613                 break;
00614             }
00615             display(-1);
00616             CRC = getBinData(2);
00617             display();
00618 
00619             // then store all data
00620             nbHits = storeHits(event);
00621         } else {
00622             display();
00623             FILE_LOG(logERROR) << "    expected header not found" << endl;
00624         }
00625 
00626         if (fileError == EOF)
00627             break;
00628     }
00629 
00630     return EVENT_CORRECT;
00631 } // DiracReader.getNextEvent

static const char* DiracReader::type ( const int  subtype = 0  )  [inline, static]

Definition at line 115 of file DiracReader.hh.

Referenced by main().

00115 { return LABVIEW_DIRAC_READER; };

void DiracReader::reset (  )  [private]

Definition at line 239 of file DiracReader.cpp.

Referenced by DiracReader(), and getNextEvent().

00239                         {
00240     nbDifs = 0;
00241     CRC = 0;
00242     bcId_AbsOrg = -1; // the bcId origin
00243 } // DiracReader.reset

int DiracReader::getConfigData (  )  [private]

Definition at line 248 of file DiracReader.cpp.

Referenced by getNextEvent().

00248                                {
00249     int difId = getBinData(1); // read 8 bits
00250 
00251     for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00252         DiracDifConfig& difConfig = difConfigs[difNum];
00253         if (difConfig.id == difId) {
00254             difConfig.nbChips = getBinData(1); // read 8 bits
00255 
00256             display();
00257             FILE_LOG(logDEBUG1) << " DIF #" << difConfig.id << " : " << difConfig.nbChips << " chips" << endl;
00258 
00259             for (int chipNum = 0; chipNum < difConfig.nbChips; chipNum++) {
00260                 DiracChipConfig chipConfig;
00261 
00262                 unsigned char buf[24];
00263                 getBinData(23, buf);
00264                 display();
00265                 chipConfig.read(buf);
00266                 // InitGetBits(23);
00267                 // chipconfg.read();   <--- GetBits(n);
00268                 // FinishGetBits();
00269 
00270                 difConfig.chipConfigs.push_back(chipConfig);
00271 
00272                 const Detector &detector = run.getDetector();
00273                 try
00274                 {
00275                     Dif& dif = detector.getDifById(difId);
00276                     BoardMap_t boards = dif.getBoards();
00277                     for (BoardMap_t::iterator boardIt = boards.begin(); boardIt != boards.end(); boardIt++) {
00278                         Board& b = *boardIt->second;
00279                         try
00280                         {
00281                             DiracChip& chip = dynamic_cast<DiracChip&> (b.getChipById(chipNum));
00282                             chip.setBLoThreshold(chipConfig.bLoThreshold);
00283                             chip.setBMidThreshold(chipConfig.bMidThreshold);
00284                             chip.setBHiThreshold(chipConfig.bHiThreshold);
00285                             chip.setALoThreshold(chipConfig.aLoThreshold);
00286                             chip.setAMidThreshold(chipConfig.aMidThreshold);
00287                             chip.setAHiThreshold(chipConfig.aHiThreshold);
00288                             chip.setGain(chipConfig.gain);
00289                             for (int chNum = 0; chNum < 64; chNum++) {
00290                                 Channel& ch = chip.getChannelById(chNum);
00291                                 ch.setEnable(chipConfig.chConfig[chNum].enable);
00292                                 ch.setStimulate(chipConfig.chConfig[chNum].test);
00293                             }
00294                             break;
00295                         }
00296 
00297                         catch(...) {
00298                         }
00299                     }
00300                 }
00301 
00302                 catch(...) {
00303                 }
00304             }
00305 
00306             if (difConfigs.size() > nbDifs) {
00307                 FILE_LOG(logERROR) << "DIF config error" << endl;
00308                 return (0);
00309             }
00310             return (1);
00311         } // if
00312     } // for
00313 
00314     FILE_LOG(logERROR) << "DIF #" << difId << " not found" << endl;
00315     return (0);
00316 } // DiracReader.getConfigData

int DiracReader::getAcqData (  )  [private]

Definition at line 320 of file DiracReader.cpp.

Referenced by getNextEvent().

00320                             {
00321     acqDifId = getBinData(1); // read 8 bits
00322     unsigned int nbTotalTrigger = getBinData(4);
00323     unsigned int nbTrigger = getBinData(4);
00324     display();
00325 
00326     FILE_LOG(logDEBUG) << "  data for DIF #" << acqDifId << ", " << nbTotalTrigger << " total, " << nbTrigger << " triggers" << endl;
00327 
00328     // look for current DIF configuration
00329     for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00330         const DiracDifConfig& difConfig = difConfigs[difNum];
00331         if (difConfig.id == acqDifId) {
00332             // read 8 trigger informations (10 bytes each)
00333             for (int trigInfoNum = 0; trigInfoNum < 8; trigInfoNum++) {
00334                 unsigned char buf[11];
00335                 getBinData(10, buf);
00336                 display();
00337                 trigInfos[trigInfoNum].read(buf);
00338             }
00339 
00340             // set the origin for absolute bcId
00341             //      if (bcId_AbsOrg == -1) {
00342             //        // set the absolute bcId origin
00343             //        bcId_AbsOrg = trigInfos[0].bcIdAbs;
00344             //        FILE_LOG(logDEBUG)   << "    set bc origin         " << bcId_AbsOrg << endl;
00345             //      }
00346             bcId_AbsOrg = getbcIdAbsOrg(acqDifId, trigInfos[0].bcIdAbs);
00347             FILE_LOG(logDEBUG) << " getbcIdAbsOrg(" << acqDifId << " , trigInfos[0].bcIdAbs)  " << getbcIdAbsOrg(acqDifId, trigInfos[0].bcIdAbs) << endl;
00348 
00349             // the following is sent for every chip for the given DIF
00350             for (int chipNum = 0; chipNum < difConfig.nbChips; chipNum++) {
00351                 unsigned int header = getBinData(1);
00352                 if (header != 0xB4) {
00353                     FILE_LOG(logERROR) << "parsing file error : header 0xB4 not found" << endl;
00354                     return (0);
00355                 }
00356                 display(+1);
00357 
00358                 // get 8 bcIds (96 bits = 12 bytes) <-> 8 triggers * 12-bit bcId
00359                 for (int bitNum = 11; bitNum >= 0; bitNum--) {
00360                     const unsigned int data = getBinData(1);
00361                     unsigned int mask = 0x80;
00362                     for (int trigNum = 0; trigNum < 8; trigNum++, mask >>= 1) {
00363                         DiracTriggerInfo& trigInfo = trigInfos[trigNum];
00364                         unsigned int bit = ((data & mask) != 0) ? 1 : 0;
00365                         trigInfo.bcId |= bit << bitNum;
00366                     }
00367                 }
00368                 display();
00369 
00370                 // get 8 channel data (1024 bits = 128 bytes) <-> 8 triggers * 64 channels * 2-bit data
00371                 for (int chNum = 63; chNum >= 0; chNum--) {
00372                     for (int bitNum = 0; bitNum < 2; bitNum++) {
00373                         const unsigned int data = getBinData(1);
00374                         unsigned int mask = 0x80;
00375                         for (int trigNum = 0; trigNum < 8; trigNum++, mask >>= 1) {
00376                             DiracTriggerInfo& trigInfo = trigInfos[trigNum];
00377                             unsigned int bit = ((data & mask) != 0) ? 1 : 0;
00378                             trigInfo.data[chNum] |= bit << bitNum;
00379                         }
00380                     }
00381                     if (chNum == 32)
00382                         display();
00383                 }
00384                 display();
00385 
00386                 header = getBinData(1);
00387                 display(-1);
00388                 if (header != 0xA3) {
00389                     FILE_LOG(logERROR) << "parsing file error : trailer 0xA3 not found" << endl;
00390                     return (0);
00391                 }
00392             } // for chipNum
00393 
00394             return (1);
00395         } // if
00396     } // for difNum
00397 
00398     return (0);
00399 } // DiracReader.getAcqData

int DiracReader::storeHits ( Event event  )  [private]

Definition at line 403 of file DiracReader.cpp.

Referenced by getNextEvent().

00403                                        {
00404     int nbHits = 0;
00405 
00406     for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00407         const DiracDifConfig& difConfig = difConfigs[difNum];
00408         if (difConfig.id == acqDifId) {
00409             for (int chipNum = 0; chipNum < difConfig.nbChips; chipNum++) {
00410 
00411                 for (int trigNum = 0; trigNum < 8; trigNum++) {
00412                     const DiracTriggerInfo& trigInfo = trigInfos[trigNum];
00413 
00414                     for (int chNum = 0; chNum < 64; chNum++) {
00415                         if (trigInfo.data[chNum]) {
00416                             if (newHit(event, difConfig, trigInfo, chipNum, chNum))
00417                                 nbHits++;
00418                         }
00419                     }
00420                 }
00421             } // for chipNum
00422 
00423             if (nbHits) {
00424                 // CREATE EVENT for current Run
00425                 event.setId(++lastEventId);
00426                 FILE_LOG(logDEBUG1) << "  STORE EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00427             } else
00428                 FILE_LOG(logDEBUG) << "  EMPTY EVENT" << endl;
00429             return (nbHits);
00430         } // if
00431     } // for difNum
00432 
00433     return (0);
00434 } // DiracReader.storeHits

ChannelHit * DiracReader::newHit ( Event event,
const DiracDifConfig difConfig,
const DiracTriggerInfo trigInfo,
const int  chipNum,
const int  chNum 
) [private]

Definition at line 438 of file DiracReader.cpp.

Referenced by storeHits().

00438                                                                                                                                                    {
00439     const Detector &detector = run.getDetector();
00440     const ChamberMap_t &chambers = detector.getChambers();
00441     for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00442         try
00443         {
00444             DiracChamber1& chamber = dynamic_cast<DiracChamber1&> (*(it->second));
00445             const BoardMap_t& boards = chamber.getBoardsByDifId(difConfig.id);
00446             // store new event data
00447             try
00448             {
00449                 const Channel& channel = chamber.getChannelById(chNum, chipNum, difConfig.id);
00450                 const int data = trigInfo.data[chNum];
00451                 ChannelHit *hit = new ChannelHit(channel, data, 0);
00452                 // all in bcPeriods - this depends on the acquisition mode
00453                 i64 bcId_Abs; // absolute bc Id for current DIF ALWAYS @5MHz
00454                 i64 bcId_Dif; // current bcId for current DIF
00455                 i64 bcId_Hit; // bcId of current hit
00456                 i64 bcId_Hit_Fine = trigInfos[0].trigger;
00457 
00458                 //        if (difConfig.acqMode == 0x0A) {
00459                 bcId_Abs = trigInfos[0].bcIdAbs;
00460                 bcId_Dif = trigInfos[0].bcIdFpga;
00461                 //        }
00462                 //        else {
00463                 //          bcId_Abs = trigInfo.bcIdAbs;
00464                 //          bcId_Dif = trigInfo.bcIdFpga;
00465                 //        }
00466 
00467                 bcId_Hit = trigInfo.bcId;
00468 
00469                 hit->setHardrocTime(globalTime, bcId_Abs, bcId_Dif, bcId_Hit,bcId_Hit_Fine);
00470                 event.insertHit(chamber, hit);
00471                 i64 timeStamp = (globalTime * 1000) // all in milliseconds
00472                         + ((trigInfo.bcIdAbs - bcId_AbsOrg) / 5000000) // ALWAYS @5MHz
00473                         + (((trigInfo.bcId - trigInfo.bcIdFpga) * difConfig.bcPeriod) / 1000);
00474                 event.setTimeStamp(timeStamp);
00475                 FILE_LOG(logDEBUG) << "bcId_Abs[" << bcId_Abs << "] - bcId_AbsOrg[" << bcId_AbsOrg << "] = [" << bcId_Abs - bcId_AbsOrg << "]" << endl;
00476                 FILE_LOG(logDEBUG) << "  new Hit : ch " << chNum << ", chip " << chipNum << ", dif " << difConfig.id
00477                         << ", Time=" << timeStamp << "ms, bcId:abs=" << (bcId_Abs - bcId_AbsOrg) << ",dif=" << bcId_Dif << ",hit=" << bcId_Hit << " Data=" << data << endl;
00478                 return (hit);
00479             }
00480 
00481             catch(MicroException & e) {
00482                 FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipNum << ", dif " << difConfig.id << " : " << e.getMessage() << endl;
00483                 return (0);
00484             }
00485         }
00486 
00487         catch(...) {
00488             continue;
00489         }
00490 
00491     } // for
00492     FILE_LOG(logERROR) << "ch " << chNum << ", chip " << chipNum << ", dif " << difConfig.id << " : geometry element not found" << endl;
00493     return (0);
00494 } // DiracReader.newHit

void DiracReader::storeDifInfo ( const int  difId  )  [private]

Definition at line 498 of file DiracReader.cpp.

Referenced by getNextEvent().

00498                                               {
00499     const Detector &detector = run.getDetector();
00500     try
00501     {
00502         Dif& dif = detector.getDifById(difId);
00503         for (int difNum = 0; difNum < difConfigs.size(); difNum++) {
00504             DiracDifConfig& difConfig = difConfigs[difNum];
00505             if (difConfig.id == difId) {
00506                 dif.setAcqMode(difConfig.acqMode);
00507                 dif.setLowRegister(difConfig.lowState);
00508                 dif.setHighRegister(difConfig.highState);
00509                 dif.setAfterRegister(difConfig.afterState);
00510                 dif.setBeforeRegister(difConfig.beforeState);
00511                 return;
00512             }
00513         }
00514     }
00515 
00516     catch(...) {
00517     };
00518 }; // DiracReader.storeDifInfo


Member Data Documentation

unsigned short DiracReader::CRC [protected]

Definition at line 128 of file DiracReader.hh.

Referenced by getNextEvent(), and reset().

int DiracReader::nbDifs [protected]

Definition at line 131 of file DiracReader.hh.

Referenced by getConfigData(), getNextEvent(), and reset().

std::vector<DiracDifConfig> DiracReader::difConfigs [protected]

Definition at line 132 of file DiracReader.hh.

Referenced by getAcqData(), getConfigData(), getNextEvent(), storeDifInfo(), and storeHits().

i64 DiracReader::bcId_AbsOrg [protected]

Definition at line 135 of file DiracReader.hh.

Referenced by getAcqData(), newHit(), and reset().

int DiracReader::acqDifId [protected]

Definition at line 136 of file DiracReader.hh.

Referenced by getAcqData(), and storeHits().

DiracTriggerInfo DiracReader::trigInfos[8] [protected]

Definition at line 137 of file DiracReader.hh.

Referenced by getAcqData(), newHit(), and storeHits().


The documentation for this class was generated from the following files:
Generated on Mon Jan 7 13:18:16 2013 for MicromegasFramework by  doxygen 1.4.7