{
"cells": [
{
"cell_type": "markdown",
"id": "53a926e1",
"metadata": {},
"source": [
"# rf315_projectpdf\n",
"Multidimensional models: marginizalization of multi-dimensional pdfs through integration\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:31 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "26dccf02",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:22.367878Z",
"iopub.status.busy": "2026-05-19T20:31:22.367775Z",
"iopub.status.idle": "2026-05-19T20:31:22.382030Z",
"shell.execute_reply": "2026-05-19T20:31:22.381486Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataHist.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooProdPdf.h\"\n",
"#include \"RooPolyVar.h\"\n",
"#include \"TH1.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"RooPlot.h\"\n",
"#include \"RooNumIntConfig.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "84c4e019",
"metadata": {},
"source": [
"Create pdf m(x,y) = gx(x|y)*g(y)\n",
"--------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "bb0bbb12",
"metadata": {},
"source": [
"Increase default precision of numeric integration\n",
"as this exercise has high sensitivity to numeric integration precision"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a5bcceec",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:22.383796Z",
"iopub.status.busy": "2026-05-19T20:31:22.383673Z",
"iopub.status.idle": "2026-05-19T20:31:22.703026Z",
"shell.execute_reply": "2026-05-19T20:31:22.702299Z"
}
},
"outputs": [],
"source": [
"RooAbsPdf::defaultIntegratorConfig()->setEpsRel(1e-8);\n",
"RooAbsPdf::defaultIntegratorConfig()->setEpsAbs(1e-8);"
]
},
{
"cell_type": "markdown",
"id": "9eab8606",
"metadata": {},
"source": [
"Create observables"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d2f0f476",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:22.704726Z",
"iopub.status.busy": "2026-05-19T20:31:22.704581Z",
"iopub.status.idle": "2026-05-19T20:31:22.912599Z",
"shell.execute_reply": "2026-05-19T20:31:22.911949Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", -5, 5);\n",
"RooRealVar y(\"y\", \"y\", -2, 2);"
]
},
{
"cell_type": "markdown",
"id": "4bf0204f",
"metadata": {},
"source": [
"Create function f(y) = a0 + a1*y"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d62d88d6",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:22.914515Z",
"iopub.status.busy": "2026-05-19T20:31:22.914388Z",
"iopub.status.idle": "2026-05-19T20:31:23.122193Z",
"shell.execute_reply": "2026-05-19T20:31:23.121775Z"
}
},
"outputs": [],
"source": [
"RooRealVar a0(\"a0\", \"a0\", 0);\n",
"RooRealVar a1(\"a1\", \"a1\", -1.5, -3, 1);\n",
"RooPolyVar fy(\"fy\", \"fy\", y, RooArgSet(a0, a1));"
]
},
{
"cell_type": "markdown",
"id": "19f2b914",
"metadata": {},
"source": [
"Create gaussx(x,f(y),sx)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3c0a8c71",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:23.124585Z",
"iopub.status.busy": "2026-05-19T20:31:23.124467Z",
"iopub.status.idle": "2026-05-19T20:31:23.332926Z",
"shell.execute_reply": "2026-05-19T20:31:23.332355Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#0] WARNING:InputArguments -- The parameter 'sigmax' with range [-inf, inf] of the RooGaussian 'gaussx' exceeds the safe range of (0, inf). Advise to limit its range.\n"
]
}
],
"source": [
"RooRealVar sigmax(\"sigmax\", \"width of gaussian\", 0.5);\n",
"RooGaussian gaussx(\"gaussx\", \"Gaussian in x with shifting mean in y\", x, fy, sigmax);"
]
},
{
"cell_type": "markdown",
"id": "06756a5b",
"metadata": {},
"source": [
"Create gaussy(y,0,2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "2429726b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:23.334717Z",
"iopub.status.busy": "2026-05-19T20:31:23.334571Z",
"iopub.status.idle": "2026-05-19T20:31:23.542825Z",
"shell.execute_reply": "2026-05-19T20:31:23.542120Z"
}
},
"outputs": [],
"source": [
"RooGaussian gaussy(\"gaussy\", \"Gaussian in y\", y, 0.0, 2.0);"
]
},
{
"cell_type": "markdown",
"id": "a17eb105",
"metadata": {},
"source": [
"Create gaussx(x,sx|y) * gaussy(y)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "1556d015",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:23.544773Z",
"iopub.status.busy": "2026-05-19T20:31:23.544656Z",
"iopub.status.idle": "2026-05-19T20:31:23.753115Z",
"shell.execute_reply": "2026-05-19T20:31:23.752426Z"
}
},
"outputs": [],
"source": [
"RooProdPdf model(\"model\", \"gaussx(x|y)*gaussy(y)\", gaussy, Conditional(gaussx, x));"
]
},