CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HLTMuonMatchAndPlot.cc
Go to the documentation of this file.
1 
6 
15 #include <boost/algorithm/string/replace.hpp>
16 
17 #include <iostream>
18 #include <string>
19 #include <utility>
20 #include <utility>
21 
24 
25 using namespace std;
26 using namespace edm;
27 using namespace reco;
28 using namespace trigger;
29 using namespace l1extra;
30 
31 using vstring = std::vector<std::string>;
32 
35 
37 HLTMuonMatchAndPlot::HLTMuonMatchAndPlot(const ParameterSet& pset, string hltPath, string moduleLabel, bool islastfilter)
38  : hltProcessName_(pset.getParameter<string>("hltProcessName")),
39  destination_(pset.getUntrackedParameter<string>("destination")),
40  requiredTriggers_(pset.getUntrackedParameter<vstring>("requiredTriggers")),
41  targetParams_(pset.getParameterSet("targetParams")),
42  probeParams_(pset.getParameterSet("probeParams")),
43  hltPath_(std::move(hltPath)),
44  moduleLabel_(std::move(moduleLabel)),
45  isLastFilter_(islastfilter),
46  hasTargetRecoCuts(targetParams_.exists("recoCuts")),
47  hasProbeRecoCuts(probeParams_.exists("recoCuts")),
48  targetMuonSelector_(targetParams_.getUntrackedParameter<string>("recoCuts", "")),
49  targetZ0Cut_(targetParams_.getUntrackedParameter<double>("z0Cut", 0.)),
50  targetD0Cut_(targetParams_.getUntrackedParameter<double>("d0Cut", 0.)),
51  targetptCutZ_(targetParams_.getUntrackedParameter<double>("ptCut_Z", 20.)),
52  targetptCutJpsi_(targetParams_.getUntrackedParameter<double>("ptCut_Jpsi", 20.)),
53  probeMuonSelector_(probeParams_.getUntrackedParameter<string>("recoCuts", "")),
54  probeZ0Cut_(probeParams_.getUntrackedParameter<double>("z0Cut", 0.)),
55  probeD0Cut_(probeParams_.getUntrackedParameter<double>("d0Cut", 0.)),
56  triggerSelector_(targetParams_.getUntrackedParameter<string>("hltCuts", "")),
57  hasTriggerCuts_(targetParams_.exists("hltCuts")) {
58  // Create std::map<string, T> from ParameterSets.
59  fillMapFromPSet(binParams_, pset, "binParams");
60  fillMapFromPSet(plotCuts_, pset, "plotCuts");
61 
62  // Get the trigger level.
63  triggerLevel_ = "L3";
64  TPRegexp levelRegexp("L[1-3]");
65  // size_t nModules = moduleLabels_.size();
66  // cout << moduleLabel_ << " " << hltPath_ << endl;
67  TObjArray* levelArray = levelRegexp.MatchS(moduleLabel_);
68  if (levelArray->GetEntriesFast() > 0) {
69  triggerLevel_ = static_cast<const char*>(((TObjString*)levelArray->At(0))->GetString());
70  }
71  delete levelArray;
72 
73  // Get the pT cut by parsing the name of the HLT path.
74  cutMinPt_ = 3;
75  TPRegexp ptRegexp("Mu([0-9]*)");
76  TObjArray* objArray = ptRegexp.MatchS(hltPath_);
77  if (objArray->GetEntriesFast() >= 2) {
78  auto* ptCutString = (TObjString*)objArray->At(1);
79  cutMinPt_ = atoi(ptCutString->GetString());
80  cutMinPt_ = ceil(cutMinPt_ * plotCuts_["minPtFactor"]);
81  }
82  delete objArray;
83 }
84 
85 void HLTMuonMatchAndPlot::beginRun(DQMStore::IBooker& iBooker, const edm::Run& iRun, const edm::EventSetup& iSetup) {
86  TPRegexp suffixPtCut("Mu[0-9]+$");
87 
88  string baseDir = destination_;
89  if (baseDir[baseDir.size() - 1] != '/')
90  baseDir += '/';
91  string pathSansSuffix = hltPath_;
92  if (hltPath_.rfind("_v") < hltPath_.length())
93  pathSansSuffix = hltPath_.substr(0, hltPath_.rfind("_v"));
94 
95  if (isLastFilter_)
96  iBooker.setCurrentFolder(baseDir + pathSansSuffix);
97  else
98  iBooker.setCurrentFolder(baseDir + pathSansSuffix + "/" + moduleLabel_);
99 
100  // Form is book1D(name, binningType, title) where 'binningType' is used
101  // to fetch the bin settings from binParams_.
102  if (isLastFilter_) {
103  book1D(iBooker, "hltPt", "pt", ";p_{T} of HLT object");
104  book1D(iBooker, "hltEta", "eta", ";#eta of HLT object");
105  book1D(iBooker, "hltPhi", "phi", ";#phi of HLT object");
106  book1D(iBooker, "resolutionEta", "resolutionEta", ";#eta^{reco}-#eta^{HLT};");
107  book1D(iBooker, "resolutionPhi", "resolutionPhi", ";#phi^{reco}-#phi^{HLT};");
108  }
109  book1D(iBooker, "deltaR", "deltaR", ";#Deltar(reco, HLT);");
110 
111  book1D(iBooker, "resolutionPt", "resolutionRel", ";(p_{T}^{reco}-p_{T}^{HLT})/|p_{T}^{reco}|;");
112 
113  for (const auto& suffix : EFFICIENCY_SUFFIXES) {
114  if (isLastFilter_)
115  iBooker.setCurrentFolder(baseDir + pathSansSuffix);
116  else
117  iBooker.setCurrentFolder(baseDir + pathSansSuffix + "/" + moduleLabel_);
118 
119  book1D(iBooker, "efficiencyEta_" + suffix, "eta", ";#eta;");
120  book1D(iBooker, "efficiencyPhi_" + suffix, "phi", ";#phi;");
121  book1D(iBooker, "efficiencyTurnOn_" + suffix, "pt", ";p_{T};");
122  book1D(iBooker, "efficiencyVertex_" + suffix, "NVertex", ";NVertex;");
123  book1D(iBooker, "efficiencyDeltaR_" + suffix, "deltaR2", ";#Delta R;");
124 
125  book2D(iBooker, "efficiencyPhiVsEta_" + suffix, "etaCoarse", "phiCoarse", ";#eta;#phi");
126 
127  auto MRbaseDir = boost::replace_all_copy<string>(baseDir, "HLT/Muon", "HLT/Muon/MR");
128  if (isLastFilter_)
129  iBooker.setCurrentFolder(MRbaseDir + pathSansSuffix);
130  else
131  iBooker.setCurrentFolder(MRbaseDir + pathSansSuffix + "/" + moduleLabel_);
132 
133  book2D(iBooker, "MR_efficiencyPhiVsEta_" + suffix, "etaCoarse", "phiHEP17", ";#eta;#phi");
134 
135  if (isLastFilter_)
136  iBooker.setCurrentFolder(baseDir + pathSansSuffix);
137  else
138  iBooker.setCurrentFolder(baseDir + pathSansSuffix + "/" + moduleLabel_);
139 
140  if (!isLastFilter_)
141  continue; //this will be plotted only for the last filter
142 
143  book1D(iBooker, "efficiencyD0_" + suffix, "d0", ";d0;");
144  book1D(iBooker, "efficiencyZ0_" + suffix, "z0", ";z0;");
145  book1D(iBooker, "efficiencyCharge_" + suffix, "charge", ";charge;");
146 
147  book1D(iBooker, "fakerateEta_" + suffix, "eta", ";#eta;");
148  book1D(iBooker, "fakerateVertex_" + suffix, "NVertex", ";NVertex;");
149  book1D(iBooker, "fakeratePhi_" + suffix, "phi", ";#phi;");
150  book1D(iBooker, "fakerateTurnOn_" + suffix, "pt", ";p_{T};");
151 
152  book1D(iBooker, "massVsEtaZ_" + suffix, "etaCoarse", ";#eta");
153  book1D(iBooker, "massVsEtaJpsi_" + suffix, "etaCoarse", ";#eta");
154  book1D(iBooker, "massVsPtZ_" + suffix, "ptCoarse", ";p_{T}");
155  book1D(iBooker, "massVsPtJpsi_" + suffix, "ptCoarse", ";p_{T}");
156  book1D(iBooker, "massVsVertexZ_" + suffix, "NVertex", ";NVertex");
157  book1D(iBooker, "massVsVertexJpsi_" + suffix, "NVertex", ";NVertex");
158  book1D(iBooker, "massVsDZZ_" + suffix, "z0", ";z0;");
159 
160  if (!requiredTriggers_.empty()) {
161  book1D(iBooker, "Refefficiency_Eta_Mu1_" + suffix, "etaCoarse", ";#eta;");
162  book1D(iBooker, "Refefficiency_Eta_Mu2_" + suffix, "etaCoarse", ";#eta;");
163  book1D(iBooker, "Refefficiency_TurnOn_Mu1_" + suffix, "ptCoarse", ";p_{T};");
164  book1D(iBooker, "Refefficiency_TurnOn_Mu2_" + suffix, "ptCoarse", ";p_{T};");
165  book1D(iBooker, "Refefficiency_Vertex_" + suffix, "NVertex", ";NVertex;");
166  book1D(iBooker, "Refefficiency_DZ_Mu_" + suffix, "z0", ";z0;");
167 
168  book2D(iBooker, "Refefficiency_Eta_" + suffix, "etaCoarse", "etaCoarse", ";#eta;#eta");
169  book2D(iBooker, "Refefficiency_Pt_" + suffix, "ptCoarse", "ptCoarse", ";p_{T};p_{T}");
170  book1D(iBooker, "Refefficiency_DZ_Vertex_" + suffix, "NVertex", ";NVertex;");
171  book1D(iBooker, "Ref_SS_pt1_" + suffix, "ptCoarse", ";p_{T}");
172  book1D(iBooker, "Ref_SS_pt2_" + suffix, "ptCoarse", ";p_{T}");
173  book1D(iBooker, "Ref_SS_eta1_" + suffix, "etaCoarse", ";#eta;");
174  book1D(iBooker, "Ref_SS_eta2_" + suffix, "etaCoarse", ";#eta;");
175  // book1D(iBooker, "Refefficiency_DZ_Mu2_" + suffix, "z0", ";z0;");
176  }
177  // string MRbaseDir = boost::replace_all_copy<string>(baseDir, "HLT/Muon","HLT/Muon/MR");
178  iBooker.setCurrentFolder(MRbaseDir + pathSansSuffix + "/");
179 
180  if (!requiredTriggers_.empty()) {
181  book1D(iBooker, "MR_Refefficiency_TurnOn_Mu1_" + suffix, "pt", ";p_{T};");
182  book1D(iBooker, "MR_Refefficiency_TurnOn_Mu2_" + suffix, "pt", ";p_{T};");
183  book1D(iBooker, "MR_Refefficiency_Vertex_" + suffix, "NVertexFine", ";NVertex;");
184  book1D(iBooker, "MR_Refefficiency_DZ_Mu_" + suffix, "z0Fine", ";z0;");
185  // book1D(iBooker, "MR_Refefficiency_DZ_Mu2_" + suffix, "z0Fine", ";z0;");
186  book2D(iBooker, "MR_Refefficiency_Pt_" + suffix, "pt", "pt", ";p_{T};p_{T}");
187  book1D(iBooker, "MR_Refefficiency_DZ_Vertex_" + suffix, "NVertexFine", ";NVertex;");
188  }
189  book1D(iBooker, "MR_massVsPtZ_" + suffix, "pt", ";p_{T}");
190  book1D(iBooker, "MR_massVsPtJpsi_" + suffix, "pt", ";p_{T}");
191  book1D(iBooker, "MR_massVsVertexZ_" + suffix, "NVertex", ";NVertex");
192  book1D(iBooker, "MR_massVsVertexJpsi_" + suffix, "NVertexFine", ";NVertex");
193  book1D(iBooker, "MR_massVsDZZ_" + suffix, "z0Fine", ";z0;");
194  book1D(iBooker, "MR_massVsEtaZ_" + suffix, "etaFine", ";#eta");
195  book1D(iBooker, "MR_massVsEtaJpsi_" + suffix, "etaFine", ";#eta");
196  book1D(iBooker, "MR_massVsPhiZ_" + suffix, "phiFine", ";#phi");
197  book1D(iBooker, "MR_massVsPhiJpsi_" + suffix, "phiFine", ";#phi");
198  }
199 }
200 
201 void HLTMuonMatchAndPlot::endRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {}
202 
206  Handle<TriggerEvent>& triggerSummary,
208  const edm::TriggerNames& trigNames) {
209  /*
210  if(gen != 0) {
211  for(g_part = gen->begin(); g_part != gen->end(); g_part++){
212  if( abs(g_part->pdgId()) == 13) {
213  if(!( g_part->status() ==1 || (g_part->status() ==2 && abs(g_part->pdgId())==5))) continue;
214  bool GenMomExists (true);
215  bool GenGrMomExists(true);
216  if( g_part->pt() < 10.0 ) continue;
217  if( fabs(g_part->eta()) > 2.4 ) continue;
218  int gen_id= g_part->pdgId();
219  const GenParticle* gen_lept = &(*g_part);
220  // get mother of gen_lept
221  const GenParticle* gen_mom = static_cast<const GenParticle*> (gen_lept->mother());
222  if(gen_mom==NULL) GenMomExists=false;
223  int m_id=-999;
224  if(GenMomExists) m_id = gen_mom->pdgId();
225  if(m_id != gen_id || !GenMomExists);
226  else{
227  int id= m_id;
228  while(id == gen_id && GenMomExists){
229  gen_mom = static_cast<const GenParticle*> (gen_mom->mother());
230  if(gen_mom==NULL){
231  GenMomExists=false;
232  }
233  if(GenMomExists) id=gen_mom->pdgId();
234  }
235  }
236  if(GenMomExists) m_id = gen_mom->pdgId();
237  gen_leptsp.push_back(gen_lept);
238  gen_momsp.push_back(gen_mom);
239  }
240  }
241  }
242 
243 
244  std::vector<GenParticle> gen_lepts;
245  for(size_t i = 0; i < gen_leptsp.size(); i++) {
246  gen_lepts.push_back(*gen_leptsp[i]);
247  }
248 
249 
250  */
251 
252  // Select objects based on the configuration.
253  MuonCollection targetMuons =
255  MuonCollection probeMuons =
257  TriggerObjectCollection allTriggerObjects = triggerSummary->getObjects();
258  TriggerObjectCollection hltMuons =
259  selectedTriggerObjects(allTriggerObjects, *triggerSummary, hasTriggerCuts_, triggerSelector_);
260  // Fill plots for HLT muons.
261  if (isLastFilter_) {
262  for (auto& hltMuon : hltMuons) {
263  hists_["hltPt"]->Fill(hltMuon.pt());
264  hists_["hltEta"]->Fill(hltMuon.eta());
265  hists_["hltPhi"]->Fill(hltMuon.phi());
266  }
267  }
268  // Find the best trigger object matches for the targetMuons.
269  vector<size_t> matches = matchByDeltaR(targetMuons, hltMuons, plotCuts_[triggerLevel_ + "DeltaR"]);
270 
271  // Fill plots for matched muons.
272  bool pairalreadyconsidered = false;
273  for (size_t i = 0; i < targetMuons.size(); i++) {
274  Muon& muon = targetMuons[i];
275 
276  // Fill plots which are not efficiencies.
277  if (matches[i] < targetMuons.size()) {
278  TriggerObject& hltMuon = hltMuons[matches[i]];
279  double ptRes = (muon.pt() - hltMuon.pt()) / muon.pt();
280  hists_["resolutionPt"]->Fill(ptRes);
281  hists_["deltaR"]->Fill(deltaR(muon, hltMuon));
282 
283  if (isLastFilter_) {
284  double etaRes = muon.eta() - hltMuon.eta();
285  double phiRes = muon.phi() - hltMuon.phi();
286  hists_["resolutionEta"]->Fill(etaRes);
287  hists_["resolutionPhi"]->Fill(phiRes);
288  }
289  }
290 
291  // Fill numerators and denominators for efficiency plots.
292  for (const auto& suffix : EFFICIENCY_SUFFIXES) {
293  // If no match was found, then the numerator plots don't get filled.
294  if (suffix == "numer" && matches[i] >= targetMuons.size())
295  continue;
296 
297  if (muon.pt() > cutMinPt_) {
298  hists_["efficiencyEta_" + suffix]->Fill(muon.eta());
299  hists_["efficiencyPhiVsEta_" + suffix]->Fill(muon.eta(), muon.phi());
300  hists_["MR_efficiencyPhiVsEta_" + suffix]->Fill(muon.eta(), muon.phi());
301  }
302 
303  if (fabs(muon.eta()) < plotCuts_["maxEta"]) {
304  hists_["efficiencyTurnOn_" + suffix]->Fill(muon.pt());
305  }
306 
307  if (muon.pt() > cutMinPt_ && fabs(muon.eta()) < plotCuts_["maxEta"]) {
308  const Track* track = nullptr;
309  if (muon.isTrackerMuon())
310  track = &*muon.innerTrack();
311  else if (muon.isStandAloneMuon())
312  track = &*muon.outerTrack();
313  if (track) {
314  hists_["efficiencyVertex_" + suffix]->Fill(vertices->size());
315  hists_["efficiencyPhi_" + suffix]->Fill(muon.phi());
316 
317  if (isLastFilter_) {
318  double d0 = track->dxy(beamSpot->position());
319  double z0 = track->dz(beamSpot->position());
320  hists_["efficiencyD0_" + suffix]->Fill(d0);
321  hists_["efficiencyZ0_" + suffix]->Fill(z0);
322  hists_["efficiencyCharge_" + suffix]->Fill(muon.charge());
323  }
324  }
325  }
326  } // finish loop numerator / denominator...
327 
328  if (!isLastFilter_)
329  continue;
330  // Fill plots for tag and probe
331  // Muon cannot be a tag because doesn't match an hlt muon
332  if (matches[i] >= targetMuons.size())
333  continue;
334  for (size_t k = 0; k < targetMuons.size(); k++) {
335  if (k == i)
336  continue;
337  Muon& theProbe = targetMuons[k];
338  if (muon.charge() != theProbe.charge() && !pairalreadyconsidered) {
339  double mass = (muon.p4() + theProbe.p4()).M();
340 
341  if (mass > 60 && mass < 120) {
342  if (muon.pt() < targetptCutZ_)
343  continue;
344  hists_["massVsPtZ_denom"]->Fill(theProbe.pt());
345  hists_["massVsEtaZ_denom"]->Fill(theProbe.eta());
346  if (theProbe.pt() > cutMinPt_) {
347  hists_["MR_massVsEtaZ_denom"]->Fill(theProbe.eta());
348  hists_["MR_massVsPhiZ_denom"]->Fill(theProbe.phi());
349  hists_["MR_massVsPtZ_denom"]->Fill(theProbe.pt());
350  hists_["massVsVertexZ_denom"]->Fill(vertices->size());
351  hists_["MR_massVsVertexZ_denom"]->Fill(vertices->size());
352  }
353  const Track* track = nullptr;
354  if (theProbe.isTrackerMuon())
355  track = &*theProbe.innerTrack();
356  else if (theProbe.isStandAloneMuon())
357  track = &*theProbe.outerTrack();
358  if (track) {
359  hists_["massVsDZZ_denom"]->Fill(track->dz(beamSpot->position()));
360  hists_["MR_massVsDZZ_denom"]->Fill(track->dz(beamSpot->position()));
361  }
362  hists_["efficiencyDeltaR_denom"]->Fill(deltaR(theProbe, muon));
363  if (matches[k] < targetMuons.size()) {
364  hists_["massVsPtZ_numer"]->Fill(theProbe.pt());
365  hists_["MR_massVsPtZ_numer"]->Fill(theProbe.pt());
366  if (theProbe.pt() > cutMinPt_) {
367  hists_["MR_massVsPhiZ_numer"]->Fill(theProbe.phi());
368  hists_["massVsEtaZ_numer"]->Fill(theProbe.eta());
369  hists_["MR_massVsEtaZ_numer"]->Fill(theProbe.eta());
370  hists_["massVsVertexZ_numer"]->Fill(vertices->size());
371  hists_["MR_massVsVertexZ_numer"]->Fill(vertices->size());
372  }
373  if (track) {
374  hists_["massVsDZZ_numer"]->Fill(track->dz(beamSpot->position()));
375  hists_["MR_massVsDZZ_numer"]->Fill(track->dz(beamSpot->position()));
376  }
377  hists_["efficiencyDeltaR_numer"]->Fill(deltaR(theProbe, muon));
378  }
379  pairalreadyconsidered = true;
380  }
381  if (mass > 1 && mass < 4) {
382  if (muon.pt() < targetptCutJpsi_)
383  continue;
384  hists_["massVsEtaJpsi_denom"]->Fill(theProbe.eta());
385  hists_["MR_massVsEtaJpsi_denom"]->Fill(theProbe.eta());
386  hists_["massVsPtJpsi_denom"]->Fill(theProbe.pt());
387  hists_["MR_massVsPtJpsi_denom"]->Fill(theProbe.pt());
388  hists_["massVsVertexJpsi_denom"]->Fill(vertices->size());
389  hists_["MR_massVsVertexJpsi_denom"]->Fill(vertices->size());
390  if (matches[k] < targetMuons.size()) {
391  hists_["massVsEtaJpsi_numer"]->Fill(theProbe.eta());
392  hists_["MR_massVsEtaJpsi_numer"]->Fill(theProbe.eta());
393  hists_["massVsPtJpsi_numer"]->Fill(theProbe.pt());
394  hists_["MR_massVsPtJpsi_numer"]->Fill(theProbe.pt());
395  hists_["massVsVertexJpsi_numer"]->Fill(vertices->size());
396  hists_["MR_massVsVertexJpsi_numer"]->Fill(vertices->size());
397  }
398  pairalreadyconsidered = true;
399  }
400  }
401  } // End loop over denominator and numerator.
402  } // End loop over targetMuons.
403 
404  // fill eff histograms for reference trigger method
405  // Denominator: events passing reference trigger and two target muons
406  // Numerator: events in the denominator with two target muons
407  // matched to hlt muons
408  if (!isLastFilter_)
409  return;
410  unsigned int numTriggers = trigNames.size();
411  bool passTrigger = false;
412  if (requiredTriggers_.empty())
413  passTrigger = true;
414  for (auto const& requiredTrigger : requiredTriggers_) {
415  for (unsigned int hltIndex = 0; hltIndex < numTriggers; ++hltIndex) {
416  passTrigger = (trigNames.triggerName(hltIndex).find(requiredTrigger) != std::string::npos &&
417  triggerResults->wasrun(hltIndex) && triggerResults->accept(hltIndex));
418  if (passTrigger)
419  break;
420  }
421  }
422 
423  int nMatched = 0;
424  for (unsigned long matche : matches) {
425  if (matche < targetMuons.size())
426  nMatched++;
427  }
428  if (!requiredTriggers_.empty() && targetMuons.size() > 1 && passTrigger) {
429  // denominator:
430  hists_["Refefficiency_Eta_Mu1_denom"]->Fill(targetMuons.at(0).eta());
431  hists_["Refefficiency_Eta_Mu2_denom"]->Fill(targetMuons.at(1).eta());
432  hists_["Refefficiency_TurnOn_Mu1_denom"]->Fill(targetMuons.at(0).pt());
433  hists_["MR_Refefficiency_TurnOn_Mu1_denom"]->Fill(targetMuons.at(0).pt());
434  hists_["Refefficiency_TurnOn_Mu2_denom"]->Fill(targetMuons.at(1).pt());
435  hists_["MR_Refefficiency_TurnOn_Mu2_denom"]->Fill(targetMuons.at(1).pt());
436  hists_["Refefficiency_Vertex_denom"]->Fill(vertices->size());
437  hists_["MR_Refefficiency_Vertex_denom"]->Fill(vertices->size());
438  hists_["MR_Refefficiency_Pt_denom"]->Fill(targetMuons.at(0).pt(), targetMuons.at(1).pt());
439  hists_["Refefficiency_Pt_denom"]->Fill(targetMuons.at(0).pt(), targetMuons.at(1).pt());
440  hists_["Refefficiency_Eta_denom"]->Fill(targetMuons.at(0).eta(), targetMuons.at(1).eta());
441 
442  // if (track0){
443  // hists_["Refefficiency_DZ_Mu1_denom"]->Fill(track0->dz(beamSpot->position()));
444  // hists_["MR_Refefficiency_DZ_Mu1_denom"]->Fill(track0->dz(beamSpot->position()));
445  // }
446 
447  // if (track1){
448  // hists_["Refefficiency_DZ_Mu2_denom"]->Fill(track1->dz(beamSpot->position()));
449  // hists_["MR_Refefficiency_DZ_Mu2_denom"]->Fill(track1->dz(beamSpot->position()));
450  // }
451 
452  // numerator:
453  if (nMatched > 1) {
454  hists_["Refefficiency_Eta_Mu1_numer"]->Fill(targetMuons.at(0).eta());
455  hists_["Refefficiency_Eta_Mu2_numer"]->Fill(targetMuons.at(1).eta());
456  hists_["Refefficiency_TurnOn_Mu1_numer"]->Fill(targetMuons.at(0).pt());
457  hists_["MR_Refefficiency_TurnOn_Mu1_numer"]->Fill(targetMuons.at(0).pt());
458  hists_["Refefficiency_TurnOn_Mu2_numer"]->Fill(targetMuons.at(1).pt());
459  hists_["MR_Refefficiency_TurnOn_Mu2_numer"]->Fill(targetMuons.at(1).pt());
460  hists_["Refefficiency_Vertex_numer"]->Fill(vertices->size());
461  hists_["MR_Refefficiency_Vertex_numer"]->Fill(vertices->size());
462  hists_["MR_Refefficiency_Pt_numer"]->Fill(targetMuons.at(0).pt(), targetMuons.at(1).pt());
463  hists_["Refefficiency_Pt_numer"]->Fill(targetMuons.at(0).pt(), targetMuons.at(1).pt());
464  hists_["Refefficiency_Eta_numer"]->Fill(targetMuons.at(0).eta(), targetMuons.at(1).eta());
465 
466  // if (track0){
467  // hists_["Refefficiency_DZ_Mu1_numer"]->Fill(track0->dz(beamSpot->position()));
468  // hists_["MR_Refefficiency_DZ_Mu1_numer"]->Fill(track0->dz(beamSpot->position()));
469  // }
470  // if (track1){
471  // hists_["Refefficiency_DZ_Mu2_numer"]->Fill(track1->dz(beamSpot->position()));
472  // hists_["MR_Refefficiency_DZ_Mu2_numer"]->Fill(track1->dz(beamSpot->position()));
473  // }
474  }
475  }
476 
477  string nonSameSignPath = hltPath_;
478  bool ssPath = false;
479  if (nonSameSignPath.rfind("_SameSign") < nonSameSignPath.length()) {
480  ssPath = true;
481  nonSameSignPath = boost::replace_all_copy<string>(nonSameSignPath, "_SameSign", "");
482  nonSameSignPath = nonSameSignPath.substr(0, nonSameSignPath.rfind("_v") + 2);
483  }
484  bool passTriggerSS = false;
485  if (ssPath) {
486  for (unsigned int hltIndex = 0; hltIndex < numTriggers; ++hltIndex) {
487  passTriggerSS =
488  passTriggerSS || (trigNames.triggerName(hltIndex).substr(0, nonSameSignPath.size()) == nonSameSignPath &&
489  triggerResults->wasrun(hltIndex) && triggerResults->accept(hltIndex));
490  }
491 
492  if (ssPath && targetMuons.size() > 1 && passTriggerSS) {
493  if (targetMuons[0].charge() * targetMuons[1].charge() > 0) {
494  hists_["Ref_SS_pt1_denom"]->Fill(targetMuons[0].pt());
495  hists_["Ref_SS_pt2_denom"]->Fill(targetMuons[1].pt());
496  hists_["Ref_SS_eta1_denom"]->Fill(targetMuons[0].eta());
497  hists_["Ref_SS_eta2_denom"]->Fill(targetMuons[1].eta());
498  if (nMatched > 1) {
499  hists_["Ref_SS_pt1_numer"]->Fill(targetMuons[0].pt());
500  hists_["Ref_SS_pt2_numer"]->Fill(targetMuons[1].pt());
501  hists_["Ref_SS_eta1_numer"]->Fill(targetMuons[0].eta());
502  hists_["Ref_SS_eta2_numer"]->Fill(targetMuons[1].eta());
503  }
504  }
505  }
506  }
507 
508  string nonDZPath = hltPath_;
509  bool dzPath = false;
510  if (nonDZPath.rfind("_DZ") < nonDZPath.length()) {
511  dzPath = true;
512  nonDZPath = boost::replace_all_copy<string>(nonDZPath, "_DZ", "");
513  nonDZPath = nonDZPath.substr(0, nonDZPath.rfind("_v") + 2);
514  }
515  bool passTriggerDZ = false;
516  if (dzPath) {
517  for (unsigned int hltIndex = 0; hltIndex < numTriggers; ++hltIndex) {
518  passTriggerDZ = passTriggerDZ || (trigNames.triggerName(hltIndex).find(nonDZPath) != std::string::npos &&
519  triggerResults->wasrun(hltIndex) && triggerResults->accept(hltIndex));
520  }
521  }
522 
523  if (dzPath && targetMuons.size() > 1 && passTriggerDZ) {
524  const Track* track0 = nullptr;
525  const Track* track1 = nullptr;
526  if (targetMuons.at(0).isTrackerMuon())
527  track0 = &*targetMuons.at(0).innerTrack();
528  else if (targetMuons.at(0).isTrackerMuon())
529  track0 = &*targetMuons.at(0).outerTrack();
530  if (targetMuons.at(1).isTrackerMuon())
531  track1 = &*targetMuons.at(1).innerTrack();
532  else if (targetMuons.at(1).isTrackerMuon())
533  track1 = &*targetMuons.at(1).outerTrack();
534 
535  if (track0 && track1) {
536  hists_["Refefficiency_DZ_Mu_denom"]->Fill(track0->dz(beamSpot->position()) - track1->dz(beamSpot->position()));
537  hists_["MR_Refefficiency_DZ_Mu_denom"]->Fill(track0->dz(beamSpot->position()) - track1->dz(beamSpot->position()));
538  }
539  hists_["Refefficiency_DZ_Vertex_denom"]->Fill(vertices->size());
540  hists_["MR_Refefficiency_DZ_Vertex_denom"]->Fill(vertices->size());
541  if (nMatched > 1) {
542  if (track0 && track1) {
543  hists_["Refefficiency_DZ_Mu_numer"]->Fill(track0->dz(beamSpot->position()) - track1->dz(beamSpot->position()));
544  hists_["MR_Refefficiency_DZ_Mu_numer"]->Fill(track0->dz(beamSpot->position()) -
545  track1->dz(beamSpot->position()));
546  hists_["Refefficiency_DZ_Vertex_numer"]->Fill(vertices->size());
547  hists_["MR_Refefficiency_DZ_Vertex_numer"]->Fill(vertices->size());
548  }
549  }
550  }
551 
552  // Plot fake rates (efficiency for HLT objects to not get matched to RECO).
553  vector<size_t> hltMatches = matchByDeltaR(hltMuons, targetMuons, plotCuts_[triggerLevel_ + "DeltaR"]);
554  for (size_t i = 0; i < hltMuons.size(); i++) {
555  TriggerObject& hltMuon = hltMuons[i];
556  bool isFake = hltMatches[i] > hltMuons.size();
557  for (const auto& suffix : EFFICIENCY_SUFFIXES) {
558  // If match is found, then numerator plots should not get filled
559  if (suffix == "numer" && !isFake)
560  continue;
561  hists_["fakerateVertex_" + suffix]->Fill(vertices->size());
562  hists_["fakerateEta_" + suffix]->Fill(hltMuon.eta());
563  hists_["fakeratePhi_" + suffix]->Fill(hltMuon.phi());
564  hists_["fakerateTurnOn_" + suffix]->Fill(hltMuon.pt());
565  } // End loop over numerator and denominator.
566  } // End loop over hltMuons.
567 
568 } // End analyze() method.
569 
570 // Method to fill binning parameters from a vector of doubles.
571 void HLTMuonMatchAndPlot::fillEdges(size_t& nBins, float*& edges, const vector<double>& binning) {
572  if (binning.size() < 3) {
573  LogWarning("HLTMuonVal") << "Invalid binning parameters!";
574  return;
575  }
576 
577  // Fixed-width binning.
578  if (binning.size() == 3) {
579  nBins = binning[0];
580  edges = new float[nBins + 1];
581  const double min = binning[1];
582  const double binwidth = (binning[2] - binning[1]) / nBins;
583  for (size_t i = 0; i <= nBins; i++)
584  edges[i] = min + (binwidth * i);
585  }
586 
587  // Variable-width binning.
588  else {
589  nBins = binning.size() - 1;
590  edges = new float[nBins + 1];
591  for (size_t i = 0; i <= nBins; i++)
592  edges[i] = binning[i];
593  }
594 }
595 
596 // This is an unorthodox method of getting parameters, but cleaner in my mind
597 // It looks for parameter 'target' in the pset, and assumes that all entries
598 // have the same class (T), filling the values into a std::map.
599 template <class T>
600 void HLTMuonMatchAndPlot::fillMapFromPSet(map<string, T>& m, const ParameterSet& pset, const string& target) {
601  // Get the ParameterSet with name 'target' from 'pset'
602  ParameterSet targetPset;
603  if (pset.existsAs<ParameterSet>(target, true)) // target is tracked
604  targetPset = pset.getParameterSet(target);
605  else if (pset.existsAs<ParameterSet>(target, false)) // target is untracked
606  targetPset = pset.getUntrackedParameterSet(target);
607 
608  // Get the parameter names from targetPset
609  vector<string> names = targetPset.getParameterNames();
610  vector<string>::const_iterator iter;
611 
612  for (iter = names.begin(); iter != names.end(); ++iter) {
613  if (targetPset.existsAs<T>(*iter, true)) // target is tracked
614  m[*iter] = targetPset.getParameter<T>(*iter);
615  else if (targetPset.existsAs<T>(*iter, false)) // target is untracked
616  m[*iter] = targetPset.getUntrackedParameter<T>(*iter);
617  }
618 }
619 
620 // A generic method to find the best deltaR matches from 2 collections.
621 template <class T1, class T2>
622 vector<size_t> HLTMuonMatchAndPlot::matchByDeltaR(const vector<T1>& collection1,
623  const vector<T2>& collection2,
624  const double maxDeltaR) {
625  const size_t n1 = collection1.size();
626  const size_t n2 = collection2.size();
627 
628  vector<size_t> result(n1, -1);
629  vector<vector<double> > deltaRMatrix(n1, vector<double>(n2, NOMATCH));
630 
631  for (size_t i = 0; i < n1; i++)
632  for (size_t j = 0; j < n2; j++) {
633  deltaRMatrix[i][j] = deltaR(collection1[i], collection2[j]);
634  }
635 
636  // Run through the matrix n1 times to make sure we've found all matches.
637  for (size_t k = 0; k < n1; k++) {
638  size_t i_min = -1;
639  size_t j_min = -1;
640  double minDeltaR = maxDeltaR;
641  // find the smallest deltaR
642  for (size_t i = 0; i < n1; i++)
643  for (size_t j = 0; j < n2; j++)
644  if (deltaRMatrix[i][j] < minDeltaR) {
645  i_min = i;
646  j_min = j;
647  minDeltaR = deltaRMatrix[i][j];
648  }
649  // If a match has been made, save it and make those candidates unavailable.
650  if (minDeltaR < maxDeltaR) {
651  result[i_min] = j_min;
652  deltaRMatrix[i_min] = vector<double>(n2, NOMATCH);
653  for (size_t i = 0; i < n1; i++)
654  deltaRMatrix[i][j_min] = NOMATCH;
655  }
656  }
657 
658  return result;
659 }
660 
662  const BeamSpot& beamSpot,
663  bool hasRecoCuts,
664  const StringCutObjectSelector<reco::Muon>& selector,
665  double d0Cut,
666  double z0Cut) {
667  // If there is no selector (recoCuts does not exists), return an empty collection.
668  if (!hasRecoCuts)
669  return MuonCollection();
670 
671  MuonCollection reducedMuons;
672  for (auto const& mu : allMuons) {
673  const Track* track = nullptr;
674  if (mu.isTrackerMuon())
675  track = &*mu.innerTrack();
676  else if (mu.isStandAloneMuon())
677  track = &*mu.outerTrack();
678  if (track && selector(mu) && fabs(track->dxy(beamSpot.position())) < d0Cut &&
679  fabs(track->dz(beamSpot.position())) < z0Cut)
680  reducedMuons.push_back(mu);
681  }
682 
683  return reducedMuons;
684 }
685 
687  const TriggerObjectCollection& triggerObjects,
688  const TriggerEvent& triggerSummary,
689  bool hasTriggerCuts,
690  const StringCutObjectSelector<TriggerObject>& triggerSelector) {
691  if (!hasTriggerCuts)
692  return TriggerObjectCollection();
693 
694  InputTag filterTag(moduleLabel_, "", hltProcessName_);
695  size_t filterIndex = triggerSummary.filterIndex(filterTag);
696 
697  TriggerObjectCollection selectedObjects;
698 
699  if (filterIndex < triggerSummary.sizeFilters()) {
700  const Keys& keys = triggerSummary.filterKeys(filterIndex);
701  for (unsigned short key : keys) {
702  TriggerObject foundObject = triggerObjects[key];
703  if (triggerSelector(foundObject))
704  selectedObjects.push_back(foundObject);
705  }
706  }
707 
708  return selectedObjects;
709 }
710 
711 void HLTMuonMatchAndPlot::book1D(DQMStore::IBooker& iBooker, string name, const string& binningType, string title) {
712  /* Properly delete the array of floats that has been allocated on
713  * the heap by fillEdges. Avoid multiple copies and internal ROOT
714  * clones by simply creating the histograms directly in the DQMStore
715  * using the appropriate book1D method to handle the variable bins
716  * case. */
717 
718  size_t nBins;
719  float* edges = nullptr;
720  fillEdges(nBins, edges, binParams_[binningType]);
721  hists_[name] = iBooker.book1D(name, title, nBins, edges);
722  if (hists_[name])
723  if (hists_[name]->getTH1F()->GetSumw2N())
724  hists_[name]->enableSumw2();
725 
726  if (edges)
727  delete[] edges;
728 }
729 
731  const string& name,
732  const string& binningTypeX,
733  const string& binningTypeY,
734  const string& title) {
735  /* Properly delete the arrays of floats that have been allocated on
736  * the heap by fillEdges. Avoid multiple copies and internal ROOT
737  * clones by simply creating the histograms directly in the DQMStore
738  * using the appropriate book2D method to handle the variable bins
739  * case. */
740 
741  size_t nBinsX;
742  float* edgesX = nullptr;
743  fillEdges(nBinsX, edgesX, binParams_[binningTypeX]);
744 
745  size_t nBinsY;
746  float* edgesY = nullptr;
747  fillEdges(nBinsY, edgesY, binParams_[binningTypeY]);
748 
749  hists_[name] = iBooker.book2D(name.c_str(), title.c_str(), nBinsX, edgesX, nBinsY, edgesY);
750  if (hists_[name])
751  if (hists_[name]->getTH2F()->GetSumw2N())
752  hists_[name]->enableSumw2();
753 
754  if (edgesX)
755  delete[] edgesX;
756  if (edgesY)
757  delete[] edgesY;
758 }
std::size_t size() const
Definition: TriggerNames.cc:59
constexpr int32_t ceil(float num)
T getUntrackedParameter(std::string const &, T const &) const
reco::MuonCollection selectedMuons(const reco::MuonCollection &, const reco::BeamSpot &, bool, const StringCutObjectSelector< reco::Muon > &, double, double)
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:171
StringCutObjectSelector< reco::Muon > probeMuonSelector_
double pt() const final
transverse momentum
The single EDProduct to be saved for each event (AOD case)
Definition: TriggerEvent.h:25
trigger::size_type sizeFilters() const
Definition: TriggerEvent.h:146
const std::string EFFICIENCY_SUFFIXES[2]
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
vector< string > vstring
Definition: ExoticaDQM.cc:8
virtual TrackRef innerTrack() const
Definition: Muon.h:45
float phi() const
Definition: TriggerObject.h:54
ParameterSet const & getParameterSet(ParameterSetID const &id)
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:118
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:132
std::map< std::string, std::vector< double > > binParams_
ParameterSet getUntrackedParameterSet(std::string const &name, ParameterSet const &defaultValue) const
std::map< std::string, MonitorElement * > hists_
float eta() const
Definition: TriggerObject.h:53
const std::string names[nVars_]
bool isTrackerMuon() const override
Definition: Muon.h:300
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
HLTMuonMatchAndPlot(const edm::ParameterSet &, std::string, std::string, bool)
Constructor.
tuple result
Definition: mps_fire.py:311
std::vector< std::string > requiredTriggers_
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
const double NOMATCH
void book2D(DQMStore::IBooker &, const std::string &, const std::string &, const std::string &, const std::string &)
std::vector< size_t > matchByDeltaR(const std::vector< T1 > &, const std::vector< T2 > &, const double maxDeltaR=NOMATCH)
void fillMapFromPSet(std::map< std::string, T > &, const edm::ParameterSet &, const std::string &)
def move
Definition: eostools.py:511
trigger::TriggerObjectCollection selectedTriggerObjects(const trigger::TriggerObjectCollection &, const trigger::TriggerEvent &, bool hasTriggerCuts, const StringCutObjectSelector< trigger::TriggerObject > &triggerSelector)
tuple key
prepare the HTCondor submission files and eventually submit them
tuple allMuons
Definition: allMuons_cfi.py:3
const int mu
Definition: Constants.h:22
T min(T a, T b)
Definition: MathUtil.h:58
static std::string const triggerResults
Definition: EdmProvDump.cc:44
std::vector< std::string > getParameterNames() const
virtual TrackRef outerTrack() const
reference to Track reconstructed in the muon detector only
Definition: Muon.h:48
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:622
void book1D(DQMStore::IBooker &, std::string, const std::string &, std::string)
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:75
static const char *const trigNames[]
Definition: EcalDumpRaw.cc:57
void analyze(edm::Handle< reco::MuonCollection > &, edm::Handle< reco::BeamSpot > &, edm::Handle< reco::VertexCollection > &, edm::Handle< trigger::TriggerEvent > &, edm::Handle< edm::TriggerResults > &, const edm::TriggerNames &)
static constexpr float d0
StringCutObjectSelector< reco::Muon > targetMuonSelector_
ParameterSet const & getParameterSet(std::string const &) const
std::string const & triggerName(unsigned int index) const
Definition: TriggerNames.cc:50
std::vector< size_type > Keys
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:177
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
tuple binning
Definition: pileupCalc.py:163
StringCutObjectSelector< trigger::TriggerObject > triggerSelector_
void endRun(const edm::Run &, const edm::EventSetup &)
const Point & position() const
position
Definition: BeamSpot.h:59
Log< level::Warning, false > LogWarning
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:608
moduleLabel_(iConfig.getParameter< string >("@module_label"))
long double T
double phi() const final
momentum azimuthal angle
std::map< std::string, double > plotCuts_
void fillEdges(size_t &nBins, float *&edges, const std::vector< double > &binning)
Definition: Run.h:45
int charge() const final
electric charge
void beginRun(DQMStore::IBooker &, const edm::Run &, const edm::EventSetup &)
bool isStandAloneMuon() const override
Definition: Muon.h:301
double eta() const final
momentum pseudorapidity