Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf303_conditional.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #303
5## Use of tailored p.d.f as conditional p.d.fs.s
6##
7## pdf = gauss(x,f(y),sx | y ) with f(y) = a0 + a1*y
8##
9## \macro_code
10##
11## \date February 2018
12## \author Clemens Lange
13## \author Wouter Verkerke (C version)
14
15import ROOT
16
17
18def makeFakeDataXY():
19 x = ROOT.RooRealVar("x", "x", -10, 10)
20 y = ROOT.RooRealVar("y", "y", -10, 10)
21 coord = {x, y}
22
23 d = ROOT.RooDataSet("d", "d", coord)
24
25 for i in range(10000):
26 tmpy = ROOT.gRandom.Gaus(0, 10)
27 tmpx = ROOT.gRandom.Gaus(0.5 * tmpy, 1)
28 if (abs(tmpy) < 10) and (abs(tmpx) < 10):
29 x.setVal(tmpx)
30 y.setVal(tmpy)
31 d.add(coord)
32
33 return d
34
35
36# Set up composed model gauss(x, m(y), s)
37# -----------------------------------------------------------------------
38
39# Create observables
40x = ROOT.RooRealVar("x", "x", -10, 10)
41y = ROOT.RooRealVar("y", "y", -10, 10)
42
43# Create function f(y) = a0 + a1*y
44a0 = ROOT.RooRealVar("a0", "a0", -0.5, -5, 5)
45a1 = ROOT.RooRealVar("a1", "a1", -0.5, -1, 1)
46fy = ROOT.RooPolyVar("fy", "fy", y, [a0, a1])
47
48# Creat gauss(x,f(y),s)
49sigma = ROOT.RooRealVar("sigma", "width of gaussian", 0.5, 0.1, 2.0)
50model = ROOT.RooGaussian("model", "Gaussian with shifting mean", x, fy, sigma)
51
52# Obtain fake external experimental dataset with values for x and y
53expDataXY = makeFakeDataXY()
54
55# Generate data from conditional p.d.f. model(x|y)
56# ---------------------------------------------------------------------------------------------
57
58# Make subset of experimental data with only y values
59expDataY = expDataXY.reduce({y})
60
61# Generate 10000 events in x obtained from _conditional_ model(x|y) with y
62# values taken from experimental data
63data = model.generate({x}, ProtoData=expDataY)
64data.Print()
65
66# Fit conditional p.d.f model(x|y) to data
67# ---------------------------------------------------------------------------------------------
68
69model.fitTo(expDataXY, ConditionalObservables={y})
70
71# Project conditional p.d.f on x and y dimensions
72# ---------------------------------------------------------------------------------------------
73
74# Plot x distribution of data and projection of model x = 1/Ndata
75# sum(data(y_i)) model(x;y_i)
76xframe = x.frame()
77expDataXY.plotOn(xframe)
78model.plotOn(xframe, ProjWData=expDataY)
79
80# Speed up (and approximate) projection by using binned clone of data for
81# projection
82binnedDataY = expDataY.binnedClone()
83model.plotOn(xframe, ProjWData=binnedDataY, LineColor="c", LineStyle=":")
84
85# Show effect of projection with too coarse binning
86(expDataY.get().find("y")).setBins(5)
87binnedDataY2 = expDataY.binnedClone()
88model.plotOn(xframe, ProjWData=binnedDataY2, LineColor="r")
89
90# Make canvas and draw ROOT.RooPlots
91c = ROOT.TCanvas("rf303_conditional", "rf303_conditional", 600, 460)
92ROOT.gPad.SetLeftMargin(0.15)
93xframe.GetYaxis().SetTitleOffset(1.2)
94xframe.Draw()
95
96c.SaveAs("rf303_conditional.png")