Logo ROOT  
Reference Guide
rf311_rangeplot.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Multidimensional models: projecting pdf and data ranges in continuous observables
5 ##
6 ## \macro_code
7 ##
8 ## \date February 2018
9 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
10 
11 import ROOT
12 
13 # Create 3D pdf and data
14 # -------------------------------------------
15 
16 # Create observables
17 x = ROOT.RooRealVar("x", "x", -5, 5)
18 y = ROOT.RooRealVar("y", "y", -5, 5)
19 z = ROOT.RooRealVar("z", "z", -5, 5)
20 
21 # Create signal pdf gauss(x)*gauss(y)*gauss(z)
22 gx = ROOT.RooGaussian(
23  "gx", "gx", x, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(1))
24 gy = ROOT.RooGaussian(
25  "gy", "gy", y, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(1))
26 gz = ROOT.RooGaussian(
27  "gz", "gz", z, ROOT.RooFit.RooConst(0), ROOT.RooFit.RooConst(1))
28 sig = ROOT.RooProdPdf("sig", "sig", ROOT.RooArgList(gx, gy, gz))
29 
30 # Create background pdf poly(x)*poly(y)*poly(z)
31 px = ROOT.RooPolynomial("px", "px", x, ROOT.RooArgList(
32  ROOT.RooFit.RooConst(-0.1), ROOT.RooFit.RooConst(0.004)))
33 py = ROOT.RooPolynomial("py", "py", y, ROOT.RooArgList(
34  ROOT.RooFit.RooConst(0.1), ROOT.RooFit.RooConst(-0.004)))
35 pz = ROOT.RooPolynomial("pz", "pz", z)
36 bkg = ROOT.RooProdPdf("bkg", "bkg", ROOT.RooArgList(px, py, pz))
37 
38 # Create composite pdf sig+bkg
39 fsig = ROOT.RooRealVar("fsig", "signal fraction", 0.1, 0., 1.)
40 model = ROOT.RooAddPdf(
41  "model", "model", ROOT.RooArgList(
42  sig, bkg), ROOT.RooArgList(fsig))
43 
44 data = model.generate(ROOT.RooArgSet(x, y, z), 20000)
45 
46 # Project pdf and data on x
47 # -------------------------------------------------
48 
49 # Make plain projection of data and pdf on x observable
50 frame = x.frame(ROOT.RooFit.Title(
51  "Projection of 3D data and pdf on X"), ROOT.RooFit.Bins(40))
52 data.plotOn(frame)
53 model.plotOn(frame)
54 
55 # Project pdf and data on x in signal range
56 # ----------------------------------------------------------------------------------
57 
58 # Define signal region in y and z observables
59 y.setRange("sigRegion", -1, 1)
60 z.setRange("sigRegion", -1, 1)
61 
62 # Make plot frame
63 frame2 = x.frame(ROOT.RooFit.Title(
64  "Same projection on X in signal range of (Y,Z)"), ROOT.RooFit.Bins(40))
65 
66 # Plot subset of data in which all observables are inside "sigRegion"
67 # For observables that do not have an explicit "sigRegion" range defined (e.g. observable)
68 # an implicit definition is used that is identical to the full range (i.e.
69 # [-5,5] for x)
70 data.plotOn(frame2, ROOT.RooFit.CutRange("sigRegion"))
71 
72 # Project model on x, projected observables (y,z) only in "sigRegion"
73 model.plotOn(frame2, ROOT.RooFit.ProjectionRange("sigRegion"))
74 
75 c = ROOT.TCanvas("rf311_rangeplot", "rf310_rangeplot", 800, 400)
76 c.Divide(2)
77 c.cd(1)
78 ROOT.gPad.SetLeftMargin(0.15)
79 frame.GetYaxis().SetTitleOffset(1.4)
80 frame.Draw()
81 c.cd(2)
82 ROOT.gPad.SetLeftMargin(0.15)
83 frame2.GetYaxis().SetTitleOffset(1.4)
84 frame2.Draw()
85 
86 c.SaveAs("rf311_rangeplot.png")