CMS 3D CMS Logo

anautil.h
Go to the documentation of this file.
1 #ifndef anautil_h
2 #define anautil_h
3 
4 #include "cutflowutil.h"
5 #include "ttreex.h"
6 #include "printutil.h"
7 #include <utility>
8 #include <vector>
9 #include <map>
10 #include <tuple>
11 #include "TH1.h"
12 #include "TString.h"
13 #include <iostream>
14 #include <algorithm>
15 
16 bool PASS();
17 float UNITY();
18 
19 namespace RooUtil {
20  class Histograms {
21  public:
22 #ifdef USE_CUTLAMBDA
23  std::map<TString, std::tuple<std::vector<float>, std::function<float()>>> th1fs_varbin;
24  std::map<TString, std::tuple<unsigned int, float, float, std::function<float()>>> th1fs;
25  std::map<TString,
26  std::tuple<std::vector<float>, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>
28  std::map<
29  TString,
30  std::tuple<unsigned int, float, float, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>
32  std::map<
33  std::pair<TString, TString>,
34  std::tuple<unsigned int, float, float, unsigned int, float, float, std::function<float()>, std::function<float()>>>
36  std::map<std::pair<TString, TString>,
37  std::tuple<unsigned int,
38  float,
39  float,
40  unsigned int,
41  float,
42  float,
43  std::function<std::vector<float>()>,
44  std::function<std::vector<float>()>,
45  std::function<std::vector<float>()>>>
47  std::map<std::pair<TString, TString>,
48  std::tuple<std::vector<float>,
49  unsigned int,
50  float,
51  float,
52  std::function<std::vector<float>()>,
53  std::function<std::vector<float>()>,
54  std::function<std::vector<float>()>>>
56 #else
57  std::map<TString, std::vector<float>> th1fs_varbin;
58  std::map<TString, std::tuple<unsigned int, float, float>> th1fs;
59  std::map<std::pair<TString, TString>, std::tuple<unsigned int, float, float, unsigned int, float, float>> th2fs;
60 #endif
61  Histograms();
62  ~Histograms();
63 #ifdef USE_CUTLAMBDA
64  void addHistogram(TString, unsigned int, float, float, std::function<float()>);
65  void addVecHistogram(TString,
66  unsigned int,
67  float,
68  float,
69  std::function<std::vector<float>()>,
70  std::function<std::vector<float>()> wgt = nullptr);
71  void addHistogram(TString, std::vector<float>, std::function<float()>);
72  void addVecHistogram(TString,
73  std::vector<float>,
74  std::function<std::vector<float>()>,
75  std::function<std::vector<float>()> wgt = nullptr);
76  void add2DHistogram(TString,
77  unsigned int,
78  float,
79  float,
80  TString,
81  unsigned int,
82  float,
83  float,
84  std::function<float()>,
85  std::function<float()>);
86  void add2DVecHistogram(TString,
87  unsigned int,
88  float,
89  float,
90  TString,
91  unsigned int,
92  float,
93  float,
94  std::function<std::vector<float>()>,
95  std::function<std::vector<float>()>,
96  std::function<std::vector<float>()> wgt = nullptr);
97  void add2DVecHistogram(TString,
98  std::vector<float>,
99  TString,
100  unsigned int,
101  float,
102  float,
103  std::function<std::vector<float>()>,
104  std::function<std::vector<float>()>,
105  std::function<std::vector<float>()> wgt = nullptr);
106 #else
107  void addHistogram(TString, unsigned int, float, float, std::function<float()> vardef);
108  void addHistogram(TString, std::vector<float>, std::function<float()> vardef);
109  void add2DHistogram(TString, unsigned int, float, float, TString, unsigned int, float, float);
110 #endif
111  };
112 
113  class Cutflow {
114  public:
116  CutTree* last_active_cut; // when getCut is called this is set
117  std::map<TREEMAPSTRING, CutTree*> cuttreemap;
118  std::map<CUTFLOWMAPSTRING, THist*> cutflow_histograms;
119  std::map<CUTFLOWMAPSTRING, THist*> rawcutflow_histograms;
120  std::map<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>, THist*>
121  booked_histograms; // key is <cutname, syst, varname>
122  std::map<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>, TH2F*>
123  booked_2dhistograms; // key is <cutname, syst, varname, varnamey>
124  std::vector<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>>
125  booked_histograms_nominal_keys; // key is <cutname, syst="", varname>
126  std::vector<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>>
127  booked_2dhistograms_nominal_keys; // key is <cutname, syst="", varname, varnamey>
128  std::vector<std::tuple<THist*, std::vector<int*>, std::vector<float*>, std::function<float()>>>
130  std::vector<std::tuple<THist*, std::vector<int*>>> rawcutflow_histograms_v2;
131  std::vector<TString> cutflow_nofill_cut_list;
132  TFile* ofile;
133  TTree* t;
135  std::vector<TString> cutsysts;
136  std::vector<TString> systs;
137  std::map<TString, std::function<float()>> systs_funcs;
138  std::map<TString, std::vector<TString>> cutlists;
139  std::map<TString, std::vector<CutTree*>> cuttreelists;
145  Cutflow();
146  Cutflow(TFile* o);
147  ~Cutflow();
148  void setTFile(TFile* o) { ofile = o; }
149  void addToCutTreeMap(TString n);
150  void setLastActiveCut(TString n);
151 #ifdef USE_CUTLAMBDA
152  void addCut(TString n, std::function<bool()> pass, std::function<float()> weight);
153  void addCutToLastActiveCut(TString n, std::function<bool()> pass, std::function<float()> weight);
154 #else
155  void addCut(TString n);
156  void addCutToLastActiveCut(TString n);
157 #endif
158  void copyAndEditCuts(TString, std::map<TString, TString>);
159  void printCuts();
160  CutTree& getCut(TString n);
161  void removeCut(TString n);
162  // void filterCuts(TString n);
163  void filterCuts(std::vector<TString> ns);
164  void setCutLists(std::vector<TString> regions);
165  void addCutToSkipCutflowList(TString n);
166  void bookCutflowTree();
167  void bookCutflowHistograms();
170  void bookCutflowsForRegions(std::vector<TString> regions);
171  void bookCutflows();
172  void setSkipSystematicHistograms(bool = true);
173  void setSaveTTreeX(bool = true);
174  void saveOutput();
175  void saveCutflows();
176  void saveHistograms();
177  void saveTTreeX();
178 #ifdef USE_CUTLAMBDA
179  void setCut(TString cutname, std::function<bool()> pass, std::function<float()> weight);
180  void setCutSyst(TString cutname, TString syst, std::function<bool()> pass, std::function<float()> weight);
181 #else
182  void setCut(TString cutname, bool pass, float weight);
183  void setCutSyst(TString cutname, TString syst, bool pass, float weight);
184 #endif
185  void addCutSyst(TString syst,
186  std::vector<TString> pattern,
187  std::vector<TString> vetopattern = std::vector<TString>());
188 #ifdef USE_CUTLAMBDA
189  void addWgtSyst(TString syst, std::function<float()>);
190 #else
191  void addWgtSyst(TString syst);
192 #endif
193  void setWgtSyst(TString syst, float weight); // TODO make TTreeX using lambda...?
194  void createWgtSystBranches();
195  void setVariable(TString varname, float);
196  void setEventID(int, int, unsigned long long);
197  void bookEventLists();
198  void fill();
199  void fillCutflows(TString syst = "", bool iswgtsyst = true);
200  void fillCutflow(std::vector<TString>& cutlist, THist* h, THist* hraw, float wgtsyst = 1);
201  void fillCutflows_v1(TString syst = "", bool iswgtsyst = true);
202  void fillCutflow_v2(std::vector<CutTree*>& cutlist, THist* h, THist* hraw, float wgtsyst = 1);
203  void fillCutflows_v2(TString syst = "", bool iswgtsyst = true);
204  void fillHistograms(TString syst = "", bool iswgtsyst = true);
205 #ifdef USE_CUTLAMBDA
206  void bookHistogram(TString,
207  std::pair<TString, std::tuple<unsigned, float, float, std::function<float()>>>,
208  TString = "");
209  void bookVecHistogram(
210  TString,
211  std::pair<
212  TString,
213  std::tuple<unsigned, float, float, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>,
214  TString = "");
215  void bookHistogram(TString,
216  std::pair<TString, std::tuple<std::vector<float>, std::function<float()>>>,
217  TString = "");
218  void bookVecHistogram(
219  TString,
220  std::pair<
221  TString,
222  std::tuple<std::vector<float>, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>,
223  TString = "");
224  void book2DHistogram(
225  TString,
226  std::pair<
227  std::pair<TString, TString>,
228  std::tuple<unsigned, float, float, unsigned, float, float, std::function<float()>, std::function<float()>>>,
229  TString = "");
230  void book2DVecHistogram(TString,
231  std::pair<std::pair<TString, TString>,
232  std::tuple<unsigned,
233  float,
234  float,
235  unsigned,
236  float,
237  float,
238  std::function<std::vector<float>()>,
239  std::function<std::vector<float>()>,
240  std::function<std::vector<float>()>>>,
241  TString = "");
242  void book2DVecHistogram(TString,
243  std::pair<std::pair<TString, TString>,
244  std::tuple<std::vector<float>,
245  unsigned,
246  float,
247  float,
248  std::function<std::vector<float>()>,
249  std::function<std::vector<float>()>,
250  std::function<std::vector<float>()>>>,
251  TString = "");
252 #else
253  void bookHistogram(TString, std::pair<TString, std::tuple<unsigned, float, float>>, TString = "");
254  void bookHistogram(TString, std::pair<TString, std::vector<float>>, TString = "");
255  void book2DHistogram(
256  TString,
257  std::pair<std::pair<TString, TString>, std::tuple<unsigned, float, float, unsigned, float, float>>,
258  TString = "");
259 #endif
261  void bookHistograms(Histograms& histograms, std::vector<TString> cutlist);
266  void printSetFunctionError(TString msg);
267  void setHistsAxesExtendable();
268  };
269 } // namespace RooUtil
270 
271 #endif
void bookHistogramsForCutAndAbove(Histograms &histograms, TString)
Definition: anautil.cc:1058
std::map< TString, std::function< float()> > systs_funcs
Definition: anautil.h:137
void setVariable(TString varname, float)
Definition: anautil.cc:437
void bookCutflowHistograms()
Definition: anautil.cc:181
void setCut(TString cutname, std::function< bool()> pass, std::function< float()> weight)
Definition: anautil.cc:343
void bookHistogramsForEndCuts(Histograms &histograms)
Definition: anautil.cc:1063
void bookHistogramsForCutAndBelow(Histograms &histograms, TString)
Definition: anautil.cc:1050
void addToCutTreeMap(TString n)
Definition: anautil.cc:51
void removeCut(TString n)
Definition: anautil.cc:107
std::map< TString, std::tuple< std::vector< float >, std::function< std::vector< float >)>, std::function< std::vector< float >)> > > th1vecfs_varbin
Definition: anautil.h:27
void saveOutput()
Definition: anautil.cc:308
std::vector< TString > systs
Definition: anautil.h:136
float UNITY()
Definition: anautil.cc:4
std::map< std::tuple< std::string, std::string, std::string, std::string >, TH2F * > booked_2dhistograms
Definition: anautil.h:123
void bookCutflowHistograms_v1()
Definition: anautil.cc:259
std::map< TString, std::vector< CutTree * > > cuttreelists
Definition: anautil.h:139
void addHistogram(TString, unsigned int, float, float, std::function< float()>)
Definition: anautil.cc:1104
void book2DVecHistogram(TString, std::pair< std::pair< TString, TString >, std::tuple< unsigned, float, float, unsigned, float, float, std::function< std::vector< float >()>, std::function< std::vector< float >()>, std::function< std::vector< float >()>>>, TString="")
Definition: anautil.cc:822
CutTree & getCut(TString n)
Definition: anautil.cc:65
void setLastActiveCut(TString n)
Definition: anautil.cc:59
Definition: weight.py:1
void copyAndEditCuts(TString, std::map< TString, TString >)
bool iseventlistbooked
Definition: anautil.h:140
void setSaveTTreeX(bool=true)
Definition: anautil.cc:1074
std::map< std::pair< TString, TString >, std::tuple< unsigned int, float, float, unsigned int, float, float, std::function< std::vector< float >)>, std::function< std::vector< float >)>, std::function< std::vector< float >)> > > th2vecfs
Definition: anautil.h:46
void filterCuts(std::vector< TString > ns)
Definition: anautil.cc:114
std::vector< std::tuple< std::string, std::string, std::string, std::string > > booked_2dhistograms_nominal_keys
Definition: anautil.h:127
std::map< TString, std::tuple< unsigned int, float, float, std::function< std::vector< float >)>, std::function< std::vector< float >)> > > th1vecfs
Definition: anautil.h:31
std::vector< std::tuple< TH1D *, std::vector< int * >, std::vector< float * >, std::function< float()> > > cutflow_histograms_v2
Definition: anautil.h:129
void setSkipSystematicHistograms(bool=true)
Definition: anautil.cc:1071
void bookCutflowsForRegions(std::vector< TString > regions)
Definition: anautil.cc:293
void fillCutflow(std::vector< TString > &cutlist, TH1D *h, TH1D *hraw, float wgtsyst=1)
Definition: anautil.cc:565
void saveTTreeX()
Definition: anautil.cc:334
void add2DHistogram(TString, unsigned int, float, float, TString, unsigned int, float, float, std::function< float()>, std::function< float()>)
Definition: anautil.cc:1149
void addCutSyst(TString syst, std::vector< TString > pattern, std::vector< TString > vetopattern=std::vector< TString >())
Definition: anautil.cc:412
void bookCutflows()
Definition: anautil.cc:300
bool doskipsysthist
Definition: anautil.h:142
void fillCutflow_v2(std::vector< CutTree *> &cutlist, TH1D *h, TH1D *hraw, float wgtsyst=1)
Definition: anautil.cc:579
void setTFile(TFile *o)
Definition: anautil.h:148
void printSetFunctionError(TString msg)
Definition: anautil.cc:1077
std::vector< TString > cutsysts
Definition: anautil.h:135
void saveCutflows()
Definition: anautil.cc:318
void printCuts()
Definition: anautil.cc:62
void bookHistogram(TString, std::pair< TString, std::tuple< unsigned, float, float, std::function< float()>>>, TString="")
Definition: anautil.cc:683
std::map< TString, std::vector< TString > > cutlists
Definition: anautil.h:138
void createWgtSystBranches()
Definition: anautil.cc:429
#define THist
Definition: cutflowutil.h:21
std::map< std::pair< TString, TString >, std::tuple< std::vector< float >, unsigned int, float, float, std::function< std::vector< float >)>, std::function< std::vector< float >)>, std::function< std::vector< float >)> > > th2vecfs_xvarbin
Definition: anautil.h:55
void bookHistogramsForCut(Histograms &histograms, TString)
Definition: anautil.cc:998
void addVecHistogram(TString, unsigned int, float, float, std::function< std::vector< float >()>, std::function< std::vector< float >()> wgt=nullptr)
Definition: anautil.cc:1114
std::map< TString, TH1D * > rawcutflow_histograms
Definition: anautil.h:119
void add2DVecHistogram(TString, unsigned int, float, float, TString, unsigned int, float, float, std::function< std::vector< float >()>, std::function< std::vector< float >()>, std::function< std::vector< float >()> wgt=nullptr)
Definition: anautil.cc:1167
std::map< TString, std::tuple< unsigned int, float, float, std::function< float()> > > th1fs
Definition: anautil.h:24
void setCutLists(std::vector< TString > regions)
Definition: anautil.cc:144
void setCutSyst(TString cutname, TString syst, std::function< bool()> pass, std::function< float()> weight)
Definition: anautil.cc:349
void bookVecHistogram(TString, std::pair< TString, std::tuple< unsigned, float, float, std::function< std::vector< float >()>, std::function< std::vector< float >()>>>, TString="")
Definition: anautil.cc:706
void bookCutflowHistograms_v2()
Definition: anautil.cc:190
TFile * ofile
Definition: anautil.h:132
void book2DHistogram(TString, std::pair< std::pair< TString, TString >, std::tuple< unsigned, float, float, unsigned, float, float, std::function< float()>, std::function< float()>>>, TString="")
Definition: anautil.cc:786
void addCutToSkipCutflowList(TString n)
Definition: anautil.cc:159
tuple msg
Definition: mps_check.py:286
TTreeX * tx
Definition: anautil.h:134
void setHistsAxesExtendable()
Definition: anautil.cc:1091
std::vector< std::tuple< TH1D *, std::vector< int * > > > rawcutflow_histograms_v2
Definition: anautil.h:130
void fillCutflows_v1(TString syst="", bool iswgtsyst=true)
Definition: anautil.cc:525
void addCutToLastActiveCut(TString n, std::function< bool()> pass, std::function< float()> weight)
Definition: anautil.cc:81
bool PASS()
Definition: anautil.cc:3
std::vector< TString > cutflow_nofill_cut_list
Definition: anautil.h:131
void bookCutflowTree()
Definition: anautil.cc:165
std::map< TString, std::tuple< std::vector< float >, std::function< float()> > > th1fs_varbin
Definition: anautil.h:23
std::map< std::tuple< std::string, std::string, std::string >, TH1D * > booked_histograms
Definition: anautil.h:121
void fillHistograms(TString syst="", bool iswgtsyst=true)
Definition: anautil.cc:599
void addWgtSyst(TString syst, std::function< float()>)
Definition: anautil.cc:419
CutTree * last_active_cut
Definition: anautil.h:116
bool cutflow_booked
Definition: anautil.h:144
void bookEventLists()
Definition: anautil.cc:459
void setEventID(int, int, unsigned long long)
Definition: anautil.cc:447
CutTree cuttree
Definition: anautil.h:115
void fillCutflows(TString syst="", bool iswgtsyst=true)
Definition: anautil.cc:522
void saveHistograms()
Definition: anautil.cc:325
void setWgtSyst(TString syst, float weight)
Definition: anautil.cc:402
std::map< TString, TH1D * > cutflow_histograms
Definition: anautil.h:118
bool dosavettreex
Definition: anautil.h:143
void bookHistograms(Histograms &histograms)
Definition: anautil.cc:666
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void fillCutflows_v2(TString syst="", bool iswgtsyst=true)
Definition: anautil.cc:538
void addCut(TString n, std::function< bool()> pass, std::function< float()> weight)
Definition: anautil.cc:73
std::vector< std::tuple< std::string, std::string, std::string > > booked_histograms_nominal_keys
Definition: anautil.h:125
std::map< std::string, CutTree * > cuttreemap
Definition: anautil.h:117
std::map< std::pair< TString, TString >, std::tuple< unsigned int, float, float, unsigned int, float, float, std::function< float()>, std::function< float()> > > th2fs
Definition: anautil.h:35