/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/parser/f12MicrorocXDaqReader.cpp

Go to the documentation of this file.
00001 /* @version $Revision: 1624 $ * @modifiedby $Author: lcdet $ * @lastmodified $Date: 2012-03-30 14:49:30 +0200 (Fri, 30 Mar 2012) $ */
00002 
00003 ////////////////////////////////////////////////////////////////////////
00004 // MICROMEGAS
00005 //
00006 //       EVENT RECONSTRUCTION SOFTWARE FOR MICROROC BOARDS
00007 //
00008 ////////////////////////////////////////////////////////////////////////
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <errno.h>
00013 
00014 #include "parser/MicrorocXDaqReader.hh"
00015 #include "parser/MicrorocSCReader.hh"
00016 #include "parser/AcquisitionParser.hh"
00017 
00018 #include "event/Event.hh"
00019 
00020 #include "geometry/Chamber.hh"
00021 #include "geometry/Board.hh"
00022 #include "geometry/Dif.hh"
00023 #include "geometry/MicrorocChip.hh"
00024 #include "geometry/Detector.hh"
00025 
00026 #include "tools/Toolbox.hh"
00027 #include "tools/SteerDesc.hh"
00028 #include "tools/Log.hh"
00029 #include "tools/MicroException.hh"
00030 
00031 
00032 #include "lcio.h"
00033 #include "IO/LCReader.h"
00034 #include "IMPL/LCTOOLS.h"
00035 #include "IMPL/LCGenericObjectImpl.h"
00036 #include "EVENT/LCCollection.h"
00037 #include "EVENT/LCRunHeader.h"
00038 
00039 #include "geometry/MicrorocChannel.hh"
00040 
00041 #include "ilcConfDb/IlcConfDb.hh"
00042 
00043 
00044 #include <iostream>
00045 #include <iomanip>
00046 
00047 
00048 #define NBBYTETOSKIP  94
00049 
00050 using namespace std;
00051 using namespace lcio ;
00052 
00053 
00054 ////////////////////////////////////////////////////////////////////////
00055 MicrorocXDaqReader::MicrorocXDaqReader(Run& aRun, string aFileFullPath, ui32 lastEventId, set<unsigned int>*_difList) : MicrorocReader(aRun, NULL, lastEventId), nbChipPerLine(0), analogDataActivate(false), firstEvent(true), slowControlSet(false), difList(_difList)
00056 
00057 {
00058   FILE_LOG(logDEBUG1) << "--MicrorocXDaqReader constructor()"<< endl;
00059 
00060   lcReader = LCFactory::getInstance()->createLCReader() ;
00061   try {
00062     lcReader->open(aFileFullPath);
00063 /* Seems to be not set by XDAQ
00064     EVENT::LCRunHeader *runHeader = lcReader->readNextRunHeader();
00065     if ( runHeader != NULL ) 
00066     {
00067       cout << "-------------runNumber: " << runHeader->getRunNumber() << endl;
00068       cout << "-------------runDetectorName: " << runHeader->getDetectorName() <<endl;
00069       cout << "-------------runDescription: " << runHeader->getDescription() <<endl;
00070       exit (10);
00071     }
00072     else { cout << "runHeader == NULL: "  << endl; }
00073 */
00074   }
00075   catch( IOException& e) {
00076     cout << e.what() << endl ;
00077     exit(-1) ;
00078   }
00079   
00080   if ( lcReader == NULL ) { exit(-1); }
00081 
00082   for ( ui16 line = 0 ; line < NBLINE ; line++)
00083   {
00084     analogDataActivatedLine[line] = false;
00085   }
00086 
00087   localReset();
00088 
00089 }
00090 
00091 //--------------- Destructeur -------------//
00092 MicrorocXDaqReader::~MicrorocXDaqReader()
00093 {
00094   lcReader->close();
00095   delete lcReader;
00096   FILE_LOG(logDEBUG1)<< "----MicrorocXDaqReader destructor"<< endl;
00097 }
00098                                                                                                              
00099 //--------------- reset -------------//
00100 void MicrorocXDaqReader::localReset()
00101 {
00102   reset(); // appel MicrorocReader::reset();
00103   slowControlSet = false;
00104   rawdataList.clear();
00105 }
00106 //------------------------------------------//
00107 int MicrorocXDaqReader::getScData()
00108 {
00109 return 0;
00110 }
00111 //------------------------------------------//
00112 int MicrorocXDaqReader::getNextEvent(Event& event) 
00113 {
00114   Level(0);
00115   ui16 dataFormat = run.getDataFormat();
00116 
00117   if (dataFormat == 0) {
00118     // first read of the file
00119     bcIdDifAbsOrgMap.clear();
00120     // read file header
00121     dataFormat = 1;
00122     run.setDataFormat(dataFormat);
00123     //FILE_LOG(logINFO) << " labview data format[" << hex << dataFormat << "] "<< endl;
00124     //run.setDaqSoftware(333);
00125     //run.setCccFirmware(333);
00126     //globalTime = 333;
00127     
00128     return NEW_CONFIG;
00129   } // end dataFormat == 0
00130 
00131   // loops until a non-empty event has been found
00132   bool eventFound = false;
00133   while (! eventFound)
00134   {
00135     LCEvent* lcioEvt(0) ;
00136     lcioEvt = lcReader->readNextEvent() ;
00137     if ( lcioEvt == NULL ) // EOF reach
00138     {
00139       return LAST_FILE_EVENT;
00140     }
00141 
00142 
00143     // There is one more LCEvent
00144     //LCTOOLS::dumpEvent( lcioEvt ) ;
00145     if ( ! slowControlSet )
00146     {
00147       // Read Slow control from database
00148       if ( event.getRun().getId() == 0 )
00149       {  // try to find run Id in lcioEvt
00150         run.setId(lcioEvt->getRunNumber());
00151         FILE_LOG(logINFO) << "Set run Id from lcioEvent information[" << run.getId() << "]" << endl;
00152       }
00153       globalTime = lcioEvt->getTimeStamp();
00154 
00155       run.setDaqSoftware(0);
00156       run.setCccFirmware(0);
00157 
00158       IlcConfDb *ilcconf = IlcConfDb::getInstance();
00159       if ( ilcconf != NULL )
00160       {
00161         MicrorocSCReader scReader(run);
00162         scReader.parseSCFromDb(*ilcconf);
00163         slowControlSet = true;
00164       }
00165 
00166     }
00167     
00168     if ( firstEvent )
00169     {
00170       FILE_LOG(logINFO) << "firstEvent  skip"  << endl;
00171       firstEvent = false;
00172       // skip first event because of its corruption ???
00173     }
00174     else
00175     {
00176       // vnames to store lcioEvt collections name
00177       std::vector<std::string >* vnames;
00178       try
00179       {
00180         vnames= (std::vector<std::string >*)lcioEvt->getCollectionNames();
00181         /*
00182         cout << "run number: " << lcioEvt->getRunNumber()<< endl;
00183         cout << "event number: " << lcioEvt->getEventNumber()<< endl;
00184         cout << "timestamp: " << lcioEvt->getTimeStamp()<< endl;
00185         cout << "detectorName: " << lcioEvt->getDetectorName()<< endl;
00186         cout << "weight: " << lcioEvt->getWeight()<< endl;
00187         */
00188         for ( vector<std::string >::iterator it = vnames->begin();  it!= vnames->end(); it++)
00189         {
00190           if ((*it).compare("RU_XDAQ")==0)
00191           {
00192             EVENT::LCCollection* col= lcioEvt->getCollection(*it);
00193     //        cout << "collection name:["<< *it << "]"  << endl;
00194 
00195             for (int difRawDataIndex=0;difRawDataIndex<col->getNumberOfElements(); difRawDataIndex++)
00196             {
00197               IMPL::LCGenericObjectImpl* go= (IMPL::LCGenericObjectImpl*) col->getElementAt(difRawDataIndex);
00198               int rusize =go->getNInt();
00199      //         std::cout<<" New RU found with "<<rusize<<" int stored" <<std::endl;
00200               if ( rusize == 56 ) continue; // skip first RU_XDAQ si tsille est 56 int
00201 
00202               int shift=-1;
00203               ui16 nbInt = go->getNInt();
00204       //        cout << "DEBUG ---nbInt [" << dec << nbInt << "]-----------------" <<endl;
00205 
00206               
00207               unsigned int buf[go->getNInt()];
00208 
00209               for (int k=0; k < (go->getNInt() );k++) { buf[k] = go->getIntVal(k); }
00210               // skip first NBBYTETOSKIP ( 94 ) bytes 
00211               // then fill rawdataList with unsigned char
00212               unsigned char* bufChar = ( unsigned char* ) &(buf); 
00213               ui16 rapport = (ui16) (sizeof(unsigned int ) / sizeof ( unsigned char) );
00214 
00215               rawdataList.clear();
00216               for ( ui32 j = NBBYTETOSKIP ; j < go->getNInt()*rapport ; j++ )
00217               {
00218                 rawdataList.push_back(*(bufChar+j));
00219               }
00220               
00221               itRawdata = rawdataList.begin();
00222               unsigned int globalHeader = getData(1);
00223        //       cout << "globalHeader[" << hex << globalHeader <<  "]" << endl;
00224 
00225               if (globalHeader == 0xB1) {
00226                 // slow control data
00227                 getScData();
00228                 exit(-1); // Conf in Oracle DB not in raw data
00229               }
00230               else if (globalHeader == 0xC0) {
00231                 // acquisition data
00232                 getAnalogData(event);
00233               }
00234               else if (globalHeader == 0xB0) {
00235                 // acquisition data
00236                 getAcqData(event,bufChar);
00237                 //eventFound = true;
00238               }
00239               else {
00240                 FILE_LOG(logERROR) << "  parsing file error. global header error : " << hex << globalHeader << dec << endl;
00241               }
00242             } // end for data for dif
00243             eventFound = true ;
00244             event.setId(++lastEventId);
00245           } // ed if collection = XU_XDAQ
00246           /*
00247           else  if ((*it).compare("DHCALRawHits")==0)  
00248           {
00249             std::cout << "--- Found Collection DHCALRawHits" << endl;
00250             EVENT::LCCollection* col= lcioEvt->getCollection("DHCALRawHits");
00251             std::cout << "--- col lcioEvt->getCollection(\"DHCALRawHits\") " << col << endl;
00252           }
00253           */
00254         } // end for collection in event
00255       }
00256       catch (IOException& e)
00257       {
00258         std::cout << e.what() << std::endl ;
00259         std::string s=e.what();s+=__PRETTY_FUNCTION__;
00260         exit(-1);
00261       }
00262       catch (...)
00263       {
00264         std::cout<<" unknow io exeption "<<std::endl;
00265         exit(-1);
00266       }
00267       /*DEBUG */  unsigned int globalHeader = 0XB0;
00268     } // end else not first event
00269 
00270     
00271   }// end while event not found
00272 
00273   return EVENT_CORRECT;
00274 
00275 
00276 
00277 
00278 
00279 return 0;
00280 }
00281 //------------------------------------------//
00282 int MicrorocXDaqReader::getAcqData(Event& event,unsigned char *bufChar) 
00283 {
00284  ui32 nbanalogHit = 0;
00285   unsigned int nbHits = 0;
00286 
00287   display(+1);
00288   FILE_LOG(logDEBUG) << " global header B0 found" << endl;
00289 
00290 
00291   this->setComputeCrcFlag(true);
00292   //FILE_LOG(logDEBUG) << "MicrorocLabviewReader crc adr[" <<  crc << "]" << endl;
00293    FILE_LOG(logDEBUG) << "crc->compute(0xB0)[ "<< crc <<"]"   << endl;
00294 
00295   crc->compute(0xB0);
00296 
00297 
00298 
00299   // reset the data for the block
00300   //  bcId_AbsOrg = -1;
00301   bcId_Abs = 0; // max. 48 bits
00302   bcId_Dif = 0;
00303   // the following data is repeated one time for each Dif which has sent data
00304   difId = getData(1);
00305 
00306   difTriggerCounter = getData(4);
00307   usbBusyTrigger = getData(4);  // trigger counter version >= 4
00308   globalTriggerCounter = getData(4);
00309 
00310   ui64 bcIdHigh = getData(2);
00311   ui64 bcIdLow = getData(4);
00312   bcId_Abs = (bcIdHigh << 32) + bcIdLow;
00313   //FILE_LOG(logDEBUG) << "bcId_Abs[" << hex << bcId_Abs <<  endl;
00314 
00315   bcId_Dif = getData(3);
00316 
00317   bcId_AbsOrg =  getbcIdAbsOrg(difId, bcId_Abs);
00318   display();
00319 
00320 /* NOT IMPLEMENTAED ??? 
00321   ui16 lineInfo =  getData(1);
00322   FILE_LOG(logDEBUG)   << " lineInfo[" <<hex << lineInfo << "]"  << endl;
00323   ui16 nbLine   = (lineInfo & 0xF0 ) >> 4;
00324   ui16 usedLine = lineInfo & 0x0F; //bit 3 (4eme ligne,)  bit 2 (3eme ligne)  bit 1 (2eme ligne)  bit 0 (1ere ligne)
00325   display();
00326 */
00327 
00328 
00329 
00330  // the bcTime in milliseconds
00331   i64 bcTime = ((bcId_Abs - bcId_AbsOrg) * bcPeriod) / 1000;
00332   // the reading time in milliseconds
00333   i64 readTime = globalTime + bcTime;
00334 
00335   FILE_LOG(logDEBUG)   << "  START EVENT ------------- time " << readTime << " ms" << endl;
00336   FILE_LOG(logDEBUG)   << "    bc time               " << bcTime << " ms" << endl;
00337   FILE_LOG(logDEBUG)   << "    DIF Id                0x" << hex << (int)difId << dec << endl;
00338   FILE_LOG(logDEBUG)   << "    DIF trigger counter   " << difTriggerCounter << endl;
00339   FILE_LOG(logDEBUG)   << "    USB Busy trigger counter   " << usbBusyTrigger << endl;
00340   FILE_LOG(logDEBUG)   << "    total trigger counter " << globalTriggerCounter << endl;
00341   FILE_LOG(logDEBUG)   << "    absolute bcId         " << bcId_Abs - bcId_AbsOrg << endl;
00342  // FILE_LOG(logDEBUG)   << "    nb line          " << dec <<nbLine << endl;
00343  // FILE_LOG(logDEBUG)   << "    used line        " << hex << usedLine << dec <<endl;
00344   event.setDifTriggerCounter(difTriggerCounter);
00345   event.setGlobalTriggerCounter(globalTriggerCounter);
00346 
00347 
00348   Detector &detector = run.getDetector();
00349 
00350   if ( analogDataActivate )//&& orderInChipMemory == 0 )
00351   {
00352     for ( map<ui16,map<ui16,ui16> >::iterator iterAnal = analogData.begin(); iterAnal!= analogData.end(); iterAnal++)
00353     {
00354        map<ui16,ui16>& byChip = iterAnal->second;
00355        ui16 chipId = iterAnal->first;
00356       // When analog is anable, all channel of all chip enable for analogic respond
00357       for ( std::map<ui16,ui16>::iterator iterChip = byChip.begin(); iterChip != byChip.end();iterChip++)
00358       {
00359         ui16 analogValue =  iterChip->second;
00360         ui16 channelId = iterChip->first;
00361         if ( difList == NULL || difList->find(difId) != difList->end()  ) // No difid filter or difId in difList
00362         {
00363           nbHits += newHit(event, detector, 0, analogValue,channelId , chipId, difId);
00364           nbanalogHit++;
00365         }
00366       }
00367     }
00368   }
00369   // end of global header
00370 
00371  bool difFound = false;
00372   { const ChamberMap_t &chambers = detector.getChambers();
00373     for (ChamberMap_t::const_iterator it = chambers.begin(); it != chambers.end(); ++it) {
00374       try {
00375         Chamber& chamber = *(it->second);
00376         const BoardMap_t& boards = chamber.getBoardsByDifId(difId);
00377       }
00378       catch (...) {
00379         // any error case - try next chamber
00380         continue;
00381       }
00382 
00383       // the DIF has been found in a chamber. Process all data and continue
00384       difFound = true;
00385       break;
00386     } // for
00387     if (!difFound) {
00388       FILE_LOG(logERROR) << "    No dif found with Id 0x" << hex << difId << ", " << dec << difId << "                                     " <<endl;
00389     }
00390   }
00391 
00392   for (bool finish = false; !finish; ) {
00393     int delimiter = 0;
00394 
00395     display();
00396     for (; delimiter != 0xA0 && delimiter != 0xB4; ) {
00397       // seek for first data frame header (0xB4)
00398       delimiter = getData(1);
00399       if (fileError == EOF) {
00400         delimiter = 0;
00401         break;
00402       }
00403       if (delimiter == 0xA3)
00404         FILE_LOG(logERROR) << "    frame trailer A3 found -- should not" << endl;
00405     }
00406     if (delimiter == 0x00) {
00407       FILE_LOG(logERROR) << "  end of file -- should not" << endl;
00408       break;
00409     }
00410     if (delimiter == 0xA0) {
00411    FILE_LOG(logDEBUG) << "    frame trailer A0 found" << endl;
00412       this->setComputeCrcFlag(false);
00413       unsigned short CRC_check = getData(2);
00414       FILE_LOG(logDEBUG) << hex << " Compute CRC[" << crc->getCrc() << "]" <<  endl;
00415       FILE_LOG(logDEBUG) << hex << " Received CRC[" << CRC_check << "]" <<  endl;
00416       if ( CRC_check != crc->getCrc() )
00417       {
00418         FILE_LOG(logERROR)  << " CRC Error for event id " <<  lastEventId << endl;
00419         FILE_LOG(logERROR) << hex << " Compute CRC[" << crc->getCrc() << "]" <<  endl;
00420         FILE_LOG(logERROR) << hex << " Received CRC[" << CRC_check << "]" <<  endl;
00421         event.setCrcIsCorrect(false);
00422        // exit(-1);
00423       }
00424       else
00425       {
00426         event.setCrcIsCorrect(true);
00427       }
00428 
00429       display(-1);
00430       FILE_LOG(logDEBUG) << " global trailer A0 found" << endl;
00431       break;
00432     }
00433     display(+1);
00434     FILE_LOG(logDEBUG) << "   frame header B4 found" << endl;
00435 
00436 
00437     int chipId = -1;
00438     ui16 orderInChipMemory = 0;
00439     for (; ; )  // Read in chip memory for different BCId_HIT. 
00440     {
00441       chipId = getData(1);
00442       FILE_LOG(logDEBUG) << "8 bits read[" << chipId << "] [0x"  << hex << chipId << "]" << dec << endl;
00443 
00444       if (fileError == EOF) {
00445         finish = true;
00446         FILE_LOG(logINFO) << "    end of file found -- should not" << endl;
00447         break;
00448       }
00449       if (chipId == 0x00) {
00450         // dummy null bytes
00451         continue;
00452       }
00453   else if (chipId == 0xA3) {
00454         display(-1);
00455         FILE_LOG(logDEBUG) << "   frame trailer A3 found" << endl;
00456         break;
00457       }
00458       else if (chipId == 0xA0) {
00459         finish = true;
00460         display(-1);
00461 //        FILE_LOG(logERROR) << "    global trailer A0 found -- should not" << endl;
00462         //int CRC_check = getData(1);
00463         break;
00464       }
00465       else if (chipId == 0xC3) {
00466         display(-1);
00467         FILE_LOG(logERROR) << "    frame trailer C3 found - error occured during data transfer" << endl;
00468         break;
00469       }
00470 
00471       // bcId from hardroc chip is counted as Gray code
00472       bcId_Hit = grayToBinary(getData(3));
00473       // event time in milliseconds
00474       eventTime = readTime + (((bcId_Dif - bcId_Hit) * bcPeriod) / 1000);
00475 
00476       orderInChipMemory++;
00477       // not on first line and bcId OK : get and check data
00478       if (difFound)
00479       {
00480         FILE_LOG(logDEBUG) << "      chip Id               0x" << hex << (int)chipId << dec << endl;
00481         FILE_LOG(logDEBUG) << "      bcId Microroc          " << hex << (int)bcId_Hit << endl;
00482         FILE_LOG(logDEBUG) << "      event time            " << eventTime << endl;
00483 
00484         for (int chNum = 48; chNum >= 0; chNum -= 16)  // read data ( 128) for different channel Id with same BCID_HIT
00485         {
00486           unsigned int data4 = getData(4); // lit 4 bytes => 8 char => 16 canaux
00487           for (int index = 0; index < 16; index++)
00488           {
00489               // sur les 16 canaux, recupere les donnees canal par canal
00490             int trigger = (data4 >> ((15 - index) * 2)) & 0x03; // 2 bits
00491             // store new event data if any
00492     if (trigger)  // on elimine tous les hits dans la valeur est 0 
00493             {
00494               if ( difList == NULL || difList->find(difId) != difList->end()  ) // No difid filter or difId in difList
00495               { 
00496                  if ( analogDataActivate && orderInChipMemory == 1 )
00497                  {
00498                   i16 analogValue = analogData[chipId][index + chNum];
00499                   nbHits += newHit(event, detector, trigger, analogValue, index + chNum, chipId, difId,orderInChipMemory);
00500                  }
00501                  else
00502                  {
00503                   nbHits += newHit(event, detector, trigger, 0, index + chNum, chipId, difId,orderInChipMemory);
00504                  }
00505               }
00506 
00507                 //digital[index+chNum]=trigger;
00508                 FILE_LOG(logDEBUG) << "digital chipId[" << chipId << "] channelNum["<< index+chNum <<"]= " << trigger << endl;
00509             }
00510           } // for (index)
00511         } // End digital data. for (chNum)
00512       }
00513     display();
00514     } // for(;;)
00515 
00516   } // for (;;)
00517   display(-1);
00518 
00519  // CREATE EVENT for current Run
00520 
00521 // if ( nbHits > 0 )
00522 // {
00523   event.setTimeStamp(globalTime + ((bcId_AbsOrg /1000000 * bcPeriod) )  );  // globalTime -> millisecond and bcId_AbsOrg * bcPeriod -> nano second
00524   //FILE_LOG(logINFO) << "  event.setTimeStamp  " <<  event.getTimeStamp()  << endl;
00525   //event.setId(++lastEventId);
00526   FILE_LOG(logINFO) << "  Add Hit for event  EVENT " << lastEventId << ", " << nbHits << " hits \r" << flush;
00527 //  FILE_LOG(logINFO) << "  Add Hit for event  EVENT " << lastEventId << ", " << nbHits << " hits " << endl;
00528 // }
00529 // else
00530 // {
00531 //  FILE_LOG(logDEBUG) << "  EMPTY EVENT " << lastEventId << ", " << nbHits << " hits" << endl;
00532 // }
00533 
00534 
00535   if (SkippedBcId) {
00536     FILE_LOG(logWARNING) << "    skipped " << SkippedBcId << " data because of bad bcId" << endl;
00537     SkippedBcId = 0;
00538   }
00539 
00540   return(nbHits);
00541 
00542 
00543 }
00544 
00545 //------------------------------------------//
00546 int MicrorocXDaqReader::getAnalogData(Event& event) 
00547 {
00548 return 0;
00549 }
00550 
00551 //------------------------------------------//
00552 unsigned int MicrorocXDaqReader::getData(const unsigned int nBytes)
00553 {
00554 unsigned int result = 0;
00555 
00556   fileError = 0;
00557   for (unsigned int byte = 0; byte < nBytes; ++byte) 
00558   {
00559     unsigned int data = *itRawdata;
00560     itRawdata++;
00561     if ( computeCrc ) 
00562     {
00563       crc->compute(data) ;
00564     }
00565     result = (result << 8) + data;
00566   }
00567 
00568 putData(result, nBytes);
00569 
00570 return result;
00571 }
00572 
00573 //------------------------------------------//
00574 /*
00575 void MicrorocXDaqReader::display(const int inc) {
00576   line++;
00577   if (inc < 0) {
00578     level += inc;
00579     if (level < 0) level = 0;
00580   }
00581     FILE_LOG(logDEBUG) << setfill(' ') << setw(level * 2) << "display[" << hex << (unsigned int)( *itRawdata )<< "]" << endl;
00582 
00583   if (inc > 0)
00584     level += inc;
00585 } // DifReader.display
00586 */
00587 

Generated on Mon Jun 11 16:55:46 2012 for MicromegasFramework by  doxygen 1.4.7