Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:46

0001 // system include files
0002 #include <atomic>
0003 #include <memory>
0004 #include <cmath>
0005 #include <iostream>
0006 #include <sstream>
0007 #include <fstream>
0008 
0009 // user include files
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/Framework/interface/stream/EDFilter.h"
0012 #include "FWCore/Framework/interface/Event.h"
0013 #include "FWCore/Framework/interface/Run.h"
0014 #include "FWCore/Framework/interface/LuminosityBlock.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0018 #include "FWCore/Common/interface/TriggerNames.h"
0019 
0020 #include "DataFormats/Common/interface/Handle.h"
0021 
0022 //Triggers
0023 #include "DataFormats/Common/interface/TriggerResults.h"
0024 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0025 
0026 //#define EDM_ML_DEBUG
0027 //
0028 // class declaration
0029 //
0030 
0031 namespace alCaIsoTracksProducerFilter {
0032   struct Counters {
0033     Counters() : nAll_(0), nGood_(0) {}
0034     mutable std::atomic<unsigned int> nAll_, nGood_;
0035   };
0036 }  // namespace alCaIsoTracksProducerFilter
0037 
0038 class AlCaIsoTracksProducerFilter
0039     : public edm::stream::EDFilter<edm::GlobalCache<alCaIsoTracksProducerFilter::Counters> > {
0040 public:
0041   explicit AlCaIsoTracksProducerFilter(edm::ParameterSet const&, const alCaIsoTracksProducerFilter::Counters* count);
0042   ~AlCaIsoTracksProducerFilter() override = default;
0043 
0044   static std::unique_ptr<alCaIsoTracksProducerFilter::Counters> initializeGlobalCache(edm::ParameterSet const& iConfig) {
0045     return std::make_unique<alCaIsoTracksProducerFilter::Counters>();
0046   }
0047 
0048   bool filter(edm::Event&, edm::EventSetup const&) override;
0049   void endStream() override;
0050   static void globalEndJob(const alCaIsoTracksProducerFilter::Counters* counters);
0051   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0052 
0053 private:
0054   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0055   void endRun(edm::Run const&, edm::EventSetup const&) override;
0056 
0057   // ----------member data ---------------------------
0058   HLTConfigProvider hltConfig_;
0059   unsigned int nRun_, nAll_, nGood_;
0060   const std::vector<std::string> trigNames_;
0061   const std::string processName_;
0062   const edm::InputTag triggerResultsLabel_;
0063   const edm::EDGetTokenT<edm::TriggerResults> tok_trigRes_;
0064 };
0065 
0066 AlCaIsoTracksProducerFilter::AlCaIsoTracksProducerFilter(const edm::ParameterSet& iConfig,
0067                                                          const alCaIsoTracksProducerFilter::Counters* count)
0068     : nRun_(0),
0069       nAll_(0),
0070       nGood_(0),
0071       trigNames_(iConfig.getParameter<std::vector<std::string> >("triggers")),
0072       processName_(iConfig.getParameter<std::string>("processName")),
0073       triggerResultsLabel_(iConfig.getParameter<edm::InputTag>("triggerResultLabel")),
0074       tok_trigRes_(consumes<edm::TriggerResults>(triggerResultsLabel_)) {
0075   edm::LogVerbatim("HcalIsoTrack") << "Use process name " << processName_ << " Labels " << triggerResultsLabel_
0076                                    << " selecting " << trigNames_.size() << " triggers\n";
0077   for (unsigned int k = 0; k < trigNames_.size(); ++k) {
0078     edm::LogVerbatim("HcalIsoTrack") << "Trigger[" << k << "] " << trigNames_[k] << std::endl;
0079   }
0080 }
0081 
0082 bool AlCaIsoTracksProducerFilter::filter(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0083   ++nAll_;
0084   edm::LogVerbatim("HcalIsoTrack") << "Run " << iEvent.id().run() << " Event " << iEvent.id().event() << " Luminosity "
0085                                    << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing() << std::endl;
0086 
0087   //Find if the event passes one of the chosen triggers
0088   bool triggerSatisfied(false);
0089   if (trigNames_.empty()) {
0090     triggerSatisfied = true;
0091   } else {
0092     auto const& triggerResults = iEvent.getHandle(tok_trigRes_);
0093     if (triggerResults.isValid()) {
0094       std::vector<std::string> modules;
0095       const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
0096       const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
0097       for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
0098         int hlt = triggerResults->accept(iHLT);
0099         for (unsigned int i = 0; i < trigNames_.size(); ++i) {
0100           if (triggerNames_[iHLT].find(trigNames_[i]) != std::string::npos) {
0101 #ifdef EDM_ML_DEBUG
0102             edm::LogVerbatim("HcalIsoTrack")
0103                 << triggerNames_[iHLT] << " has got HLT flag " << hlt << ":" << triggerSatisfied;
0104 #endif
0105             if (hlt > 0) {
0106               triggerSatisfied = true;
0107               break;
0108             }
0109           }
0110         }
0111         if (triggerSatisfied)
0112           break;
0113       }
0114     }
0115   }
0116 #ifdef EDM_ML_DEBUG
0117   edm::LogVerbatim("HcalIsoTrack") << "AlCaIsoTracksProducerFilter:: triggerSatisfied: " << triggerSatisfied;
0118 #endif
0119   if (triggerSatisfied)
0120     ++nGood_;
0121   return triggerSatisfied;
0122 }
0123 
0124 void AlCaIsoTracksProducerFilter::endStream() {
0125   globalCache()->nAll_ += nAll_;
0126   globalCache()->nGood_ += nGood_;
0127 }
0128 
0129 void AlCaIsoTracksProducerFilter::globalEndJob(const alCaIsoTracksProducerFilter::Counters* count) {
0130   edm::LogVerbatim("HcalIsoTrack") << "Selects " << count->nGood_ << " in " << count->nAll_ << " events " << std::endl;
0131 }
0132 
0133 void AlCaIsoTracksProducerFilter::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0134   bool changed(false);
0135   bool flag = hltConfig_.init(iRun, iSetup, processName_, changed);
0136   edm::LogVerbatim("HcalIsoTrack") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init " << flag << std::endl;
0137 }
0138 
0139 void AlCaIsoTracksProducerFilter::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0140   ++nRun_;
0141   edm::LogVerbatim("HcalIsoTrack") << "endRun[" << nRun_ << "] " << iRun.run() << std::endl;
0142 }
0143 
0144 void AlCaIsoTracksProducerFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0145   //The following says we do not know what parameters are allowed so do no validation
0146   // Please change this to state exactly what you do use, even if it is no parameters
0147   edm::ParameterSetDescription desc;
0148   desc.add<edm::InputTag>("triggerResultLabel", edm::InputTag("TriggerResults", "", "HLT"));
0149   std::vector<std::string> trigger = {"HLT_IsoTrackHB", "HLT_IsoTrackHE"};
0150   desc.add<std::vector<std::string> >("triggers", trigger);
0151   desc.add<std::string>("processName", "HLT");
0152   descriptions.add("alcaIsoTracksProducerFilter", desc);
0153 }
0154 
0155 #include "FWCore/Framework/interface/MakerMacros.h"
0156 DEFINE_FWK_MODULE(AlCaIsoTracksProducerFilter);