Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:45:11

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           if (pos == nullptr) {
0197             if (debug)
0198               LogTrace("CSCDDUEventData|CSCRawToDigi")
0199                   << "skip unpacking of CSC " << cscid << " due to format errors (NULL pointer to chamber data)";
0200             continue;
0201           }
0202 
0203           ExaminerStatusType errors = examiner->errorsForChamber(cscid);
0204           if ((errors & examiner->getMask()) > 0) {
0205             if (debug)
0206               LogTrace("CSCDDUEventData|CSCRawToDigi")
0207                   << "skip unpacking of CSC " << cscid << " due to format errors: 0x" << std::hex << errors << std::dec;
0208             continue;
0209           }
0210 
0211           theData.push_back(CSCEventData(pos, theFormatVersion));
0212         }
0213       }
0214 
0215       if (debug) {
0216         LogTrace("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
0217       }
0218       // decode ddu tail
0219       theDDUTrailer.setFromBuffer(inputBuf + dduBufSize);
0220       // memcpy(&theDDUTrailer, dduBlock+(dduBufSize-theDDUTrailer.sizeInWords())*2, theDDUTrailer.sizeInWords()*2);
0221       if (debug)
0222         LogTrace("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
0223       errorstat = theDDUTrailer.errorstat();
0224       if ((errorstat & errMask) != 0) {
0225         if (theDDUTrailer.check()) {
0226           if (debug)
0227             LogTrace("CSCDDUEventData|CSCRawToDigi")
0228                 << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
0229           if (debug)
0230             decodeStatus(errorstat);
0231         } else {
0232           if (debug)
0233             LogTrace("CSCDDUEventData|CSCRawToDigi")
0234                 << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
0235         }
0236       }
0237 
0238       if (debug) {
0239         LogTrace("CSCDDUEventData|CSCRawToDigi") << " Final errorstat " << std::hex << errorstat << std::dec;
0240       }
0241       // the trailer counts in 64-bit words
0242 
0243       // theSizeInWords = dduBufSize;
0244       // buf=inputBuf+dduBufSize;
0245     }
0246     theSizeInWords = dduBufSize + 12;
0247 
0248   } else {
0249     while ((((buf[0] & 0xf000) == 0x9000) || ((buf[0] & 0xf000) == 0xa000)) && (buf[3] != 0x8000)) {
0250       // ++i;
0251       if (debug)
0252         LogTrace("CSCDDUEventData|CSCRawToDigi") << "unpack csc data loop started";
0253       theData.push_back(CSCEventData(buf, theFormatVersion));
0254       buf += (theData.back()).size();
0255       if (debug) {
0256         LogTrace("CSCDDUEventData|CSCRawToDigi") << "size of vector of cscData = " << theData.size();
0257       }
0258     }
0259 
0260     if (debug) {
0261       LogTrace("CSCDDUEventData|CSCRawToDigi") << "unpacking ddu trailer ";
0262       LogTrace("CSCDDUEventData|CSCRawToDigi") << std::hex << buf[3] << " " << buf[2] << " " << buf[1] << " " << buf[0];
0263     }
0264 
0265     // decode ddu tail
0266     theDDUTrailer.setFromBuffer(buf);
0267     if (debug)
0268       LogTrace("CSCDDUEventData|CSCRawToDigi") << theDDUTrailer.check();
0269     errorstat = theDDUTrailer.errorstat();
0270     if ((errorstat & errMask) != 0) {
0271       if (theDDUTrailer.check()) {
0272         if (debug)
0273           LogTrace("CSCDDUEventData|CSCRawToDigi")
0274               << "+++ CSCDDUEventData warning: DDU Trailer errors = " << std::hex << errorstat << " +++ ";
0275         if (debug)
0276           decodeStatus(errorstat);
0277       } else {
0278         if (debug)
0279           LogTrace("CSCDDUEventData|CSCRawToDigi")
0280               << " Unpacking lost DDU trailer - check() failed and 8 8 ffff 8 was not found ";
0281       }
0282     }
0283 
0284     if (debug)
0285       LogTrace("CSCDDUEventData|CSCRawToDigi") << " Final errorstat " << std::hex << errorstat << std::dec;
0286     // the trailer counts in 64-bit words
0287     buf += theDDUTrailer.sizeInWords();
0288 
0289     theSizeInWords = buf - inputBuf;
0290   }
0291 
0292   /// Pack Trailer 0 (to access TTS)
0293   theDDUTrailer0 = inputBuf0[theSizeInWords - 4];
0294 }
0295 
0296 bool CSCDDUEventData::check() const {
0297   // the trailer counts in 64-bit words
0298   if (debug) {
0299     LogTrace("CSCDDUEventData|CSCRawToDigi") << sizeInWords();
0300     LogTrace("CSCDDUEventData|CSCRawToDigi") << "wordcount = " << theDDUTrailer.wordcount() * 4;
0301   }
0302 
0303   return theDDUHeader.check() && theDDUTrailer.check();
0304 }
0305 
0306 boost::dynamic_bitset<> CSCDDUEventData::pack() {
0307   boost::dynamic_bitset<> result =
0308       bitset_utilities::ushortToBitset(theDDUHeader.sizeInWords() * 16, theDDUHeader.data());
0309   //std::cout <<"SANDRIK inside DDUEvdata check = ";
0310   //theDDUHeader.check();
0311   //std::cout <<std::endl;
0312   //std::cout <<"printing out ddu header words"<<std::endl;
0313   //for (unsigned int i=0;i<theDDUHeader.sizeInWords();i++) {
0314   //  unsigned short * buf = theDDUHeader.data();
0315   //  printf("%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i]);
0316   //  i+=3;
0317   //}
0318   //std::cout <<"printing out ddu header words via bitset"<<std::endl;
0319   //bitset_utilities::printWords(result);
0320 
0321   for (unsigned int i = 0; i < theData.size(); ++i) {
0322     result = bitset_utilities::append(result, theData[i].pack());
0323   }
0324   theSizeInWords = result.size() / 16 + theDDUTrailer.sizeInWords();
0325   // 64-bit word count
0326   theDDUTrailer.setWordCount(theSizeInWords / 4);
0327   boost::dynamic_bitset<> dduTrailer =
0328       bitset_utilities::ushortToBitset(theDDUTrailer.sizeInWords() * 16, theDDUTrailer.data());
0329   result = bitset_utilities::append(result, dduTrailer);
0330 
0331   return result;
0332 }