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