Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
StandardTestStatDistributionDemo.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roostats
3/// \notebook
4/// StandardTestStatDistributionDemo.C
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 likelihood 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/// \macro_image
20/// \macro_output
21/// \macro_code
22///
23/// \author Kyle Cranmer
24
25#include "TFile.h"
26#include "TROOT.h"
27#include "TH1F.h"
28#include "TCanvas.h"
29#include "TSystem.h"
30#include "TF1.h"
31#include "TSystem.h"
32
33#include "RooWorkspace.h"
34#include "RooAbsData.h"
35
41
47
48using namespace RooFit;
49using namespace RooStats;
50
51// -------------------------------------------------------
52// The actual macro
53
54void StandardTestStatDistributionDemo(const char *infile = "", const char *workspaceName = "combined",
55 const char *modelConfigName = "ModelConfig", const char *dataName = "obsData")
56{
57
58 // the number of toy MC used to generate the distribution
59 int nToyMC = 1000;
60 // The parameter below is needed for asymptotic distribution to be chi-square,
61 // but set to false if your model is not numerically stable if mu<0
62 bool allowNegativeMu = true;
63
64 // -------------------------------------------------------
65 // First part is just to access a user-defined file
66 // or create the standard example file if it doesn't exist
67 const char *filename = "";
68 if (!strcmp(infile, "")) {
69 filename = "results/example_combined_GaussExample_model.root";
70 bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
71 // if file does not exists generate with histfactory
72 if (!fileExist) {
73 // Normally this would be run on the command line
74 cout << "will run standard hist2workspace example" << endl;
75 gROOT->ProcessLine(".! prepareHistFactory .");
76 gROOT->ProcessLine(".! hist2workspace config/example.xml");
77 cout << "\n\n---------------------" << endl;
78 cout << "Done creating example input" << endl;
79 cout << "---------------------\n\n" << endl;
80 }
81
82 } else
83 filename = infile;
84
85 // Try to open the file
86 TFile *file = TFile::Open(filename);
87
88 // if input file was specified but not found, quit
89 if (!file) {
90 cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
91 return;
92 }
93
94 // -------------------------------------------------------
95 // Now get the data and workspace
96
97 // get the workspace out of the file
99 if (!w) {
100 cout << "workspace not found" << endl;
101 return;
102 }
103
104 // get the modelConfig out of the file
105 ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
106
107 // get the modelConfig out of the file
109
110 // make sure ingredients are found
111 if (!data || !mc) {
112 w->Print();
113 cout << "data or ModelConfig was not found" << endl;
114 return;
115 }
116
117 mc->Print();
118 // -------------------------------------------------------
119 // Now find the upper limit based on the asymptotic results
120 RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();
121 ProfileLikelihoodCalculator plc(*data, *mc);
122 LikelihoodInterval *interval = plc.GetInterval();
123 double plcUpperLimit = interval->UpperLimit(*firstPOI);
124 delete interval;
125 cout << "\n\n--------------------------------------" << endl;
126 cout << "Will generate sampling distribution at " << firstPOI->GetName() << " = " << plcUpperLimit << endl;
127 int nPOI = mc->GetParametersOfInterest()->getSize();
128 if (nPOI > 1) {
129 cout << "not sure what to do with other parameters of interest, but here are their values" << endl;
130 mc->GetParametersOfInterest()->Print("v");
131 }
132
133 // -------------------------------------------------------
134 // create the test stat sampler
136
137 // to avoid effects from boundary and simplify asymptotic comparison, set min=-max
138 if (allowNegativeMu)
139 firstPOI->setMin(-1 * firstPOI->getMax());
140
141 // temporary RooArgSet
142 RooArgSet poi;
143 poi.add(*mc->GetParametersOfInterest());
144
145 // create and configure the ToyMCSampler
146 ToyMCSampler sampler(ts, nToyMC);
147 sampler.SetPdf(*mc->GetPdf());
148 sampler.SetObservables(*mc->GetObservables());
149 sampler.SetGlobalObservables(*mc->GetGlobalObservables());
150 if (!mc->GetPdf()->canBeExtended() && (data->numEntries() == 1)) {
151 cout << "tell it to use 1 event" << endl;
152 sampler.SetNEventsPerToy(1);
153 }
154 firstPOI->setVal(plcUpperLimit); // set POI value for generation
155 sampler.SetParametersForTestStat(*mc->GetParametersOfInterest()); // set POI value for evaluation
156
157 firstPOI->setVal(plcUpperLimit);
158 RooArgSet allParameters;
159 allParameters.add(*mc->GetParametersOfInterest());
160 allParameters.add(*mc->GetNuisanceParameters());
161 allParameters.Print("v");
162
163 SamplingDistribution *sampDist = sampler.GetSamplingDistribution(allParameters);
164 SamplingDistPlot plot;
165 plot.AddSamplingDistribution(sampDist);
166 plot.GetTH1F(sampDist)->GetYaxis()->SetTitle(
167 Form("f(-log #lambda(#mu=%.2f) | #mu=%.2f)", plcUpperLimit, plcUpperLimit));
168 plot.SetAxisTitle(Form("-log #lambda(#mu=%.2f)", plcUpperLimit));
169
170 TCanvas *c1 = new TCanvas("c1");
171 c1->SetLogy();
172 plot.Draw();
173 double min = plot.GetTH1F(sampDist)->GetXaxis()->GetXmin();
174 double max = plot.GetTH1F(sampDist)->GetXaxis()->GetXmax();
175
176 TF1 *f = new TF1("f", Form("2*ROOT::Math::chisquared_pdf(2*x,%d,0)", nPOI), min, max);
177 f->Draw("same");
178 c1->SaveAs("standard_test_stat_distribution.pdf");
179}
#define f(i)
Definition RSha256.hxx:104
#define gROOT
Definition TROOT.h:417
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
externTSystem * gSystem
Definition TSystem.h:582
Int_t getSize() const
Return the number of elements in the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsArg * first() const
void Print(Option_t *options=nullptr) const override
This method must be overridden when a class wants to print itself.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:56
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:214
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
void setMin(const char *name, double value, bool shared=true)
Set minimum of name range to given value.
LikelihoodInterval is a concrete implementation of the RooStats::ConfInterval interface.
double UpperLimit(const RooRealVar &param)
return the upper bound of the interval on a given parameter
const RooArgSet * GetGlobalObservables() const
get RooArgSet for global observables (return nullptr if not existing)
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
void Print(Option_t *option="") const override
overload the print method
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
The ProfileLikelihoodCalculator is a concrete implementation of CombinedCalculator (the interface cla...
ProfileLikelihoodTestStat is an implementation of the TestStatistic interface that calculates the pro...
This class provides simple and straightforward utilities to plot SamplingDistribution objects.
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
double AddSamplingDistribution(const SamplingDistribution *samplingDist, Option_t *drawOptions="NORMALIZE HIST")
adds the sampling distribution and returns the scale factor
TH1F * GetTH1F(const SamplingDistribution *sampleDist=nullptr)
Returns the TH1F associated with the give SamplingDistribution.
void SetAxisTitle(char *varName)
This class simply holds a sampling distribution of some test statistic.
const std::vector< double > & GetSamplingDistribution() const
Get test statistics values.
ToyMCSampler is an implementation of the TestStatSampler interface.
Persistable container for RooFit projects.
Double_t GetXmax() const
Definition TAxis.h:142
Double_t GetXmin() const
Definition TAxis.h:141
The Canvas class.
Definition TCanvas.h:23
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
Definition TF1.h:182
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3787
TAxis * GetXaxis()
Definition TH1.h:571
TAxis * GetYaxis()
Definition TH1.h:572
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
return c1
Definition legend1.C:41
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:72
RooStats::ModelConfig ModelConfig
Namespace for the RooStats classes.
Definition CodegenImpl.h:66
__device__ AFloat max(AFloat x, AFloat y)
Definition Kernels.cuh:207