Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
5## for observed number of events.
6##
7## \macro_code
8##
9## \date February 2018
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
13
14# Set up component pdfs
15# ---------------------------------------
16
17# Declare observable x
18x = ROOT.RooRealVar("x", "x", 0, 10)
19
20# Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and
21# their parameters
22mean = ROOT.RooRealVar("mean", "mean of gaussians", 5)
23sigma1 = ROOT.RooRealVar("sigma1", "width of gaussians", 0.5)
24sigma2 = ROOT.RooRealVar("sigma2", "width of gaussians", 1)
25
26sig1 = ROOT.RooGaussian("sig1", "Signal component 1", x, mean, sigma1)
27sig2 = ROOT.RooGaussian("sig2", "Signal component 2", x, mean, sigma2)
28
29# Build Chebychev polynomial pdf
30a0 = ROOT.RooRealVar("a0", "a0", 0.5, 0.0, 1.0)
31a1 = ROOT.RooRealVar("a1", "a1", -0.2, 0.0, 1.0)
32bkg = ROOT.RooChebychev("bkg", "Background", x, [a0, a1])
33
34# Sum the signal components into a composite signal pdf
35sig1frac = ROOT.RooRealVar("sig1frac", "fraction of component 1 in signal", 0.8, 0.0, 1.0)
36sig = ROOT.RooAddPdf("sig", "Signal", [sig1, sig2], [sig1frac])
37
38# Construct extended comps with range spec
39# ------------------------------------------------------------------------------
40
41# Define signal range in which events counts are to be defined
42x.setRange("signalRange", 4, 6)
43
44# Associated nsig/nbkg as expected number of events with sig/bkg
45# _in_the_range_ "signalRange"
46nsig = ROOT.RooRealVar("nsig", "number of signal events in signalRange", 500, 0.0, 10000)
47nbkg = ROOT.RooRealVar("nbkg", "number of background events in signalRange", 500, 0, 10000)
48esig = ROOT.RooExtendPdf("esig", "extended signal pdf", sig, nsig, "signalRange")
49ebkg = ROOT.RooExtendPdf("ebkg", "extended background pdf", bkg, nbkg, "signalRange")
50
51# Sum extended components
52# ---------------------------------------------
53
54# Construct sum of two extended pdf (no coefficients required)
55model = ROOT.RooAddPdf("model", "(g1+g2)+a", [ebkg, esig])
56
57# Sample data, fit model
58# -------------------------------------------
59
60# Generate 1000 events from model so that nsig, come out to numbers <<500
61# in fit
62data = model.generate({x}, 1000)
63
64# Perform unbinned extended ML fit to data
65r = model.fitTo(data, Extended=True, Save=True)
66r.Print()