Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-02 06:10:13

0001 // system include files
0002 #include <algorithm>
0003 #include <atomic>
0004 #include <memory>
0005 #include <cmath>
0006 
0007 // user include files
0008 #include "FWCore/Framework/interface/Frameworkfwd.h"
0009 #include "FWCore/Framework/interface/global/EDFilter.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/Framework/interface/Run.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0016 
0017 #include "DataFormats/Common/interface/Handle.h"
0018 #include "DataFormats/Common/interface/Ref.h"
0019 #include "DataFormats/HcalCalibObjects/interface/HcalIsoTrkCalibVariables.h"
0020 
0021 //#define EDM_ML_DEBUG
0022 //
0023 // class declaration
0024 //
0025 
0026 namespace alcaHcalIsoTrk {
0027   struct Counters {
0028     Counters() : nAll_(0), nGood_(0), nLow_(0), nHigh_(0) {}
0029     mutable std::atomic<unsigned int> nAll_, nGood_, nLow_, nHigh_;
0030   };
0031 }  // namespace alcaHcalIsoTrk
0032 
0033 class AlCaHcalIsotrkFilter : public edm::global::EDFilter<edm::RunCache<alcaHcalIsoTrk::Counters>> {
0034 public:
0035   AlCaHcalIsotrkFilter(edm::ParameterSet const&);
0036   ~AlCaHcalIsotrkFilter() override = default;
0037 
0038   std::shared_ptr<alcaHcalIsoTrk::Counters> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
0039 
0040   bool filter(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0041   void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override;
0042   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0043 
0044 private:
0045   // ----------member data ---------------------------
0046   const double pTrackLow_, pTrackHigh_;
0047   const int prescaleLow_, prescaleHigh_;
0048   const edm::InputTag labelIsoTkVar_;
0049   const std::vector<int> debEvents_;
0050   const edm::EDGetTokenT<HcalIsoTrkCalibVariablesCollection> tokIsoTrkVar_;
0051 };
0052 
0053 //
0054 // constructors and destructor
0055 //
0056 AlCaHcalIsotrkFilter::AlCaHcalIsotrkFilter(edm::ParameterSet const& iConfig)
0057     : pTrackLow_(iConfig.getParameter<double>("momentumLow")),
0058       pTrackHigh_(iConfig.getParameter<double>("momentumHigh")),
0059       prescaleLow_(iConfig.getParameter<int>("prescaleLow")),
0060       prescaleHigh_(iConfig.getParameter<int>("prescaleHigh")),
0061       labelIsoTkVar_(iConfig.getParameter<edm::InputTag>("isoTrackLabel")),
0062       debEvents_(iConfig.getParameter<std::vector<int>>("debugEvents")),
0063       tokIsoTrkVar_(consumes<HcalIsoTrkCalibVariablesCollection>(labelIsoTkVar_)) {
0064   edm::LogVerbatim("HcalIsoTrack") << "Parameters read from config file \n\t momentumLow_ " << pTrackLow_
0065                                    << "\t prescaleLow_ " << prescaleLow_ << "\t momentumHigh_ " << pTrackHigh_
0066                                    << "\t prescaleHigh_ " << prescaleHigh_ << "\n\t Labels " << labelIsoTkVar_
0067                                    << "\tand " << debEvents_.size() << " events to be debugged";
0068 }  // AlCaHcalIsotrkFilter::AlCaHcalIsotrkFilter  constructor
0069 
0070 //
0071 // member functions
0072 //
0073 
0074 // ------------ method called on each new Event  ------------
0075 bool AlCaHcalIsotrkFilter::filter(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const {
0076   bool accept(false);
0077   ++(runCache(iEvent.getRun().index())->nAll_);
0078 #ifdef EDM_ML_DEBUG
0079   bool debug = (debEvents_.empty())
0080                    ? true
0081                    : (std::find(debEvents_.begin(), debEvents_.end(), iEvent.id().event()) != debEvents_.end());
0082   if (debug)
0083     edm::LogVerbatim("HcalIsoTrack") << "AlCaHcalIsotrkFilter::Run " << iEvent.id().run() << " Event "
0084                                      << iEvent.id().event() << " Luminosity " << iEvent.luminosityBlock() << " Bunch "
0085                                      << iEvent.bunchCrossing();
0086 #endif
0087 
0088   auto const& isotrkCalibColl = iEvent.getHandle(tokIsoTrkVar_);
0089   if (isotrkCalibColl.isValid()) {
0090     auto isotrkCalib = isotrkCalibColl.product();
0091     bool low(false), high(false), inRange(false);
0092     for (auto itr = isotrkCalib->begin(); itr != isotrkCalib->end(); ++itr) {
0093       if (itr->p_ < pTrackLow_) {
0094         low = true;
0095       } else if (itr->p_ > pTrackHigh_) {
0096         high = true;
0097       } else {
0098         inRange = true;
0099       }
0100     }
0101 #ifdef EDM_ML_DEBUG
0102     if (debug)
0103       edm::LogVerbatim("HcalIsoTrack") << "AlCaHcalIsotrkFilter::Finds " << isotrkCalib->size()
0104                                        << " entries in HcalIsoTrkCalibVariables collection with inRange " << inRange
0105                                        << " low " << low << " high " << high;
0106 #endif
0107     if (low)
0108       ++(runCache(iEvent.getRun().index())->nLow_);
0109     if (high)
0110       ++(runCache(iEvent.getRun().index())->nHigh_);
0111     if (inRange) {
0112       accept = true;
0113     } else {
0114       if (low) {
0115         if (prescaleLow_ <= 1)
0116           accept = true;
0117         else if (runCache(iEvent.getRun().index())->nLow_ % prescaleLow_ == 1)
0118           accept = true;
0119       }
0120       if (high) {
0121         if (prescaleHigh_ <= 1)
0122           accept = true;
0123         else if (runCache(iEvent.getRun().index())->nHigh_ % prescaleHigh_ == 1)
0124           accept = true;
0125       }
0126     }
0127   } else {
0128     edm::LogVerbatim("HcalIsoTrack") << "AlCaHcalIsotrkFilter::Cannot find the collection for HcalIsoTrkCalibVariables";
0129   }
0130 
0131   // Return the acceptance flag
0132   if (accept) {
0133     ++(runCache(iEvent.getRun().index())->nGood_);
0134     edm::LogVerbatim("HcalIsoTrackX") << "Run " << iEvent.id().run() << " Event " << iEvent.id().event();
0135   }
0136 #ifdef EDM_ML_DEBUG
0137   if (debug)
0138     edm::LogVerbatim("HcalIsoTrack") << "AlCaHcalIsotrkFilter::Accept flag " << accept << " All "
0139                                      << runCache(iEvent.getRun().index())->nAll_ << " Good "
0140                                      << runCache(iEvent.getRun().index())->nGood_ << " Low "
0141                                      << runCache(iEvent.getRun().index())->nLow_ << " High "
0142                                      << runCache(iEvent.getRun().index())->nHigh_;
0143 #endif
0144   return accept;
0145 
0146 }  // AlCaHcalIsotrkFilter::filter
0147 
0148 // ------------ method called when starting to processes a run  ------------
0149 std::shared_ptr<alcaHcalIsoTrk::Counters> AlCaHcalIsotrkFilter::globalBeginRun(edm::Run const& iRun,
0150                                                                                edm::EventSetup const&) const {
0151   edm::LogVerbatim("HcalIsoTrack") << "Start the Run " << iRun.run();
0152   return std::make_shared<alcaHcalIsoTrk::Counters>();
0153 }
0154 
0155 // ------------ method called when ending the processing of a run  ------------
0156 void AlCaHcalIsotrkFilter::globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const {
0157   edm::LogVerbatim("HcalIsoTrack") << "Select " << runCache(iRun.index())->nGood_ << " good events out of "
0158                                    << runCache(iRun.index())->nAll_ << " total # of events with "
0159                                    << runCache(iRun.index())->nLow_ << ":" << runCache(iRun.index())->nHigh_
0160                                    << " events below and above the required range";
0161 }
0162 
0163 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0164 void AlCaHcalIsotrkFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0165   edm::ParameterSetDescription desc;
0166   desc.add<double>("momentumLow", 40.0);
0167   desc.add<double>("momentumHigh", 60.0);
0168   desc.add<int>("prescaleLow", 1);
0169   desc.add<int>("prescaleHigh", 1);
0170   desc.add<edm::InputTag>("isoTrackLabel", edm::InputTag("alcaHcalIsotrkProducer", "HcalIsoTrack"));
0171   std::vector<int> events;
0172   desc.add<std::vector<int>>("debugEvents", events);
0173   descriptions.add("alcaHcalIsotrkFilter", desc);
0174 }
0175 
0176 //define this as a plug-in
0177 #include "FWCore/Framework/interface/MakerMacros.h"
0178 
0179 DEFINE_FWK_MODULE(AlCaHcalIsotrkFilter);