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