Logo ROOT  
Reference Guide
rf804_mcstudy_constr.py
Go to the documentation of this file.
1## \ingroup tutorial_roofit
2## \notebook
3##
4## 'VALIDATION AND MC STUDIES' RooFit tutorial macro #804
5##
6## Using RooMCStudy on models with constraints
7##
8## \macro_code
9##
10## \date February 2018
11## \author Clemens Lange
12
13
14import ROOT
15
16
17# Create model with parameter constraint
18# ---------------------------------------------------------------------------
19
20# Observable
21x = ROOT.RooRealVar("x", "x", -10, 10)
22
23# Signal component
24m = ROOT.RooRealVar("m", "m", 0, -10, 10)
25s = ROOT.RooRealVar("s", "s", 2, 0.1, 10)
26g = ROOT.RooGaussian("g", "g", x, m, s)
27
28# Background component
29p = ROOT.RooPolynomial("p", "p", x)
30
31# Composite model
32f = ROOT.RooRealVar("f", "f", 0.4, 0.0, 1.0)
33sum = ROOT.RooAddPdf("sum", "sum", [g, p], [f])
34
35# Construct constraint on parameter f
36fconstraint = ROOT.RooGaussian("fconstraint", "fconstraint", f, ROOT.RooFit.RooConst(0.7), ROOT.RooFit.RooConst(0.1))
37
38# Multiply constraint with p.d.f
39sumc = ROOT.RooProdPdf("sumc", "sum with constraint", [sum, fconstraint])
40
41# Setup toy study with model
42# ---------------------------------------------------
43
44# Perform toy study with internal constraint on f
45mcs = ROOT.RooMCStudy(
46 sumc,
47 {x},
48 ROOT.RooFit.Constrain({f}),
49 ROOT.RooFit.Silence(),
50 ROOT.RooFit.Binned(),
51 ROOT.RooFit.FitOptions(ROOT.RooFit.PrintLevel(-1)),
52)
53
54# Run 500 toys of 2000 events.
55# Before each toy is generated, value for the f is sampled from the constraint pdf and
56# that value is used for the generation of that toy.
57mcs.generateAndFit(500, 2000)
58
59# Make plot of distribution of generated value of f parameter
60h_f_gen = ROOT.RooAbsData.createHistogram(mcs.fitParDataSet(), "f_gen", -40)
61
62# Make plot of distribution of fitted value of f parameter
63frame1 = mcs.plotParam(f, ROOT.RooFit.Bins(40))
64frame1.SetTitle("Distribution of fitted f values")
65
66# Make plot of pull distribution on f
67frame2 = mcs.plotPull(f, ROOT.RooFit.Bins(40), ROOT.RooFit.FitGauss())
68frame1.SetTitle("Distribution of f pull values")
69
70c = ROOT.TCanvas("rf804_mcstudy_constr", "rf804_mcstudy_constr", 1200, 400)
71c.Divide(3)
72c.cd(1)
73ROOT.gPad.SetLeftMargin(0.15)
74h_f_gen.GetYaxis().SetTitleOffset(1.4)
75h_f_gen.Draw()
76c.cd(2)
77ROOT.gPad.SetLeftMargin(0.15)
78frame1.GetYaxis().SetTitleOffset(1.4)
79frame1.Draw()
80c.cd(3)
81ROOT.gPad.SetLeftMargin(0.15)
82frame2.GetYaxis().SetTitleOffset(1.4)
83frame2.Draw()
84
85c.SaveAs("rf804_mcstudy_constr.png")