Rf 3 1 5_Projectpdf

Multidimensional models: marginizalization of multi-dimensional pdfs through integration

Author: Clemens Lange, Wouter Verkerke (C++ version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 10:01 AM.

In [ ]:
import ROOT

Create pdf m(x,y) = gx(x|y) * g(y)

Increase default precision of numeric integration as self exercise has high sensitivity to numeric integration precision

In [ ]:
ROOT.RooAbsPdf.defaultIntegratorConfig().setEpsRel(1e-8)
ROOT.RooAbsPdf.defaultIntegratorConfig().setEpsAbs(1e-8)

Create observables

In [ ]:
x = ROOT.RooRealVar("x", "x", -5, 5)
y = ROOT.RooRealVar("y", "y", -2, 2)

Create function f(y) = a0 + a1*y

In [ ]:
a0 = ROOT.RooRealVar("a0", "a0", 0)
a1 = ROOT.RooRealVar("a1", "a1", -1.5, -3, 1)
fy = ROOT.RooPolyVar("fy", "fy", y, [a0, a1])

Create gaussx(x,f(y),sx)

In [ ]:
sigmax = ROOT.RooRealVar("sigmax", "width of gaussian", 0.5)
gaussx = ROOT.RooGaussian("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax)

Create gaussy(y,0,2)

In [ ]:
gaussy = ROOT.RooGaussian("gaussy", "Gaussian in y", y, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(2))

Create gaussx(x,sx|y) * gaussy(y)

In [ ]:
model = ROOT.RooProdPdf(
    "model",
    "gaussx(x|y)*gaussy(y)",
    {gaussy},
    Conditional=({gaussx}, {x}),
)

Marginalize m(x,y) to m(x)

modelx(x) = Int model(x,y) dy

In [ ]:
modelx = model.createProjection({y})

Use marginalized pdf as regular 1D pdf

Sample 1000 events from modelx

In [ ]:
data = modelx.generateBinned({x}, 1000)

Fit modelx to toy data

In [ ]:
modelx.fitTo(data, Verbose=True)

Plot modelx over data

In [ ]:
frame = x.frame(40)
data.plotOn(frame)
modelx.plotOn(frame)

Make 2D histogram of model(x,y)

In [ ]:
hh = model.createHistogram("x,y")
hh.SetLineColor(ROOT.kBlue)

c = ROOT.TCanvas("rf315_projectpdf", "rf315_projectpdf", 800, 400)
c.Divide(2)
c.cd(1)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()
c.cd(2)
ROOT.gPad.SetLeftMargin(0.20)
hh.GetZaxis().SetTitleOffset(2.5)
hh.Draw("surf")
c.SaveAs("rf315_projectpdf.png")

Draw all canvases

In [ ]:
from ROOT import gROOT 
gROOT.GetListOfCanvases().Draw()