{
"cells": [
{
"cell_type": "markdown",
"id": "e4479b7b",
"metadata": {},
"source": [
"# rf607_fitresult\n",
"Likelihood and minimization: demonstration of options of the RooFitResult class\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": "c8a2290f",
"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 \"TMatrixDSym.h\"\n",
"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "65556614",
"metadata": {},
"source": [
"Create pdf, data\n",
"--------------------------------"
]
},
{
"cell_type": "markdown",
"id": "fda197d0",
"metadata": {},
"source": [
"Declare observable x"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "26c72054",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", 0, 10);"
]
},
{
"cell_type": "markdown",
"id": "bdd2c637",
"metadata": {},
"source": [
"Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "97b444a1",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar mean(\"mean\", \"mean of gaussians\", 5, -10, 10);\n",
"RooRealVar sigma1(\"sigma1\", \"width of gaussians\", 0.5, 0.1, 10);\n",
"RooRealVar sigma2(\"sigma2\", \"width of gaussians\", 1, 0.1, 10);\n",
"\n",
"RooGaussian sig1(\"sig1\", \"Signal component 1\", x, mean, sigma1);\n",
"RooGaussian sig2(\"sig2\", \"Signal component 2\", x, mean, sigma2);"
]
},
{
"cell_type": "markdown",
"id": "cb0a2ede",
"metadata": {},
"source": [
"Build Chebychev polynomial pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fc3f2fec",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar a0(\"a0\", \"a0\", 0.5, 0., 1.);\n",
"RooRealVar a1(\"a1\", \"a1\", -0.2);\n",
"RooChebychev bkg(\"bkg\", \"Background\", x, RooArgSet(a0, a1));"
]
},
{
"cell_type": "markdown",
"id": "00f13ff5",
"metadata": {},
"source": [
"Sum the signal components into a composite signal pdf"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b695cc7",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar sig1frac(\"sig1frac\", \"fraction of component 1 in signal\", 0.8, 0., 1.);\n",
"RooAddPdf sig(\"sig\", \"Signal\", RooArgList(sig1, sig2), sig1frac);"
]
},
{
"cell_type": "markdown",
"id": "279e6cb1",
"metadata": {},
"source": [
"Sum the composite signal and background"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c77f11f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooRealVar bkgfrac(\"bkgfrac\", \"fraction of background\", 0.5, 0., 1.);\n",
"RooAddPdf model(\"model\", \"g1+g2+a\", RooArgList(bkg, sig), bkgfrac);"
]
},
{
"cell_type": "markdown",
"id": "17cd3d07",
"metadata": {},
"source": [
"Generate 1000 events"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54f8613b",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"std::unique_ptr data{model.generate(x, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "c0cbd0d7",
"metadata": {},
"source": [
"Fit pdf to data, save fitresult\n",
"-------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "a2a10c23",
"metadata": {},
"source": [
"Perform fit and save result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf1e345c",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"std::unique_ptr r{model.fitTo(*data, Save(), PrintLevel(-1))};"
]
},
{
"cell_type": "markdown",
"id": "546f6b10",
"metadata": {},
"source": [
"Print fit results\n",
"---------------------------------"
]
},
{
"cell_type": "markdown",
"id": "38f16647",
"metadata": {},
"source": [
"Summary printing: Basic info plus final values of floating fit parameters"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13369a02",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r->Print();"
]
},
{
"cell_type": "markdown",
"id": "80d443e0",
"metadata": {},
"source": [
"Verbose printing: Basic info, values of constant parameters, initial and\n",
"final values of floating parameters, global correlations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9aa0c152",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"r->Print(\"v\");"
]
},
{
"cell_type": "markdown",
"id": "cb09d602",
"metadata": {},
"source": [
"Visualize correlation matrix\n",
"-------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "7189f1e0",
"metadata": {},
"source": [
"Construct 2D color plot of correlation matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "54b7ff46",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gStyle->SetOptStat(0);\n",
"TH2 *hcorr = r->correlationHist();"
]
},
{
"cell_type": "markdown",
"id": "93f0b026",
"metadata": {},
"source": [
"Visualize ellipse corresponding to single correlation matrix element"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f050530b",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"RooPlot *frame = new RooPlot(sigma1, sig1frac, 0.45, 0.60, 0.65, 0.90);\n",
"frame->SetTitle(\"Covariance between sigma1 and sig1frac\");\n",
"r->plotOn(frame, sigma1, sig1frac, \"ME12ABHV\");"
]
},
{
"cell_type": "markdown",
"id": "b1cbc3fd",
"metadata": {},
"source": [
"Access fit result information\n",
"---------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "b7b81292",
"metadata": {},
"source": [
"Access basic information"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b60e87fd",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cout << \"EDM = \" << r->edm() << endl;\n",
"cout << \"-log(L) at minimum = \" << r->minNll() << endl;"
]
},
{
"cell_type": "markdown",
"id": "5a672598",
"metadata": {},
"source": [
"Access list of final fit parameter values"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e18be87f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cout << \"final value of floating parameters\" << endl;\n",
"r->floatParsFinal().Print(\"s\");"
]
},
{
"cell_type": "markdown",
"id": "6664fd39",
"metadata": {},
"source": [
"Access correlation matrix elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ac3e9ff",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cout << \"correlation between sig1frac and a0 is \" << r->correlation(sig1frac, a0) << endl;\n",
"cout << \"correlation between bkgfrac and mean is \" << r->correlation(\"bkgfrac\", \"mean\") << endl;"
]
},
{
"cell_type": "markdown",
"id": "5c253db2",
"metadata": {},
"source": [
"Extract covariance and correlation matrix as TMatrixDSym"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b71ed3bb",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"const TMatrixDSym &cor = r->correlationMatrix();\n",
"const TMatrixDSym &cov = r->covarianceMatrix();"
]
},
{
"cell_type": "markdown",
"id": "62de5cd7",
"metadata": {},
"source": [
"Print correlation, covariance matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d373ef30",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cout << \"correlation matrix\" << endl;\n",
"cor.Print();\n",
"cout << \"covariance matrix\" << endl;\n",
"cov.Print();"
]
},
{
"cell_type": "markdown",
"id": "9d52731a",
"metadata": {},
"source": [
"Persist fit result in root file\n",
"-------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "736ba87a",
"metadata": {},
"source": [
"Open new ROOT file save save result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "625d14e3",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TFile f(\"rf607_fitresult.root\", \"RECREATE\");\n",
"r->Write(\"rf607\");\n",
"f.Close();"
]
},
{
"cell_type": "markdown",
"id": "71b36329",
"metadata": {},
"source": [
"In a clean ROOT session retrieve the persisted fit result as follows:\n",
"RooFitResult* r = gDirectory->Get(\"rf607\") ;"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51c705d6",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TCanvas *c = new TCanvas(\"rf607_fitresult\", \"rf607_fitresult\", 800, 400);\n",
"c->Divide(2);\n",
"c->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"hcorr->GetYaxis()->SetTitleOffset(1.4);\n",
"hcorr->Draw(\"colz\");\n",
"c->cd(2);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame->GetYaxis()->SetTitleOffset(1.6);\n",
"frame->Draw();"
]
},
{
"cell_type": "markdown",
"id": "d7107852",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "378c7195",
"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
}