Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
rf202_extendedmlfit.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Addition and convolution: setting up an extended maximum likelihood fit
5 ##
6 ## \macro_code
7 ##
8 ## \date February 2018
9 ## \author Clemens Lange, Wouter Verkerke (C++ version)
10 
11 import ROOT
12 
13 # Set up component pdfs
14 # ---------------------------------------
15 
16 # Declare observable x
17 x = ROOT.RooRealVar("x", "x", 0, 10)
18 
19 # Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
20 # their parameters
21 mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
22 sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
23 sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
24 
25 sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
26 sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
27 
28 # Build Chebychev polynomial p.d.f.
29 a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0., 1.)
30 a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0., 1.)
31 bkg = ROOT.RooChebychev("bkg", "Background", x, ROOT.RooArgList(a0, a1))
32 
33 # Sum the signal components into a composite signal p.d.f.
34 sig1frac = ROOT.RooRealVar(
35  "sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.)
36 sig = ROOT.RooAddPdf(
37  "sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(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
44 nsig = ROOT.RooRealVar("nsig", "number of signal events", 500, 0., 10000)
45 nbkg = ROOT.RooRealVar(
46  "nbkg", "number of background events", 500, 0, 10000)
47 model = ROOT.RooAddPdf(
48  "model",
49  "(g1+g2)+a",
50  ROOT.RooArgList(
51  bkg,
52  sig),
53  ROOT.RooArgList(
54  nbkg,
55  nsig))
56 
57 # Sample, fit and plot extended model
58 # ---------------------------------------------------------------------
59 
60 # Generate a data sample of expected number events in x from model
61 # = model.expectedEvents() = nsig+nbkg
62 data = model.generate(ROOT.RooArgSet(x))
63 
64 # Fit model to data, ML term automatically included
65 model.fitTo(data)
66 
67 # Plot data and PDF overlaid, expected number of events for p.d.f projection normalization
68 # rather than observed number of events (==data.numEntries())
69 xframe = x.frame(ROOT.RooFit.Title("extended ML fit example"))
70 data.plotOn(xframe)
71 model.plotOn(xframe, ROOT.RooFit.Normalization(
72  1.0, ROOT.RooAbsReal.RelativeExpected))
73 
74 # Overlay the background component of model with a dashed line
75 ras_bkg = ROOT.RooArgSet(bkg)
76 model.plotOn(
77  xframe, ROOT.RooFit.Components(ras_bkg), ROOT.RooFit.LineStyle(
78  ROOT.kDashed), ROOT.RooFit.Normalization(
79  1.0, ROOT.RooAbsReal.RelativeExpected))
80 
81 # Overlay the background+sig2 components of model with a dotted line
82 ras_bkg_sig2 = ROOT.RooArgSet(bkg, sig2)
83 model.plotOn(
84  xframe, ROOT.RooFit.Components(ras_bkg_sig2), ROOT.RooFit.LineStyle(
85  ROOT.kDotted), ROOT.RooFit.Normalization(
86  1.0, ROOT.RooAbsReal.RelativeExpected))
87 
88 # Print structure of composite p.d.f.
89 model.Print("t")
90 
91 
92 # Method 2 - Construct extended components first
93 # ---------------------------------------------------------------------
94 
95 # Associated nsig/nbkg as expected number of events with sig/bkg
96 esig = ROOT.RooExtendPdf("esig", "extended signal p.d.f", sig, nsig)
97 ebkg = ROOT.RooExtendPdf("ebkg", "extended background p.d.f", bkg, nbkg)
98 
99 # Sum extended components without coefs
100 # -------------------------------------------------------------------------
101 
102 # Construct sum of two extended p.d.f. (no coefficients required)
103 model2 = ROOT.RooAddPdf("model2", "(g1+g2)+a", ROOT.RooArgList(ebkg, esig))
104 
105 # Draw the frame on the canvas
106 c = ROOT.TCanvas("rf202_extendedmlfit", "rf202_extendedmlfit", 600, 600)
107 ROOT.gPad.SetLeftMargin(0.15)
108 xframe.GetYaxis().SetTitleOffset(1.4)
109 xframe.Draw()
110 
111 c.SaveAs("rf202_extendedmlfit.png")