Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf802_mcstudy_addons.py
Go to the documentation of this file.
1## \ingroup tutorial_roofit
2## \notebook
3##
4## 'VALIDATION AND MC STUDIES' RooFit tutorial macro #802
5##
6## RooMCStudy: using separate fit and generator models, the chi^2 calculator model
7##
8## \macro_code
9##
10## \date February 2018
11## \author Clemens Lange
12
13
14import ROOT
15
16
17# Create model
18# -----------------------
19
20# Observables, parameters
21x = ROOT.RooRealVar("x", "x", -10, 10)
22x.setBins(10)
23mean = ROOT.RooRealVar("mean", "mean of gaussian", 0, -2.0, 1.8)
24sigma = ROOT.RooRealVar("sigma", "width of gaussian", 5, 1, 10)
25
26# Create Gaussian pdf
27gauss = ROOT.RooGaussian("gauss", "gaussian PDF", x, mean, sigma)
28
29# Create manager with chi^2 add-on module
30# ----------------------------------------------------------------------------
31
32# Create study manager for binned likelihood fits of a Gaussian pdf in 10
33# bins
34mcs = ROOT.RooMCStudy(gauss, {x}, Silence=True, Binned=True)
35
36# Add chi^2 calculator module to mcs
37chi2mod = ROOT.RooChi2MCSModule()
38mcs.addModule(chi2mod)
39
40# Generate 1000 samples of 1000 events
41mcs.generateAndFit(2000, 1000)
42
43# Number of bins for chi2 plots
44nBins = 100
45
46# Fill histograms with distributions chi2 and prob(chi2,ndf) that
47# are calculated by ROOT.RooChiMCSModule
48hist_chi2 = mcs.fitParDataSet().createHistogram("chi2", AutoBinning=nBins)
49hist_prob = mcs.fitParDataSet().createHistogram("prob", AutoBinning=nBins)
50
51# Create manager with separate fit model
52# ----------------------------------------------------------------------------
53
54# Create alternate pdf with shifted mean
55mean2 = ROOT.RooRealVar("mean2", "mean of gaussian 2", 2.0)
56gauss2 = ROOT.RooGaussian("gauss2", "gaussian PDF2", x, mean2, sigma)
57
58# Create study manager with separate generation and fit model. ROOT.This configuration
59# is set up to generate bad fits as the fit and generator model have different means
60# and the mean parameter is not floating in the fit
61mcs2 = ROOT.RooMCStudy(gauss2, {x}, FitModel=gauss, Silence=True, Binned=True)
62
63# Add chi^2 calculator module to mcs
64chi2mod2 = ROOT.RooChi2MCSModule()
65mcs2.addModule(chi2mod2)
66
67# Generate 1000 samples of 1000 events
68mcs2.generateAndFit(2000, 1000)
69
70# Request a the pull plot of mean. The pulls will be one-sided because
71# `mean` is limited to 1.8.
72# Note that RooFit will have trouble to compute the pulls because the parameters
73# are called `mean` in the fit, but `mean2` in the generator model. It is not obvious
74# that these are related. RooFit will nevertheless compute pulls, but complain that
75# this is risky.
76pullMeanFrame = mcs2.plotPull(mean)
77
78# Fill histograms with distributions chi2 and prob(chi2,ndf) that
79# are calculated by ROOT.RooChiMCSModule
80hist2_chi2 = mcs2.fitParDataSet().createHistogram("chi2", AutoBinning=nBins)
81hist2_prob = mcs2.fitParDataSet().createHistogram("prob", AutoBinning=nBins)
82hist2_chi2.SetLineColor(ROOT.kRed)
83hist2_prob.SetLineColor(ROOT.kRed)
84
85c = ROOT.TCanvas("rf802_mcstudy_addons", "rf802_mcstudy_addons", 800, 400)
86c.Divide(3)
87c.cd(1)
88ROOT.gPad.SetLeftMargin(0.15)
89hist_chi2.GetYaxis().SetTitleOffset(1.4)
90hist_chi2.Draw()
91hist2_chi2.Draw("esame")
92c.cd(2)
93ROOT.gPad.SetLeftMargin(0.15)
94hist_prob.GetYaxis().SetTitleOffset(1.4)
95hist_prob.Draw()
96hist2_prob.Draw("esame")
97c.cd(3)
98pullMeanFrame.Draw()
99
100c.SaveAs("rf802_mcstudy_addons.png")
101
102# Make RooMCStudy object available on command line after
103# macro finishes
104ROOT.gDirectory.Add(mcs)