ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StandardProfileLikelihoodDemo.C
Go to the documentation of this file.
1 // Standard demo of the Profile Likelihood calculcator
2 /*
3 StandardProfileLikelihoodDemo
4 
5 Author: Kyle Cranmer
6 date: Dec. 2010
7 
8 This is a standard demo that can be used with any ROOT file
9 prepared in the standard way. You specify:
10  - name for input ROOT file
11  - name of workspace inside ROOT file that holds model and data
12  - name of ModelConfig that specifies details for calculator tools
13  - name of dataset
14 
15 With default parameters the macro will attempt to run the
16 standard hist2workspace example and read the ROOT file
17 that it produces.
18 
19 The actual heart of the demo is only about 10 lines long.
20 
21 The ProfileLikelihoodCalculator is based on Wilks's theorem
22 and the asymptotic properties of the profile likeihood ratio
23 (eg. that it is chi-square distributed for the true value).
24 */
25 
26 #include "TFile.h"
27 #include "TROOT.h"
28 #include "TSystem.h"
29 #include "RooWorkspace.h"
30 #include "RooAbsData.h"
31 #include "RooRealVar.h"
32 
33 #include "RooStats/ModelConfig.h"
37 
38 using namespace RooFit;
39 using namespace RooStats;
40 
41 void StandardProfileLikelihoodDemo(const char* infile = "",
42  const char* workspaceName = "combined",
43  const char* modelConfigName = "ModelConfig",
44  const char* dataName = "obsData"){
45 
46  /////////////////////////////////////////////////////////////
47  // First part is just to access a user-defined file
48  // or create the standard example file if it doesn't exist
49  ////////////////////////////////////////////////////////////
50  const char* filename = "";
51  if (!strcmp(infile,"")) {
52  filename = "results/example_combined_GaussExample_model.root";
53  bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
54  // if file does not exists generate with histfactory
55  if (!fileExist) {
56 #ifdef _WIN32
57  cout << "HistFactory file cannot be generated on Windows - exit" << endl;
58  return;
59 #endif
60  // Normally this would be run on the command line
61  cout <<"will run standard hist2workspace example"<<endl;
62  gROOT->ProcessLine(".! prepareHistFactory .");
63  gROOT->ProcessLine(".! hist2workspace config/example.xml");
64  cout <<"\n\n---------------------"<<endl;
65  cout <<"Done creating example input"<<endl;
66  cout <<"---------------------\n\n"<<endl;
67  }
68 
69  }
70  else
71  filename = infile;
72 
73  // Try to open the file
74  TFile *file = TFile::Open(filename);
75 
76  // if input file was specified byt not found, quit
77  if(!file ){
78  cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
79  return;
80  }
81 
82  /////////////////////////////////////////////////////////////
83  // Tutorial starts here
84  ////////////////////////////////////////////////////////////
85 
86  // get the workspace out of the file
87  RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
88  if(!w){
89  cout <<"workspace not found" << endl;
90  return;
91  }
92 
93  // get the modelConfig out of the file
94  ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);
95 
96  // get the modelConfig out of the file
97  RooAbsData* data = w->data(dataName);
98 
99  // make sure ingredients are found
100  if(!data || !mc){
101  w->Print();
102  cout << "data or ModelConfig was not found" <<endl;
103  return;
104  }
105 
106  /////////////////////////////////////////////
107  // create and use the ProfileLikelihoodCalculator
108  // to find and plot the 95% confidence interval
109  // on the parameter of interest as specified
110  // in the model config
111  ProfileLikelihoodCalculator pl(*data,*mc);
112  pl.SetConfidenceLevel(0.95); // 95% interval
113  LikelihoodInterval* interval = pl.GetInterval();
114 
115  // print out the iterval on the first Parameter of Interest
116  RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
117  cout << "\n95% interval on " <<firstPOI->GetName()<<" is : ["<<
118  interval->LowerLimit(*firstPOI) << ", "<<
119  interval->UpperLimit(*firstPOI) <<"] "<<endl;
120 
121  // make a plot
122 
123  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";
124  LikelihoodIntervalPlot plot(interval);
125  plot.SetNPoints(50); // do not use too many points, it could become very slow for some models
126  plot.Draw(""); // use option TF1 if too slow (plot.Draw("tf1")
127 
128 
129 }
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:1213
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
void Draw(const Option_t *options=0)
draw the likelihood interval or contour for the 1D case a RooPlot is drawn by default of the profiled...
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found...
This class provides simple and straightforward utilities to plot a LikelihoodInterval object...
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
static const char * filename()
#define gROOT
Definition: TROOT.h:344
Double_t LowerLimit(const RooRealVar &param)
return the lower bound of the interval on a given parameter
virtual LikelihoodInterval * GetInterval() const
Return a likelihood interval.
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:3851
RooAbsArg * first() const
void plot(TString fname="data.root", TString var0="var0", TString var1="var1")
Definition: createData.C:17
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
tuple infile
Definition: mrt.py:15
void StandardProfileLikelihoodDemo(const char *infile="", const char *workspaceName="combined", const char *modelConfigName="ModelConfig", const char *dataName="obsData")
Double_t UpperLimit(const RooRealVar &param)
return the upper bound of the interval on a given parameter
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
tuple w
Definition: qtexample.py:51
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
tuple pl
Definition: first.py:10
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
tuple file
Definition: fildir.py:20
virtual void SetConfidenceLevel(Double_t cl)
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval) ...
TObject * obj(const char *name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name) ...
void Print(Option_t *opts=0) const
Print contents of the workspace.
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:247
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42