Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
StandardBayesianMCMCDemo.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roostats
3/// \notebook -js
4/// Standard demo of the Bayesian MCMC calculator
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/// The actual heart of the demo is only about 10 lines long.
18///
19/// The MCMCCalculator is a Bayesian tool that uses
20/// the Metropolis-Hastings algorithm to efficiently integrate
21/// in many dimensions. It is not as accurate as the BayesianCalculator
22/// for simple problems, but it scales to much more complicated cases.
23///
24/// \macro_image
25/// \macro_output
26/// \macro_code
27///
28/// \author Kyle Cranmer
29
30#include "TFile.h"
31#include "TROOT.h"
32#include "TCanvas.h"
33#include "TMath.h"
34#include "TSystem.h"
35#include "RooWorkspace.h"
36#include "RooAbsData.h"
37
45#include "RooFitResult.h"
46
47using namespace RooFit;
48using namespace RooStats;
49
50struct BayesianMCMCOptions {
51
52 double confLevel = 0.95;
53 int intervalType = 2; // type of interval (0 is shortest, 1 central, 2 upper limit)
54 double maxPOI = -999; // force different values of POI for doing the scan (default is given value)
55 double minPOI = -999;
56 int numIters = 100000; // number of iterations
57 int numBurnInSteps = 100; // number of burn in steps to be ignored
58};
59
60BayesianMCMCOptions optMCMC;
61
62void StandardBayesianMCMCDemo(const char *infile = "", const char *workspaceName = "combined",
63 const char *modelConfigName = "ModelConfig", const char *dataName = "obsData")
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 // Normally this would be run on the command line
77 cout << "will run standard hist2workspace example" << endl;
78 gROOT->ProcessLine(".! prepareHistFactory .");
79 gROOT->ProcessLine(".! hist2workspace config/example.xml");
80 cout << "\n\n---------------------" << endl;
81 cout << "Done creating example input" << endl;
82 cout << "---------------------\n\n" << endl;
83 }
84
85 } else
86 filename = infile;
87
88 // Try to open the file
89 TFile *file = TFile::Open(filename);
90
91 // if input file was specified byt not found, quit
92 if (!file) {
93 cout << "StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
94 return;
95 }
96
97 // -------------------------------------------------------
98 // Tutorial starts here
99 // -------------------------------------------------------
100
101 // get the workspace out of the file
102 RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
103 if (!w) {
104 cout << "workspace not found" << endl;
105 return;
106 }
107
108 // get the modelConfig out of the file
109 ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
110
111 // get the modelConfig out of the file
112 RooAbsData *data = w->data(dataName);
113
114 // make sure ingredients are found
115 if (!data || !mc) {
116 w->Print();
117 cout << "data or ModelConfig was not found" << endl;
118 return;
119 }
120
121 // Want an efficient proposal function
122 // default is uniform.
123
124 /*
125 // this one is based on the covariance matrix of fit
126 RooFitResult* fit = mc->GetPdf()->fitTo(*data,Save());
127 ProposalHelper ph;
128 ph.SetVariables((RooArgSet&)fit->floatParsFinal());
129 ph.SetCovMatrix(fit->covarianceMatrix());
130 ph.SetUpdateProposalParameters(kTRUE); // auto-create mean vars and add mappings
131 ph.SetCacheSize(100);
132 ProposalFunction* pf = ph.GetProposalFunction();
133 */
134
135 // this proposal function seems fairly robust
136 SequentialProposal sp(0.1);
137 // -------------------------------------------------------
138 // create and use the MCMCCalculator
139 // to find and plot the 95% credible interval
140 // on the parameter of interest as specified
141 // in the model config
142 MCMCCalculator mcmc(*data, *mc);
143 mcmc.SetConfidenceLevel(optMCMC.confLevel); // 95% interval
144 // mcmc.SetProposalFunction(*pf);
145 mcmc.SetProposalFunction(sp);
146 mcmc.SetNumIters(optMCMC.numIters); // Metropolis-Hastings algorithm iterations
147 mcmc.SetNumBurnInSteps(optMCMC.numBurnInSteps); // first N steps to be ignored as burn-in
148
149 // default is the shortest interval.
150 if (optMCMC.intervalType == 0)
151 mcmc.SetIntervalType(MCMCInterval::kShortest); // for shortest interval (not really needed)
152 if (optMCMC.intervalType == 1)
153 mcmc.SetLeftSideTailFraction(0.5); // for central interval
154 if (optMCMC.intervalType == 2)
155 mcmc.SetLeftSideTailFraction(0.); // for upper limit
156
157 RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();
158 if (optMCMC.minPOI != -999)
159 firstPOI->setMin(optMCMC.minPOI);
160 if (optMCMC.maxPOI != -999)
161 firstPOI->setMax(optMCMC.maxPOI);
162
163 MCMCInterval *interval = mcmc.GetInterval();
164
165 // make a plot
166 // TCanvas* c1 =
167 auto c1 = new TCanvas("IntervalPlot");
168 MCMCIntervalPlot plot(*interval);
169 plot.Draw();
170
171 TCanvas *c2 = new TCanvas("extraPlots");
172 const RooArgSet *list = mc->GetNuisanceParameters();
173 if (list->getSize() > 1) {
174 double n = list->getSize();
175 int ny = TMath::CeilNint(sqrt(n));
176 int nx = TMath::CeilNint(double(n) / ny);
177 c2->Divide(nx, ny);
178 }
179
180 // draw a scatter plot of chain results for poi vs each nuisance parameters
181 int iPad = 1; // iPad, that's funny
182 for (auto *nuis : static_range_cast<RooRealVar *>(*mc->GetNuisanceParameters())) {
183 c2->cd(iPad++);
184 plot.DrawChainScatter(*firstPOI, *nuis);
185 }
186
187 // print out the interval on the first Parameter of Interest
188 cout << "\n>>>> RESULT : " << optMCMC.confLevel * 100 << "% interval on " << firstPOI->GetName() << " is : ["
189 << interval->LowerLimit(*firstPOI) << ", " << interval->UpperLimit(*firstPOI) << "] " << endl;
190
191 gPad = c1;
192}
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
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
#define gPad
Int_t getSize() const
Return the number of elements in the collection.
RooAbsArg * first() const
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Variable that can be changed from the outside.
Definition RooRealVar.h:37
void setMin(const char *name, double value)
Set minimum of name range to given value.
void setMax(const char *name, double value)
Set maximum of name range to given value.
Bayesian Calculator estimating an interval or a credible region using the Markov-Chain Monte Carlo me...
This class provides simple and straightforward utilities to plot a MCMCInterval object.
MCMCInterval is a concrete implementation of the RooStats::ConfInterval interface.
virtual double UpperLimit(RooRealVar &param)
get the highest value of param that is within the confidence interval
virtual double LowerLimit(RooRealVar &param)
get the lowest value of param that is within the confidence interval
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
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)
Class implementing a proposal function that samples the parameter space by moving only in one coordin...
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.
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
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
const Int_t n
Definition legend1.C:16
return c2
Definition legend2.C:14
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
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
Int_t CeilNint(Double_t x)
Returns the nearest integer of TMath::Ceil(x).
Definition TMath.h:674