{
"cells": [
{
"cell_type": "markdown",
"id": "05006a9b",
"metadata": {},
"source": [
"# rf103_interprfuncs\n",
"Basic functionality: interpreted functions and PDFs.\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:28 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6dde8232",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:29.214456Z",
"iopub.status.busy": "2026-05-19T20:28:29.214348Z",
"iopub.status.idle": "2026-05-19T20:28:29.226444Z",
"shell.execute_reply": "2026-05-19T20:28:29.226077Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"RooFitResult.h\"\n",
"#include \"RooGenericPdf.h\"\n",
"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "4fa72646",
"metadata": {},
"source": [
"----------------------------------------------------\n",
"Generic interpreted p.d.f.\n",
"===================================================="
]
},
{
"cell_type": "markdown",
"id": "2a967478",
"metadata": {},
"source": [
"Declare observable x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "77db7715",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:29.228062Z",
"iopub.status.busy": "2026-05-19T20:28:29.227951Z",
"iopub.status.idle": "2026-05-19T20:28:29.434814Z",
"shell.execute_reply": "2026-05-19T20:28:29.434119Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -20, 20);"
]
},
{
"cell_type": "markdown",
"id": "9cd1016a",
"metadata": {},
"source": [
"Construct generic pdf from interpreted expression\n",
"-------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "46b891e9",
"metadata": {},
"source": [
"To construct a proper pdf, the formula expression is explicitly normalized internally by dividing\n",
"it by a numeric integral of the expression over x in the range [-20,20]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7f17b98a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:29.436297Z",
"iopub.status.busy": "2026-05-19T20:28:29.436186Z",
"iopub.status.idle": "2026-05-19T20:28:29.643676Z",
"shell.execute_reply": "2026-05-19T20:28:29.643099Z"
}
},
"outputs": [],
"source": [
"RooRealVar alpha(\"alpha\", \"alpha\", 5, 0.1, 10);\n",
"RooGenericPdf genpdf(\"genpdf\", \"genpdf\", \"(1+0.1*abs(x)+sin(sqrt(abs(x*alpha+0.1))))\", RooArgSet(x, alpha));"
]
},
{
"cell_type": "markdown",
"id": "290a36b9",
"metadata": {},
"source": [
"Sample, fit and plot generic pdf\n",
"---------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "6df23411",
"metadata": {},
"source": [
"Generate a toy dataset from the interpreted pdf"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "861b72da",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:29.645720Z",
"iopub.status.busy": "2026-05-19T20:28:29.645584Z",
"iopub.status.idle": "2026-05-19T20:28:29.853959Z",
"shell.execute_reply": "2026-05-19T20:28:29.853019Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(genpdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)\n",
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(genpdf_Int[x]) using numeric integrator RooIntegrator1D to calculate Int(x)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_57: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{genpdf.generate(x, 10000)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{genpdf.generate(x, 10000)};"
]
},
{
"cell_type": "markdown",
"id": "e684a500",
"metadata": {},
"source": [
"Fit the interpreted pdf to the generated data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "2b04eade",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:29.855405Z",
"iopub.status.busy": "2026-05-19T20:28:29.855292Z",
"iopub.status.idle": "2026-05-19T20:28:30.064026Z",
"shell.execute_reply": "2026-05-19T20:28:30.063581Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_58:2:16: error: reference to 'data' is ambiguous\n",
" genpdf.fitTo(*data, PrintLevel(-1));\n",
" ^\n",
"input_line_57:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{genpdf.generate(x, 10000)};\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": [
"genpdf.fitTo(*data, PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "ffd01f20",
"metadata": {},
"source": [
"Make a plot of the data and the pdf overlaid"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "64145b30",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:30.066092Z",
"iopub.status.busy": "2026-05-19T20:28:30.065970Z",
"iopub.status.idle": "2026-05-19T20:28:30.274645Z",
"shell.execute_reply": "2026-05-19T20:28:30.274129Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_59:3:1: error: reference to 'data' is ambiguous\n",
"data->plotOn(xframe);\n",
"^\n",
"input_line_57:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{genpdf.generate(x, 10000)};\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(\"Interpreted expression pdf\"));\n",
"data->plotOn(xframe);\n",
"genpdf.plotOn(xframe);"
]
},
{
"cell_type": "markdown",
"id": "bd708a23",
"metadata": {},
"source": [
"-----------------------------------------------------------------------------------------------------------\n",
"Standard p.d.f adjust with interpreted helper function\n",
"==========================================================================================================\n",
"Make a gauss(x,sqrt(mean2),sigma) from a standard RooGaussian"
]
},
{
"cell_type": "markdown",
"id": "4d0747ad",
"metadata": {},
"source": [
"Construct standard pdf with formula replacing parameter\n",
"------------------------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "070de3db",
"metadata": {},
"source": [
"Construct parameter mean2 and sigma"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "8b582c1c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:30.276419Z",
"iopub.status.busy": "2026-05-19T20:28:30.276293Z",
"iopub.status.idle": "2026-05-19T20:28:30.483785Z",
"shell.execute_reply": "2026-05-19T20:28:30.482864Z"
}
},
"outputs": [],
"source": [
"RooRealVar mean2(\"mean2\", \"mean^2\", 10, 0, 200);\n",
"RooRealVar sigma(\"sigma\", \"sigma\", 3, 0.1, 10);"
]
},