Logo ROOT   6.16/01
Reference Guide
rf801_mcstudy.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## 'VALIDATION AND MC STUDIES' RooFit tutorial macro #801
6##
7## A Toy Monte Carlo study that perform cycles of
8## event generation and fitting
9##
10## \macro_code
11##
12## \date February 2018
13## \author Clemens Lange
14
15
16import ROOT
17
18
19# Create model
20# -----------------------
21
22# Declare observable x
23x = ROOT.RooRealVar("x", "x", 0, 10)
24x.setBins(40)
25
26# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
27# their parameters
28mean = ROOT.RooRealVar("mean", "mean of gaussians", 5, 0, 10)
29sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
30sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
31
32sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
33sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
34
35# Build Chebychev polynomial p.d.f.
36a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0., 1.)
37a1 = ROOT.RooRealVar("a1", "a1", -0.2, -1, 1.)
38bkg = ROOT.RooChebychev("bkg", "Background", x, ROOT.RooArgList(a0, a1))
39
40# Sum the signal components into a composite signal p.d.f.
41sig1frac = ROOT.RooRealVar(
42 "sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.)
43sig = ROOT.RooAddPdf(
44 "sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(sig1frac))
45
46# Sum the composite signal and background
47nbkg = ROOT.RooRealVar(
48 "nbkg", "number of background events, ", 150, 0, 1000)
49nsig = ROOT.RooRealVar("nsig", "number of signal events", 150, 0, 1000)
50model = ROOT.RooAddPdf(
51 "model", "g1+g2+a", ROOT.RooArgList(bkg, sig), ROOT.RooArgList(nbkg, nsig))
52
53# Create manager
54# ---------------------------
55
56# Instantiate ROOT.RooMCStudy manager on model with x as observable and given choice of fit options
57#
58# The Silence() option kills all messages below the PROGRESS level, only a single message
59# per sample executed, any error message that occur during fitting
60#
61# The Extended() option has two effects:
62# 1) The extended ML term is included in the likelihood and
63# 2) A poisson fluctuation is introduced on the number of generated events
64#
65# The FitOptions() given here are passed to the fitting stage of each toy experiment.
66# If Save() is specified, fit result of each experiment is saved by the manager
67#
68# A Binned() option is added in self example to bin the data between generation and fitting
69# to speed up the study at the expemse of some precision
70
71mcstudy = ROOT.RooMCStudy(model, ROOT.RooArgSet(x), ROOT.RooFit.Binned(ROOT.kTRUE), ROOT.RooFit.Silence(), ROOT.RooFit.Extended(),
72 ROOT.RooFit.FitOptions(ROOT.RooFit.Save(ROOT.kTRUE), ROOT.RooFit.PrintEvalErrors(0)))
73
74# Generate and fit events
75# ---------------------------------------------
76
77# Generate and fit 1000 samples of Poisson(nExpected) events
78mcstudy.generateAndFit(1000)
79
80# Explore results of study
81# ------------------------------------------------
82
83# Make plots of the distributions of mean, error on mean and the pull of
84# mean
85frame1 = mcstudy.plotParam(mean, ROOT.RooFit.Bins(40))
86frame2 = mcstudy.plotError(mean, ROOT.RooFit.Bins(40))
87frame3 = mcstudy.plotPull(mean, ROOT.RooFit.Bins(
88 40), ROOT.RooFit.FitGauss(ROOT.kTRUE))
89
90# Plot distribution of minimized likelihood
91frame4 = mcstudy.plotNLL(ROOT.RooFit.Bins(40))
92
93# Make some histograms from the parameter dataset
94hh_cor_a0_s1f = ROOT.RooAbsData.createHistogram(mcstudy.fitParDataSet(),
95 "hh", a1, ROOT.RooFit.YVar(sig1frac))
96hh_cor_a0_a1 = ROOT.RooAbsData.createHistogram(mcstudy.fitParDataSet(),
97 "hh", a0, ROOT.RooFit.YVar(a1))
98
99# Access some of the saved fit results from individual toys
100corrHist000 = mcstudy.fitResult(0).correlationHist("c000")
101corrHist127 = mcstudy.fitResult(127).correlationHist("c127")
102corrHist953 = mcstudy.fitResult(953).correlationHist("c953")
103
104# Draw all plots on a canvas
105ROOT.gStyle.SetPalette(1)
106ROOT.gStyle.SetOptStat(0)
107c = ROOT.TCanvas("rf801_mcstudy", "rf801_mcstudy", 900, 900)
108c.Divide(3, 3)
109c.cd(1)
110ROOT.gPad.SetLeftMargin(0.15)
111frame1.GetYaxis().SetTitleOffset(1.4)
112frame1.Draw()
113c.cd(2)
114ROOT.gPad.SetLeftMargin(0.15)
115frame2.GetYaxis().SetTitleOffset(1.4)
116frame2.Draw()
117c.cd(3)
118ROOT.gPad.SetLeftMargin(0.15)
119frame3.GetYaxis().SetTitleOffset(1.4)
120frame3.Draw()
121c.cd(4)
122ROOT.gPad.SetLeftMargin(0.15)
123frame4.GetYaxis().SetTitleOffset(1.4)
124frame4.Draw()
125c.cd(5)
126ROOT.gPad.SetLeftMargin(0.15)
127hh_cor_a0_s1f.GetYaxis().SetTitleOffset(1.4)
128hh_cor_a0_s1f.Draw("box")
129c.cd(6)
130ROOT.gPad.SetLeftMargin(0.15)
131hh_cor_a0_a1.GetYaxis().SetTitleOffset(1.4)
132hh_cor_a0_a1.Draw("box")
133c.cd(7)
134ROOT.gPad.SetLeftMargin(0.15)
135corrHist000.GetYaxis().SetTitleOffset(1.4)
136corrHist000.Draw("colz")
137c.cd(8)
138ROOT.gPad.SetLeftMargin(0.15)
139corrHist127.GetYaxis().SetTitleOffset(1.4)
140corrHist127.Draw("colz")
141c.cd(9)
142ROOT.gPad.SetLeftMargin(0.15)
143corrHist953.GetYaxis().SetTitleOffset(1.4)
144corrHist953.Draw("colz")
145
146c.SaveAs("rf801_mcstudy.png")
147
148# Make ROOT.RooMCStudy object available on command line after
149# macro finishes
150ROOT.gDirectory.Add(mcstudy)