{
"cells": [
{
"cell_type": "markdown",
"id": "f0208ef1",
"metadata": {},
"source": [
"# rf604_constraints\n",
"Likelihood and minimization: fitting with constraints\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": 1,
"id": "b12e6b4d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:05.308935Z",
"iopub.status.busy": "2026-05-19T20:33:05.308807Z",
"iopub.status.idle": "2026-05-19T20:33:05.323763Z",
"shell.execute_reply": "2026-05-19T20:33:05.323200Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooPolynomial.h\"\n",
"#include \"RooAddPdf.h\"\n",
"#include \"RooProdPdf.h\"\n",
"#include \"RooFitResult.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"TH1.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "fb6b171f",
"metadata": {},
"source": [
"Create model and dataset\n",
"----------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "d83ad2d7",
"metadata": {},
"source": [
"Construct a Gaussian pdf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "16ff8cf5",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:05.325588Z",
"iopub.status.busy": "2026-05-19T20:33:05.325471Z",
"iopub.status.idle": "2026-05-19T20:33:05.691022Z",
"shell.execute_reply": "2026-05-19T20:33:05.690321Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -10, 10);\n",
"\n",
"RooRealVar m(\"m\", \"m\", 0, -10, 10);\n",
"RooRealVar s(\"s\", \"s\", 2, 0.1, 10);\n",
"RooGaussian gauss(\"gauss\", \"gauss(x,m,s)\", x, m, s);"
]
},
{
"cell_type": "markdown",
"id": "7c856e3e",
"metadata": {},
"source": [
"Construct a flat pdf (polynomial of 0th order)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "42529dcf",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:05.696332Z",
"iopub.status.busy": "2026-05-19T20:33:05.696204Z",
"iopub.status.idle": "2026-05-19T20:33:05.904875Z",
"shell.execute_reply": "2026-05-19T20:33:05.904128Z"
}
},
"outputs": [],
"source": [
"RooPolynomial poly(\"poly\", \"poly(x)\", x);"
]
},
{
"cell_type": "markdown",
"id": "34038356",
"metadata": {},
"source": [
"Construct model = f*gauss + (1-f)*poly"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ee1b8014",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:05.906756Z",
"iopub.status.busy": "2026-05-19T20:33:05.906626Z",
"iopub.status.idle": "2026-05-19T20:33:06.115295Z",
"shell.execute_reply": "2026-05-19T20:33:06.114548Z"
}
},
"outputs": [],
"source": [
"RooRealVar f(\"f\", \"f\", 0.5, 0., 1.);\n",
"RooAddPdf model(\"model\", \"model\", RooArgSet(gauss, poly), f);"
]
},
{
"cell_type": "markdown",
"id": "8fc592b6",
"metadata": {},
"source": [
"Generate small dataset for use in fitting below"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "fc448fee",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:06.117283Z",
"iopub.status.busy": "2026-05-19T20:33:06.117152Z",
"iopub.status.idle": "2026-05-19T20:33:06.325792Z",
"shell.execute_reply": "2026-05-19T20:33:06.325091Z"
}
},
"outputs": [],
"source": [
"std::unique_ptr d{model.generate(x, 50)};"
]
},
{
"cell_type": "markdown",
"id": "8024ba5c",
"metadata": {},
"source": [
"Create constraint pdf\n",
"-----------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "cbabf47f",
"metadata": {},
"source": [
"Construct Gaussian constraint pdf on parameter f at 0.8 with resolution of 0.1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "30379827",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:06.327826Z",
"iopub.status.busy": "2026-05-19T20:33:06.327703Z",
"iopub.status.idle": "2026-05-19T20:33:06.536349Z",
"shell.execute_reply": "2026-05-19T20:33:06.535641Z"
}
},
"outputs": [],
"source": [
"RooGaussian fconstraint(\"fconstraint\", \"fconstraint\", f, 0.8, 0.2);"
]
},
{
"cell_type": "markdown",
"id": "74b63105",
"metadata": {},
"source": [
"METHOD 1 - Add internal constraint to model\n",
"-------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "cb9fb5fb",
"metadata": {},
"source": [
"Multiply constraint term with regular pdf using RooProdPdf\n",
"Specify in fitTo() that internal constraints on parameter f should be used"
]
},
{
"cell_type": "markdown",
"id": "60d4d351",
"metadata": {},
"source": [
"Multiply constraint with pdf"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "59300fcb",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:06.538323Z",
"iopub.status.busy": "2026-05-19T20:33:06.538201Z",
"iopub.status.idle": "2026-05-19T20:33:06.746919Z",
"shell.execute_reply": "2026-05-19T20:33:06.746160Z"
}
},
"outputs": [],
"source": [
"RooProdPdf modelc(\"modelc\", \"model with constraint\", RooArgSet(model, fconstraint));"
]
},
{
"cell_type": "markdown",
"id": "adef0070",
"metadata": {},
"source": [
"Fit model (without use of constraint term)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "0a91f2b4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:06.748931Z",
"iopub.status.busy": "2026-05-19T20:33:06.748803Z",
"iopub.status.idle": "2026-05-19T20:33:06.958704Z",
"shell.execute_reply": "2026-05-19T20:33:06.958086Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data\n",
"[#1] INFO:Fitting -- using generic CPU library compiled with no vectorizations\n",
"[#1] INFO:Fitting -- Creation of NLL object took 798.163 μs\n",
"[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n",
"[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n"
]
}
],
"source": [
"std::unique_ptr r1{model.fitTo(*d, Save(), PrintLevel(-1))};"
]
},