NOTE: This demo uses code that is generated by the macro, which can be compiled on the fly (set to MyPdfV3 below). To use MyPdfV1 or MyPdfV2, adjust lines below accordingly.
import ROOT
ROOT.RooClassFactory.makePdf("MyPdfV1", "x,A,B")
ROOT.RooClassFactory.makePdf(
"MyPdfV2", "x,A,B", "", "A*fabs(x)+pow(x-B,2)")
ROOT.RooClassFactory.makePdf(
"MyPdfV3",
"x,A,B",
"",
"A*fabs(x)+pow(x-B,2)",
ROOT.kTRUE,
ROOT.kFALSE,
"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))")
ROOT.gROOT.ProcessLineSync(".x MyPdfV3.cxx+")
a = ROOT.RooRealVar("a", "a", 1)
b = ROOT.RooRealVar("b", "b", 2, -10, 10)
y = ROOT.RooRealVar("y", "y", -10, 10)
pdf = ROOT.MyPdfV3("pdf", "pdf", y, a, b)
frame1 = y.frame(ROOT.RooFit.Title("Compiled class MyPdfV3"))
data = pdf.generate(ROOT.RooArgSet(y), 1000)
pdf.fitTo(data)
data.plotOn(frame1)
pdf.plotOn(frame1)
x = ROOT.RooRealVar("x", "x", -20, 20)
alpha = ROOT.RooRealVar("alpha", "alpha", 5, 0.1, 10)
genpdf = ROOT.RooClassFactory.makePdfInstance(
"GenPdf",
"(1+0.1*fabs(x)+sin(sqrt(fabs(x*alpha+0.1))))",
ROOT.RooArgList(
x,
alpha))
data2 = genpdf.generate(ROOT.RooArgSet(x), 50000)
genpdf.fitTo(data2)
frame2 = x.frame(ROOT.RooFit.Title("Compiled version of pdf of rf103"))
data2.plotOn(frame2)
genpdf.plotOn(frame2)
c = ROOT.TCanvas("rf104_classfactory", "rf104_classfactory", 800, 400)
c.Divide(2)
c.cd(1)
ROOT.gPad.SetLeftMargin(0.15)
frame1.GetYaxis().SetTitleOffset(1.4)
frame1.Draw()
c.cd(2)
ROOT.gPad.SetLeftMargin(0.15)
frame2.GetYaxis().SetTitleOffset(1.4)
frame2.Draw()
c.SaveAs("rf104_classfactory.png")