Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-22 22:47:32

0001 #include "CalibFormats/CaloTPG/interface/CaloTPGRecord.h"
0002 #include "CalibFormats/CaloTPG/interface/CaloTPGTranscoder.h"
0003 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
0004 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
0005 #include "CalibFormats/HcalObjects/interface/HcalTPGCoder.h"
0006 #include "CalibFormats/HcalObjects/interface/HcalTPGRecord.h"
0007 #include "CalibFormats/CaloTPG/interface/HcalTPGCompressor.h"
0008 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
0009 #include "CondFormats/HcalObjects/interface/HcalLutMetadata.h"
0010 #include "CondFormats/HcalObjects/interface/HcalTPChannelParameters.h"
0011 #include "CondFormats/DataRecord/interface/HcalLutMetadataRcd.h"
0012 #include "DataFormats/Common/interface/Handle.h"
0013 #include "DataFormats/HcalDigi/interface/HBHEDataFrame.h"
0014 #include "DataFormats/HcalDigi/interface/HFDataFrame.h"
0015 #include "DataFormats/HcalDigi/interface/HcalTriggerPrimitiveDigi.h"
0016 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
0017 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0018 #include "FWCore/Framework/interface/stream/EDProducer.h"
0019 #include "FWCore/Utilities/interface/ESGetToken.h"
0020 #include "FWCore/Framework/interface/Event.h"
0021 #include "FWCore/Framework/interface/EventSetup.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h"
0025 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0026 #include "Geometry/HcalTowerAlgo/interface/HcalGeometry.h"
0027 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0028 #include "SimCalorimetry/HcalTrigPrimAlgos/interface/HcalTriggerPrimitiveAlgo.h"
0029 
0030 #include <algorithm>
0031 #include <vector>
0032 
0033 class HcalTrigPrimDigiProducer : public edm::stream::EDProducer<> {
0034 public:
0035   explicit HcalTrigPrimDigiProducer(const edm::ParameterSet& ps);
0036   ~HcalTrigPrimDigiProducer() override {}
0037 
0038   /**Produces the EDM products,*/
0039   void beginRun(const edm::Run& r, const edm::EventSetup& c) override;
0040   void produce(edm::Event& e, const edm::EventSetup& c) override;
0041 
0042 private:
0043   HcalTriggerPrimitiveAlgo theAlgo_;
0044 
0045   /// input tags for HCAL digis
0046   std::vector<edm::InputTag> inputLabel_;
0047   std::vector<edm::InputTag> inputUpgradeLabel_;
0048   // this seems a strange way of doing things
0049   edm::EDGetTokenT<QIE11DigiCollection> tok_hbhe_up_;
0050   edm::EDGetTokenT<QIE10DigiCollection> tok_hf_up_;
0051 
0052   edm::EDGetTokenT<HBHEDigiCollection> tok_hbhe_;
0053   edm::EDGetTokenT<HFDigiCollection> tok_hf_;
0054 
0055   bool overrideDBweightsAndFilterHE_;
0056   bool overrideDBweightsAndFilterHB_;
0057 
0058   /// input tag for FEDRawDataCollection
0059   edm::InputTag inputTagFEDRaw_;
0060   edm::EDGetTokenT<FEDRawDataCollection> tok_raw_;
0061   double MinLongEnergy_, MinShortEnergy_, LongShortSlope_, LongShortOffset_;
0062 
0063   bool runZS_;
0064 
0065   bool runFrontEndFormatError_;
0066 
0067   bool upgrade_;
0068   bool legacy_;
0069 
0070   bool HFEMB_;
0071   edm::ParameterSet LongShortCut_;
0072   edm::ESGetToken<HcalTPGCoder, HcalTPGRecord> tok_tpgCoder_;
0073   edm::ESGetToken<CaloTPGTranscoder, CaloTPGRecord> tok_tpgTranscoder_;
0074   edm::ESGetToken<HcalLutMetadata, HcalLutMetadataRcd> tok_lutMetadata_;
0075   edm::ESGetToken<HcalTrigTowerGeometry, CaloGeometryRecord> tok_trigTowerGeom_;
0076   edm::ESGetToken<HcalTopology, HcalRecNumberingRecord> tok_hcalTopo_;
0077   edm::ESGetToken<HcalDbService, HcalDbRecord> tok_dbService_;
0078   edm::ESGetToken<HcalDbService, HcalDbRecord> tok_dbService_beginRun_;
0079 };
0080 
0081 HcalTrigPrimDigiProducer::HcalTrigPrimDigiProducer(const edm::ParameterSet& ps)
0082     : theAlgo_(ps.getParameter<bool>("peakFilter"),
0083                ps.getParameter<std::vector<double> >("weights"),
0084                ps.getParameter<int>("latency"),
0085                ps.getParameter<uint32_t>("FG_threshold"),
0086                ps.getParameter<std::vector<uint32_t> >("FG_HF_thresholds"),
0087                ps.getParameter<uint32_t>("ZS_threshold"),
0088                ps.getParameter<int>("numberOfSamples"),
0089                ps.getParameter<int>("numberOfPresamples"),
0090                ps.getParameter<int>("numberOfFilterPresamplesHBQIE11"),
0091                ps.getParameter<int>("numberOfFilterPresamplesHEQIE11"),
0092                ps.getParameter<int>("numberOfSamplesHF"),
0093                ps.getParameter<int>("numberOfPresamplesHF"),
0094                ps.getParameter<bool>("useTDCInMinBiasBits"),
0095                ps.getParameter<uint32_t>("MinSignalThreshold"),
0096                ps.getParameter<uint32_t>("PMTNoiseThreshold")),
0097       inputLabel_(ps.getParameter<std::vector<edm::InputTag> >("inputLabel")),
0098       inputUpgradeLabel_(ps.getParameter<std::vector<edm::InputTag> >("inputUpgradeLabel")),
0099       inputTagFEDRaw_(ps.getParameter<edm::InputTag>("InputTagFEDRaw")),
0100       runZS_(ps.getParameter<bool>("RunZS")),
0101       runFrontEndFormatError_(ps.getParameter<bool>("FrontEndFormatError")) {
0102   std::vector<bool> upgrades = {
0103       ps.getParameter<bool>("upgradeHB"), ps.getParameter<bool>("upgradeHE"), ps.getParameter<bool>("upgradeHF")};
0104   upgrade_ = std::any_of(std::begin(upgrades), std::end(upgrades), [](bool a) { return a; });
0105   legacy_ = std::any_of(std::begin(upgrades), std::end(upgrades), [](bool a) { return !a; });
0106 
0107   overrideDBweightsAndFilterHE_ = ps.getParameter<bool>("overrideDBweightsAndFilterHE");
0108   overrideDBweightsAndFilterHB_ = ps.getParameter<bool>("overrideDBweightsAndFilterHB");
0109 
0110   theAlgo_.setWeightsQIE11(ps.getParameter<edm::ParameterSet>("weightsQIE11"));
0111 
0112   if (ps.exists("parameters")) {
0113     auto pset = ps.getUntrackedParameter<edm::ParameterSet>("parameters");
0114     theAlgo_.overrideParameters(pset);
0115   }
0116   theAlgo_.setUpgradeFlags(upgrades[0], upgrades[1], upgrades[2]);
0117   theAlgo_.setFixSaturationFlag(ps.getParameter<bool>("applySaturationFix"));
0118 
0119   HFEMB_ = false;
0120   if (ps.exists("LSConfig")) {
0121     LongShortCut_ = ps.getUntrackedParameter<edm::ParameterSet>("LSConfig");
0122     HFEMB_ = LongShortCut_.getParameter<bool>("HcalFeatureHFEMBit");
0123     MinLongEnergy_ = LongShortCut_.getParameter<double>("Min_Long_Energy");    //minimum long energy
0124     MinShortEnergy_ = LongShortCut_.getParameter<double>("Min_Short_Energy");  //minimum short energy
0125     LongShortSlope_ =
0126         LongShortCut_.getParameter<double>("Long_vrs_Short_Slope");  //slope of the line that cuts are based on
0127     LongShortOffset_ = LongShortCut_.getParameter<double>("Long_Short_Offset");  //offset of line
0128   }
0129   tok_tpgCoder_ = esConsumes<HcalTPGCoder, HcalTPGRecord>();
0130   tok_tpgTranscoder_ = esConsumes<CaloTPGTranscoder, CaloTPGRecord>();
0131   tok_lutMetadata_ = esConsumes<HcalLutMetadata, HcalLutMetadataRcd>();
0132   tok_trigTowerGeom_ = esConsumes<HcalTrigTowerGeometry, CaloGeometryRecord>();
0133   tok_hcalTopo_ = esConsumes<HcalTopology, HcalRecNumberingRecord, edm::Transition::BeginRun>();
0134 
0135   // register for data access
0136   if (runFrontEndFormatError_) {
0137     tok_raw_ = consumes<FEDRawDataCollection>(inputTagFEDRaw_);
0138   }
0139 
0140   if (legacy_) {
0141     tok_hbhe_ = consumes<HBHEDigiCollection>(inputLabel_[0]);
0142     tok_hf_ = consumes<HFDigiCollection>(inputLabel_[1]);
0143   }
0144 
0145   if (upgrade_) {
0146     tok_hbhe_up_ = consumes<QIE11DigiCollection>(inputUpgradeLabel_[0]);
0147     tok_hf_up_ = consumes<QIE10DigiCollection>(inputUpgradeLabel_[1]);
0148   }
0149   tok_dbService_ = esConsumes<HcalDbService, HcalDbRecord>();
0150   tok_dbService_beginRun_ = esConsumes<HcalDbService, HcalDbRecord, edm::Transition::BeginRun>();
0151   produces<HcalTrigPrimDigiCollection>();
0152   theAlgo_.setPeakFinderAlgorithm(ps.getParameter<int>("PeakFinderAlgorithm"));
0153 
0154   edm::ParameterSet hfSS = ps.getParameter<edm::ParameterSet>("tpScales").getParameter<edm::ParameterSet>("HF");
0155 
0156   theAlgo_.setNCTScaleShift(hfSS.getParameter<int>("NCTShift"));
0157   theAlgo_.setRCTScaleShift(hfSS.getParameter<int>("RCTShift"));
0158 }
0159 
0160 void HcalTrigPrimDigiProducer::beginRun(const edm::Run& run, const edm::EventSetup& eventSetup) {
0161   edm::ESHandle<HcalDbService> db = eventSetup.getHandle(tok_dbService_beginRun_);
0162   const HcalTopology* topo = &eventSetup.getData(tok_hcalTopo_);
0163 
0164   const HcalElectronicsMap* emap = db->getHcalMapping();
0165 
0166   int lastHERing = topo->lastHERing();
0167   int lastHBRing = topo->lastHBRing();
0168 
0169   std::vector<HcalElectronicsId> vIds = emap->allElectronicsIdTrigger();
0170   for (std::vector<HcalElectronicsId>::const_iterator eId = vIds.begin(); eId != vIds.end(); eId++) {
0171     HcalTrigTowerDetId hcalTTDetId(emap->lookupTrigger(*eId));
0172     if (hcalTTDetId.null())
0173       continue;
0174 
0175     int aieta = abs(hcalTTDetId.ieta());
0176 
0177     // Filter weight represented in fixed point 8 bit
0178     int fixedPointWeight = -1;
0179 
0180     // The absence of TT channels in the HcalTPChannelParameters
0181     // is intepreted as to not use the new filter
0182     auto tpParam = db->getHcalTPChannelParameter(hcalTTDetId, false);
0183     if (tpParam)
0184       fixedPointWeight = tpParam->getauxi1();
0185 
0186     // Do not let ieta 29 in the map
0187     // If the aieta already has a weight in the map, then move on
0188     if (aieta <= lastHBRing) {
0189       // Fix number of filter presamples to one if we are using DB weights
0190       // Size of filter is already known when using DB weights
0191       // Weight from DB represented as 8-bit integer
0192       if (!overrideDBweightsAndFilterHB_) {
0193         if (fixedPointWeight != -1) {
0194           theAlgo_.setNumFilterPresamplesHBQIE11(1);
0195           theAlgo_.setWeightQIE11(aieta, fixedPointWeight);
0196         } else {
0197           theAlgo_.setNumFilterPresamplesHBQIE11(0);
0198           theAlgo_.setWeightQIE11(aieta, 255);
0199         }
0200       }
0201     } else if (aieta < lastHERing) {
0202       if (!overrideDBweightsAndFilterHE_) {
0203         if (fixedPointWeight != -1) {
0204           theAlgo_.setNumFilterPresamplesHEQIE11(1);
0205           theAlgo_.setWeightQIE11(aieta, fixedPointWeight);
0206         } else {
0207           theAlgo_.setNumFilterPresamplesHEQIE11(0);
0208           theAlgo_.setWeightQIE11(aieta, 255);
0209         }
0210       }
0211     }
0212   }
0213 }
0214 
0215 void HcalTrigPrimDigiProducer::produce(edm::Event& iEvent, const edm::EventSetup& eventSetup) {
0216   // Step A: get the conditions, for the decoding
0217   edm::ESHandle<HcalTPGCoder> inputCoder = eventSetup.getHandle(tok_tpgCoder_);
0218 
0219   edm::ESHandle<CaloTPGTranscoder> outTranscoder = eventSetup.getHandle(tok_tpgTranscoder_);
0220 
0221   edm::ESHandle<HcalLutMetadata> lutMetadata = eventSetup.getHandle(tok_lutMetadata_);
0222   float rctlsb = lutMetadata->getRctLsb();
0223 
0224   edm::ESHandle<HcalTrigTowerGeometry> pG = eventSetup.getHandle(tok_trigTowerGeom_);
0225 
0226   // Step B: Create empty output
0227   std::unique_ptr<HcalTrigPrimDigiCollection> result(new HcalTrigPrimDigiCollection());
0228 
0229   edm::Handle<HBHEDigiCollection> hbheDigis;
0230   edm::Handle<HFDigiCollection> hfDigis;
0231 
0232   edm::Handle<QIE11DigiCollection> hbheUpDigis;
0233   edm::Handle<QIE10DigiCollection> hfUpDigis;
0234 
0235   if (legacy_) {
0236     iEvent.getByToken(tok_hbhe_, hbheDigis);
0237     iEvent.getByToken(tok_hf_, hfDigis);
0238 
0239     // protect here against missing input collections
0240     // there is no protection in HcalTriggerPrimitiveAlgo
0241 
0242     if (!hbheDigis.isValid() and legacy_) {
0243       edm::LogInfo("HcalTrigPrimDigiProducer") << "\nWarning: HBHEDigiCollection with input tag " << inputLabel_[0]
0244                                                << "\nrequested in configuration, but not found in the event."
0245                                                << "\nQuit returning empty product." << std::endl;
0246 
0247       // put empty HcalTrigPrimDigiCollection in the event
0248       iEvent.put(std::move(result));
0249 
0250       return;
0251     }
0252 
0253     if (!hfDigis.isValid() and legacy_) {
0254       edm::LogInfo("HcalTrigPrimDigiProducer") << "\nWarning: HFDigiCollection with input tag " << inputLabel_[1]
0255                                                << "\nrequested in configuration, but not found in the event."
0256                                                << "\nQuit returning empty product." << std::endl;
0257 
0258       // put empty HcalTrigPrimDigiCollection in the event
0259       iEvent.put(std::move(result));
0260 
0261       return;
0262     }
0263   }
0264 
0265   if (upgrade_) {
0266     iEvent.getByToken(tok_hbhe_up_, hbheUpDigis);
0267     iEvent.getByToken(tok_hf_up_, hfUpDigis);
0268 
0269     if (!hbheUpDigis.isValid() and upgrade_) {
0270       edm::LogInfo("HcalTrigPrimDigiProducer")
0271           << "\nWarning: Upgrade HBHEDigiCollection with input tag " << inputUpgradeLabel_[0]
0272           << "\nrequested in configuration, but not found in the event."
0273           << "\nQuit returning empty product." << std::endl;
0274 
0275       // put empty HcalTrigPrimDigiCollection in the event
0276       iEvent.put(std::move(result));
0277 
0278       return;
0279     }
0280 
0281     if (!hfUpDigis.isValid() and upgrade_) {
0282       edm::LogInfo("HcalTrigPrimDigiProducer") << "\nWarning: HFDigiCollection with input tag " << inputUpgradeLabel_[1]
0283                                                << "\nrequested in configuration, but not found in the event."
0284                                                << "\nQuit returning empty product." << std::endl;
0285 
0286       // put empty HcalTrigPrimDigiCollection in the event
0287       iEvent.put(std::move(result));
0288 
0289       return;
0290     }
0291   }
0292 
0293   edm::ESHandle<HcalDbService> pSetup = eventSetup.getHandle(tok_dbService_);
0294 
0295   HcalFeatureBit* hfembit = nullptr;
0296 
0297   if (HFEMB_) {
0298     hfembit = new HcalFeatureHFEMBit(MinShortEnergy_,
0299                                      MinLongEnergy_,
0300                                      LongShortSlope_,
0301                                      LongShortOffset_,
0302                                      *pSetup);  //inputs values that cut will be based on
0303   }
0304 
0305   // Step C: Invoke the algorithm, passing in inputs and getting back outputs.
0306   if (legacy_ and not upgrade_) {
0307     theAlgo_.run(inputCoder.product(),
0308                  outTranscoder->getHcalCompressor().get(),
0309                  pSetup.product(),
0310                  *result,
0311                  &(*pG),
0312                  rctlsb,
0313                  hfembit,
0314                  *hbheDigis,
0315                  *hfDigis);
0316   } else if (legacy_ and upgrade_) {
0317     theAlgo_.run(inputCoder.product(),
0318                  outTranscoder->getHcalCompressor().get(),
0319                  pSetup.product(),
0320                  *result,
0321                  &(*pG),
0322                  rctlsb,
0323                  hfembit,
0324                  *hbheDigis,
0325                  *hfDigis,
0326                  *hbheUpDigis,
0327                  *hfUpDigis);
0328   } else {
0329     theAlgo_.run(inputCoder.product(),
0330                  outTranscoder->getHcalCompressor().get(),
0331                  pSetup.product(),
0332                  *result,
0333                  &(*pG),
0334                  rctlsb,
0335                  hfembit,
0336                  *hbheUpDigis,
0337                  *hfUpDigis);
0338   }
0339 
0340   // Step C.1: Run FE Format Error / ZS for real data.
0341   if (runFrontEndFormatError_) {
0342     const HcalElectronicsMap* emap = pSetup->getHcalMapping();
0343 
0344     edm::Handle<FEDRawDataCollection> fedHandle;
0345     iEvent.getByToken(tok_raw_, fedHandle);
0346 
0347     if (fedHandle.isValid() && emap != nullptr) {
0348       theAlgo_.runFEFormatError(fedHandle.product(), emap, *result);
0349     } else {
0350       edm::LogInfo("HcalTrigPrimDigiProducer") << "\nWarning: FEDRawDataCollection with input tag " << inputTagFEDRaw_
0351                                                << "\nrequested in configuration, but not found in the event."
0352                                                << "\nQuit returning empty product." << std::endl;
0353 
0354       // produce empty HcalTrigPrimDigiCollection and put it in the event
0355       std::unique_ptr<HcalTrigPrimDigiCollection> emptyResult(new HcalTrigPrimDigiCollection());
0356 
0357       iEvent.put(std::move(emptyResult));
0358 
0359       return;
0360     }
0361   }
0362 
0363   if (runZS_)
0364     theAlgo_.runZS(*result);
0365 
0366   //  edm::LogInfo("HcalTrigPrimDigiProducer") << "HcalTrigPrims: " << result->size();
0367 
0368   // Step D: Put outputs into event
0369   iEvent.put(std::move(result));
0370 }
0371 
0372 #include "FWCore/PluginManager/interface/ModuleDef.h"
0373 #include "FWCore/Framework/interface/MakerMacros.h"
0374 
0375 DEFINE_FWK_MODULE(HcalTrigPrimDigiProducer);