
{
"cell_type": "markdown",
"id": "a0dc6587",
"metadata": {},
"source": [
"Construct interpreted function mean = sqrt(mean^2)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "42ca2dba",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:30.485563Z",
"iopub.status.busy": "2026-05-19T20:28:30.485416Z",
"iopub.status.idle": "2026-05-19T20:28:30.692778Z",
"shell.execute_reply": "2026-05-19T20:28:30.692146Z"
}
},
"outputs": [],
"source": [
"RooFormulaVar mean(\"mean\", \"mean\", \"sqrt(mean2)\", mean2);"
]
},
{
"cell_type": "markdown",
"id": "03d41076",
"metadata": {},
"source": [
"Construct a gaussian g2(x,sqrt(mean2),sigma) ;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e5819a47",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:30.694875Z",
"iopub.status.busy": "2026-05-19T20:28:30.694757Z",
"iopub.status.idle": "2026-05-19T20:28:30.901862Z",
"shell.execute_reply": "2026-05-19T20:28:30.901271Z"
}
},
"outputs": [],
"source": [
"RooGaussian g2(\"g2\", \"h2\", x, mean, sigma);"
]
},
{
"cell_type": "markdown",
"id": "fa429328",
"metadata": {},
"source": [
"Generate toy data\n",
"---------------------------------"
]
},
{
"cell_type": "markdown",
"id": "fa8f7554",
"metadata": {},
"source": [
"Construct a separate gaussian g1(x,10,3) to generate a toy Gaussian dataset with mean 10 and width 3"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "dd0b7c77",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:30.903786Z",
"iopub.status.busy": "2026-05-19T20:28:30.903670Z",
"iopub.status.idle": "2026-05-19T20:28:31.110807Z",
"shell.execute_reply": "2026-05-19T20:28:31.110238Z"
}
},
"outputs": [],
"source": [
"RooGaussian g1(\"g1\", \"g1\", x, 10.0, 3.0);\n",
"std::unique_ptr data2{g1.generate(x, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "4efc3b32",
"metadata": {},
"source": [
"Fit and plot tailored standard pdf\n",
"-------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "6ea02b5e",
"metadata": {},
"source": [
"Fit g2 to data from g1"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "d606a5fd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:31.113123Z",
"iopub.status.busy": "2026-05-19T20:28:31.113010Z",
"iopub.status.idle": "2026-05-19T20:28:31.320641Z",
"shell.execute_reply": "2026-05-19T20:28:31.320296Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:Fitting -- RooAbsPdf::fitTo(g2_over_g2_Int[x]) 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 831.994 μs\n",
"[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_g2_over_g2_Int[x]_g1Data) Summation contains a RooNLLVar, using its error level\n",
"[#1] INFO:Minimization -- [fitFCN] No discrete parameters, performing continuous minimization only\n",
"\n",
" RooFitResult: minimized FCN value: 2551.39, estimated distance to minimum: 4.39288e-06\n",
" covariance matrix quality: Full, accurate covariance matrix\n",
" Status : MINIMIZE=0 HESSE=0 \n",
"\n",
" Floating Parameter FinalValue +/- Error \n",
" -------------------- --------------------------\n",
" mean2 1.0010e+02 +/- 1.98e+00\n",
" sigma 3.1172e+00 +/- 7.12e-02\n",
"\n"
]
}
],
"source": [
"std::unique_ptr fitResult{g2.fitTo(*data2, Save(), PrintLevel(-1))};\n",
"fitResult->Print();"
]
},
{
"cell_type": "markdown",
"id": "c79d75fd",
"metadata": {},
"source": [
"Plot data on frame and overlay projection of g2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "930a97d7",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:31.322522Z",
"iopub.status.busy": "2026-05-19T20:28:31.322411Z",
"iopub.status.idle": "2026-05-19T20:28:31.530099Z",
"shell.execute_reply": "2026-05-19T20:28:31.529429Z"
}
},
"outputs": [],
"source": [
"RooPlot *xframe2 = x.frame(Title(\"Tailored Gaussian pdf\"));\n",
"data2->plotOn(xframe2);\n",
"g2.plotOn(xframe2);"
]
},
{
"cell_type": "markdown",
"id": "bbbc4f8a",
"metadata": {},
"source": [
"Draw all frames on a canvas"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "4eec9e72",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:31.532367Z",
"iopub.status.busy": "2026-05-19T20:28:31.532253Z",
"iopub.status.idle": "2026-05-19T20:28:31.740040Z",
"shell.execute_reply": "2026-05-19T20:28:31.739344Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_92:2:3: error: use of undeclared identifier 'xframe'\n",
" (xframe->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
" ^\n",
"Error in : Error evaluating expression (xframe->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"TCanvas *c = new TCanvas(\"rf103_interprfuncs\", \"rf103_interprfuncs\", 800, 400);\n",
"c->Divide(2);\n",
"c->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"xframe->GetYaxis()->SetTitleOffset(1.4);\n",
"xframe->Draw();\n",
"c->cd(2);\n",
"gPad->SetLeftMargin(0.15);\n",
"xframe2->GetYaxis()->SetTitleOffset(1.4);\n",
"xframe2->Draw();"
]
},
{
"cell_type": "markdown",
"id": "26e97afd",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a204c45f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:28:31.741553Z",
"iopub.status.busy": "2026-05-19T20:28:31.741441Z",
"iopub.status.idle": "2026-05-19T20:28:31.976275Z",
"shell.execute_reply": "2026-05-19T20:28:31.975545Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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
}