ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StandardTestStatDistributionDemo.C
Go to the documentation of this file.
1 /*
2 StandardTestStatDistributionDemo.C
3 author Kyle Cranmer
4 date: summer solstice, 2011
5 
6 This simple script plots the sampling distribution of the profile likelihood
7 ratio test statistic based on the input Model File. To do this one needs to
8 specify the value of the parameter of interest that will be used for evaluating
9 the test statistic and the value of the parameters used for generating the toy data.
10 In this case, it uses the upper-limit estimated from the ProfileLikleihoodCalculator,
11 which assumes the asymptotic chi-square distribution for -2 log profile likleihood ratio.
12 Thus, the script is handy for checking to see if the asymptotic approximations are valid.
13 To aid, that comparison, the script overlays a chi-square distribution as well.
14 The most common parameter of interest is a parameter proportional to the signal rate,
15 and often that has a lower-limit of 0, which breaks the standard chi-square distribution.
16 Thus the script allows the parameter to be negative so that the overlay chi-square is
17 the correct asymptotic distribution.
18 */
19 
20 #include "TFile.h"
21 #include "TROOT.h"
22 #include "TH1F.h"
23 #include "TCanvas.h"
24 #include "TSystem.h"
25 #include "TF1.h"
26 #include "TSystem.h"
27 
28 #include "RooWorkspace.h"
29 #include "RooAbsData.h"
30 
31 #include "RooStats/ModelConfig.h"
33 #include "RooStats/ToyMCSampler.h"
36 
42 
43 using namespace RooFit;
44 using namespace RooStats;
45 
46 
47 bool useProof = false; // flag to control whether to use Proof
48 int nworkers = 0; // number of workers (default use all available cores)
49 
50 /////////////////////////////////////////////////////////////////////////
51 // The actual macro
52 
54  const char* workspaceName = "combined",
55  const char* modelConfigName = "ModelConfig",
56  const char* dataName = "obsData"){
57 
58 
59  // the number of toy MC used to generate the distribution
60  int nToyMC = 1000;
61  // The parameter below is needed for asymptotic distribution to be chi-square,
62  // but set to false if your model is not numerically stable if mu<0
63  bool allowNegativeMu=true;
64 
65 
66  /////////////////////////////////////////////////////////////
67  // First part is just to access a user-defined file
68  // or create the standard example file if it doesn't exist
69  ////////////////////////////////////////////////////////////
70  const char* filename = "";
71  if (!strcmp(infile,"")) {
72  filename = "results/example_combined_GaussExample_model.root";
73  bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
74  // if file does not exists generate with histfactory
75  if (!fileExist) {
76 #ifdef _WIN32
77  cout << "HistFactory file cannot be generated on Windows - exit" << endl;
78  return;
79 #endif
80  // Normally this would be run on the command line
81  cout <<"will run standard hist2workspace example"<<endl;
82  gROOT->ProcessLine(".! prepareHistFactory .");
83  gROOT->ProcessLine(".! hist2workspace config/example.xml");
84  cout <<"\n\n---------------------"<<endl;
85  cout <<"Done creating example input"<<endl;
86  cout <<"---------------------\n\n"<<endl;
87  }
88 
89  }
90  else
91  filename = infile;
92 
93  // Try to open the file
94  TFile *file = TFile::Open(filename);
95 
96  // if input file was specified byt not found, quit
97  if(!file ){
98  cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
99  return;
100  }
101 
102 
103  /////////////////////////////////////////////////////////////
104  // Now get the data and workspace
105  ////////////////////////////////////////////////////////////
106 
107  // get the workspace out of the file
108  RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
109  if(!w){
110  cout <<"workspace not found" << endl;
111  return;
112  }
113 
114  // get the modelConfig out of the file
115  ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);
116 
117  // get the modelConfig out of the file
118  RooAbsData* data = w->data(dataName);
119 
120  // make sure ingredients are found
121  if(!data || !mc){
122  w->Print();
123  cout << "data or ModelConfig was not found" <<endl;
124  return;
125  }
126 
127  mc->Print();
128  /////////////////////////////////////////////////////////////
129  // Now find the upper limit based on the asymptotic results
130  ////////////////////////////////////////////////////////////
131  RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
132  ProfileLikelihoodCalculator plc(*data,*mc);
133  LikelihoodInterval* interval = plc.GetInterval();
134  double plcUpperLimit = interval->UpperLimit(*firstPOI);
135  delete interval;
136  cout << "\n\n--------------------------------------"<<endl;
137  cout <<"Will generate sampling distribution at " << firstPOI->GetName() << " = " << plcUpperLimit <<endl;
138  int nPOI = mc->GetParametersOfInterest()->getSize();
139  if(nPOI>1){
140  cout <<"not sure what to do with other parameters of interest, but here are their values"<<endl;
141  mc->GetParametersOfInterest()->Print("v");
142  }
143 
144  /////////////////////////////////////////////
145  // create thte test stat sampler
147 
148  // to avoid effects from boundary and simplify asymptotic comparison, set min=-max
149  if(allowNegativeMu)
150  firstPOI->setMin(-1*firstPOI->getMax());
151 
152  // temporary RooArgSet
153  RooArgSet poi;
154  poi.add(*mc->GetParametersOfInterest());
155 
156  // create and configure the ToyMCSampler
157  ToyMCSampler sampler(ts,nToyMC);
158  sampler.SetPdf(*mc->GetPdf());
159  sampler.SetObservables(*mc->GetObservables());
161  if(!mc->GetPdf()->canBeExtended() && (data->numEntries()==1)){
162  cout << "tell it to use 1 event"<<endl;
163  sampler.SetNEventsPerToy(1);
164  }
165  firstPOI->setVal(plcUpperLimit); // set POI value for generation
166  sampler.SetParametersForTestStat(*mc->GetParametersOfInterest()); // set POI value for evaluation
167 
168  if (useProof) {
169  ProofConfig pc(*w, nworkers, "",false);
170  sampler.SetProofConfig(&pc); // enable proof
171  }
172 
173  firstPOI->setVal(plcUpperLimit);
174  RooArgSet allParameters;
175  allParameters.add(*mc->GetParametersOfInterest());
176  allParameters.add(*mc->GetNuisanceParameters());
177  allParameters.Print("v");
178 
179  SamplingDistribution* sampDist = sampler.GetSamplingDistribution(allParameters);
181  plot.AddSamplingDistribution(sampDist);
182  plot.GetTH1F(sampDist)->GetYaxis()->SetTitle(Form("f(-log #lambda(#mu=%.2f) | #mu=%.2f)",plcUpperLimit,plcUpperLimit));
183  plot.SetAxisTitle(Form("-log #lambda(#mu=%.2f)",plcUpperLimit));
184 
185  TCanvas* c1 = new TCanvas("c1");
186  c1->SetLogy();
187  plot.Draw();
188  double min = plot.GetTH1F(sampDist)->GetXaxis()->GetXmin();
189  double max = plot.GetTH1F(sampDist)->GetXaxis()->GetXmax();
190 
191  TF1* f = new TF1("f",Form("2*ROOT::Math::chisquared_pdf(2*x,%d,0)",nPOI),min,max);
192  f->Draw("same");
193  c1->SaveAs("standard_test_stat_distribution.pdf");
194 
195 }
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
Holds configuration options for proof and proof-lite.
Definition: ProofConfig.h:49
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
Definition: ModelConfig.h:244
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:185
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...
virtual void SetGlobalObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:205
TCanvas * c1
Definition: legend1.C:2
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return NULL if not existing)
Definition: ModelConfig.h:265
ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface class f...
static const char * filename()
Double_t AddSamplingDistribution(const SamplingDistribution *samplingDist, Option_t *drawOptions="NORMALIZE HIST")
adds the sampling distribution and returns the scale factor
virtual void SetNEventsPerToy(const Int_t nevents)
Definition: ToyMCSampler.h:177
#define gROOT
Definition: TROOT.h:344
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:250
TFile * f
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
void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
tuple infile
Definition: mrt.py:15
virtual void setVal(Double_t value)
Set value of variable to 'value'.
Definition: RooRealVar.cxx:203
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
void setMin(const char *name, Double_t value)
Set minimum of name range to given value.
Definition: RooRealVar.cxx:386
void SetAxisTitle(char *varName)
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:293
void SetProofConfig(ProofConfig *pc=NULL)
Definition: ToyMCSampler.h:263
char * Form(const char *fmt,...)
tuple w
Definition: qtexample.py:51
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
ProfileLikelihoodTestStat is an implementation of the TestStatistic interface that calculates the pro...
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:99
This class simply holds a sampling distribution of some test statistic.
virtual void Print(Option_t *option="") const
overload the print method
Definition: ModelConfig.cxx:90
const RooArgSet * GetObservables() const
get RooArgSet for observables (return NULL if not existing)
Definition: ModelConfig.h:259
TH1F * GetTH1F(const SamplingDistribution *samplDist=NULL)
Returns the TH1F associated with the give SamplingDistribution.
tuple file
Definition: fildir.py:20
virtual void SetObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:203
Bool_t canBeExtended() const
Definition: RooAbsPdf.h:216
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:190
virtual SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint)
TObject * obj(const char *name) const
Return any type of object (RooAbsArg, RooAbsData or generic object) with given name) ...
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
This class provides simple and straightforward utilities to plot SamplingDistribution objects...
virtual Double_t getMax(const char *name=0) const
void StandardTestStatDistributionDemo(const char *infile="", const char *workspaceName="combined", const char *modelConfigName="ModelConfig", const char *dataName="obsData")
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
Int_t getSize() const
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42