Loading [MathJax]/extensions/tex2jax.js
Logo ROOT   6.16/01
Reference Guide
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
rf308_normintegration2d.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## 'MULTIDIMENSIONAL MODELS' RooFit tutorial macro #308
6##
7## Examples on normalization of p.d.f.s,
8## integration of p.d.fs, construction
9## of cumulative distribution functions from p.d.f.s
10## in two dimensions
11##
12## \macro_code
13##
14## \date February 2018
15## \author Clemens Lange
16## \author Wouter Verkerke (C version)
17
18from __future__ import print_function
19import ROOT
20
21# Set up model
22# ---------------------
23
24# Create observables x,y
25x = ROOT.RooRealVar("x", "x", -10, 10)
26y = ROOT.RooRealVar("y", "y", -10, 10)
27
28# Create p.d.f. gaussx(x,-2,3), gaussy(y,2,2)
29gx = ROOT.RooGaussian(
30 "gx", "gx", x, ROOT.RooFit.RooConst(-2), ROOT.RooFit.RooConst(3))
31gy = ROOT.RooGaussian(
32 "gy", "gy", y, ROOT.RooFit.RooConst(+2), ROOT.RooFit.RooConst(2))
33
34# gxy = gx(x)*gy(y)
35gxy = ROOT.RooProdPdf("gxy", "gxy", ROOT.RooArgList(gx, gy))
36
37# Retrieve raw & normalized values of RooFit p.d.f.s
38# --------------------------------------------------------------------------------------------------
39
40# Return 'raw' unnormalized value of gx
41print("gxy = ", gxy.getVal())
42
43# Return value of gxy normalized over x _and_ y in range [-10,10]
44nset_xy = ROOT.RooArgSet(x, y)
45print("gx_Norm[x,y] = ", gxy.getVal(nset_xy))
46
47# Create object representing integral over gx
48# which is used to calculate gx_Norm[x,y] == gx / gx_Int[x,y]
49igxy = gxy.createIntegral(ROOT.RooArgSet(x, y))
50print("gx_Int[x,y] = ", igxy.getVal())
51
52# NB: it is also possible to do the following
53
54# Return value of gxy normalized over x in range [-10,10] (i.e. treating y
55# as parameter)
56nset_x = ROOT.RooArgSet(x)
57print("gx_Norm[x] = ", gxy.getVal(nset_x))
58
59# Return value of gxy normalized over y in range [-10,10] (i.e. treating x
60# as parameter)
61nset_y = ROOT.RooArgSet(y)
62print("gx_Norm[y] = ", gxy.getVal(nset_y))
63
64# Integarte normalizes pdf over subrange
65# ----------------------------------------------------------------------------
66
67# Define a range named "signal" in x from -5,5
68x.setRange("signal", -5, 5)
69y.setRange("signal", -3, 3)
70
71# Create an integral of gxy_Norm[x,y] over x and y in range "signal"
72# ROOT.This is the fraction of of p.d.f. gxy_Norm[x,y] which is in the
73# range named "signal"
74igxy_sig = gxy.createIntegral(ROOT.RooArgSet(x, y), ROOT.RooFit.NormSet(
75 ROOT.RooArgSet(x, y)), ROOT.RooFit.Range("signal"))
76print("gx_Int[x,y|signal]_Norm[x,y] = ", igxy_sig.getVal())
77
78# Construct cumulative distribution function from pdf
79# -----------------------------------------------------------------------------------------------------
80
81# Create the cumulative distribution function of gx
82# i.e. calculate Int[-10,x] gx(x') dx'
83gxy_cdf = gxy.createCdf(ROOT.RooArgSet(x, y))
84
85# Plot cdf of gx versus x
86hh_cdf = gxy_cdf.createHistogram("hh_cdf", x, ROOT.RooFit.Binning(
87 40), ROOT.RooFit.YVar(y, ROOT.RooFit.Binning(40)))
88hh_cdf.SetLineColor(ROOT.kBlue)
89
90c = ROOT.TCanvas("rf308_normintegration2d",
91 "rf308_normintegration2d", 600, 600)
92ROOT.gPad.SetLeftMargin(0.15)
93hh_cdf.GetZaxis().SetTitleOffset(1.8)
94hh_cdf.Draw("surf")
95
96c.SaveAs("rf308_normintegration2d.png")