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 
18 
23 
24 
25 
26 using namespace std;
27 using namespace edm;
28 using namespace reco;
29 using namespace trigger;
30 using namespace l1extra;
31 
32 
33 
34 typedef vector<ParameterSet> Parameters;
35 
36 
37 
39  string hltPath,
40  const std::vector<string>& moduleLabels,
41  const std::vector<string>& stepLabels) :
42  l1Matcher_(pset)
43 {
44 
45  hltPath_ = hltPath;
46  moduleLabels_ = moduleLabels;
47  stepLabels_ = stepLabels;
48  hltProcessName_ = pset.getParameter<string>("hltProcessName");
49 
50  genParticleLabel_ = pset.getParameter<string>("genParticleLabel");
51  recMuonLabel_ = pset.getParameter<string>( "recMuonLabel");
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 
66  dbe_->setVerbose(0);
67 
68 }
69 
70 
71 
72 void
74 {
75 }
76 
77 
78 
79 void
80 HLTMuonPlotter::beginRun(const Run & iRun, const EventSetup & iSetup)
81 {
82 
83  static int runNumber = 0;
84  runNumber++;
85 
86  l1Matcher_.init(iSetup);
87 
88  cutMaxEta_ = 2.4;
89  if (hltPath_.find("eta2p1") != string::npos)
90  cutMaxEta_ = 2.1;
91 
92  // Choose a pT cut for gen/rec muons based on the pT cut in the hltPath_
93  unsigned int threshold = 0;
94  TPRegexp ptRegexp("Mu([0-9]+)");
95  TObjArray * regexArray = ptRegexp.MatchS(hltPath_);
96  if (regexArray->GetEntriesFast() == 2) {
97  threshold = atoi(((TObjString *)regexArray->At(1))->GetString());
98  }
99  delete regexArray;
100  // We select a whole number min pT cut slightly above the hltPath_'s final
101  // pt threshold, then subtract a bit to let through particle gun muons with
102  // exact integer pT:
103  cutMinPt_ = ceil(threshold * 1.1) - 0.01;
104  if (cutMinPt_ < 0.) cutMinPt_ = 0.;
105 
106  string baseDir = "HLT/Muon/Distributions/";
107  dbe_->setCurrentFolder(baseDir + hltPath_);
108 
109  vector<string> sources(2);
110  sources[0] = "gen";
111  sources[1] = "rec";
112 
113  if (dbe_->get(baseDir + hltPath_ + "/CutMinPt") == 0) {
114 
115  elements_["CutMinPt" ] = dbe_->bookFloat("CutMinPt" );
116  elements_["CutMaxEta"] = dbe_->bookFloat("CutMaxEta");
117  elements_["CutMinPt" ]->Fill(cutMinPt_);
118  elements_["CutMaxEta"]->Fill(cutMaxEta_);
119 
120  for (size_t i = 0; i < sources.size(); i++) {
121  string source = sources[i];
122  for (size_t j = 0; j < stepLabels_.size(); j++) {
123  bookHist(hltPath_, stepLabels_[j], source, "Eta");
124  bookHist(hltPath_, stepLabels_[j], source, "Phi");
125  bookHist(hltPath_, stepLabels_[j], source, "MaxPt1");
126  bookHist(hltPath_, stepLabels_[j], source, "MaxPt2");
127  }
128  }
129  }
130 
131 }
132 
133 
134 
135 void
137 {
138 
139  static int eventNumber = 0;
140  eventNumber++;
141  LogTrace("HLTMuonVal") << "In HLTMuonPlotter::analyze, "
142  << "Event: " << eventNumber;
143 
144  // cout << hltPath_ << endl;
145  // for (size_t i = 0; i < moduleLabels_.size(); i++)
146  // cout << " " << moduleLabels_[i] << endl;
147 
148  Handle< TriggerEventWithRefs> rawTriggerEvent;
149  Handle< MuonCollection> recMuons;
151 
152  iEvent.getByLabel("hltTriggerSummaryRAW", rawTriggerEvent);
153  if (rawTriggerEvent.failedToGet())
154  {LogError("HLTMuonVal") << "No trigger summary found"; return;}
155  iEvent.getByLabel( recMuonLabel_, recMuons );
156  iEvent.getByLabel(genParticleLabel_, genParticles );
157 
158  vector<string> sources;
159  if (genParticles.isValid()) sources.push_back("gen");
160  if ( recMuons.isValid()) sources.push_back("rec");
161 
162  for (size_t sourceNo = 0; sourceNo < sources.size(); sourceNo++) {
163 
164  string source = sources[sourceNo];
165 
166  // If this is the first event, initialize selectors
171 
172  // Make each good gen/rec muon into the base cand for a MatchStruct
173  vector<MatchStruct> matches;
174  if (source == "gen" && genParticles.isValid())
175  for (size_t i = 0; i < genParticles->size(); i++)
176  if ((*genMuonSelector_)(genParticles->at(i)))
177  matches.push_back(MatchStruct(& genParticles->at(i)));
178  if (source == "rec" && recMuons.isValid())
179  for (size_t i = 0; i < recMuons->size(); i++)
180  if ((*recMuonSelector_)(recMuons->at(i)))
181  matches.push_back(MatchStruct(& recMuons->at(i)));
182 
183  // Sort the MatchStructs by pT for later filling of turn-on curve
184  sort(matches.begin(), matches.end(), matchesByDescendingPt());
185 
186  const bool isDoubleMuonPath = (hltPath_.find("Double") != string::npos);
187  const size_t nFilters = moduleLabels_.size();
188  const size_t nSteps = stepLabels_.size();
189  const size_t nStepsHlt = nSteps - 2;
190  const int nObjectsToPassPath = (isDoubleMuonPath) ? 2 : 1;
191  vector< L1MuonParticleRef > candsL1;
192  vector< vector< RecoChargedCandidateRef > > refsHlt(nStepsHlt);
193  vector< vector< const RecoChargedCandidate * > > candsHlt(nStepsHlt);
194 
195  for (size_t i = 0; i < nFilters; i++) {
196  const int hltStep = i - 1;
198  size_t iFilter = rawTriggerEvent->filterIndex(tag);
199  if (iFilter < rawTriggerEvent->size()) {
200  if (i == 0)
201  rawTriggerEvent->getObjects(iFilter, TriggerL1Mu, candsL1);
202  else
203  rawTriggerEvent->getObjects(iFilter, TriggerMuon,
204  refsHlt[hltStep]);
205  }
206  else LogTrace("HLTMuonVal") << "No collection with label " << tag;
207  }
208  for (size_t i = 0; i < nStepsHlt; i++)
209  for (size_t j = 0; j < refsHlt[i].size(); j++)
210  if (refsHlt[i][j].isAvailable()) {
211  candsHlt[i].push_back(& * refsHlt[i][j]);
212  } else {
213  LogWarning("HLTMuonPlotter")
214  << "Ref refsHlt[i][j]: product not available "
215  << i << " " << j;
216  }
217 
218  // Add trigger objects to the MatchStructs
219  findMatches(matches, candsL1, candsHlt);
220 
221  vector<size_t> matchesInEtaRange;
222  vector<bool> hasMatch(matches.size(), true);
223 
224  for (size_t step = 0; step < nSteps; step++) {
225 
226  const size_t hltStep = (step >= 2) ? step - 2 : 0;
227  size_t level = 0;
228  if (stepLabels_[step].find("L3") != string::npos) level = 3;
229  else if (stepLabels_[step].find("L2") != string::npos) level = 2;
230  else if (stepLabels_[step].find("L1") != string::npos) level = 1;
231 
232  for (size_t j = 0; j < matches.size(); j++) {
233  if (level == 0) {
234  if (fabs(matches[j].candBase->eta()) < cutMaxEta_)
235  matchesInEtaRange.push_back(j);
236  }
237  else if (level == 1) {
238  if (matches[j].candL1 == 0)
239  hasMatch[j] = false;
240  }
241  else if (level >= 2) {
242  if (matches[j].candHlt[hltStep] == 0)
243  hasMatch[j] = false;
244  else if (!hasMatch[j]) {
245  LogTrace("HLTMuonVal") << "Match found for HLT step " << hltStep
246  << " of " << nStepsHlt
247  << " without previous match!";
248  break;
249  }
250  }
251  }
252 
253  if (std::count(hasMatch.begin(), hasMatch.end(), true) <
254  nObjectsToPassPath)
255  break;
256 
257  string pre = source + "Pass";
258  string post = "_" + stepLabels_[step];
259 
260  for (size_t j = 0; j < matches.size(); j++) {
261  float pt = matches[j].candBase->pt();
262  float eta = matches[j].candBase->eta();
263  float phi = matches[j].candBase->phi();
264  if (hasMatch[j]) {
265  if (matchesInEtaRange.size() >= 1 && j == matchesInEtaRange[0])
266  elements_[pre + "MaxPt1" + post]->Fill(pt);
267  if (matchesInEtaRange.size() >= 2 && j == matchesInEtaRange[1])
268  elements_[pre + "MaxPt2" + post]->Fill(pt);
269  if (pt > cutMinPt_) {
270  elements_[pre + "Eta" + post]->Fill(eta);
271  if (fabs(eta) < cutMaxEta_)
272  elements_[pre + "Phi" + post]->Fill(phi);
273  }
274  }
275  }
276 
277  }
278 
279 
280 
281  } // End loop over sources
282 
283 }
284 
285 
286 
287 void
289  vector<MatchStruct> & matches,
290  const std::vector<L1MuonParticleRef>& candsL1,
291  const std::vector< vector< const RecoChargedCandidate *> >& candsHlt)
292 {
293 
294  set<size_t>::iterator it;
295 
296  set<size_t> indicesL1;
297  for (size_t i = 0; i < candsL1.size(); i++)
298  indicesL1.insert(i);
299 
300  vector< set<size_t> > indicesHlt(candsHlt.size());
301  for (size_t i = 0; i < candsHlt.size(); i++)
302  for (size_t j = 0; j < candsHlt[i].size(); j++)
303  indicesHlt[i].insert(j);
304 
305  for (size_t i = 0; i < matches.size(); i++) {
306 
307  const Candidate * cand = matches[i].candBase;
308 
309  double bestDeltaR = cutsDr_[0];
310  size_t bestMatch = kNull;
311  for (it = indicesL1.begin(); it != indicesL1.end(); it++) {
312  if (candsL1[*it].isAvailable()) {
313  double dR = deltaR(cand->eta(), cand->phi(),
314  candsL1[*it]->eta(), candsL1[*it]->phi());
315  if (dR < bestDeltaR) {
316  bestMatch = *it;
317  bestDeltaR = dR;
318  }
319  // TrajectoryStateOnSurface propagated;
320  // float dR = 999., dPhi = 999.;
321  // bool isValid = l1Matcher_.match(* cand, * candsL1[*it],
322  // dR, dPhi, propagated);
323  // if (isValid && dR < bestDeltaR) {
324  // bestMatch = *it;
325  // bestDeltaR = dR;
326  // }
327  } else {
328  LogWarning("HLTMuonPlotter")
329  << "Ref candsL1[*it]: product not available "
330  << *it;
331  }
332  }
333  if (bestMatch != kNull)
334  matches[i].candL1 = & * candsL1[bestMatch];
335  indicesL1.erase(bestMatch);
336 
337  matches[i].candHlt.assign(candsHlt.size(), 0);
338  for (size_t j = 0; j < candsHlt.size(); j++) {
339  size_t level = (candsHlt.size() == 4) ? (j < 2) ? 2 : 3 :
340  (candsHlt.size() == 2) ? (j < 1) ? 2 : 3 :
341  2;
342  bestDeltaR = cutsDr_[level - 2];
343  bestMatch = kNull;
344  for (it = indicesHlt[j].begin(); it != indicesHlt[j].end(); it++) {
345  double dR = deltaR(cand->eta(), cand->phi(),
346  candsHlt[j][*it]->eta(), candsHlt[j][*it]->phi());
347  if (dR < bestDeltaR) {
348  bestMatch = *it;
349  bestDeltaR = dR;
350  }
351  }
352  if (bestMatch != kNull)
353  matches[i].candHlt[j] = candsHlt[j][bestMatch];
354  indicesHlt[j].erase(bestMatch);
355  }
356 
357 // cout << " Muon: " << cand->eta() << ", ";
358 // if (matches[i].candL1) cout << matches[i].candL1->eta() << ", ";
359 // else cout << "none, ";
360 // for (size_t j = 0; j < candsHlt.size(); j++)
361 // if (matches[i].candHlt[j]) cout << matches[i].candHlt[j]->eta() << ", ";
362 // else cout << "none, ";
363 // cout << endl;
364 
365  }
366 
367 }
368 
369 
370 
371 
372 void
374  string source, string type)
375 {
376 
377  string sourceUpper = source;
378  sourceUpper[0] = toupper(sourceUpper[0]);
379  string name = source + "Pass" + type + "_" + label;
380  TH1F * h;
381 
382  if (type.find("MaxPt") != string::npos) {
383  string desc = (type == "MaxPt1") ? "Leading" : "Next-to-Leading";
384  string title = "pT of " + desc + " " + sourceUpper + " Muon "+
385  "matched to " + label;
386  const size_t nBins = parametersTurnOn_.size() - 1;
387  float * edges = new float[nBins + 1];
388  for (size_t i = 0; i < nBins + 1; i++) edges[i] = parametersTurnOn_[i];
389  h = new TH1F(name.c_str(), title.c_str(), nBins, edges);
390  }
391 
392  else {
393  string symbol = (type == "Eta") ? "#eta" : "#phi";
394  string title = symbol + " of " + sourceUpper + " Muons " +
395  "matched to " + label;
396  vector<double> params = (type == "Eta") ? parametersEta_ : parametersPhi_;
397  int nBins = (int)params[0];
398  double min = params[1];
399  double max = params[2];
400  h = new TH1F(name.c_str(), title.c_str(), nBins, min, max);
401  }
402 
403  h->Sumw2();
404  elements_[name] = dbe_->book1D(name, h);
405  delete h;
406 
407 }
408 
type
Definition: HCALResponse.h:21
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...
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:722
enum start value shifted to 81 so as to avoid clashes with PDG codes
virtual float eta() const =0
momentum pseudorapidity
std::string hltPath_
std::vector< double > parametersEta_
virtual float phi() const =0
momentum azimuthal angle
#define min(a, b)
Definition: mlp_lapack.h:161
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::string genParticleLabel_
std::vector< std::string > moduleLabels_
MonitorElement * bookFloat(const char *name)
Book float.
Definition: DQMStore.cc:659
void beginRun(const edm::Run &, const edm::EventSetup &)
std::string recMuonCut_
int iEvent
Definition: GenABIO.cc:243
vector< ParameterSet > Parameters
StringCutObjectSelector< reco::Muon > * recMuonSelector_
const T & max(const T &a, const T &b)
std::vector< double > cutsDr_
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
const unsigned int kNull
void bookHist(std::string, std::string, std::string, std::string)
int j
Definition: DBlmapReader.cc:9
void analyze(const edm::Event &, const edm::EventSetup &)
void setVerbose(unsigned level)
Definition: DQMStore.cc:398
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1473
bool isValid() const
Definition: HandleBase.h:76
StringCutObjectSelector< reco::GenParticle > * genMuonSelector_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:361
#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:80
void findMatches(std::vector< MatchStruct > &, const std::vector< l1extra::L1MuonParticleRef > &, const 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
std::string recMuonLabel_
#define begin
Definition: vmac.h:31
std::vector< double > parametersPhi_
HLTMuonPlotter(const edm::ParameterSet &, std::string, const std::vector< std::string > &, const std::vector< std::string > &)
tuple level
Definition: testEve_cfg.py:34
std::map< std::string, MonitorElement * > elements_
std::string hltProcessName_
std::vector< double > parametersTurnOn_
tuple size
Write out results.
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:434
Definition: Run.h:36
DQMStore * dbe_
Definition: DDAxes.h:10