Logo ROOT  
Reference Guide
rf801_mcstudy.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit
3/// \notebook -js
4///
5/// Validation and MC studies: toy Monte Carlo study that perform cycles of event generation and fitting
6///
7/// \macro_image
8/// \macro_output
9/// \macro_code
10///
11/// \date February 2018
12/// \author Wouter Verkerke
13
14#include "RooRealVar.h"
15#include "RooDataSet.h"
16#include "RooGaussian.h"
17#include "RooConstVar.h"
18#include "RooChebychev.h"
19#include "RooAddPdf.h"
20#include "RooMCStudy.h"
21#include "RooPlot.h"
22#include "TCanvas.h"
23#include "TAxis.h"
24#include "TH2.h"
25#include "RooFitResult.h"
26#include "TStyle.h"
27#include "TDirectory.h"
28
29using namespace RooFit;
30
31void rf801_mcstudy()
32{
33 // C r e a t e m o d e l
34 // -----------------------
35
36 // Declare observable x
37 RooRealVar x("x", "x", 0, 10);
38 x.setBins(40);
39
40 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
41 RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
42 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
43 RooRealVar sigma2("sigma2", "width of gaussians", 1);
44
45 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
46 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
47
48 // Build Chebychev polynomial p.d.f.
49 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
50 RooRealVar a1("a1", "a1", -0.2, -1, 1.);
51 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
52
53 // Sum the signal components into a composite signal p.d.f.
54 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
55 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
56
57 // Sum the composite signal and background
58 RooRealVar nbkg("nbkg", "number of background events,", 150, 0, 1000);
59 RooRealVar nsig("nsig", "number of signal events", 150, 0, 1000);
60 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), RooArgList(nbkg, nsig));
61
62 // C r e a t e m a n a g e r
63 // ---------------------------
64
65 // Instantiate RooMCStudy manager on model with x as observable and given choice of fit options
66 //
67 // The Silence() option kills all messages below the PROGRESS level, leaving only a single message
68 // per sample executed, and any error message that occur during fitting
69 //
70 // The Extended() option has two effects:
71 // 1) The extended ML term is included in the likelihood and
72 // 2) A poisson fluctuation is introduced on the number of generated events
73 //
74 // The FitOptions() given here are passed to the fitting stage of each toy experiment.
75 // If Save() is specified, the fit result of each experiment is saved by the manager
76 //
77 // A Binned() option is added in this example to bin the data between generation and fitting
78 // to speed up the study at the expense of some precision
79
80 RooMCStudy *mcstudy =
82
83 // G e n e r a t e a n d f i t e v e n t s
84 // ---------------------------------------------
85
86 // Generate and fit 1000 samples of Poisson(nExpected) events
87 mcstudy->generateAndFit(1000);
88
89 // E x p l o r e r e s u l t s o f s t u d y
90 // ------------------------------------------------
91
92 // Make plots of the distributions of mean, the error on mean and the pull of mean
93 RooPlot *frame1 = mcstudy->plotParam(mean, Bins(40));
94 RooPlot *frame2 = mcstudy->plotError(mean, Bins(40));
95 RooPlot *frame3 = mcstudy->plotPull(mean, Bins(40), FitGauss(kTRUE));
96
97 // Plot distribution of minimized likelihood
98 RooPlot *frame4 = mcstudy->plotNLL(Bins(40));
99
100 // Make some histograms from the parameter dataset
101 TH1 *hh_cor_a0_s1f = mcstudy->fitParDataSet().createHistogram("hh", a1, YVar(sig1frac));
102 TH1 *hh_cor_a0_a1 = mcstudy->fitParDataSet().createHistogram("hh", a0, YVar(a1));
103
104 // Access some of the saved fit results from individual toys
105 TH2 *corrHist000 = mcstudy->fitResult(0)->correlationHist("c000");
106 TH2 *corrHist127 = mcstudy->fitResult(127)->correlationHist("c127");
107 TH2 *corrHist953 = mcstudy->fitResult(953)->correlationHist("c953");
108
109 // Draw all plots on a canvas
110 gStyle->SetOptStat(0);
111 TCanvas *c = new TCanvas("rf801_mcstudy", "rf801_mcstudy", 900, 900);
112 c->Divide(3, 3);
113 c->cd(1);
114 gPad->SetLeftMargin(0.15);
115 frame1->GetYaxis()->SetTitleOffset(1.4);
116 frame1->Draw();
117 c->cd(2);
118 gPad->SetLeftMargin(0.15);
119 frame2->GetYaxis()->SetTitleOffset(1.4);
120 frame2->Draw();
121 c->cd(3);
122 gPad->SetLeftMargin(0.15);
123 frame3->GetYaxis()->SetTitleOffset(1.4);
124 frame3->Draw();
125 c->cd(4);
126 gPad->SetLeftMargin(0.15);
127 frame4->GetYaxis()->SetTitleOffset(1.4);
128 frame4->Draw();
129 c->cd(5);
130 gPad->SetLeftMargin(0.15);
131 hh_cor_a0_s1f->GetYaxis()->SetTitleOffset(1.4);
132 hh_cor_a0_s1f->Draw("box");
133 c->cd(6);
134 gPad->SetLeftMargin(0.15);
135 hh_cor_a0_a1->GetYaxis()->SetTitleOffset(1.4);
136 hh_cor_a0_a1->Draw("box");
137 c->cd(7);
138 gPad->SetLeftMargin(0.15);
139 corrHist000->GetYaxis()->SetTitleOffset(1.4);
140 corrHist000->Draw("colz");
141 c->cd(8);
142 gPad->SetLeftMargin(0.15);
143 corrHist127->GetYaxis()->SetTitleOffset(1.4);
144 corrHist127->Draw("colz");
145 c->cd(9);
146 gPad->SetLeftMargin(0.15);
147 corrHist953->GetYaxis()->SetTitleOffset(1.4);
148 corrHist953->Draw("colz");
149
150 // Make RooMCStudy object available on command line after
151 // macro finishes
152 gDirectory->Add(mcstudy);
153}
#define c(i)
Definition: RSha256.hxx:101
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define gDirectory
Definition: TDirectory.h:229
R__EXTERN TStyle * gStyle
Definition: TStyle.h:410
#define gPad
Definition: TVirtualPad.h:287
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:29
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
Chebychev polynomial p.d.f.
Definition: RooChebychev.h:25
TH2F * createHistogram(const RooAbsRealLValue &var1, const RooAbsRealLValue &var2, const char *cuts="", const char *name="hist") const
Create a TH2F histogram of the distribution of the specified variable using this dataset.
TH2 * correlationHist(const char *name="correlation_matrix") const
Return TH2D of correlation matrix.
Plain Gaussian p.d.f.
Definition: RooGaussian.h:25
RooMCStudy is a helper class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies,...
Definition: RooMCStudy.h:32
RooPlot * plotPull(const RooRealVar &param, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of pull values for the specified parameter on a newly created frame.
RooPlot * plotError(const RooRealVar &param, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of the fit errors for the specified parameter on a newly created frame.
RooPlot * plotParam(const RooRealVar &param, const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of the fitted value of the given parameter on a newly created frame.
RooPlot * plotNLL(const RooCmdArg &arg1=RooCmdArg::none(), const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none())
Plot the distribution of the -log(L) values on a newly created frame.
const RooFitResult * fitResult(Int_t sampleNum) const
Return the RooFitResult of the fit with the given run number.
const RooDataSet & fitParDataSet()
Return a RooDataSet containing the post-fit parameters of each toy cycle.
Definition: RooMCStudy.cxx:980
Bool_t generateAndFit(Int_t nSamples, Int_t nEvtPerSample=0, Bool_t keepGenData=kFALSE, const char *asciiFilePat=0)
Generate and fit 'nSamples' samples of 'nEvtPerSample' events.
Definition: RooMCStudy.cxx:659
A RooPlot is a plot frame and a container for graphics objects within that frame.
Definition: RooPlot.h:44
TAxis * GetYaxis() const
Definition: RooPlot.cxx:1277
virtual void Draw(Option_t *options=0)
Draw this plot and all of the elements it contains.
Definition: RooPlot.cxx:712
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition: TAttAxis.cxx:294
The Canvas class.
Definition: TCanvas.h:27
The TH1 histogram class.
Definition: TH1.h:56
TAxis * GetYaxis()
Definition: TH1.h:317
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
Service class for 2-Dim histogram classes.
Definition: TH2.h:30
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1590
RooCmdArg Binned(Bool_t flag=kTRUE)
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg=RooCmdArg::none())
RooCmdArg FitGauss(Bool_t flag=kTRUE)
RooCmdArg Silence(Bool_t flag=kTRUE)
RooCmdArg Bins(Int_t nbin)
RooCmdArg Extended(Bool_t flag=kTRUE)
RooCmdArg PrintEvalErrors(Int_t numErrors)
RooCmdArg Save(Bool_t flag=kTRUE)
RooCmdArg FitOptions(const char *opts)
Double_t x[n]
Definition: legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...