CMS 3D CMS Logo

CSCFileReader.cc

Go to the documentation of this file.
00001 #include "CSCFileReader.h"
00002 
00003 //#include <iostream.h>
00004 
00005 #include <errno.h>
00006 #include <stdlib.h>
00007 #include <string>
00008 
00009 #include <DataFormats/FEDRawData/interface/FEDHeader.h>
00010 #include <DataFormats/FEDRawData/interface/FEDTrailer.h>
00011 #include <DataFormats/FEDRawData/interface/FEDNumbering.h>
00012 #include <DataFormats/Provenance/interface/EventID.h>
00013 #include <DataFormats/Provenance/interface/Timestamp.h>
00014 #include <DataFormats/FEDRawData/interface/FEDRawData.h>
00015 #include <DataFormats/FEDRawData/interface/FEDRawDataCollection.h>
00016 
00017 #include <FWCore/ParameterSet/interface/ParameterSet.h>
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 
00020 #include <vector>
00021 #include <string>
00022 #include <iosfwd>
00023 #include <sstream>
00024 #include <iostream>
00025 #include <algorithm>
00026 
00027 #define nRUIs 40
00028 #define nFUs  4
00029 
00030 CSCFileReader::CSCFileReader(const edm::ParameterSet& pset):DaqBaseReader(){
00031         LogDebug("CSCFileReader|ctor")<<"Started ...";
00032         // Below some data members are recycled for both cases: RUIs and FUs
00033         //  this is ok as long as eighter of RUI or FU are are provided in .cfg (not both)
00034         nActiveRUIs = 0;
00035         nActiveFUs  = 0;
00036         for(int unit=0; unit<nRUIs; unit++){
00037                 std::ostringstream ruiName, fuName;
00038                 ruiName<<"RUI"<<(unit<10?"0":"")<<unit<<std::ends;
00039                 fuName <<"FU" <<unit<<std::ends;
00040                 std::vector<std::string> ruiFiles = pset.getUntrackedParameter< std::vector<std::string> >(ruiName.str().c_str(),std::vector<std::string>(0));
00041                 std::vector<std::string> fuFiles  = pset.getUntrackedParameter< std::vector<std::string> >(fuName.str().c_str(),std::vector<std::string>(0));
00042                 if( ruiFiles.begin() != ruiFiles.end() ) nActiveRUIs++;
00043                 if( fuFiles.begin()  != fuFiles.end()  ) nActiveFUs++;
00044         }
00045         if( nActiveFUs && nActiveRUIs )
00046                 throw cms::Exception("CSCFileReader|configuration")<<"RUIs and FUs in conflict: either RUI or FU may be defined at a time, not both";
00047         if( !nActiveFUs && !nActiveRUIs )
00048                 throw cms::Exception("CSCFileReader|configuration")<<"Module lacks configuration";
00049 
00050 
00051         // Get list of RUI input files from .cfg file
00052         for(int rui=0; rui<nRUIs && !nActiveFUs; rui++){
00053                 std::ostringstream name;
00054                 name<<"RUI"<<(rui<10?"0":"")<<rui<<std::ends;
00055 
00056                 // Obtain list of files associated with current RUI
00057                 fileNames[rui] = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
00058                 currentFile[rui] = fileNames[rui].begin();
00059 
00060                 // If list of files is not empty, open first file
00061                 if( currentFile[rui] != fileNames[rui].end() ){
00062                         try {
00063                                 RUI[rui].open(currentFile[rui]->c_str());
00064                         } catch ( std::runtime_error err ){
00065                                 throw cms::Exception("CSCFileReader")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
00066                         }
00067                         nActiveRUIs++;
00068                 }
00069 
00070                 // Filter out possible corruptions
00071                 RUI[rui].reject(FileReaderDDU::DDUoversize|FileReaderDDU::FFFF|FileReaderDDU::Unknown);
00072                 // Do not select anything in particular
00073                 RUI[rui].select(0);
00074 
00075                 currentL1A[rui] = -1;
00076         }
00077 
00078         // Get list of FU input files from .cfg file
00079         for(int fu=0; fu<nFUs && !nActiveRUIs; fu++){
00080                 std::ostringstream name;
00081                 name<<"FU"<<fu<<std::ends;
00082 
00083                 // Obtain list of files associated with current FU
00084                 fileNames[fu] = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
00085                 currentFile[fu] = fileNames[fu].begin();
00086 
00087                 // If list of files is not empty, open first file
00088                 if( currentFile[fu] != fileNames[fu].end() ){
00089                         try {
00090                                 FU[fu].open(currentFile[fu]->c_str());
00091                         } catch ( std::runtime_error err ){
00092                                 throw cms::Exception("CSCFileReader")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
00093                         }
00094                         nActiveFUs++;
00095                 }
00096 
00097                 // Filter out possible corruptions
00098                 FU[fu].reject(FileReaderDCC::DCCoversize|FileReaderDCC::FFFF|FileReaderDCC::Unknown);
00099                 // Do not select anything in particular
00100                 FU[fu].select(0);
00101 
00102                 currentL1A[fu] = -1;
00103         }
00104 
00105         if( nActiveRUIs && !nActiveFUs ){
00106                 // Assign RUIs to FEDs
00107                 for(int fed=FEDNumbering::getCSCFEDIds().first; fed<=FEDNumbering::getCSCFEDIds().second; fed++){
00108                         std::ostringstream name;
00109                         name<<"FED"<<fed<<std::ends;
00110                         std::vector<std::string> rui_list = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
00111                         for(std::vector<std::string>::const_iterator rui=rui_list.begin(); rui!=rui_list.end(); rui++)
00112                                 FED[fed].push_back((unsigned int)atoi(rui->c_str()+rui->length()-2));
00113                 }
00114                 // Do the same for Track-Finder FED
00115                 for(int fed=FEDNumbering::getCSCTFFEDIds().first; fed<=FEDNumbering::getCSCTFFEDIds().second; fed++){
00116                         std::ostringstream name;
00117                         name<<"FED"<<fed<<std::ends;
00118                         std::vector<std::string> rui_list = pset.getUntrackedParameter< std::vector<std::string> >(name.str().c_str(),std::vector<std::string>(0));
00119                         for(std::vector<std::string>::const_iterator rui=rui_list.begin(); rui!=rui_list.end(); rui++)
00120                                 FED[fed].push_back((unsigned int)atoi(rui->c_str()+rui->length()-2));
00121                 }
00122         }
00123         // Starting point
00124         firstEvent = pset.getUntrackedParameter<int>("firstEvent",0);
00125         nEvents = 0;
00126         expectedNextL1A = -1;
00127 
00128         // If Track-Finder was in readout specify its position in the record or set -1 otherwise
00129         //  Current agriment is that if there is a TF event it is first DDU record
00130         tfDDUnumber = pset.getUntrackedParameter<int>("tfDDUnumber",-1);
00131 
00132         // For efficiency reasons create this big chunk of data only once
00133         tmpBuf = new unsigned short[200000*nRUIs+4*4];
00134         // Event buffer and its length for every FU
00135         fuEvent[0]=0; fuEventSize[0]=0;
00136         fuEvent[1]=0; fuEventSize[1]=0;
00137         fuEvent[2]=0; fuEventSize[2]=0;
00138         fuEvent[3]=0; fuEventSize[3]=0;
00139 
00140         LogDebug("CSCFileReader|ctor")<<"... and finished";
00141 }
00142 
00143 CSCFileReader::~CSCFileReader(void){ if(tmpBuf) delete [] tmpBuf; }
00144 
00145 int CSCFileReader::readRUI(int rui, const unsigned short* &buf, size_t &length){
00146         if( currentFile[rui] == fileNames[rui].end() ) return -1;
00147         do {
00148                 try {
00149                         length = RUI[rui].next(buf);
00150                 } catch ( std::runtime_error err ){
00151                         throw cms::Exception("CSCFileReader|reading")<<"EndOfStream: "<<err.what()<<" (errno="<<errno<<")";
00152                 }
00153                 if( length==0 ){ // end of file, try next one
00154                         if( ++currentFile[rui] != fileNames[rui].end() ){
00155                                 try {
00156                                         RUI[rui].open(currentFile[rui]->c_str());
00157                                 } catch ( std::runtime_error err ){
00158                                         throw cms::Exception("CSCFileReader|reading")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
00159                                 }
00160                         } else return -1;
00161                 }
00162         } while( length==0 );
00163         return buf[2]|((buf[3]&0xFF)<<16);
00164 }
00165 
00166 int CSCFileReader::readFU(int fu, const unsigned short* &buf, size_t &length){
00167         if( currentFile[fu] == fileNames[fu].end() ) return -1;
00168         do {
00169                 try {
00170                         length = FU[fu].next(buf);
00171                 } catch ( std::runtime_error err ){
00172                         throw cms::Exception("CSCFileReader|reading")<<"EndOfStream: "<<err.what()<<" (errno="<<errno<<")";
00173                 }
00174                 if( length==0 ){ // end of file, try next one
00175                         if( ++currentFile[fu] != fileNames[fu].end() ){
00176                                 try {
00177                                         FU[fu].open(currentFile[fu]->c_str());
00178                                 } catch ( std::runtime_error err ){
00179                                         throw cms::Exception("CSCFileReader|reading")<<"InputFileMissing: "<<err.what()<<" (errno="<<errno<<")";
00180                                 }
00181                         } else return -1;
00182                 }
00183         } while( length==0 );
00184         // Take L1A from first DDU header in the DCC record (shift=8)
00185         return buf[2+8]|((buf[3+8]&0xFF)<<16);
00186 }
00187 
00188 int CSCFileReader::buildEventFromRUIs(FEDRawDataCollection *data){
00189         // Event buffer and its length for every RUI
00190         const unsigned short *buf[nRUIs];
00191         size_t length[nRUIs];
00192         bzero(length,sizeof(length));
00193 
00194         int eventNumber =-1; // Will determine below
00195 
00196         do {
00197                 // Read next event from RUIs
00198                 for(int rui=0; rui<nRUIs; rui++){
00199                         // read event from the RUI only in two cases:
00200                         //     1) it is readable (currentL1A>0) and we expect next event from the RUI
00201                         //     2) it is first time (expectedNextL1A<0)
00202                         if((currentL1A[rui]>0 && currentL1A[rui]<expectedNextL1A) || expectedNextL1A<0 )
00203                                 currentL1A[rui] = readRUI(rui,buf[rui],length[rui]);
00204                 }
00205                 eventNumber =-1;
00206 
00207                 // Select lowest L1A from all RUIs and don't expect next event from RUIs that currently hold higher L1A
00208                 for(int rui=0; rui<nRUIs; rui++)
00209                         if( currentL1A[rui]>=0 && (eventNumber>currentL1A[rui] || eventNumber==-1) ) eventNumber=currentL1A[rui];
00210                 // No readable RUIs => fall out
00211                 if( eventNumber<0 ) return -1;
00212                 // Expect next event to be incremented by 1 wrt. to the current event
00213                 expectedNextL1A = eventNumber+1;
00214 
00215         } while(nEvents++<firstEvent);
00216 
00217         for(std::map<unsigned int,std::list<unsigned int> >::const_iterator fed=FED.begin(); fed!=FED.end(); fed++)
00218                 if( fed->first<(unsigned int)FEDNumbering::getCSCTFFEDIds().first ){
00219                         // Now let's pretend that DDU data were wrapped with DCC Header (2 64-bit words) and Trailer (2 64-bit words):
00220                         unsigned short *dccBuf=tmpBuf, *dccCur=dccBuf;
00221                         dccCur[3] = 0x5000; dccCur[2] = 0x0000; dccCur[1] = 0x0000; dccCur[0] = 0x005F; // Fake DCC Header 1
00222                         dccCur[7] = 0xD900; dccCur[6] = 0x0000; dccCur[5] = 0x0000; dccCur[4] = 0x0017; // Fake DCC Header 2
00223                         dccCur += 8;
00224 
00225                         for(std::list<unsigned int>::const_iterator rui=fed->second.begin(); rui!=fed->second.end(); rui++){
00226 //cout<<"Event:"<<eventNumber<<"  FED:"<<fed->first<<"  RUI:"<<*(fed->second.begin())<<" currL1A:"<<currentL1A[*rui]<<endl;
00227                                 if( currentL1A[*rui]==eventNumber ){
00228                                         if(dccCur-dccBuf+length[*rui]>=200000*nRUIs+8) throw cms::Exception("CSCFileReader|eventBuffer")<<"OutOfBuffer: Event size exceeds maximal size allowed!";
00229                                         memcpy(dccCur,buf[*rui],length[*rui]*sizeof(unsigned short));
00230                                         dccCur += length[*rui];
00231                                 }
00232                         }
00233                         dccCur[3] = 0xEF00; dccCur[2] = 0x0000; dccCur[1] = 0x0000; dccCur[0] = 0x0000; // Fake DCC Trailer 2
00234                         dccCur[7] = 0xAF00; dccCur[6] = 0x0000; dccCur[5] = 0x0000; dccCur[4] = 0x0007; // Fake DCC Trailer 2
00235                         dccCur += 8;
00236 
00237                         FEDRawData& fedRawData = data->FEDData(fed->first);
00238                         fedRawData.resize((dccCur-dccBuf)*sizeof(unsigned short));
00239                         std::copy((unsigned char*)dccBuf,(unsigned char*)dccCur,fedRawData.data());
00240                 } else {
00241                         for(std::list<unsigned int>::const_iterator rui=fed->second.begin(); rui!=fed->second.end(); rui++){
00242                                 FEDRawData& fedRawData = data->FEDData(fed->first);
00243                                 fedRawData.resize(length[*rui]*sizeof(unsigned short));
00244                                 std::copy((unsigned char*)buf[*rui],(unsigned char*)(buf[*rui]+length[*rui]),fedRawData.data());
00245                         }
00246                 }
00247 
00248         return eventNumber;
00249 }
00250 
00251 int CSCFileReader::nextEventFromFUs(FEDRawDataCollection *data){
00252         int eventNumber =-1; // Will determine below
00253 
00254         // If this is a first time - read one event from each FU
00255         if( expectedNextL1A<0 )
00256                 for(int fu=0; fu<nFUs; fu++)
00257                         currentL1A[fu] = readFU(fu,fuEvent[fu],fuEventSize[fu]);
00258 
00259         // Keep buffers for every FU ready at all times
00260         // When buffer from some FU is ready to go as the next event,
00261         //  release it, but read next one
00262         int readyToGo = -1;
00263         for(int fu=0; fu<nFUs; fu++){
00264                 // If FU is readable and (first loop of this cycle or current FU holds smallest L1A)
00265                 if(currentL1A[fu]>=0 && (eventNumber<0 || currentL1A[fu]<eventNumber)){
00266                         readyToGo   = fu;
00267                         eventNumber = currentL1A[fu];
00268                 }
00269         }
00270         // No readable FUs => fall out
00271         if( readyToGo<0 ) return -1;
00272 
00273         expectedNextL1A = eventNumber + 1;
00274 
00275         // Compose event from DDC record striped of Track-Finder DDU and a separate TF DDU event
00276         unsigned long long *start = (unsigned long long *)fuEvent[readyToGo];
00277         unsigned long long *end   = 0;
00278         enum {Header=1,Trailer=2};
00279         unsigned int eventStatus  = 0;
00280         for(int dduRecord=0; dduRecord<=tfDDUnumber; dduRecord++){
00281                 unsigned long long word_0=0, word_1=0, word_2=0;
00282                 size_t dduWordCount = 0;
00283                 while( !end && dduWordCount<fuEventSize[readyToGo] ){
00284                         unsigned long long *dduWord = start;
00285 
00286                         while( dduWordCount<fuEventSize[readyToGo] ){
00287                                 word_0 =  word_1; // delay by 2 DDU words
00288                                 word_1 =  word_2; // delay by 1 DDU word
00289                                 word_2 = *dduWord;// current DDU word
00290                                 if( (word_2&0xFFFFFFFFFFFF0000LL)==0x8000000180000000LL ){
00291                                         if( eventStatus&Header ){ // Second header
00292                                                 word_2 = word_1;
00293                                                 end = dduWord;
00294                                                 break;
00295                                         }
00296                                         start = dduWord;
00297                                 }
00298                                 if( (word_0&0xFFFFFFFFFFFF0000LL)==0x8000FFFF80000000LL ){
00299                                         eventStatus |= Trailer;
00300                                         end = ++dduWord;
00301                                         break;
00302                                 }
00303                                 // Increase counters by one DDU word
00304                                 dduWord++;
00305                                 dduWordCount++;
00306                         }
00307                 }
00308                 // If reach max length
00309                 if( dduWordCount==fuEventSize[readyToGo] ){
00310                         end = (unsigned long long *)(fuEvent[readyToGo]+fuEventSize[readyToGo]);
00311                         break;
00312                 }
00313         }
00314         // Include 0x5xxx preHeader if exists
00315         if( start>(unsigned long long *)fuEvent[readyToGo] && (*(start-1)&0xF000000000000000LL)==0x5000000000000000LL ) start--;
00316         // Cut out Track-Finder DDU from the buffer
00317         FEDRawData& tfRawData = data->FEDData(FEDNumbering::getCSCTFFEDIds().first);
00318         tfRawData.resize((end-start)*sizeof(unsigned long long));
00319         std::copy((unsigned char*)start,(unsigned char*)end,tfRawData.data());
00320 
00321         // Create a new buffer from everything before and after TF DDU
00322         unsigned short *event = tmpBuf;
00323         memcpy(event,fuEvent[readyToGo],((unsigned short*)start-fuEvent[readyToGo])*sizeof(unsigned short));
00324         event += ((unsigned short*)start-fuEvent[readyToGo]);
00325         memcpy(event,end,(fuEvent[readyToGo]+fuEventSize[readyToGo]-(unsigned short*)end)*sizeof(unsigned short));
00326         event += fuEvent[readyToGo]+fuEventSize[readyToGo]-(unsigned short*)end;
00327         FEDRawData& fedRawData = data->FEDData(FEDNumbering::getCSCFEDIds().first);
00328         fedRawData.resize((fuEventSize[readyToGo]-((unsigned short*)end-(unsigned short*)start))*sizeof(unsigned short));
00329         std::copy((unsigned char*)tmpBuf,(unsigned char*)event,fedRawData.data());
00330 
00331         currentL1A[readyToGo] = readFU(readyToGo,fuEvent[readyToGo],fuEventSize[readyToGo]);
00332 
00333         return eventNumber;
00334 }
00335 
00336 bool CSCFileReader::fillRawData(edm::EventID& eID, edm::Timestamp& tstamp, FEDRawDataCollection *& data){
00337         data = new FEDRawDataCollection();
00338 
00339         int runNumber   = 0; // Unknown at the level of EMu local DAQ
00340         int eventNumber =-1; // Will determine below
00341 
00342         if( !nActiveFUs && nActiveRUIs ){
00343                 eventNumber = buildEventFromRUIs(data);
00344         } else {
00345                 eventNumber = nextEventFromFUs(data);
00346         }
00347 
00348         if( eventNumber<0 ) return false;
00349 
00350         eID = edm::EventID(runNumber,eventNumber);
00351 
00352         return true;
00353 }
00354 
00355 #undef nRUIs
00356 #undef nFUs

Generated on Tue Jun 9 17:39:23 2009 for CMSSW by  doxygen 1.5.4