Logo ROOT  
Reference Guide
 
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_code
11##
12## \date February 2018
13## \authors Clemens Lange, Wouter Verkerke (C++ version)
14
15import ROOT
16
17# Write class skeleton code
18# --------------------------------------------------
19
20# Write skeleton pdf class with variable x,a,b
21# To use this class,
22# - Edit the file MyPdfV1.cxx and implement the evaluate() method in terms of x,a and b
23# - Compile and link class with '.x MyPdfV1.cxx+'
24#
25ROOT.RooClassFactory.makePdf("MyPdfV1", "x,A,B")
26
27# With added initial value expression
28# ---------------------------------------------------------------------
29
30# Write skeleton pdf class with variable x,a,b and given formula expression
31# To use this class,
32# - Compile and link class with '.x MyPdfV2.cxx+'
33#
34ROOT.RooClassFactory.makePdf("MyPdfV2", "x,A,B", "", "A*fabs(x)+pow(x-B,2)")
35
36# With added analytical integral expression
37# ---------------------------------------------------------------------------------
38
39# Write skeleton pdf class with variable x,a,b, given formula expression _and_
40# given expression for analytical integral over x
41# To use this class,
42# - Compile and link class with '.x MyPdfV3.cxx+'
43#
44ROOT.RooClassFactory.makePdf(
45 "MyPdfV3",
46 "x,A,B",
47 "",
48 "A*fabs(x)+pow(x-B,2)",
49 True,
50 False,
51 "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))",
52)
53
54# Use instance of created class
55# ---------------------------------------------------------
56
57# Compile MyPdfV3 class
58ROOT.gROOT.ProcessLineSync(".x MyPdfV3.cxx+")
59
60# Creat instance of MyPdfV3 class
61a = ROOT.RooRealVar("a", "a", 1)
62b = ROOT.RooRealVar("b", "b", 2, -10, 10)
63y = ROOT.RooRealVar("y", "y", -10, 10)
64pdf = ROOT.MyPdfV3("pdf", "pdf", y, a, b)
65
66# Generate toy data from pdf and plot data and pdf on frame
67frame1 = y.frame(Title="Compiled class MyPdfV3")
68data = pdf.generate({y}, 1000)
69pdf.fitTo(data)
70data.plotOn(frame1)
71pdf.plotOn(frame1)
72
73# /
74# 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 #
75# /
76
77# Declare observable x
78x = ROOT.RooRealVar("x", "x", -20, 20)
79
80# The ROOT.RooClassFactory.makePdfInstance() function performs code writing, compiling, linking
81# and object instantiation in one go and can serve as a straight
82# replacement of ROOT.RooGenericPdf
83
84alpha = ROOT.RooRealVar("alpha", "alpha", 5, 0.1, 10)
85genpdf = ROOT.RooClassFactory.makePdfInstance("GenPdf", "(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))", [x, alpha])
86
87# Generate a toy dataset from the interpreted pdf
88data2 = genpdf.generate({x}, 50000)
89
90# Fit the interpreted pdf to the generated data
91genpdf.fitTo(data2)
92
93# Make a plot of the data and the pdf overlaid
94frame2 = x.frame(Title="Compiled version of pdf of rf103")
95data2.plotOn(frame2)
96genpdf.plotOn(frame2)
97
98# Draw all frames on a canvas
99c = ROOT.TCanvas("rf104_classfactory", "rf104_classfactory", 800, 400)
100c.Divide(2)
101c.cd(1)
102ROOT.gPad.SetLeftMargin(0.15)
103frame1.GetYaxis().SetTitleOffset(1.4)
104frame1.Draw()
105c.cd(2)
106ROOT.gPad.SetLeftMargin(0.15)
107frame2.GetYaxis().SetTitleOffset(1.4)
108frame2.Draw()
109
110c.SaveAs("rf104_classfactory.png")