Logo ROOT   6.14/05
Reference Guide
StandardProfileLikelihoodDemo.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_roostats
3 /// \notebook
4 /// Standard demo of the Profile Likelihood calculator
5 /// StandardProfileLikelihoodDemo
6 ///
7 /// This is a standard demo that can be used with any ROOT file
8 /// prepared in the standard way. You specify:
9 /// - name for input ROOT file
10 /// - name of workspace inside ROOT file that holds model and data
11 /// - name of ModelConfig that specifies details for calculator tools
12 /// - name of dataset
13 ///
14 /// With default parameters the macro will attempt to run the
15 /// standard hist2workspace example and read the ROOT file
16 /// that it produces.
17 ///
18 /// The actual heart of the demo is only about 10 lines long.
19 ///
20 /// The ProfileLikelihoodCalculator is based on Wilks's theorem
21 /// and the asymptotic properties of the profile likelihood ratio
22 /// (eg. that it is chi-square distributed for the true value).
23 ///
24 /// \macro_image
25 /// \macro_output
26 /// \macro_code
27 ///
28 /// \author Kyle Cranmer
29 
30 #include "TFile.h"
31 #include "TROOT.h"
32 #include "TSystem.h"
33 #include "RooWorkspace.h"
34 #include "RooAbsData.h"
35 #include "RooRealVar.h"
36 
37 #include "RooStats/ModelConfig.h"
41 
42 using namespace RooFit;
43 using namespace RooStats;
44 
45 struct ProfileLikelihoodOptions {
46 
47 double confLevel = 0.95;
48 int nScanPoints = 50;
49 bool plotAsTF1 = false;
50 double poiMinPlot = 1;
51 double poiMaxPlot = 0;
52  bool doHypoTest = false;
53  double nullValue = 0;
54 
55 };
56 
57 ProfileLikelihoodOptions optPL;
58 
59 void StandardProfileLikelihoodDemo(const char* infile = "",
60  const char* workspaceName = "combined",
61  const char* modelConfigName = "ModelConfig",
62  const char* dataName = "obsData"){
63 
64  double confLevel = optPL.confLevel;
65  double nScanPoints = optPL.nScanPoints;
66  bool plotAsTF1 = optPL.plotAsTF1;
67  double poiXMin = optPL.poiMinPlot;
68  double poiXMax = optPL.poiMaxPlot;
69  bool doHypoTest = optPL.doHypoTest;
70  double nullParamValue = optPL.nullValue;
71 
72  // -------------------------------------------------------
73  // First part is just to access a user-defined file
74  // or create the standard example file if it doesn't exist
75  const char* filename = "";
76  if (!strcmp(infile,"")) {
77  filename = "results/example_combined_GaussExample_model.root";
78  bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
79  // if file does not exists generate with histfactory
80  if (!fileExist) {
81 #ifdef _WIN32
82  cout << "HistFactory file cannot be generated on Windows - exit" << endl;
83  return;
84 #endif
85  // Normally this would be run on the command line
86  cout <<"will run standard hist2workspace example"<<endl;
87  gROOT->ProcessLine(".! prepareHistFactory .");
88  gROOT->ProcessLine(".! hist2workspace config/example.xml");
89  cout <<"\n\n---------------------"<<endl;
90  cout <<"Done creating example input"<<endl;
91  cout <<"---------------------\n\n"<<endl;
92  }
93 
94  }
95  else
96  filename = infile;
97 
98  // Try to open the file
99  TFile *file = TFile::Open(filename);
100 
101  // if input file was specified byt not found, quit
102  if(!file ){
103  cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
104  return;
105  }
106 
107  // -------------------------------------------------------
108  // Tutorial starts here
109  // -------------------------------------------------------
110 
111  // get the workspace out of the file
112  RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
113  if(!w){
114  cout <<"workspace not found" << endl;
115  return;
116  }
117 
118  // get the modelConfig out of the file
119  ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);
120 
121  // get the modelConfig out of the file
122  RooAbsData* data = w->data(dataName);
123 
124  // make sure ingredients are found
125  if(!data || !mc){
126  w->Print();
127  cout << "data or ModelConfig was not found" <<endl;
128  return;
129  }
130 
131  // ---------------------------------------------
132  // create and use the ProfileLikelihoodCalculator
133  // to find and plot the 95% confidence interval
134  // on the parameter of interest as specified
135  // in the model config
136  ProfileLikelihoodCalculator pl(*data,*mc);
137  pl.SetConfidenceLevel(confLevel); // 95% interval
138  LikelihoodInterval* interval = pl.GetInterval();
139 
140  // print out the interval on the first Parameter of Interest
141  RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
142  cout << "\n>>>> RESULT : " << confLevel*100 << "% interval on " <<firstPOI->GetName()<<" is : ["<<
143  interval->LowerLimit(*firstPOI) << ", "<<
144  interval->UpperLimit(*firstPOI) <<"]\n "<<endl;
145 
146  // make a plot
147 
148  cout << "making a plot of the profile likelihood function ....(if it is taking a lot of time use less points or the TF1 drawing option)\n";
149  LikelihoodIntervalPlot plot(interval);
150  plot.SetNPoints(nScanPoints); // do not use too many points, it could become very slow for some models
151  if (poiXMin < poiXMax) plot.SetRange(poiXMin, poiXMax);
152  TString opt;
153  if (plotAsTF1) opt += TString("tf1");
154  plot.Draw(opt); // use option TF1 if too slow (plot.Draw("tf1")
155 
156 
157  // if requested perform also an hypothesis test for the significance
158  if (doHypoTest) {
159  RooArgSet nullparams("nullparams");
160  nullparams.addClone(*firstPOI);
161  nullparams.setRealValue(firstPOI->GetName(), nullParamValue);
162  pl.SetNullParameters(nullparams);
163  std::cout << "Perform Test of Hypothesis : null Hypothesis is " << firstPOI->GetName() << nullParamValue << std::endl;
164  auto result = pl.GetHypoTest();
165  std::cout << "\n>>>> Hypotheis Test Result \n";
166  result->Print();
167  }
168 
169 
170 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1276
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:30
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
This class provides simple and straightforward utilities to plot a LikelihoodInterval object...
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
#define gROOT
Definition: TROOT.h:410
Double_t LowerLimit(const RooRealVar &param)
return the lower bound of the interval on a given parameter
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found...
Double_t UpperLimit(const RooRealVar &param)
return the upper bound of the interval on a given parameter
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
RooAbsArg * first() const
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
TObject * obj(const char *name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name) ...
Namespace for the RooStats classes.
Definition: Asimov.h:20
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:225
Definition: file.py:1
void Print(Option_t *opts=0) const
Print contents of the workspace.
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:43