{
"cells": [
{
"cell_type": "markdown",
"id": "bee51db8",
"metadata": {},
"source": [
"# rf707_kernelestimation\n",
"Special pdf's: using non-parametric (multi-dimensional) kernel estimation 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:34 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "fad8ac86",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:46.644473Z",
"iopub.status.busy": "2026-05-19T20:34:46.644368Z",
"iopub.status.idle": "2026-05-19T20:34:46.659935Z",
"shell.execute_reply": "2026-05-19T20:34:46.659351Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"RooRealVar.h\"\n",
"#include \"RooDataSet.h\"\n",
"#include \"RooGaussian.h\"\n",
"#include \"RooPolynomial.h\"\n",
"#include \"RooKeysPdf.h\"\n",
"#include \"RooNDKeysPdf.h\"\n",
"#include \"RooProdPdf.h\"\n",
"#include \"TCanvas.h\"\n",
"#include \"TAxis.h\"\n",
"#include \"TH1.h\"\n",
"#include \"RooPlot.h\"\n",
"using namespace RooFit;"
]
},
{
"cell_type": "markdown",
"id": "f7952e9d",
"metadata": {},
"source": [
"Create low stats 1-D dataset\n",
"-------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "0094455a",
"metadata": {},
"source": [
"Create a toy pdf for sampling"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "007861cd",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:46.669847Z",
"iopub.status.busy": "2026-05-19T20:34:46.669724Z",
"iopub.status.idle": "2026-05-19T20:34:47.083351Z",
"shell.execute_reply": "2026-05-19T20:34:47.082618Z"
}
},
"outputs": [],
"source": [
"RooRealVar x(\"x\", \"x\", 0, 20);\n",
"RooPolynomial p(\"p\", \"p\", x, RooArgList(0.01, -0.01, 0.0004));"
]
},
{
"cell_type": "markdown",
"id": "d0137715",
"metadata": {},
"source": [
"Sample 500 events from p"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "809ad9f2",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:47.094464Z",
"iopub.status.busy": "2026-05-19T20:34:47.094333Z",
"iopub.status.idle": "2026-05-19T20:34:47.303889Z",
"shell.execute_reply": "2026-05-19T20:34:47.302661Z"
}
},
"outputs": [],
"source": [
"std::unique_ptr data1{p.generate(x, 200)};"
]
},
{
"cell_type": "markdown",
"id": "12375aa8",
"metadata": {},
"source": [
"Create 1-D kernel estimation pdf\n",
"---------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "e26d1171",
"metadata": {},
"source": [
"Create adaptive kernel estimation pdf. In this configuration the input data\n",
"is mirrored over the boundaries to minimize edge effects in distribution\n",
"that do not fall to zero towards the edges"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1149339c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:47.305481Z",
"iopub.status.busy": "2026-05-19T20:34:47.305365Z",
"iopub.status.idle": "2026-05-19T20:34:47.514314Z",
"shell.execute_reply": "2026-05-19T20:34:47.513639Z"
}
},
"outputs": [],
"source": [
"RooKeysPdf kest1(\"kest1\", \"kest1\", x, *data1, RooKeysPdf::MirrorBoth);"
]
},
{
"cell_type": "markdown",
"id": "91bdfd2d",
"metadata": {},
"source": [
"An adaptive kernel estimation pdf on the same data without mirroring option\n",
"for comparison"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4a961c0a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:47.516408Z",
"iopub.status.busy": "2026-05-19T20:34:47.516290Z",
"iopub.status.idle": "2026-05-19T20:34:47.725235Z",
"shell.execute_reply": "2026-05-19T20:34:47.724514Z"
}
},
"outputs": [],
"source": [
"RooKeysPdf kest2(\"kest2\", \"kest2\", x, *data1, RooKeysPdf::NoMirror);"
]
},
{
"cell_type": "markdown",
"id": "aec3d214",
"metadata": {},
"source": [
"Adaptive kernel estimation pdf with increased bandwidth scale factor\n",
"(promotes smoothness over detail preservation)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "6ca64e03",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:47.727142Z",
"iopub.status.busy": "2026-05-19T20:34:47.727020Z",
"iopub.status.idle": "2026-05-19T20:34:47.936042Z",
"shell.execute_reply": "2026-05-19T20:34:47.935348Z"
}
},
"outputs": [],
"source": [
"RooKeysPdf kest3(\"kest1\", \"kest1\", x, *data1, RooKeysPdf::MirrorBoth, 2);"
]
},
{
"cell_type": "markdown",
"id": "dd1b1e57",
"metadata": {},
"source": [
"Plot kernel estimation pdfs with and without mirroring over data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "aa5d5b07",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:47.938072Z",
"iopub.status.busy": "2026-05-19T20:34:47.937955Z",
"iopub.status.idle": "2026-05-19T20:34:48.147040Z",
"shell.execute_reply": "2026-05-19T20:34:48.146307Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame = x.frame(Title(\"Adaptive kernel estimation pdf with and w/o mirroring\"), Bins(20));\n",
"data1->plotOn(frame);\n",
"kest1.plotOn(frame);\n",
"kest2.plotOn(frame, LineStyle(kDashed), LineColor(kRed));"
]
},