ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StandardFrequentistDiscovery.C
Go to the documentation of this file.
1 // StandardFrequentistDiscovery
2 
3 /*
4  StandardFrequentistDiscovery
5 
6  Author: Sven Kreiss, Kyle Cranmer
7  date: May 2012
8 
9  This is a standard demo that can be used with any ROOT file
10  prepared in the standard way. You specify:
11  - name for input ROOT file
12  - name of workspace inside ROOT file that holds model and data
13  - name of ModelConfig that specifies details for calculator tools
14  - name of dataset
15 
16  With default parameters the macro will attempt to run the
17  standard hist2workspace example and read the ROOT file
18  that it produces.
19  */
20 
21 #include "TFile.h"
22 #include "TROOT.h"
23 #include "TH1F.h"
24 #include "TF1.h"
25 #include "TCanvas.h"
26 #include "TStopwatch.h"
27 
28 #include "RooWorkspace.h"
29 #include "RooAbsData.h"
30 #include "RooRandom.h"
31 #include "RooRealSumPdf.h"
32 #include "RooNumIntConfig.h"
33 
34 #include "RooStats/ModelConfig.h"
37 #include "RooStats/HypoTestPlot.h"
44 
46 #include "TSystem.h"
47 
48 #include <vector>
49 
50 using namespace RooFit;
51 using namespace RooStats;
52 
53 
54 
55 
57  const char* infile = "",
58  const char* workspaceName = "channel1",
59  const char* modelConfigNameSB = "ModelConfig",
60  const char* dataName = "obsData",
61  int toys = 1000,
62  double poiValueForBackground = 0.0,
63  double poiValueForSignal = 1.0
64 ) {
65 
66  // The workspace contains the model for s+b. The b model is "autogenerated"
67  // by copying s+b and setting the one parameter of interest to zero.
68  // To keep the script simple, multiple parameters of interest or different
69  // functional forms of the b model are not supported.
70 
71  // for now, assume there is only one parameter of interest, and these are
72  // its values:
73 
74  /////////////////////////////////////////////////////////////
75  // First part is just to access a user-defined file
76  // or create the standard example file if it doesn't exist
77  ////////////////////////////////////////////////////////////
78  const char* filename = "";
79  if (!strcmp(infile,"")) {
80  filename = "results/example_channel1_GammaExample_model.root";
81  bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
82  // if file does not exists generate with histfactory
83  if (!fileExist) {
84 #ifdef _WIN32
85  cout << "HistFactory file cannot be generated on Windows - exit" << endl;
86  return -1;
87 #endif
88  // Normally this would be run on the command line
89  cout <<"will run standard hist2workspace example"<<endl;
90  gROOT->ProcessLine(".! prepareHistFactory .");
91  gROOT->ProcessLine(".! hist2workspace config/example.xml");
92  cout <<"\n\n---------------------"<<endl;
93  cout <<"Done creating example input"<<endl;
94  cout <<"---------------------\n\n"<<endl;
95  }
96 
97  }
98  else
99  filename = infile;
100 
101  // Try to open the file
102  TFile *file = TFile::Open(filename);
103 
104  // if input file was specified byt not found, quit
105  if(!file ){
106  cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
107  return -1;
108  }
109 
110 
111  /////////////////////////////////////////////////////////////
112  // Tutorial starts here
113  ////////////////////////////////////////////////////////////
114 
115  TStopwatch *mn_t = new TStopwatch;
116  mn_t->Start();
117 
118  // get the workspace out of the file
119  RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
120  if (!w) {
121  cout << "workspace not found" << endl;
122  return -1.0;
123  }
124 
125  // get the modelConfig out of the file
126  ModelConfig* mc = (ModelConfig*) w->obj(modelConfigNameSB);
127 
128  // get the data out of the file
129  RooAbsData* data = w->data(dataName);
130 
131  // make sure ingredients are found
132  if (!data || !mc) {
133  w->Print();
134  cout << "data or ModelConfig was not found" << endl;
135  return -1.0;
136  }
137 
138 
139  RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
140  firstPOI->setVal(poiValueForSignal);
142  // create null model
143  ModelConfig *mcNull = mc->Clone("ModelConfigNull");
144  firstPOI->setVal(poiValueForBackground);
145  mcNull->SetSnapshot(*(RooArgSet*)mcNull->GetParametersOfInterest()->snapshot());
146 
147 
148 
149  // ----------------------------------------------------
150  // Configure a ProfileLikelihoodTestStat and a SimpleLikelihoodRatioTestStat
151  // to use simultaneously with ToyMCSampler
153  plts->SetOneSidedDiscovery(true);
154  plts->SetVarName( "q_{0}/2" );
155 
156  // ----------------------------------------------------
157  // configure the ToyMCImportanceSampler with two test statistics
158  ToyMCSampler toymcs(*plts, 50);
159 
160 
161 
162  // Since this tool needs to throw toy MC the PDF needs to be
163  // extended or the tool needs to know how many entries in a dataset
164  // per pseudo experiment.
165  // In the 'number counting form' where the entries in the dataset
166  // are counts, and not values of discriminating variables, the
167  // datasets typically only have one entry and the PDF is not
168  // extended.
169  if (!mc->GetPdf()->canBeExtended()) {
170  if (data->numEntries() == 1) {
171  toymcs.SetNEventsPerToy(1);
172  } else cout << "Not sure what to do about this model" << endl;
173  }
174 
175  // We can use PROOF to speed things along in parallel
176  // ProofConfig pc(*w, 2, "user@yourfavoriteproofcluster", false);
177  ProofConfig pc(*w, 2, "", false);
178  //toymcs.SetProofConfig(&pc); // enable proof
179 
180 
181  // instantiate the calculator
182  FrequentistCalculator freqCalc(*data, *mc, *mcNull, &toymcs);
183  freqCalc.SetToys( toys,toys ); // null toys, alt toys
184 
185  // Run the calculator and print result
186  HypoTestResult* freqCalcResult = freqCalc.GetHypoTest();
187  freqCalcResult->GetNullDistribution()->SetTitle( "b only" );
188  freqCalcResult->GetAltDistribution()->SetTitle( "s+b" );
189  freqCalcResult->Print();
190  double pvalue = freqCalcResult->NullPValue();
191 
192  // stop timing
193  mn_t->Stop();
194  cout << "total CPU time: " << mn_t->CpuTime() << endl;
195  cout << "total real time: " << mn_t->RealTime() << endl;
196 
197  // plot
198  TCanvas* c1 = new TCanvas();
199  HypoTestPlot *plot = new HypoTestPlot(*freqCalcResult, 100, -0.49, 9.51 );
200  plot->SetLogYaxis(true);
201 
202  // add chi2 to plot
203  int nPOI = 1;
204  TF1* f = new TF1("f", TString::Format("1*ROOT::Math::chisquared_pdf(2*x,%d,0)",nPOI), 0,20);
205  f->SetLineColor( kBlack );
206  f->SetLineStyle( 7 );
207  plot->AddTF1( f, TString::Format("#chi^{2}(2x,%d)",nPOI) );
208 
209  plot->Draw();
210  c1->SaveAs("standard_discovery_output.pdf");
211 
212 
213  return pvalue;
214 }
215 
216 
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
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
Definition: ModelConfig.h:244
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
RooAbsData * data(const char *name) const
Retrieve dataset (binned or unbinned) with given name. A null pointer is returned if not found...
void SetToys(int toysNull, int toysAlt)
set number of toys
TCanvas * c1
Definition: legend1.C:2
Definition: Rtypes.h:60
HypoTestResult is a base class for results from hypothesis tests.
static const char * filename()
virtual void SetNEventsPerToy(const Int_t nevents)
Definition: ToyMCSampler.h:177
#define gROOT
Definition: TROOT.h:344
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:123
This class provides the plots for the result of a study performed with any of the HypoTestCalculatorG...
Definition: HypoTestPlot.h:37
double StandardFrequentistDiscovery(const char *infile="", const char *workspaceName="channel1", const char *modelConfigNameSB="ModelConfig", const char *dataName="obsData", int toys=1000, double poiValueForBackground=0.0, double poiValueForSignal=1.0)
SamplingDistribution * GetAltDistribution(void) const
void SetLogYaxis(Bool_t ly)
changes plot to log scale on y axis
TFile * f
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
virtual ModelConfig * Clone(const char *name="") const
clone
Definition: ModelConfig.h:76
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
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:2321
virtual HypoTestResult * GetHypoTest() const
inherited methods from HypoTestCalculator interface
void Print(const Option_t *="") const
void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
virtual Double_t NullPValue() const
Return p-value for null hypothesis.
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
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:293
tuple w
Definition: qtexample.py: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
void AddTF1(TF1 *f, const char *title=NULL, Option_t *drawOptions="SAME")
add a TF1
tuple file
Definition: fildir.py:20
Bool_t canBeExtended() const
Definition: RooAbsPdf.h:216
SamplingDistribution * GetNullDistribution(void) const
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.
Hypothesis Test Calculator using a full frequentist procedure for sampling the test statistic distrib...
const RooArgSet * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return NULL if not existing)
Definition: ModelConfig.h:247
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
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...
virtual void SetVarName(const char *name)
The RooWorkspace is a persistable container for RooFit projects.
Definition: RooWorkspace.h:42
Stopwatch class.
Definition: TStopwatch.h:30