Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf706_histpdf.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Special pdf's: histogram based pdfs and functions
5##
6## \macro_code
7##
8## \date February 2018
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11import ROOT
12
13
14# Create pdf for sampling
15# ---------------------------------------------
16
17x = ROOT.RooRealVar("x", "x", 0, 20)
18p = ROOT.RooPolynomial("p", "p", x, [0.01, -0.01, 0.0004])
19
20# Create low stats histogram
21# ---------------------------------------------------
22
23# Sample 500 events from p
24x.setBins(20)
25data1 = p.generate({x}, 500)
26
27# Create a binned dataset with 20 bins and 500 events
28hist1 = data1.binnedClone()
29
30# Represent data in dh as pdf in x
31histpdf1 = ROOT.RooHistPdf("histpdf1", "histpdf1", {x}, hist1, 0)
32
33# Plot unbinned data and histogram pdf overlaid
34frame1 = x.frame(Title="Low statistics histogram pdf", Bins=100)
35data1.plotOn(frame1)
36histpdf1.plotOn(frame1)
37
38# Create high stats histogram
39# -----------------------------------------------------
40
41# Sample 100000 events from p
42x.setBins(10)
43data2 = p.generate({x}, 100000)
44
45# Create a binned dataset with 10 bins and 100K events
46hist2 = data2.binnedClone()
47
48# Represent data in dh as pdf in x, 2nd order interpolation
49histpdf2 = ROOT.RooHistPdf("histpdf2", "histpdf2", {x}, hist2, 2)
50
51# Plot unbinned data and histogram pdf overlaid
52frame2 = x.frame(Title="High stats histogram pdf with interpolation", Bins=100)
53data2.plotOn(frame2)
54histpdf2.plotOn(frame2)
55
56c = ROOT.TCanvas("rf706_histpdf", "rf706_histpdf", 800, 400)
57c.Divide(2)
58c.cd(1)
59ROOT.gPad.SetLeftMargin(0.15)
60frame1.GetYaxis().SetTitleOffset(1.4)
61frame1.Draw()
62c.cd(2)
63ROOT.gPad.SetLeftMargin(0.15)
64frame2.GetYaxis().SetTitleOffset(1.8)
65frame2.Draw()
66
67c.SaveAs("rf706_histpdf.png")