{
"cells": [
{
"cell_type": "markdown",
"id": "c35411ad",
"metadata": {},
"source": [
"# rf202_extendedmlfit\n",
"Setting up an extended maximum likelihood fit.\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:29 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cd0cb3aa",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:38.118134Z",
"iopub.status.busy": "2026-05-19T20:29:38.118023Z",
"iopub.status.idle": "2026-05-19T20:29:38.130700Z",
"shell.execute_reply": "2026-05-19T20:29:38.130213Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooChebychev.h\"\n",
"#include \"RooAddPdf.h\"\n",
"#include \"RooExtendPdf.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "6d764304",
"metadata": {},
"source": [
"Setup component pdfs\n",
"---------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "f1a992e3",
"metadata": {},
"source": [
"Declare observable x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "07a82e7a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:38.132265Z",
"iopub.status.busy": "2026-05-19T20:29:38.132153Z",
"iopub.status.idle": "2026-05-19T20:29:38.445509Z",
"shell.execute_reply": "2026-05-19T20:29:38.444862Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", 0, 10);"
]
},
{
"cell_type": "markdown",
"id": "db326a94",
"metadata": {},
"source": [
"Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1a5e9cd3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:38.447669Z",
"iopub.status.busy": "2026-05-19T20:29:38.447530Z",
"iopub.status.idle": "2026-05-19T20:29:38.655796Z",
"shell.execute_reply": "2026-05-19T20:29:38.655231Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#0] WARNING:InputArguments -- The parameter 'sigma1' with range [-inf, inf] of the RooGaussian 'sig1' exceeds the safe range of (0, inf). Advise to limit its range.\n",
"[#0] WARNING:InputArguments -- The parameter 'sigma2' with range [-inf, inf] of the RooGaussian 'sig2' exceeds the safe range of (0, inf). Advise to limit its range.\n"
]
}
],
"source": [
"RooRealVar mean(\"mean\", \"mean of gaussians\", 5);\n",
"RooRealVar sigma1(\"sigma1\", \"width of gaussians\", 0.5);\n",
"RooRealVar sigma2(\"sigma2\", \"width of gaussians\", 1);\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": "f398d34a",
"metadata": {},
"source": [
"Build Chebychev polynomial pdf"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "80febff6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:38.657383Z",
"iopub.status.busy": "2026-05-19T20:29:38.657268Z",
"iopub.status.idle": "2026-05-19T20:29:38.887114Z",
"shell.execute_reply": "2026-05-19T20:29:38.886495Z"
}
},
"outputs": [],
"source": [
"RooRealVar a0(\"a0\", \"a0\", 0.5, 0., 1.);\n",
"RooRealVar a1(\"a1\", \"a1\", 0.2, 0., 1.);\n",
"RooChebychev bkg(\"bkg\", \"Background\", x, RooArgSet(a0, a1));"
]
},
{
"cell_type": "markdown",
"id": "c01be2b4",
"metadata": {},
"source": [
"Sum the signal components into a composite signal pdf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6bbaa088",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:38.890315Z",
"iopub.status.busy": "2026-05-19T20:29:38.890187Z",
"iopub.status.idle": "2026-05-19T20:29:39.097577Z",
"shell.execute_reply": "2026-05-19T20:29:39.096976Z"
}
},
"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": "d5fea3e8",
"metadata": {},
"source": [
"----------------\n",
"METHOD 1\n",
"================"
]
},
{
"cell_type": "markdown",
"id": "6f596051",
"metadata": {},
"source": [
"Construct extended composite model\n",
"-------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "50ed3e99",
"metadata": {},
"source": [
"Sum the composite signal and background into an extended pdf nsig*sig+nbkg*bkg"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "12564600",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:39.099563Z",
"iopub.status.busy": "2026-05-19T20:29:39.099445Z",
"iopub.status.idle": "2026-05-19T20:29:39.307311Z",
"shell.execute_reply": "2026-05-19T20:29:39.306732Z"
}
},
"outputs": [],
"source": [
"RooRealVar nsig(\"nsig\", \"number of signal events\", 500, 0., 10000);\n",
"RooRealVar nbkg(\"nbkg\", \"number of background events\", 500, 0, 10000);\n",
"RooAddPdf model(\"model\", \"(g1+g2)+a\", RooArgList(bkg, sig), RooArgList(nbkg, nsig));"
]
},