Logo ROOT  
Reference Guide
rf204_extrangefit.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook -nodraw
4## Addition and convolution: extended maximum likelihood fit with alternate range definition for observed number of events.
5##
6## \macro_code
7##
8## \date February 2018
9## \author Clemens Lange, Wouter Verkerke (C++ version)
10
11import ROOT
12
13# Set up component pdfs
14# ---------------------------------------
15
16# Declare observable x
17x = 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
21mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
22sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
23sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
24
25sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
26sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
27
28# Build Chebychev polynomial p.d.f.
29a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0., 1.)
30a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0., 1.)
31bkg = ROOT.RooChebychev("bkg", "Background", x, ROOT.RooArgList(a0, a1))
32
33# Sum the signal components into a composite signal p.d.f.
34sig1frac = ROOT.RooRealVar(
35 "sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.)
36sig = ROOT.RooAddPdf(
37 "sig", "Signal", ROOT.RooArgList(sig1, sig2), ROOT.RooArgList(sig1frac))
38
39# Construct extended comps with range spec
40# ------------------------------------------------------------------------------
41
42# Define signal range in which events counts are to be defined
43x.setRange("signalRange", 4, 6)
44
45# Associated nsig/nbkg as expected number of events with sig/bkg
46# _in_the_range_ "signalRange"
47nsig = ROOT.RooRealVar(
48 "nsig", "number of signal events in signalRange", 500, 0., 10000)
49nbkg = ROOT.RooRealVar(
50 "nbkg", "number of background events in signalRange", 500, 0, 10000)
51esig = ROOT.RooExtendPdf(
52 "esig", "extended signal p.d.f", sig, nsig, "signalRange")
53ebkg = ROOT.RooExtendPdf(
54 "ebkg", "extended background p.d.f", bkg, nbkg, "signalRange")
55
56# Sum extended components
57# ---------------------------------------------
58
59# Construct sum of two extended p.d.f. (no coefficients required)
60model = ROOT.RooAddPdf("model", "(g1+g2)+a", ROOT.RooArgList(ebkg, esig))
61
62# Sample data, fit model
63# -------------------------------------------
64
65# Generate 1000 events from model so that nsig, come out to numbers <<500
66# in fit
67data = model.generate(ROOT.RooArgSet(x), 1000)
68
69# Perform unbinned extended ML fit to data
70r = model.fitTo(data, ROOT.RooFit.Extended(ROOT.kTRUE), ROOT.RooFit.Save())
71r.Print()