Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-12 08:23:55

0001 // system include files
0002 #include <atomic>
0003 #include <memory>
0004 #include <cmath>
0005 
0006 // user include files
0007 #include "FWCore/Framework/interface/Frameworkfwd.h"
0008 #include "FWCore/Framework/interface/global/EDFilter.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/EventSetup.h"
0011 #include "FWCore/Framework/interface/Run.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 
0016 #include "DataFormats/Common/interface/Handle.h"
0017 #include "DataFormats/Common/interface/Ref.h"
0018 #include "DataFormats/HcalCalibObjects/interface/HcalHBHEMuonVariables.h"
0019 
0020 //#define EDM_ML_DEBUG
0021 //
0022 // class declaration
0023 //
0024 
0025 namespace alcaHcalHBHEMuon {
0026   struct Counters {
0027     Counters() : nAll_(0), nGood_(0), nFinal_(0) {}
0028     mutable std::atomic<unsigned int> nAll_, nGood_, nFinal_;
0029   };
0030 }  // namespace alcaHcalHBHEMuon
0031 
0032 class AlCaHcalHEMuonFilter : public edm::global::EDFilter<edm::RunCache<alcaHcalHBHEMuon::Counters> > {
0033 public:
0034   AlCaHcalHEMuonFilter(edm::ParameterSet const&);
0035   ~AlCaHcalHEMuonFilter() override = default;
0036 
0037   std::shared_ptr<alcaHcalHBHEMuon::Counters> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
0038 
0039   bool filter(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0040   void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override;
0041   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0042 
0043 private:
0044   // ----------member data ---------------------------
0045   const int prescale_;
0046   const double muonptCut_, muonetaCut_;
0047   const edm::InputTag labelHBHEMuonVar_;
0048   const edm::EDGetTokenT<HcalHBHEMuonVariablesCollection> tokHBHEMuonVar_;
0049 };
0050 
0051 //
0052 // constructors and destructor
0053 //
0054 AlCaHcalHEMuonFilter::AlCaHcalHEMuonFilter(edm::ParameterSet const& iConfig)
0055     : prescale_(iConfig.getParameter<int>("prescale")),
0056       muonptCut_(iConfig.getParameter<double>("muonPtCut")),
0057       muonetaCut_(iConfig.getParameter<double>("muonEtaCut")),
0058       labelHBHEMuonVar_(iConfig.getParameter<edm::InputTag>("hbheMuonLabel")),
0059       tokHBHEMuonVar_(consumes<HcalHBHEMuonVariablesCollection>(labelHBHEMuonVar_)) {
0060   edm::LogVerbatim("HBHEMuon") << "Parameters read from config file \n\t prescale_ " << prescale_ << "\n\t Labels "
0061                                << labelHBHEMuonVar_;
0062 }  // AlCaHcalHEMuonFilter::AlCaHcalHEMuonFilter  constructor
0063 
0064 //
0065 // member functions
0066 //
0067 
0068 // ------------ method called on each new Event  ------------
0069 bool AlCaHcalHEMuonFilter::filter(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const {
0070   bool accept(false);
0071   ++(runCache(iEvent.getRun().index())->nAll_);
0072 #ifdef EDM_ML_DEBUG
0073   edm::LogVerbatim("HBHEMuon") << "AlCaHcalHEMuonFilter::Run " << iEvent.id().run() << " Event " << iEvent.id().event()
0074                                << " Luminosity " << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing();
0075 #endif
0076 
0077   auto const& hbheMuonColl = iEvent.getHandle(tokHBHEMuonVar_);
0078   if (hbheMuonColl.isValid()) {
0079     auto hbheMuon = hbheMuonColl.product();
0080     if (!hbheMuon->empty()) {
0081       bool ok(false);
0082       for (auto const& muon : *hbheMuon)
0083         if ((muon.ptGlob_ >= muonptCut_) && (std::abs(muon.etaGlob_) > muonetaCut_))
0084           ok = true;
0085 #ifdef EDM_ML_DEBUG
0086       edm::LogVerbatim("HBHEMuon") << "AlCaHcalHEMuonFilter::Flag for finding a muon with pt > " << muonptCut_
0087                                    << " and |eta| > " << muonetaCut_ << " is " << ok;
0088 #endif
0089       if (ok) {
0090         ++(runCache(iEvent.getRun().index())->nGood_);
0091         if (prescale_ <= 1)
0092           accept = true;
0093         else if (runCache(iEvent.getRun().index())->nGood_ % prescale_ == 1)
0094           accept = true;
0095       }
0096     }
0097   } else {
0098     edm::LogVerbatim("HBHEMuon") << "AlCaHcalHEMuonFilter::Cannot find the collection for HcalHBHEMuonVariables";
0099   }
0100 
0101   // Return the acceptance flag
0102   if (accept)
0103     ++(runCache(iEvent.getRun().index())->nFinal_);
0104 #ifdef EDM_ML_DEBUG
0105   edm::LogVerbatim("HBHEMuon") << "AlCaHcalHEMuonFilter::Accept flag " << accept << " All "
0106                                << runCache(iEvent.getRun().index())->nAll_ << " Good "
0107                                << runCache(iEvent.getRun().index())->nGood_ << " Final "
0108                                << runCache(iEvent.getRun().index())->nFinal_;
0109 #endif
0110   return accept;
0111 
0112 }  // AlCaHcalHEMuonFilter::filter
0113 
0114 // ------------ method called when starting to processes a run  ------------
0115 std::shared_ptr<alcaHcalHBHEMuon::Counters> AlCaHcalHEMuonFilter::globalBeginRun(edm::Run const& iRun,
0116                                                                                  edm::EventSetup const&) const {
0117   edm::LogVerbatim("HBHEMuon") << "Start the Run " << iRun.run();
0118   return std::make_shared<alcaHcalHBHEMuon::Counters>();
0119 }
0120 
0121 // ------------ method called when ending the processing of a run  ------------
0122 void AlCaHcalHEMuonFilter::globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const {
0123   edm::LogVerbatim("HBHEMuon") << "Select " << runCache(iRun.index())->nFinal_ << " out of "
0124                                << runCache(iRun.index())->nGood_ << " good and " << runCache(iRun.index())->nAll_
0125                                << " total events";
0126 }
0127 
0128 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0129 void AlCaHcalHEMuonFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0130   edm::ParameterSetDescription desc;
0131   desc.add<int>("prescale", 1);
0132   desc.add<double>("muonPtCut", 20.0);
0133   desc.add<double>("muonEtaCut", 1.305);
0134   desc.add<edm::InputTag>("hbheMuonLabel", edm::InputTag("alcaHcalHBHEMuonProducer", "hbheMuon"));
0135   descriptions.add("alcaHcalHEMuonFilter", desc);
0136 }
0137 
0138 //define this as a plug-in
0139 #include "FWCore/Framework/interface/MakerMacros.h"
0140 
0141 DEFINE_FWK_MODULE(AlCaHcalHEMuonFilter);