
{
"cell_type": "markdown",
"id": "0ef5eabf",
"metadata": {},
"source": [
"Sample, fit and plot extended model\n",
"---------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "5b03ee06",
"metadata": {},
"source": [
"Generate a data sample of expected number events in x from model\n",
"= model.expectedEvents() = nsig+nbkg"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "5d252020",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:39.310335Z",
"iopub.status.busy": "2026-05-19T20:29:39.310212Z",
"iopub.status.idle": "2026-05-19T20:29:39.517684Z",
"shell.execute_reply": "2026-05-19T20:29:39.517374Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_54:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n",
" std::unique_ptr data{model.generate(x)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{model.generate(x)};"
]
},
{
"cell_type": "markdown",
"id": "c124cb5a",
"metadata": {},
"source": [
"Fit model to data, extended ML term automatically included"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6e4854e0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:39.519934Z",
"iopub.status.busy": "2026-05-19T20:29:39.519818Z",
"iopub.status.idle": "2026-05-19T20:29:39.728160Z",
"shell.execute_reply": "2026-05-19T20:29:39.727655Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_55:2:15: error: reference to 'data' is ambiguous\n",
" model.fitTo(*data, PrintLevel(-1));\n",
" ^\n",
"input_line_54:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{model.generate(x)};\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n"
]
}
],
"source": [
"model.fitTo(*data, PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "a3a5f0a0",
"metadata": {},
"source": [
"Plot data and PDF overlaid"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1d790b5e",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:39.729948Z",
"iopub.status.busy": "2026-05-19T20:29:39.729830Z",
"iopub.status.idle": "2026-05-19T20:29:39.946287Z",
"shell.execute_reply": "2026-05-19T20:29:39.945493Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_56:3:1: error: reference to 'data' is ambiguous\n",
"data->plotOn(xframe);\n",
"^\n",
"input_line_54:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{model.generate(x)};\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:344:5: note: candidate found by name lookup is 'std::data'\n",
" data(initializer_list<_Tp> __il) noexcept\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:312:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:323:5: note: candidate found by name lookup is 'std::data'\n",
" data(const _Container& __cont) noexcept(noexcept(__cont.data()))\n",
" ^\n",
"/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/bits/range_access.h:334:5: note: candidate found by name lookup is 'std::data'\n",
" data(_Tp (&__array)[_Nm]) noexcept\n",
" ^\n"
]
}
],
"source": [
"RooPlot *xframe = x.frame(Title(\"extended ML fit example\"));\n",
"data->plotOn(xframe);\n",
"model.plotOn(xframe);"
]
},
{
"cell_type": "markdown",
"id": "c8dfe524",
"metadata": {},
"source": [
"Overlay the background component of model with a dashed line"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "043fd455",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:39.947873Z",
"iopub.status.busy": "2026-05-19T20:29:39.947751Z",
"iopub.status.idle": "2026-05-19T20:29:40.155561Z",
"shell.execute_reply": "2026-05-19T20:29:40.154977Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_57:2:50: error: cannot take the address of an rvalue of type 'ELineStyle'\n",
" model.plotOn(xframe, Components(bkg), LineStyle(kDashed));\n",
" ^~~~~~~\n",
"Error while creating dynamic expression for:\n",
" model.plotOn(xframe, Components(bkg), LineStyle(kDashed))\n"
]
}
],
"source": [
"model.plotOn(xframe, Components(bkg), LineStyle(kDashed));"
]
},
{
"cell_type": "markdown",
"id": "2e1c3006",
"metadata": {},
"source": [
"Overlay the background+sig2 components of model with a dotted line"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "a883283f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:40.157165Z",
"iopub.status.busy": "2026-05-19T20:29:40.157045Z",
"iopub.status.idle": "2026-05-19T20:29:40.364999Z",
"shell.execute_reply": "2026-05-19T20:29:40.364350Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:2:67: error: cannot take the address of an rvalue of type 'ELineStyle'\n",
" model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted));\n",
" ^~~~~~~\n",
"Error while creating dynamic expression for:\n",
" model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted))\n"
]
}
],
"source": [
"model.plotOn(xframe, Components(RooArgSet(bkg, sig2)), LineStyle(kDotted));"
]
},