Loading [MathJax]/extensions/tex2jax.js
Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
rf315_projectpdf.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Multidimensional models: marginizalization of multi-dimensional pdfs through integration
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11import ROOT
12
13
14# Create pdf m(x,y) = gx(x|y) * g(y)
15# --------------------------------------------------------------
16
17# Increase default precision of numeric integration
18# as self exercise has high sensitivity to numeric integration precision
19ROOT.RooAbsPdf.defaultIntegratorConfig().setEpsRel(1e-8)
20ROOT.RooAbsPdf.defaultIntegratorConfig().setEpsAbs(1e-8)
21
22# Create observables
23x = ROOT.RooRealVar("x", "x", -5, 5)
24y = ROOT.RooRealVar("y", "y", -2, 2)
25
26# Create function f(y) = a0 + a1*y
27a0 = ROOT.RooRealVar("a0", "a0", 0)
28a1 = ROOT.RooRealVar("a1", "a1", -1.5, -3, 1)
29fy = ROOT.RooPolyVar("fy", "fy", y, [a0, a1])
30
31# Create gaussx(x,f(y),sx)
32sigmax = ROOT.RooRealVar("sigmax", "width of gaussian", 0.5)
33gaussx = ROOT.RooGaussian("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax)
34
35# Create gaussy(y,0,2)
36gaussy = ROOT.RooGaussian("gaussy", "Gaussian in y", y, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(2))
37
38# Create gaussx(x,sx|y) * gaussy(y)
39model = ROOT.RooProdPdf(
40 "model",
41 "gaussx(x|y)*gaussy(y)",
42 {gaussy},
43 Conditional=({gaussx}, {x}),
44)
45
46# Marginalize m(x,y) to m(x)
47# ----------------------------------------------------
48
49# modelx(x) = Int model(x,y) dy
50modelx = model.createProjection({y})
51
52# Use marginalized pdf as regular 1D pdf
53# -----------------------------------------------
54
55# Sample 1000 events from modelx
56data = modelx.generateBinned({x}, 1000)
57
58# Fit modelx to toy data
59modelx.fitTo(data, Verbose=True)
60
61# Plot modelx over data
62frame = x.frame(40)
63data.plotOn(frame)
64modelx.plotOn(frame)
65
66# Make 2D histogram of model(x,y)
67hh = model.createHistogram("x,y")
68hh.SetLineColor(ROOT.kBlue)
69
70c = ROOT.TCanvas("rf315_projectpdf", "rf315_projectpdf", 800, 400)
71c.Divide(2)
72c.cd(1)
73ROOT.gPad.SetLeftMargin(0.15)
74frame.GetYaxis().SetTitleOffset(1.4)
75frame.Draw()
76c.cd(2)
77ROOT.gPad.SetLeftMargin(0.20)
78hh.GetZaxis().SetTitleOffset(2.5)
79hh.Draw("surf")
80c.SaveAs("rf315_projectpdf.png")