Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "EventFilter/CastorRawToDigi/plugins/CastorRawToDigi.h"
0002 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0003 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0004 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
0005 #include "FWCore/Framework/interface/ESHandle.h"
0006 #include "FWCore/Framework/interface/Run.h"
0007 #include <fstream>
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include <iostream>
0010 #include "EventFilter/CastorRawToDigi/interface/CastorRawCollections.h"
0011 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
0012 #include "Geometry/CaloTopology/interface/HcalTopology.h"
0013 #include "FWCore/ParameterSet/interface/FileInPath.h"
0014 using namespace std;
0015 
0016 CastorRawToDigi::CastorRawToDigi(edm::ParameterSet const& conf)
0017     : dataTag_(conf.getParameter<edm::InputTag>("InputLabel")),
0018       unpacker_(conf.getParameter<int>("CastorFirstFED"),
0019                 conf.getParameter<int>("firstSample"),
0020                 conf.getParameter<int>("lastSample")),
0021       zdcunpacker_(conf.getParameter<int>("CastorFirstFED"),
0022                    conf.getParameter<int>("firstSample"),
0023                    conf.getParameter<int>("lastSample")),
0024       ctdcunpacker_(conf.getParameter<int>("CastorFirstFED"),
0025                     conf.getParameter<int>("firstSample"),
0026                     conf.getParameter<int>("lastSample")),
0027       filter_(
0028           conf.getParameter<bool>("FilterDataQuality"), conf.getParameter<bool>("FilterDataQuality"), false, 0, 0, -1),
0029       fedUnpackList_(conf.getUntrackedParameter<std::vector<int> >("FEDs", std::vector<int>())),
0030       firstFED_(conf.getParameter<int>("CastorFirstFED")),
0031       complainEmptyData_(conf.getUntrackedParameter<bool>("ComplainEmptyData", false)),
0032       usingctdc_(conf.getParameter<bool>("CastorCtdc")),
0033       unpackTTP_(conf.getParameter<bool>("UnpackTTP")),
0034       unpackZDC_(conf.getParameter<bool>("UnpackZDC")),
0035       silent_(conf.getUntrackedParameter<bool>("silent", true)),
0036       usenominalOrbitMessageTime_(conf.getParameter<bool>("UseNominalOrbitMessageTime")),
0037       expectedOrbitMessageTime_(conf.getParameter<int>("ExpectedOrbitMessageTime"))
0038 
0039 {
0040   if (fedUnpackList_.empty()) {
0041     for (int i = FEDNumbering::MINCASTORFEDID; i <= FEDNumbering::MAXCASTORFEDID; i++)
0042       fedUnpackList_.push_back(i);
0043   }
0044 
0045   unpacker_.setExpectedOrbitMessageTime(expectedOrbitMessageTime_);
0046   std::ostringstream ss;
0047   for (unsigned int i = 0; i < fedUnpackList_.size(); i++)
0048     ss << fedUnpackList_[i] << " ";
0049   edm::LogInfo("CASTOR") << "CastorRawToDigi will unpack FEDs ( " << ss.str() << ")";
0050 
0051   // products produced...
0052   produces<CastorDigiCollection>();
0053   produces<ZDCDigiCollection>();
0054   produces<CastorTrigPrimDigiCollection>();
0055   produces<HcalUnpackerReport>();
0056   if (unpackTTP_)
0057     produces<HcalTTPDigiCollection>();
0058 
0059   tok_input_ = consumes<FEDRawDataCollection>(dataTag_);
0060   tok_pSetup_ = esConsumes<CastorDbService, CastorDbRecord>();
0061 }
0062 
0063 // Virtual destructor needed.
0064 CastorRawToDigi::~CastorRawToDigi() {}
0065 
0066 // Functions that gets called by framework every event
0067 void CastorRawToDigi::produce(edm::Event& e, const edm::EventSetup& es) {
0068   // Step A: Get Inputs
0069   edm::Handle<FEDRawDataCollection> rawraw;
0070   e.getByToken(tok_input_, rawraw);
0071   // get the mapping
0072   edm::ESHandle<CastorDbService> pSetup = es.getHandle(tok_pSetup_);
0073   const CastorElectronicsMap* readoutMap = pSetup->getCastorMapping();
0074 
0075   // Step B: Create empty output  : three vectors for three classes...
0076   std::vector<CastorDataFrame> castor;
0077   std::vector<ZDCDataFrame> zdc;
0078   std::vector<HcalTTPDigi> ttp;
0079   std::vector<CastorTriggerPrimitiveDigi> htp;
0080 
0081   auto report = std::make_unique<HcalUnpackerReport>();
0082 
0083   CastorRawCollections colls;
0084   colls.castorCont = &castor;
0085   colls.zdcCont = &zdc;
0086   if (unpackTTP_)
0087     colls.ttp = &ttp;
0088   colls.tpCont = &htp;
0089 
0090   // Step C: unpack all requested FEDs
0091   const FEDRawData& fed722 = rawraw->FEDData(722);
0092   const int fed722size = fed722.size();
0093   const FEDRawData& fed693 = rawraw->FEDData(693);
0094   const int fed693size = fed693.size();
0095   for (std::vector<int>::const_iterator i = fedUnpackList_.begin(); i != fedUnpackList_.end(); i++) {
0096     const FEDRawData& fed = rawraw->FEDData(*i);
0097     //std::cout<<"Fed number "<<*i<<"is being worked on"<<std::endl;
0098     if (*i == 693 && fed693size == 0 && fed722size != 0)
0099       continue;
0100     if (*i == 722 && fed722size == 0 && fed693size != 0)
0101       continue;
0102 
0103     if (*i != 693 && *i != 722) {
0104       if (fed.size() == 0) {
0105         if (complainEmptyData_) {
0106           edm::LogWarning("EmptyData") << "No data for FED " << *i;
0107           report->addError(*i);
0108         }
0109       } else if (fed.size() < 8 * 3) {
0110         edm::LogWarning("EmptyData") << "Tiny data " << fed.size() << " for FED " << *i;
0111         report->addError(*i);
0112       } else {
0113         try {
0114           if (usingctdc_) {
0115             ctdcunpacker_.unpack(fed, *readoutMap, colls, *report);
0116           } else {
0117             unpacker_.unpack(fed, *readoutMap, colls, *report);
0118           }
0119           report->addUnpacked(*i);
0120         } catch (cms::Exception& e) {
0121           edm::LogWarning("Unpacking error") << e.what();
0122           report->addError(*i);
0123         } catch (...) {
0124           edm::LogWarning("Unpacking exception");
0125           report->addError(*i);
0126         }
0127       }
0128     }
0129 
0130     if (*i == 693 && unpackZDC_) {
0131       if (fed.size() == 0) {
0132         if (complainEmptyData_) {
0133           edm::LogWarning("EmptyData") << "No data for FED " << *i;
0134           report->addError(*i);
0135         }
0136       }
0137       if (fed.size() != 0) {
0138         zdcunpacker_.unpack(fed, *readoutMap, colls, *report);
0139         report->addUnpacked(*i);
0140       }
0141     }
0142 
0143     if (*i == 722 && unpackZDC_) {
0144       if (fed.size() == 0) {
0145         if (complainEmptyData_) {
0146           edm::LogWarning("EmptyData") << "No data for FED " << *i;
0147           report->addError(*i);
0148         }
0149       }
0150       if (fed.size() != 0) {
0151         zdcunpacker_.unpack(fed, *readoutMap, colls, *report);
0152         report->addUnpacked(*i);
0153       }
0154     }
0155 
0156   }  //end of loop over feds
0157 
0158   // Step B: encapsulate vectors in actual collections
0159   auto castor_prod = std::make_unique<CastorDigiCollection>();
0160   auto htp_prod = std::make_unique<CastorTrigPrimDigiCollection>();
0161 
0162   castor_prod->swap_contents(castor);
0163   htp_prod->swap_contents(htp);
0164 
0165   // Step C2: filter FEDs, if required
0166   if (filter_.active()) {
0167     CastorDigiCollection filtered_castor = filter_.filter(*castor_prod, *report);
0168 
0169     castor_prod->swap(filtered_castor);
0170   }
0171 
0172   // Step D: Put outputs into event
0173   // just until the sorting is proven
0174   castor_prod->sort();
0175   htp_prod->sort();
0176 
0177   if (unpackZDC_) {
0178     auto zdc_prod = std::make_unique<ZDCDigiCollection>();
0179     zdc_prod->swap_contents(zdc);
0180 
0181     zdc_prod->sort();
0182     e.put(std::move(zdc_prod));
0183   }
0184 
0185   e.put(std::move(castor_prod));
0186   e.put(std::move(htp_prod));
0187 
0188   if (unpackTTP_) {
0189     auto prod = std::make_unique<HcalTTPDigiCollection>();
0190     prod->swap_contents(ttp);
0191 
0192     prod->sort();
0193     e.put(std::move(prod));
0194   }
0195   e.put(std::move(report));
0196 }
0197 void CastorRawToDigi::beginRun(edm::Run const& irun, edm::EventSetup const& es) {
0198   if (usenominalOrbitMessageTime_) {
0199     if (irun.run() > 132640) {
0200       expectedOrbitMessageTime_ = 3560;
0201     } else if (irun.run() > 132174) {
0202       expectedOrbitMessageTime_ = 3559;
0203     } else if (irun.run() > 124371) {
0204       expectedOrbitMessageTime_ = 3557;
0205     } else if (irun.run() > 123984) {
0206       expectedOrbitMessageTime_ = 3559;
0207     } else if (irun.run() > 123584) {
0208       expectedOrbitMessageTime_ = 2;
0209     } else {
0210       expectedOrbitMessageTime_ = 3562;
0211     }
0212     unpacker_.setExpectedOrbitMessageTime(expectedOrbitMessageTime_);
0213   }
0214 }