Logo ROOT  
Reference Guide
rf205_compplot.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## Addition and convolution: options for plotting components of composite p.d.f.s.
6##
7## \macro_code
8##
9## \date February 2018
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14# Set up composite pdf
15# --------------------------------------
16
17# Declare observable x
18x = ROOT.RooRealVar("x", "x", 0, 10)
19
20# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
21# their parameters
22mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
23sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
24sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
25sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
26sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
27
28# Sum the signal components into a composite signal p.d.f.
29sig1frac = ROOT.RooRealVar(
30 "sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.)
31sig = ROOT.RooAddPdf(
32 "sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(sig1frac))
33
34# Build Chebychev polynomial p.d.f.
35a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0., 1.)
36a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0., 1.)
37bkg1 = ROOT.RooChebychev("bkg1", "Background 1",
38 x, ROOT.RooArgList(a0, a1))
39
40# Build expontential pdf
41alpha = ROOT.RooRealVar("alpha", "alpha", -1)
42bkg2 = ROOT.RooExponential("bkg2", "Background 2", x, alpha)
43
44# Sum the background components into a composite background p.d.f.
45bkg1frac = ROOT.RooRealVar(
46 "sig1frac", "fraction of component 1 in background", 0.2, 0., 1.)
47bkg = ROOT.RooAddPdf(
48 "bkg", "Signal", ROOT.RooArgList(bkg1, bkg2), ROOT.RooArgList(sig1frac))
49
50# Sum the composite signal and background
51bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0., 1.)
52model = ROOT.RooAddPdf(
53 "model", "g1+g2+a", ROOT.RooArgList(bkg, sig), ROOT.RooArgList(bkgfrac))
54
55# Set up basic plot with data and full pdf
56# ------------------------------------------------------------------------------
57
58# Generate a data sample of 1000 events in x from model
59data = model.generate(ROOT.RooArgSet(x), 1000)
60
61# Plot data and complete PDF overlaid
62xframe = x.frame(ROOT.RooFit.Title(
63 "Component plotting of pdf=(sig1+sig2)+(bkg1+bkg2)"))
64data.plotOn(xframe)
65model.plotOn(xframe)
66
67# Clone xframe for use below
68xframe2 = xframe.Clone("xframe2")
69
70# Make component by object reference
71# --------------------------------------------------------------------
72
73# Plot single background component specified by object reference
74ras_bkg = ROOT.RooArgSet(bkg)
75model.plotOn(xframe, ROOT.RooFit.Components(
76 ras_bkg), ROOT.RooFit.LineColor(ROOT.kRed))
77
78# Plot single background component specified by object reference
79ras_bkg2 = ROOT.RooArgSet(bkg2)
80model.plotOn(xframe, ROOT.RooFit.Components(ras_bkg2), ROOT.RooFit.LineStyle(
81 ROOT.kDashed), ROOT.RooFit.LineColor(ROOT.kRed))
82
83# Plot multiple background components specified by object reference
84# Note that specified components may occur at any level in object tree
85# (e.g bkg is component of 'model' and 'sig2' is component 'sig')
86ras_bkg_sig2 = ROOT.RooArgSet(bkg, sig2)
87model.plotOn(xframe, ROOT.RooFit.Components(ras_bkg_sig2),
88 ROOT.RooFit.LineStyle(ROOT.kDotted))
89
90# Make component by name/regexp
91# ------------------------------------------------------------
92
93# Plot single background component specified by name
94model.plotOn(xframe2, ROOT.RooFit.Components(
95 "bkg"), ROOT.RooFit.LineColor(ROOT.kCyan))
96
97# Plot multiple background components specified by name
98model.plotOn(
99 xframe2,
100 ROOT.RooFit.Components("bkg1,sig2"),
101 ROOT.RooFit.LineStyle(
102 ROOT.kDotted),
103 ROOT.RooFit.LineColor(
104 ROOT.kCyan))
105
106# Plot multiple background components specified by regular expression on
107# name
108model.plotOn(
109 xframe2,
110 ROOT.RooFit.Components("sig*"),
111 ROOT.RooFit.LineStyle(
112 ROOT.kDashed),
113 ROOT.RooFit.LineColor(
114 ROOT.kCyan))
115
116# Plot multiple background components specified by multiple regular
117# expressions on name
118model.plotOn(
119 xframe2,
120 ROOT.RooFit.Components("bkg1,sig*"),
121 ROOT.RooFit.LineStyle(
122 ROOT.kDashed),
123 ROOT.RooFit.LineColor(
124 ROOT.kYellow),
125 ROOT.RooFit.Invisible())
126
127# Draw the frame on the canvas
128c = ROOT.TCanvas("rf205_compplot", "rf205_compplot", 800, 400)
129c.Divide(2)
130c.cd(1)
131ROOT.gPad.SetLeftMargin(0.15)
132xframe.GetYaxis().SetTitleOffset(1.4)
133xframe.Draw()
134c.cd(2)
135ROOT.gPad.SetLeftMargin(0.15)
136xframe2.GetYaxis().SetTitleOffset(1.4)
137xframe2.Draw()
138
139c.SaveAs("rf205_compplot.png")