Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-25 23:29:57

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/EventSetup.h"
0014 #include "FWCore/Framework/interface/Run.h"
0015 #include "FWCore/Framework/interface/LuminosityBlock.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0018 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0019 #include "FWCore/Common/interface/TriggerNames.h"
0020 
0021 #include "DataFormats/Common/interface/Handle.h"
0022 #include "DataFormats/Common/interface/Ref.h"
0023 #include "DataFormats/Common/interface/TriggerResults.h"
0024 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0025 #include "DataFormats/MuonReco/interface/Muon.h"
0026 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0027 
0028 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0029 #include "Calibration/IsolatedParticles/interface/CaloPropagateTrack.h"
0030 
0031 #include "MagneticField/Engine/interface/MagneticField.h"
0032 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0033 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
0034 #include "Geometry/Records/interface/CaloGeometryRecord.h"
0035 
0036 //#define EDM_ML_DEBUG
0037 //
0038 // class declaration
0039 //
0040 
0041 namespace alCaHBHEMuonFilter {
0042   struct Counters {
0043     Counters() : nAll_(0), nGood_(0), nFinal_(0) {}
0044     mutable std::atomic<unsigned int> nAll_, nGood_, nFinal_;
0045   };
0046 }  // namespace alCaHBHEMuonFilter
0047 
0048 class AlCaHBHEMuonFilter : public edm::stream::EDFilter<edm::GlobalCache<alCaHBHEMuonFilter::Counters> > {
0049 public:
0050   explicit AlCaHBHEMuonFilter(edm::ParameterSet const&, const alCaHBHEMuonFilter::Counters* count);
0051   ~AlCaHBHEMuonFilter() override = default;
0052 
0053   static std::unique_ptr<alCaHBHEMuonFilter::Counters> initializeGlobalCache(edm::ParameterSet const&) {
0054     return std::make_unique<alCaHBHEMuonFilter::Counters>();
0055   }
0056 
0057   bool filter(edm::Event&, edm::EventSetup const&) override;
0058   void endStream() override;
0059   static void globalEndJob(const alCaHBHEMuonFilter::Counters* counters);
0060   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0061 
0062 private:
0063   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0064   void endRun(edm::Run const&, edm::EventSetup const&) override;
0065 
0066   // ----------member data ---------------------------
0067   HLTConfigProvider hltConfig_;
0068   unsigned int nRun_, nAll_, nGood_, nFinal_;
0069   const std::vector<std::string> trigNames_;
0070   const std::string processName_;
0071   const edm::InputTag triggerResults_, labelMuon_;
0072   const bool pfCut_;
0073   const double trackIsoCut_, caloIsoCut_, pfIsoCut_;
0074   const int preScale_;
0075   const bool onlyOuterTrack_;
0076   const edm::EDGetTokenT<trigger::TriggerEvent> tok_trigEvt;
0077   const edm::EDGetTokenT<edm::TriggerResults> tok_trigRes_;
0078   const edm::EDGetTokenT<reco::MuonCollection> tok_Muon_;
0079   const edm::ESGetToken<CaloGeometry, CaloGeometryRecord> tok_geom_;
0080   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> tok_magField_;
0081 };
0082 
0083 //
0084 // constants, enums and typedefs
0085 //
0086 
0087 //
0088 // static data member definitions
0089 //
0090 
0091 //
0092 // constructors and destructor
0093 //
0094 AlCaHBHEMuonFilter::AlCaHBHEMuonFilter(edm::ParameterSet const& iConfig, const alCaHBHEMuonFilter::Counters* count)
0095     : nRun_(0),
0096       nAll_(0),
0097       nGood_(0),
0098       nFinal_(0),
0099       trigNames_(iConfig.getParameter<std::vector<std::string> >("Triggers")),
0100       processName_(iConfig.getParameter<std::string>("ProcessName")),
0101       triggerResults_(iConfig.getParameter<edm::InputTag>("TriggerResultLabel")),
0102       labelMuon_(iConfig.getParameter<edm::InputTag>("MuonLabel")),
0103       pfCut_(iConfig.getParameter<bool>("PFCut")),
0104       trackIsoCut_(iConfig.getParameter<double>("TrackIsolationCut")),
0105       caloIsoCut_(iConfig.getParameter<double>("CaloIsolationCut")),
0106       pfIsoCut_(iConfig.getParameter<double>("PFIsolationCut")),
0107       preScale_(iConfig.getParameter<int>("PreScale") > 0 ? iConfig.getParameter<int>("PreScale") : 1),
0108       onlyOuterTrack_(iConfig.getParameter<bool>("OnlyOuterTrack")),
0109       tok_trigRes_(consumes<edm::TriggerResults>(triggerResults_)),
0110       tok_Muon_(consumes<reco::MuonCollection>(labelMuon_)),
0111       tok_geom_(esConsumes<CaloGeometry, CaloGeometryRecord>()),
0112       tok_magField_(esConsumes<MagneticField, IdealMagneticFieldRecord>()) {
0113   // load the parameters and define tokens for access
0114 
0115   edm::LogVerbatim("HBHEMuon") << "Parameters read from config file \nProcess " << processName_ << "  Prescale "
0116                                << preScale_ << "  Isolation Cuts " << pfCut_ << ":" << pfIsoCut_ << ":" << trackIsoCut_
0117                                << ":" << caloIsoCut_ << " OnlyOuterTrack " << onlyOuterTrack_ << "\nInput tags "
0118                                << labelMuon_ << " and " << triggerResults_ << "\nStudy " << trigNames_.size()
0119                                << " triggers: ";
0120   for (unsigned int k = 0; k < trigNames_.size(); ++k)
0121     edm::LogVerbatim("HBHEMuon") << "Trigger[" << k << "] " << trigNames_[k] << "\n";
0122 }  // AlCaHBHEMuonFilter::AlCaHBHEMuonFilter  constructor
0123 
0124 //
0125 // member functions
0126 //
0127 
0128 // ------------ method called on each new Event  ------------
0129 bool AlCaHBHEMuonFilter::filter(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0130   bool accept(false);
0131   ++nAll_;
0132 #ifdef EDM_ML_DEBUG
0133   edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Run " << iEvent.id().run() << " Event " << iEvent.id().event()
0134                                << " Luminosity " << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing()
0135                                << std::endl;
0136 #endif
0137   //Step1: Find if the event passes one of the chosen triggers
0138   bool ok(false);
0139   /////////////////////////////TriggerResults
0140   if (trigNames_.empty()) {
0141     ok = true;
0142   } else {
0143     auto const& triggerResults = iEvent.getHandle(tok_trigRes_);
0144     if (triggerResults.isValid()) {
0145       std::vector<std::string> modules;
0146       const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
0147       const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
0148       for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
0149         int hlt = triggerResults->accept(iHLT);
0150         for (unsigned int i = 0; i < trigNames_.size(); ++i) {
0151           if (triggerNames_[iHLT].find(trigNames_[i]) != std::string::npos) {
0152             if (hlt > 0) {
0153               ok = true;
0154             }
0155 #ifdef EDM_ML_DEBUG
0156             edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Trigger " << triggerNames_[iHLT] << " Flag " << hlt
0157                                          << ":" << ok << std::endl;
0158 #endif
0159           }
0160         }
0161       }
0162     }
0163   }
0164   if (ok) {
0165     //Step2: Get geometry/B-field information
0166     const MagneticField* bField = &(iSetup.getData(tok_magField_));
0167     const CaloGeometry* geo = &(iSetup.getData(tok_geom_));
0168 
0169     // Relevant blocks from iEvent
0170     auto muonHandle = iEvent.getHandle(tok_Muon_);
0171 #ifdef EDM_ML_DEBUG
0172     edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Muon Handle " << muonHandle.isValid();
0173 #endif
0174     if (muonHandle.isValid()) {
0175 #ifdef EDM_ML_DEBUG
0176       edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Size of collection " << muonHandle->size();
0177 #endif
0178       for (reco::MuonCollection::const_iterator RecMuon = muonHandle->begin(); RecMuon != muonHandle->end();
0179            ++RecMuon) {
0180 #ifdef EDM_ML_DEBUG
0181         edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Muon:Track " << RecMuon->track().isNonnull()
0182                                      << " innerTrack " << RecMuon->innerTrack().isNonnull() << " outerTrack "
0183                                      << RecMuon->outerTrack().isNonnull() << " globalTrack "
0184                                      << RecMuon->globalTrack().isNonnull() << std::endl;
0185 #endif
0186         bool check = (onlyOuterTrack_) ? (RecMuon->outerTrack().isNonnull())
0187                                        : ((RecMuon->track().isNonnull()) && (RecMuon->innerTrack().isNonnull()) &&
0188                                           (RecMuon->outerTrack().isNonnull()) && (RecMuon->globalTrack().isNonnull()));
0189         if (check) {
0190           const reco::Track* pTrack = (onlyOuterTrack_) ? (RecMuon->outerTrack()).get() : (RecMuon->innerTrack()).get();
0191           spr::propagatedTrackID trackID = spr::propagateCALO(pTrack, geo, bField, false);
0192 #ifdef EDM_ML_DEBUG
0193           edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Propagate: ECAL " << trackID.okECAL << " to HCAL "
0194                                        << trackID.okHCAL << std::endl;
0195 #endif
0196           double trackIso = RecMuon->isolationR03().sumPt;
0197           double caloIso = RecMuon->isolationR03().emEt + RecMuon->isolationR03().hadEt;
0198           double isolR04 =
0199               ((RecMuon->pfIsolationR04().sumChargedHadronPt +
0200                 std::max(0.,
0201                          RecMuon->pfIsolationR04().sumNeutralHadronEt + RecMuon->pfIsolationR04().sumPhotonEt -
0202                              (0.5 * RecMuon->pfIsolationR04().sumPUPt))) /
0203                RecMuon->pt());
0204           bool isoCut = (pfCut_) ? (isolR04 < pfIsoCut_) : ((trackIso < trackIsoCut_) && (caloIso < caloIsoCut_));
0205 #ifdef EDM_ML_DEBUG
0206           edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Isolation: " << trackIso << ":" << caloIso << ":"
0207                                        << isolR04 << " --> " << isoCut;
0208 #endif
0209           if ((trackID.okECAL) && (trackID.okHCAL) && isoCut) {
0210             accept = true;
0211             break;
0212           }
0213         }
0214       }
0215     }
0216   }
0217   // Step 4:  Return the acceptance flag
0218   if (accept) {
0219     ++nGood_;
0220     if (((nGood_ - 1) % preScale_) != 0) {
0221       accept = false;
0222     } else {
0223       ++nFinal_;
0224     }
0225   }
0226   return accept;
0227 
0228 }  // AlCaHBHEMuonFilter::filter
0229 
0230 // ------------ method called once each job just after ending the event loop  ------------
0231 void AlCaHBHEMuonFilter::endStream() {
0232   globalCache()->nAll_ += nAll_;
0233   globalCache()->nGood_ += nGood_;
0234   globalCache()->nFinal_ += nFinal_;
0235 }
0236 
0237 void AlCaHBHEMuonFilter::globalEndJob(const alCaHBHEMuonFilter::Counters* count) {
0238   edm::LogVerbatim("HBHEMuon") << "Selects " << count->nFinal_ << " out of " << count->nGood_ << " good events out of "
0239                                << count->nAll_ << " total # of events\n";
0240 }
0241 
0242 // ------------ method called when starting to processes a run  ------------
0243 void AlCaHBHEMuonFilter::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0244   bool changed(false);
0245   bool flag = hltConfig_.init(iRun, iSetup, processName_, changed);
0246   edm::LogVerbatim("HBHEMuon") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init " << flag << std::endl;
0247 }
0248 
0249 // ------------ method called when ending the processing of a run  ------------
0250 void AlCaHBHEMuonFilter::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0251   edm::LogVerbatim("HBHEMuon") << "endRun[" << nRun_ << "] " << iRun.run() << "\n";
0252   nRun_++;
0253 }
0254 
0255 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0256 void AlCaHBHEMuonFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0257   //The following says we do not know what parameters are allowed so do no validation
0258   // Please change this to state exactly what you do use, even if it is no parameters
0259   edm::ParameterSetDescription desc;
0260   std::vector<std::string> triggers = {"HLT_IsoMu", "HLT_Mu"};
0261   desc.add<std::string>("ProcessName", "HLT");
0262   desc.add<edm::InputTag>("TriggerResultLabel", edm::InputTag("TriggerResults", "", "HLT"));
0263   desc.add<edm::InputTag>("MuonLabel", edm::InputTag("muons"));
0264   desc.add<double>("MinimumMuonP", 5.0);
0265   desc.add<std::vector<std::string> >("Triggers", triggers);
0266   desc.add<bool>("PFCut", true);
0267   desc.add<double>("PFIsolationCut", 0.12);
0268   desc.add<double>("TrackIsolationCut", 3.0);
0269   desc.add<double>("CaloIsolationCut", 5.0);
0270   desc.add<int>("PreScale", 2);
0271   desc.add<bool>("OnlyOuterTrack", false);
0272   descriptions.add("AlcaHBHEMuonFilter", desc);
0273 }
0274 
0275 //define this as a plug-in
0276 #include "FWCore/Framework/interface/MakerMacros.h"
0277 DEFINE_FWK_MODULE(AlCaHBHEMuonFilter);