Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-12 02:57:56

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 alcaHcalHBHEMuonFilter {
0026   struct Counters {
0027     Counters() : nAll_(0), nGood_(0), nFinal_(0) {}
0028     mutable std::atomic<unsigned int> nAll_, nGood_, nFinal_;
0029   };
0030 }  // namespace alcaHcalHBHEMuonFilter
0031 
0032 class AlCaHcalHBHEMuonFilter : public edm::global::EDFilter<edm::RunCache<alcaHcalHBHEMuonFilter::Counters> > {
0033 public:
0034   AlCaHcalHBHEMuonFilter(edm::ParameterSet const&);
0035   ~AlCaHcalHBHEMuonFilter() override = default;
0036 
0037   std::shared_ptr<alcaHcalHBHEMuonFilter::Counters> globalBeginRun(edm::Run const&,
0038                                                                    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 int prescale_;
0047   const double minimumMuonP_;
0048   const edm::InputTag labelHBHEMuonVar_;
0049   const edm::EDGetTokenT<HcalHBHEMuonVariablesCollection> tokHBHEMuonVar_;
0050 };
0051 
0052 //
0053 // constructors and destructor
0054 //
0055 AlCaHcalHBHEMuonFilter::AlCaHcalHBHEMuonFilter(edm::ParameterSet const& iConfig)
0056     : prescale_(iConfig.getParameter<int>("prescale")),
0057       minimumMuonP_(iConfig.getParameter<double>("minimumMuonP")),
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_ << "\t minimumMuonP_ "
0061                                << minimumMuonP_ << "\n\t Labels " << labelHBHEMuonVar_;
0062 }  // AlCaHcalHBHEMuonFilter::AlCaHcalHBHEMuonFilter  constructor
0063 
0064 //
0065 // member functions
0066 //
0067 
0068 // ------------ method called on each new Event  ------------
0069 bool AlCaHcalHBHEMuonFilter::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") << "AlCaHcalHBHEMuonFilter::Run " << iEvent.id().run() << " Event "
0074                                << iEvent.id().event() << " Luminosity " << iEvent.luminosityBlock() << " Bunch "
0075                                << iEvent.bunchCrossing();
0076 #endif
0077 
0078   auto const& hbheMuonColl = iEvent.getHandle(tokHBHEMuonVar_);
0079   if (hbheMuonColl.isValid()) {
0080     auto hbheMuon = hbheMuonColl.product();
0081     if (!hbheMuon->empty()) {
0082       bool ok(false);
0083       for (auto const& muon : *hbheMuon)
0084         if (muon.pMuon_ >= minimumMuonP_)
0085           ok = true;
0086       if (ok) {
0087         ++(runCache(iEvent.getRun().index())->nGood_);
0088         if (prescale_ <= 1)
0089           accept = true;
0090         else if (runCache(iEvent.getRun().index())->nGood_ % prescale_ == 1)
0091           accept = true;
0092       }
0093     }
0094   } else {
0095     edm::LogVerbatim("HBHEMuon") << "AlCaHcalHBHEMuonFilter::Cannot find the collection for HcalHBHEMuonVariables";
0096   }
0097 
0098   // Return the acceptance flag
0099   if (accept)
0100     ++(runCache(iEvent.getRun().index())->nFinal_);
0101 #ifdef EDM_ML_DEBUG
0102   edm::LogVerbatim("HBHEMuon") << "AlCaHcalHBHEMuonFilter::Accept flag " << accept << " All "
0103                                << runCache(iEvent.getRun().index())->nAll_ << " Good "
0104                                << runCache(iEvent.getRun().index())->nGood_ << " Final "
0105                                << runCache(iEvent.getRun().index())->nFinal_;
0106 #endif
0107   return accept;
0108 
0109 }  // AlCaHcalHBHEMuonFilter::filter
0110 
0111 // ------------ method called when starting to processes a run  ------------
0112 std::shared_ptr<alcaHcalHBHEMuonFilter::Counters> AlCaHcalHBHEMuonFilter::globalBeginRun(edm::Run const& iRun,
0113                                                                                          edm::EventSetup const&) const {
0114   edm::LogVerbatim("HBHEMuon") << "Start the Run " << iRun.run();
0115   return std::make_shared<alcaHcalHBHEMuonFilter::Counters>();
0116 }
0117 
0118 // ------------ method called when ending the processing of a run  ------------
0119 void AlCaHcalHBHEMuonFilter::globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const {
0120   edm::LogVerbatim("HBHEMuon") << "Select " << runCache(iRun.index())->nFinal_ << " out of "
0121                                << runCache(iRun.index())->nGood_ << " good and " << runCache(iRun.index())->nAll_
0122                                << " total events";
0123 }
0124 
0125 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0126 void AlCaHcalHBHEMuonFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0127   edm::ParameterSetDescription desc;
0128   desc.add<int>("prescale", 1);
0129   desc.add<double>("minimumMuonP", 10.0);
0130   desc.add<edm::InputTag>("hbheMuonLabel", edm::InputTag("alcaHcalHBHEMuonProducer", "hbheMuon"));
0131   descriptions.add("alcaHcalHBHEMuonFilter", desc);
0132 }
0133 
0134 //define this as a plug-in
0135 #include "FWCore/Framework/interface/MakerMacros.h"
0136 
0137 DEFINE_FWK_MODULE(AlCaHcalHBHEMuonFilter);