Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:14:35

0001 #include "EventFilter/CastorRawToDigi/interface/CastorUnpacker.h"
0002 #include "EventFilter/HcalRawToDigi/interface/HcalDCCHeader.h"
0003 #include "EventFilter/HcalRawToDigi/interface/HcalHTRData.h"
0004 #include "EventFilter/HcalRawToDigi/interface/HcalTTPUnpacker.h"
0005 #include "DataFormats/HcalDetId/interface/HcalOtherDetId.h"
0006 #include "DataFormats/HcalDigi/interface/HcalQIESample.h"
0007 #include "DataFormats/HcalDigi/interface/CastorDataFrame.h"
0008 #include "DataFormats/HcalDigi/interface/HcalTriggerPrimitiveSample.h"
0009 #include <iostream>
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 
0012 namespace CastorUnpacker_impl {
0013   template <class DigiClass>
0014   const HcalQIESample* unpack(const HcalQIESample* startPoint,
0015                               const HcalQIESample* limit,
0016                               DigiClass& digi,
0017                               int presamples,
0018                               const CastorElectronicsId& eid,
0019                               int startSample,
0020                               int endSample,
0021                               int expectedTime,
0022                               const HcalHTRData& hhd) {
0023     // set parameters
0024     digi.setPresamples(presamples);
0025     int fiber = startPoint->fiber();
0026     int fiberchan = startPoint->fiberChan();
0027     uint32_t zsmask = hhd.zsBunchMask() >> startSample;
0028     digi.setZSInfo(hhd.isUnsuppressed(), hhd.wasMarkAndPassZS(fiber, fiberchan), zsmask);
0029 
0030     //   digi.setReadoutIds(eid);
0031     //   setReadoutIds is missing in  CastorDataFrame class  digi.setReadoutIds(eid);
0032     if (expectedTime >= 0 && !hhd.isUnsuppressed()) {
0033       // std::cout << hhd.getFibOrbMsgBCN(fiber) << " " << expectedTime << " fiber="<<fiber<< std::endl;
0034       digi.setFiberIdleOffset(hhd.getFibOrbMsgBCN(fiber) - expectedTime);
0035     }
0036     // what is my sample number?
0037     int myFiberChan = startPoint->fiberAndChan();
0038     int ncurr = 0, ntaken = 0;
0039     const HcalQIESample* qie_work = startPoint;
0040     while (qie_work != limit && qie_work->fiberAndChan() == myFiberChan) {
0041       if (ncurr >= startSample && ncurr <= endSample) {
0042         digi.setSample(ntaken, *qie_work);
0043         ++ntaken;
0044       }
0045       ncurr++;
0046       qie_work++;
0047     }
0048     digi.setSize(ntaken);
0049     return qie_work;
0050   }
0051 }  // namespace CastorUnpacker_impl
0052 
0053 namespace {
0054   inline bool isTPGSOI(const HcalTriggerPrimitiveSample& s) { return (s.raw() & 0x200) != 0; }
0055 }  // namespace
0056 
0057 CastorUnpacker::CastorUnpacker(int sourceIdOffset, int beg, int end)
0058     : sourceIdOffset_(sourceIdOffset), expectedOrbitMessageTime_(-1) {
0059   if (beg >= 0 && beg <= CastorDataFrame::MAXSAMPLES - 1) {
0060     startSample_ = beg;
0061   } else {
0062     startSample_ = 0;
0063   }
0064   if (end >= 0 && end <= CastorDataFrame::MAXSAMPLES - 1 && end >= beg) {
0065     endSample_ = end;
0066   } else {
0067     endSample_ = CastorDataFrame::MAXSAMPLES - 1;
0068   }
0069 }
0070 
0071 static int slb(const HcalTriggerPrimitiveSample& theSample) { return ((theSample.raw() >> 13) & 0x7); }
0072 static int slbChan(const HcalTriggerPrimitiveSample& theSample) { return (theSample.raw() >> 11) & 0x3; }
0073 static int slbAndChan(const HcalTriggerPrimitiveSample& theSample) { return (theSample.raw() >> 11) & 0x1F; }
0074 
0075 void CastorUnpacker::unpack(const FEDRawData& raw,
0076                             const CastorElectronicsMap& emap,
0077                             CastorRawCollections& colls,
0078                             HcalUnpackerReport& report,
0079                             bool silent) {
0080   if (raw.size() < 16) {
0081     if (!silent)
0082       edm::LogWarning("Invalid Data") << "Empty/invalid DCC data, size = " << raw.size();
0083     return;
0084   }
0085 
0086   // get the DCC header
0087   const HcalDCCHeader* dccHeader = (const HcalDCCHeader*)(raw.data());
0088   int dccid = dccHeader->getSourceId() - sourceIdOffset_;
0089 
0090   // check the summary status
0091 
0092   // walk through the HTR data...
0093   HcalHTRData htr;
0094   const unsigned short *daq_first, *daq_last, *tp_first, *tp_last;
0095   const HcalQIESample *qie_begin, *qie_end, *qie_work;
0096   const HcalTriggerPrimitiveSample *tp_begin, *tp_end, *tp_work;
0097   for (int spigot = 0; spigot < HcalDCCHeader::SPIGOT_COUNT; spigot++) {
0098     if (!dccHeader->getSpigotPresent(spigot))
0099       continue;
0100 
0101     int retval = dccHeader->getSpigotData(spigot, htr, raw.size());
0102     if (retval != 0) {
0103       if (retval == -1) {
0104         if (!silent)
0105           edm::LogWarning("Invalid Data") << "Invalid HTR data (data beyond payload size) observed on spigot " << spigot
0106                                           << " of DCC with source id " << dccHeader->getSourceId();
0107         report.countSpigotFormatError();
0108       }
0109       continue;
0110     }
0111     // check
0112     if (dccHeader->getSpigotCRCError(spigot)) {
0113       if (!silent)
0114         edm::LogWarning("Invalid Data") << "CRC Error on HTR data observed on spigot " << spigot
0115                                         << " of DCC with source id " << dccHeader->getSourceId();
0116       report.countSpigotFormatError();
0117       continue;
0118     }
0119     if (!htr.check()) {
0120       if (!silent)
0121         edm::LogWarning("Invalid Data") << "Invalid HTR data observed on spigot " << spigot << " of DCC with source id "
0122                                         << dccHeader->getSourceId();
0123       report.countSpigotFormatError();
0124       continue;
0125     }
0126     if (htr.isHistogramEvent()) {
0127       if (!silent)
0128         edm::LogWarning("Invalid Data") << "Histogram data passed to non-histogram unpacker on spigot " << spigot
0129                                         << " of DCC with source id " << dccHeader->getSourceId();
0130       continue;
0131     }
0132     if ((htr.getFirmwareFlavor() & 0xE0) == 0x80) {  // some kind of TTP data
0133       if (colls.ttp != nullptr) {
0134         HcalTTPUnpacker ttpUnpack;
0135         colls.ttp->push_back(HcalTTPDigi());
0136         ttpUnpack.unpack(htr, colls.ttp->back());
0137       } else {
0138         LogDebug("CastorUnpackerHcalTechTrigProcessor")
0139             << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId()
0140             << " which is from the TechTrigProcessor (use separate unpacker!)";
0141       }
0142       continue;
0143     }
0144     if (htr.getFirmwareFlavor() >= 0x80) {
0145       if (!silent)
0146         edm::LogWarning("CastorUnpacker")
0147             << "Skipping data on spigot " << spigot << " of DCC with source id " << dccHeader->getSourceId()
0148             << " which is of unknown flavor " << htr.getFirmwareFlavor();
0149       continue;
0150     }
0151     // calculate "real" number of presamples
0152     int nps = htr.getNPS() - startSample_;
0153 
0154     // get pointers
0155     htr.dataPointers(&daq_first, &daq_last, &tp_first, &tp_last);
0156     unsigned int smid = htr.getSubmodule();
0157     int htr_tb = smid & 0x1;
0158     int htr_slot = (smid >> 1) & 0x1F;
0159     int htr_cr = (smid >> 6) & 0x1F;
0160 
0161     tp_begin = (const HcalTriggerPrimitiveSample*)tp_first;
0162     tp_end = (const HcalTriggerPrimitiveSample*)(tp_last + 1);  // one beyond last..
0163 
0164     /// work through the samples
0165     int currFiberChan = 0x3F;  // invalid fiber+channel...
0166     int ncurr = 0;
0167     bool valid = false;
0168     //////////////////////////////////////////////
0169     bool tpgSOIbitInUse = htr.getFormatVersion() >= 3;  // version 3 and later
0170     // bool isHOtpg=htr.getFormatVersion()>=3 && htr.getFirmwareFlavor()==0; // HO is flavor zero
0171     int npre = 0;
0172     /*
0173       Unpack the trigger primitives
0174     */
0175     // lookup the right channel
0176     bool dotp = true;
0177     CastorElectronicsId eid(0, 1, spigot, dccid);
0178     eid.setHTR(htr_cr, htr_slot, htr_tb);
0179     DetId did = emap.lookup(eid);
0180     if (did.null())
0181       dotp = false;
0182     HcalCastorDetId id1(did);
0183     HcalCastorDetId id((id1.zside() == 0), id1.sector(), 1);
0184     if (id1.module() > 12)
0185       dotp = false;
0186     if (dotp) {
0187       // std::cout << " tp_first="<< tp_first << " tp_last="<< tp_last<< " tb="<<htr_tb<<" slot="<<htr_slot<<" crate="<<htr_cr<<" dccid="<< dccid<< std::endl;
0188       // regular TPs (not HO)
0189       for (tp_work = tp_begin; tp_work != tp_end; tp_work++) {
0190         //    std::cout << "raw=0x"<<std::hex<< tp_work->raw()<<std::dec <<std::endl;
0191         if (tp_work->raw() == 0xFFFF)
0192           continue;                                   // filler word
0193         if (slbAndChan(*tp_work) != currFiberChan) {  // start new set
0194           npre = 0;
0195           currFiberChan = slbAndChan(*tp_work);
0196 
0197           // std::cout<< " NEW SET "<<std::endl;
0198           //HcalElectronicsId eid(tp_work->slbChan(),tp_work->slb(),spigot,dccid,htr_cr,htr_slot,htr_tb);
0199           //DetId did=emap.lookupTrigger(eid);
0200           //if (did.null()) {
0201           //report.countUnmappedTPDigi(eid);
0202           //if (unknownIdsTrig_.find(eid)==unknownIdsTrig_.end()) {
0203           //if (!silent) edm::LogWarning("HCAL") << "HcalUnpacker: No trigger primitive match found for electronics id :" << eid;
0204           //unknownIdsTrig_.insert(eid);
0205           //}
0206           //valid=false;
0207           //continue;
0208           //} else if (did==HcalTrigTowerDetId::Undefined ||
0209           //(did.det()==DetId::Hcal && did.subdetId()==0)) {
0210           //// known to be unmapped
0211           //valid=false;
0212           //continue;
0213           //}
0214 
0215           colls.tpCont->push_back(CastorTriggerPrimitiveDigi(id));
0216           // set the various bits
0217           if (!tpgSOIbitInUse)
0218             colls.tpCont->back().setPresamples(nps);
0219           colls.tpCont->back().setZSInfo(htr.isUnsuppressed(),
0220                                          htr.wasMarkAndPassZSTP(slb(*tp_work), slbChan(*tp_work)));
0221 
0222           // no hits recorded for current
0223           ncurr = 0;
0224           valid = true;
0225         }
0226         // add the word (if within settings or recent firmware [recent firmware ignores startSample/endSample])
0227         if (valid && ((tpgSOIbitInUse && ncurr < 10) || (ncurr >= startSample_ && ncurr <= endSample_))) {
0228           colls.tpCont->back().setSample(colls.tpCont->back().size(), *tp_work);
0229           colls.tpCont->back().setSize(colls.tpCont->back().size() + 1);
0230         }
0231         // set presamples,if SOI
0232         if (valid && tpgSOIbitInUse && isTPGSOI(*tp_work)) {
0233           colls.tpCont->back().setPresamples(ncurr);
0234         }
0235         ncurr++;
0236         npre++;
0237       }
0238     }
0239 
0240     //////////////////////////////////////////////
0241     qie_begin = (const HcalQIESample*)daq_first;
0242     qie_end = (const HcalQIESample*)(daq_last + 1);  // one beyond last..
0243 
0244     /// work through the samples
0245 
0246     for (qie_work = qie_begin; qie_work != qie_end;) {
0247       if (qie_work->raw() == 0xFFFF) {
0248         qie_work++;
0249         continue;  // filler word
0250       }
0251 
0252       // lookup the right channel
0253       CastorElectronicsId eid(qie_work->fiberChan(), qie_work->fiber(), spigot, dccid);
0254       eid.setHTR(htr_cr, htr_slot, htr_tb);
0255       DetId did = emap.lookup(eid);
0256 
0257       if (!did.null()) {
0258         colls.castorCont->push_back(CastorDataFrame(HcalCastorDetId(did)));
0259         qie_work = CastorUnpacker_impl::unpack<CastorDataFrame>(qie_work,
0260                                                                 qie_end,
0261                                                                 colls.castorCont->back(),
0262                                                                 nps,
0263                                                                 eid,
0264                                                                 startSample_,
0265                                                                 endSample_,
0266                                                                 expectedOrbitMessageTime_,
0267                                                                 htr);
0268       } else {
0269         report.countUnmappedDigi();
0270         if (unknownIds_.find(eid) == unknownIds_.end()) {
0271           if (!silent)
0272             edm::LogWarning("CASTOR") << "CastorUnpacker: No match found for electronics id :" << eid;
0273           unknownIds_.insert(eid);
0274         }
0275         for (int fiberC = qie_work->fiberAndChan(); qie_work != qie_end && qie_work->fiberAndChan() == fiberC;
0276              qie_work++)
0277           ;
0278       }
0279     }
0280   }
0281 }