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

Go to the documentation of this file.
00001 /* @version $Revision: 1952 $ * @modifiedby $Author: jacquem $ * @lastmodified $Date: 2012-11-20 15:34:41 +0100 (Tue, 20 Nov 2012) $ */
00002 
00003 ////////////////////////////////////////////////////////////////////////
00004 // MICROMEGAS
00005 //
00006 //       EVENT RECONSTRUCTION SOFTWARE FOR MICROROC BOARDS
00007 //
00008 ////////////////////////////////////////////////////////////////////////
00009 
00010 #include <unistd.h>
00011 
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <errno.h>
00015 
00016 #include "parser/MicrorocXDaqReader.hh"
00017 #include "parser/MicrorocSCReader.hh"
00018 #include "parser/AcquisitionParser.hh"
00019 
00020 #include "event/Event.hh"
00021 
00022 #include "geometry/Chamber.hh"
00023 #include "geometry/Board.hh"
00024 #include "geometry/Dif.hh"
00025 #include "geometry/MicrorocChip.hh"
00026 #include "geometry/Detector.hh"
00027 
00028 #include "tools/Toolbox.hh"
00029 #include "tools/SteerDesc.hh"
00030 #include "tools/Log.hh"
00031 #include "tools/MicroException.hh"
00032 
00033 
00034 #include "lcio.h"
00035 #include "IO/LCReader.h"
00036 #include "IMPL/LCTOOLS.h"
00037 #include "IMPL/LCGenericObjectImpl.h"
00038 #include "EVENT/LCCollection.h"
00039 #include "EVENT/LCRunHeader.h"
00040 
00041 #include "geometry/MicrorocChannel.hh"
00042 
00043 #include "ilcConfDb/IlcConfDb.hh"
00044 
00045 
00046 #include <iostream>
00047 #include <iomanip>
00048 
00049 
00050 #define NBBYTETOSKIP  94
00051 
00052 using namespace std;
00053 using namespace lcio ;
00054 
00055 
00056 ////////////////////////////////////////////////////////////////////////
00057 MicrorocXDaqReader::MicrorocXDaqReader(Run& aRun, string aFileFullPath, ui32 lastEventId,bool _forceAnalog, set<unsigned int>*_difList) : MicrorocMergeReader(aRun, NULL, lastEventId,_forceAnalog,_difList), firstEvent(true)
00058 
00059 {
00060   FILE_LOG(logDEBUG1) << "--MicrorocXDaqReader constructor()"<< endl;
00061 
00062   lcReader = LCFactory::getInstance()->createLCReader() ;
00063   try {
00064     lcReader->open(aFileFullPath);
00065   }
00066   catch( IOException& e) {
00067     cout << e.what() << endl ;
00068     exit(-1) ;
00069   }
00070   
00071   if ( lcReader == NULL ) { exit(-1); }
00072 
00073 
00074   localReset();
00075 
00076 }
00077 
00078 //--------------- Destructeur -------------//
00079 MicrorocXDaqReader::~MicrorocXDaqReader()
00080 {
00081   lcReader->close();
00082   delete lcReader;
00083   FILE_LOG(logDEBUG1)<< "----MicrorocXDaqReader destructor"<< endl;
00084 }
00085                                                                                                              
00086 //--------------- reset -------------//
00087 void MicrorocXDaqReader::localReset()
00088 {
00089   rawdataList.clear();
00090 }
00091 //------------------------------------------//
00092 /*
00093 int MicrorocXDaqReader::getScData()
00094 {
00095 return 0;
00096 }
00097 */
00098 //------------------------------------------//
00099 int MicrorocXDaqReader::getNextEvent(Event& event) 
00100 {
00101   Level(0);
00102   ui16 dataFormat = run.getDataFormat();
00103 
00104   if (dataFormat == 0) {
00105     // first read of the file
00106     bcIdDifAbsOrgMap.clear();
00107     // read file header
00108     dataFormat = 12;
00109     run.setDataFormat(dataFormat);
00110     
00111     return NEW_CONFIG;
00112   } // end dataFormat == 0
00113 
00114   // loops until a non-empty event has been found
00115   bool eventFound = false;
00116   while (! eventFound)
00117   {
00118     LCEvent* lcioEvt(0) ;
00119     lcioEvt = lcReader->readNextEvent() ;
00120     if ( lcioEvt == NULL ) // EOF reach
00121     {
00122       return LAST_FILE_EVENT;
00123     }
00124 
00125 
00126     // There is one more LCEvent
00127     //LCTOOLS::dumpEvent( lcioEvt ) ;
00128     if ( ! slowControlSet )
00129     {
00130       // Read Slow control from database
00131       if ( event.getRun().getId() == 0 )
00132       {  // try to find run Id in lcioEvt
00133         run.setId(lcioEvt->getRunNumber());
00134         FILE_LOG(logINFO) << "Set run Id from lcioEvent information[" << run.getId() << "]" << endl;
00135       }
00136       
00137       
00138       globalTime = (i64) ( lcioEvt->getTimeStamp()*1000 );
00139 
00140       run.setDaqSoftware(0);
00141       run.setCccFirmware(0);
00142 
00143       IlcConfDb *ilcconf = IlcConfDb::getInstance();
00144       if ( ilcconf != NULL )
00145       {
00146         MicrorocSCReader scReader(run);
00147         scReader.parseSCFromDb(*ilcconf);
00148         slowControlSet = true;
00149 
00150 
00151       // Set dif Asu1Id and Asu2Id from XML as there is no available information in SlowControl 
00152         Detector &det = run.getDetector();
00153         const ChamberMap_t& chambers= det.getChambers();
00154         for (ChamberMap_t::const_iterator chamberIt = chambers.begin(); chamberIt != chambers.end(); ++chamberIt)
00155         {
00156           Chamber& chamber = *(chamberIt->second);
00157           const DifMap_t& difs = chamber.getDifs();
00158           for (DifMap_t::const_iterator difIt = difs.begin(); difIt != difs.end(); ++difIt)
00159           {
00160             Dif& dif = *(difIt->second);
00161             ui16 asuIndex = 1; 
00162             const BoardMap_t& boards = dif.getBoards();
00163             for (BoardMap_t::const_iterator boardIt = boards.begin(); boardIt != boards.end(); ++boardIt)
00164             {
00165               Board& board = *(boardIt->second);
00166               if ( asuIndex == 1 ) 
00167               {
00168                  dif.setAsu1Id(board.getId());
00169                  asuIndex++; 
00170                  FILE_LOG(logDEBUG) << "Set difid[" << dif.getId() << "] Asu1Id to[" << dif.getAsu1Id() << "]"  << endl;
00171               } 
00172               else if ( asuIndex == 2 ) 
00173               {
00174                  dif.setAsu2Id(board.getId()); 
00175                  FILE_LOG(logDEBUG) << "Set difid[" << dif.getId() << "] Asu2Id to[" << dif.getAsu2Id() << "]"  << endl;
00176               } 
00177             }
00178           }
00179         }
00180 
00181       }
00182 
00183     }
00184     if ( firstEvent  )
00185     {
00186       FILE_LOG(logINFO) << "firstEvent  skip"  << endl;
00187       firstEvent = false;
00188       // skip first event because of its corruption ???
00189     }
00190     else if (  skipNbEvent != 0 && debugEventId < skipNbEvent)
00191     {
00192       debugEventId++;
00193       FILE_LOG(logINFO) << "skip event " << debugEventId << "\r"  << flush;
00194     }
00195     else if (  skipNbEvent == debugEventId )
00196     {
00197       FILE_LOG(logINFO) << skipNbEvent << " event(s) have been skipped" << debugEventId << endl;
00198       debugEventId++;
00199     }    
00200 
00201     else
00202     {
00203       // vnames to store lcioEvt collections name
00204       std::vector<std::string >* vnames;
00205       try
00206       {
00207         vnames= (std::vector<std::string >*)lcioEvt->getCollectionNames();
00208         for ( vector<std::string >::iterator it = vnames->begin();  it!= vnames->end(); it++)
00209         {
00210           if ((*it).compare("RU_XDAQ")==0)
00211           {
00212             EVENT::LCCollection* col= lcioEvt->getCollection(*it);
00213 
00214             for (int difRawDataIndex=0;difRawDataIndex<col->getNumberOfElements(); difRawDataIndex++)
00215             {
00216               IMPL::LCGenericObjectImpl* go= (IMPL::LCGenericObjectImpl*) col->getElementAt(difRawDataIndex);
00217               int rusize =go->getNInt();
00218               if ( rusize == 56 ) continue; // skip first RU_XDAQ si tsille est 56 int
00219 
00220               int shift=-1;
00221               ui16 nbInt = go->getNInt();
00222 
00223               
00224               unsigned int buf[go->getNInt()];
00225       
00226               for (int k=0; k < (go->getNInt() );k++) { buf[k] = go->getIntVal(k); }
00227               // skip first NBBYTETOSKIP ( 94 ) bytes 
00228               // then fill rawdataList with unsigned char
00229               unsigned char* bufChar = ( unsigned char* ) &(buf); 
00230               ui16 rapport = (ui16) (sizeof(unsigned int ) / sizeof ( unsigned char) );
00231 
00232               rawdataList.clear();
00233               for ( ui32 j = NBBYTETOSKIP ; j < go->getNInt()*rapport ; j++ )
00234               {
00235                 rawdataList.push_back(*(bufChar+j));
00236                // sleep(1);
00237               }
00238               //cout << endl;
00239               
00240               itRawdata = rawdataList.begin();
00241 
00242               MicrorocMergeReader::decodeData(event);
00243 
00244             } // end for data for dif
00245             eventFound = true ;
00246             //event.setId(++lastEventId);
00247           } // ed if collection = XU_XDAQ
00248         } // end for collection in event
00249       }
00250       catch (IOException& e)
00251       {
00252         std::cout << e.what() << std::endl ;
00253         std::string s=e.what();s+=__PRETTY_FUNCTION__;
00254         exit(-1);
00255       }
00256       catch (...)
00257       {
00258         std::cout<<" unknow io exeption "<<std::endl;
00259         exit(-1);
00260       }
00261       /*DEBUG */  unsigned int globalHeader = 0XB0;
00262     } // end else not first event
00263 
00264     
00265   }// end while event not found
00266 
00267  if ( event.getChannelHitSize() > 0 )
00268   {
00269     event.setId(++lastEventId);
00270     return EVENT_CORRECT;
00271   }
00272   else
00273   {
00274     return EVENT_ERROR;
00275   }
00276 
00277 
00278 
00279 
00280 
00281 return 0;
00282 }
00283 
00284 //------------------------------------------//
00285 unsigned int MicrorocXDaqReader::getSpecificData(const unsigned int nBytes)
00286 {
00287 unsigned int result = 0;
00288 
00289   fileError = 0;
00290   for (unsigned int byte = 0; byte < nBytes; ++byte) 
00291   {
00292     unsigned int data = *itRawdata;
00293     itRawdata++;
00294     if ( computeCrc ) 
00295     {
00296      // if  ( data != 0xc4 && data != 0xd4 )
00297       {
00298         crc->compute(data) ;
00299       }
00300     }
00301     result = (result << 8) + data;
00302   }
00303 
00304 putData(result, nBytes);
00305 
00306 return result;
00307 }

Generated on Mon Jan 7 13:15:22 2013 for MicromegasFramework by  doxygen 1.4.7