File indexing completed on 2024-04-06 11:58:44
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 alCaHBHEMuonFilter {
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<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
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 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
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
0125
0126
0127
0128
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
0138 bool ok(false);
0139
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
0166 const MagneticField* bField = &(iSetup.getData(tok_magField_));
0167 const CaloGeometry* geo = &(iSetup.getData(tok_geom_));
0168
0169
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
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 }
0229
0230
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
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
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
0256 void AlCaHBHEMuonFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0257
0258
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
0276 #include "FWCore/Framework/interface/MakerMacros.h"
0277 DEFINE_FWK_MODULE(AlCaHBHEMuonFilter);