Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-09 23:41:46

0001 #include <memory>
0002 
0003 // user include files
0004 
0005 #include "EventFilter/EcalDigiToRaw/interface/BlockFormatter.h"
0006 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0007 #include "DataFormats/EcalDetId/interface/EcalElectronicsId.h"
0008 
0009 using namespace std;
0010 
0011 BlockFormatter::BlockFormatter(Config const& iC, Params const& iP)
0012     : plistDCCId_{iC.plistDCCId_},
0013       orbit_number_{iP.orbit_number_},
0014       bx_{iP.bx_},
0015       lv1_{iP.lv1_},
0016       runnumber_{iP.runnumber_},
0017       debug_{iC.debug_},
0018       doBarrel_{iC.doBarrel_},
0019       doEndCap_{iC.doEndCap_},
0020       doTCC_{iC.doTCC_},
0021       doSR_{iC.doSR_},
0022       doTower_{iC.doTower_} {}
0023 
0024 void BlockFormatter::DigiToRaw(FEDRawDataCollection* productRawData) {
0025   auto const run_number = runnumber_;
0026   auto const bx = bx_;
0027   auto const lv1 = lv1_;
0028 
0029   if (debug_)
0030     cout << "in BlockFormatter::DigiToRaw  run_number orbit_number bx lv1 " << dec << run_number << " " << orbit_number_
0031          << " " << bx << " " << lv1 << endl;
0032 
0033   for (int idcc = 1; idcc <= 54; idcc++) {
0034     if ((!doBarrel_) && (idcc >= EcalElectronicsId::MIN_DCCID_EBM && idcc <= EcalElectronicsId::MAX_DCCID_EBP))
0035       continue;
0036     if ((!doEndCap_) && (idcc <= EcalElectronicsId::MAX_DCCID_EEM || idcc >= EcalElectronicsId::MIN_DCCID_EEP))
0037       continue;
0038 
0039     int FEDid = FEDNumbering::MINECALFEDID + idcc;
0040     FEDRawData& rawdata = productRawData->FEDData(FEDid);
0041     unsigned char* pData;
0042     short int DCC_ERRORS = 0;
0043 
0044     if (rawdata.size() == 0) {
0045       rawdata.resize(8);
0046       pData = rawdata.data();
0047 
0048       Word64 word = 0x18 + ((FEDid & 0xFFF) << 8) + ((Word64)((Word64)bx & 0xFFF) << 20) +
0049                     ((Word64)((Word64)lv1 & 0xFFFFFF) << 32) + (Word64)((Word64)0x51 << 56);
0050       Word64* pw = reinterpret_cast<Word64*>(const_cast<unsigned char*>(pData));
0051       *pw = word;  // DAQ header
0052 
0053       rawdata.resize(rawdata.size() + 8 * 8);  // DCC header
0054       pData = rawdata.data();
0055       pData[11] = DCC_ERRORS & 0xFF;
0056       pData[12] = run_number & 0xFF;
0057       pData[13] = (run_number >> 8) & 0xFF;
0058       pData[14] = (run_number >> 16) & 0xFF;
0059       pData[15] = 0x01;
0060 
0061       for (int i = 16; i <= 22; i++) {
0062         pData[i] = 0;  // to be filled for local data taking or calibration
0063       }
0064       pData[23] = 0x02;
0065       pData[24] = orbit_number_ & 0xFF;
0066       pData[25] = (orbit_number_ >> 8) & 0xFF;
0067       pData[26] = (orbit_number_ >> 16) & 0xFF;
0068       pData[27] = (orbit_number_ >> 24) & 0xFF;
0069       int SRenable_ = 1;
0070       int SR = SRenable_;
0071       int ZS = 0;
0072       int TZS = 0;
0073       // int SR_CHSTATUS = 0;
0074       pData[28] = (SR & 0x1) + ((ZS & 0x1) << 1) + ((TZS & 0x1) << 2);
0075       pData[31] = 0x03;
0076 
0077       for (int i = 0; i <= 4; i++) {
0078         for (int j = 0; j < 7; j++) {
0079           pData[32 + 8 * i + j] = 0;
0080         }
0081         pData[32 + 8 * i + 7] = 0x04;
0082       }
0083 
0084     }  // endif rawdatasize == 0
0085   }  // loop on id
0086 }
0087 
0088 void BlockFormatter::print(FEDRawData& rawdata) {
0089   int size = rawdata.size();
0090   cout << "Print RawData  size " << dec << size << endl;
0091   unsigned char* pData = rawdata.data();
0092 
0093   int n = size / 8;
0094   for (int i = 0; i < n; i++) {
0095     for (int j = 7; j >= 0; j--) {
0096       if (8 * i + j <= size)
0097         cout << hex << (int)pData[8 * i + j] << " ";
0098     }
0099     cout << endl;
0100   }
0101 }
0102 
0103 void BlockFormatter::CleanUp(FEDRawDataCollection* productRawData, map<int, map<int, int> >* FEDorder) {
0104   for (int id = 0; id < 36 + 18; id++) {
0105     if ((!doBarrel_) && (id >= 9 && id <= 44))
0106       continue;
0107     if ((!doEndCap_) && (id <= 8 || id >= 45))
0108       continue;
0109 
0110     int FEDid = FEDNumbering::MINECALFEDID + id + 1;
0111     FEDRawData& rawdata = productRawData->FEDData(FEDid);
0112 
0113     // ---- if raw need not be made for a given fed, set its size to empty and return
0114     if (find((*plistDCCId_).begin(), (*plistDCCId_).end(), (id + 1)) == (*plistDCCId_).end()) {
0115       rawdata.resize(0);
0116       continue;
0117     }
0118 
0119     // ---- Add the trailer word
0120     int lastline = rawdata.size();
0121     rawdata.resize(lastline + 8);
0122     unsigned char* pData = rawdata.data();
0123     int event_length = (lastline + 8) / 8;  // in 64 bits words
0124 
0125     pData[lastline + 7] = 0xa0;
0126     // pData[lastline+4] = event_length & 0xFFFFFF;
0127     pData[lastline + 4] = event_length & 0xFF;
0128     pData[lastline + 5] = (event_length >> 8) & 0xFF;
0129     pData[lastline + 6] = (event_length >> 16) & 0xFF;
0130     int event_status = 0;
0131     pData[lastline + 1] = event_status & 0x0F;
0132     int tts = 0 << 4;
0133     pData[lastline] = tts & 0xF0;
0134 
0135     // ---- Write the event length in the DCC header
0136     // pData[8] = event_length & 0xFFFFFF;
0137     pData[8] = event_length & 0xFF;
0138     pData[9] = (event_length >> 8) & 0xFF;
0139     pData[10] = (event_length >> 16) & 0xFF;
0140 
0141     // cout << " in BlockFormatter::CleanUp. FEDid = " << FEDid << " event_length*8 " << dec << event_length*8 << endl;
0142 
0143     map<int, map<int, int> >::iterator fen = FEDorder->find(FEDid);
0144 
0145     bool FED_has_data = true;
0146     if (fen == FEDorder->end())
0147       FED_has_data = false;
0148     if (debug_ && (!FED_has_data))
0149       cout << " FEDid is not in FEDorder ! " << endl;
0150     if (!FED_has_data) {
0151       int ch_status = 7;
0152       for (int iFE = 1; iFE <= 68; iFE++) {
0153         int irow = (iFE - 1) / 14;
0154         int kval = ((iFE - 1) % 14) / 2;
0155         if (iFE % 2 == 1)
0156           pData[32 + 8 * irow + kval] |= ch_status & 0xFF;
0157         else
0158           pData[32 + 8 * irow + kval] |= ((ch_status << 4) & 0xFF);
0159       }
0160     }
0161 
0162     if (FED_has_data) {
0163       map<int, int>& FEorder = (*fen).second;
0164 
0165       for (int iFE = 1; iFE <= 68; iFE++) {
0166         map<int, int>::iterator fe = FEorder.find(iFE);
0167         int ch_status = 0;
0168         if (fe == FEorder.end())  // FE not present due to SRP, update CH_status
0169           ch_status = 7;          // CH_SUPPRESS
0170         int irow = (iFE - 1) / 14;
0171         int kval = ((iFE - 1) % 14) / 2;
0172         if (iFE % 2 == 1)
0173           pData[32 + 8 * irow + kval] |= ch_status & 0xFF;
0174         else
0175           pData[32 + 8 * irow + kval] |= ((ch_status << 4) & 0xFF);
0176       }
0177     }
0178   }
0179 }
0180 
0181 void BlockFormatter::PrintSizes(FEDRawDataCollection* productRawData) {
0182   for (int id = 0; id < 36 + 18; id++) {
0183     // if ( (! doBarrel_) && (id >= 9 && id <= 44)) continue;
0184     // if ( (! doEndCap_) && (id <= 8 || id >= 45)) continue;
0185 
0186     int FEDid = FEDNumbering::MINECALFEDID + id;
0187     FEDRawData& rawdata = productRawData->FEDData(FEDid);
0188     if (rawdata.size() > 0)
0189       cout << "Size of FED id " << dec << FEDid << " is : " << dec << rawdata.size() << endl;
0190   }
0191 }