{
"cells": [
{
"cell_type": "markdown",
"id": "cc6e95a2",
"metadata": {},
"source": [
"# rf608_fitresultaspdf\n",
"Likelihood and minimization: representing the parabolic approximation of the fit as a multi-variate Gaussian on the\n",
"parameters of the fitted pdf\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Wouter Verkerke \n",
"This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:33 PM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28064b85",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooAddPdf.h\"\n",
"#include \"RooChebychev.h\"\n",
"#include \"RooFitResult.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"TFile.h\"\n",
"#include \"TStyle.h\"\n",
"#include \"TH2.h\"\n",
"#include \"TH3.h\"\n",
"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "6ceb6d93",
"metadata": {},
"source": [
"Create model and dataset\n",
"-----------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "09217f09",
"metadata": {},
"source": [
"Observable"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4eb1f537",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -20, 20);"
]
},
{
"cell_type": "markdown",
"id": "e5f6a1d9",
"metadata": {},
"source": [
"Model (intentional strong correlations)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bb6bc40",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar mean(\"mean\", \"mean of g1 and g2\", 0, -1, 1);\n",
"RooRealVar sigma_g1(\"sigma_g1\", \"width of g1\", 2);\n",
"RooGaussian g1(\"g1\", \"g1\", x, mean, sigma_g1);\n",
"\n",
"RooRealVar sigma_g2(\"sigma_g2\", \"width of g2\", 4, 3.0, 5.0);\n",
"RooGaussian g2(\"g2\", \"g2\", x, mean, sigma_g2);\n",
"\n",
"RooRealVar frac(\"frac\", \"frac\", 0.5, 0.0, 1.0);\n",
"RooAddPdf model(\"model\", \"model\", RooArgList(g1, g2), frac);"
]
},
{
"cell_type": "markdown",
"id": "08807965",
"metadata": {},
"source": [
"Generate 1000 events"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd02c679",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"std::unique_ptr data{model.generate(x, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "2d23fd5b",
"metadata": {},
"source": [
"Fit model to data\n",
"----------------------------------"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d576fd69",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"std::unique_ptr r{model.fitTo(*data, Save(), PrintLevel(-1))};"
]
},
{
"cell_type": "markdown",
"id": "cbc7edfc",
"metadata": {},
"source": [
"CreateMV Gaussian pdf of fitted parameters\n",
"------------------------------------------------------------------------------------"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fea77329",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooAbsPdf *parabPdf = r->createHessePdf(RooArgSet(frac, mean, sigma_g2));"
]
},
{
"cell_type": "markdown",
"id": "c8e7f535",
"metadata": {},
"source": [
"Some execercises with the parameter pdf\n",
"-----------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "dbb2e03f",
"metadata": {},
"source": [
"Generate 100K points in the parameter space, sampled from the MVGaussian pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c594795c",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"std::unique_ptr d{parabPdf->generate({mean, sigma_g2, frac}, 100000)};"
]
},
{
"cell_type": "markdown",
"id": "720f2a45",
"metadata": {},
"source": [
"Sample a 3-D histogram of the pdf to be visualized as an error ellipsoid using the GLISO draw option"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9846d4c",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TH3 *hh_3d = (TH3 *)parabPdf->createHistogram(\"mean,sigma_g2,frac\", 25, 25, 25);\n",
"hh_3d->SetFillColor(kBlue);"
]
},
{
"cell_type": "markdown",
"id": "c4dbef66",
"metadata": {},
"source": [
"Project 3D parameter pdf down to 3 permutations of two-dimensional pdfs\n",
"The integrations corresponding to these projections are performed analytically\n",
"by the MV Gaussian pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72b07a79",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooAbsPdf *pdf_sigmag2_frac = parabPdf->createProjection(mean);\n",
"RooAbsPdf *pdf_mean_frac = parabPdf->createProjection(sigma_g2);\n",
"RooAbsPdf *pdf_mean_sigmag2 = parabPdf->createProjection(frac);"
]
},
{
"cell_type": "markdown",
"id": "6a7388e3",
"metadata": {},
"source": [
"Make 2D plots of the 3 two-dimensional pdf projections"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5074d4d",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TH2 *hh_sigmag2_frac = (TH2 *)pdf_sigmag2_frac->createHistogram(\"sigma_g2,frac\", 50, 50);\n",
"TH2 *hh_mean_frac = (TH2 *)pdf_mean_frac->createHistogram(\"mean,frac\", 50, 50);\n",
"TH2 *hh_mean_sigmag2 = (TH2 *)pdf_mean_sigmag2->createHistogram(\"mean,sigma_g2\", 50, 50);\n",
"hh_mean_frac->SetLineColor(kBlue);\n",
"hh_sigmag2_frac->SetLineColor(kBlue);\n",
"hh_mean_sigmag2->SetLineColor(kBlue);"
]
},
{
"cell_type": "markdown",
"id": "4193b385",
"metadata": {},
"source": [
"Draw the 'sigar'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "86316251",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"new TCanvas(\"rf608_fitresultaspdf_1\", \"rf608_fitresultaspdf_1\", 600, 600);\n",
"hh_3d->Draw(\"iso\");"
]
},
{
"cell_type": "markdown",
"id": "c0eb5778",
"metadata": {},
"source": [
"Draw the 2D projections of the 3D pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "01b26950",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TCanvas *c2 = new TCanvas(\"rf608_fitresultaspdf_2\", \"rf608_fitresultaspdf_2\", 900, 600);\n",
"c2->Divide(3, 2);\n",
"c2->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"hh_mean_sigmag2->GetZaxis()->SetTitleOffset(1.4);\n",
"hh_mean_sigmag2->Draw(\"surf3\");\n",
"c2->cd(2);\n",
"gPad->SetLeftMargin(0.15);\n",
"hh_sigmag2_frac->GetZaxis()->SetTitleOffset(1.4);\n",
"hh_sigmag2_frac->Draw(\"surf3\");\n",
"c2->cd(3);\n",
"gPad->SetLeftMargin(0.15);\n",
"hh_mean_frac->GetZaxis()->SetTitleOffset(1.4);\n",
"hh_mean_frac->Draw(\"surf3\");"
]
},
{
"cell_type": "markdown",
"id": "250933be",
"metadata": {},
"source": [
"Draw the distributions of parameter points sampled from the pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a4dbd4e",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TH1 *tmp1 = d->createHistogram(\"mean,sigma_g2\", Binning(50), Binning(50));\n",
"TH1 *tmp2 = d->createHistogram(\"sigma_g2,frac\", Binning(50), Binning(50));\n",
"TH1 *tmp3 = d->createHistogram(\"mean,frac\", Binning(50), Binning(50));\n",
"\n",
"c2->cd(4);\n",
"gPad->SetLeftMargin(0.15);\n",
"tmp1->GetZaxis()->SetTitleOffset(1.4);\n",
"tmp1->Draw(\"lego3\");\n",
"c2->cd(5);\n",
"gPad->SetLeftMargin(0.15);\n",
"tmp2->GetZaxis()->SetTitleOffset(1.4);\n",
"tmp2->Draw(\"lego3\");\n",
"c2->cd(6);\n",
"gPad->SetLeftMargin(0.15);\n",
"tmp3->GetZaxis()->SetTitleOffset(1.4);\n",
"tmp3->Draw(\"lego3\");"
]
},
{
"cell_type": "markdown",
"id": "b7d72d70",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "21bdc6a6",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "ROOT C++",
"language": "c++",
"name": "root"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".C",
"mimetype": " text/x-c++src",
"name": "c++"
}
},
"nbformat": 4,
"nbformat_minor": 5
}