Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "EventFilter/CastorRawToDigi/interface/CastorCtdcUnpacker.h"
0002 #include "EventFilter/CastorRawToDigi/interface/CastorCTDCHeader.h"
0003 #include "EventFilter/CastorRawToDigi/interface/CastorCORData.h"
0004 #include "DataFormats/HcalDetId/interface/HcalOtherDetId.h"
0005 #include "DataFormats/HcalDigi/interface/HcalQIESample.h"
0006 #include "DataFormats/HcalDigi/interface/CastorDataFrame.h"
0007 #include "DataFormats/HcalDigi/interface/HcalTriggerPrimitiveSample.h"
0008 #include <iostream>
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 using namespace std;
0011 
0012 CastorCtdcUnpacker::CastorCtdcUnpacker(int sourceIdOffset, int beg, int end) : sourceIdOffset_(sourceIdOffset) {
0013   if (beg >= 0 && beg <= CastorDataFrame::MAXSAMPLES - 1) {
0014     startSample_ = beg;
0015   } else {
0016     startSample_ = 0;
0017   }
0018   if (end >= 0 && end <= CastorDataFrame::MAXSAMPLES - 1 && end >= beg) {
0019     endSample_ = end;
0020   } else {
0021     endSample_ = CastorDataFrame::MAXSAMPLES - 1;
0022   }
0023 }
0024 
0025 void CastorCtdcUnpacker::unpack(const FEDRawData& raw,
0026                                 const CastorElectronicsMap& emap,
0027                                 CastorRawCollections& colls,
0028                                 HcalUnpackerReport& report) {
0029   if (raw.size() < 16) {
0030     edm::LogWarning("Invalid Data") << "Empty/invalid DCC data, size = " << raw.size();
0031     return;
0032   }
0033 
0034   // get the CTDC header
0035   const CastorCTDCHeader* ctdcHeader = (const CastorCTDCHeader*)(raw.data());
0036   int ctdcid = ctdcHeader->getSourceId() - sourceIdOffset_;
0037 
0038   // space for unpacked data from one COR
0039   std::vector<unsigned short> precdata(CastorCORData::CHANNELS_PER_SPIGOT * CastorCORData::MAXIMUM_SAMPLES_PER_CHANNEL);
0040   std::vector<unsigned short> trigdata(CastorCORData::CHANNELS_PER_SPIGOT * CastorCORData::MAXIMUM_SAMPLES_PER_CHANNEL);
0041   std::vector<unsigned char> preclen(CastorCORData::CHANNELS_PER_SPIGOT);
0042   std::vector<unsigned char> triglen(CastorCORData::CHANNELS_PER_SPIGOT);
0043 
0044   // walk through the COR data...
0045   CastorCORData cor;
0046   for (int spigot = 0; spigot < CastorCTDCHeader::SPIGOT_COUNT; spigot++) {
0047     if (!ctdcHeader->getSpigotPresent(spigot))
0048       continue;
0049 
0050     int retval = ctdcHeader->getSpigotData(spigot, cor, raw.size());
0051     if (retval != 0) {
0052       if (retval == -1) {
0053         edm::LogWarning("Invalid Data") << "Invalid COR data (data beyond payload size) observed on spigot " << spigot
0054                                         << " of CTDC with source id " << ctdcHeader->getSourceId();
0055         report.countSpigotFormatError();
0056       }
0057       continue;
0058     }
0059     // check
0060     if (!cor.check()) {
0061       edm::LogWarning("Invalid Data") << "Invalid COR data observed on spigot " << spigot << " of CTDC with source id "
0062                                       << ctdcHeader->getSourceId();
0063       report.countSpigotFormatError();
0064       continue;
0065     }
0066     if (cor.isHistogramEvent()) {
0067       edm::LogWarning("Invalid Data") << "Histogram data passed to non-histogram unpacker on spigot " << spigot
0068                                       << " of CTDC with source id " << ctdcHeader->getSourceId();
0069       continue;
0070     }
0071     // calculate "real" number of presamples
0072     int nps = cor.getNPS() - startSample_;
0073 
0074     // new: do not use get pointers .. instead make use of CastorCORData::unpack
0075 
0076     cor.unpack(&(preclen[0]), &(precdata[0]), &(triglen[0]), &(trigdata[0]));
0077 
0078     /// work through all channels
0079     int ichan;
0080     for (ichan = 0; ichan < CastorCORData::CHANNELS_PER_SPIGOT; ichan++) {
0081       if (preclen[ichan] == 0 || preclen[ichan] & 0xc0)
0082         continue;
0083       int fiber = ichan / 3;
0084       int fiberchan = ichan % 3;
0085       // lookup the right channel
0086       CastorElectronicsId partialEid(fiberchan, fiber + 1, spigot, ctdcid);
0087       // does this partial id exist?
0088       CastorElectronicsId eid;
0089       HcalGenericDetId did;
0090       bool found;
0091       found = emap.lookup(partialEid, eid, did);
0092 
0093       if (found) {
0094         CastorDataFrame digi = CastorDataFrame(HcalCastorDetId(did));
0095         // set parameters - presamples
0096         digi.setPresamples(nps);
0097 
0098         int ntaken = 0;
0099         for (int sample = startSample_; sample <= endSample_; sample++) {
0100           digi.setSample(ntaken, precdata[ichan * CastorCORData::MAXIMUM_SAMPLES_PER_CHANNEL + sample]);
0101           ntaken++;
0102         }
0103         digi.setSize(ntaken);
0104         colls.castorCont->push_back(digi);
0105       } else {
0106         report.countUnmappedDigi();
0107         if (unknownIds_.find(partialEid) == unknownIds_.end()) {
0108           edm::LogWarning("CASTOR") << "CastorCtdcUnpacker: No match found for electronics partialEid :" << partialEid;
0109           unknownIds_.insert(partialEid);
0110         }
0111       }
0112     }
0113   }
0114 }