CMS 3D CMS Logo

AlCaHEMuonFilter.cc
Go to the documentation of this file.
1 // system include files
2 #include <atomic>
3 #include <memory>
4 #include <cmath>
5 #include <iostream>
6 #include <sstream>
7 #include <fstream>
8 
9 // user include files
21 
28 
31 
36 
37 //#define DebugLog
38 //
39 // class declaration
40 //
41 
42 namespace AlCaHEMuons {
43  struct Counters {
44  Counters() : nAll_(0), nGood_(0), nFinal_(0) {}
45  mutable std::atomic<unsigned int> nAll_, nGood_, nFinal_;
46  };
47 } // namespace AlCaHEMuons
48 
49 class AlCaHEMuonFilter : public edm::stream::EDFilter<edm::GlobalCache<AlCaHEMuons::Counters> > {
50 public:
52  ~AlCaHEMuonFilter() override;
53 
54  static std::unique_ptr<AlCaHEMuons::Counters> initializeGlobalCache(edm::ParameterSet const&) {
55  return std::make_unique<AlCaHEMuons::Counters>();
56  }
57 
58  bool filter(edm::Event&, edm::EventSetup const&) override;
59  void endStream() override;
60  static void globalEndJob(const AlCaHEMuons::Counters* counters);
61  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
62 
63 private:
64  void beginRun(edm::Run const&, edm::EventSetup const&) override;
65  void endRun(edm::Run const&, edm::EventSetup const&) override;
66 
67  // ----------member data ---------------------------
69  std::vector<std::string> trigNames_, HLTNames_;
71  bool pfCut_;
72  double trackIsoCut_, caloIsoCut_, pfIsoCut_, muonptCut_, muonetaCut_;
73  int preScale_;
74  unsigned int nRun_, nAll_, nGood_, nFinal_;
79 };
80 
81 //
82 // constants, enums and typedefs
83 //
84 
85 //
86 // static data member definitions
87 //
88 
89 //
90 // constructors and destructor
91 //
93  : nRun_(0), nAll_(0), nGood_(0), nFinal_(0) {
94  //now do what ever initialization is needed
95  trigNames_ = iConfig.getParameter<std::vector<std::string> >("triggers");
96  processName_ = iConfig.getParameter<std::string>("processName");
97  triggerResults_ = iConfig.getParameter<edm::InputTag>("triggerResultLabel");
98  labelMuon_ = iConfig.getParameter<edm::InputTag>("muonLabel");
99  muonptCut_ = iConfig.getParameter<double>("muonPtCut");
100  muonetaCut_ = iConfig.getParameter<double>("muonEtaCut");
101  pfCut_ = iConfig.getParameter<bool>("pfCut");
102  pfIsoCut_ = iConfig.getParameter<double>("pfIsolationCut");
103  trackIsoCut_ = iConfig.getParameter<double>("trackIsolationCut");
104  caloIsoCut_ = iConfig.getParameter<double>("caloIsolationCut");
105  preScale_ = iConfig.getParameter<int>("preScale");
106  if (preScale_ < 1)
107  preScale_ = 1;
108 
109  // define tokens for access
110  tok_trigRes_ = consumes<edm::TriggerResults>(triggerResults_);
111  tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
112  edm::LogInfo("HEMuon") << "Parameters read from config file \n"
113  << "Process " << processName_ << " Prescale " << preScale_ << " Isolation Cuts "
114  << trackIsoCut_ << ":" << caloIsoCut_ << "\n";
115  for (unsigned int k = 0; k < trigNames_.size(); ++k)
116  edm::LogInfo("HEMuon") << "Trigger[" << k << "] " << trigNames_[k] << "\n";
117 } // AlCaHEMuonFilter::AlCaHEMuonFilter constructor
118 
120 
121 //
122 // member functions
123 //
124 
125 // ------------ method called on each new Event ------------
127  bool accept(false);
128  ++nAll_;
129 #ifdef DebugLog
130  edm::LogInfo("HEMuon") << "AlCaHEMuonFilter::Run " << iEvent.id().run() << " Event " << iEvent.id().event()
131  << " Luminosity " << iEvent.luminosityBlock() << " Bunch " << iEvent.bunchCrossing()
132  << std::endl;
133 #endif
134  //Step1: Find if the event passes one of the chosen triggers
137  iEvent.getByToken(tok_trigRes_, triggerResults);
138  if (triggerResults.isValid()) {
139  bool ok(false);
140  std::vector<std::string> modules;
141  const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
142  const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
143  for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
144  int hlt = triggerResults->accept(iHLT);
145  for (unsigned int i = 0; i < trigNames_.size(); ++i) {
146  if (triggerNames_[iHLT].find(trigNames_[i]) != std::string::npos) {
147  if (hlt > 0) {
148  ok = true;
149  }
150 #ifdef DebugLog
151  edm::LogInfo("HEMuon") << "AlCaHEMuonFilter::Trigger " << triggerNames_[iHLT] << " Flag " << hlt << ":" << ok
152  << std::endl;
153 #endif
154  }
155  }
156  }
157  if (ok) {
158  //Step2: Get geometry/B-field information
159  //Get magnetic field
161  iSetup.get<IdealMagneticFieldRecord>().get(bFieldH);
162  const MagneticField* bField = bFieldH.product();
163  // get handles to calogeometry
165  iSetup.get<CaloGeometryRecord>().get(pG);
166  const CaloGeometry* geo = pG.product();
167 
168  // Relevant blocks from iEvent
170  iEvent.getByToken(tok_Muon_, _Muon);
171 #ifdef DebugLog
172  edm::LogInfo("HEMuon") << "AlCaHEMuonFilter::Muon Handle " << _Muon.isValid() << std::endl;
173 #endif
174  if (_Muon.isValid()) {
175  for (reco::MuonCollection::const_iterator RecMuon = _Muon->begin(); RecMuon != _Muon->end(); ++RecMuon) {
176 #ifdef DebugLog
177  edm::LogInfo("HEMuon") << "AlCaHEMuonFilter::Muon:Track " << RecMuon->track().isNonnull() << " innerTrack "
178  << RecMuon->innerTrack().isNonnull() << " outerTrack "
179  << RecMuon->outerTrack().isNonnull() << " globalTrack "
180  << RecMuon->globalTrack().isNonnull() << std::endl;
181 #endif
182  if ((RecMuon->track().isNonnull()) && (RecMuon->innerTrack().isNonnull()) &&
183  (RecMuon->outerTrack().isNonnull()) && (RecMuon->globalTrack().isNonnull())) {
184  bool ptCut = RecMuon->pt() < muonptCut_;
185  bool etaCut = fabs(RecMuon->eta()) < muonetaCut_;
186  if (ptCut || etaCut)
187  continue;
188 
189  const reco::Track* pTrack = (RecMuon->innerTrack()).get();
190  spr::propagatedTrackID trackID = spr::propagateCALO(pTrack, geo, bField, false);
191 #ifdef DebugLog
192  edm::LogInfo("HEMuon") << "AlCaHEMuonFilter::Propagate: ECAL " << trackID.okECAL << " to HCAL "
193  << trackID.okHCAL << std::endl;
194 #endif
195  double trackIso = RecMuon->isolationR03().sumPt;
196  double caloIso = RecMuon->isolationR03().emEt + RecMuon->isolationR03().hadEt;
197  double isolR04 =
198  ((RecMuon->pfIsolationR04().sumChargedHadronPt +
199  std::max(0.,
200  RecMuon->pfIsolationR04().sumNeutralHadronEt + RecMuon->pfIsolationR04().sumPhotonEt -
201  (0.5 * RecMuon->pfIsolationR04().sumPUPt))) /
202  RecMuon->pt());
203  bool isoCut = (pfCut_) ? (isolR04 < pfIsoCut_) : ((trackIso < trackIsoCut_) && (caloIso < caloIsoCut_));
204  if ((trackID.okECAL) && (trackID.okHCAL) && isoCut) {
205  accept = true;
206  break;
207  }
208  }
209  }
210  }
211  }
212  }
213  // Step 4: Return the acceptance flag
214  if (accept) {
215  ++nGood_;
216  if (((nGood_ - 1) % preScale_) != 0) {
217  accept = false;
218  } else {
219  ++nFinal_;
220  }
221  }
222  return accept;
223 
224 } // AlCaHEMuonFilter::filter
225 
226 // ------------ method called once each job just after ending the event loop ------------
228  globalCache()->nAll_ += nAll_;
229  globalCache()->nGood_ += nGood_;
230  globalCache()->nFinal_ += nFinal_;
231 }
232 
234  edm::LogInfo("HEMuon") << "Selects " << count->nFinal_ << " out of " << count->nGood_ << " good events out of "
235  << count->nAll_ << " total # of events\n";
236 }
237 
238 // ------------ method called when starting to processes a run ------------
239 void AlCaHEMuonFilter::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
240  bool changed(false);
241  bool flag = hltConfig_.init(iRun, iSetup, processName_, changed);
242  edm::LogInfo("HEMuon") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init " << flag << std::endl;
243 }
244 
245 // ------------ method called when ending the processing of a run ------------
247  edm::LogInfo("HEMuon") << "endRun[" << nRun_ << "] " << iRun.run() << "\n";
248  nRun_++;
249 }
250 
251 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
253  //The following says we do not know what parameters are allowed so do no validation
254  // Please change this to state exactly what you do use, even if it is no parameters
256  std::vector<std::string> triggers = {"HLT_IsoMu", "HLT_Mu"};
257  desc.add<std::string>("processName", "HLT");
258  desc.add<edm::InputTag>("triggerResultLabel", edm::InputTag("TriggerResults", "", "HLT"));
259  desc.add<edm::InputTag>("muonLabel", edm::InputTag("muons"));
260  desc.add<double>("minimumMuonP", 10.0);
261  desc.add<std::vector<std::string> >("triggers", triggers);
262  desc.add<double>("muonPtCut", 20.0);
263  desc.add<double>("muonEtaCut", 1.305);
264  desc.add<bool>("pfCut", true);
265  desc.add<double>("pfIsolationCut", 0.15);
266  desc.add<double>("trackIsolationCut", 3.0);
267  desc.add<double>("caloIsolationCut", 5.0);
268  desc.add<int>("preScale", 1);
269  descriptions.add("alcaHEMuonFilter", desc);
270 }
271 
272 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
edm::InputTag labelMuon_
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
edm::EDGetTokenT< edm::TriggerResults > tok_trigRes_
std::vector< spr::propagatedTrackID > propagateCALO(edm::Handle< reco::TrackCollection > &trkCollection, const CaloGeometry *geo, const MagneticField *bField, const std::string &theTrackQuality, bool debug=false)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
RunNumber_t run() const
Definition: RunBase.h:40
~AlCaHEMuonFilter() override
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::atomic< unsigned int > nGood_
bool accept() const
Has at least one path accepted the event?
int bunchCrossing() const
Definition: EventBase.h:64
AlCaHEMuonFilter(edm::ParameterSet const &, const AlCaHEMuons::Counters *count)
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
void beginRun(edm::Run const &, edm::EventSetup const &) override
unsigned int nFinal_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
edm::EDGetTokenT< trigger::TriggerEvent > tok_trigEvt
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:30
void endStream() override
Strings const & triggerNames() const
Definition: TriggerNames.cc:20
std::atomic< unsigned int > nFinal_
std::atomic< unsigned int > nAll_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int nAll_
unsigned int size() const
Get number of paths stored.
edm::InputTag triggerResults_
static std::string const triggerResults
Definition: EdmProvDump.cc:45
HLTConfigProvider hltConfig_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
int k[5][pyjets_maxn]
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool filter(edm::Event &, edm::EventSetup const &) override
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
unsigned int nRun_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EventID id() const
Definition: EventBase.h:59
void endRun(edm::Run const &, edm::EventSetup const &) override
T get() const
Definition: EventSetup.h:71
static std::unique_ptr< AlCaHEMuons::Counters > initializeGlobalCache(edm::ParameterSet const &)
static void globalEndJob(const AlCaHEMuons::Counters *counters)
std::vector< std::string > trigNames_
edm::EDGetTokenT< reco::MuonCollection > tok_Muon_
unsigned int nGood_
T const * product() const
Definition: ESHandle.h:86
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:256
std::string processName_
Definition: Run.h:45