Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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