Logo ROOT   6.16/01
Reference Guide
rf104_classfactory.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## BASIC FUNCTIONALITY' RooFit tutorial macro #104
5## The class factory for functions and p.d.f.s
6##
7## NOTE: This demo uses code that is generated by the macro,
8## which can be compiled on the fly (set to MyPdfV3 below).
9## To use MyPdfV1 or MyPdfV2, adjust lines below accordingly.
10##
11## \macro_code
12##
13## \date February 2018
14## \author Clemens Lange
15## \author Wouter Verkerke (C version)
16
17import ROOT
18
19# Write class skeleton code
20# --------------------------------------------------
21
22# Write skeleton p.d.f 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 p.d.f 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(
37 "MyPdfV2", "x,A,B", "", "A*fabs(x)+pow(x-B,2)")
38
39# With added analytical integral expression
40# ---------------------------------------------------------------------------------
41
42# Write skeleton p.d.f class with variable x,a,b, given formula expression _and_
43# given expression for analytical integral over x
44# To use this class,
45# - Compile and link class with '.x MyPdfV3.cxx+'
46#
47ROOT.RooClassFactory.makePdf("MyPdfV3", "x,A,B", "", "A*fabs(x)+pow(x-B,2)", ROOT.kTRUE, ROOT.kFALSE,
48 "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))")
49
50# Use instance of created class
51# ---------------------------------------------------------
52
53# Compile MyPdfV3 class
54ROOT.gROOT.ProcessLineSync(".x MyPdfV3.cxx+")
55
56# Creat instance of MyPdfV3 class
57a = ROOT.RooRealVar("a", "a", 1)
58b = ROOT.RooRealVar("b", "b", 2, -10, 10)
59y = ROOT.RooRealVar("y", "y", -10, 10)
60pdf = ROOT.MyPdfV3("pdf", "pdf", y, a, b)
61
62# Generate toy data from pdf and plot data and p.d.f on frame
63frame1 = y.frame(ROOT.RooFit.Title("Compiled class MyPdfV3"))
64data = pdf.generate(ROOT.RooArgSet(y), 1000)
65pdf.fitTo(data)
66data.plotOn(frame1)
67pdf.plotOn(frame1)
68
69# /
70# 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 #
71# /
72
73# Declare observable x
74x = ROOT.RooRealVar("x", "x", -20, 20)
75
76# The ROOT.RooClassFactory.makePdfInstance() function performs code writing, compiling, linking
77# and object instantiation in one go and can serve as a straight
78# replacement of ROOT.RooGenericPdf
79
80alpha = ROOT.RooRealVar("alpha", "alpha", 5, 0.1, 10)
81genpdf = ROOT.RooClassFactory.makePdfInstance(
82 "GenPdf", "(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))", ROOT.RooArgList(x, alpha))
83
84# Generate a toy dataset from the interpreted p.d.f
85data2 = genpdf.generate(ROOT.RooArgSet(x), 50000)
86
87# Fit the interpreted p.d.f to the generated data
88genpdf.fitTo(data2)
89
90# Make a plot of the data and the p.d.f overlaid
91frame2 = x.frame(ROOT.RooFit.Title("Compiled version of pdf of rf103"))
92data2.plotOn(frame2)
93genpdf.plotOn(frame2)
94
95# Draw all frames on a canvas
96c = ROOT.TCanvas("rf104_classfactory", "rf104_classfactory", 800, 400)
97c.Divide(2)
98c.cd(1)
99ROOT.gPad.SetLeftMargin(0.15)
100frame1.GetYaxis().SetTitleOffset(1.4)
101frame1.Draw()
102c.cd(2)
103ROOT.gPad.SetLeftMargin(0.15)
104frame2.GetYaxis().SetTitleOffset(1.4)
105frame2.Draw()
106
107c.SaveAs("rf104_classfactory.png")