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 //Triggers
0022 #include "DataFormats/Common/interface/TriggerResults.h"
0023 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0024 //
0025 // class declaration
0026 //
0027 
0028 //#define EDM_ML_DEBUG
0029 
0030 namespace alCaIsolatedBunchSelector {
0031   struct Counters {
0032     Counters() : nAll_(0), nGood_(0) {}
0033     mutable std::atomic<unsigned int> nAll_, nGood_;
0034   };
0035 }  // namespace alCaIsolatedBunchSelector
0036 
0037 class AlCaIsolatedBunchSelector : public edm::stream::EDFilter<edm::GlobalCache<alCaIsolatedBunchSelector::Counters> > {
0038 public:
0039   explicit AlCaIsolatedBunchSelector(edm::ParameterSet const&, const alCaIsolatedBunchSelector::Counters* count);
0040   ~AlCaIsolatedBunchSelector() override = default;
0041 
0042   static std::unique_ptr<alCaIsolatedBunchSelector::Counters> initializeGlobalCache(edm::ParameterSet const& iConfig) {
0043     return std::make_unique<alCaIsolatedBunchSelector::Counters>();
0044   }
0045 
0046   bool filter(edm::Event&, edm::EventSetup const&) override;
0047   void endStream() override;
0048   static void globalEndJob(const alCaIsolatedBunchSelector::Counters* counters);
0049   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0050 
0051 private:
0052   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0053   void endRun(edm::Run const&, edm::EventSetup const&) override;
0054 
0055   // ----------member data ---------------------------
0056   HLTConfigProvider hltConfig_;
0057   unsigned int nRun_, nAll_, nGood_;
0058   const std::string trigName_;
0059   const std::string processName_;
0060   const edm::InputTag theTriggerResultsLabel_;
0061   const edm::EDGetTokenT<edm::TriggerResults> tok_trigRes_;
0062 };
0063 
0064 //
0065 // constructors and destructor
0066 //
0067 AlCaIsolatedBunchSelector::AlCaIsolatedBunchSelector(const edm::ParameterSet& iConfig,
0068                                                      const alCaIsolatedBunchSelector::Counters* count)
0069     : nRun_(0),
0070       nAll_(0),
0071       nGood_(0),
0072       trigName_(iConfig.getParameter<std::string>("triggerName")),
0073       processName_(iConfig.getParameter<std::string>("processName")),
0074       theTriggerResultsLabel_(iConfig.getParameter<edm::InputTag>("triggerResultLabel")),
0075       tok_trigRes_(consumes<edm::TriggerResults>(theTriggerResultsLabel_)) {
0076   edm::LogVerbatim("AlCaIsoBunch") << "Input tag for trigger results " << theTriggerResultsLabel_
0077                                    << " with trigger name " << trigName_ << " and process " << processName_
0078                                    << std::endl;
0079 }
0080 
0081 //
0082 // member functions
0083 //
0084 
0085 // ------------ method called on each new Event  ------------
0086 bool AlCaIsolatedBunchSelector::filter(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0087   bool accept(false);
0088   ++nAll_;
0089 #ifdef EDM_ML_DEBUG
0090   edm::LogVerbatim("AlCaIsoBunch") << "Run " << iEvent.id().run() << " Event " << iEvent.id().event() << " Luminosity "
0091                                    << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing() << std::endl;
0092 #endif
0093   if (trigName_.empty()) {
0094     accept = true;
0095   } else {
0096     //Step1: Find if the event passes the chosen trigger
0097     auto const& triggerResults = iEvent.getHandle(tok_trigRes_);
0098     if (triggerResults.isValid()) {
0099       const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
0100       const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
0101       for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
0102         int hlt = triggerResults->accept(iHLT);
0103         if (triggerNames_[iHLT].find(trigName_) != std::string::npos) {
0104           if (hlt > 0) {
0105             accept = true;
0106 #ifdef EDM_ML_DEBUG
0107             edm::LogVerbatim("AlCaIsoBunch")
0108                 << triggerNames_[iHLT] << " has got HLT flag " << hlt << ":" << accept << std::endl;
0109 #endif
0110             break;
0111           }
0112         }
0113       }
0114     }
0115   }
0116 
0117   // Step 2:  Return the acceptance flag
0118   if (accept)
0119     ++nGood_;
0120   return accept;
0121 
0122 }  // AlCaIsolatedBunchSelector::filter
0123 // ------------ method called once each job just after ending the event loop  ------------
0124 void AlCaIsolatedBunchSelector::endStream() {
0125   globalCache()->nAll_ += nAll_;
0126   globalCache()->nGood_ += nGood_;
0127 }
0128 
0129 void AlCaIsolatedBunchSelector::globalEndJob(const alCaIsolatedBunchSelector::Counters* count) {
0130   edm::LogVerbatim("AlCaIsoBunch") << "Selects " << count->nGood_ << " in " << count->nAll_ << " events" << std::endl;
0131 }
0132 
0133 // ------------ method called when starting to processes a run  ------------
0134 void AlCaIsolatedBunchSelector::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0135   bool changed(false);
0136   edm::LogVerbatim("AlCaIsoBunch") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init "
0137                                    << hltConfig_.init(iRun, iSetup, processName_, changed) << std::endl;
0138 }
0139 // ------------ method called when ending the processing of a run  ------------
0140 void AlCaIsolatedBunchSelector::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0141   ++nRun_;
0142   edm::LogVerbatim("AlCaIsoBunch") << "endRun[" << nRun_ << "] " << iRun.run() << std::endl;
0143 }
0144 
0145 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0146 void AlCaIsolatedBunchSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0147   edm::ParameterSetDescription desc;
0148   desc.add<edm::InputTag>("triggerResultLabel", edm::InputTag("TriggerResults", "", "HLT"));
0149   desc.add<std::string>("processName", "HLT");
0150   desc.add<std::string>("triggerName", "");
0151   descriptions.add("alcaIsolatedBunchSelector", desc);
0152 }
0153 
0154 //define this as a plug-in
0155 #include "FWCore/Framework/interface/MakerMacros.h"
0156 DEFINE_FWK_MODULE(AlCaIsolatedBunchSelector);