ROOT
Version v6.32
master
v6.34
v6.30
v6.28
v6.26
v6.24
v6.22
v6.20
v6.18
v6.16
v6.14
v6.12
v6.10
v6.08
v6.06
Reference Guide
►
ROOT
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
rf104_classfactory.py
Go to the documentation of this file.
1
## \file
2
## \ingroup tutorial_roofit
3
## \notebook
4
## Basic functionality: the class factory for functions and pdfs
5
##
6
## NOTE: This demo uses code that is generated by the macro,
7
## which can be compiled on the fly (set to MyPdfV3 below).
8
## To use MyPdfV1 or MyPdfV2, adjust lines below accordingly.
9
##
10
## \macro_image
11
## \macro_code
12
## \macro_output
13
##
14
## \date February 2018
15
## \authors Clemens Lange, Wouter Verkerke (C++ version)
16
17
import
ROOT
18
19
# Write class skeleton code
20
# --------------------------------------------------
21
22
# Write skeleton pdf class with variable x,a,b
23
# To use this class,
24
# - Edit the file MyPdfV1.cxx and implement the evaluate() method in terms of x,a and b
25
# - Compile and link class with '.x MyPdfV1.cxx+'
26
#
27
ROOT.RooClassFactory.makePdf
(
"MyPdfV1"
,
"x,A,B"
)
28
29
# With added initial value expression
30
# ---------------------------------------------------------------------
31
32
# Write skeleton pdf class with variable x,a,b and given formula expression
33
# To use this class,
34
# - Compile and link class with '.x MyPdfV2.cxx+'
35
#
36
ROOT.RooClassFactory.makePdf
(
"MyPdfV2"
,
"x,A,B"
,
""
,
"A*fabs(x)+pow(x-B,2)"
)
37
38
# With added analytical integral expression
39
# ---------------------------------------------------------------------------------
40
41
# Write skeleton pdf class with variable x,a,b, given formula expression _and_
42
# given expression for analytical integral over x
43
# To use this class,
44
# - Compile and link class with '.x MyPdfV3.cxx+'
45
#
46
ROOT.RooClassFactory.makePdf
(
47
"MyPdfV3"
,
48
"x,A,B"
,
49
""
,
50
"A*fabs(x)+pow(x-B,2)"
,
51
True
,
52
False
,
53
"x:(A/2)*(pow(x.max(rangeName),2)+pow(x.min(rangeName),2))+(1./3)*(pow(x.max(rangeName)-B,3)-pow(x.min(rangeName)-B,3))"
,
54
)
55
56
# Use instance of created class
57
# ---------------------------------------------------------
58
59
# Compile MyPdfV3 class
60
ROOT.gROOT.ProcessLineSync
(
".x MyPdfV3.cxx+"
)
61
62
# Creat instance of MyPdfV3 class
63
a =
ROOT.RooRealVar
(
"a"
,
"a"
, 1)
64
b =
ROOT.RooRealVar
(
"b"
,
"b"
, 2, -10, 10)
65
y =
ROOT.RooRealVar
(
"y"
,
"y"
, -10, 10)
66
pdf =
ROOT.MyPdfV3
(
"pdf"
,
"pdf"
, y, a, b)
67
68
# Generate toy data from pdf and plot data and pdf on frame
69
frame1 =
y.frame
(Title=
"Compiled class MyPdfV3"
)
70
data =
pdf.generate
({y}, 1000)
71
pdf.fitTo
(data, PrintLevel=-1)
72
data.plotOn
(frame1)
73
pdf.plotOn
(frame1)
74
75
# /
76
# C o m p i l e d v e r s i o n o f e x a m p l e r f 1 0 3 #
77
# /
78
79
# Declare observable x
80
x =
ROOT.RooRealVar
(
"x"
,
"x"
, -20, 20)
81
82
# The ROOT.RooClassFactory.makePdfInstance() function performs code writing, compiling, linking
83
# and object instantiation in one go and can serve as a straight
84
# replacement of ROOT.RooGenericPdf
85
86
alpha =
ROOT.RooRealVar
(
"alpha"
,
"alpha"
, 5, 0.1, 10)
87
genpdf =
ROOT.RooClassFactory.makePdfInstance
(
"GenPdf"
,
"(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))"
, [x, alpha])
88
89
# Generate a toy dataset from the interpreted pdf
90
data2 =
genpdf.generate
({x}, 50000)
91
92
# Fit the interpreted pdf to the generated data
93
genpdf.fitTo
(data2, PrintLevel=-1)
94
95
# Make a plot of the data and the pdf overlaid
96
frame2 =
x.frame
(Title=
"Compiled version of pdf of rf103"
)
97
data2.plotOn
(frame2)
98
genpdf.plotOn
(frame2)
99
100
# Draw all frames on a canvas
101
c =
ROOT.TCanvas
(
"rf104_classfactory"
,
"rf104_classfactory"
, 800, 400)
102
c.Divide
(2)
103
c.cd
(1)
104
ROOT.gPad.SetLeftMargin
(0.15)
105
frame1.GetYaxis
().SetTitleOffset(1.4)
106
frame1.Draw
()
107
c.cd
(2)
108
ROOT.gPad.SetLeftMargin
(0.15)
109
frame2.GetYaxis
().SetTitleOffset(1.4)
110
frame2.Draw
()
111
112
c.SaveAs
(
"rf104_classfactory.png"
)
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
tutorials
roofit
rf104_classfactory.py
ROOT v6-32 - Reference Guide Generated on Sun Mar 30 2025 15:10:43 (GVA Time) using Doxygen 1.10.0