Logo ROOT  
Reference Guide
rf201_composite.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Addition and convolution: composite pdf with signal and background component
5##
6## ```
7## pdf = f_bkg * bkg(x,a0,a1) + (1-fbkg) * (f_sig1 * sig1(x,m,s1 + (1-f_sig1) * sig2(x,m,s2)))
8## ```
9##
10## \macro_code
11##
12## \date February 2018
13## \authors Clemens Lange, Wouter Verkerke (C++ version)
14
15import ROOT
16
17# Setup component pdfs
18# ---------------------------------------
19
20# Declare observable x
21x = ROOT.RooRealVar("x", "x", 0, 10)
22
23# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
24# their parameters
25mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
26sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
27sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
28
29sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
30sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
31
32# Build Chebychev polynomial pdf
33a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
34a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
35bkg = ROOT.RooChebychev("bkg", "Background", x, [a0, a1])
36
37
38# Method 1 - Two RooAddPdfs
39# ------------------------------------------
40# Add signal components
41
42# Sum the signal components into a composite signal pdf
43sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
44sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
45
46# Add signal and background
47# ------------------------------------------------
48
49# Sum the composite signal and background
50bkgfrac = ROOT.RooRealVar("bkgfrac", "fraction of background", 0.5, 0.0, 1.0)
51model = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig], [bkgfrac])
52
53# Sample, fit and plot model
54# ---------------------------------------------------
55
56# Generate a data sample of 1000 events in x from model
57data = model.generate({x}, 1000)
58
59# Fit model to data
60model.fitTo(data)
61
62# Plot data and PDF overlaid
63xframe = x.frame(Title="Example of composite pdf=(sig1+sig2)+bkg")
64data.plotOn(xframe)
65model.plotOn(xframe)
66
67# Overlay the background component of model with a dashed line
68model.plotOn(xframe, Components={bkg}, LineStyle="--")
69
70# Overlay the background+sig2 components of model with a dotted line
71model.plotOn(xframe, Components={bkg, sig2}, LineStyle=":")
72
73# Print structure of composite pdf
74model.Print("t")
75
76# Method 2 - One RooAddPdf with recursive fractions
77# ---------------------------------------------------
78
79# Construct sum of models on one go using recursive fraction interpretations
80#
81# model2 = bkg + (sig1 + sig2)
82#
83model2 = ROOT.RooAddPdf("model", "g1+g2+a", [bkg, sig1, sig2], [bkgfrac, sig1frac], True)
84
85# NB: Each coefficient is interpreted as the fraction of the
86# left-hand component of the i-th recursive sum, i.e.
87#
88# sum4 = A + ( B + ( C + D) with fraction fA, and fC expands to
89#
90# sum4 = fA*A + (1-fA)*(fB*B + (1-fB)*(fC*C + (1-fC)*D))
91
92# Plot recursive addition model
93# ---------------------------------------------------------
94model2.plotOn(xframe, LineColor="r", LineStyle="--")
95model2.plotOn(xframe, Components={bkg, sig2}, LineColor="r", LineStyle="--")
96model2.Print("t")
97
98# Draw the frame on the canvas
99c = ROOT.TCanvas("rf201_composite", "rf201_composite", 600, 600)
100ROOT.gPad.SetLeftMargin(0.15)
101xframe.GetYaxis().SetTitleOffset(1.4)
102xframe.Draw()
103
104c.SaveAs("rf201_composite.png")