Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-07 05:50:20

0001 /** \file CSCDCCEventData.cc
0002  *
0003  *  $Date: 2010/06/11 15:50:28 $
0004  *  $Revision: 1.31 $
0005  *  \author A. Tumanov - Rice - But long, long ago...
0006  *
0007  */
0008 
0009 #include "EventFilter/CSCRawToDigi/interface/CSCDCCEventData.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include <iostream>
0012 #include <cstdio>
0013 #include "EventFilter/CSCRawToDigi/interface/bitset_append.h"
0014 
0015 #ifdef LOCAL_UNPACK
0016 bool CSCDCCEventData::debug = false;
0017 #else
0018 #include <atomic>
0019 std::atomic<bool> CSCDCCEventData::debug{false};
0020 #endif
0021 
0022 CSCDCCEventData::CSCDCCEventData(int sourceId, int nDDUs, int bx, int l1a) : theDCCHeader(bx, l1a, sourceId) {
0023   theDDUData.reserve(nDDUs);
0024 }
0025 
0026 CSCDCCEventData::CSCDCCEventData(const uint16_t* buf, CSCDCCExaminer* examiner) { unpack_data(buf, examiner); }
0027 
0028 CSCDCCEventData::~CSCDCCEventData() {}
0029 
0030 void CSCDCCEventData::unpack_data(const uint16_t* buf, CSCDCCExaminer* examiner) {
0031   /*
0032   for (int i=0;i<20;i++) {
0033     printf("%04x %04x %04x %04x\n",buf[i+3],buf[i+2],buf[i+1],buf[i]);
0034     i+=3;
0035   }
0036 */
0037 
0038   theDDUData.clear();
0039   if (debug)
0040     LogTrace("CSCDCCEventData|CSCRawToDigi") << "CSCDCCEventData::unpack_data() is called";
0041 
0042   // decode DCC header (128 bits)
0043   if (debug)
0044     LogTrace("CSCDCCEventData|CSCRawToDigi") << "unpacking dcc header...";
0045   theDCCHeader.setFromBuffer(buf);
0046   //theDCCHeader = CSCDCCHeader(buf); // direct unpacking instead of bitfields
0047   buf += theDCCHeader.sizeInWords();
0048 
0049   //std::cout <<"Sandrik DCC Id = " << theDCCHeader.getCDFSourceId()  << std::endl;
0050 
0051   ///loop over DDUEventDatas
0052   while ((buf[7] == 0x8000) && (buf[6] == 0x0001) && (buf[5] == 0x8000)) {
0053     CSCDDUEventData dduEventData(buf, examiner);
0054     //  CSCDDUEventData dduEventData(buf);
0055 
0056     if (debug)
0057       LogTrace("CSCDCCEventData|CSCRawToDigi") << " checking ddu data integrity ";
0058     if (dduEventData.check()) {
0059       theDDUData.push_back(dduEventData);
0060       buf += dduEventData.sizeInWords();
0061     } else {
0062       if (debug)
0063         LogTrace("CSCDCCEventData|CSCRawToDigi") << "DDU Data Check failed!  ";
0064       break;
0065     }
0066   }
0067 
0068   if (debug) {
0069     LogTrace("CSCDCCEventData|CSCRawToDigi") << "unpacking dcc trailer ";
0070     LogTrace("CSCDCCEventData|CSCRawToDigi") << std::hex << buf[3] << " " << buf[2] << " " << buf[1] << " " << buf[0];
0071   }
0072 
0073   //decode dcc trailer (128 bits)
0074   if (debug)
0075     LogTrace("CSCDCCEventData|CSCRawToDigi") << "decoding DCC trailer";
0076   theDCCTrailer.setFromBuffer(buf);
0077   if (debug)
0078     LogTrace("CSCDCCEventData|CSCRawToDigi") << "checking DDU Trailer" << theDCCTrailer.check();
0079 
0080   // buf += theDCCTrailer.sizeInWords(); /* =VB= Commented out to please static analyzer */
0081 
0082   //std::cout << " DCC Size: " << std::dec << theSizeInWords << std::endl;
0083   //std::cout << "LastBuf: "  << std::hex << inputBuf[theSizeInWords-4] << std::endl;
0084 }
0085 
0086 bool CSCDCCEventData::check() const {
0087   // the trailer counts in 64-bit words
0088   if (debug) {
0089     LogTrace("CSCDCCEventData|CSCRawToDigi") << "size in Words () = " << std::dec << sizeInWords();
0090   }
0091 
0092   return theDCCHeader.check() && theDCCTrailer.check();
0093 }
0094 
0095 void CSCDCCEventData::addChamber(
0096     CSCEventData& chamber, int dduID, int dduSlot, int dduInput, int dmbID, uint16_t format_version) {
0097   // first, find this DDU
0098   int dduIndex = -1;
0099   int nDDUs = theDDUData.size();
0100   for (int i = 0; dduIndex == -1 && i < nDDUs; ++i) {
0101     if (theDDUData[i].header().source_id() == dduID)
0102       dduIndex = i;
0103   }
0104 
0105   /// Set DDU format_version field in header depending on desired format version
0106   unsigned ddu_fmt_version = 0x6;  // 2005 Format
0107   if (format_version >= 2013)
0108     ddu_fmt_version = 0x7;  /// 2013 Format
0109 
0110   if (dduIndex == -1) {
0111     // make a new one
0112     CSCDDUHeader newDDUHeader(
0113         dccHeader().getCDFBunchCounter(), dccHeader().getCDFEventNumber(), dduID, ddu_fmt_version);
0114     theDDUData.push_back(CSCDDUEventData(newDDUHeader));
0115     dduIndex = nDDUs;
0116     dccHeader().setDAV(dduSlot);
0117   }
0118   theDDUData[dduIndex].add(chamber, dmbID, dduInput, format_version);
0119 }
0120 
0121 boost::dynamic_bitset<> CSCDCCEventData::pack() {
0122   boost::dynamic_bitset<> result(theDCCHeader.sizeInWords() * 16);
0123   result = bitset_utilities::ushortToBitset(theDCCHeader.sizeInWords() * 16, theDCCHeader.data());
0124   //std::cout <<"SANDRIK DCC size of header  in words"<< theDCCHeader.sizeInWords()*16 <<std::endl;
0125   //std::cout <<"SANDRIK DCC size of header in bits"<< result.size()<<std::endl;
0126   //for(size_t i = 0; i < result.size(); ++i) {
0127   //  std::cout<<result[i];
0128   //  if (((i+1)%32)==0) std::cout<<std::endl;
0129   //}
0130 
0131   for (size_t i = 0; i < theDDUData.size(); ++i) {
0132     result = bitset_utilities::append(result, theDDUData[i].pack());
0133     //std::cout <<"SANDRIK here is ddu data check ";
0134     //theDDUData[i].header().check();
0135     //std::cout <<std::endl;
0136     //bitset_utilities::printWords(result);
0137   }
0138 
0139   //std::cout <<"SANDRIK packed dcc size is "<<result.size()<<std::endl;
0140   //for(size_t i = 0; i < result.size(); ++i) {
0141   //  std::cout<<result[i];
0142   //  if (((i+1)%32)==0) std::cout<<std::endl;
0143   //}
0144 
0145   boost::dynamic_bitset<> dccTrailer =
0146       bitset_utilities::ushortToBitset(theDCCTrailer.sizeInWords() * 16, theDCCTrailer.data());
0147   result = bitset_utilities::append(result, dccTrailer);
0148   //  bitset_utilities::printWords(result);
0149   return result;
0150 }