Logo ROOT  
Reference Guide
rf305_condcorrprod.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Multidimensional models: multi-dimensional pdfs with conditional pdfs in product
5##
6## `pdf = gauss(x,f(y),sx | y ) * gauss(y,ms,sx)` with `f(y) = a0 + a1*y`
7##
8## \macro_code
9##
10## \date February 2018
11## \authors Clemens Lange, Wouter Verkerke (C++ version)
12
13import ROOT
14
15# Create conditional pdf gx(x|y)
16# -----------------------------------------------------------
17
18# Create observables
19x = ROOT.RooRealVar("x", "x", -5, 5)
20y = ROOT.RooRealVar("y", "y", -5, 5)
21
22# Create function f(y) = a0 + a1*y
23a0 = ROOT.RooRealVar("a0", "a0", -0.5, -5, 5)
24a1 = ROOT.RooRealVar("a1", "a1", -0.5, -1, 1)
25fy = ROOT.RooPolyVar("fy", "fy", y, [a0, a1])
26
27# Create gaussx(x,f(y),sx)
28sigmax = ROOT.RooRealVar("sigma", "width of gaussian", 0.5)
29gaussx = ROOT.RooGaussian("gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax)
30
31# Create pdf gy(y)
32# -----------------------------------------------------------
33
34# Create gaussy(y,0,5)
35gaussy = ROOT.RooGaussian("gaussy", "Gaussian in y", y, 0.0, 3.0)
36
37# Create product gx(x|y)*gy(y)
38# -------------------------------------------------------
39
40# Create gaussx(x,sx|y) * gaussy(y)
41model = ROOT.RooProdPdf("model", "gaussx(x|y)*gaussy(y)", {gaussy}, Conditional=({gaussx}, {x}))
42
43# Sample, fit and plot product pdf
44# ---------------------------------------------------------------
45
46# Generate 1000 events in x and y from model
47data = model.generate({x, y}, 10000)
48
49# Plot x distribution of data and projection of model x = Int(dy)
50# model(x,y)
51xframe = x.frame()
52data.plotOn(xframe)
53model.plotOn(xframe)
54
55# Plot x distribution of data and projection of model y = Int(dx)
56# model(x,y)
57yframe = y.frame()
58data.plotOn(yframe)
59model.plotOn(yframe)
60
61# Make two-dimensional plot in x vs y
62hh_model = model.createHistogram("hh_model", x, ROOT.RooFit.Binning(50), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(50)))
63hh_model.SetLineColor(ROOT.kBlue)
64
65# Make canvas and draw ROOT.RooPlots
66c = ROOT.TCanvas("rf305_condcorrprod", "rf05_condcorrprod", 1200, 400)
67c.Divide(3)
68c.cd(1)
69ROOT.gPad.SetLeftMargin(0.15)
70xframe.GetYaxis().SetTitleOffset(1.6)
71xframe.Draw()
72c.cd(2)
73ROOT.gPad.SetLeftMargin(0.15)
74yframe.GetYaxis().SetTitleOffset(1.6)
75yframe.Draw()
76c.cd(3)
77ROOT.gPad.SetLeftMargin(0.20)
78hh_model.GetZaxis().SetTitleOffset(2.5)
79hh_model.Draw("surf")
80
81c.SaveAs("rf305_condcorrprod.png")