# Rf 6 0 5_Profilell¶

Likelihood and minimization: working with the profile likelihood estimator

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 10:08 AM.

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

## Create model and dataset¶

Observable

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

Model (intentional strong correlations)

In [4]:
RooRealVar mean("mean", "mean of g1 and g2", 0, -10, 10);
RooRealVar sigma_g1("sigma_g1", "width of g1", 3);
RooGaussian g1("g1", "g1", x, mean, sigma_g1);

RooRealVar sigma_g2("sigma_g2", "width of g2", 4, 3.0, 6.0);
RooGaussian g2("g2", "g2", x, mean, sigma_g2);

RooRealVar frac("frac", "frac", 0.5, 0.0, 1.0);
RooAddPdf model("model", "model", RooArgList(g1, g2), frac);
[#0] WARNING:InputArguments -- The parameter 'sigma_g1' with range [-1e+30, 1e+30] of the RooGaussian 'g1' exceeds the safe range of (0, inf). Advise to limit its range.

Generate 1000 events

In [5]:
RooDataSet *data = model.generate(x, 1000);

## Construct plain likelihood¶

Construct unbinned likelihood

In [6]:
RooAbsReal *nll = model.createNLL(*data, NumCPU(2));
input_line_56:2:37: error: reference to 'data' is ambiguous
RooAbsReal *nll = model.createNLL(*data, NumCPU(2));
^
input_line_55:2:14: note: candidate found by name lookup is '__cling_N523::data'
RooDataSet *data = model.generate(x, 1000);
^
/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
^

Minimize likelihood w.r.t all parameters before making plots

In [7]:
input_line_58:2:18: error: use of undeclared identifier 'nll'
^
Error in <HandleInterpreterException>: Error evaluating expression (RooMinimizer(* nll).migrad())
Execution of your code was aborted.

Plot likelihood scan frac

In [8]:
RooPlot *frame1 = frac.frame(Bins(10), Range(0.01, 0.95), Title("LL and profileLL in frac"));
nll->plotOn(frame1, ShiftToZero());
input_line_59:3:21: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)()'
nll->plotOn(frame1, ShiftToZero());
^~~~~~~~~~~

Plot likelihood scan in sigma_g2

In [9]:
RooPlot *frame2 = sigma_g2.frame(Bins(10), Range(3.3, 5.0), Title("LL and profileLL in sigma_g2"));
nll->plotOn(frame2, ShiftToZero());
input_line_60:3:21: error: cannot initialize an array element of type 'void *' with an rvalue of type 'RooCmdArg (*)()'
nll->plotOn(frame2, ShiftToZero());
^~~~~~~~~~~

## Construct profile likelihood in frac¶

The profile likelihood estimator on nll for frac will minimize nll w.r.t all floating parameters except frac for each evaluation

In [10]:
RooAbsReal *pll_frac = nll->createProfile(frac);
input_line_62:2:3: error: use of undeclared identifier 'nll'
(nll->createProfile(((*(class RooRealVar*)0x7fc3c404e618))))
^
Error in <HandleInterpreterException>: Error evaluating expression (nll->createProfile(((*(class RooRealVar*)0x7fc3c404e618))))
Execution of your code was aborted.

Plot the profile likelihood in frac

In [11]:
pll_frac->plotOn(frame1, LineColor(kRed));
input_line_63:2:37: error: cannot take the address of an rvalue of type 'EColor'
pll_frac->plotOn(frame1, LineColor(kRed));
^~~~
Error while creating dynamic expression for:
pll_frac->plotOn(frame1, LineColor(kRed))

Adjust frame maximum for visual clarity

In [12]:
frame1->SetMinimum(0);
frame1->SetMaximum(3);
input_line_65:2:3: error: use of undeclared identifier 'frame1'
(frame1->SetMinimum(0))
^
Error in <HandleInterpreterException>: Error evaluating expression (frame1->SetMinimum(0))
Execution of your code was aborted.

## Construct profile likelihood in sigma_g2¶

The profile likelihood estimator on nll for sigma_g2 will minimize nll w.r.t all floating parameters except sigma_g2 for each evaluation

In [13]:
RooAbsReal *pll_sigmag2 = nll->createProfile(sigma_g2);
input_line_67:2:3: error: use of undeclared identifier 'nll'
(nll->createProfile(((*(class RooRealVar*)0x7fc3c404dcf8))))
^
Error in <HandleInterpreterException>: Error evaluating expression (nll->createProfile(((*(class RooRealVar*)0x7fc3c404dcf8))))
Execution of your code was aborted.

Plot the profile likelihood in sigma_g2

In [14]:
pll_sigmag2->plotOn(frame2, LineColor(kRed));
input_line_68:2:40: error: cannot take the address of an rvalue of type 'EColor'
pll_sigmag2->plotOn(frame2, LineColor(kRed));
^~~~
Error while creating dynamic expression for:
pll_sigmag2->plotOn(frame2, LineColor(kRed))

Adjust frame maximum for visual clarity

In [15]:
frame2->SetMinimum(0);
frame2->SetMaximum(3);
input_line_70:2:3: error: use of undeclared identifier 'frame2'
(frame2->SetMinimum(0))
^
Error in <HandleInterpreterException>: Error evaluating expression (frame2->SetMinimum(0))
Execution of your code was aborted.

Make canvas and draw rooplots

In [16]:
TCanvas *c = new TCanvas("rf605_profilell", "rf605_profilell", 800, 400);
c->Divide(2);
c->cd(1);
frame1->GetYaxis()->SetTitleOffset(1.4);
frame1->Draw();
c->cd(2);
frame2->GetYaxis()->SetTitleOffset(1.4);
frame2->Draw();

delete pll_frac;
delete pll_sigmag2;
delete nll;
input_line_72:2:3: error: use of undeclared identifier 'frame1'
(frame1->GetYaxis()->SetTitleOffset(1.3999999999999999))
^
Error in <HandleInterpreterException>: Error evaluating expression (frame1->GetYaxis()->SetTitleOffset(1.3999999999999999))
Execution of your code was aborted.

Draw all canvases

In [17]:
%jsroot on
gROOT->GetListOfCanvases()->Draw()