CMS 3D CMS Logo

cutflowutil.cc
Go to the documentation of this file.
1 #include "cutflowutil.h"
2 
3 //_______________________________________________________________________________________________________
4 void RooUtil::CutflowUtil::createCutflowBranches(std::map<TString, std::vector<TString>>& cutlists,
5  RooUtil::TTreeX& tx) {
6  for (auto& cutlist : cutlists) {
7  for (auto& cutname : cutlist.second) {
8  // std::cout << " cutname: " << cutname << std::endl;
9  if (!tx.hasBranch<bool>(cutname))
10  tx.createBranch<bool>(cutname);
11  if (!tx.hasBranch<float>(cutname + "_weight"))
12  tx.createBranch<float>(cutname + "_weight");
13  }
14  }
15 }
16 
17 //_______________________________________________________________________________________________________
19  std::map<TString, std::vector<TString>> obj = cutlists.getStdVersion();
21 }
22 
23 //_______________________________________________________________________________________________________
24 std::tuple<std::vector<bool>, std::vector<float>> RooUtil::CutflowUtil::getCutflow(std::vector<TString> cutlist,
25  RooUtil::TTreeX& tx) {
26  std::vector<float> rtnwgt;
27  std::vector<bool> rtn;
28  float wgtall = 1;
29  bool passall = true;
30  for (auto& cutname : cutlist) {
31  bool pass = tx.getBranch<bool>(cutname);
32  float wgt = tx.getBranch<float>(cutname + "_weight");
33  wgtall *= wgt;
34  passall &= pass;
35  rtnwgt.push_back(wgtall);
36  rtn.push_back(passall);
37  }
38  return std::make_tuple(rtn, rtnwgt);
39 }
40 
41 //_______________________________________________________________________________________________________
42 std::pair<bool, float> RooUtil::CutflowUtil::passCuts(std::vector<TString> cutlist, RooUtil::TTreeX& tx) {
43  std::vector<bool> cutflow;
44  std::vector<float> cutflow_weight;
45  std::tie(cutflow, cutflow_weight) = getCutflow(cutlist, tx);
46  // Just need to check the last one
47  bool passall = cutflow.back();
48  float wgtall = cutflow_weight.back();
49  return std::make_pair(passall, wgtall);
50 }
51 
52 //_______________________________________________________________________________________________________
54  std::vector<bool> cutflow;
55  std::vector<float> cutflow_weight;
56  std::tie(cutflow, cutflow_weight) = getCutflow(cutlist, tx);
57  for (unsigned int i = 0; i < cutflow.size(); ++i)
58  if (cutflow[i] > 0)
59  h->Fill(i, cutflow[i] * cutflow_weight[i]);
60  else
61  return;
62 }
63 
64 //_______________________________________________________________________________________________________
66  std::vector<bool> cutflow;
67  std::vector<float> cutflow_weight;
68  std::tie(cutflow, cutflow_weight) = getCutflow(cutlist, tx);
69  for (unsigned int i = 0; i < cutflow.size(); ++i)
70  if (cutflow[i] > 0)
71  h->Fill(i);
72  else
73  return;
74 }
75 
76 //_______________________________________________________________________________________________________
77 std::tuple<std::map<CUTFLOWMAPSTRING, THist*>, std::map<CUTFLOWMAPSTRING, THist*>>
79  std::map<TString, std::vector<TString>> obj = cutlists.getStdVersion();
80  return createCutflowHistograms(obj, syst);
81 }
82 
83 //_______________________________________________________________________________________________________
84 std::tuple<std::map<CUTFLOWMAPSTRING, THist*>, std::map<CUTFLOWMAPSTRING, THist*>>
85 RooUtil::CutflowUtil::createCutflowHistograms(std::map<TString, std::vector<TString>>& cutlists, TString syst) {
86  std::map<CUTFLOWMAPSTRING, THist*> cutflows;
87  std::map<CUTFLOWMAPSTRING, THist*> rawcutflows;
88  for (auto& cutlist : cutlists) {
89  cutflows[(cutlist.first + syst).Data()] =
90  new THist(cutlist.first + syst + "_cutflow", "", cutlist.second.size(), 0, cutlist.second.size());
91  rawcutflows[(cutlist.first + syst).Data()] =
92  new THist(cutlist.first + syst + "_rawcutflow", "", cutlist.second.size(), 0, cutlist.second.size());
93  cutflows[(cutlist.first + syst).Data()]->Sumw2();
94  rawcutflows[(cutlist.first + syst).Data()]->Sumw2();
95  cutflows[(cutlist.first + syst).Data()]->SetDirectory(0);
96  rawcutflows[(cutlist.first + syst).Data()]->SetDirectory(0);
97  for (unsigned int i = 0; i < cutlist.second.size(); ++i) {
98  cutflows[(cutlist.first + syst).Data()]->GetXaxis()->SetBinLabel(i + 1, cutlist.second[i]);
99  rawcutflows[(cutlist.first + syst).Data()]->GetXaxis()->SetBinLabel(i + 1, cutlist.second[i]);
100  }
101  }
102  return std::make_tuple(cutflows, rawcutflows);
103 }
104 
106 //void RooUtil::CutflowUtil::fillCutflowHistograms(RooUtil::CutflowUtil::CutNameListMap& cutlists, RooUtil::TTreeX& tx, std::map<TString, THist*>& cutflows, std::map<TString, THist*>& rawcutflows)
107 //{
108 // std::map<TString, std::vector<TString>> obj = cutlists.getStdVersion();
109 // fillCutflowHistograms(obj, tx, cutflows, rawcutflows);
110 //}
111 //
113 //void RooUtil::CutflowUtil::fillCutflowHistograms(std::map<TString, std::vector<TString>>& cutlists, RooUtil::TTreeX& tx, std::map<TString, THist*>& cutflows, std::map<TString, THist*>& rawcutflows)
114 //{
115 // for (auto& cutlist : cutlists)
116 // {
117 // RooUtil::CutflowUtil::fillCutflow(cutlist.second, tx, cutflows[cutlist.first]);
118 // RooUtil::CutflowUtil::fillRawCutflow(cutlist.second, tx, rawcutflows[cutlist.first]);
119 // }
120 //}
121 
122 //_______________________________________________________________________________________________________
123 void RooUtil::CutflowUtil::saveCutflowHistograms(std::map<CUTFLOWMAPSTRING, THist*>& cutflows,
124  std::map<CUTFLOWMAPSTRING, THist*>& rawcutflows) {
125  for (auto& cutflow : cutflows)
126  cutflow.second->Write();
127  for (auto& rawcutflow : rawcutflows)
128  rawcutflow.second->Write();
129 }
void fillCutflow(std::vector< TString > cutlist, RooUtil::TTreeX &tx, TH1D *h)
Definition: cutflowutil.cc:53
std::tuple< std::map< TString, TH1D * >, std::map< TString, TH1D * > > createCutflowHistograms(CutNameListMap &cutlists, TString syst="")
Definition: cutflowutil.cc:78
void fillRawCutflow(std::vector< TString > cutlist, RooUtil::TTreeX &tx, TH1D *h)
Definition: cutflowutil.cc:65
const T & getBranch(TString, bool=true)
void saveCutflowHistograms(std::map< TString, TH1D *> &cutflows, std::map< TString, TH1D *> &rawcutflows)
Definition: cutflowutil.cc:123
std::tuple< std::vector< bool >, std::vector< float > > getCutflow(std::vector< TString > cutlist, RooUtil::TTreeX &tx)
Definition: cutflowutil.cc:24
void createBranch(TString, bool=true)
bool hasBranch(TString)
#define THist
Definition: cutflowutil.h:21
void createCutflowBranches(CutNameListMap &cutlists, RooUtil::TTreeX &tx)
Definition: cutflowutil.cc:18
std::map< TString, std::vector< TString > > getStdVersion()
Definition: cutflowutil.h:51
std::pair< bool, float > passCuts(std::vector< TString > cutlist, RooUtil::TTreeX &tx)
Definition: cutflowutil.cc:42
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4