CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HLTMuonValidator.cc
Go to the documentation of this file.
1 
17 
22 
23 
24 
25 using namespace std;
26 using namespace edm;
27 using namespace reco;
28 using namespace trigger;
29 using namespace l1extra;
30 
31 
32 
33 typedef vector<ParameterSet> Parameters;
34 
35 
36 
38  l1Matcher_(pset)
39 {
40 
41  hltProcessName_ = pset.getParameter< string >("hltProcessName");
42  hltPathsToCheck_ = pset.getParameter< vector<string> >("hltPathsToCheck");
43 
44  genParticleLabel_ = pset.getParameter<string>("genParticleLabel");
45  recMuonLabel_ = pset.getParameter<string>( "recMuonLabel");
46  l1CandLabel_ = pset.getParameter<string>( "l1CandLabel");
47  l2CandLabel_ = pset.getParameter<string>( "l2CandLabel");
48  l3CandLabel_ = pset.getParameter<string>( "l3CandLabel");
49 
50  cutsDr_ = pset.getParameter< vector<double> >("cutsDr" );
51 
52  parametersEta_ = pset.getParameter< vector<double> >("parametersEta");
53  parametersPhi_ = pset.getParameter< vector<double> >("parametersPhi");
54  parametersTurnOn_ = pset.getParameter< vector<double> >("parametersTurnOn");
55 
56  genMuonCut_ = pset.getParameter<string>("genMuonCut");
57  recMuonCut_ = pset.getParameter<string>("recMuonCut");
58 
59  genMuonSelector_ = 0;
60  recMuonSelector_ = 0;
61 
63  dbe_->setVerbose(0);
64 
65 }
66 
67 
68 
69 void
71 {
72 }
73 
74 
75 
76 void
77 HLTMuonValidator::beginRun(const Run & iRun, const EventSetup & iSetup)
78 {
79 
80  static int runNumber = 0;
81  runNumber++;
82 
83  l1Matcher_.init(iSetup);
84 
85  bool changedConfig;
86  if (!hltConfig_.init(iRun, iSetup, hltProcessName_, changedConfig)) {
87  LogError("HLTMuonVal") << "Initialization of HLTConfigProvider failed!!";
88  return;
89  }
90 
91  hltPaths_.clear();
92  filterLabels_.clear();
93  vector<string> validTriggerNames = hltConfig_.triggerNames();
94  for (size_t i = 0; i < hltPathsToCheck_.size(); i++) {
95  TPRegexp pattern(hltPathsToCheck_[i]);
96  for (size_t j = 0; j < validTriggerNames.size(); j++)
97  if (TString(validTriggerNames[j]).Contains(pattern))
98  hltPaths_.insert(validTriggerNames[j]);
99  // Add fake HLT path where we bypass all filters
100  if (TString("NoFilters").Contains(pattern))
101  hltPaths_.insert("NoFilters");
102  }
103 
104  initializeHists();
105 
106 }
107 
108 
109 
110 void
112 {
113 
114  vector<string> sources(2);
115  sources[0] = "gen";
116  sources[1] = "rec";
117 
118  set<string>::iterator iPath;
119  TPRegexp suffixPtCut("[0-9]+$");
120 
121  for (iPath = hltPaths_.begin(); iPath != hltPaths_.end(); iPath++) {
122 
123  string path = * iPath;
124 
125  if (path == "NoFilters") {
126  filterLabels_[path].push_back("hltL1sL1SingleMuOpenL1SingleMu0");
127  // The L2 and L3 collections will be built manually later on
128  filterLabels_[path].push_back("");
129  filterLabels_[path].push_back("");
130  }
131 
132  vector<string> moduleLabels;
133  if (path != "NoFilters") moduleLabels = hltConfig_.moduleLabels(path);
134 
135  for (size_t i = 0; i < moduleLabels.size(); i++)
136  if (moduleLabels[i].find("Filtered") != string::npos)
137  filterLabels_[path].push_back(moduleLabels[i]);
138 
139  // Getting a reliable L1 pT measurement is impossible beyond 2.1, so most
140  // paths have no L1 beyond 2.1 except those passing kLooseL1Requirement
141  double cutMaxEta = (TString(path).Contains(kLooseL1Requirement)) ? 2.4:2.1;
142 
143  // Choose a pT cut for gen/rec muons based on the pT cut in the path
144  unsigned int index = TString(path).Index(suffixPtCut);
145  unsigned int threshold = 3;
146  if (index < path.length()) threshold = atoi(path.substr(index).c_str());
147  // We select a whole number min pT cut slightly above the path's final
148  // pt threshold, then subtract a bit to let through particle gun muons with
149  // exact integer pT:
150  double cutMinPt = ceil(threshold * 1.1) - 0.01;
151  if (cutMinPt < 0. || path == "NoFilters") cutMinPt = 0.;
152  cutsMinPt_[path] = cutMinPt;
153 
154  string baseDir = "HLT/Muon/Distributions/";
155  dbe_->setCurrentFolder(baseDir + path);
156 
157  if (dbe_->get(baseDir + path + "/CutMinPt") == 0) {
158 
159  elements_[path + "_" + "CutMinPt" ] = dbe_->bookFloat("CutMinPt" );
160  elements_[path + "_" + "CutMaxEta"] = dbe_->bookFloat("CutMaxEta");
161  elements_[path + "_" + "CutMinPt" ]->Fill(cutMinPt);
162  elements_[path + "_" + "CutMaxEta"]->Fill(cutMaxEta);
163 
164  // Standardize the names that will be applied to each step
165  const int nFilters = filterLabels_[path].size();
166  stepLabels_[path].push_back("All");
167  stepLabels_[path].push_back("L1");
168  if (nFilters == 2) {
169  stepLabels_[path].push_back("L2");
170  }
171  if (nFilters == 3) {
172  stepLabels_[path].push_back("L2");
173  stepLabels_[path].push_back("L3");
174  }
175  if (nFilters == 5) {
176  stepLabels_[path].push_back("L2");
177  stepLabels_[path].push_back("L2Iso");
178  stepLabels_[path].push_back("L3");
179  stepLabels_[path].push_back("L3Iso");
180  }
181 
182  // string l1Name = path + "_L1Quality";
183  // elements_[l1Name.c_str()] =
184  // dbe_->book1D("L1Quality", "Quality of L1 Muons", 8, 0, 8);
185  // for (size_t i = 0; i < 8; i++)
186  // elements_[l1Name.c_str()]->setBinLabel(i + 1, Form("%i", i));
187 
188  for (size_t i = 0; i < sources.size(); i++) {
189  string source = sources[i];
190  for (size_t j = 0; j < stepLabels_[path].size(); j++) {
191  bookHist(path, stepLabels_[path][j], source, "Eta");
192  bookHist(path, stepLabels_[path][j], source, "Phi");
193  bookHist(path, stepLabels_[path][j], source, "MaxPt1");
194  bookHist(path, stepLabels_[path][j], source, "MaxPt2");
195  }
196  }
197 
198  }
199 
200  }
201 
202 }
203 
204 
205 
206 void
208 {
209 
210  static int eventNumber = 0;
211  eventNumber++;
212  LogTrace("HLTMuonVal") << "In HLTMuonValidator::analyze, "
213  << "Event: " << eventNumber;
214 
215  Handle< TriggerEventWithRefs> rawTriggerEvent;
216  Handle< MuonCollection> recMuons;
218 
219  iEvent.getByLabel("hltTriggerSummaryRAW", rawTriggerEvent);
220  if (rawTriggerEvent.failedToGet())
221  {LogError("HLTMuonVal") << "No trigger summary found"; return;}
222  iEvent.getByLabel( recMuonLabel_, recMuons );
223  iEvent.getByLabel(genParticleLabel_, genParticles );
224 
225  vector<string> sources;
226  if (genParticles.isValid()) sources.push_back("gen");
227  if ( recMuons.isValid()) sources.push_back("rec");
228 
229  for (size_t sourceNo = 0; sourceNo < sources.size(); sourceNo++) {
230 
231  string source = sources[sourceNo];
232 
233  // If this is the first event, initialize selectors
238 
239  // Make each good gen/rec muon into the base cand for a MatchStruct
240  vector<MatchStruct> matches;
241  if (source == "gen" && genParticles.isValid())
242  for (size_t i = 0; i < genParticles->size(); i++)
243  if ((*genMuonSelector_)(genParticles->at(i)))
244  matches.push_back(MatchStruct(& genParticles->at(i)));
245  if (source == "rec" && recMuons.isValid())
246  for (size_t i = 0; i < recMuons->size(); i++)
247  if ((*recMuonSelector_)(recMuons->at(i)))
248  matches.push_back(MatchStruct(& recMuons->at(i)));
249 
250  // Sort the MatchStructs by pT for later filling of turn-on curve
251  sort(matches.begin(), matches.end(), matchesByDescendingPt());
252 
253  set<string>::iterator iPath;
254  for (iPath = hltPaths_.begin(); iPath != hltPaths_.end(); iPath++)
255  analyzePath(iEvent, * iPath, source, matches, rawTriggerEvent);
256 
257  } // End loop over sources
258 
259 }
260 
261 
262 
263 void
265  const string & path,
266  const string & source,
267  vector<MatchStruct> matches,
268  Handle<TriggerEventWithRefs> rawTriggerEvent)
269 {
270 
271  const bool skipFilters = (path == "NoFilters");
272 
273  const float maxEta = elements_[path + "_" + "CutMaxEta"]->getFloatValue();
274  const bool isDoubleMuonPath = (path.find("Double") != string::npos);
275  const size_t nFilters = filterLabels_[path].size();
276  const size_t nSteps = stepLabels_[path].size();
277  const size_t nStepsHlt = nSteps - 2;
278  const int nObjectsToPassPath = (isDoubleMuonPath) ? 2 : 1;
279  vector< L1MuonParticleRef > candsL1;
280  vector< vector< RecoChargedCandidateRef > > refsHlt(nStepsHlt);
281  vector< vector< const RecoChargedCandidate * > > candsHlt(nStepsHlt);
282 
283  for (size_t i = 0; i < nFilters; i++) {
284  const int hltStep = i - 1;
286  size_t iFilter = rawTriggerEvent->filterIndex(tag);
287  if (iFilter < rawTriggerEvent->size()) {
288  if (i == 0)
289  rawTriggerEvent->getObjects(iFilter, TriggerL1Mu, candsL1);
290  else
291  rawTriggerEvent->getObjects(iFilter, TriggerMuon,
292  refsHlt[hltStep]);
293  }
294  else if (!skipFilters)
295  LogTrace("HLTMuonVal") << "No collection with label " << tag;
296  }
297  if (skipFilters) {
300  iEvent.getByLabel(l2CandLabel_, handleCandsL2);
301  iEvent.getByLabel(l3CandLabel_, handleCandsL3);
302  if (handleCandsL2.isValid())
303  for (size_t i = 0; i < handleCandsL2->size(); i++)
304  candsHlt[0].push_back(& handleCandsL2->at(i));
305  if (handleCandsL3.isValid())
306  for (size_t i = 0; i < handleCandsL3->size(); i++)
307  candsHlt[1].push_back(& handleCandsL3->at(i));
308  }
309  else for (size_t i = 0; i < nStepsHlt; i++)
310  for (size_t j = 0; j < refsHlt[i].size(); j++)
311  candsHlt[i].push_back(& * refsHlt[i][j]);
312 
313  // Add trigger objects to the MatchStructs
314  findMatches(matches, candsL1, candsHlt);
315 
316  vector<size_t> matchesInEtaRange;
317  vector<bool> hasMatch(matches.size(), true);
318 
319  for (size_t step = 0; step < nSteps; step++) {
320 
321  const size_t hltStep = (step >= 2) ? step - 2 : 0;
322  const size_t level = (step == 1) ? 1 :
323  (step == 2) ? 2 :
324  (step == 3) ? ((nStepsHlt == 4) ? 2 : 3) :
325  (step >= 4) ? 3 :
326  0; // default value when step == 0
327 
328  for (size_t j = 0; j < matches.size(); j++) {
329  if (level == 0) {
330  if (fabs(matches[j].candBase->eta()) < maxEta)
331  matchesInEtaRange.push_back(j);
332  }
333  else if (level == 1) {
334  if (matches[j].candL1 == 0)
335  hasMatch[j] = false;
336  }
337  else if (level >= 2) {
338  if (matches[j].candHlt[hltStep] == 0)
339  hasMatch[j] = false;
340  else if (!hasMatch[j]) {
341  LogTrace("HLTMuonVal") << "Match found for HLT step " << hltStep
342  << " of " << nStepsHlt
343  << " without previous match!";
344  break;
345  }
346  }
347  }
348 
349  if (std::count(hasMatch.begin(), hasMatch.end(), true) <
350  nObjectsToPassPath)
351  break;
352 
353  string pre = path + "_" + source + "Pass";
354  string post = "_" + stepLabels_[path][step];
355 
356  for (size_t j = 0; j < matches.size(); j++) {
357  float pt = matches[j].candBase->pt();
358  float eta = matches[j].candBase->eta();
359  float phi = matches[j].candBase->phi();
360  if (hasMatch[j]) {
361  if (matchesInEtaRange.size() >= 1 && j == matchesInEtaRange[0])
362  elements_[pre + "MaxPt1" + post]->Fill(pt);
363  if (matchesInEtaRange.size() >= 2 && j == matchesInEtaRange[1])
364  elements_[pre + "MaxPt2" + post]->Fill(pt);
365  if(fabs(eta) < maxEta && pt > cutsMinPt_[path]) {
366  elements_[pre + "Eta" + post]->Fill(eta);
367  elements_[pre + "Phi" + post]->Fill(phi);
368  }
369  }
370  }
371 
372  }
373 
374 }
375 
376 
377 
378 void
380  vector<MatchStruct> & matches,
381  vector<L1MuonParticleRef> candsL1,
382  vector< vector< const RecoChargedCandidate *> > candsHlt)
383 {
384 
385  set<size_t>::iterator it;
386 
387  set<size_t> indicesL1;
388  for (size_t i = 0; i < candsL1.size(); i++)
389  indicesL1.insert(i);
390 
391  vector< set<size_t> > indicesHlt(candsHlt.size());
392  for (size_t i = 0; i < candsHlt.size(); i++)
393  for (size_t j = 0; j < candsHlt[i].size(); j++)
394  indicesHlt[i].insert(j);
395 
396  for (size_t i = 0; i < matches.size(); i++) {
397 
398  const Candidate * cand = matches[i].candBase;
399 
400  double bestDeltaR = cutsDr_[0];
401  size_t bestMatch = kNull;
402  for (it = indicesL1.begin(); it != indicesL1.end(); it++) {
403  double dR = deltaR(cand->eta(), cand->phi(),
404  candsL1[*it]->eta(), candsL1[*it]->phi());
405  if (dR < bestDeltaR) {
406  bestMatch = *it;
407  bestDeltaR = dR;
408  }
409  // TrajectoryStateOnSurface propagated;
410  // float dR = 999., dPhi = 999.;
411  // bool isValid = l1Matcher_.match(* cand, * candsL1[*it],
412  // dR, dPhi, propagated);
413  // if (isValid && dR < bestDeltaR) {
414  // bestMatch = *it;
415  // bestDeltaR = dR;
416  // }
417  }
418  if (bestMatch != kNull)
419  matches[i].candL1 = & * candsL1[bestMatch];
420  indicesL1.erase(bestMatch);
421 
422  matches[i].candHlt.assign(candsHlt.size(), 0);
423  for (size_t j = 0; j < candsHlt.size(); j++) {
424  size_t level = (candsHlt.size() == 4) ? (j < 2) ? 2 : 3 :
425  (candsHlt.size() == 2) ? (j < 1) ? 2 : 3 :
426  2;
427  bestDeltaR = cutsDr_[level - 2];
428  bestMatch = kNull;
429  for (it = indicesHlt[j].begin(); it != indicesHlt[j].end(); it++) {
430  double dR = deltaR(cand->eta(), cand->phi(),
431  candsHlt[j][*it]->eta(), candsHlt[j][*it]->phi());
432  if (dR < bestDeltaR) {
433  bestMatch = *it;
434  bestDeltaR = dR;
435  }
436  }
437  if (bestMatch != kNull)
438  matches[i].candHlt[j] = candsHlt[j][bestMatch];
439  indicesHlt[j].erase(bestMatch);
440  }
441 
442 // cout << " Muon: " << cand->eta() << ", ";
443 // if (matches[i].candL1) cout << matches[i].candL1->eta() << ", ";
444 // else cout << "none, ";
445 // for (size_t j = 0; j < candsHlt.size(); j++)
446 // if (matches[i].candHlt[j]) cout << matches[i].candHlt[j]->eta() << ", ";
447 // else cout << "none, ";
448 // cout << endl;
449 
450  }
451 
452 }
453 
454 
455 
456 
457 void
459  string source, string type)
460 {
461 
462  string sourceUpper = source;
463  sourceUpper[0] = toupper(sourceUpper[0]);
464  string name = source + "Pass" + type + "_" + label;
465  string rootName = path + "_" + name;
466  TH1F * h;
467 
468  if (type.find("MaxPt") != string::npos) {
469  string desc = (type == "MaxPt1") ? "Leading" : "Next-to-Leading";
470  string title = "pT of " + desc + " " + sourceUpper + " Muon "+
471  "matched to " + label;
472  const size_t nBins = parametersTurnOn_.size() - 1;
473  float * edges = new float[nBins + 1];
474  for (size_t i = 0; i < nBins + 1; i++) edges[i] = parametersTurnOn_[i];
475  h = new TH1F(rootName.c_str(), title.c_str(), nBins, edges);
476  }
477 
478  else {
479  string symbol = (type == "Eta") ? "#eta" : "#phi";
480  string title = symbol + " of " + sourceUpper + " Muons " +
481  "matched to " + label;
482  vector<double> params = (type == "Eta") ? parametersEta_ : parametersPhi_;
483  int nBins = (int)params[0];
484  double min = params[1];
485  double max = params[2];
486  h = new TH1F(rootName.c_str(), title.c_str(), nBins, min, max);
487  }
488 
489  h->Sumw2();
490  elements_[rootName] = dbe_->book1D(name, h);
491  delete h;
492 
493 }
494 
495 
496 
type
Definition: HCALResponse.h:22
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
void init(const edm::EventSetup &iSetup)
Call this method at the beginning of each run, to initialize geometry, magnetic field and propagators...
std::map< std::string, std::vector< std::string > > stepLabels_
const unsigned int kNull
const std::string & label
Definition: MVAComputer.cc:186
std::string recMuonLabel_
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:519
enum start value shifted to 81 so as to avoid clashes with PDG codes
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const std::vector< std::string > & triggerNames() const
names of trigger paths
std::set< std::string > hltPaths_
virtual void analyze(const edm::Event &, const edm::EventSetup &)
#define min(a, b)
Definition: mlp_lapack.h:161
double maxEta
std::vector< double > cutsDr_
std::string l2CandLabel_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
std::vector< std::string > hltPathsToCheck_
dictionary edges
T eta() const
MonitorElement * bookFloat(const char *name)
Book float.
Definition: DQMStore.cc:456
int path() const
Definition: HLTadd.h:3
StringCutObjectSelector< reco::GenParticle > * genMuonSelector_
std::vector< double > parametersPhi_
int iEvent
Definition: GenABIO.cc:243
const T & max(const T &a, const T &b)
float threshold
Definition: crabWrap.py:319
L1MuonMatcherAlgo l1Matcher_
int j
Definition: DBlmapReader.cc:9
tuple pset
Definition: CrabTask.py:85
void setVerbose(unsigned level)
Definition: DQMStore.cc:201
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1270
bool isValid() const
Definition: HandleBase.h:76
std::map< std::string, MonitorElement * > elements_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
#define LogTrace(id)
virtual void beginJob()
StringCutObjectSelector< reco::Muon > * recMuonSelector_
std::map< std::string, std::vector< std::string > > filterLabels_
const std::vector< std::string > & moduleLabels(unsigned int trigger) const
label(s) of module(s) on a trigger path
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
std::string l3CandLabel_
bool failedToGet() const
Definition: HandleBase.h:80
HLTConfigProvider hltConfig_
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
std::string recMuonCut_
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
std::string genParticleLabel_
std::vector< double > parametersTurnOn_
void bookHist(std::string, std::string, std::string, std::string)
std::vector< AlignmentParameters * > Parameters
Definition: Utilities.h:29
std::string genMuonCut_
std::string hltProcessName_
std::vector< double > parametersEta_
#define begin
Definition: vmac.h:31
TPRegexp kLooseL1Requirement("HLT_Mu3|Double|NoFilters")
tuple level
Definition: testEve_cfg.py:34
std::string l1CandLabel_
void findMatches(std::vector< MatchStruct > &, std::vector< l1extra::L1MuonParticleRef >, std::vector< std::vector< const reco::RecoChargedCandidate * > >)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
bool insert(Storage &, ItemType *, const IdTag &)
void analyzePath(const edm::Event &, const std::string &, const std::string &, const std::vector< MatchStruct >, edm::Handle< trigger::TriggerEventWithRefs >)
tuple size
Write out results.
std::map< std::string, double > cutsMinPt_
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:237
HLTMuonValidator(const edm::ParameterSet &)
Definition: Run.h:31
virtual double phi() const =0
momentum azimuthal angle
virtual double eta() const =0
momentum pseudorapidity
Definition: DDAxes.h:10