Crombie Tools
CutflowMaker.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iostream>
3 #include <iomanip>
4 
5 #include "TTreeFormula.h"
6 #include "TCanvas.h"
7 #include "TH1D.h"
8 #include "TH1.h"
9 
10 #include "CutflowMaker.h"
11 #include "PlotUtils.h"
12 
14 
15 //--------------------------------------------------------------------
16 void
17 CutflowMaker::GetCutflow(UInt_t index)
18 {
19  TTree *inTree = fDefaultTree;
20 
21  if (fInTrees.size() != 0)
22  inTree = fInTrees[index];
23 
24  if (fYields.size() == 0) {
25 
26  std::vector<TTreeFormula*> formulae;
27  TTreeFormula *tempFormula;
28  std::set<TString> needed;
29 
30  for (auto& cut : fCuts) {
31  AddNecessaryBranches(needed, inTree, cut);
32  }
33 
34  inTree->SetBranchStatus("*", 0);
35  for (auto need : needed) {
36  Message(eDebug, "Enabling %s", need.Data());
37  inTree->SetBranchStatus(need, 1);
38  }
39 
40  for (UInt_t iCut = 0; iCut != fCuts.size(); ++iCut) {
41  tempFormula = new TTreeFormula(fCutNames[iCut], fCuts[iCut], inTree);
42  tempFormula->SetQuickLoad(true);
43  formulae.push_back(tempFormula);
44  fYields.push_back(0);
45  }
46 
47  Int_t numEntries = inTree->GetEntriesFast();
48 
49  for (Int_t iEntry = 0; iEntry != numEntries; ++iEntry) {
50 
51  inTree->GetEntry(iEntry);
52 
53  for (UInt_t iCut = 0; iCut != formulae.size(); ++iCut) {
54 
55  // Should get a vector filled so we can do triggers
56  formulae[iCut]->GetNdata();
57 
58  if (formulae[iCut]->EvalInstance() == 0)
59  break;
60 
61  ++fYields[iCut];
62 
63  }
64  }
65 
66  for (UInt_t iFormula = 0; iFormula != formulae.size(); ++iFormula)
67  delete formulae[iFormula];
68  }
69 }
70 
71 //--------------------------------------------------------------------
72 void
74 {
75  GetCutflow(0);
76  std::cout << std::endl;
77 
78  auto max_length = std::max_element(fCuts.begin(), fCuts.end(),
79  [] (const TString& str1, const TString& str2)
80  {return str1.Length() < str2.Length();})->Length();
81 
82  for (UInt_t iCut = 0; iCut != fCuts.size(); ++iCut) {
83 
84  if (table != kNumbers) {
85 
86  std::cout << std::setw(max_length + 2);
87  std::cout << fCutNames[iCut];
88 
89  if (table == kLatex)
90  std::cout << " & ";
91  else
92  std::cout << ": ";
93 
94  }
95 
96  std::cout << std::setw(15);
97  std::cout << fYields[iCut];
98  if (table == kLatex)
99  std::cout << " \\";
100 
101  std::cout << std::endl;
102 
103  }
104  std::cout << std::endl;
105 }
106 
107 //--------------------------------------------------------------------
108 void
109 CutflowMaker::MakePlot(TString name, PlotType type)
110 {
111 
112  std::vector<TH1D*> theHists;
113 
114  for (UInt_t iTree = 0; iTree != fInTrees.size(); ++iTree) {
115 
116  GetCutflow(iTree);
117 
118  TH1D* theHist = new TH1D(TString::Format("cutflow_%d", iTree),";;Number of Events",
119  fCuts.size(), 0, fCuts.size());
120 
121  for (UInt_t iCut = 0; iCut != fCuts.size(); ++iCut) {
122 
123  theHist->GetXaxis()->SetBinLabel(iCut+1,fCutNames[iCut]);
124 
125  if (type == kAbsolute)
126  theHist->SetBinContent(iCut+1,fYields[iCut]);
127 
128  if (type == kFractional) {
129 
130  if (iCut == 0)
131  theHist->SetBinContent(iCut+1,float(fYields[iCut])/fInTrees[iTree]->GetEntriesFast());
132  else
133  theHist->SetBinContent(iCut+1,float(fYields[iCut])/fYields[iCut - 1]);
134 
135  }
136  }
137 
138  theHists.push_back(theHist);
139  fYields.resize(0);
140 
141  }
142 
143  BaseCanvas(name, theHists, "", "Yield");
144 
145  for (UInt_t iHist = 0; iHist != theHists.size(); ++iHist)
146  delete theHists[iHist];
147 }