Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
StandardFrequentistDiscovery.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roostats
3/// \notebook
4/// StandardFrequentistDiscovery
5///
6/// This is a standard demo that can be used with any ROOT file
7/// prepared in the standard way. You specify:
8/// - name for input ROOT file
9/// - name of workspace inside ROOT file that holds model and data
10/// - name of ModelConfig that specifies details for calculator tools
11/// - name of dataset
12///
13/// With default parameters the macro will attempt to run the
14/// standard hist2workspace example and read the ROOT file
15/// that it produces.
16///
17/// \macro_image
18/// \macro_output
19/// \macro_code
20///
21/// \authors Sven Kreiss, Kyle Cranmer
22
23#include "TFile.h"
24#include "TROOT.h"
25#include "TH1F.h"
26#include "TF1.h"
27#include "TCanvas.h"
28#include "TStopwatch.h"
29
30#include "RooWorkspace.h"
31#include "RooAbsData.h"
32#include "RooRandom.h"
33#include "RooRealSumPdf.h"
34#include "RooNumIntConfig.h"
35
46
48#include "TSystem.h"
49
50#include <vector>
51
52using namespace RooFit;
53using namespace RooStats;
54
55double StandardFrequentistDiscovery(const char *infile = "", const char *workspaceName = "channel1",
56 const char *modelConfigNameSB = "ModelConfig", const char *dataName = "obsData",
57 int toys = 1000, double poiValueForBackground = 0.0, double poiValueForSignal = 1.0)
58{
59
60 // The workspace contains the model for s+b. The b model is "autogenerated"
61 // by copying s+b and setting the one parameter of interest to zero.
62 // To keep the script simple, multiple parameters of interest or different
63 // functional forms of the b model are not supported.
64
65 // for now, assume there is only one parameter of interest, and these are
66 // its values:
67
68 // -------------------------------------------------------
69 // First part is just to access a user-defined file
70 // or create the standard example file if it doesn't exist
71 const char *filename = "";
72 if (!strcmp(infile, "")) {
73 filename = "results/example_channel1_GammaExample_model.root";
74 bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
75 // if file does not exists generate with histfactory
76 if (!fileExist) {
77 // Normally this would be run on the command line
78 cout << "will run standard hist2workspace example" << endl;
79 gROOT->ProcessLine(".! prepareHistFactory .");
80 gROOT->ProcessLine(".! hist2workspace config/example.xml");
81 cout << "\n\n---------------------" << endl;
82 cout << "Done creating example input" << endl;
83 cout << "---------------------\n\n" << endl;
84 }
85
86 } else
87 filename = infile;
88
89 // Try to open the file
90 TFile *file = TFile::Open(filename);
91
92 // if input file was specified but not found, quit
93 if (!file) {
94 cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
95 return -1;
96 }
97
98 // -------------------------------------------------------
99 // Tutorial starts here
100 // -------------------------------------------------------
101
102 TStopwatch *mn_t = new TStopwatch;
103 mn_t->Start();
104
105 // get the workspace out of the file
107 if (!w) {
108 cout << "workspace not found" << endl;
109 return -1.0;
110 }
111
112 // get the modelConfig out of the file
113 ModelConfig *mc = (ModelConfig *)w->obj(modelConfigNameSB);
114
115 // get the data out of the file
117
118 // make sure ingredients are found
119 if (!data || !mc) {
120 w->Print();
121 cout << "data or ModelConfig was not found" << endl;
122 return -1.0;
123 }
124
125 RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();
126 firstPOI->setVal(poiValueForSignal);
128 // create null model
129 ModelConfig *mcNull = mc->Clone("ModelConfigNull");
130 firstPOI->setVal(poiValueForBackground);
131 mcNull->SetSnapshot(*(RooArgSet *)mcNull->GetParametersOfInterest()->snapshot());
132
133 // ----------------------------------------------------
134 // Configure a ProfileLikelihoodTestStat and a SimpleLikelihoodRatioTestStat
135 // to use simultaneously with ToyMCSampler
137 plts->SetOneSidedDiscovery(true);
138 plts->SetVarName("q_{0}/2");
139
140 // ----------------------------------------------------
141 // configure the ToyMCImportanceSampler with two test statistics
142 ToyMCSampler toymcs(*plts, 50);
143
144 // Since this tool needs to throw toy MC the PDF needs to be
145 // extended or the tool needs to know how many entries in a dataset
146 // per pseudo experiment.
147 // In the 'number counting form' where the entries in the dataset
148 // are counts, and not values of discriminating variables, the
149 // datasets typically only have one entry and the PDF is not
150 // extended.
151 if (!mc->GetPdf()->canBeExtended()) {
152 if (data->numEntries() == 1) {
153 toymcs.SetNEventsPerToy(1);
154 } else
155 cout << "Not sure what to do about this model" << endl;
156 }
157
158 // instantiate the calculator
159 FrequentistCalculator freqCalc(*data, *mc, *mcNull, &toymcs);
160 freqCalc.SetToys(toys, toys); // null toys, alt toys
161
162 // Run the calculator and print result
163 HypoTestResult *freqCalcResult = freqCalc.GetHypoTest();
164 freqCalcResult->GetNullDistribution()->SetTitle("b only");
165 freqCalcResult->GetAltDistribution()->SetTitle("s+b");
166 freqCalcResult->Print();
167 double pvalue = freqCalcResult->NullPValue();
168
169 // stop timing
170 mn_t->Stop();
171 cout << "total CPU time: " << mn_t->CpuTime() << endl;
172 cout << "total real time: " << mn_t->RealTime() << endl;
173
174 // plot
175 TCanvas *c1 = new TCanvas();
176 HypoTestPlot *plot = new HypoTestPlot(*freqCalcResult, 100, -0.49, 9.51);
177 plot->SetLogYaxis(true);
178
179 // add chi2 to plot
180 int nPOI = 1;
181 TF1 *f = new TF1("f", TString::Format("1*ROOT::Math::chisquared_pdf(2*x,%d,0)", nPOI), 0, 20);
182 f->SetLineColor(kBlack);
183 f->SetLineStyle(7);
184 plot->AddTF1(f, TString::Format("#chi^{2}(2x,%d)", nPOI));
185
186 plot->Draw();
187 c1->SaveAs("standard_discovery_output.pdf");
188
189 return pvalue;
190}
#define f(i)
Definition RSha256.hxx:104
@ kBlack
Definition Rtypes.h:66
#define gROOT
Definition TROOT.h:417
externTSystem * gSystem
Definition TSystem.h:582
RooAbsArg * first() const
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
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:159
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setVal(double value) override
Set value of variable to 'value'.
Does a frequentist hypothesis test.
This class provides the plots for the result of a study performed with any of the HypoTestCalculatorG...
HypoTestResult is a base class for results from hypothesis tests.
void Print(const Option_t *="") const override
Print out some information about the results Note: use Alt/Null labels for the hypotheses here as the...
virtual double NullPValue() const
Return p-value for null hypothesis.
SamplingDistribution * GetNullDistribution(void) const
SamplingDistribution * GetAltDistribution(void) const
void SetSnapshot(const RooArgSet &set)
Set parameter values for a particular hypothesis if using a common PDF by saving a snapshot in the wo...
ModelConfig * Clone(const char *name="") const override
clone
Definition ModelConfig.h:56
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
ProfileLikelihoodTestStat is an implementation of the TestStatistic interface that calculates the pro...
virtual void SetVarName(const char *name)
void Draw(Option_t *options=nullptr) override
Draw this plot and all of the elements it contains.
void SetLogYaxis(bool ly)
changes plot to log scale on y axis
void AddTF1(TF1 *f, const char *title=nullptr, Option_t *drawOptions="SAME")
add a TF1
ToyMCSampler is an implementation of the TestStatSampler interface.
Persistable container for RooFit projects.
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
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
Stopwatch class.
Definition TStopwatch.h:28
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void Stop()
Stop the stopwatch.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2385
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