ROOT   Reference Guide
Searching...
No Matches
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_image
9## \macro_code
10## \macro_output
11##
12## \date February 2018
13## \authors Clemens Lange, Wouter Verkerke (C version)
14
15import ROOT
16
17# Bind ROOT TMath::Erf C function
18# ---------------------------------------------------
19
20# Bind one-dimensional ROOT.TMath.Erf function as ROOT.RooAbsReal function
21x = ROOT.RooRealVar("x", "x", -3, 3)
22erf = ROOT.RooFit.bindFunction("erf", ROOT.TMath.Erf, x)
23
24# Print erf definition
25erf.Print()
26
27# Plot erf on frame
28frame1 = x.frame(Title="TMath.Erf bound as ROOT.RooFit function")
29erf.plotOn(frame1)
30
31# Bind ROOT::Math::beta_pdf C function
32# -----------------------------------------------------------------------
33
34# Bind pdf ROOT.Math.Beta with three variables as ROOT.RooAbsPdf function
35x2 = ROOT.RooRealVar("x2", "x2", 0, 0.999)
36a = ROOT.RooRealVar("a", "a", 5, 0, 10)
37b = ROOT.RooRealVar("b", "b", 2, 0, 10)
38beta = ROOT.RooFit.bindPdf("beta", ROOT.Math.beta_pdf, x2, a, b)
39
40# Perf beta definition
41beta.Print()
42
43# Generate some events and fit
44data = beta.generate({x2}, 10000)
45beta.fitTo(data, PrintLevel=-1)
46
47# Plot data and pdf on frame
48frame2 = x2.frame(Title="ROOT.Math.Beta bound as ROOT.RooFit pdf")
49data.plotOn(frame2)
50beta.plotOn(frame2)
51
52# Bind ROOT TF1 as RooFit function
53# ---------------------------------------------------------------
54
55# Create a ROOT TF1 function
56fa1 = ROOT.TF1("fa1", "sin(x)/x", 0, 10)
57
58# Create an observable
59x3 = ROOT.RooRealVar("x3", "x3", 0.01, 20)
60
61# Create binding of TF1 object to above observable
62rfa1 = ROOT.RooFit.bindFunction(fa1, x3)
63
64# Print rfa1 definition
65rfa1.Print()
66
67# Make plot frame in observable, TF1 binding function
68frame3 = x3.frame(Title="TF1 bound as ROOT.RooFit function")
69rfa1.plotOn(frame3)
70
71c = ROOT.TCanvas("rf105_funcbinding", "rf105_funcbinding", 1200, 400)
72c.Divide(3)
73c.cd(1)