ROOT   Reference Guide
rf105_funcbinding.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'BASIC FUNCTIONALITY' RooFit tutorial macro #105
5## Demonstration of binding ROOT Math functions as RooFit functions
6## and pdfs
7##
8## \macro_code
9##
10## \date February 2018
11## \author Clemens Lange
12## \author Wouter Verkerke (C version)
13
14import ROOT
15
16# Bind ROOT TMath::Erf C function
17# ---------------------------------------------------
18
19# Bind one-dimensional ROOT.TMath.Erf function as ROOT.RooAbsReal function
20# Directly trying this in python doesn't work:
21# x = ROOT.RooRealVar("x", "x", -3, 3)
22# erf = ROOT.RooFit.bindFunction("erf", ROOT.TMath.Erf, x)
23# Need to go through C interface
24ROOT.gInterpreter.ProcessLine(
25 'auto x = RooRealVar("x", "x", -3, 3); auto myerf = RooFit::bindFunction("erf", TMath::Erf, x)'
26)
27x = ROOT.x
28erf = ROOT.myerf
29
30# Print erf definition
31erf.Print()
32
33# Plot erf on frame
34frame1 = x.frame(Title="TMath.Erf bound as ROOT.RooFit function")
35erf.plotOn(frame1)
36
37# Bind ROOT::Math::beta_pdf C function
38# -----------------------------------------------------------------------
39
40# Bind pdf ROOT.Math.Beta with three variables as ROOT.RooAbsPdf function
41# As above, this does not work directly in python
42# x2 = ROOT.RooRealVar("x2", "x2", 0, 0.999)
43# a = ROOT.RooRealVar("a", "a", 5, 0, 10)
44# b = ROOT.RooRealVar("b", "b", 2, 0, 10)
45# beta = ROOT.RooFit.bindPdf("beta", ROOT.Math.beta_pdf, x2, a, b)
46ROOT.gInterpreter.ProcessLine(
47 'auto x2 = RooRealVar("x2", "x2", 0, 0.999);\
48 auto a = RooRealVar("a", "a", 5, 0, 10);\
49 auto b = RooRealVar("b", "b", 5, 0, 10);\
50 auto beta = RooFit::bindPdf("beta", ROOT::Math::beta_pdf, x2, a, b)'
51)
52x2 = ROOT.x2
53a = ROOT.a
54b = ROOT.b
55beta = ROOT.beta
56
57# Perf beta definition
58beta.Print()
59
60# Generate some events and fit
61data = beta.generate({x2}, 10000)
62beta.fitTo(data)
63
64# Plot data and pdf on frame
65frame2 = x2.frame(Title="ROOT.Math.Beta bound as ROOT.RooFit pdf")
66data.plotOn(frame2)
67beta.plotOn(frame2)
68
69# Bind ROOT TF1 as RooFit function
70# ---------------------------------------------------------------
71
72# Create a ROOT TF1 function
73fa1 = ROOT.TF1("fa1", "sin(x)/x", 0, 10)
74
75# Create an observable
76x3 = ROOT.RooRealVar("x3", "x3", 0.01, 20)
77
78# Create binding of TF1 object to above observable
79rfa1 = ROOT.RooFit.bindFunction(fa1, x3)
80
81# Print rfa1 definition
82rfa1.Print()
83
84# Make plot frame in observable, TF1 binding function
85frame3 = x3.frame(Title="TF1 bound as ROOT.RooFit function")
86rfa1.plotOn(frame3)
87
88c = ROOT.TCanvas("rf105_funcbinding", "rf105_funcbinding", 1200, 400)
89c.Divide(3)
90c.cd(1)
91ROOT.gPad.SetLeftMargin(0.15)
92frame1.GetYaxis().SetTitleOffset(1.6)
93frame1.Draw()
94c.cd(2)
95ROOT.gPad.SetLeftMargin(0.15)
96frame2.GetYaxis().SetTitleOffset(1.6)
97frame2.Draw()
98c.cd(3)
99ROOT.gPad.SetLeftMargin(0.15)
100frame3.GetYaxis().SetTitleOffset(1.6)
101frame3.Draw()
102
103c.SaveAs("rf105_funcbinding.png")