DESY Hbb Analysis Framework
MuonAnalyser.cc
Go to the documentation of this file.
1 // system include files
2 #include "boost/program_options.hpp"
3 #include "boost/algorithm/string.hpp"
4 #include <string>
5 #include <iostream>
6 #include <fstream>
7 #include <vector>
8 //
9 // user include files
11 
12 //
13 // class declaration
14 //
15 
16 using namespace analysis;
17 using namespace analysis::tools;
18 
20 {
21 }
22 
23 
24 MuonAnalyser::MuonAnalyser(int argc, char * argv[]) : BaseAnalyser(argc,argv)
25 {
26  // Physics objects
27  // Muons
28  muonsanalysis_ = ( analysis_->addTree<Muon> ("Muons",config_->muonsCollection()) != nullptr && config_ -> nMuonsMin() > 0 );
29 
30 }
31 
33 {
34  // do anything here that needs to be done at desctruction time
35  // (e.g. close files, deallocate resources etc.)
36 }
37 
38 
39 //
40 // member functions
41 //
42 // ------------ method called for each event ------------
43 
45 {
46  muons_.clear();
47  selectedMuons_.clear();
48  onlineMatchedMuons_.clear();
49 
50  // trigger emulation
51  // L1 muons
52  std::string triggerObjectsL1Muons;
53  if ( config_->triggerObjectsL1Muons() != "" )
54  {
55  triggerObjectsL1Muons = config_->triggerObjectsL1Muons();
56  if ( config_->triggerEmulateL1Muons() != "" && config_->triggerEmulateL1MuonsNMin() > 0 )
57  {
58  int nmin = config_->triggerEmulateL1MuonsNMin();
59  float ptmin = config_->triggerEmulateL1MuonsPtMin();
60  float etamax = config_->triggerEmulateL1MuonsEtaMax();
61  std::string newL1Muons = config_->triggerEmulateL1Muons();
62  triggerEmulation(triggerObjectsL1Muons,nmin,ptmin,etamax,newL1Muons);
63  triggerObjectsL1Muons = newL1Muons;
64  }
65  }
66 
67  // L3 muons
68  std::string triggerObjectsL3Muons;
69  if ( config_->triggerObjectsL3Muons() != "" )
70  {
71  triggerObjectsL3Muons = config_->triggerObjectsL3Muons();
72  if ( config_->triggerEmulateL3Muons() != "" && config_->triggerEmulateL3MuonsNMin() > 0 )
73  {
74  int nmin = config_->triggerEmulateL3MuonsNMin();
75  float ptmin = config_->triggerEmulateL3MuonsPtMin();
76  float etamax = config_->triggerEmulateL3MuonsEtaMax();
77  std::string newL3Muons = config_->triggerEmulateL3Muons();
78  triggerEmulation(triggerObjectsL3Muons,nmin,ptmin,etamax,newL3Muons);
79  triggerObjectsL3Muons = newL3Muons;
80  }
81  }
82 
83 
84  if ( ! muonsanalysis_ ) return false;
85 
86  ++cutflow_;
87  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
88  {
89  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Using Muon collection: %s",(config_->muonsCollection()).c_str()));
90  }
91  h1_["cutflow"] -> Fill(cutflow_,weight_);
92 
93 
94  if ( config_->triggerObjectsL1Muons() != "" )
95  {
96  analysis_->match<Muon,TriggerObject>("Muons",triggerObjectsL1Muons,config_->triggerMatchL1MuonsDrMax());
97 
98  }
99 
100  if ( config_->triggerObjectsL3Muons() != "" )
101  {
102  analysis_->match<Muon,TriggerObject>("Muons",triggerObjectsL3Muons,config_->triggerMatchL3MuonsDrMax());
103  }
104 
105  auto muons = analysis_->collection<Muon>("Muons");
106  for ( int j = 0 ; j < muons->size() ; ++j ) muons_.push_back(std::make_shared<Muon>(muons->at(j)));
107 
109 
110  return true;
111 }
112 
113 
114 std::vector< std::shared_ptr<Muon> > MuonAnalyser::muons()
115 {
116  return muons_;
117 }
118 
119 std::vector< std::shared_ptr<Muon> > MuonAnalyser::selectedMuons()
120 {
121  return selectedMuons_;
122 }
123 
124 std::vector< std::shared_ptr<Muon> > MuonAnalyser::onlineMatchedMuons()
125 {
126  return onlineMatchedMuons_;
127 }
128 
129 
130 
131 
132 // FIXME: need to pass label to the histograms
133 void MuonAnalyser::muonHistograms(const std::string & obj, const int & n)
134 {
135  if ( obj == "muon" )
136  {
137  for ( int j = 0; j < n; ++j )
138  {
139  h1_[Form("pt_%s%d" , obj.c_str(),j+1)] = std::make_shared<TH1F>(Form("pt_%s%d" , obj.c_str(),j+1) , "" ,100 , 0 , 1000 );
140  h1_[Form("eta_%s%d" , obj.c_str(),j+1)] = std::make_shared<TH1F>(Form("eta_%s%d" , obj.c_str(),j+1) , "" , 60 , -3, 3 );
141  h1_[Form("phi_%s%d" , obj.c_str(),j+1)] = std::make_shared<TH1F>(Form("phi_%s%d" , obj.c_str(),j+1) , "" , 64 , -3.2, 3.2 );
142  }
143 
144  }
145 }
146 
147 
148 bool MuonAnalyser::selectionMuon(const int & r)
149 {
150  bool isgood = true;
151  ++cutflow_;
152  int m = r-1;
153 
154  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
155  {
156  if ( config_->muonsPtMax().size() > 0 && config_->muonsPtMax()[m] > config_->muonsPtMin()[m] )
157  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muon %d: pt > %5.1f GeV and pt < %5.1f GeV and |eta| < %3.1f",r,config_->muonsPtMin()[m], config_->muonsPtMax()[m],config_->muonsEtaMax()[m] ));
158  else
159  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muon %d: pt > %5.1f GeV and |eta| < %3.1f",r,config_->muonsPtMin()[m], config_->muonsEtaMax()[m] ));
160  }
161 
162  // kinematic selection
163  if ( selectedMuons_[m] -> pt() < config_->muonsPtMin()[m] && !(config_->muonsPtMin()[m] < 0) ) return false;
164  if ( fabs(selectedMuons_[m] -> eta()) > config_->muonsEtaMax()[m] && !(config_->muonsEtaMax()[m] < 0) ) return false;
165 
166  h1_["cutflow"] -> Fill(cutflow_,weight_);
167 
168  return isgood;
169 }
170 
172 {
173  // selectedMuons will be composed of muons with the lowest pt threshold
174 
175  if ( ! muonsanalysis_ ) return true; // will skip this selection
176 
177 
178  bool isgood = true;
179  ++cutflow_;
180 //
181  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
182  {
183  if ( config_->muonsPtMax().size() > 0 && config_->muonsPtMax().back() > config_->muonsPtMin().back() )
184  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muons selected: pt > %5.1f GeV and pt < %5.1f GeV and |eta| < %3.1f", config_->muonsPtMin().back(), config_->muonsPtMax().back(), config_->muonsEtaMax().back() ));
185  else
186  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muons selected: pt > %5.1f GeV and |eta| < %3.1f", config_->muonsPtMin().back(), config_->muonsEtaMax().back() ));
187  }
188 
189  // kinematic selection
190  auto muon = std::begin(selectedMuons_);
191  while ( muon != std::end(selectedMuons_) )
192  {
193  if ( config_->muonsPtMax().size() > 0 && config_->muonsPtMax().back() > config_->muonsPtMin().back() )
194  {
195  if ( (*muon)->pt() < config_->muonsPtMin().back() || (*muon)->pt() > config_->muonsPtMax().back() || fabs((*muon)->eta()) > config_->muonsEtaMax().back() )
196  muon = selectedMuons_.erase(muon);
197  else
198  ++muon;
199  }
200  else
201  {
202  if ( (*muon)->pt() < config_->muonsPtMin().back() || fabs((*muon)->eta()) > config_->muonsEtaMax().back() )
203  muon = selectedMuons_.erase(muon);
204  else
205  ++muon;
206  }
207  }
208  if ( (int)selectedMuons_.size() < config_->nMuonsMin() ) return false;
209 
210  h1_["cutflow"] -> Fill(cutflow_,weight_);
211 //
212  return isgood;
213 }
214 
215 
216 
218 {
219  if ( ! muonsanalysis_ ) return true; // will skip this selection
220 
221  ++cutflow_;
222 
223  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
224  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("MuonId: %s",config_->muonsId().c_str()));
225 
226 // if ( ! muonsanalysis_ ) return false;
227 
228  auto muon = std::begin(selectedMuons_);
229  while ( muon != std::end(selectedMuons_) )
230  {
231  if ( ! (*muon)->id(config_->muonsId() ) )
232  muon = selectedMuons_.erase(muon);
233  else
234  ++muon;
235  }
236  if ( selectedMuons_.size() == 0 ) return false;
237 
238 
239  h1_["cutflow"] -> Fill(cutflow_,weight_);
240 
241  return true;
242 }
243 
245 {
246  if ( ! muonsanalysis_ ) return true; // will skip this selection
247 
248 
249  ++cutflow_;
250 
251  if ((int)selectedMuons_.size() < config_->nMuonsMin()) return false;
252 
253  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
254  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("NMuons >= %d",config_->nMuonsMin()));
255 
256  h1_["cutflow"] -> Fill(cutflow_,weight_);
257 
258  return true;
259 
260 }
261 bool MuonAnalyser::selectionMuonDr(const int & r1, const int & r2, const float & delta)
262 {
263  if ( r1 > config_->nMuonsMin() || r2 > config_->nMuonsMin() ) return true;
264 
265  bool isgood = true;
266 
267  std::string label = Form("DR(muon %d, muon %d) < %4.2f",r1,r2,fabs(delta));
268  if ( delta < 0 )
269  label = Form("DR(muon %d, muon %d) > %4.2f",r1,r2,fabs(delta));
270 
271  int m1 = r1-1;
272  int m2 = r2-1;
273 
274  if ( delta > 0 )
275  isgood = ( selectedMuons_[m1]->deltaR(*selectedMuons_[m2]) < fabs(delta) );
276  else
277  isgood = ( selectedMuons_[m1]->deltaR(*selectedMuons_[m2]) > fabs(delta) );
278 
279  cutflow(label,isgood);
280 
281  return isgood;
282 
283 }
284 
285 bool MuonAnalyser::selectionMuonDr(const int & r1, const int & r2)
286 {
287  bool ok = true;
288  if (config_->muonsDrMax() < 0 )
289  {
290  ok = ok && true;
291  }
292  else
293  {
294  ok = ok && selectionMuonDr(r1,r2,config_->muonsDrMax());
295  }
296 
297  if (config_->muonsDrMin() < 0 )
298  {
299  ok = ok && true;
300  }
301  else
302  {
303  ok = ok && selectionMuonDr(r1,r2,-1*config_->muonsDrMin());
304  }
305  return ok;
306 }
307 
308 
310 {
311  if ( ! muonsanalysis_ ) return true; // will skip this selection
312 
313  if ( config_->triggerObjectsL1Muons() == "" && config_->triggerObjectsL3Muons() == "" ) return true;
314 
315  ++cutflow_;
316  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
317  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muons selected: Online muon matching: L1 (deltaR < %4.3f) and L3 (deltaR < %4.3f)",config_-> triggerMatchL1MuonsDrMax(),config_-> triggerMatchL3MuonsDrMax()));
318 
319  auto muon = std::begin(selectedMuons_);
320  while ( muon != std::end(selectedMuons_) )
321  {
322  if ( !((*muon)->matched(config_->triggerObjectsL1Muons()) && (*muon)->matched(config_->triggerObjectsL3Muons()) ))
323  muon = selectedMuons_.erase(muon);
324  else
325  ++muon;
326  }
327 
328  if ( (int)selectedMuons_.size() < config_->nMuonsMin() ) return false;
329 
330  h1_["cutflow"] -> Fill(cutflow_,weight_);
331 
332  return true;
333 }
334 
336 {
337  int j = r-1;
338  if ( config_->triggerObjectsL1Muons() == "" ) return true;
339 
340  ++cutflow_;
341 
342  std::string triggerObjectsL1Muons = config_->triggerObjectsL1Muons();
343  if ( config_->triggerEmulateL1Muons() != "" && config_->triggerEmulateL1MuonsNMin() > 0 )
344  {
345  triggerObjectsL1Muons = config_->triggerEmulateL1Muons();
346  }
347 
348 
349  if ( r > config_->nMuonsMin() )
350  {
351  std::cout << "*Warning* MuonAnalyser::onlineL1MuonMatching(): asking for matching of unselected muon. Returning false!" << std::endl;
352  return false; // asking for a match beyond the selection, that's wrong, therefore false
353  }
354  if ( selectedMuons_.size() == 0 )
355  {
356  std::cout << "*Warning* MuonAnalyser::onlineL1MuonMatching(): selectedMuons is empty. Returning false!" << std::endl;
357  return false; // asking for a match beyond the selection, that's wrong, therefore false
358  }
359 
360  if ( ! selectedMuons_[j]->matched(triggerObjectsL1Muons) ) return false;
361 
362  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
363  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muon %d: L1 muon match (deltaR < %4.3f)",r,config_-> triggerMatchL1MuonsDrMax()));
364 
365  h1_["cutflow"] -> Fill(cutflow_,weight_);
366 
367  return true;
368 }
369 
371 {
372  int j = r-1;
373  if ( config_->triggerObjectsL3Muons() == "" ) return true;
374 
375  ++cutflow_;
376 
377  std::string triggerObjectsL3Muons = config_->triggerObjectsL3Muons();
378  if ( config_->triggerEmulateL3Muons() != "" && config_->triggerEmulateL3MuonsNMin() > 0 )
379  {
380  triggerObjectsL3Muons = config_->triggerEmulateL3Muons();
381  }
382 
383  if ( r > config_->nMuonsMin() )
384  {
385  std::cout << "*Warning* MuonAnalyser::onlineL3MuonMatching(): asking for matching of unselected muon. Returning false!" << std::endl;
386  return false; // asking for a match beyond the selection, that's wrong, therefore false
387  }
388  if ( selectedMuons_.size() == 0 )
389  {
390  std::cout << "*Warning* MuonAnalyser::onlineL3MuonMatching(): selectedMuons is empty. Returning false!" << std::endl;
391  return false; // asking for a match beyond the selection, that's wrong, therefore false
392  }
393 
394  if ( ! selectedMuons_[j]->matched(triggerObjectsL3Muons) ) return false;
395 
396  if ( std::string(h1_["cutflow"] -> GetXaxis()-> GetBinLabel(cutflow_+1)) == "" )
397  h1_["cutflow"] -> GetXaxis()-> SetBinLabel(cutflow_+1,Form("Muon %d: L3 muon match (deltaR < %4.3f)",r,config_-> triggerMatchL3MuonsDrMax()));
398 
399  h1_["cutflow"] -> Fill(cutflow_,weight_);
400 
401  return true;
402 }
403 
404 
405 // FIXME: need to pass label to the histograms
407 {
408  int n = config_->nMuonsMin();
409 
410  for ( int j = 0; j < n; ++j )
411  {
412  h1_[Form("pt_muon%d",j+1)] -> Fill(selectedMuons_[j]->pt());
413  h1_[Form("eta_muon%d",j+1)] -> Fill(selectedMuons_[j]->eta());
414  h1_[Form("phi_muon%d",j+1)] -> Fill(selectedMuons_[j]->phi());
415  }
416 
417 }
int cutflow()
get cutflow index
virtual bool onlineL1MuonMatching(const int &)
virtual bool selectionMuonDr(const int &r1, const int &r2)
Given the rankings r1 and r2 of two jets, it returns whether the jets passes the delta_R selection; t...
std::vector< std::shared_ptr< Muon > > muons_
Definition: MuonAnalyser.h:46
std::vector< std::shared_ptr< Muon > > muons()
std::shared_ptr< Config > config_
Config objects.
Definition: BaseAnalyser.h:63
virtual bool onlineL3MuonMatching(const int &)
std::shared_ptr< Analysis > analysis_
Analysis objects.
Definition: BaseAnalyser.h:61
std::vector< std::shared_ptr< Muon > > onlineMatchedMuons_
Definition: MuonAnalyser.h:48
std::vector< std::shared_ptr< Muon > > selectedMuons()
float weight_
event weight
Definition: BaseAnalyser.h:72
std::vector< std::shared_ptr< Muon > > selectedMuons_
Definition: MuonAnalyser.h:47
std::vector< std::shared_ptr< Muon > > onlineMatchedMuons()
std::map< std::string, std::shared_ptr< TH1F > > h1_
1D histograms mapping
Definition: BaseAnalyser.h:80
int cutflow_
Cutflow index.
Definition: BaseAnalyser.h:66
virtual bool selectionMuon(const int &)
virtual bool analysisWithMuons()
Definition: MuonAnalyser.cc:44
virtual bool triggerEmulation(const std::string &, const int &, const float &, const float &, const std::string &)
emulate l1 trigger
virtual void muonHistograms(const std::string &, const int &n=1)