Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "cutflowutil.h"
0002 
0003 //_______________________________________________________________________________________________________
0004 void RooUtil::CutflowUtil::createCutflowBranches(std::map<TString, std::vector<TString>>& cutlists,
0005                                                  RooUtil::TTreeX& tx) {
0006   for (auto& cutlist : cutlists) {
0007     for (auto& cutname : cutlist.second) {
0008       //            std::cout <<  " cutname: " << cutname <<  std::endl;
0009       if (!tx.hasBranch<bool>(cutname))
0010         tx.createBranch<bool>(cutname);
0011       if (!tx.hasBranch<float>(cutname + "_weight"))
0012         tx.createBranch<float>(cutname + "_weight");
0013     }
0014   }
0015 }
0016 
0017 //_______________________________________________________________________________________________________
0018 void RooUtil::CutflowUtil::createCutflowBranches(CutNameListMap& cutlists, RooUtil::TTreeX& tx) {
0019   std::map<TString, std::vector<TString>> obj = cutlists.getStdVersion();
0020   createCutflowBranches(obj, tx);
0021 }
0022 
0023 //_______________________________________________________________________________________________________
0024 std::tuple<std::vector<bool>, std::vector<float>> RooUtil::CutflowUtil::getCutflow(std::vector<TString> cutlist,
0025                                                                                    RooUtil::TTreeX& tx) {
0026   std::vector<float> rtnwgt;
0027   std::vector<bool> rtn;
0028   float wgtall = 1;
0029   bool passall = true;
0030   for (auto& cutname : cutlist) {
0031     bool pass = tx.getBranch<bool>(cutname);
0032     float wgt = tx.getBranch<float>(cutname + "_weight");
0033     wgtall *= wgt;
0034     passall &= pass;
0035     rtnwgt.push_back(wgtall);
0036     rtn.push_back(passall);
0037   }
0038   return std::make_tuple(rtn, rtnwgt);
0039 }
0040 
0041 //_______________________________________________________________________________________________________
0042 std::pair<bool, float> RooUtil::CutflowUtil::passCuts(std::vector<TString> cutlist, RooUtil::TTreeX& tx) {
0043   std::vector<bool> cutflow;
0044   std::vector<float> cutflow_weight;
0045   std::tie(cutflow, cutflow_weight) = getCutflow(cutlist, tx);
0046   // Just need to check the last one
0047   bool passall = cutflow.back();
0048   float wgtall = cutflow_weight.back();
0049   return std::make_pair(passall, wgtall);
0050 }
0051 
0052 //_______________________________________________________________________________________________________
0053 void RooUtil::CutflowUtil::fillCutflow(std::vector<TString> cutlist, RooUtil::TTreeX& tx, THist* h) {
0054   std::vector<bool> cutflow;
0055   std::vector<float> cutflow_weight;
0056   std::tie(cutflow, cutflow_weight) = getCutflow(cutlist, tx);
0057   for (unsigned int i = 0; i < cutflow.size(); ++i)
0058     if (cutflow[i] > 0)
0059       h->Fill(i, cutflow[i] * cutflow_weight[i]);
0060     else
0061       return;
0062 }
0063 
0064 //_______________________________________________________________________________________________________
0065 void RooUtil::CutflowUtil::fillRawCutflow(std::vector<TString> cutlist, RooUtil::TTreeX& tx, THist* h) {
0066   std::vector<bool> cutflow;
0067   std::vector<float> cutflow_weight;
0068   std::tie(cutflow, cutflow_weight) = getCutflow(cutlist, tx);
0069   for (unsigned int i = 0; i < cutflow.size(); ++i)
0070     if (cutflow[i] > 0)
0071       h->Fill(i);
0072     else
0073       return;
0074 }
0075 
0076 //_______________________________________________________________________________________________________
0077 std::tuple<std::map<CUTFLOWMAPSTRING, THist*>, std::map<CUTFLOWMAPSTRING, THist*>>
0078 RooUtil::CutflowUtil::createCutflowHistograms(RooUtil::CutflowUtil::CutNameListMap& cutlists, TString syst) {
0079   std::map<TString, std::vector<TString>> obj = cutlists.getStdVersion();
0080   return createCutflowHistograms(obj, syst);
0081 }
0082 
0083 //_______________________________________________________________________________________________________
0084 std::tuple<std::map<CUTFLOWMAPSTRING, THist*>, std::map<CUTFLOWMAPSTRING, THist*>>
0085 RooUtil::CutflowUtil::createCutflowHistograms(std::map<TString, std::vector<TString>>& cutlists, TString syst) {
0086   std::map<CUTFLOWMAPSTRING, THist*> cutflows;
0087   std::map<CUTFLOWMAPSTRING, THist*> rawcutflows;
0088   for (auto& cutlist : cutlists) {
0089     cutflows[(cutlist.first + syst).Data()] =
0090         new THist(cutlist.first + syst + "_cutflow", "", cutlist.second.size(), 0, cutlist.second.size());
0091     rawcutflows[(cutlist.first + syst).Data()] =
0092         new THist(cutlist.first + syst + "_rawcutflow", "", cutlist.second.size(), 0, cutlist.second.size());
0093     cutflows[(cutlist.first + syst).Data()]->Sumw2();
0094     rawcutflows[(cutlist.first + syst).Data()]->Sumw2();
0095     cutflows[(cutlist.first + syst).Data()]->SetDirectory(0);
0096     rawcutflows[(cutlist.first + syst).Data()]->SetDirectory(0);
0097     for (unsigned int i = 0; i < cutlist.second.size(); ++i) {
0098       cutflows[(cutlist.first + syst).Data()]->GetXaxis()->SetBinLabel(i + 1, cutlist.second[i]);
0099       rawcutflows[(cutlist.first + syst).Data()]->GetXaxis()->SetBinLabel(i + 1, cutlist.second[i]);
0100     }
0101   }
0102   return std::make_tuple(cutflows, rawcutflows);
0103 }
0104 
0105 ////_______________________________________________________________________________________________________
0106 //void RooUtil::CutflowUtil::fillCutflowHistograms(RooUtil::CutflowUtil::CutNameListMap& cutlists, RooUtil::TTreeX& tx, std::map<TString, THist*>& cutflows, std::map<TString, THist*>& rawcutflows)
0107 //{
0108 //    std::map<TString, std::vector<TString>> obj = cutlists.getStdVersion();
0109 //    fillCutflowHistograms(obj, tx, cutflows, rawcutflows);
0110 //}
0111 //
0112 ////_______________________________________________________________________________________________________
0113 //void RooUtil::CutflowUtil::fillCutflowHistograms(std::map<TString, std::vector<TString>>& cutlists, RooUtil::TTreeX& tx, std::map<TString, THist*>& cutflows, std::map<TString, THist*>& rawcutflows)
0114 //{
0115 //    for (auto& cutlist : cutlists)
0116 //    {
0117 //        RooUtil::CutflowUtil::fillCutflow(cutlist.second, tx, cutflows[cutlist.first]);
0118 //        RooUtil::CutflowUtil::fillRawCutflow(cutlist.second, tx, rawcutflows[cutlist.first]);
0119 //    }
0120 //}
0121 
0122 //_______________________________________________________________________________________________________
0123 void RooUtil::CutflowUtil::saveCutflowHistograms(std::map<CUTFLOWMAPSTRING, THist*>& cutflows,
0124                                                  std::map<CUTFLOWMAPSTRING, THist*>& rawcutflows) {
0125   for (auto& cutflow : cutflows)
0126     cutflow.second->Write();
0127   for (auto& rawcutflow : rawcutflows)
0128     rawcutflow.second->Write();
0129 }