
{
"cell_type": "markdown",
"id": "30e829da",
"metadata": {},
"source": [
"Marginalize m(x,y) to m(x)\n",
"----------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "1adc9a14",
"metadata": {},
"source": [
"modelx(x) = Int model(x,y) dy"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e71439f4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:23.754996Z",
"iopub.status.busy": "2026-05-19T20:31:23.754879Z",
"iopub.status.idle": "2026-05-19T20:31:23.962895Z",
"shell.execute_reply": "2026-05-19T20:31:23.962211Z"
}
},
"outputs": [],
"source": [
"RooAbsPdf *modelx = model.createProjection(y);"
]
},
{
"cell_type": "markdown",
"id": "b3edb1aa",
"metadata": {},
"source": [
"Use marginalized p.d.f. as regular 1-D p.d.f.\n",
"------------------------------------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "00b28da3",
"metadata": {},
"source": [
"Sample 1000 events from modelx"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "6cfa51e1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:23.965027Z",
"iopub.status.busy": "2026-05-19T20:31:23.964908Z",
"iopub.status.idle": "2026-05-19T20:31:24.172961Z",
"shell.execute_reply": "2026-05-19T20:31:24.172663Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[#1] INFO:NumericIntegration -- RooRealIntegral::init(SPECINT[gaussy_NORM[y]_X_gaussx_NORM[x]]_Int[y]) using numeric integrator RooIntegrator1D to calculate Int(y)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_62: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{modelx->generateBinned(x, 1000)};\n",
" ^\n"
]
}
],
"source": [
"std::unique_ptr data{modelx->generateBinned(x, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "5eaac9e9",
"metadata": {},
"source": [
"Fit modelx to toy data"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "60314e0c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:24.174832Z",
"iopub.status.busy": "2026-05-19T20:31:24.174716Z",
"iopub.status.idle": "2026-05-19T20:31:24.383529Z",
"shell.execute_reply": "2026-05-19T20:31:24.382938Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_63:2:17: error: reference to 'data' is ambiguous\n",
" modelx->fitTo(*data, Verbose(), PrintLevel(-1));\n",
" ^\n",
"input_line_62:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{modelx->generateBinned(x, 1000)};\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": [
"modelx->fitTo(*data, Verbose(), PrintLevel(-1));"
]
},
{
"cell_type": "markdown",
"id": "063725e5",
"metadata": {},
"source": [
"Plot modelx over data"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "869b7e1b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:24.385427Z",
"iopub.status.busy": "2026-05-19T20:31:24.385311Z",
"iopub.status.idle": "2026-05-19T20:31:24.593895Z",
"shell.execute_reply": "2026-05-19T20:31:24.593357Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_64:3:1: error: reference to 'data' is ambiguous\n",
"data->plotOn(frame);\n",
"^\n",
"input_line_62:2:30: note: candidate found by name lookup is 'data'\n",
" std::unique_ptr data{modelx->generateBinned(x, 1000)};\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 *frame = x.frame(40);\n",
"data->plotOn(frame);\n",
"modelx->plotOn(frame);"
]
},
{
"cell_type": "markdown",
"id": "8e0fa0e0",
"metadata": {},
"source": [
"Make 2D histogram of model(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "766e3366",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:24.595709Z",
"iopub.status.busy": "2026-05-19T20:31:24.595574Z",
"iopub.status.idle": "2026-05-19T20:31:24.801218Z",
"shell.execute_reply": "2026-05-19T20:31:24.800632Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_66:2:3: error: use of undeclared identifier 'frame'\n",
" (frame->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
" ^\n",
"Error in : Error evaluating expression (frame->GetYaxis()->SetTitleOffset(1.3999999999999999))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"TH1 *hh = model.createHistogram(\"x,y\");\n",
"hh->SetLineColor(kBlue);\n",
"\n",
"TCanvas *c = new TCanvas(\"rf315_projectpdf\", \"rf315_projectpdf\", 800, 400);\n",
"c->Divide(2);\n",
"c->cd(1);\n",
"gPad->SetLeftMargin(0.15);\n",
"frame->GetYaxis()->SetTitleOffset(1.4);\n",
"frame->Draw();\n",
"c->cd(2);\n",
"gPad->SetLeftMargin(0.20);\n",
"hh->GetZaxis()->SetTitleOffset(2.5);\n",
"hh->Draw(\"surf\");"
]
},
{
"cell_type": "markdown",
"id": "77158a7f",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "190ddf15",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:31:24.802913Z",
"iopub.status.busy": "2026-05-19T20:31:24.802793Z",
"iopub.status.idle": "2026-05-19T20:31:25.011159Z",
"shell.execute_reply": "2026-05-19T20:31:25.010466Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
"
\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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
}