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