CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/EventFilter/CSCRawToDigi/src/CSCDDUEventData.cc

Go to the documentation of this file.
00001 /* CSCDDUEventData.cc
00002  * Modified 4/21/03 to get rid of arrays and store all CSC data 
00003  * in vectors. 
00004  * A.Tumanov
00005  */
00006 
00007 #include "EventFilter/CSCRawToDigi/interface/CSCDDUEventData.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 
00010 #include <iostream>
00011 #include <cstdio>
00012 
00013 #include "EventFilter/CSCRawToDigi/src/bitset_append.h"
00014 
00015 bool CSCDDUEventData::debug = false;
00016 uint32_t CSCDDUEventData::errMask = 0xFFFFFFFF;
00017 
00018 
00019 CSCDDUEventData::CSCDDUEventData(const CSCDDUHeader & header) 
00020 {
00021   theDDUHeader = header;
00022 }
00023 
00024   
00025 CSCDDUEventData::CSCDDUEventData(uint16_t *buf, CSCDCCExaminer* examiner) 
00026 {
00027   unpack_data(buf, examiner);
00028 }
00029 
00030 CSCDDUEventData::~CSCDDUEventData() 
00031 {
00032 }
00033 
00034 
00035 void CSCDDUEventData::add(CSCEventData & cscData, int dmbId, int dduInput) 
00036 {
00037   theDDUHeader.setDMBDAV(dduInput);
00038   //@@ Tim: The following sets the word which is supposed to be CSCs in error, with bit 15 set for DMB Full
00039   //@@ so I think sim should not set it at all
00040   //@@  theDDUTrailer.setDMBDAV(dmbId);
00041   cscData.dmbHeader()->setdmbID(dmbId);
00042   cscData.setEventInformation(theDDUHeader.bxnum(), theDDUHeader.lvl1num());
00043   theData.push_back(cscData);
00044 }
00045 
00046 void CSCDDUEventData::decodeStatus() const 
00047 {
00048   this->decodeStatus(theDDUTrailer.errorstat());
00049 }
00050 
00051 void CSCDDUEventData::decodeStatus(int code) const 
00052 {
00053   // JRG is Jason Gilmore
00054   // JRG, low-order 16-bit status (most serious errors):
00055   if((code&errMask)>0)
00056     {
00057       // JRG, low-order 16-bit status (most serious errors):
00058       if((code&0x0000F000)>0)
00059         {
00060           if((0x00008000&code)>0)
00061             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Critical Error, ** needs reset **";
00062           if((0x00004000&code)>0)
00063             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Single Error, bad event";
00064           if((0x00002000&code)>0)
00065             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Single Warning";
00066           if((0x00001000&code)>0)
00067             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Near Full Warning";
00068         }
00069       if((code&0x00000F00)>0)
00070         {
00071           if((0x00000800&code)>0) 
00072             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU 64-bit Alignment Error";
00073           if((0x00000400&code)>0)
00074             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Control DLL Error occured";
00075           if((0x00000200&code)>0)
00076             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU DMB Error occurred";
00077           if((0x00000100&code)>0)
00078             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Lost In Event Error";
00079         }
00080       if((code&0x000000F0)>0)
00081         {
00082           if((0x00000080&code)>0)
00083             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Lost In Data Error occurred";
00084           if((0x00000040&code)>0)
00085             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Timeout Error";
00086           if((0x00000020&code)>0)
00087             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   TMB or ALCT CRC Error";
00088           if((0x00000010&code)>0)
00089             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Multiple Transmit Errors";
00090         }
00091       if((code&0x0000000F)>0)
00092         {
00093           if((0x00000008&code)>0)
00094             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Sync Lost or FIFO Full Error";
00095           if((0x00000004&code)>0)
00096             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Fiber/FIFO Connection Error";
00097           if((0x00000002&code)>0)
00098             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU L1A Match Error";
00099           if((0x00000001&code)>0)
00100             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU DMB or CFEB CRC Error";
00101         }
00102       if((code&0xF0000000)>0)
00103         {
00104           // JRG, high-order 16-bit status (not-so-serious errors):
00105           if((0x80000000&code)>0)
00106             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU DMB LCT/DAV/Movlp Mismatch";
00107           if((0x40000000&code)>0)
00108             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU-CFEB L1 Mismatch";
00109           if((0x20000000&code)>0)
00110             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU saw no good DMB CRCs";
00111           if((0x10000000&code)>0)
00112             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU CFEB Count Error";
00113         }
00114       if((code&0x0F000000)>0)
00115         {
00116           if((0x08000000&code)>0)
00117             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU FirstDat Error";
00118           if((0x04000000&code)>0)
00119             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU L1A-FIFO Full Error";
00120           if((0x02000000&code)>0)
00121             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Data Stuck in FIFO";
00122           if((0x01000000&code)>0)
00123             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU NoLiveFibers Error";
00124         }
00125       if((code&0x00F00000)>0)
00126         {
00127           if((0x00800000&code)>0)
00128             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Spwd single-bit Warning";
00129           if((0x00400000&code)>0)
00130             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Input FPGA Error";
00131           if((0x00200000&code)>0)
00132             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU DAQ Stop bit set";
00133           if((0x00100000&code)>0)
00134             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU DAQ says Not Ready";
00135           if((0x00300000&code)==0x00200000)
00136             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU DAQ Applied Backpressure";
00137         }
00138       if((code&0x000F0000)>0)
00139         {
00140           if((0x00080000&code)>0)
00141             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU TMB Error";
00142           if((0x00040000&code)>0)
00143             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU ALCT Error";
00144           if((0x00020000&code)>0)
00145             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Trigger Readout Wordcount Error";
00146           if((0x00010000&code)>0)
00147             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "   DDU Trigger L1A Match Error";
00148         }
00149     }
00150 }
00151 
00152 void CSCDDUEventData::unpack_data(uint16_t *buf, CSCDCCExaminer* examiner) 
00153 {
00154   // just to calculate length
00155   uint16_t * inputBuf = buf;
00156   uint16_t * inputBuf0 = buf; 
00157   theData.clear();
00158   if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << "CSCDDUEventData::unpack_data() is called";
00159   if (debug) 
00160    for (int i=0;i<6;++i) 
00161     {
00162       LogTrace ("CSCDDUEventData|CSCRawToDigi") << i << std::hex << buf[4*i+3] << buf[4*i+2] 
00163                                                 << buf[4*i+1] << buf[4*i];
00164      std::cout << i << " " << std::hex << buf[4*i+3] << " " << buf[4*i+2] << " " 
00165                                                 << buf[4*i+1] << " " << buf[4*i] << std::endl;
00166     }
00167   //std::cout << "DDU Size: " << std::dec << theDDUHeader.sizeInWords() << std::endl;
00168 
00169   memcpy(&theDDUHeader, buf, theDDUHeader.sizeInWords()*2);
00170   
00171   if (debug) {
00172     LogTrace ("CSCDDUEventData|CSCRawToDigi") << "size of ddu header in words = " << theDDUHeader.sizeInWords();
00173     LogTrace ("CSCDDUEventData|CSCRawToDigi") << "sizeof(DDUHeader) = " << sizeof(theDDUHeader);
00174   }
00175   buf += theDDUHeader.sizeInWords();
00176   
00177 
00178 
00179 
00180   //std::cout << "sandrik dduID =" << theDDUHeader.source_id() << std::endl; 
00181   //int i=-1;
00182 
00183  
00184   // we really don't want to copy CSCEventData's while filling the vec
00185   theData.clear();
00186   theData.reserve(theDDUHeader.ncsc());
00187 
00188   if (examiner!= NULL) { // Use selective unpacking mode
00189 
00190     if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << "selective unpacking starting";
00191 
00192     // Find this DDU in examiner's DDUs list
00193     DDUIdType dduID = theDDUHeader.source_id(); 
00194         
00195     std::map<DDUIdType,std::map<CSCIdType,const uint16_t*> > ddus = examiner->DMB_block();
00196     std::map<DDUIdType,std::map<CSCIdType,const uint16_t*> >::iterator ddu_itr = ddus.find(dduID);
00197     uint16_t* dduBlock = (uint16_t*)((examiner->DDU_block())[dduID]);
00198     uint32_t dduBufSize = (examiner->DDU_size())[dduID];
00199                 
00200     if (ddu_itr != ddus.end() && dduBufSize!=0 && dduBlock==inputBuf) {
00201       std::map<CSCIdType,const uint16_t*> &cscs = ddu_itr->second;
00202       std::map<CSCIdType,const uint16_t*>::iterator csc_itr;
00203 
00204       for (csc_itr=cscs.begin(); csc_itr != cscs.end(); ++csc_itr) {
00205         short cscid = csc_itr->first;
00206 
00207         if(cscid != -1)
00208         {
00209           uint16_t* pos = (uint16_t*)csc_itr->second;
00210         
00211         
00212           ExaminerStatusType errors = examiner->errorsForChamber(cscid);
00213           if ((errors & examiner->getMask()) > 0 ) {    
00214                 if (debug) 
00215                 LogTrace ("CSCDDUEventData|CSCRawToDigi" )
00216                        << "skip unpacking of CSC " << cscid << " due format errors: 0x" << std::hex << errors << std::dec;
00217             continue;
00218           } 
00219         
00220           theData.push_back(CSCEventData(pos));
00221         }
00222       }
00223 
00224       if (debug)
00225         {
00226           LogTrace ("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
00227         }
00228       // std::cout << std::dec << theDDUTrailer.sizeInWords() << std::endl;
00229       // decode ddu tail
00230       memcpy(&theDDUTrailer, inputBuf+dduBufSize, theDDUTrailer.sizeInWords()*2);
00231       // memcpy(&theDDUTrailer, dduBlock+(dduBufSize-theDDUTrailer.sizeInWords())*2, theDDUTrailer.sizeInWords()*2);
00232       if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
00233       errorstat=theDDUTrailer.errorstat();
00234       if ((errorstat&errMask) != 0)
00235         {
00236           if (theDDUTrailer.check())
00237             {
00238               if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi")
00239                 << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
00240               if (debug) decodeStatus(errorstat);
00241             }
00242           else
00243             {
00244               if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi" )
00245                 << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
00246             }
00247         }
00248 
00249       if (debug) {
00250         LogTrace ("CSCDDUEventData|CSCRawToDigi")  << " Final errorstat " << std::hex << errorstat << std::dec ;
00251       }
00252       // the trailer counts in 64-bit words
00253 
00254       // theSizeInWords = dduBufSize;
00255       // buf=inputBuf+dduBufSize;
00256 
00257     }
00258     theSizeInWords = dduBufSize+12;
00259     buf=inputBuf+dduBufSize;
00260         
00261   } else {
00262 
00263 
00264     while( (((buf[0]&0xf000) == 0x9000)||((buf[0]&0xf000) == 0xa000)) 
00265            && (buf[3] != 0x8000)) {
00266         // ++i;
00267         if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << "unpack csc data loop started";
00268         theData.push_back(CSCEventData(buf));
00269         buf += (theData.back()).size();
00270         if (debug) {
00271             LogTrace ("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
00272         }
00273     }
00274  
00275     if (debug) {
00276         LogTrace ("CSCDDUEventData|CSCRawToDigi") << "unpacking ddu trailer ";
00277         LogTrace ("CSCDDUEventData|CSCRawToDigi") << std::hex << buf[3]<<" " << buf[2] 
00278                                          <<" " << buf[1]<<" " << buf[0];
00279     }
00280 
00281     // decode ddu tail
00282     memcpy(&theDDUTrailer, buf, theDDUTrailer.sizeInWords()*2);
00283     if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
00284     errorstat=theDDUTrailer.errorstat();
00285     if ((errorstat&errMask) != 0)  
00286       {
00287         if (theDDUTrailer.check())
00288           {
00289             if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi") 
00290               << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
00291             if (debug) decodeStatus(errorstat);
00292           } 
00293         else 
00294           {
00295             if (debug) LogTrace ("CSCDDUEventData|CSCRawToDigi" ) 
00296               << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
00297           }
00298       }
00299    
00300     if (debug) 
00301       LogTrace ("CSCDDUEventData|CSCRawToDigi")  << " Final errorstat " << std::hex << errorstat << std::dec ;
00302     // the trailer counts in 64-bit words
00303     buf += theDDUTrailer.sizeInWords();
00304   
00305     theSizeInWords = buf - inputBuf;
00306   }
00307 
00308 //std::cout << "DDUevData Size: " << theSizeInWords << " BUFlast: " << std::hex << inputBuf0[theSizeInWords-4] << //std::endl;
00310 theDDUTrailer0 = inputBuf0[theSizeInWords-4];
00311 }
00312 
00313 
00314 bool CSCDDUEventData::check() const 
00315 {
00316   // the trailer counts in 64-bit words
00317   if (debug)
00318     {
00319       LogTrace ("CSCDDUEventData|CSCRawToDigi") << sizeInWords();
00320       LogTrace ("CSCDDUEventData|CSCRawToDigi") << "wordcount = " << theDDUTrailer.wordcount()*4;
00321     }
00322 
00323   return theDDUHeader.check() && theDDUTrailer.check();
00324 }
00325 
00326 boost::dynamic_bitset<> CSCDDUEventData::pack() 
00327 {
00328   boost::dynamic_bitset<> result = bitset_utilities::ushortToBitset( theDDUHeader.sizeInWords()*16,
00329                                                                      theDDUHeader.data());
00330   //std::cout <<"SANDRIK inside DDUEvdata check = ";
00331   //theDDUHeader.check();
00332   //std::cout <<std::endl;
00333   //std::cout <<"printing out ddu header words"<<std::endl;
00334   //for (unsigned int i=0;i<theDDUHeader.sizeInWords();i++) {
00335   //  unsigned short * buf = theDDUHeader.data();
00336   //  printf("%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i]);
00337   //  i+=3;
00338   //}
00339   //std::cout <<"printing out ddu header words via bitset"<<std::endl;
00340   //bitset_utilities::printWords(result);
00341  
00342   for(unsigned int i = 0; i < theData.size(); ++i) 
00343     {
00344       result = bitset_utilities::append(result,theData[i].pack());
00345     }
00346   theSizeInWords = result.size()/16 + theDDUTrailer.sizeInWords();
00347   // 64-bit word count
00348   theDDUTrailer.setWordCount(theSizeInWords/4); 
00349   boost::dynamic_bitset<> dduTrailer = bitset_utilities::ushortToBitset ( theDDUTrailer.sizeInWords()*16, 
00350                                                                           theDDUTrailer.data());
00351   result =  bitset_utilities::append(result,dduTrailer);
00352 
00353   return result;
00354 }
00355