Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf801_mcstudy.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_roofit_main
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_code
8/// \macro_output
9///
10/// \date February 2018
11/// \author Wouter Verkerke
12
13#include "RooRealVar.h"
14#include "RooDataSet.h"
15#include "RooGaussian.h"
16#include "RooChebychev.h"
17#include "RooAddPdf.h"
18#include "RooMCStudy.h"
19#include "RooPlot.h"
20#include "TCanvas.h"
21#include "TAxis.h"
22#include "TH2.h"
23#include "RooFitResult.h"
24#include "TStyle.h"
25#include "TDirectory.h"
26
27using namespace RooFit;
28
29void rf801_mcstudy()
30{
31 // C r e a t e m o d e l
32 // -----------------------
33
34 // Declare observable x
35 RooRealVar x("x", "x", 0, 10);
36 x.setBins(40);
37
38 // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters
39 RooRealVar mean("mean", "mean of gaussians", 5, 0, 10);
40 RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
41 RooRealVar sigma2("sigma2", "width of gaussians", 1);
42
43 RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
44 RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
45
46 // Build Chebychev polynomial pdf
47 RooRealVar a0("a0", "a0", 0.5, 0., 1.);
48 RooRealVar a1("a1", "a1", -0.2, -1, 1.);
49 RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));
50
51 // Sum the signal components into a composite signal pdf
52 RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
53 RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);
54
55 // Sum the composite signal and background
56 RooRealVar nbkg("nbkg", "number of background events,", 150, 0, 1000);
57 RooRealVar nsig("nsig", "number of signal events", 150, 0, 1000);
58 RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), RooArgList(nbkg, nsig));
59
60 // C r e a t e m a n a g e r
61 // ---------------------------
62
63 // Instantiate RooMCStudy manager on model with x as observable and given choice of fit options
64 //
65 // The Silence() option kills all messages below the PROGRESS level, leaving only a single message
66 // per sample executed, and any error message that occur during fitting
67 //
68 // The Extended() option has two effects:
69 // 1) The extended ML term is included in the likelihood and
70 // 2) A poisson fluctuation is introduced on the number of generated events
71 //
72 // The FitOptions() given here are passed to the fitting stage of each toy experiment.
73 // If Save() is specified, the fit result of each experiment is saved by the manager
74 //
75 // A Binned() option is added in this example to bin the data between generation and fitting
76 // to speed up the study at the expense of some precision
77
79 new RooMCStudy(model, x, Binned(true), Silence(), Extended(), FitOptions(Save(true), PrintEvalErrors(0)));
80
81 // G e n e r a t e a n d f i t e v e n t s
82 // ---------------------------------------------
83
84 // Generate and fit 1000 samples of Poisson(nExpected) events
85 mcstudy->generateAndFit(1000);
86
87 // E x p l o r e r e s u l t s o f s t u d y
88 // ------------------------------------------------
89
90 // Make plots of the distributions of mean, the error on mean and the pull of mean.
91 // The FitGauss() option overlays a Gaussian fit of the plotted distribution,
92 // which is for example useful for linearity studies of the fitted parameter.
93 RooPlot *frame1 = mcstudy->plotParam(mean, Bins(40), FitGauss(true));
94 RooPlot *frame2 = mcstudy->plotError(mean, Bins(40));
95 RooPlot *frame3 = mcstudy->plotPull(mean, Bins(40), FitGauss(true));
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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:385
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gPad
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:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Chebychev polynomial p.d.f.
Plain Gaussian p.d.f.
Definition RooGaussian.h:24
Helper class to facilitate Monte Carlo studies such as 'goodness-of-fit' studies, that involve fittin...
Definition RooMCStudy.h:32
Plot frame and a container for graphics objects within that frame.
Definition RooPlot.h:43
Variable that can be changed from the outside.
Definition RooRealVar.h:37
The Canvas class.
Definition TCanvas.h:23
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
Service class for 2-D 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:1641
RooCmdArg FitGauss(bool flag=true)
RooCmdArg FitOptions(const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={})
RooCmdArg YVar(const RooAbsRealLValue &var, const RooCmdArg &arg={})
RooCmdArg Silence(bool flag=true)
RooCmdArg Binned(bool flag=true)
RooCmdArg Bins(Int_t nbin)
RooCmdArg Save(bool flag=true)
RooCmdArg PrintEvalErrors(Int_t numErrors)
Double_t x[n]
Definition legend1.C:17
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73