CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HLTMuonPlotter.cc
Go to the documentation of this file.
1 
16 
21 
22 
23 
24 using namespace std;
25 using namespace edm;
26 using namespace reco;
27 using namespace trigger;
28 using namespace l1extra;
29 
30 
31 
32 typedef vector<ParameterSet> Parameters;
33 
34 
35 
37  string hltPath,
38  const std::vector<string>& moduleLabels,
39  const std::vector<string>& stepLabels,
43  ) :
44 
45  l1Matcher_(pset)
46 {
47 
48  hltPath_ = hltPath;
49  moduleLabels_ = moduleLabels;
50  stepLabels_ = stepLabels;
51  hltProcessName_ = pset.getParameter<string>("hltProcessName");
52 
53  cutsDr_ = pset.getParameter< vector<double> >("cutsDr" );
54 
55  parametersEta_ = pset.getParameter< vector<double> >("parametersEta");
56  parametersPhi_ = pset.getParameter< vector<double> >("parametersPhi");
57  parametersTurnOn_ = pset.getParameter< vector<double> >("parametersTurnOn");
58 
59  genMuonCut_ = pset.getParameter<string>("genMuonCut");
60  recMuonCut_ = pset.getParameter<string>("recMuonCut");
61 
62  genMuonSelector_ = 0;
63  recMuonSelector_ = 0;
64 
65  //set tokens
66  hltTriggerSummaryRAW_ = tokens.get<0>();
67  genParticleLabel_ = tokens.get<1>();
68  recMuonLabel_ = tokens.get<2>();
69 
70 }
71 
72 
73 
74 void
76 {
77 
78 }
79 
80 
81 
82 void
84  const Run & iRun, const EventSetup & iSetup)
85 {
86 
87  static int runNumber = 0;
88  runNumber++;
89 
90  l1Matcher_.init(iSetup);
91 
92  cutMaxEta_ = 2.4;
93  if (hltPath_.find("eta2p1") != string::npos)
94  cutMaxEta_ = 2.1;
95 
96  // Choose a pT cut for gen/rec muons based on the pT cut in the hltPath_
97  unsigned int threshold = 0;
98  TPRegexp ptRegexp("Mu([0-9]+)");
99  TObjArray * regexArray = ptRegexp.MatchS(hltPath_);
100  if (regexArray->GetEntriesFast() == 2) {
101  threshold = atoi(((TObjString *)regexArray->At(1))->GetString());
102  }
103  delete regexArray;
104  // We select a whole number min pT cut slightly above the hltPath_'s final
105  // pt threshold, then subtract a bit to let through particle gun muons with
106  // exact integer pT:
107  cutMinPt_ = ceil(threshold * 1.1) - 0.01;
108  if (cutMinPt_ < 0.) cutMinPt_ = 0.;
109 
110  string baseDir = "HLT/Muon/Distributions/";
111  iBooker.setCurrentFolder(baseDir + hltPath_);
112 
113  vector<string> sources(2);
114  sources[0] = "gen";
115  sources[1] = "rec";
116 
117  elements_["CutMinPt" ] = iBooker.bookFloat("CutMinPt" );
118  elements_["CutMaxEta"] = iBooker.bookFloat("CutMaxEta");
119  elements_["CutMinPt" ]->Fill(cutMinPt_);
120  elements_["CutMaxEta"]->Fill(cutMaxEta_);
121 
122  for (size_t i = 0; i < sources.size(); i++) {
123  string source = sources[i];
124  for (size_t j = 0; j < stepLabels_.size(); j++) {
125  bookHist(iBooker, hltPath_, stepLabels_[j], source, "Eta");
126  bookHist(iBooker, hltPath_, stepLabels_[j], source, "Phi");
127  bookHist(iBooker, hltPath_, stepLabels_[j], source, "MaxPt1");
128  bookHist(iBooker, hltPath_, stepLabels_[j], source, "MaxPt2");
129  }
130  }
131 
132 }
133 
134 
135 
136 void
138 {
139 
140  static int eventNumber = 0;
141  eventNumber++;
142  LogTrace("HLTMuonVal") << "In HLTMuonPlotter::analyze, "
143  << "Event: " << eventNumber;
144 
145  // cout << hltPath_ << endl;
146  // for (size_t i = 0; i < moduleLabels_.size(); i++)
147  // cout << " " << moduleLabels_[i] << endl;
148 
149  Handle< TriggerEventWithRefs> rawTriggerEvent;
150  Handle< MuonCollection> recMuons;
152 
153  iEvent.getByToken(hltTriggerSummaryRAW_, rawTriggerEvent);
154  if (rawTriggerEvent.failedToGet())
155  {LogError("HLTMuonVal") << "No trigger summary found"; return;}
156  iEvent.getByToken(recMuonLabel_, recMuons);
157  iEvent.getByToken(genParticleLabel_, genParticles );
158 
159  vector<string> sources;
160  if (genParticles.isValid()) sources.push_back("gen");
161  if ( recMuons.isValid()) sources.push_back("rec");
162 
163  for (size_t sourceNo = 0; sourceNo < sources.size(); sourceNo++) {
164 
165  string source = sources[sourceNo];
166 
167  // If this is the first event, initialize selectors
172 
173  // Make each good gen/rec muon into the base cand for a MatchStruct
174  vector<MatchStruct> matches;
175  if (source == "gen" && genParticles.isValid())
176  for (size_t i = 0; i < genParticles->size(); i++)
177  if ((*genMuonSelector_)(genParticles->at(i)))
178  matches.push_back(MatchStruct(& genParticles->at(i)));
179  if (source == "rec" && recMuons.isValid())
180  for (size_t i = 0; i < recMuons->size(); i++)
181  if ((*recMuonSelector_)(recMuons->at(i)))
182  matches.push_back(MatchStruct(& recMuons->at(i)));
183 
184  // Sort the MatchStructs by pT for later filling of turn-on curve
185  sort(matches.begin(), matches.end(), matchesByDescendingPt());
186 
187  const bool isDoubleMuonPath = (hltPath_.find("Double") != string::npos);
188  const size_t nFilters = moduleLabels_.size();
189  const size_t nSteps = stepLabels_.size();
190  const size_t nStepsHlt = nSteps - 2;
191  const int nObjectsToPassPath = (isDoubleMuonPath) ? 2 : 1;
192  vector< L1MuonParticleRef > candsL1;
193  vector< vector< RecoChargedCandidateRef > > refsHlt(nStepsHlt);
194  vector< vector< const RecoChargedCandidate * > > candsHlt(nStepsHlt);
195 
196  for (size_t i = 0; i < nFilters; i++) {
197  const int hltStep = i - 1;
199  size_t iFilter = rawTriggerEvent->filterIndex(tag);
200  if (iFilter < rawTriggerEvent->size()) {
201  if (i == 0)
202  rawTriggerEvent->getObjects(iFilter, TriggerL1Mu, candsL1);
203  else
204  rawTriggerEvent->getObjects(iFilter, TriggerMuon,
205  refsHlt[hltStep]);
206  }
207  else LogTrace("HLTMuonVal") << "No collection with label " << tag;
208  }
209  for (size_t i = 0; i < nStepsHlt; i++)
210  for (size_t j = 0; j < refsHlt[i].size(); j++)
211  if (refsHlt[i][j].isAvailable()) {
212  candsHlt[i].push_back(& * refsHlt[i][j]);
213  } else {
214  LogWarning("HLTMuonPlotter")
215  << "Ref refsHlt[i][j]: product not available "
216  << i << " " << j;
217  }
218 
219  // Add trigger objects to the MatchStructs
220  findMatches(matches, candsL1, candsHlt);
221 
222  vector<size_t> matchesInEtaRange;
223  vector<bool> hasMatch(matches.size(), true);
224 
225  for (size_t step = 0; step < nSteps; step++) {
226 
227  const size_t hltStep = (step >= 2) ? step - 2 : 0;
228  size_t level = 0;
229  if ((stepLabels_[step].find("L3TkIso") != string::npos)||(stepLabels_[step].find("TkTkIso") != string::npos)) level = 6;
230  else if ((stepLabels_[step].find("L3HcalIso") != string::npos)||(stepLabels_[step].find("TkEcalIso") != string::npos)) level = 5;
231  else if ((stepLabels_[step].find("L3EcalIso") != string::npos)||(stepLabels_[step].find("TkEcalIso") != string::npos)) level = 4;
232  else if ((stepLabels_[step].find("L3") != string::npos)||(stepLabels_[step].find("Tk") != string::npos)) level = 3;
233  else if (stepLabels_[step].find("L2") != string::npos) level = 2;
234  else if (stepLabels_[step].find("L1") != string::npos) level = 1;
235 
236  for (size_t j = 0; j < matches.size(); j++) {
237  if (level == 0) {
238  if (fabs(matches[j].candBase->eta()) < cutMaxEta_)
239  matchesInEtaRange.push_back(j);
240  }
241  else if (level == 1) {
242  if (matches[j].candL1 == 0)
243  hasMatch[j] = false;
244  }
245  else if (level >= 2) {
246  if (matches[j].candHlt[hltStep] == 0)
247  hasMatch[j] = false;
248  else if (!hasMatch[j]) {
249  LogTrace("HLTMuonVal") << "Match found for HLT step " << hltStep
250  << " of " << nStepsHlt
251  << " without previous match!";
252  break;
253  }
254  }
255  }
256 
257  if (std::count(hasMatch.begin(), hasMatch.end(), true) <
258  nObjectsToPassPath)
259  break;
260 
261  string pre = source + "Pass";
262  string post = "_" + stepLabels_[step];
263 
264  for (size_t j = 0; j < matches.size(); j++) {
265  float pt = matches[j].candBase->pt();
266  float eta = matches[j].candBase->eta();
267  float phi = matches[j].candBase->phi();
268  if (hasMatch[j]) {
269  if (matchesInEtaRange.size() >= 1 && j == matchesInEtaRange[0])
270  elements_[pre + "MaxPt1" + post]->Fill(pt);
271  if (matchesInEtaRange.size() >= 2 && j == matchesInEtaRange[1])
272  elements_[pre + "MaxPt2" + post]->Fill(pt);
273  if (pt > cutMinPt_) {
274  elements_[pre + "Eta" + post]->Fill(eta);
275  if (fabs(eta) < cutMaxEta_)
276  elements_[pre + "Phi" + post]->Fill(phi);
277  }
278  }
279  }
280 
281  }
282 
283 
284 
285  } // End loop over sources
286 
287 }
288 
289 
290 
291 boost::tuple<
295 {
296 
297  edm::EDGetTokenT<trigger::TriggerEventWithRefs> _hltTriggerSummaryRAW = iC.consumes<TriggerEventWithRefs>(edm::InputTag("hltTriggerSummaryRAW"));
298  edm::EDGetTokenT<reco::GenParticleCollection> _genParticleLabel = iC.consumes<GenParticleCollection>(pset.getParameter<string>("genParticleLabel"));
299  edm::EDGetTokenT<reco::MuonCollection> _recMuonLabel = iC.consumes<MuonCollection>(pset.getParameter<string>("recMuonLabel"));
300 
301  boost::tuple<
304  edm::EDGetTokenT<reco::MuonCollection> > myTuple(_hltTriggerSummaryRAW,_genParticleLabel,_recMuonLabel);
305 
306  return (myTuple);
307 }
308 
309 
310 
311 void
313  vector<MatchStruct> & matches,
314  const std::vector<L1MuonParticleRef>& candsL1,
315  const std::vector< vector< const RecoChargedCandidate *> >& candsHlt)
316 {
317 
318  set<size_t>::iterator it;
319 
320  set<size_t> indicesL1;
321  for (size_t i = 0; i < candsL1.size(); i++)
322  indicesL1.insert(i);
323 
324  vector< set<size_t> > indicesHlt(candsHlt.size());
325  for (size_t i = 0; i < candsHlt.size(); i++)
326  for (size_t j = 0; j < candsHlt[i].size(); j++)
327  indicesHlt[i].insert(j);
328 
329  for (size_t i = 0; i < matches.size(); i++) {
330 
331  const Candidate * cand = matches[i].candBase;
332 
333  double bestDeltaR = cutsDr_[0];
334  size_t bestMatch = kNull;
335  for (it = indicesL1.begin(); it != indicesL1.end(); it++) {
336  if (candsL1[*it].isAvailable()) {
337  double dR = deltaR(cand->eta(), cand->phi(),
338  candsL1[*it]->eta(), candsL1[*it]->phi());
339  if (dR < bestDeltaR) {
340  bestMatch = *it;
341  bestDeltaR = dR;
342  }
343  // TrajectoryStateOnSurface propagated;
344  // float dR = 999., dPhi = 999.;
345  // bool isValid = l1Matcher_.match(* cand, * candsL1[*it],
346  // dR, dPhi, propagated);
347  // if (isValid && dR < bestDeltaR) {
348  // bestMatch = *it;
349  // bestDeltaR = dR;
350  // }
351  } else {
352  LogWarning("HLTMuonPlotter")
353  << "Ref candsL1[*it]: product not available "
354  << *it;
355  }
356  }
357  if (bestMatch != kNull)
358  matches[i].candL1 = & * candsL1[bestMatch];
359  indicesL1.erase(bestMatch);
360 
361  matches[i].candHlt.assign(candsHlt.size(), 0);
362  for (size_t j = 0; j < candsHlt.size(); j++) {
363  size_t level = (candsHlt.size() == 4) ? (j < 2) ? 2 : 3 :
364  (candsHlt.size() == 2) ? (j < 1) ? 2 : 3 :
365  2;
366  bestDeltaR = cutsDr_[level - 2];
367  bestMatch = kNull;
368  for (it = indicesHlt[j].begin(); it != indicesHlt[j].end(); it++) {
369  double dR = deltaR(cand->eta(), cand->phi(),
370  candsHlt[j][*it]->eta(), candsHlt[j][*it]->phi());
371  if (dR < bestDeltaR) {
372  bestMatch = *it;
373  bestDeltaR = dR;
374  }
375  }
376  if (bestMatch != kNull)
377  matches[i].candHlt[j] = candsHlt[j][bestMatch];
378  indicesHlt[j].erase(bestMatch);
379  }
380 
381 // cout << " Muon: " << cand->eta() << ", ";
382 // if (matches[i].candL1) cout << matches[i].candL1->eta() << ", ";
383 // else cout << "none, ";
384 // for (size_t j = 0; j < candsHlt.size(); j++)
385 // if (matches[i].candHlt[j]) cout << matches[i].candHlt[j]->eta() << ", ";
386 // else cout << "none, ";
387 // cout << endl;
388 
389  }
390 
391 }
392 
393 
394 
395 
396 void
398  string path, string label,
399  string source, string type)
400 {
401 
402  string sourceUpper = source;
403  sourceUpper[0] = toupper(sourceUpper[0]);
404  string name = source + "Pass" + type + "_" + label;
405  TH1F * h;
406 
407  if (type.find("MaxPt") != string::npos) {
408  string desc = (type == "MaxPt1") ? "Leading" : "Next-to-Leading";
409  string title = "pT of " + desc + " " + sourceUpper + " Muon "+
410  "matched to " + label;
411  const size_t nBins = parametersTurnOn_.size() - 1;
412  float * edges = new float[nBins + 1];
413  for (size_t i = 0; i < nBins + 1; i++) edges[i] = parametersTurnOn_[i];
414  h = new TH1F(name.c_str(), title.c_str(), nBins, edges);
415  }
416 
417  else {
418  string symbol = (type == "Eta") ? "#eta" : "#phi";
419  string title = symbol + " of " + sourceUpper + " Muons " +
420  "matched to " + label;
421  vector<double> params = (type == "Eta") ? parametersEta_ : parametersPhi_;
422  int nBins = (int)params[0];
423  double min = params[1];
424  double max = params[2];
425  h = new TH1F(name.c_str(), title.c_str(), nBins, min, max);
426  }
427 
428  h->Sumw2();
429  elements_[name] = iBooker.book1D(name, h);
430  delete h;
431 
432 }
433 
def bestMatch
Definition: deltar.py:136
type
Definition: HCALResponse.h:21
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
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...
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:449
enum start value shifted to 81 so as to avoid clashes with PDG codes
void beginRun(DQMStore::IBooker &, const edm::Run &, const edm::EventSetup &)
std::string hltPath_
std::vector< double > parametersEta_
L1MuonMatcherAlgo l1Matcher_
T eta() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
std::string genMuonCut_
dictionary edges
std::vector< std::string > moduleLabels_
std::vector< Muon > MuonCollection
collection of Muon objects
Definition: MuonFwd.h:9
static boost::tuple< edm::EDGetTokenT< trigger::TriggerEventWithRefs >, edm::EDGetTokenT< reco::GenParticleCollection >, edm::EDGetTokenT< reco::MuonCollection > > getTokens(const edm::ParameterSet &, edm::ConsumesCollector &&)
std::string recMuonCut_
int iEvent
Definition: GenABIO.cc:230
vector< ParameterSet > Parameters
tuple path
else: Piece not in the list, fine.
StringCutObjectSelector< reco::Muon > * recMuonSelector_
std::vector< double > cutsDr_
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
const unsigned int kNull
edm::EDGetTokenT< reco::MuonCollection > recMuonLabel_
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:115
int j
Definition: DBlmapReader.cc:9
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void analyze(const edm::Event &, const edm::EventSetup &)
T min(T a, T b)
Definition: MathUtil.h:58
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
bool isValid() const
Definition: HandleBase.h:75
StringCutObjectSelector< reco::GenParticle > * genMuonSelector_
#define LogTrace(id)
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
std::vector< std::string > stepLabels_
bool failedToGet() const
Definition: HandleBase.h:79
void findMatches(std::vector< MatchStruct > &, const std::vector< l1extra::L1MuonParticleRef > &, const std::vector< std::vector< const reco::RecoChargedCandidate * > > &)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:274
void bookHist(DQMStore::IBooker &, std::string, std::string, std::string, std::string)
#define begin
Definition: vmac.h:30
std::vector< double > parametersPhi_
tuple level
Definition: testEve_cfg.py:34
MonitorElement * bookFloat(Args &&...args)
Definition: DQMStore.h:109
std::map< std::string, MonitorElement * > elements_
std::string hltProcessName_
edm::EDGetTokenT< reco::GenParticleCollection > genParticleLabel_
HLTMuonPlotter(const edm::ParameterSet &, std::string, const std::vector< std::string > &, const std::vector< std::string > &, const boost::tuple< edm::EDGetTokenT< trigger::TriggerEventWithRefs >, edm::EDGetTokenT< reco::GenParticleCollection >, edm::EDGetTokenT< reco::MuonCollection > > &)
edm::EDGetTokenT< trigger::TriggerEventWithRefs > hltTriggerSummaryRAW_
std::vector< double > parametersTurnOn_
static std::string const source
Definition: EdmProvDump.cc:42
tuple size
Write out results.
Definition: Run.h:41
virtual double phi() const =0
momentum azimuthal angle
virtual double eta() const =0
momentum pseudorapidity
Definition: DDAxes.h:10