Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-19 23:20:30

0001 #ifndef anautil_h
0002 #define anautil_h
0003 
0004 #include "cutflowutil.h"
0005 #include "ttreex.h"
0006 #include "printutil.h"
0007 #include <utility>
0008 #include <vector>
0009 #include <map>
0010 #include <tuple>
0011 #include "TH1.h"
0012 #include "TString.h"
0013 #include <iostream>
0014 #include <algorithm>
0015 
0016 bool PASS();
0017 float UNITY();
0018 
0019 namespace RooUtil {
0020   class Histograms {
0021   public:
0022 #ifdef USE_CUTLAMBDA
0023     std::map<TString, std::tuple<std::vector<float>, std::function<float()>>> th1fs_varbin;
0024     std::map<TString, std::tuple<unsigned int, float, float, std::function<float()>>> th1fs;
0025     std::map<TString,
0026              std::tuple<std::vector<float>, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>
0027         th1vecfs_varbin;
0028     std::map<
0029         TString,
0030         std::tuple<unsigned int, float, float, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>
0031         th1vecfs;
0032     std::map<
0033         std::pair<TString, TString>,
0034         std::tuple<unsigned int, float, float, unsigned int, float, float, std::function<float()>, std::function<float()>>>
0035         th2fs;
0036     std::map<std::pair<TString, TString>,
0037              std::tuple<unsigned int,
0038                         float,
0039                         float,
0040                         unsigned int,
0041                         float,
0042                         float,
0043                         std::function<std::vector<float>()>,
0044                         std::function<std::vector<float>()>,
0045                         std::function<std::vector<float>()>>>
0046         th2vecfs;
0047     std::map<std::pair<TString, TString>,
0048              std::tuple<std::vector<float>,
0049                         unsigned int,
0050                         float,
0051                         float,
0052                         std::function<std::vector<float>()>,
0053                         std::function<std::vector<float>()>,
0054                         std::function<std::vector<float>()>>>
0055         th2vecfs_xvarbin;
0056 #else
0057     std::map<TString, std::vector<float>> th1fs_varbin;
0058     std::map<TString, std::tuple<unsigned int, float, float>> th1fs;
0059     std::map<std::pair<TString, TString>, std::tuple<unsigned int, float, float, unsigned int, float, float>> th2fs;
0060 #endif
0061     Histograms();
0062     ~Histograms();
0063 #ifdef USE_CUTLAMBDA
0064     void addHistogram(TString, unsigned int, float, float, std::function<float()>);
0065     void addVecHistogram(TString,
0066                          unsigned int,
0067                          float,
0068                          float,
0069                          std::function<std::vector<float>()>,
0070                          std::function<std::vector<float>()> wgt = nullptr);
0071     void addHistogram(TString, std::vector<float>, std::function<float()>);
0072     void addVecHistogram(TString,
0073                          std::vector<float>,
0074                          std::function<std::vector<float>()>,
0075                          std::function<std::vector<float>()> wgt = nullptr);
0076     void add2DHistogram(TString,
0077                         unsigned int,
0078                         float,
0079                         float,
0080                         TString,
0081                         unsigned int,
0082                         float,
0083                         float,
0084                         std::function<float()>,
0085                         std::function<float()>);
0086     void add2DVecHistogram(TString,
0087                            unsigned int,
0088                            float,
0089                            float,
0090                            TString,
0091                            unsigned int,
0092                            float,
0093                            float,
0094                            std::function<std::vector<float>()>,
0095                            std::function<std::vector<float>()>,
0096                            std::function<std::vector<float>()> wgt = nullptr);
0097     void add2DVecHistogram(TString,
0098                            std::vector<float>,
0099                            TString,
0100                            unsigned int,
0101                            float,
0102                            float,
0103                            std::function<std::vector<float>()>,
0104                            std::function<std::vector<float>()>,
0105                            std::function<std::vector<float>()> wgt = nullptr);
0106 #else
0107     void addHistogram(TString, unsigned int, float, float, std::function<float()> vardef);
0108     void addHistogram(TString, std::vector<float>, std::function<float()> vardef);
0109     void add2DHistogram(TString, unsigned int, float, float, TString, unsigned int, float, float);
0110 #endif
0111   };
0112 
0113   class Cutflow {
0114   public:
0115     CutTree cuttree;
0116     CutTree* last_active_cut;  // when getCut is called this is set
0117     std::map<TREEMAPSTRING, CutTree*> cuttreemap;
0118     std::map<CUTFLOWMAPSTRING, THist*> cutflow_histograms;
0119     std::map<CUTFLOWMAPSTRING, THist*> rawcutflow_histograms;
0120     std::map<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>, THist*>
0121         booked_histograms;  // key is <cutname, syst, varname>
0122     std::map<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>, TH2F*>
0123         booked_2dhistograms;  // key is <cutname, syst, varname, varnamey>
0124     std::vector<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>>
0125         booked_histograms_nominal_keys;  // key is <cutname, syst="", varname>
0126     std::vector<std::tuple<TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING, TREEMAPSTRING>>
0127         booked_2dhistograms_nominal_keys;  // key is <cutname, syst="", varname, varnamey>
0128     std::vector<std::tuple<THist*, std::vector<int*>, std::vector<float*>, std::function<float()>>>
0129         cutflow_histograms_v2;
0130     std::vector<std::tuple<THist*, std::vector<int*>>> rawcutflow_histograms_v2;
0131     std::vector<TString> cutflow_nofill_cut_list;
0132     TFile* ofile;
0133     TTree* t;
0134     TTreeX* tx;
0135     std::vector<TString> cutsysts;
0136     std::vector<TString> systs;
0137     std::map<TString, std::function<float()>> systs_funcs;
0138     std::map<TString, std::vector<TString>> cutlists;
0139     std::map<TString, std::vector<CutTree*>> cuttreelists;
0140     bool iseventlistbooked;
0141     int seterrorcount;
0142     bool doskipsysthist;
0143     bool dosavettreex;
0144     bool cutflow_booked;
0145     Cutflow();
0146     Cutflow(TFile* o);
0147     ~Cutflow();
0148     void setTFile(TFile* o) { ofile = o; }
0149     void addToCutTreeMap(TString n);
0150     void setLastActiveCut(TString n);
0151 #ifdef USE_CUTLAMBDA
0152     void addCut(TString n, std::function<bool()> pass, std::function<float()> weight);
0153     void addCutToLastActiveCut(TString n, std::function<bool()> pass, std::function<float()> weight);
0154 #else
0155     void addCut(TString n);
0156     void addCutToLastActiveCut(TString n);
0157 #endif
0158     void copyAndEditCuts(TString, std::map<TString, TString>);
0159     void printCuts();
0160     CutTree& getCut(TString n);
0161     void removeCut(TString n);
0162     // void filterCuts(TString n);
0163     void filterCuts(std::vector<TString> ns);
0164     void setCutLists(std::vector<TString> regions);
0165     void addCutToSkipCutflowList(TString n);
0166     void bookCutflowTree();
0167     void bookCutflowHistograms();
0168     void bookCutflowHistograms_v1();
0169     void bookCutflowHistograms_v2();
0170     void bookCutflowsForRegions(std::vector<TString> regions);
0171     void bookCutflows();
0172     void setSkipSystematicHistograms(bool = true);
0173     void setSaveTTreeX(bool = true);
0174     void saveOutput();
0175     void saveCutflows();
0176     void saveHistograms();
0177     void saveTTreeX();
0178 #ifdef USE_CUTLAMBDA
0179     void setCut(TString cutname, std::function<bool()> pass, std::function<float()> weight);
0180     void setCutSyst(TString cutname, TString syst, std::function<bool()> pass, std::function<float()> weight);
0181 #else
0182     void setCut(TString cutname, bool pass, float weight);
0183     void setCutSyst(TString cutname, TString syst, bool pass, float weight);
0184 #endif
0185     void addCutSyst(TString syst,
0186                     std::vector<TString> pattern,
0187                     std::vector<TString> vetopattern = std::vector<TString>());
0188 #ifdef USE_CUTLAMBDA
0189     void addWgtSyst(TString syst, std::function<float()>);
0190 #else
0191     void addWgtSyst(TString syst);
0192 #endif
0193     void setWgtSyst(TString syst, float weight);  // TODO make TTreeX using lambda...?
0194     void createWgtSystBranches();
0195     void setVariable(TString varname, float);
0196     void setEventID(int, int, unsigned long long);
0197     void bookEventLists();
0198     void fill();
0199     void fillCutflows(TString syst = "", bool iswgtsyst = true);
0200     void fillCutflow(std::vector<TString>& cutlist, THist* h, THist* hraw, float wgtsyst = 1);
0201     void fillCutflows_v1(TString syst = "", bool iswgtsyst = true);
0202     void fillCutflow_v2(std::vector<CutTree*>& cutlist, THist* h, THist* hraw, float wgtsyst = 1);
0203     void fillCutflows_v2(TString syst = "", bool iswgtsyst = true);
0204     void fillHistograms(TString syst = "", bool iswgtsyst = true);
0205 #ifdef USE_CUTLAMBDA
0206     void bookHistogram(TString,
0207                        std::pair<TString, std::tuple<unsigned, float, float, std::function<float()>>>,
0208                        TString = "");
0209     void bookVecHistogram(
0210         TString,
0211         std::pair<
0212             TString,
0213             std::tuple<unsigned, float, float, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>,
0214         TString = "");
0215     void bookHistogram(TString,
0216                        std::pair<TString, std::tuple<std::vector<float>, std::function<float()>>>,
0217                        TString = "");
0218     void bookVecHistogram(
0219         TString,
0220         std::pair<
0221             TString,
0222             std::tuple<std::vector<float>, std::function<std::vector<float>()>, std::function<std::vector<float>()>>>,
0223         TString = "");
0224     void book2DHistogram(
0225         TString,
0226         std::pair<
0227             std::pair<TString, TString>,
0228             std::tuple<unsigned, float, float, unsigned, float, float, std::function<float()>, std::function<float()>>>,
0229         TString = "");
0230     void book2DVecHistogram(TString,
0231                             std::pair<std::pair<TString, TString>,
0232                                       std::tuple<unsigned,
0233                                                  float,
0234                                                  float,
0235                                                  unsigned,
0236                                                  float,
0237                                                  float,
0238                                                  std::function<std::vector<float>()>,
0239                                                  std::function<std::vector<float>()>,
0240                                                  std::function<std::vector<float>()>>>,
0241                             TString = "");
0242     void book2DVecHistogram(TString,
0243                             std::pair<std::pair<TString, TString>,
0244                                       std::tuple<std::vector<float>,
0245                                                  unsigned,
0246                                                  float,
0247                                                  float,
0248                                                  std::function<std::vector<float>()>,
0249                                                  std::function<std::vector<float>()>,
0250                                                  std::function<std::vector<float>()>>>,
0251                             TString = "");
0252 #else
0253     void bookHistogram(TString, std::pair<TString, std::tuple<unsigned, float, float>>, TString = "");
0254     void bookHistogram(TString, std::pair<TString, std::vector<float>>, TString = "");
0255     void book2DHistogram(
0256         TString,
0257         std::pair<std::pair<TString, TString>, std::tuple<unsigned, float, float, unsigned, float, float>>,
0258         TString = "");
0259 #endif
0260     void bookHistograms(Histograms& histograms);
0261     void bookHistograms(Histograms& histograms, std::vector<TString> cutlist);
0262     void bookHistogramsForCut(Histograms& histograms, TString);
0263     void bookHistogramsForCutAndBelow(Histograms& histograms, TString);
0264     void bookHistogramsForCutAndAbove(Histograms& histograms, TString);
0265     void bookHistogramsForEndCuts(Histograms& histograms);
0266     void printSetFunctionError(TString msg);
0267     void setHistsAxesExtendable();
0268   };
0269 }  // namespace RooUtil
0270 
0271 #endif