ROOT   Reference Guide
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
17import 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#
27ROOT.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#
36ROOT.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#
46ROOT.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
60ROOT.gROOT.ProcessLineSync(".x MyPdfV3.cxx+")
61
62# Creat instance of MyPdfV3 class
63a = ROOT.RooRealVar("a", "a", 1)
64b = ROOT.RooRealVar("b", "b", 2, -10, 10)
65y = ROOT.RooRealVar("y", "y", -10, 10)
66pdf = ROOT.MyPdfV3("pdf", "pdf", y, a, b)
67
68# Generate toy data from pdf and plot data and pdf on frame
69frame1 = y.frame(Title="Compiled class MyPdfV3")
70data = pdf.generate({y}, 1000)
71pdf.fitTo(data, PrintLevel=-1)
72data.plotOn(frame1)
73pdf.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
80x = 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
86alpha = ROOT.RooRealVar("alpha", "alpha", 5, 0.1, 10)
87genpdf = 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
90data2 = genpdf.generate({x}, 50000)
91
92# Fit the interpreted pdf to the generated data
93genpdf.fitTo(data2, PrintLevel=-1)
94
95# Make a plot of the data and the pdf overlaid
96frame2 = x.frame(Title="Compiled version of pdf of rf103")
97data2.plotOn(frame2)
98genpdf.plotOn(frame2)
99
100# Draw all frames on a canvas
101c = ROOT.TCanvas("rf104_classfactory", "rf104_classfactory", 800, 400)
102c.Divide(2)
103c.cd(1)