
{
"cell_type": "markdown",
"id": "d66b1f5e",
"metadata": {},
"source": [
"Print structure of composite pdf"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "c8e8f6ea",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:40.366544Z",
"iopub.status.busy": "2026-05-19T20:29:40.366424Z",
"iopub.status.idle": "2026-05-19T20:29:40.573451Z",
"shell.execute_reply": "2026-05-19T20:29:40.572949Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0x7f4b11e037d0 RooAddPdf::model = 0.9/1 [Auto,Clean] \n",
" 0x7f4b11e0e7d0/V- RooChebychev::bkg = 0.8 [Auto,Dirty] \n",
" 0x7f4b11ede000/V- RooRealVar::x = 5\n",
" 0x7f4b11e0e000/V- RooRealVar::a0 = 0.5\n",
" 0x7f4b11e0e3e8/V- RooRealVar::a1 = 0.2\n",
" 0x7f4b11e033e8/V- RooRealVar::nbkg = 500\n",
" 0x7f4b11e063e8/V- RooAddPdf::sig = 1/1 [Auto,Clean] \n",
" 0x7f4b11e11bb8/V- RooGaussian::sig1 = 1 [Auto,Dirty] \n",
" 0x7f4b11ede000/V- RooRealVar::x = 5\n",
" 0x7f4b11e11000/V- RooRealVar::mean = 5\n",
" 0x7f4b11e113e8/V- RooRealVar::sigma1 = 0.5\n",
" 0x7f4b11e06000/V- RooRealVar::sig1frac = 0.8\n",
" 0x7f4b11e12110/V- RooGaussian::sig2 = 1 [Auto,Dirty] \n",
" 0x7f4b11ede000/V- RooRealVar::x = 5\n",
" 0x7f4b11e11000/V- RooRealVar::mean = 5\n",
" 0x7f4b11e117d0/V- RooRealVar::sigma2 = 1\n",
" 0x7f4b11e03000/V- RooRealVar::nsig = 500\n"
]
}
],
"source": [
"model.Print(\"t\");"
]
},
{
"cell_type": "markdown",
"id": "dd9edbe7",
"metadata": {},
"source": [
"----------------\n",
"METHOD 2\n",
"================"
]
},
{
"cell_type": "markdown",
"id": "ca406a7e",
"metadata": {},
"source": [
"Construct extended components first\n",
"---------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "c818ead6",
"metadata": {},
"source": [
"Associated nsig/nbkg as expected number of events with sig/bkg"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "36140fe5",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:40.575426Z",
"iopub.status.busy": "2026-05-19T20:29:40.575302Z",
"iopub.status.idle": "2026-05-19T20:29:40.789552Z",
"shell.execute_reply": "2026-05-19T20:29:40.788713Z"
}
},
"outputs": [],
"source": [
"RooExtendPdf esig(\"esig\", \"extended signal pdf\", sig, nsig);\n",
"RooExtendPdf ebkg(\"ebkg\", \"extended background pdf\", bkg, nbkg);"
]
},
{
"cell_type": "markdown",
"id": "2285d045",
"metadata": {},
"source": [
"Sum extended components without coefs\n",
"-------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "cf655d6b",
"metadata": {},
"source": [
"Construct sum of two extended pdf (no coefficients required)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "d22b1c69",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:40.798116Z",
"iopub.status.busy": "2026-05-19T20:29:40.797981Z",
"iopub.status.idle": "2026-05-19T20:29:41.008635Z",
"shell.execute_reply": "2026-05-19T20:29:41.007932Z"
}
},
"outputs": [],
"source": [
"RooAddPdf model2(\"model2\", \"(g1+g2)+a\", RooArgList(ebkg, esig));"
]
},
{
"cell_type": "markdown",
"id": "2ae0426d",
"metadata": {},
"source": [
"Draw the frame on the canvas"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ef663bf9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:41.014823Z",
"iopub.status.busy": "2026-05-19T20:29:41.014690Z",
"iopub.status.idle": "2026-05-19T20:29:41.358022Z",
"shell.execute_reply": "2026-05-19T20:29:41.357098Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal9EvaluateTIvEET_PNS1_15DynamicExprInfoEPN5clang11DeclContextE' unresolved while linking [cling interface function]!\n",
"You are probably missing the definition of void cling::runtime::internal::EvaluateT(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*)\n",
"Maybe you need to load the corresponding shared library?\n"
]
}
],
"source": [
"new TCanvas(\"rf202_composite\", \"rf202_composite\", 600, 600);\n",
"gPad->SetLeftMargin(0.15);\n",
"xframe->GetYaxis()->SetTitleOffset(1.4);\n",
"xframe->Draw();"
]
},
{
"cell_type": "markdown",
"id": "e6e877bb",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "226f9a6d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:29:41.359595Z",
"iopub.status.busy": "2026-05-19T20:29:41.359473Z",
"iopub.status.idle": "2026-05-19T20:29:41.587494Z",
"shell.execute_reply": "2026-05-19T20:29:41.586833Z"
}
},
"outputs": [],
"source": [
"%jsroot on\n",
"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
}