ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
plot_likelihood.cxx
Go to the documentation of this file.
1 #include "plot_likelihood.hpp"
2 
3 #include <iostream>
4 #include <string>
5 #include <stdexcept>
6 #include <algorithm>
7 #include <limits>
8 
9 #include <stdlib.h>
10 #include <getopt.h>
11 
12 #include "TFile.h"
13 #include "TH1D.h"
14 #include "TCanvas.h"
15 
16 #include "RooRealVar.h"
17 #include "RooArgList.h"
18 #include "RooMinuit.h"
19 #include "RooFitResult.h"
20 #include "RooAbsPdf.h"
21 #include "RooDataSet.h"
22 
23 #include "utilities.hpp"
24 #include "styles.hpp"
25 
26 using namespace std;
27 
28 namespace{
29  string file_name = "";
30  string workspace_name = "w";
31 }
32 
33 int main(int argc, char *argv[]){
34  GetOptions(argc, argv);
35 
36  if(file_name == ""){
37  ERROR("Specify name of file containing workspace with -f.");
38  }
39  execute("export blah=$(pwd); cd ~/cmssw/CMSSW_7_1_5/src; eval `scramv1 runtime -sh`; cd $blah; combine -M MaxLikelihoodFit --saveWorkspace --saveWithUncertainties --minos=all -w "+workspace_name+" "+file_name);
40 
41  styles style("RA4");
42  style.setDefaultStyle();
43 
44  string fit_workspace_file_name("higgsCombineTest.MaxLikelihoodFit.mH120.root");
45  TFile fit_workspace_file(fit_workspace_file_name.c_str(),"read");
46  if(!fit_workspace_file.IsOpen()){
47  ERROR("File "+fit_workspace_file_name+" was not produced.");
48  }
49  RooWorkspace *w = static_cast<RooWorkspace*>(fit_workspace_file.Get(workspace_name.c_str()));
50  if(w == nullptr) {
51  ERROR("Workspace "+workspace_name+" not found in file "+fit_workspace_file_name);
52  }
53 
54  string fit_file_name("mlfit.root");
55  TFile fit_file(fit_file_name.c_str(), "read");
56  if(!fit_file.IsOpen()){
57  ERROR("File "+fit_file_name+" was not produced.");
58  }
59 
60  string out_file_name = ChangeExtension(file_name, "_profiles.root");
61  TFile out_file(out_file_name.c_str(), "recreate");
62  if(!out_file.IsOpen()){
63  ERROR("Could not open output file "+out_file_name);
64  }
65  out_file.cd();
66 
67  RooFitResult *fit_b = static_cast<RooFitResult*>(fit_file.Get("fit_b"));
68  if(fit_b != nullptr){
69  SetVariables(*w, *fit_b);
70  PlotVars(*w, true);
71  }
72  RooFitResult *fit_s = static_cast<RooFitResult*>(fit_file.Get("fit_s"));
73  if(fit_s != nullptr){
74  SetVariables(*w, *fit_s);
75  PlotVars(*w, false);
76  }
77 
78  out_file.Close();
79  fit_file.Close();
80  fit_workspace_file.Close();
81 }
82 
83 void PlotVars(RooWorkspace &w, bool bkg_only){
84  RooAbsPdf *model = w.pdf(bkg_only ? "model_b" : "model_s");
85  if(model == nullptr) return;
86  RooDataSet *data = static_cast<RooDataSet*>(w.data("data_obs"));
87  if(data ==nullptr) return;
88  RooAbsReal *nll = model->createNLL(*data);
89  if(nll == nullptr) return;
90  RooMinuit m(*nll);
91  m.migrad();
92  TIter iter(w.allVars().createIterator());
93  int size = w.allVars().getSize();
94  RooRealVar *arg = nullptr;
95  int i = 0;
96  while((arg = static_cast<RooRealVar*>(iter())) && i < size){
97  ++i;
98  if(arg == nullptr) continue;
99  if(arg->isConstant()) continue;
100  string name = arg->GetName();
101  double vmin = arg->getMin();
102  double vmax = arg->getMax();
103  double vval = arg->getVal();
104  double vehi = arg->getErrorHi();
105  double velo = arg->getErrorLo();
106  double verr = arg->getError();
107  cout << name << ": " << vmin << " " << (vval+velo) << " " << vval << " (" << verr << ") " << (vval+vehi) << " " << vmax << endl;
108  double low = max(vmin, vval+5*velo);
109  double high = min(vmax, vval+5*vehi);
110  TH1D h("", (";"+name+";-2 log(L)").c_str(), 11, low, high);
111  double minval=numeric_limits<double>::max();
112  for(int bin = 1; bin <= h.GetNbinsX(); ++bin){
113  arg->setVal(h.GetBinCenter(bin));
114  arg->setConstant(true);
115  m.migrad();
116  RooFitResult *f = m.save();
117  if(f == nullptr) continue;
118  double val = f->minNll();
119  h.SetBinContent(bin, val);
120  if(val<minval) minval = val;
121  }
122  for(int bin = 1; bin <= h.GetNbinsX(); ++bin){
123  h.SetBinContent(bin, 2.*(h.GetBinContent(bin)-minval));
124  }
125  arg->setVal(vval);
126  arg->setConstant(false);
127  TCanvas c;
128  h.Draw();
129  c.Print((name+".pdf").c_str());
130  }
131  iter.Reset();
132 }
133 
134 vector<string> GetVarNames(const RooWorkspace &w){
135  vector<string> names;
136  TIter iter(w.allVars().createIterator());
137  int size = w.allVars().getSize();
138  TObject *obj = nullptr;
139  int i = 0;
140  while((obj = iter()) && i < size){
141  ++i;
142  if(obj == nullptr) continue;
143  string name = obj->GetName();
144  Append(names, name);
145  }
146  iter.Reset();
147  sort(names.begin(), names.end());
148  return names;
149 }
150 
151 RooRealVar * SetVariables(RooWorkspace &w,
152  const RooFitResult &f){
153  bool set_r = false;
154  RooArgList pars = f.floatParsFinal();
155  for(int ipar = 0; ipar < pars.getSize(); ++ipar){
156  RooRealVar *fit_var = static_cast<RooRealVar*>(pars.at(ipar));
157  if(fit_var == nullptr) continue;
158  RooRealVar *w_var = w.var(fit_var->GetName());
159  if(w_var == nullptr) continue;
160  w_var->setMin(fit_var->getMin());
161  w_var->setMax(fit_var->getMax());
162  w_var->setVal(fit_var->getVal());
163  w_var->setError(fit_var->getError());
164  if(fit_var->GetName() == string("r")) set_r = true;
165  }
166 
167  RooRealVar *r_var = static_cast<RooRealVar*>(w.var("r"));
168  if(r_var != nullptr){
169  if(!set_r){
170  r_var->setVal(0);
171  r_var->setConstant(true);
172  }else{
173  r_var->setConstant(false);
174  }
175  }
176  return r_var;
177 }
178 
179 void GetOptions(int argc, char *argv[]){
180  while(true){
181  static struct option long_options[] = {
182  {"file", required_argument, 0, 'f'},
183  {"workspace", required_argument, 0, 'w'},
184  {0, 0, 0, 0}
185  };
186 
187  char opt = -1;
188  int option_index;
189  opt = getopt_long(argc, argv, "f:w:", long_options, &option_index);
190  if( opt == -1) break;
191 
192  string optname;
193  switch(opt){
194  case 'w':
195  workspace_name = optarg;
196  break;
197  case 'f':
198  file_name = optarg;
199  break;
200  default:
201  printf("Bad option! getopt_long returned character code 0%o\n", opt);
202  break;
203  }
204  }
205 }
void Append(T &collection, const typename T::value_type &value)
Definition: utilities.hpp:48
void GetOptions(int argc, char *argv[])
void PlotVars(RooWorkspace &w, bool bkg_only)
void setDefaultStyle()
Definition: styles.cpp:36
STL namespace.
std::string execute(const std::string &cmd)
Definition: utilities.cpp:87
std::string ChangeExtension(std::string path, const std::string &new_ext)
Definition: utilities.cpp:115
RooRealVar * SetVariables(RooWorkspace &w, const RooFitResult &f)
#define ERROR(x)
Definition: utilities.hpp:15
def style(h, width, style, color)
vector< string > GetVarNames(const RooWorkspace &w)
int main(int argc, char *argv[])