Rf 2 0 2_Extendedmlfit¶

Setting up an extended maximum likelihood fit.

Author: Wouter Verkerke
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Monday, January 17, 2022 at 09:53 AM.

In :
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooExtendPdf.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
In :
%%cpp -d
// This is a workaround to make sure the namespace is used inside functions
using namespace RooFit;

Setup component pdfs¶

Declare observable x

In :
RooRealVar x("x", "x", 0, 10);
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University

Create two gaussian pdfs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters

In :
RooRealVar mean("mean", "mean of gaussians", 5);
RooRealVar sigma1("sigma1", "width of gaussians", 0.5);
RooRealVar sigma2("sigma2", "width of gaussians", 1);

RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1);
RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2);
[#0] WARNING:InputArguments -- The parameter 'sigma1' with range [-1e+30, 1e+30] of the RooGaussian 'sig1' exceeds the safe range of (0, inf). Advise to limit its range.
[#0] WARNING:InputArguments -- The parameter 'sigma2' with range [-1e+30, 1e+30] of the RooGaussian 'sig2' exceeds the safe range of (0, inf). Advise to limit its range.

Build chebychev polynomial pdf

In :
RooRealVar a0("a0", "a0", 0.5, 0., 1.);
RooRealVar a1("a1", "a1", 0.2, 0., 1.);
RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1));

Sum the signal components into a composite signal pdf

In :
RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.);
RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac);

METHOD 1¶

Construct extended composite model¶

Sum the composite signal and background into an extended pdf nsigsig+nbkgbkg

In :
RooRealVar nsig("nsig", "number of signal events", 500, 0., 10000);
RooRealVar nbkg("nbkg", "number of background events", 500, 0, 10000);
RooAddPdf model("model", "(g1+g2)+a", RooArgList(bkg, sig), RooArgList(nbkg, nsig));

Sample, fit and plot extended model¶

Generate a data sample of expected number events in x from model = model.expectedEvents() = nsig+nbkg

In :
RooDataSet *data = model.generate(x);
[#0] WARNING:Eval -- Evaluating RooAddPdf without a defined normalization set. This can lead to ambiguos coefficients definition and incorrect results. Use RooAddPdf::fixCoefNormalization(nset) to provide a normalization set for defining uniquely RooAddPdf coefficients!

Fit model to data, extended ml term automatically included

In :
model.fitTo(*data);
input_line_59:2:15: error: reference to 'data' is ambiguous
model.fitTo(*data);
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
RooDataSet *data = model.generate(x);
^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
data(initializer_list<_Tp> __il) noexcept
^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
data(_Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
data(const _Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
data(_Tp (&__array)[_Nm]) noexcept
^

Plot data and pdf overlaid, use expected number of events for pdf projection normalization rather than observed number of events (==data->numEntries())

In :
RooPlot *xframe = x.frame(Title("extended ML fit example"));
data->plotOn(xframe);
model.plotOn(xframe, Normalization(1.0, RooAbsReal::RelativeExpected));
input_line_60:3:1: error: reference to 'data' is ambiguous
data->plotOn(xframe);
^
input_line_58:2:14: note: candidate found by name lookup is '__cling_N526::data'
RooDataSet *data = model.generate(x);
^
/usr/include/c++/9/bits/range_access.h:318:5: note: candidate found by name lookup is 'std::data'
data(initializer_list<_Tp> __il) noexcept
^
/usr/include/c++/9/bits/range_access.h:289:5: note: candidate found by name lookup is 'std::data'
data(_Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:299:5: note: candidate found by name lookup is 'std::data'
data(const _Container& __cont) noexcept(noexcept(__cont.data()))
^
/usr/include/c++/9/bits/range_access.h:309:5: note: candidate found by name lookup is 'std::data'
data(_Tp (&__array)[_Nm]) noexcept
^

Overlay the background component of model with a dashed line

In :
model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1.0, RooAbsReal::RelativeExpected));
input_line_61:2:50: error: cannot take the address of an rvalue of type 'ELineStyle'
model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1.0, RooAbsReal::RelativeExpected));
^~~~~~~
Error while creating dynamic expression for:
model.plotOn(xframe, Components(bkg), LineStyle(kDashed), Normalization(1., RooAbsReal::RelativeExpected))

Overlay the background+sig2 components of model with a dotted line

In :
model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted),
Normalization(1.0, RooAbsReal::RelativeExpected));
input_line_62:2:67: error: cannot take the address of an rvalue of type 'ELineStyle'
model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted),
^~~~~~~
Error while creating dynamic expression for:
model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted), Normalization(1., RooAbsReal::RelativeExpected))

Print structure of composite pdf

In :
model.Print("t");
0x7f01b40067b0/V- RooChebychev::bkg = 0.8 [Auto,Dirty]
0x7f01cf978028/V- RooRealVar::x = 5
0x7f01b4006000/V- RooRealVar::a0 = 0.5
0x7f01b40063d8/V- RooRealVar::a1 = 0.2
0x7f01b40013d8/V- RooRealVar::nbkg = 500
0x7f01b4009b88/V- RooGaussian::sig1 = 1 [Auto,Dirty]
0x7f01cf978028/V- RooRealVar::x = 5
0x7f01b4009000/V- RooRealVar::mean = 5
0x7f01b40093d8/V- RooRealVar::sigma1 = 0.5
0x7f01cf978400/V- RooRealVar::sig1frac = 0.8
0x7f01b400a0d0/V- RooGaussian::sig2 = 1 [Auto,Dirty]
0x7f01cf978028/V- RooRealVar::x = 5
0x7f01b4009000/V- RooRealVar::mean = 5
0x7f01b40097b0/V- RooRealVar::sigma2 = 1
0x7f01b4001000/V- RooRealVar::nsig = 500

METHOD 2¶

Construct extended components first¶

Associated nsig/nbkg as expected number of events with sig/bkg

In :
RooExtendPdf esig("esig", "extended signal pdf", sig, nsig);
RooExtendPdf ebkg("ebkg", "extended background pdf", bkg, nbkg);

Sum extended components without coefs¶

Construct sum of two extended pdf (no coefficients required)

In :

Draw the frame on the canvas

In :
new TCanvas("rf202_composite", "rf202_composite", 600, 600);