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