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 byt 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
106 RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
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
116 RooAbsData *data = w->data(dataName);
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 // We can use PROOF to speed things along in parallel
159 // ProofConfig pc(*w, 2, "user@yourfavoriteproofcluster", false);
160 ProofConfig pc(*w, 2, "", false);
161 // toymcs.SetProofConfig(&pc); // enable proof
162
163 // instantiate the calculator
164 FrequentistCalculator freqCalc(*data, *mc, *mcNull, &toymcs);
165 freqCalc.SetToys(toys, toys); // null toys, alt toys
166
167 // Run the calculator and print result
168 HypoTestResult *freqCalcResult = freqCalc.GetHypoTest();
169 freqCalcResult->GetNullDistribution()->SetTitle("b only");
170 freqCalcResult->GetAltDistribution()->SetTitle("s+b");
171 freqCalcResult->Print();
172 double pvalue = freqCalcResult->NullPValue();
173
174 // stop timing
175 mn_t->Stop();
176 cout << "total CPU time: " << mn_t->CpuTime() << endl;
177 cout << "total real time: " << mn_t->RealTime() << endl;
178
179 // plot
180 TCanvas *c1 = new TCanvas();
181 HypoTestPlot *plot = new HypoTestPlot(*freqCalcResult, 100, -0.49, 9.51);
182 plot->SetLogYaxis(true);
183
184 // add chi2 to plot
185 int nPOI = 1;
186 TF1 *f = new TF1("f", TString::Format("1*ROOT::Math::chisquared_pdf(2*x,%d,0)", nPOI), 0, 20);
187 f->SetLineColor(kBlack);
188 f->SetLineStyle(7);
189 plot->AddTF1(f, TString::Format("#chi^{2}(2x,%d)", nPOI));
190
191 plot->Draw();
192 c1->SaveAs("standard_discovery_output.pdf");
193
194 return pvalue;
195}
#define f(i)
Definition RSha256.hxx:104
@ kBlack
Definition Rtypes.h:65
winID h TVirtualViewer3D TVirtualGLPainter char TVirtualGLPainter plot
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
RooAbsArg * first() const
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
bool canBeExtended() const
If true, PDF can provide extended likelihood term.
Definition RooAbsPdf.h:219
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
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
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
virtual 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:57
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)
Holds configuration options for proof and proof-lite.
Definition ProofConfig.h:45
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.
1-Dim function class
Definition TF1.h:233
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4082
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
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:2378
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:1296
return c1
Definition legend1.C:41
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26
Namespace for the RooStats classes.
Definition Asimov.h:19