
{
"cell_type": "markdown",
"id": "ad0e12e8",
"metadata": {},
"source": [
"Fit modelc with constraint term on parameter f"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1c484447",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:06.960056Z",
"iopub.status.busy": "2026-05-19T20:33:06.959940Z",
"iopub.status.idle": "2026-05-19T20:33:07.169987Z",
"shell.execute_reply": "2026-05-19T20:33:07.169347Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Minimization -- Including the following constraint terms in minimization: (fconstraint)\n",
"[#1] INFO:Minimization -- The global observables are not defined , normalize constraints with respect to the parameters (f)\n",
"[#1] INFO:Fitting -- RooAbsPdf::fitTo(modelc) fixing normalization set for coefficient determination to observables in data\n",
"[#1] INFO:Fitting -- Creation of NLL object took 1.97668 ms\n",
"[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_modelc_modelData) Summation contains a RooNLLVar, using its error level\n",
"[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n"
]
}
],
"source": [
"std::unique_ptr r2{modelc.fitTo(*d, Constrain(f), Save(), PrintLevel(-1))};"
]
},
{
"cell_type": "markdown",
"id": "57fa5481",
"metadata": {},
"source": [
"METHOD 2 - Specify external constraint when fitting\n",
"-------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "f47fb054",
"metadata": {},
"source": [
"Construct another Gaussian constraint pdf on parameter f at 0.2 with resolution of 0.1"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "432ffb31",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:07.171399Z",
"iopub.status.busy": "2026-05-19T20:33:07.171283Z",
"iopub.status.idle": "2026-05-19T20:33:07.379675Z",
"shell.execute_reply": "2026-05-19T20:33:07.379200Z"
}
},
"outputs": [],
"source": [
"RooGaussian fconstext(\"fconstext\", \"fconstext\", f, 0.2, 0.1);"
]
},
{
"cell_type": "markdown",
"id": "a4c6c184",
"metadata": {},
"source": [
"Fit with external constraint"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "5173aff2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:07.381487Z",
"iopub.status.busy": "2026-05-19T20:33:07.381376Z",
"iopub.status.idle": "2026-05-19T20:33:07.591272Z",
"shell.execute_reply": "2026-05-19T20:33:07.590757Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Minimization -- Including the following constraint terms in minimization: (fconstext)\n",
"[#1] INFO:Minimization -- The global observables are not defined , normalize constraints with respect to the parameters (f,m,s)\n",
"[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data\n",
"[#1] INFO:Fitting -- Creation of NLL object took 251.01 μs\n",
"[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level\n",
"[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n"
]
}
],
"source": [
"std::unique_ptr r3{model.fitTo(*d, ExternalConstraints(fconstext), Save(), PrintLevel(-1))};"
]
},
{
"cell_type": "markdown",
"id": "2b46ee50",
"metadata": {},
"source": [
"Print the fit results"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "79670514",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:33:07.593116Z",
"iopub.status.busy": "2026-05-19T20:33:07.592952Z",
"iopub.status.idle": "2026-05-19T20:33:07.801394Z",
"shell.execute_reply": "2026-05-19T20:33:07.800954Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"fit result without constraint (data generated at f=0.5)\n",
"\n",
" RooFitResult: minimized FCN value: 134.849, estimated distance to minimum: 2.11658e-05\n",
" covariance matrix quality: Full, accurate covariance matrix\n",
" Status : MINIMIZE=0 HESSE=0 \n",
"\n",
" Floating Parameter InitialValue FinalValue +/- Error GblCorr.\n",
" -------------------- ------------ -------------------------- --------\n",
" f 5.0000e-01 6.4987e-01 +/- 1.21e-01 \n",
" m 0.0000e+00 7.1824e-01 +/- 4.97e-01 \n",
" s 2.0000e+00 2.1880e+00 +/- 4.61e-01 \n",
"\n",
"fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)\n",
"\n",
" RooFitResult: minimized FCN value: 134.191, estimated distance to minimum: 0.000427811\n",
" covariance matrix quality: Full, accurate covariance matrix\n",
" Status : MINIMIZE=0 HESSE=0 \n",
"\n",
" Floating Parameter InitialValue FinalValue +/- Error GblCorr.\n",
" -------------------- ------------ -------------------------- --------\n",
" f 6.4987e-01 6.8952e-01 +/- 1.01e-01 \n",
" m 7.1824e-01 7.0939e-01 +/- 4.97e-01 \n",
" s 2.1880e+00 2.2657e+00 +/- 4.74e-01 \n",
"\n",
"fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)\n",
"\n",
" RooFitResult: minimized FCN value: 137.195, estimated distance to minimum: 0.000256905\n",
" covariance matrix quality: Full, accurate covariance matrix\n",
" Status : MINIMIZE=0 HESSE=0 \n",
"\n",
" Floating Parameter InitialValue FinalValue +/- Error GblCorr.\n",
" -------------------- ------------ -------------------------- --------\n",
" f 6.8952e-01 3.6252e-01 +/- 8.15e-02 \n",
" m 7.0939e-01 7.1394e-01 +/- 5.87e-01 \n",
" s 2.2657e+00 1.7027e+00 +/- 4.74e-01 \n",
"\n"
]
}
],
"source": [
"cout << \"fit result without constraint (data generated at f=0.5)\" << endl;\n",
"r1->Print(\"v\");\n",
"cout << \"fit result with internal constraint (data generated at f=0.5, constraint is f=0.8+/-0.2)\" << endl;\n",
"r2->Print(\"v\");\n",
"cout << \"fit result with (another) external constraint (data generated at f=0.5, constraint is f=0.2+/-0.1)\" << endl;\n",
"r3->Print(\"v\");"
]
}
],
"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
}