# Rf 6 0 8_Fitresultaspdf¶

Likelihood and minimization: representing the parabolic approximation of the fit as a multi-variate Gaussian on the parameters of the fitted pdf

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:09 AM.

In [1]:
%%cpp -d
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooChebychev.h"
#include "RooFitResult.h"
#include "TCanvas.h"
#include "TAxis.h"
#include "RooPlot.h"
#include "TFile.h"
#include "TStyle.h"
#include "TH2.h"
#include "TH3.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, -1, 1);
RooRealVar sigma_g1("sigma_g1", "width of g1", 2);
RooGaussian g1("g1", "g1", x, mean, sigma_g1);

RooRealVar sigma_g2("sigma_g2", "width of g2", 4, 3.0, 5.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);


## Fit model to data¶

In [6]:
RooFitResult *r = model.fitTo(*data, Save());

input_line_56:2:33: error: reference to 'data' is ambiguous
RooFitResult *r = model.fitTo(*data, Save());
^
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
^


## Createmv gaussian pdf of fitted parameters¶

In [7]:
RooAbsPdf *parabPdf = r->createHessePdf(RooArgSet(frac, mean, sigma_g2));

input_line_58:2:3: error: use of undeclared identifier 'r'
(r->createHessePdf(RooArgSet(((*(class RooRealVar*)0x7f03780cd618)), ((*(class RooRealVar*)0x7f03780cc000)), ((*(class RooRealVar*)0x7f03780cccf8)))))
^
Error in <HandleInterpreterException>: Error evaluating expression (r->createHessePdf(RooArgSet(((*(class RooRealVar*)0x7f03780cd618)), ((*(class RooRealVar*)0x7f03780cc000)), ((*(class RooRealVar*)0x7f03780cccf8)))))
Execution of your code was aborted.


## Some execercises with the parameter pdf¶

Generate 100k points in the parameter space, sampled from the mvgaussian pdf

In [8]:
RooDataSet *d = parabPdf->generate(RooArgSet(mean, sigma_g2, frac), 100000);

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_59:2:18: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
RooDataSet *d = parabPdf->generate(RooArgSet(mean, sigma_g2, frac), 100000);
^~~~~~~~


Sample a 3-d histogram of the pdf to be visualized as an error ellipsoid using the gliso draw option

In [9]:
TH3 *hh_3d = (TH3 *)parabPdf->createHistogram("mean,sigma_g2,frac", 25, 25, 25);
hh_3d->SetFillColor(kBlue);

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_60:2:22: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
TH3 *hh_3d = (TH3 *)parabPdf->createHistogram("mean,sigma_g2,frac", 25, 25, 25);
^~~~~~~~


Project 3d parameter pdf down to 3 permutations of two-dimensional pdfs The integrations corresponding to these projections are performed analytically by the MV Gaussian pdf

In [10]:
RooAbsPdf *pdf_sigmag2_frac = parabPdf->createProjection(mean);
RooAbsPdf *pdf_mean_frac = parabPdf->createProjection(sigma_g2);
RooAbsPdf *pdf_mean_sigmag2 = parabPdf->createProjection(frac);

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_61:2:32: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
RooAbsPdf *pdf_sigmag2_frac = parabPdf->createProjection(mean);
^~~~~~~~


Make 2d plots of the 3 two-dimensional pdf projections

In [11]:
TH2 *hh_sigmag2_frac = (TH2 *)pdf_sigmag2_frac->createHistogram("sigma_g2,frac", 50, 50);
TH2 *hh_mean_frac = (TH2 *)pdf_mean_frac->createHistogram("mean,frac", 50, 50);
TH2 *hh_mean_sigmag2 = (TH2 *)pdf_mean_sigmag2->createHistogram("mean,sigma_g2", 50, 50);
hh_mean_frac->SetLineColor(kBlue);
hh_sigmag2_frac->SetLineColor(kBlue);
hh_mean_sigmag2->SetLineColor(kBlue);

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_62:2:32: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
TH2 *hh_sigmag2_frac = (TH2 *)pdf_sigmag2_frac->createHistogram("sigma_g2,frac", 50, 50);
^~~~~~~~~~~~~~~~


Draw the 'sigar'

In [12]:
new TCanvas("rf608_fitresultaspdf_1", "rf608_fitresultaspdf_1", 600, 600);
hh_3d->Draw("iso");

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_63:3:1: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
hh_3d->Draw("iso");
^~~~~


Draw the 2d projections of the 3d pdf

In [13]:
TCanvas *c2 = new TCanvas("rf608_fitresultaspdf_2", "rf608_fitresultaspdf_2", 900, 600);
c2->Divide(3, 2);
c2->cd(1);
hh_mean_sigmag2->GetZaxis()->SetTitleOffset(1.4);
hh_mean_sigmag2->Draw("surf3");
c2->cd(2);
hh_sigmag2_frac->GetZaxis()->SetTitleOffset(1.4);
hh_sigmag2_frac->Draw("surf3");
c2->cd(3);
hh_mean_frac->GetZaxis()->SetTitleOffset(1.4);
hh_mean_frac->Draw("surf3");

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_65:6:1: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
hh_mean_sigmag2->GetZaxis()->SetTitleOffset(1.4);
^~~~~~~~~~~~~~~


Draw the distributions of parameter points sampled from the pdf

In [14]:
TH1 *tmp1 = d->createHistogram("mean,sigma_g2", 50, 50);
TH1 *tmp2 = d->createHistogram("sigma_g2,frac", 50, 50);
TH1 *tmp3 = d->createHistogram("mean,frac", 50, 50);

c2->cd(4);
tmp1->GetZaxis()->SetTitleOffset(1.4);
tmp1->Draw("lego3");
c2->cd(5);
tmp2->GetZaxis()->SetTitleOffset(1.4);
tmp2->Draw("lego3");
c2->cd(6);
tmp3->GetZaxis()->SetTitleOffset(1.4);
tmp3->Draw("lego3");

Error in <HandleInterpreterException>: Trying to dereference null pointer or trying to call routine taking non-null arguments
Execution of your code was aborted.
input_line_66:2:14: warning: null passed to a callee that requires a non-null argument [-Wnonnull]
TH1 *tmp1 = d->createHistogram("mean,sigma_g2", 50, 50);
^


Draw all canvases

In [15]:
gROOT->GetListOfCanvases()->Draw()