CMS 3D CMS Logo

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