File indexing completed on 2022-02-14 23:39:16
0001
0002 #include <atomic>
0003 #include <memory>
0004 #include <cmath>
0005 #include <iostream>
0006 #include <sstream>
0007 #include <fstream>
0008
0009
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
0037
0038
0039
0040
0041 namespace AlCaHBHEMuons {
0042 struct Counters {
0043 Counters() : nAll_(0), nGood_(0), nFinal_(0) {}
0044 mutable std::atomic<unsigned int> nAll_, nGood_, nFinal_;
0045 };
0046 }
0047
0048 class AlCaHBHEMuonFilter : public edm::stream::EDFilter<edm::GlobalCache<AlCaHBHEMuons::Counters> > {
0049 public:
0050 explicit AlCaHBHEMuonFilter(edm::ParameterSet const&, const AlCaHBHEMuons::Counters* count);
0051 ~AlCaHBHEMuonFilter() override;
0052
0053 static std::unique_ptr<AlCaHBHEMuons::Counters> initializeGlobalCache(edm::ParameterSet const&) {
0054 return std::make_unique<AlCaHBHEMuons::Counters>();
0055 }
0056
0057 bool filter(edm::Event&, edm::EventSetup const&) override;
0058 void endStream() override;
0059 static void globalEndJob(const AlCaHBHEMuons::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
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
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094 AlCaHBHEMuonFilter::AlCaHBHEMuonFilter(edm::ParameterSet const& iConfig, const AlCaHBHEMuons::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
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 }
0123
0124 AlCaHBHEMuonFilter::~AlCaHBHEMuonFilter() {}
0125
0126
0127
0128
0129
0130
0131 bool AlCaHBHEMuonFilter::filter(edm::Event& iEvent, edm::EventSetup const& iSetup) {
0132 bool accept(false);
0133 ++nAll_;
0134 #ifdef EDM_ML_DEBUG
0135 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Run " << iEvent.id().run() << " Event " << iEvent.id().event()
0136 << " Luminosity " << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing()
0137 << std::endl;
0138 #endif
0139
0140 bool ok(false);
0141
0142 if (trigNames_.empty()) {
0143 ok = true;
0144 } else {
0145 auto const& triggerResults = iEvent.getHandle(tok_trigRes_);
0146 if (triggerResults.isValid()) {
0147 std::vector<std::string> modules;
0148 const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
0149 const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
0150 for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
0151 int hlt = triggerResults->accept(iHLT);
0152 for (unsigned int i = 0; i < trigNames_.size(); ++i) {
0153 if (triggerNames_[iHLT].find(trigNames_[i]) != std::string::npos) {
0154 if (hlt > 0) {
0155 ok = true;
0156 }
0157 #ifdef EDM_ML_DEBUG
0158 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Trigger " << triggerNames_[iHLT] << " Flag " << hlt
0159 << ":" << ok << std::endl;
0160 #endif
0161 }
0162 }
0163 }
0164 }
0165 }
0166 if (ok) {
0167
0168 const MagneticField* bField = &(iSetup.getData(tok_magField_));
0169 const CaloGeometry* geo = &(iSetup.getData(tok_geom_));
0170
0171
0172 auto muonHandle = iEvent.getHandle(tok_Muon_);
0173 #ifdef EDM_ML_DEBUG
0174 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Muon Handle " << muonHandle.isValid();
0175 #endif
0176 if (muonHandle.isValid()) {
0177 #ifdef EDM_ML_DEBUG
0178 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Size of collection " << muonHandle->size();
0179 #endif
0180 for (reco::MuonCollection::const_iterator RecMuon = muonHandle->begin(); RecMuon != muonHandle->end();
0181 ++RecMuon) {
0182 #ifdef EDM_ML_DEBUG
0183 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Muon:Track " << RecMuon->track().isNonnull()
0184 << " innerTrack " << RecMuon->innerTrack().isNonnull() << " outerTrack "
0185 << RecMuon->outerTrack().isNonnull() << " globalTrack "
0186 << RecMuon->globalTrack().isNonnull() << std::endl;
0187 #endif
0188 bool check = (onlyOuterTrack_) ? (RecMuon->outerTrack().isNonnull())
0189 : ((RecMuon->track().isNonnull()) && (RecMuon->innerTrack().isNonnull()) &&
0190 (RecMuon->outerTrack().isNonnull()) && (RecMuon->globalTrack().isNonnull()));
0191 if (check) {
0192 const reco::Track* pTrack = (onlyOuterTrack_) ? (RecMuon->outerTrack()).get() : (RecMuon->innerTrack()).get();
0193 spr::propagatedTrackID trackID = spr::propagateCALO(pTrack, geo, bField, false);
0194 #ifdef EDM_ML_DEBUG
0195 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Propagate: ECAL " << trackID.okECAL << " to HCAL "
0196 << trackID.okHCAL << std::endl;
0197 #endif
0198 double trackIso = RecMuon->isolationR03().sumPt;
0199 double caloIso = RecMuon->isolationR03().emEt + RecMuon->isolationR03().hadEt;
0200 double isolR04 =
0201 ((RecMuon->pfIsolationR04().sumChargedHadronPt +
0202 std::max(0.,
0203 RecMuon->pfIsolationR04().sumNeutralHadronEt + RecMuon->pfIsolationR04().sumPhotonEt -
0204 (0.5 * RecMuon->pfIsolationR04().sumPUPt))) /
0205 RecMuon->pt());
0206 bool isoCut = (pfCut_) ? (isolR04 < pfIsoCut_) : ((trackIso < trackIsoCut_) && (caloIso < caloIsoCut_));
0207 #ifdef EDM_ML_DEBUG
0208 edm::LogVerbatim("HBHEMuon") << "AlCaHBHEMuonFilter::Isolation: " << trackIso << ":" << caloIso << ":"
0209 << isolR04 << " --> " << isoCut;
0210 #endif
0211 if ((trackID.okECAL) && (trackID.okHCAL) && isoCut) {
0212 accept = true;
0213 break;
0214 }
0215 }
0216 }
0217 }
0218 }
0219
0220 if (accept) {
0221 ++nGood_;
0222 if (((nGood_ - 1) % preScale_) != 0) {
0223 accept = false;
0224 } else {
0225 ++nFinal_;
0226 }
0227 }
0228 return accept;
0229
0230 }
0231
0232
0233 void AlCaHBHEMuonFilter::endStream() {
0234 globalCache()->nAll_ += nAll_;
0235 globalCache()->nGood_ += nGood_;
0236 globalCache()->nFinal_ += nFinal_;
0237 }
0238
0239 void AlCaHBHEMuonFilter::globalEndJob(const AlCaHBHEMuons::Counters* count) {
0240 edm::LogVerbatim("HBHEMuon") << "Selects " << count->nFinal_ << " out of " << count->nGood_ << " good events out of "
0241 << count->nAll_ << " total # of events\n";
0242 }
0243
0244
0245 void AlCaHBHEMuonFilter::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0246 bool changed(false);
0247 bool flag = hltConfig_.init(iRun, iSetup, processName_, changed);
0248 edm::LogVerbatim("HBHEMuon") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init " << flag << std::endl;
0249 }
0250
0251
0252 void AlCaHBHEMuonFilter::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0253 edm::LogVerbatim("HBHEMuon") << "endRun[" << nRun_ << "] " << iRun.run() << "\n";
0254 nRun_++;
0255 }
0256
0257
0258 void AlCaHBHEMuonFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0259
0260
0261 edm::ParameterSetDescription desc;
0262 std::vector<std::string> triggers = {"HLT_IsoMu", "HLT_Mu"};
0263 desc.add<std::string>("ProcessName", "HLT");
0264 desc.add<edm::InputTag>("TriggerResultLabel", edm::InputTag("TriggerResults", "", "HLT"));
0265 desc.add<edm::InputTag>("MuonLabel", edm::InputTag("muons"));
0266 desc.add<double>("MinimumMuonP", 10.0);
0267 desc.add<std::vector<std::string> >("Triggers", triggers);
0268 desc.add<bool>("PFCut", true);
0269 desc.add<double>("PFIsolationCut", 0.12);
0270 desc.add<double>("TrackIsolationCut", 3.0);
0271 desc.add<double>("CaloIsolationCut", 5.0);
0272 desc.add<int>("PreScale", 2);
0273 desc.add<bool>("OnlyOuterTrack", false);
0274 descriptions.add("AlcaHBHEMuonFilter", desc);
0275 }
0276
0277
0278 #include "FWCore/Framework/interface/MakerMacros.h"
0279 DEFINE_FWK_MODULE(AlCaHBHEMuonFilter);