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