ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
get_dilepton_uncertainties.cxx
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <fstream>
6 #include <sstream>
7 #include <algorithm>
8 
9 #include "TFile.h"
10 
11 #include "RooArgList.h"
12 #include "RooRealVar.h"
13 
14 #include "utilities.hpp"
15 
16 using namespace std;
17 
18 int main(int argc, char *argv[]){
19  for(int argi = 1; argi < argc; ++argi){
20  execute("export blah=$(pwd); cd ~/cmssw/CMSSW_7_1_5/src; eval `scramv1 runtime -sh`; cd $blah; combine -M MaxLikelihoodFit --saveWorkspace "+string(argv[argi]));
21 
22  TFile w_file("higgsCombineTest.MaxLikelihoodFit.mH120.root","read");
23  if(!w_file.IsOpen()) continue;
24  RooWorkspace *w = static_cast<RooWorkspace*>(w_file.Get("w"));
25  if(w == nullptr) continue;
26 
27  TFile fit_file("mlfit.root","read");
28  if(!fit_file.IsOpen()) continue;
29  RooFitResult *fit_b = static_cast<RooFitResult*>(fit_file.Get("fit_b"));
30  if(fit_b == nullptr) continue;
31 
32  SetVariables(*w, *fit_b);
33 
34  string outname = ChangeExtension(argv[argi], "_dilep.txt");
35  ostringstream out;
36  out << "SYSTEMATIC dilep_closure\n";
37  out << " PROCESS ttbar,other\n";
38  vector<string> bin_names = GetBinNames(*w);
39  for(const auto &bin_name: bin_names){
40  double yield = GetBkgPred(*w, bin_name);
41  double uncert = GetBkgPredErr(*w, *fit_b, bin_name);
42 
43  double frac = yield > 0. ? uncert/yield : 2.;
44  out << " " << bin_name << ' ' << frac << '\n';
45  }
46  out << flush;
47  ofstream outfile(outname);
48  outfile << out.str();
49  cout << "Dilepton closure written to " << outname << ":\n";
50  cout << out.str() << endl;
51  }
52 }
53 
54 vector<string> GetBinNames(const RooWorkspace &w){
55  vector<string> names;
56  TIter iter(w.allFunctions().createIterator());
57  int size = w.allFunctions().getSize();
58  TObject *obj;
59  int i = 0;
60  while((obj = iter()) && i < size){
61  ++i;
62  RooAbsArg *arg = static_cast<RooAbsArg*>(obj);
63  if(arg == nullptr) continue;
64  string name = arg->GetName();
65  if(name.substr(0,9) != "nexp_BLK_") continue;
66  auto bpos = name.find("_BIN_");
67  auto ppos = name.find("_PRC_");
68  if(bpos == string::npos) continue;
69  string bin_name = name.substr(bpos+5, ppos-bpos-5);
70  Append(names, bin_name);
71  }
72  iter.Reset();
73  reverse(names.begin(), names.end());
74  return names;
75 }
76 
77 double GetBkgPred(const RooWorkspace &w,
78  const string &bin_name){
79  TIter iter(w.allFunctions().createIterator());
80  int size = w.allFunctions().getSize();
81  TObject *obj;
82  int i = 0;
83  while((obj = iter()) && i < size){
84  ++i;
85  RooAbsArg *arg = static_cast<RooAbsArg*>(obj);
86  if(arg == nullptr) continue;
87  string name = arg->GetName();
88  if(name.substr(0,9) != "nbkg_BLK_") continue;
89  if(!(Contains(name, "_BIN_"+bin_name))) continue;
90  if(Contains(name, "_PRC_")) continue;
91  return static_cast<RooRealVar*>(arg)->getVal();
92  }
93  iter.Reset();
94  return -1.;
95 }
96 
97 double GetBkgPredErr(const RooWorkspace &w,
98  const RooFitResult &f,
99  const string &bin_name){
100  TIter iter(w.allFunctions().createIterator());
101  int size = w.allFunctions().getSize();
102  TObject *obj;
103  int i = 0;
104  while((obj = iter()) && i < size){
105  ++i;
106  RooAbsArg *arg = static_cast<RooAbsArg*>(obj);
107  if(arg == nullptr) continue;
108  string name = arg->GetName();
109  if(name.substr(0,9) != "nbkg_BLK_") continue;
110  if(!(Contains(name, "_BIN_"+bin_name))) continue;
111  if(Contains(name, "_PRC_")) continue;
112  return static_cast<RooRealVar*>(arg)->getPropagatedError(f);
113  }
114  iter.Reset();
115  return -1.;
116 }
117 
118 RooRealVar * SetVariables(RooWorkspace &w,
119  const RooFitResult &f){
120  bool set_r = false;
121  RooArgList pars = f.floatParsFinal();
122  for(int ipar = 0; ipar < pars.getSize(); ++ipar){
123  RooRealVar *fit_var = static_cast<RooRealVar*>(pars.at(ipar));
124  if(fit_var == nullptr) continue;
125  RooRealVar *w_var = w.var(fit_var->GetName());
126  if(w_var == nullptr) continue;
127  w_var->setVal(fit_var->getVal());
128  if(fit_var->GetName() == string("r")) set_r = true;
129  }
130  RooRealVar *r_var = static_cast<RooRealVar*>(w.var("r"));
131  if(r_var != nullptr){
132  if(!set_r){
133  r_var->setVal(0);
134  r_var->setConstant(true);
135  }else{
136  r_var->setConstant(false);
137  }
138  }
139  return r_var;
140 }
void Append(T &collection, const typename T::value_type &value)
Definition: utilities.hpp:48
double GetBkgPredErr(const RooWorkspace &w, const RooFitResult &f, const string &bin_name)
STL namespace.
bool Contains(const std::string &str, const std::string &pat)
Definition: utilities.cpp:26
std::string execute(const std::string &cmd)
Definition: utilities.cpp:87
int main(int argc, char *argv[])
std::string ChangeExtension(std::string path, const std::string &new_ext)
Definition: utilities.cpp:115
double GetBkgPred(const RooWorkspace &w, const string &bin_name)
RooRealVar * SetVariables(RooWorkspace &w, const RooFitResult &f)
vector< string > GetBinNames(const RooWorkspace &w)