Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-05 02:29:12

0001 /* CSCDDUEventData.cc
0002  * Modified 4/21/03 to get rid of arrays and store all CSC data
0003  * in vectors.
0004  * A.Tumanov
0005  */
0006 
0007 #include "EventFilter/CSCRawToDigi/interface/CSCDDUEventData.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 #include <iostream>
0011 #include <cstdio>
0012 
0013 #include "EventFilter/CSCRawToDigi/interface/bitset_append.h"
0014 
0015 #ifdef LOCAL_UNPACK
0016 bool CSCDDUEventData::debug = false;
0017 uint32_t CSCDDUEventData::errMask = 0xFFFFFFFF;
0018 #else
0019 #include <atomic>
0020 std::atomic<bool> CSCDDUEventData::debug{false};
0021 std::atomic<uint32_t> CSCDDUEventData::errMask{0xFFFFFFFF};
0022 #endif
0023 
0024 CSCDDUEventData::CSCDDUEventData(const CSCDDUHeader& header) { theDDUHeader = header; }
0025 
0026 CSCDDUEventData::CSCDDUEventData(const uint16_t* buf, CSCDCCExaminer* examiner) { unpack_data(buf, examiner); }
0027 
0028 CSCDDUEventData::~CSCDDUEventData() {}
0029 
0030 void CSCDDUEventData::add(CSCEventData& cscData, int dmbId, int dduInput, unsigned format_version) {
0031   theDDUHeader.setDMBDAV(dduInput);
0032   //@@ Tim: The following sets the word which is supposed to be CSCs in error, with bit 15 set for DMB Full
0033   //@@ so I think sim should not set it at all
0034   //@@  theDDUTrailer.setDMBDAV(dmbId);
0035   theDDUHeader.setDMBDAV(dduInput);
0036   cscData.dmbHeader()->setdmbID(dmbId);
0037   cscData.setEventInformation(theDDUHeader.bxnum(), theDDUHeader.lvl1num());
0038   theData.push_back(cscData);
0039 }
0040 
0041 void CSCDDUEventData::decodeStatus() const { this->decodeStatus(theDDUTrailer.errorstat()); }
0042 
0043 void CSCDDUEventData::decodeStatus(int code) const {
0044   // JRG is Jason Gilmore
0045   // JRG, low-order 16-bit status (most serious errors):
0046   if ((code & errMask) > 0) {  ///this is a mask for printing out errors
0047     // JRG, low-order 16-bit status (most serious errors):
0048     if ((code & 0x0000F000) > 0) {
0049       if ((0x00008000 & code) > 0)
0050         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Critical Error, ** needs reset **";
0051       if ((0x00004000 & code) > 0)
0052         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Single Error, bad event";
0053       if ((0x00002000 & code) > 0)
0054         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Single Warning";
0055       if ((0x00001000 & code) > 0)
0056         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Near Full Warning";
0057     }
0058     if ((code & 0x00000F00) > 0) {
0059       if ((0x00000800 & code) > 0)
0060         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU 64-bit Alignment Error";
0061       if ((0x00000400 & code) > 0)
0062         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Control DLL Error occured";
0063       if ((0x00000200 & code) > 0)
0064         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU DMB Error occurred";
0065       if ((0x00000100 & code) > 0)
0066         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Lost In Event Error";
0067     }
0068     if ((code & 0x000000F0) > 0) {
0069       if ((0x00000080 & code) > 0)
0070         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Lost In Data Error occurred";
0071       if ((0x00000040 & code) > 0)
0072         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Timeout Error";
0073       if ((0x00000020 & code) > 0)
0074         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   TMB or ALCT CRC Error";
0075       if ((0x00000010 & code) > 0)
0076         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Multiple Transmit Errors";
0077     }
0078     if ((code & 0x0000000F) > 0) {
0079       if ((0x00000008 & code) > 0)
0080         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Sync Lost or FIFO Full Error";
0081       if ((0x00000004 & code) > 0)
0082         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Fiber/FIFO Connection Error";
0083       if ((0x00000002 & code) > 0)
0084         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU L1A Match Error";
0085       if ((0x00000001 & code) > 0)
0086         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU DMB or CFEB CRC Error";
0087     }
0088     if ((code & 0xF0000000) > 0) {
0089       // JRG, high-order 16-bit status (not-so-serious errors):
0090       if ((0x80000000 & code) > 0)
0091         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU DMB LCT/DAV/Movlp Mismatch";
0092       if ((0x40000000 & code) > 0)
0093         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU-CFEB L1 Mismatch";
0094       if ((0x20000000 & code) > 0)
0095         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU saw no good DMB CRCs";
0096       if ((0x10000000 & code) > 0)
0097         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU CFEB Count Error";
0098     }
0099     if ((code & 0x0F000000) > 0) {
0100       if ((0x08000000 & code) > 0)
0101         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU FirstDat Error";
0102       if ((0x04000000 & code) > 0)
0103         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU L1A-FIFO Full Error";
0104       if ((0x02000000 & code) > 0)
0105         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Data Stuck in FIFO";
0106       if ((0x01000000 & code) > 0)
0107         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU NoLiveFibers Error";
0108     }
0109     if ((code & 0x00F00000) > 0) {
0110       if ((0x00800000 & code) > 0)
0111         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Spwd single-bit Warning";
0112       if ((0x00400000 & code) > 0)
0113         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Input FPGA Error";
0114       if ((0x00200000 & code) > 0)
0115         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU DAQ Stop bit set";
0116       if ((0x00100000 & code) > 0)
0117         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU DAQ says Not Ready";
0118       if ((0x00300000 & code) == 0x00200000)
0119         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU DAQ Applied Backpressure";
0120     }
0121     if ((code & 0x000F0000) > 0) {
0122       if ((0x00080000 & code) > 0)
0123         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU TMB Error";
0124       if ((0x00040000 & code) > 0)
0125         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU ALCT Error";
0126       if ((0x00020000 & code) > 0)
0127         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Trigger Readout Wordcount Error";
0128       if ((0x00010000 & code) > 0)
0129         LogTrace("CSCDDUEventData|CSCRawToDigi") << "   DDU Trigger L1A Match Error";
0130     }
0131   }
0132 }
0133 
0134 void CSCDDUEventData::unpack_data(const uint16_t* buf, CSCDCCExaminer* examiner) {
0135   // just to calculate length
0136   const uint16_t* inputBuf = buf;
0137   const uint16_t* inputBuf0 = buf;  /// To pack trailer 0
0138   theData.clear();
0139   if (debug)
0140     LogTrace("CSCDDUEventData|CSCRawToDigi") << "CSCDDUEventData::unpack_data() is called";
0141   if (debug)
0142     for (int i = 0; i < 6; ++i) {
0143       LogTrace("CSCDDUEventData|CSCRawToDigi")
0144           << i << std::hex << buf[4 * i + 3] << buf[4 * i + 2] << buf[4 * i + 1] << buf[4 * i];
0145       std::cout << i << " " << std::hex << buf[4 * i + 3] << " " << buf[4 * i + 2] << " " << buf[4 * i + 1] << " "
0146                 << buf[4 * i] << std::endl;
0147     }
0148 
0149   theDDUHeader.setFromBuffer(buf);
0150 
0151   if (debug) {
0152     LogTrace("CSCDDUEventData|CSCRawToDigi") << "size of ddu header in words = " << theDDUHeader.sizeInWords();
0153     LogTrace("CSCDDUEventData|CSCRawToDigi") << "sizeof(DDUHeader) = " << sizeof(theDDUHeader);
0154   }
0155   buf += theDDUHeader.sizeInWords();
0156 
0157   // if (theDDUHeader.format_version() >= 0x6)
0158   if (theDDUHeader.format_version() == 0x7)  /// New Data Format 2013
0159   {
0160     theFormatVersion = 2013;
0161   } else if (theDDUHeader.format_version() <= 0x6)  /// Older Data format before 2013
0162   {
0163     theFormatVersion = 2005;
0164   } else  // Add handling for any other format version
0165   {
0166     theFormatVersion = 2013;
0167   }
0168 
0169   // we really don't want to copy CSCEventData's while filling the vec
0170   theData.clear();
0171   theData.reserve(theDDUHeader.ncsc());
0172 
0173   if (examiner != nullptr) {  // Use selective unpacking mode
0174 
0175     if (debug)
0176       LogTrace("CSCDDUEventData|CSCRawToDigi") << "selective unpacking starting";
0177 
0178     // Find this DDU in examiner's DDUs list
0179     DDUIdType dduID = theDDUHeader.source_id();
0180 
0181     std::map<DDUIdType, std::map<CSCIdType, const uint16_t*> > ddus = examiner->DMB_block();
0182     std::map<DDUIdType, std::map<CSCIdType, const uint16_t*> >::iterator ddu_itr = ddus.find(dduID);
0183     const uint16_t* dduBlock = (const uint16_t*)((examiner->DDU_block())[dduID]);
0184     uint32_t dduBufSize = (examiner->DDU_size())[dduID];
0185 
0186     if (ddu_itr != ddus.end() && dduBufSize != 0 && dduBlock == inputBuf) {
0187       std::map<CSCIdType, const uint16_t*>& cscs = ddu_itr->second;
0188       std::map<CSCIdType, const uint16_t*>::iterator csc_itr;
0189 
0190       for (csc_itr = cscs.begin(); csc_itr != cscs.end(); ++csc_itr) {
0191         short cscid = csc_itr->first;
0192 
0193         if (cscid != -1) {
0194           const uint16_t* pos = (const uint16_t*)csc_itr->second;
0195 
0196           ExaminerStatusType errors = examiner->errorsForChamber(cscid);
0197           if ((errors & examiner->getMask()) > 0) {
0198             if (debug)
0199               LogTrace("CSCDDUEventData|CSCRawToDigi")
0200                   << "skip unpacking of CSC " << cscid << " due format errors: 0x" << std::hex << errors << std::dec;
0201             continue;
0202           }
0203 
0204           theData.push_back(CSCEventData(pos, theFormatVersion));
0205         }
0206       }
0207 
0208       if (debug) {
0209         LogTrace("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
0210       }
0211       // decode ddu tail
0212       theDDUTrailer.setFromBuffer(inputBuf + dduBufSize);
0213       // memcpy(&theDDUTrailer, dduBlock+(dduBufSize-theDDUTrailer.sizeInWords())*2, theDDUTrailer.sizeInWords()*2);
0214       if (debug)
0215         LogTrace("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
0216       errorstat = theDDUTrailer.errorstat();
0217       if ((errorstat & errMask) != 0) {
0218         if (theDDUTrailer.check()) {
0219           if (debug)
0220             LogTrace("CSCDDUEventData|CSCRawToDigi")
0221                 << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
0222           if (debug)
0223             decodeStatus(errorstat);
0224         } else {
0225           if (debug)
0226             LogTrace("CSCDDUEventData|CSCRawToDigi")
0227                 << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
0228         }
0229       }
0230 
0231       if (debug) {
0232         LogTrace("CSCDDUEventData|CSCRawToDigi") << " Final errorstat " << std::hex << errorstat << std::dec;
0233       }
0234       // the trailer counts in 64-bit words
0235 
0236       // theSizeInWords = dduBufSize;
0237       // buf=inputBuf+dduBufSize;
0238     }
0239     theSizeInWords = dduBufSize + 12;
0240 
0241   } else {
0242     while ((((buf[0] & 0xf000) == 0x9000) || ((buf[0] & 0xf000) == 0xa000)) && (buf[3] != 0x8000)) {
0243       // ++i;
0244       if (debug)
0245         LogTrace("CSCDDUEventData|CSCRawToDigi") << "unpack csc data loop started";
0246       theData.push_back(CSCEventData(buf, theFormatVersion));
0247       buf += (theData.back()).size();
0248       if (debug) {
0249         LogTrace("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
0250       }
0251     }
0252 
0253     if (debug) {
0254       LogTrace("CSCDDUEventData|CSCRawToDigi") << "unpacking ddu trailer ";
0255       LogTrace("CSCDDUEventData|CSCRawToDigi") << std::hex << buf[3] << " " << buf[2] << " " << buf[1] << " " << buf[0];
0256     }
0257 
0258     // decode ddu tail
0259     theDDUTrailer.setFromBuffer(buf);
0260     if (debug)
0261       LogTrace("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
0262     errorstat = theDDUTrailer.errorstat();
0263     if ((errorstat & errMask) != 0) {
0264       if (theDDUTrailer.check()) {
0265         if (debug)
0266           LogTrace("CSCDDUEventData|CSCRawToDigi")
0267               << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
0268         if (debug)
0269           decodeStatus(errorstat);
0270       } else {
0271         if (debug)
0272           LogTrace("CSCDDUEventData|CSCRawToDigi")
0273               << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
0274       }
0275     }
0276 
0277     if (debug)
0278       LogTrace("CSCDDUEventData|CSCRawToDigi") << " Final errorstat " << std::hex << errorstat << std::dec;
0279     // the trailer counts in 64-bit words
0280     buf += theDDUTrailer.sizeInWords();
0281 
0282     theSizeInWords = buf - inputBuf;
0283   }
0284 
0285   /// Pack Trailer 0 (to access TTS)
0286   theDDUTrailer0 = inputBuf0[theSizeInWords - 4];
0287 }
0288 
0289 bool CSCDDUEventData::check() const {
0290   // the trailer counts in 64-bit words
0291   if (debug) {
0292     LogTrace("CSCDDUEventData|CSCRawToDigi") << sizeInWords();
0293     LogTrace("CSCDDUEventData|CSCRawToDigi") << "wordcount = " << theDDUTrailer.wordcount() * 4;
0294   }
0295 
0296   return theDDUHeader.check() && theDDUTrailer.check();
0297 }
0298 
0299 boost::dynamic_bitset<> CSCDDUEventData::pack() {
0300   boost::dynamic_bitset<> result =
0301       bitset_utilities::ushortToBitset(theDDUHeader.sizeInWords() * 16, theDDUHeader.data());
0302   //std::cout <<"SANDRIK inside DDUEvdata check = ";
0303   //theDDUHeader.check();
0304   //std::cout <<std::endl;
0305   //std::cout <<"printing out ddu header words"<<std::endl;
0306   //for (unsigned int i=0;i<theDDUHeader.sizeInWords();i++) {
0307   //  unsigned short * buf = theDDUHeader.data();
0308   //  printf("%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i]);
0309   //  i+=3;
0310   //}
0311   //std::cout <<"printing out ddu header words via bitset"<<std::endl;
0312   //bitset_utilities::printWords(result);
0313 
0314   for (unsigned int i = 0; i < theData.size(); ++i) {
0315     result = bitset_utilities::append(result, theData[i].pack());
0316   }
0317   theSizeInWords = result.size() / 16 + theDDUTrailer.sizeInWords();
0318   // 64-bit word count
0319   theDDUTrailer.setWordCount(theSizeInWords / 4);
0320   boost::dynamic_bitset<> dduTrailer =
0321       bitset_utilities::ushortToBitset(theDDUTrailer.sizeInWords() * 16, theDDUTrailer.data());
0322   result = bitset_utilities::append(result, dduTrailer);
0323 
0324   return result;
0325 }