CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HLTMuonMatchAndPlot.cc
Go to the documentation of this file.
1 
10 
13 
23 
24 #include <iostream>
25 
28 
29 using namespace std;
30 using namespace edm;
31 using namespace reco;
32 using namespace trigger;
33 using namespace l1extra;
34 
35 typedef std::vector<std::string> vstring;
36 
37 
40 
43  string hltPath,
44  vector<string> moduleLabels) :
45  hltProcessName_(pset.getParameter<string>("hltProcessName")),
46  destination_(pset.getUntrackedParameter<string>("destination")),
47  requiredTriggers_(pset.getUntrackedParameter<vstring>("requiredTriggers")),
48  targetParams_(pset.getParameterSet("targetParams")),
49  probeParams_(pset.getParameterSet("probeParams")),
50  hltPath_(hltPath),
51  moduleLabels_(moduleLabels),
52  hasTargetRecoCuts(targetParams_.exists("recoCuts")),
53  hasProbeRecoCuts(probeParams_.exists("recoCuts")),
54  targetMuonSelector_(targetParams_.getUntrackedParameter<string>("recoCuts", "")),
55  targetZ0Cut_(targetParams_.getUntrackedParameter<double>("z0Cut",0.)),
56  targetD0Cut_(targetParams_.getUntrackedParameter<double>("d0Cut",0.)),
57  probeMuonSelector_(probeParams_.getUntrackedParameter<string>("recoCuts", "")),
58  probeZ0Cut_(probeParams_.getUntrackedParameter<double>("z0Cut",0.)),
59  probeD0Cut_(probeParams_.getUntrackedParameter<double>("d0Cut",0.))
60 {
61 
62  // Create std::map<string, T> from ParameterSets.
63  fillMapFromPSet(inputTags_, pset, "inputTags");
64  fillMapFromPSet(binParams_, pset, "binParams");
65  fillMapFromPSet(plotCuts_, pset, "plotCuts");
66 
67  // Set HLT process name for TriggerResults and TriggerSummary.
68  InputTag & resTag = inputTags_["triggerResults"];
69  resTag = InputTag(resTag.label(), resTag.instance(), hltProcessName_);
70  InputTag & sumTag = inputTags_["triggerSummary"];
71  sumTag = InputTag(sumTag.label(), sumTag.instance(), hltProcessName_);
72 
73  // Prepare the DQMStore object.
75  dbe_->setVerbose(0);
76 
77  // Get the trigger level.
78  triggerLevel_ = "L3";
79  TPRegexp levelRegexp("L[1-3]");
80  size_t nModules = moduleLabels_.size();
81  TObjArray * levelArray = levelRegexp.MatchS(moduleLabels_[nModules - 1]);
82  if (levelArray->GetEntriesFast() > 0) {
83  triggerLevel_ = ((TObjString *)levelArray->At(0))->GetString();
84  }
85  delete levelArray;
86 
87  // Get the pT cut by parsing the name of the HLT path.
88  cutMinPt_ = 3;
89  TPRegexp ptRegexp("Mu([0-9]*)");
90  TObjArray * objArray = ptRegexp.MatchS(hltPath_);
91  if (objArray->GetEntriesFast() >= 2) {
92  TObjString * ptCutString = (TObjString *)objArray->At(1);
93  cutMinPt_ = atoi(ptCutString->GetString());
94  cutMinPt_ = ceil(cutMinPt_ * plotCuts_["minPtFactor"]);
95  }
96  delete objArray;
97 }
98 
100  const edm::EventSetup& iSetup)
101 {
102 
103  TPRegexp suffixPtCut("Mu[0-9]+$");
104 
105  string baseDir = destination_;
106  if (baseDir[baseDir.size() - 1] != '/') baseDir += '/';
107  string pathSansSuffix = hltPath_;
108  if (hltPath_.rfind("_v") < hltPath_.length())
109  pathSansSuffix = hltPath_.substr(0, hltPath_.rfind("_v"));
110  dbe_->setCurrentFolder(baseDir + pathSansSuffix);
111 
112  // Form is book1D(name, binningType, title) where 'binningType' is used
113  // to fetch the bin settings from binParams_.
114  book1D("deltaR", "deltaR", ";#Deltar(reco, HLT);");
115  book1D("resolutionEta", "resolution",
116  ";(#eta^{reco}-#eta^{HLT})/|#eta^{reco}|;");
117  book1D("resolutionPhi", "resolution",
118  ";(#phi^{reco}-#phi^{HLT})/|#phi^{reco}|;");
119  book1D("resolutionPt", "resolution",
120  ";(p_{T}^{reco}-p_{T}^{HLT})/|p_{T}^{reco}|;");
121 
122  for (size_t i = 0; i < 2; i++) {
123 
124  string suffix = EFFICIENCY_SUFFIXES[i];
125 
126  book1D("efficiencyEta_" + suffix, "eta", ";#eta;");
127  book1D("efficiencyPhi_" + suffix, "phi", ";#phi;");
128  book1D("efficiencyTurnOn_" + suffix, "pt", ";p_{T};");
129  book1D("efficiencyD0_" + suffix, "d0", ";d0;");
130  book1D("efficiencyZ0_" + suffix, "z0", ";z0;");
131  book1D("efficiencyCharge_" + suffix, "charge", ";charge;");
132  book2D("efficiencyPhiVsEta_" + suffix, "etaCoarse", "phiCoarse",
133  ";#eta;#phi");
134 
135  book1D("fakerateEta_" + suffix, "eta", ";#eta;");
136  book1D("fakeratePhi_" + suffix, "phi", ";#phi;");
137  book1D("fakerateTurnOn_" + suffix, "pt", ";p_{T};");
138 
139  // Charge Flip?
140 
141  // Book histograms for tag and probe
142  if (probeParams_.exists("recoCuts")) {
143  book2D("massVsEta_" + suffix, "zMass", "etaCoarse", ";m_{#mu#mu};#eta");
144  }
145 
146  }
147 
148 }
149 
150 
151 
153  const edm::EventSetup& iSetup)
154 {
155 }
156 
157 
158 
160  const edm::EventSetup& iSetup)
161 
162 {
163 
164  // Get objects from the event.
166  iEvent.getByLabel(inputTags_["recoMuon"], allMuons);
167  Handle<BeamSpot> beamSpot;
168  iEvent.getByLabel(inputTags_["beamSpot"], beamSpot);
169  Handle<TriggerEvent> triggerSummary;
170  iEvent.getByLabel(inputTags_["triggerSummary"], triggerSummary);
171  Handle<TriggerResults> triggerResults;
172  if(!triggerSummary.isValid())
173  {
174  edm::LogError("HLTMuonMatchAndPlot")<<"Missing triggerSummary with label " << inputTags_["triggerSummary"] <<std::endl;
175  return;
176  }
177  iEvent.getByLabel(inputTags_["triggerResults"], triggerResults);
178  if(!triggerResults.isValid())
179  {
180  edm::LogError("HLTMuonMatchAndPlot")<<"Missing triggerResults with label " << inputTags_["triggerResults"] <<std::endl;
181  return;
182  }
183  // Throw out this event if it doesn't pass the required triggers.
184  for (size_t i = 0; i < requiredTriggers_.size(); i++) {
185  unsigned int triggerIndex = triggerResults->find(requiredTriggers_[i]);
186  if (triggerIndex < triggerResults->size() ||
187  !triggerResults->accept(triggerIndex))
188  return;
189  }
190 
191  // Select objects based on the configuration.
194  TriggerObjectCollection allTriggerObjects = triggerSummary->getObjects();
195  TriggerObjectCollection hltMuons =
196  selectedTriggerObjects(allTriggerObjects, * triggerSummary, targetParams_);
197 
198  // Find the best trigger object matches for the targetMuons.
199  vector<size_t> matches = matchByDeltaR(targetMuons, hltMuons,
200  plotCuts_[triggerLevel_ + "DeltaR"]);
201 
202  // Fill plots for matched muons.
203  for (size_t i = 0; i < targetMuons.size(); i++) {
204 
205  Muon & muon = targetMuons[i];
206 
207  // Fill plots which are not efficiencies.
208  if (matches[i] < targetMuons.size()) {
209  TriggerObject & hltMuon = hltMuons[matches[i]];
210  double ptRes = (muon.pt() - hltMuon.pt()) / muon.pt();
211  double etaRes = (muon.eta() - hltMuon.eta()) / fabs(muon.eta());
212  double phiRes = (muon.phi() - hltMuon.phi()) / fabs(muon.phi());
213  hists_["resolutionEta"]->Fill(etaRes);
214  hists_["resolutionPhi"]->Fill(phiRes);
215  hists_["resolutionPt"]->Fill(ptRes);
216  hists_["deltaR"]->Fill(deltaR(muon, hltMuon));
217  }
218 
219  // Fill numerators and denominators for efficiency plots.
220  for (size_t j = 0; j < 2; j++) {
221 
222  string suffix = EFFICIENCY_SUFFIXES[j];
223 
224  // If no match was found, then the numerator plots don't get filled.
225  if (suffix == "numer" && matches[i] >= targetMuons.size()) continue;
226 
227  if (muon.pt() > cutMinPt_) {
228  hists_["efficiencyEta_" + suffix]->Fill(muon.eta());
229  hists_["efficiencyPhiVsEta_" + suffix]->Fill(muon.eta(), muon.phi());
230  }
231 
232  if (fabs(muon.eta()) < plotCuts_["maxEta"]) {
233  hists_["efficiencyTurnOn_" + suffix]->Fill(muon.pt());
234  }
235 
236  if (muon.pt() > cutMinPt_ && fabs(muon.eta()) < plotCuts_["maxEta"]) {
237  const Track * track = 0;
238  if (muon.isTrackerMuon()) track = & * muon.innerTrack();
239  else if (muon.isStandAloneMuon()) track = & * muon.outerTrack();
240  if (track) {
241  double d0 = track->dxy(beamSpot->position());
242  double z0 = track->dz(beamSpot->position());
243  hists_["efficiencyPhi_" + suffix]->Fill(muon.phi());
244  hists_["efficiencyD0_" + suffix]->Fill(d0);
245  hists_["efficiencyZ0_" + suffix]->Fill(z0);
246  hists_["efficiencyCharge_" + suffix]->Fill(muon.charge());
247  }
248  }
249 
250  // Fill plots for tag and probe
251  for (size_t k = 0; k < probeMuons.size(); k++) {
252  Muon & probe = targetMuons[k];
253  if (muon.charge() != probe.charge()) {
254  double mass = (muon.p4() + probe.p4()).M();
255  hists_["massVsEta_" + suffix]->Fill(mass, muon.eta());
256  }
257  }
258 
259  } // End loop over denominator and numerator.
260 
261  } // End loop over targetMuons.
262 
263  // Plot fake rates (efficiency for HLT objects to not get matched to RECO).
264  vector<size_t> hltMatches = matchByDeltaR(hltMuons, targetMuons,
265  plotCuts_[triggerLevel_ + "DeltaR"]);
266  for (size_t i = 0; i < hltMuons.size(); i++) {
267  TriggerObject & hltMuon = hltMuons[i];
268  bool isFake = hltMatches[i] > hltMuons.size();
269  for (size_t j = 0; j < 2; j++) {
270  string suffix = EFFICIENCY_SUFFIXES[j];
271  // If match is found, then numerator plots should not get filled
272  if (suffix == "numer" && ! isFake) continue;
273  hists_["fakerateEta_" + suffix]->Fill(hltMuon.eta());
274  hists_["fakeratePhi_" + suffix]->Fill(hltMuon.phi());
275  hists_["fakerateTurnOn_" + suffix]->Fill(hltMuon.pt());
276  } // End loop over numerator and denominator.
277  } // End loop over hltMuons.
278 
279 
280 } // End analyze() method.
281 
282 
283 
284 // Method to fill binning parameters from a vector of doubles.
285 void
286 HLTMuonMatchAndPlot::fillEdges(size_t & nBins, float * & edges,
287  vector<double> binning) {
288 
289  if (binning.size() < 3) {
290  LogWarning("HLTMuonVal") << "Invalid binning parameters!";
291  return;
292  }
293 
294  // Fixed-width binning.
295  if (binning.size() == 3) {
296  nBins = binning[0];
297  edges = new float[nBins + 1];
298  const double min = binning[1];
299  const double binwidth = (binning[2] - binning[1]) / nBins;
300  for (size_t i = 0; i <= nBins; i++) edges[i] = min + (binwidth * i);
301  }
302 
303  // Variable-width binning.
304  else {
305  nBins = binning.size() - 1;
306  edges = new float[nBins + 1];
307  for (size_t i = 0; i <= nBins; i++) edges[i] = binning[i];
308  }
309 
310 }
311 
312 
313 
314 // This is an unorthodox method of getting parameters, but cleaner in my mind
315 // It looks for parameter 'target' in the pset, and assumes that all entries
316 // have the same class (T), filling the values into a std::map.
317 template <class T>
318 void
320  ParameterSet pset, string target) {
321 
322  // Get the ParameterSet with name 'target' from 'pset'
323  ParameterSet targetPset;
324  if (pset.existsAs<ParameterSet>(target, true)) // target is tracked
325  targetPset = pset.getParameterSet(target);
326  else if (pset.existsAs<ParameterSet>(target, false)) // target is untracked
327  targetPset = pset.getUntrackedParameterSet(target);
328 
329  // Get the parameter names from targetPset
330  vector<string> names = targetPset.getParameterNames();
331  vector<string>::const_iterator iter;
332 
333  for (iter = names.begin(); iter != names.end(); ++iter) {
334  if (targetPset.existsAs<T>(* iter, true)) // target is tracked
335  m[* iter] = targetPset.getParameter<T>(* iter);
336  else if (targetPset.existsAs<T>(* iter, false)) // target is untracked
337  m[* iter] = targetPset.getUntrackedParameter<T>(* iter);
338  }
339 
340 }
341 
342 
343 
344 // A generic method to find the best deltaR matches from 2 collections.
345 template <class T1, class T2>
346 vector<size_t>
347 HLTMuonMatchAndPlot::matchByDeltaR(const vector<T1> & collection1,
348  const vector<T2> & collection2,
349  const double maxDeltaR) {
350 
351  const size_t n1 = collection1.size();
352  const size_t n2 = collection2.size();
353 
354  vector<size_t> result(n1, -1);
355  vector<vector<double> > deltaRMatrix(n1, vector<double>(n2, NOMATCH));
356 
357  for (size_t i = 0; i < n1; i++)
358  for (size_t j = 0; j < n2; j++) {
359  deltaRMatrix[i][j] = deltaR(collection1[i], collection2[j]);
360  }
361 
362  // Run through the matrix n1 times to make sure we've found all matches.
363  for (size_t k = 0; k < n1; k++) {
364  size_t i_min = -1;
365  size_t j_min = -1;
366  double minDeltaR = maxDeltaR;
367  // find the smallest deltaR
368  for (size_t i = 0; i < n1; i++)
369  for (size_t j = 0; j < n2; j++)
370  if (deltaRMatrix[i][j] < minDeltaR) {
371  i_min = i;
372  j_min = j;
373  minDeltaR = deltaRMatrix[i][j];
374  }
375  // If a match has been made, save it and make those candidates unavailable.
376  if (minDeltaR < maxDeltaR) {
377  result[i_min] = j_min;
378  deltaRMatrix[i_min] = vector<double>(n2, NOMATCH);
379  for (size_t i = 0; i < n1; i++)
380  deltaRMatrix[i][j_min] = NOMATCH;
381  }
382  }
383 
384  return result;
385 
386 }
387 
388 
389 
392  const BeamSpot & beamSpot,
393  bool hasRecoCuts,
394  const StringCutObjectSelector<reco::Muon> &selector,
395  double d0Cut, double z0Cut)
396 {
397  // If there is no selector (recoCuts does not exists), return an empty collection.
398  if (!hasRecoCuts)
399  return MuonCollection();
400 
401  MuonCollection reducedMuons(allMuons);
402  MuonCollection::iterator iter = reducedMuons.begin();
403  while (iter != reducedMuons.end()) {
404  const Track * track = 0;
405  if (iter->isTrackerMuon()) track = & * iter->innerTrack();
406  else if (iter->isStandAloneMuon()) track = & * iter->outerTrack();
407  if (track && selector(* iter) &&
408  fabs(track->dxy(beamSpot.position())) < d0Cut &&
409  fabs(track->dz(beamSpot.position())) < z0Cut)
410  ++iter;
411  else reducedMuons.erase(iter);
412  }
413 
414  return reducedMuons;
415 
416 }
417 
418 
419 
422  const TriggerObjectCollection & triggerObjects,
423  const TriggerEvent & triggerSummary,
424  const ParameterSet & pset)
425 {
426 
427  // If pset is empty, return an empty collection.
428  if (!pset.exists("hltCuts"))
429  return TriggerObjectCollection();
430 
432  (pset.getUntrackedParameter<string>("hltCuts"));
433 
434  InputTag filterTag(moduleLabels_[moduleLabels_.size() - 1], "",
436  size_t filterIndex = triggerSummary.filterIndex(filterTag);
437 
438  TriggerObjectCollection selectedObjects;
439 
440  if (filterIndex < triggerSummary.sizeFilters()) {
441  const Keys &keys = triggerSummary.filterKeys(filterIndex);
442  for (size_t j = 0; j < keys.size(); j++ ){
443  TriggerObject foundObject = triggerObjects[keys[j]];
444  if (selector(foundObject))
445  selectedObjects.push_back(foundObject);
446  }
447  }
448 
449  return selectedObjects;
450 
451 }
452 
453 
454 
455 void
456 HLTMuonMatchAndPlot::book1D(string name, string binningType, string title) {
457 
458  size_t nBins;
459  float * edges;
460  fillEdges(nBins, edges, binParams_[binningType]);
461 
462  TH1F * h = new TH1F(name.c_str(), title.c_str(), nBins, edges);
463  h->Sumw2();
464  hists_[name] = dbe_->book1D(name, h);
465  delete h;
466 
467 }
468 
469 
470 
471 void
472 HLTMuonMatchAndPlot::book2D(string name, string binningTypeX,
473  string binningTypeY, string title) {
474 
475  size_t nBinsX;
476  float * edgesX;
477  fillEdges(nBinsX, edgesX, binParams_[binningTypeX]);
478 
479  size_t nBinsY;
480  float * edgesY;
481  fillEdges(nBinsY, edgesY, binParams_[binningTypeY]);
482 
483  TH2F * h = new TH2F(name.c_str(), title.c_str(),
484  nBinsX, edgesX, nBinsY, edgesY);
485  h->Sumw2();
486  hists_[name] = dbe_->book2D(name, h);
487  delete h;
488 
489 }
490 
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)
int i
Definition: DBlmapReader.cc:9
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:181
StringCutObjectSelector< reco::Muon > probeMuonSelector_
The single EDProduct to be saved for each event (AOD case)
Definition: TriggerEvent.h:27
trigger::size_type sizeFilters() const
Definition: TriggerEvent.h:130
const std::string EFFICIENCY_SUFFIXES[2]
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:514
tuple d0
Definition: debug_cff.py:3
virtual TrackRef innerTrack() const
Definition: Muon.h:38
float phi() const
Definition: TriggerObject.h:60
edm::ParameterSet targetParams_
bool isTrackerMuon() const
Definition: Muon.h:149
ParameterSet const & getParameterSet(ParameterSetID const &id)
std::vector< std::string > moduleLabels_
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:106
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:118
bool exists(std::string const &parameterName) const
checks if a parameter exists
void fillEdges(size_t &nBins, float *&edges, std::vector< double > binning)
void book1D(std::string, std::string, std::string)
std::map< std::string, std::vector< double > > binParams_
#define min(a, b)
Definition: mlp_lapack.h:161
std::map< std::string, MonitorElement * > hists_
float eta() const
Definition: TriggerObject.h:59
bool isStandAloneMuon() const
Definition: Muon.h:150
dictionary edges
virtual double eta() const
momentum pseudorapidity
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
std::vector< std::string > requiredTriggers_
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:24
const double NOMATCH
int iEvent
Definition: GenABIO.cc:243
void analyze(const edm::Event &, const edm::EventSetup &)
void beginRun(const edm::Run &, const edm::EventSetup &)
std::vector< size_t > matchByDeltaR(const std::vector< T1 > &, const std::vector< T2 > &, const double maxDeltaR=NOMATCH)
HLTMuonMatchAndPlot(const edm::ParameterSet &, std::string, std::vector< std::string >)
Constructor.
ParameterSet const & getUntrackedParameterSet(std::string const &name, ParameterSet const &defaultValue) const
tuple result
Definition: query.py:137
virtual int charge() const
electric charge
int j
Definition: DBlmapReader.cc:9
tuple allMuons
Definition: allMuons_cfi.py:3
std::vector< std::string > getParameterNames() const
bool isValid() const
Definition: HandleBase.h:76
void book2D(std::string, std::string, std::string, std::string)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
virtual TrackRef outerTrack() const
reference to Track reconstructed in the muon detector only
Definition: Muon.h:41
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:127
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:83
StringCutObjectSelector< reco::Muon > targetMuonSelector_
ParameterSet const & getParameterSet(std::string const &) const
virtual double pt() const
transverse momentum
std::vector< size_type > Keys
std::map< std::string, edm::InputTag > inputTags_
std::string const & label() const
Definition: InputTag.h:25
trigger::TriggerObjectCollection selectedTriggerObjects(const trigger::TriggerObjectCollection &, const trigger::TriggerEvent &, const edm::ParameterSet &)
std::vector< std::string > vstring
edm::ParameterSet probeParams_
void fillMapFromPSet(std::map< std::string, T > &, edm::ParameterSet, std::string)
void endRun(const edm::Run &, const edm::EventSetup &)
const Point & position() const
position
Definition: BeamSpot.h:63
MonitorElement * book2D(const char *name, const char *title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
Book 2D histogram.
Definition: DQMStore.cc:642
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
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:121
long double T
virtual double phi() const
momentum azimuthal angle
virtual const LorentzVector & p4() const
four-momentum Lorentz vector
static const HistoName names[]
std::map< std::string, double > plotCuts_
std::string const & instance() const
Definition: InputTag.h:26
tuple size
Write out results.
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:232
Definition: Run.h:32