Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:22:38

0001 ///
0002 /// \class l1t::L1TStage1Layer2Producer
0003 ///
0004 /// Description: Emulator for the stage 1 jet algorithms.
0005 ///
0006 ///
0007 /// \author: R. Alex Barbieri MIT
0008 ///
0009 
0010 // system include files
0011 #include <memory>
0012 
0013 // user include files
0014 
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/Framework/interface/Frameworkfwd.h"
0017 #include "FWCore/Framework/interface/stream/EDProducer.h"
0018 #include "FWCore/Framework/interface/ESHandle.h"
0019 #include "FWCore/Framework/interface/Event.h"
0020 #include "FWCore/Framework/interface/MakerMacros.h"
0021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0022 #include "FWCore/Utilities/interface/EDGetToken.h"
0023 #include "FWCore/Utilities/interface/InputTag.h"
0024 #include "FWCore/Utilities/interface/ESGetToken.h"
0025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0026 
0027 #include "CondFormats/L1TObjects/interface/L1CaloEtScale.h"
0028 #include "CondFormats/DataRecord/interface/L1EmEtScaleRcd.h"
0029 #include "CondFormats/DataRecord/interface/L1JetEtScaleRcd.h"
0030 #include "CondFormats/DataRecord/interface/L1HtMissScaleRcd.h"
0031 #include "CondFormats/DataRecord/interface/L1HfRingEtScaleRcd.h"
0032 
0033 #include <vector>
0034 #include "DataFormats/L1Trigger/interface/BXVector.h"
0035 
0036 //#include "CondFormats/DataRecord/interface/CaloParamsRcd.h"
0037 //#include "CondFormats/L1TCalorimeter/interface/CaloParams.h"
0038 #include "CondFormats/L1TObjects/interface/CaloParams.h"
0039 #include "CondFormats/DataRecord/interface/L1TCaloParamsRcd.h"
0040 //#include "CondFormats/L1TObjects/interface/FirmwareVersion.h"
0041 #include "L1Trigger/L1TCalorimeter/interface/CaloParamsHelper.h"
0042 
0043 #include "L1Trigger/L1TCalorimeter/interface/CaloConfigHelper.h"
0044 #include "CondFormats/DataRecord/interface/L1TCaloConfigRcd.h"
0045 
0046 #include "DataFormats/L1TCalorimeter/interface/CaloRegion.h"
0047 #include "DataFormats/L1TCalorimeter/interface/CaloEmCand.h"
0048 
0049 #include "DataFormats/L1Trigger/interface/EGamma.h"
0050 #include "DataFormats/L1Trigger/interface/Tau.h"
0051 #include "DataFormats/L1Trigger/interface/Jet.h"
0052 #include "DataFormats/L1Trigger/interface/EtSum.h"
0053 #include "DataFormats/L1Trigger/interface/CaloSpare.h"
0054 
0055 #include "L1Trigger/L1TCalorimeter/interface/Stage1Layer2MainProcessor.h"
0056 #include "L1Trigger/L1TCalorimeter/interface/Stage1Layer2FirmwareFactory.h"
0057 
0058 // print statements
0059 //#include <stdio.h>
0060 
0061 using namespace std;
0062 using namespace edm;
0063 using namespace l1t;
0064 
0065 //
0066 // class declaration
0067 //
0068 
0069 class L1TStage1Layer2Producer : public stream::EDProducer<> {
0070 public:
0071   explicit L1TStage1Layer2Producer(const ParameterSet&);
0072   ~L1TStage1Layer2Producer() override;
0073 
0074   static void fillDescriptions(ConfigurationDescriptions& descriptions);
0075 
0076 private:
0077   void produce(Event&, EventSetup const&) override;
0078   void beginRun(Run const& iR, EventSetup const& iE) override;
0079 
0080   // ----------member data ---------------------------
0081   unsigned long long m_paramsCacheId;  // Cache-ID from current parameters, to check if needs to be updated.
0082   unsigned long long m_configCacheId;  // Cache-ID from current parameters, to check if needs to be updated.
0083   CaloParamsHelper* m_params;
0084   CaloConfigHelper m_config;
0085 
0086   std::shared_ptr<Stage1Layer2MainProcessor> m_fw;  // Firmware to run per event, depends on database parameters.
0087 
0088   Stage1Layer2FirmwareFactory m_factory;  // Factory to produce algorithms based on DB parameters
0089 
0090   std::string m_conditionsLabel;
0091 
0092   // to be extended with other "consumes" stuff
0093   EDGetTokenT<BXVector<CaloRegion>> regionToken;
0094   EDGetTokenT<BXVector<CaloEmCand>> candsToken;
0095   edm::ESGetToken<CaloParams, L1TCaloParamsRcd> paramsToken;
0096   edm::ESGetToken<CaloConfig, L1TCaloConfigRcd> configToken;
0097   edm::ESGetToken<L1CaloEtScale, L1EmEtScaleRcd> emScaleToken;
0098   edm::ESGetToken<L1CaloEtScale, L1JetEtScaleRcd> jetScaleToken;
0099   edm::ESGetToken<L1CaloEtScale, L1HtMissScaleRcd> htMissScaleToken;
0100   edm::ESGetToken<L1CaloEtScale, L1HfRingEtScaleRcd> hfRingScaleToken;
0101 };
0102 
0103 //
0104 // constructors and destructor
0105 //
0106 L1TStage1Layer2Producer::L1TStage1Layer2Producer(const ParameterSet& iConfig) {
0107   // register what you produce
0108   produces<BXVector<EGamma>>();
0109   produces<BXVector<Tau>>("rlxTaus");
0110   produces<BXVector<Tau>>("isoTaus");
0111   produces<BXVector<Jet>>();
0112   produces<BXVector<Jet>>("preGtJets");
0113   produces<BXVector<EtSum>>();
0114   produces<BXVector<CaloSpare>>("HFRingSums");
0115   produces<BXVector<CaloSpare>>("HFBitCounts");
0116 
0117   // register what you consume and keep token for later access:
0118   regionToken = consumes<BXVector<CaloRegion>>(iConfig.getParameter<InputTag>("CaloRegions"));
0119   candsToken = consumes<BXVector<CaloEmCand>>(iConfig.getParameter<InputTag>("CaloEmCands"));
0120   //int ifwv=iConfig.getParameter<unsigned>("FirmwareVersion");  // LenA  make configurable for now
0121 
0122   m_conditionsLabel = iConfig.getParameter<std::string>("conditionsLabel");
0123 
0124   m_params = new CaloParamsHelper;
0125 
0126   paramsToken =
0127       esConsumes<CaloParams, L1TCaloParamsRcd, edm::Transition::BeginRun>(edm::ESInputTag("", m_conditionsLabel));
0128   configToken =
0129       esConsumes<CaloConfig, L1TCaloConfigRcd, edm::Transition::BeginRun>(edm::ESInputTag("", m_conditionsLabel));
0130   emScaleToken =
0131       esConsumes<L1CaloEtScale, L1EmEtScaleRcd, edm::Transition::BeginRun>(edm::ESInputTag("", m_conditionsLabel));
0132   jetScaleToken =
0133       esConsumes<L1CaloEtScale, L1JetEtScaleRcd, edm::Transition::BeginRun>(edm::ESInputTag("", m_conditionsLabel));
0134   htMissScaleToken =
0135       esConsumes<L1CaloEtScale, L1HtMissScaleRcd, edm::Transition::BeginRun>(edm::ESInputTag("", m_conditionsLabel));
0136   hfRingScaleToken =
0137       esConsumes<L1CaloEtScale, L1HfRingEtScaleRcd, edm::Transition::BeginRun>(edm::ESInputTag("", m_conditionsLabel));
0138 
0139   // set cache id to zero, will be set at first beginRun:
0140   m_paramsCacheId = 0;
0141   m_configCacheId = 0;
0142 }
0143 
0144 L1TStage1Layer2Producer::~L1TStage1Layer2Producer() {}
0145 
0146 //
0147 // member functions
0148 //
0149 
0150 // ------------ method called to produce the data ------------
0151 void L1TStage1Layer2Producer::produce(Event& iEvent, const EventSetup& iSetup) {
0152   LogDebug("l1t|stage 1 jets") << "L1TStage1Layer2Producer::produce function called...\n";
0153 
0154   //return;
0155 
0156   //inputs
0157   Handle<BXVector<CaloRegion>> caloRegions;
0158   iEvent.getByToken(regionToken, caloRegions);
0159 
0160   Handle<BXVector<CaloEmCand>> caloEmCands;
0161   iEvent.getByToken(candsToken, caloEmCands);
0162 
0163   int bxFirst = caloRegions->getFirstBX();
0164   int bxLast = caloRegions->getLastBX();
0165 
0166   //outputs
0167   std::unique_ptr<EGammaBxCollection> egammas(new EGammaBxCollection);
0168   std::unique_ptr<TauBxCollection> taus(new TauBxCollection);
0169   std::unique_ptr<TauBxCollection> isoTaus(new TauBxCollection);
0170   std::unique_ptr<JetBxCollection> jets(new JetBxCollection);
0171   std::unique_ptr<JetBxCollection> preGtJets(new JetBxCollection);
0172   std::unique_ptr<EtSumBxCollection> etsums(new EtSumBxCollection);
0173   std::unique_ptr<CaloSpareBxCollection> hfSums(new CaloSpareBxCollection);
0174   std::unique_ptr<CaloSpareBxCollection> hfCounts(new CaloSpareBxCollection);
0175 
0176   egammas->setBXRange(bxFirst, bxLast);
0177   taus->setBXRange(bxFirst, bxLast);
0178   isoTaus->setBXRange(bxFirst, bxLast);
0179   jets->setBXRange(bxFirst, bxLast);
0180   preGtJets->setBXRange(bxFirst, bxLast);
0181   etsums->setBXRange(bxFirst, bxLast);
0182   hfSums->setBXRange(bxFirst, bxLast);
0183   hfCounts->setBXRange(bxFirst, bxLast);
0184 
0185   //producer is responsible for splitting the BXVector into pieces for
0186   //the firmware to handle
0187   for (int i = bxFirst; i <= bxLast; ++i) {
0188     //make local inputs
0189     std::vector<CaloRegion>* localRegions = new std::vector<CaloRegion>();
0190     std::vector<CaloEmCand>* localEmCands = new std::vector<CaloEmCand>();
0191 
0192     //make local outputs
0193     std::vector<EGamma>* localEGammas = new std::vector<EGamma>();
0194     std::vector<Tau>* localTaus = new std::vector<Tau>();
0195     std::vector<Tau>* localIsoTaus = new std::vector<Tau>();
0196     std::vector<Jet>* localJets = new std::vector<Jet>();
0197     std::vector<Jet>* localPreGtJets = new std::vector<Jet>();
0198     std::vector<EtSum>* localEtSums = new std::vector<EtSum>();
0199     CaloSpare* localHfSums = new CaloSpare();
0200     localHfSums->setType(CaloSpare::HFRingSum);
0201     CaloSpare* localHfCounts = new CaloSpare();
0202     localHfCounts->setType(CaloSpare::HFBitCount);
0203 
0204     // copy over the inputs -> there must be a better way to do this
0205     for (std::vector<CaloRegion>::const_iterator region = caloRegions->begin(i); region != caloRegions->end(i);
0206          ++region)
0207       localRegions->push_back(*region);
0208     for (std::vector<CaloEmCand>::const_iterator emcand = caloEmCands->begin(i); emcand != caloEmCands->end(i);
0209          ++emcand)
0210       localEmCands->push_back(*emcand);
0211 
0212     //run the firmware on one event
0213     m_fw->processEvent(*localEmCands,
0214                        *localRegions,
0215                        localEGammas,
0216                        localTaus,
0217                        localIsoTaus,
0218                        localJets,
0219                        localPreGtJets,
0220                        localEtSums,
0221                        localHfSums,
0222                        localHfCounts);
0223 
0224     // copy the output into the BXVector -> there must be a better way
0225     for (std::vector<EGamma>::const_iterator eg = localEGammas->begin(); eg != localEGammas->end(); ++eg)
0226       egammas->push_back(i, *eg);
0227     for (std::vector<Tau>::const_iterator tau = localTaus->begin(); tau != localTaus->end(); ++tau)
0228       taus->push_back(i, *tau);
0229     for (std::vector<Tau>::const_iterator isotau = localIsoTaus->begin(); isotau != localIsoTaus->end(); ++isotau)
0230       isoTaus->push_back(i, *isotau);
0231     for (std::vector<Jet>::const_iterator jet = localJets->begin(); jet != localJets->end(); ++jet)
0232       jets->push_back(i, *jet);
0233     for (std::vector<Jet>::const_iterator jet = localPreGtJets->begin(); jet != localPreGtJets->end(); ++jet)
0234       preGtJets->push_back(i, *jet);
0235     for (std::vector<EtSum>::const_iterator etsum = localEtSums->begin(); etsum != localEtSums->end(); ++etsum)
0236       etsums->push_back(i, *etsum);
0237     hfSums->push_back(i, *localHfSums);
0238     hfCounts->push_back(i, *localHfCounts);
0239 
0240     delete localRegions;
0241     delete localEmCands;
0242     delete localEGammas;
0243     delete localTaus;
0244     delete localIsoTaus;
0245     delete localJets;
0246     delete localPreGtJets;
0247     delete localEtSums;
0248     delete localHfSums;
0249     delete localHfCounts;
0250   }
0251 
0252   iEvent.put(std::move(egammas));
0253   iEvent.put(std::move(taus), "rlxTaus");
0254   iEvent.put(std::move(isoTaus), "isoTaus");
0255   iEvent.put(std::move(jets));
0256   iEvent.put(std::move(preGtJets), "preGtJets");
0257   iEvent.put(std::move(etsums));
0258   iEvent.put(std::move(hfSums), "HFRingSums");
0259   iEvent.put(std::move(hfCounts), "HFBitCounts");
0260 }
0261 
0262 // ------------ method called when starting to processes a run ------------
0263 
0264 void L1TStage1Layer2Producer::beginRun(Run const& iR, EventSetup const& iE) {
0265   unsigned long long id = 0;
0266 
0267   id = iE.get<L1TCaloParamsRcd>().cacheIdentifier();
0268   if (id != m_paramsCacheId) {
0269     m_paramsCacheId = id;
0270 
0271     edm::ESHandle<CaloParams> paramsHandle = iE.getHandle(paramsToken);
0272 
0273     // replace our local copy of the parameters with a new one using placement new
0274     m_params->~CaloParamsHelper();
0275     m_params = new (m_params) CaloParamsHelper(*paramsHandle.product());
0276 
0277     LogDebug("L1TDebug") << *m_params << std::endl;
0278 
0279     if (!m_params) {
0280       edm::LogError("l1t|caloStage1") << "Could not retrieve params from Event Setup" << std::endl;
0281     }
0282   }
0283 
0284   id = iE.get<L1TCaloConfigRcd>().cacheIdentifier();
0285   if (id != m_configCacheId) {
0286     m_configCacheId = id;
0287 
0288     edm::ESHandle<CaloConfig> configHandle = iE.getHandle(configToken);
0289 
0290     if (!configHandle.product()) {
0291       edm::LogError("l1t|caloStage1") << "Could not retrieve config from Event Setup" << std::endl;
0292     } else {
0293       //update our DB payload in CaloConfigHelper:
0294       m_config.UpdatePayload(configHandle.product());
0295       //cout << "DEBUG:  L1T Calo Config reports the Layer2 firmware version is " << m_config.fwv() << "\n";
0296     }
0297   }
0298 
0299   LogDebug("l1t|stage 1 jets") << "L1TStage1Layer2Producer::beginRun function called...\n";
0300 
0301   //get the proper scales for conversion to physical et AND gt scales
0302   edm::ESHandle<L1CaloEtScale> emScale = iE.getHandle(emScaleToken);
0303   m_params->setEmScale(*emScale);
0304 
0305   edm::ESHandle<L1CaloEtScale> jetScale = iE.getHandle(jetScaleToken);
0306   m_params->setJetScale(*jetScale);
0307 
0308   edm::ESHandle<L1CaloEtScale> HtMissScale = iE.getHandle(jetScaleToken);
0309   m_params->setHtMissScale(*HtMissScale);
0310 
0311   //not sure if I need this one
0312   edm::ESHandle<L1CaloEtScale> HfRingScale = iE.getHandle(hfRingScaleToken);
0313   m_params->setHfRingScale(*HfRingScale);
0314 
0315   //unsigned long long id = iE.get<CaloParamsRcd>().cacheIdentifier();
0316 
0317   //if (id != m_paramsCacheId)
0318   {  // Need to update:
0319     //m_paramsCacheId = id;
0320 
0321     //ESHandle<CaloParams> parameters;
0322     //iE.get<CaloParamsRcd>().get(parameters);
0323 
0324     // LenA move the setting of the firmware version to the L1TStage1Layer2Producer constructor
0325 
0326     //m_params = std::shared_ptr<const CaloParams>(parameters.product());
0327     //m_fwv = std::shared_ptr<const FirmwareVersion>(new FirmwareVersion());
0328     //printf("Begin.\n");
0329     //m_fwv = std::make_shared<FirmwareVersion>(); //not const during testing
0330     //printf("Success m_fwv.\n");
0331     //m_fwv->setFirmwareVersion(1); //hardcode for now, 1=HI, 2=PP
0332     //printf("Success m_fwv version set.\n");
0333 
0334     // if (! m_params){
0335     //   LogError("l1t|stage 1 jets") << "L1TStage1Layer2Producer: could not retreive DB params from Event Setup\n";
0336     // }
0337 
0338     // Set the current algorithm version based on DB pars from database:
0339     //m_fw = m_factory.create(*m_fwv /*,*m_params*/);
0340     //printf("Success create.\n");
0341 
0342     //if (! m_fw) {
0343     //  // we complain here once per run
0344     //  LogError("l1t|stage 1 jets") << "L1TStage1Layer2Producer: firmware could not be configured.\n";
0345     //}
0346   }
0347 
0348   int ifwv = m_config.fwv();
0349   //cout << "DEBUG:  ifwv is " << ifwv << "\n";
0350   //m_fwv = std::make_shared<FirmwareVersion>(); //not const during testing
0351   if (ifwv == 1) {
0352     LogDebug("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Running HI implementation\n";
0353     //std::cout << "L1TStage1Layer2Producer -- Running HI implementation\n";
0354   } else if (ifwv == 2) {
0355     LogDebug("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Running pp implementation\n";
0356     //std::cout << "L1TStage1Layer2Producer -- Running pp implementation\n";
0357   } else if (ifwv == 3) {
0358     LogDebug("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Running SimpleHW implementation\n";
0359     //std::cout << "L1TStage1Layer2Producer -- Running SimpleHW implementation -- for testing only\n";
0360   } else {
0361     LogError("l1t|stage1firmware") << "L1TStage1Layer2Producer -- Unknown implementation.\n";
0362     //std::cout << "L1TStage1Layer2Producer -- Unknown implementation.\n";
0363   }
0364   //m_fwv->setFirmwareVersion(ifwv); // =1 HI, =2 PP
0365   // m_fw = m_factory.create(*m_fwv /*,*m_params*/);
0366   //m_fwv = ifwv;
0367   m_fw = m_factory.create(ifwv, m_params);
0368   //printf("Success create.\n");
0369   if (!m_fw) {
0370     // we complain here once per job
0371     LogError("l1t|stage1firmware") << "L1TStage1Layer2Producer: firmware could not be configured.\n";
0372   }
0373 }
0374 
0375 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
0376 void L1TStage1Layer2Producer::fillDescriptions(ConfigurationDescriptions& descriptions) {
0377   //The following says we do not know what parameters are allowed so do no validation
0378   // Please change this to state exactly what you do use, even if it is no parameters
0379   ParameterSetDescription desc;
0380   desc.add<InputTag>("CaloRegions");
0381   desc.add<InputTag>("CaloEmCands");
0382   desc.add<std::string>("conditionsLabel");
0383 
0384   descriptions.addDefault(desc);
0385 }
0386 
0387 //define this as a plug-in
0388 DEFINE_FWK_MODULE(L1TStage1Layer2Producer);