DESY Hbb Analysis Framework
Analysis.h
Go to the documentation of this file.
1 #ifndef Analysis_Tools_Analysis_h
2 #define Analysis_Tools_Analysis_h 1
3 
4 // -*- C++ -*-
5 //
6 // Package: Analysis/Tools
7 // Class: Analysis
8 //
16 //
17 // Original Author: Roberval Walsh Bastos Rangel
18 // Created: Mon, 20 Oct 2014 14:24:08 GMT
19 //
20 //
21 
22 // system include files
23 #include <memory>
24 #include <vector>
25 #include <string>
26 #include <typeinfo>
27 #include <any>
28 #include <boost/core/demangle.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include "stdlib.h"
31 //
32 // user include files
33 
34 #include "TChain.h"
35 #include "TFile.h"
36 #include "TFileCollection.h"
37 #include "TH2.h"
38 
40 
46 
47 
48 //#include "CondFormats/JetMETObjects/interface/JetCorrectorParameters.h"
49 //#include "CondFormats/JetMETObjects/interface/JetCorrectionUncertainty.h"
50 
51 //
52 // class declaration
53 //
54 
55 using namespace JME;
56 
57 namespace analysis {
58  namespace tools {
59 
60  class Analysis {
61  public:
62  Analysis(const std::string & inputFilelist, const std::string & evtinfo = "MssmHbb/Events/EventInfo");
63  ~Analysis();
64 
65  // Info
66  void tag(const std::string &);
67  std::string tag();
68 
70  int seed(const std::string &);
71 
72  // Event
73  int numberEvents();
74  int size();
75  void event(const int & event, const bool & addCollections = true);
76  int event();
77  int run();
78  int lumiSection();
79  bool isMC();
80  std::string fileName();
81  std::string fileFullName();
82 
83  // PileupInfo
84  int nPileup();
85  float nTruePileup();
86 
87  float lumiPileup();
88  float instantLumi();
89 
90  // GenEventInfo
91  double genWeight();
92  double genScale();
93  PDF pdf();
94 
96  double rho();
97 
98 
99  // Trees
100  template<class Object>
101  std::shared_ptr< PhysicsObjectTree<Object> > addTree(const std::string & unique_name, const std::string & path );
102  template<class Object>
103  std::shared_ptr< PhysicsObjectTree<Object> > tree(const std::string & unique_name);
104 
105  // Collections
106  template<class Object>
107  std::shared_ptr< Collection<Object> > addCollection(const std::string & unique_name);
108  template<class Object>
109  std::shared_ptr< Collection<Object> > addCollection(const Collection<Object> & collection);
110  template<class Object>
111  std::shared_ptr< Collection<Object> > addCollection(const std::vector<Object> & objects, const std::string & unique_name );
112  template<class Object>
113  std::shared_ptr< Collection<Object> > collection(const std::string & unique_name);
114 
115  template<class Object>
116  void defaultCollection(const std::string & unique_name);
117  template<class Object>
118  std::string defaultCollection();
119 
120  // Cross sections
121  int crossSections(const std::string & path);
122  double crossSection();
123  double crossSection(const std::string & title);
124  void listCrossSections();
125 
126  // Luminosity
127  double luminosity();
128  double luminosity(const std::string & title);
129 
130  // Trigger results
131  bool triggerResults(const std::string & path);
132  bool triggerResult(const std::string & trig);
133  int triggerPrescale(const std::string & trig);
134  std::map<std::string,int> triggerPrescale(const std::vector<std::string> & trigs);
135 
136  // Generator Filter
137  FilterResults generatorFilter(const std::string & path);
138  void listGeneratorFilter();
139 
140  // Event Filter
141  FilterResults eventFilter(const std::string & path);
142 
143  // Matching to trigger objects
144  template <class Object1, class Object2>
145  void match(const std::string & collection, const std::string & match_collection, const float & deltaR = 0.5);
146  template <class Object1, class Object2>
147  void match(const std::string & collection, const std::vector<std::string> & match_collections, const float & deltaR = 0.5);
148 
149  // good Json files
150  int processJsonFile(const std::string & fileName = "goodJson.txt");
151  bool selectJson();
152 
153  // btag efficiencies
154  void addBtagEfficiencies(const std::string & );
155  float btagEfficiency(const analysis::tools::Jet &, const int & rank = 0);
156  void btagEfficienciesAlgo(const std::string & );
157  void btagEfficienciesFlavour(const std::string & );
158 
159  std::shared_ptr<JetResolutionInfo> jetResolutionInfo(const std::string &, const std::string & );
160 
161  std::shared_ptr<BTagCalibrationReader> btagCalibration(const std::string & tagger,
162  const std::string & filename,
163  const std::string & wp,
164  const std::string & sysType="central",
165  const std::vector<std::string> & otherSysTypes={"up", "down"});
166 
167 
168  std::shared_ptr<BTagCalibrationReader> btagCalibration();
169 
170  float scaleLuminosity(const float & lumi); // in pb-1
171 
172  std::shared_ptr<PileupWeight> pileupWeights(const std::string & );
173  std::shared_ptr<MuonIdWeight> muonIDWeights(const std::string & );
174 
175  // ----------member data ---------------------------
176  protected:
177 
178  TFileCollection * fileCollection_;
179  TCollection * fileList_;
180  std::string inputFilelist_;
181 
182  // Info
183  std::string tag_;
184 
185  // btagging efficiencies
186  TFile * fileBtagEff_;
187  std::map<std::string,TH2F *> h2_btageff_;
188  std::string btageff_flavour_;
189  std::string btageff_algo_;
190 
191  std::shared_ptr<BTagCalibration> btagcalib_;
192  std::shared_ptr<BTagCalibrationReader> btagcalibread_;
193 
194  std::shared_ptr<JetResolutionInfo> jerinfo_;
195 
196  // pileup weight
197  std::shared_ptr<PileupWeight> puweights_;
198 
199  // muonID weight
200  std::shared_ptr<MuonIdWeight> muonIDweights_;
201 
202 
203  std::map<std::string, double> xsections_;
204  std::map<std::string, bool> triggerResults_;
205  std::map<std::string, int> triggerResultsPS_;
206  std::map<int,std::vector<std::string> > goodLumi_;
209 
210  // default collections
211  std::string defaultGenParticle_;
212 
213  int event_;
214  int run_;
215  int lumi_;
216  bool is_mc_;
217 
218  int n_pu_;
219  float n_true_pu_;
220 
221  float lumi_pu_;
222  float inst_lumi_;
223 
224  double genWeight_;
225  double genScale_;
227 
228  double rho_;
229 
230  int nevents_;
231 
232  // TREES
233  void treeInit_(const std::string & unique_name, const std::string & path);
234  TChain * t_xsection_;
235  TChain * t_genfilter_;
236  TChain * t_evtfilter_;
237  TChain * t_event_;
239 
240  // Physics objects
241  // root trees
242  std::map<std::string, TChain*> tree_;
243 
244  // Framework trees and types
245  std::map<std::string, std::any > t_any_;
246  std::map<std::string, std::string > t_type_;
247 
248  // Collections
249  std::map<std::string, std::any > c_any_;
250 
251  // Luminosity
252  float mylumi_;
253 
254  // json
255  std::map<int,std::vector<int> > json_;
256 
257  private:
258 
259 
260  }; // END OF CLASS DECLARATIONS!!!
261 
262 // ========================================================
263 // IMPLEMENTATIONS!
264 // ========================================================
265 
266 // +++++++++++++++++++++++ IMPORTANT ++++++++++++++++++++++
267 // Need to put the implementations in the header file when using template!!!
268 // otherwise would have to specialize, like the other functions.
269 // This explains the problems I have been getting.
270 // -------------------------------------------------------
271  // TREES
272  template <class Object>
273  std::shared_ptr< PhysicsObjectTree<Object> > Analysis::addTree(const std::string & unique_name, const std::string & path)
274  {
275  if ( path == "" || unique_name == "" ) return nullptr;
276  this->treeInit_(unique_name,path);
277  t_any_[unique_name] = std::shared_ptr< PhysicsObjectTree<Object> > ( new PhysicsObjectTree<Object>(tree_[unique_name], unique_name) );
278  std::string type = boost::core::demangle(typeid(Object).name());
279  std::vector<std::string> tmp;
280  boost::split( tmp, type, boost::is_any_of("::"));
281  t_type_[unique_name] = tmp.back();
282  return std::any_cast< std::shared_ptr< PhysicsObjectTree<Object> > > (t_any_[unique_name]);
283  }
284  // --
285  template <class Object>
286  std::shared_ptr< PhysicsObjectTree<Object> > Analysis::tree(const std::string & unique_name)
287  {
288  // If tree does not exist, return NULL
289  std::map<std::string, std::any >::iterator it = t_any_.find(unique_name);
290  if ( it == t_any_.end() )
291  return nullptr;
292  return std::any_cast< std::shared_ptr< PhysicsObjectTree<Object> > > (t_any_[unique_name]);
293  }
294 // -------------------------------------------------------
295  // COLLECTIONS
296  template <class Object>
297  std::shared_ptr< Collection<Object> > Analysis::addCollection(const std::string & unique_name)
298  {
299  // Still need to see how to deal with collections not originating from the ntuple,
300  // e.g. a selected jets collection from the ntuple jets collection.
301 
302  // If tree does not exist, return NULL
303  std::map<std::string, std::any >::iterator it = t_any_.find(unique_name);
304  if ( it == t_any_.end() )
305  return nullptr;
306 
307  auto tree = std::any_cast< std::shared_ptr< PhysicsObjectTree<Object> > > (t_any_[unique_name]);
308  c_any_[unique_name] = std::shared_ptr< Collection<Object> > ( new Collection<Object>(tree -> collection()));
309  std::shared_ptr< Collection<Object> > ret = std::any_cast< std::shared_ptr< Collection<Object> > > (c_any_[unique_name]);
310 
311  return ret;
312  }
313 
314  template <class Object>
315  std::shared_ptr< Collection<Object> > Analysis::addCollection(const Collection<Object> & collection)
316  {
317  std::string unique_name = collection.name();
318  t_any_[unique_name] = nullptr;
319  c_any_[unique_name] = std::shared_ptr< Collection<Object> > ( new Collection<Object>(collection) );
320  std::shared_ptr< Collection<Object> > ret = std::any_cast< std::shared_ptr< Collection<Object> > > (c_any_[unique_name]);
321 
322  return ret;
323  }
324 
325  template <class Object>
326  std::shared_ptr< Collection<Object> > Analysis::addCollection(const std::vector<Object> & objects , const std::string & unique_name )
327  {
328  Collection<Object> collection(objects,unique_name);
329  t_any_[unique_name] = nullptr;
330  c_any_[unique_name] = std::shared_ptr< Collection<Object> > ( new Collection<Object>(collection) );
331  std::shared_ptr< Collection<Object> > ret = std::any_cast< std::shared_ptr< Collection<Object> > > (c_any_[unique_name]);
332  return ret;
333  }
334 
335  template <class Object>
336  std::shared_ptr< Collection<Object> > Analysis::collection(const std::string & unique_name)
337  {
338  std::shared_ptr< Collection<Object> > ret = std::any_cast< std::shared_ptr< Collection<Object> > > (c_any_[unique_name]);
339  return ret;
340  }
341  //--
342  template <class Object1, class Object2>
343  void Analysis::match(const std::string & collection, const std::string & match_collection, const float & deltaR)
344  {
345  if ( match_collection == "" ) return;
346  auto o1 = std::any_cast< std::shared_ptr< Collection<Object1> > > (c_any_[collection]);
347  auto o2 = std::any_cast< std::shared_ptr< Collection<Object2> > > (c_any_[match_collection]);
348  o1->matchTo(o2->vectorCandidates(),o2->name(), deltaR);
349  }
350  //--
351  template <class Object1, class Object2>
352  void Analysis::match(const std::string & collection, const std::vector<std::string> & match_collections, const float & deltaR)
353  {
354  for ( auto & mc : match_collections )
355  this->match<Object1,Object2>(collection, mc, deltaR);
356  }
357 
358  template<class Object> void Analysis::defaultCollection(const std::string & unique_name)
359  {
360  if ( std::is_same<Object,GenParticle>::value ) defaultGenParticle_ = unique_name;
361  }
362 
363  template<class Object> std::string Analysis::defaultCollection()
364  {
365  std::string ret;
366  if ( std::is_same<Object,GenParticle>::value ) ret = defaultGenParticle_ ;
367  return ret;
368  }
369 
370 // ========================================================
371  inline void Analysis::tag(const std::string & t) { std::cout << "Tag " << t << " has been defined." << std::endl ;
372  tag_ = t; }
373  inline std::string Analysis::tag() { return tag_ ; }
374 
375 
376  inline int Analysis::numberEvents() { return nevents_; }
377  inline int Analysis::size() { return nevents_; }
378  inline int Analysis::event() { return event_; }
379  inline int Analysis::run() { return run_ ; }
380  inline int Analysis::lumiSection() { return lumi_ ; }
381  inline bool Analysis::isMC() { return is_mc_ ; }
382 
383  inline int Analysis::nPileup() { return n_pu_; }
384  inline float Analysis::nTruePileup() { return n_true_pu_; }
385 
386  inline float Analysis::lumiPileup() { return lumi_pu_; }
387  inline float Analysis::instantLumi() { return inst_lumi_;}
388 
389  inline double Analysis::genWeight() { return genWeight_; }
390  inline double Analysis::genScale() { return genScale_; }
391  inline PDF Analysis::pdf() { return pdf_; }
392  inline double Analysis::rho() { return rho_; }
393 
394  inline void Analysis::btagEfficienciesAlgo(const std::string & algo ) { btageff_algo_ = algo; }
395  inline void Analysis::btagEfficienciesFlavour(const std::string & flavour) { btageff_flavour_ = flavour; }
396 
397  inline std::string Analysis::fileFullName() { return std::string(t_event_ -> GetFile() -> GetName()) ; }
398 
399 // inline std::string Analysis::getGenParticleCollection() { return genParticleCollection_; }
400 
401  }
402 }
403 
404 #endif // Analysis_Tools_Analysis_h
std::shared_ptr< PileupWeight > puweights_
Definition: Analysis.h:197
std::map< std::string, double > xsections_
Definition: Analysis.h:203
std::string defaultGenParticle_
Definition: Analysis.h:211
std::shared_ptr< JetResolutionInfo > jerinfo_
Definition: Analysis.h:194
std::shared_ptr< BTagCalibrationReader > btagcalibread_
Definition: Analysis.h:192
std::map< std::string, TH2F * > h2_btageff_
Definition: Analysis.h:187
std::map< std::string, std::string > t_type_
Definition: Analysis.h:246
std::shared_ptr< MuonIdWeight > muonIDweights_
Definition: Analysis.h:200
std::map< std::string, TChain * > tree_
Definition: Analysis.h:242
std::map< std::string, std::any > c_any_
Definition: Analysis.h:249
std::string inputFilelist_
Definition: Analysis.h:180
std::string name() const
Definition: Collection.h:89
std::map< int, std::vector< std::string > > goodLumi_
Definition: Analysis.h:206
std::map< int, std::vector< int > > json_
Definition: Analysis.h:255
std::shared_ptr< BTagCalibration > btagcalib_
Definition: Analysis.h:191
std::string btageff_algo_
Definition: Analysis.h:189
std::string btageff_flavour_
Definition: Analysis.h:188
int lumi_
Definition: Ntuple.h:25
FilterResults evtfilter_
Definition: Analysis.h:208
TCollection * fileList_
Definition: Analysis.h:179
std::map< std::string, int > triggerResultsPS_
Definition: Analysis.h:205
std::map< std::string, std::any > t_any_
Definition: Analysis.h:245
int event_
Definition: Ntuple.h:26
int run_
Definition: Ntuple.h:24
TFileCollection * fileCollection_
Definition: Analysis.h:178
FilterResults genfilter_
Definition: Analysis.h:207
std::map< std::string, bool > triggerResults_
Definition: Analysis.h:204