Imperial Analysis
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
MultiDraw.cc
Go to the documentation of this file.
1 #include "../interface/MultiDraw.hh"
2 #include <iostream>
3 #include "TH1D.h"
4 #include "TH2F.h"
5 #include "TStopwatch.h"
6 #include "TTree.h"
7 #include "TTreeFormula.h"
8 #include <map>
9 
10 void MultiDraw(TTree *inTree, TObjArray *Formulae, TObjArray *Weights,
11  TObjArray *Hists, UInt_t ListLen) {
12  unsigned long i = 0;
13  unsigned long NumEvents = inTree->GetEntries();
14 
15  std::vector<TTreeFormula *> v_vars(ListLen, nullptr);
16  std::vector<TTreeFormula *> v_weights(ListLen, nullptr);
17  std::vector<TH1D *> v_hists(ListLen, nullptr);
18  std::vector<TH2F *> v_hists2d(ListLen, nullptr);
19 
20  std::vector<double> r_vars(ListLen, 0.);
21  std::vector<double> r_weights(ListLen, 0.);
22  std::vector<unsigned> i_vars(ListLen, 0);
23  std::vector<unsigned> i_weights(ListLen, 0);
24  std::map<std::string, unsigned> map_vars;
25  std::map<std::string, unsigned> map_weights;
26 
27  bool optimize = true;
28  for (unsigned idx = 0; idx < ListLen; ++idx) {
29  if (optimize) {
30  auto const& itv = map_vars.find(Formulae->At(idx)->GetTitle());
31  if (itv == map_vars.end()) {
32  map_vars[Formulae->At(idx)->GetTitle()] = idx;
33  v_vars[idx] = static_cast<TTreeFormula *>(Formulae->At(idx));
34  i_vars[idx] = idx;
35  } else {
36  i_vars[idx] = itv->second;
37  }
38 
39  auto const& itw = map_weights.find(Weights->At(idx)->GetTitle());
40  if (itw == map_weights.end()) {
41  map_weights[Weights->At(idx)->GetTitle()] = idx;
42  v_weights[idx] = static_cast<TTreeFormula *>(Weights->At(idx));
43  i_weights[idx] = idx;
44  } else {
45  i_weights[idx] = itw->second;
46  }
47  } else {
48  v_vars[idx] = static_cast<TTreeFormula *>(Formulae->At(idx));
49  i_vars[idx] = idx;
50  v_weights[idx] = static_cast<TTreeFormula *>(Weights->At(idx));
51  i_weights[idx] = idx;
52  }
53 
54  v_hists[idx] = dynamic_cast<TH1D *>(Hists->At(idx));
55  v_hists2d[idx] = dynamic_cast<TH2F *>(Hists->At(idx));
56  }
57 
58  double Value = 0.;
59  double Weight = 0.;
60  double commonWeight = 1.;
61  double treeWeight = inTree->GetWeight();
62  Int_t TreeNumber = -1;
63 
64  TStopwatch s;
65  for (i = 0; i < NumEvents; i++) {
66  // Display progress every 20000 events
67  if (i % 20000 == 0) {
68  std::cout.precision(2);
69 
70  double nTodo = NumEvents - i, perSecond = 20000 / s.RealTime();
71  Int_t seconds = (Int_t)(nTodo / perSecond),
72  minutes = (Int_t)(seconds / 60.);
73  seconds -= (Int_t)(minutes * 60.);
74 
75  std::cout << "Done " << (double(i) / (double(NumEvents)) * 100.0f)
76  << "% ";
77  if (minutes) std::cout << minutes << " minutes ";
78  std::cout << seconds << " seconds remain. \r";
79 
80  std::cout.flush();
81  s.Start();
82  }
83 
84  if (TreeNumber != inTree->GetTreeNumber()) {
85  treeWeight = inTree->GetWeight();
86  TreeNumber = inTree->GetTreeNumber();
87  }
88 
89  inTree->LoadTree(inTree->GetEntryNumber(i));
90 
91  commonWeight *= treeWeight;
92 
93  for (unsigned j = 0; j < ListLen; j++) {
94  if (v_vars[j]) {
95  r_vars[j] = v_vars[j]->EvalInstance();
96  }
97  if (v_weights[j]) {
98  r_weights[j] = v_weights[j]->EvalInstance();
99  }
100  Value = r_vars[i_vars[j]];
101  Weight = r_weights[i_weights[j]] * commonWeight;
102  if (v_hists[j] && Weight) {
103  v_hists[j]->Fill(Value, Weight);
104  }
105  // If this is a 2D hist the current Value will be the x variable
106  // and the previous one (without a histogram in the array) is
107  // the y variable
108  if (v_hists2d[j] && j >= 1 && Weight) {
109  v_hists2d[j]->Fill(Value, r_vars[i_vars[j-1]], Weight);
110  }
111  }
112  }
113 }
void MultiDraw(TTree *inTree, TObjArray *Formulae, TObjArray *Weights, TObjArray *Hists, UInt_t ListLen)
Definition: MultiDraw.cc:10