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 
12 import ROOT
13 
14 
15 # Create pdf for sampling
16 # ---------------------------------------------
17 
18 x = ROOT.RooRealVar("x", "x", 0, 20)
19 p = 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
26 x.setBins(20)
27 data1 = p.generate(ROOT.RooArgSet(x), 500)
28 
29 # Create a binned dataset with 20 bins and 500 events
30 hist1 = data1.binnedClone()
31 
32 # Represent data in dh as pdf in x
33 histpdf1 = ROOT.RooHistPdf("histpdf1", "histpdf1", ROOT.RooArgSet(x), hist1, 0)
34 
35 # Plot unbinned data and histogram pdf overlaid
36 frame1 = x.frame(ROOT.RooFit.Title(
37  "Low statistics histogram pdf"), ROOT.RooFit.Bins(100))
38 data1.plotOn(frame1)
39 histpdf1.plotOn(frame1)
40 
41 # Create high stats histogram
42 # -----------------------------------------------------
43 
44 # Sample 100000 events from p
45 x.setBins(10)
46 data2 = p.generate(ROOT.RooArgSet(x), 100000)
47 
48 # Create a binned dataset with 10 bins and 100K events
49 hist2 = data2.binnedClone()
50 
51 # Represent data in dh as pdf in x, 2nd order interpolation
52 histpdf2 = ROOT.RooHistPdf("histpdf2", "histpdf2", ROOT.RooArgSet(x), hist2, 2)
53 
54 # Plot unbinned data and histogram pdf overlaid
55 frame2 = x.frame(ROOT.RooFit.Title(
56  "High stats histogram pdf with interpolation"), ROOT.RooFit.Bins(100))
57 data2.plotOn(frame2)
58 histpdf2.plotOn(frame2)
59 
60 c = ROOT.TCanvas("rf706_histpdf", "rf706_histpdf", 800, 400)
61 c.Divide(2)
62 c.cd(1)
63 ROOT.gPad.SetLeftMargin(0.15)
64 frame1.GetYaxis().SetTitleOffset(1.4)
65 frame1.Draw()
66 c.cd(2)
67 ROOT.gPad.SetLeftMargin(0.15)
68 frame2.GetYaxis().SetTitleOffset(1.8)
69 frame2.Draw()
70 
71 c.SaveAs("rf706_histpdf.png")