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 p.d.f.s with conditional p.d.fs 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## \author 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, ROOT.RooArgList(a0, a1))
26
27# Create gaussx(x,f(y),sx)
28sigmax = ROOT.RooRealVar("sigma", "width of gaussian", 0.5)
29gaussx = ROOT.RooGaussian(
30 "gaussx", "Gaussian in x with shifting mean in y", x, fy, sigmax)
31
32# Create pdf gy(y)
33# -----------------------------------------------------------
34
35# Create gaussy(y,0,5)
36gaussy = ROOT.RooGaussian(
37 "gaussy",
38 "Gaussian in y",
39 y,
40 ROOT.RooFit.RooConst(0),
41 ROOT.RooFit.RooConst(3))
42
43# Create product gx(x|y)*gy(y)
44# -------------------------------------------------------
45
46# Create gaussx(x,sx|y) * gaussy(y)
47model = ROOT.RooProdPdf(
48 "model",
49 "gaussx(x|y)*gaussy(y)",
50 ROOT.RooArgSet(gaussy),
51 ROOT.RooFit.Conditional(
52 ROOT.RooArgSet(gaussx),
53 ROOT.RooArgSet(x)))
54
55# Sample, fit and plot product pdf
56# ---------------------------------------------------------------
57
58# Generate 1000 events in x and y from model
59data = model.generate(ROOT.RooArgSet(x, y), 10000)
60
61# Plot x distribution of data and projection of model x = Int(dy)
62# model(x,y)
63xframe = x.frame()
64data.plotOn(xframe)
65model.plotOn(xframe)
66
67# Plot x distribution of data and projection of model y = Int(dx)
68# model(x,y)
69yframe = y.frame()
70data.plotOn(yframe)
71model.plotOn(yframe)
72
73# Make two-dimensional plot in x vs y
74hh_model = model.createHistogram("hh_model", x, ROOT.RooFit.Binning(
75 50), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(50)))
76hh_model.SetLineColor(ROOT.kBlue)
77
78# Make canvas and draw ROOT.RooPlots
79c = ROOT.TCanvas("rf305_condcorrprod", "rf05_condcorrprod", 1200, 400)
80c.Divide(3)
81c.cd(1)
82ROOT.gPad.SetLeftMargin(0.15)
83xframe.GetYaxis().SetTitleOffset(1.6)
84xframe.Draw()
85c.cd(2)
86ROOT.gPad.SetLeftMargin(0.15)
87yframe.GetYaxis().SetTitleOffset(1.6)
88yframe.Draw()
89c.cd(3)
90ROOT.gPad.SetLeftMargin(0.20)
91hh_model.GetZaxis().SetTitleOffset(2.5)
92hh_model.Draw("surf")
93
94c.SaveAs("rf305_condcorrprod.png")