Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf202_extendedmlfit.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit_main
3## \notebook
4## Addition and convolution: setting up an extended maximum likelihood fit
5##
6## \macro_image
7## \macro_code
8## \macro_output
9##
10## \date February 2018
11## \authors Clemens Lange, Wouter Verkerke (C++ version)
12
13import ROOT
14
15# Set up component pdfs
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)
26
27sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
28sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
29
30# Build Chebychev polynomial pdf
31a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
32a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
33bkg = ROOT.RooChebychev("bkg", "Background", x, [a0, a1])
34
35# Sum the signal components into a composite signal pdf
36sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
37sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
38
39# Method 1 - Construct extended composite model
40# -------------------------------------------------------------------
41
42# Sum the composite signal and background into an extended pdf
43# nsig*sig+nbkg*bkg
44nsig = ROOT.RooRealVar("nsig", "number of signal events", 500, 0.0, 10000)
45nbkg = ROOT.RooRealVar("nbkg", "number of background events", 500, 0, 10000)
46model = ROOT.RooAddPdf("model", "(g1+g2)+a", [bkg, sig], [nbkg, nsig])
47
48# Sample, fit and plot extended model
49# ---------------------------------------------------------------------
50
51# Generate a data sample of expected number events in x from model
52# = model.expectedEvents() = nsig+nbkg
53data = model.generate(x)
54
55# Fit model to data, ML term automatically included
56model.fitTo(data, PrintLevel=-1)
57
58# Plot data and PDF overlaid
59xframe = x.frame(Title="extended ML fit example")
60data.plotOn(xframe)
61model.plotOn(xframe)
62
63# Overlay the background component of model with a dashed line
64model.plotOn(xframe, Components=[bkg], LineStyle=":")
65
66# Overlay the background+sig2 components of model with a dotted line
67model.plotOn(xframe, Components=[bkg, sig2], LineStyle=":")
68
69# Print structure of composite pdf
70model.Print("t")
71
72
73# Method 2 - Construct extended components first
74# ---------------------------------------------------------------------
75
76# Associated nsig/nbkg as expected number of events with sig/bkg
77esig = ROOT.RooExtendPdf("esig", "extended signal pdf", sig, nsig)
78ebkg = ROOT.RooExtendPdf("ebkg", "extended background pdf", bkg, nbkg)
79
80# Sum extended components without coefs
81# -------------------------------------------------------------------------
82
83# Construct sum of two extended pdf (no coefficients required)
84model2 = ROOT.RooAddPdf("model2", "(g1+g2)+a", [ebkg, esig])
85
86# Draw the frame on the canvas
87c = ROOT.TCanvas("rf202_extendedmlfit", "rf202_extendedmlfit", 600, 600)
89xframe.GetYaxis().SetTitleOffset(1.4)
91
92c.SaveAs("rf202_extendedmlfit.png")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.