
{
"cell_type": "markdown",
"id": "e21e61b8",
"metadata": {},
"source": [
"Plot kernel estimation pdfs with regular and increased bandwidth"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "979e976b",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:48.149072Z",
"iopub.status.busy": "2026-05-19T20:34:48.148957Z",
"iopub.status.idle": "2026-05-19T20:34:48.358024Z",
"shell.execute_reply": "2026-05-19T20:34:48.357289Z"
}
},
"outputs": [],
"source": [
"RooPlot *frame2 = x.frame(Title(\"Adaptive kernel estimation pdf with regular, increased bandwidth\"));\n",
"kest1.plotOn(frame2);\n",
"kest3.plotOn(frame2, LineColor(kMagenta));"
]
},
{
"cell_type": "markdown",
"id": "6c4f1a0c",
"metadata": {},
"source": [
"Create low stats 2-D dataset\n",
"-------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "b14f6584",
"metadata": {},
"source": [
"Construct a 2D toy pdf for sampling"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "df75c2e1",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:48.360125Z",
"iopub.status.busy": "2026-05-19T20:34:48.360008Z",
"iopub.status.idle": "2026-05-19T20:34:48.569044Z",
"shell.execute_reply": "2026-05-19T20:34:48.568370Z"
}
},
"outputs": [],
"source": [
"RooRealVar y(\"y\", \"y\", 0, 20);\n",
"RooPolynomial py(\"py\", \"py\", y, RooArgList(0.01, 0.01, -0.0004));\n",
"RooProdPdf pxy(\"pxy\", \"pxy\", RooArgSet(p, py));\n",
"std::unique_ptr data2{pxy.generate({x, y}, 1000)};"
]
},
{
"cell_type": "markdown",
"id": "b77a7934",
"metadata": {},
"source": [
"Create 2-D kernel estimation pdf\n",
"---------------------------------------------------------------"
]
},
{
"cell_type": "markdown",
"id": "aea0dfca",
"metadata": {},
"source": [
"Create 2D adaptive kernel estimation pdf with mirroring"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "05cc624d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:48.585447Z",
"iopub.status.busy": "2026-05-19T20:34:48.585307Z",
"iopub.status.idle": "2026-05-19T20:34:48.897515Z",
"shell.execute_reply": "2026-05-19T20:34:48.896802Z"
}
},
"outputs": [],
"source": [
"RooNDKeysPdf kest4(\"kest4\", \"kest4\", RooArgSet(x, y), *data2, \"am\");"
]
},
{
"cell_type": "markdown",
"id": "fe20ee25",
"metadata": {},
"source": [
"Create 2D adaptive kernel estimation pdf with mirroring and double bandwidth"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "4161799d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:48.899490Z",
"iopub.status.busy": "2026-05-19T20:34:48.899373Z",
"iopub.status.idle": "2026-05-19T20:34:49.339805Z",
"shell.execute_reply": "2026-05-19T20:34:49.339093Z"
}
},
"outputs": [],
"source": [
"RooNDKeysPdf kest5(\"kest5\", \"kest5\", RooArgSet(x, y), *data2, \"am\", 2);"
]
},
{
"cell_type": "markdown",
"id": "1efd274a",
"metadata": {},
"source": [
"Create a histogram of the data"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a1785200",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:49.341652Z",
"iopub.status.busy": "2026-05-19T20:34:49.341522Z",
"iopub.status.idle": "2026-05-19T20:34:49.550499Z",
"shell.execute_reply": "2026-05-19T20:34:49.549921Z"
}
},
"outputs": [],
"source": [
"TH1 *hh_data = data2->createHistogram(\"hh_data\", x, Binning(10), YVar(y, Binning(10)));"
]
},
{
"cell_type": "markdown",
"id": "c07d4347",
"metadata": {},
"source": [
"Create histogram of the 2d kernel estimation pdfs"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "2d2e9e14",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:49.552468Z",
"iopub.status.busy": "2026-05-19T20:34:49.552350Z",
"iopub.status.idle": "2026-05-19T20:34:49.951643Z",
"shell.execute_reply": "2026-05-19T20:34:49.950957Z"
}
},
"outputs": [],
"source": [
"TH1 *hh_pdf = kest4.createHistogram(\"hh_pdf\", x, Binning(25), YVar(y, Binning(25)));\n",
"TH1 *hh_pdf2 = kest5.createHistogram(\"hh_pdf2\", x, Binning(25), YVar(y, Binning(25)));\n",
"hh_pdf->SetLineColor(kBlue);\n",
"hh_pdf2->SetLineColor(kMagenta);\n",
"\n",
"TCanvas *c = new TCanvas(\"rf707_kernelestimation\", \"rf707_kernelestimation\", 800, 800);\n",
"c->Divide(2, 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.15);\n",
"frame2->GetYaxis()->SetTitleOffset(1.8);\n",
"frame2->Draw();\n",
"c->cd(3);\n",
"gPad->SetLeftMargin(0.15);\n",
"hh_data->GetZaxis()->SetTitleOffset(1.4);\n",
"hh_data->Draw(\"lego\");\n",
"c->cd(4);\n",
"gPad->SetLeftMargin(0.20);\n",
"hh_pdf->GetZaxis()->SetTitleOffset(2.4);\n",
"hh_pdf->Draw(\"surf\");\n",
"hh_pdf2->Draw(\"surfsame\");"
]
},
{
"cell_type": "markdown",
"id": "16456d48",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e57a160a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:34:49.953558Z",
"iopub.status.busy": "2026-05-19T20:34:49.953441Z",
"iopub.status.idle": "2026-05-19T20:34:50.162345Z",
"shell.execute_reply": "2026-05-19T20:34:50.161618Z"
}
},
"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
}