{
"cells": [
{
"cell_type": "markdown",
"id": "194209e8",
"metadata": {},
"source": [
"# exampleTKDE\n",
"Example of using the TKDE class (kernel density estimator).\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Lorenzo Moneta, Bartolomeu Rabacal (Dec 2010) \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:24 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d77eda30",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:30.366952Z",
"iopub.status.busy": "2026-05-19T20:24:30.366822Z",
"iopub.status.idle": "2026-05-19T20:24:30.374848Z",
"shell.execute_reply": "2026-05-19T20:24:30.374313Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"#include \"TH1.h\"\n",
"#include \"TF1.h\"\n",
"#include \"TKDE.h\"\n",
"#include \"TCanvas.h\"\n",
"/*#include \"TStopwatch.h\"*/\n",
"#include \"TRandom.h\"\n",
"#include \"Math/DistFunc.h\"\n",
"#include \"TLegend.h\""
]
},
{
"cell_type": "markdown",
"id": "090b47c8",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a261bf42",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:30.376512Z",
"iopub.status.busy": "2026-05-19T20:24:30.376393Z",
"iopub.status.idle": "2026-05-19T20:24:30.692805Z",
"shell.execute_reply": "2026-05-19T20:24:30.692093Z"
}
},
"outputs": [],
"source": [
"int n = 1000;"
]
},
{
"cell_type": "markdown",
"id": "b936b48b",
"metadata": {},
"source": [
"generate some gaussian points"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a7602722",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:30.694937Z",
"iopub.status.busy": "2026-05-19T20:24:30.694814Z",
"iopub.status.idle": "2026-05-19T20:24:30.918677Z",
"shell.execute_reply": "2026-05-19T20:24:30.917789Z"
}
},
"outputs": [],
"source": [
"int nbin = 100;\n",
"double xmin = 0;\n",
"double xmax = 10;\n",
"\n",
"TH1D * h1 = new TH1D(\"h1\",\"h1\",nbin,xmin,xmax);"
]
},
{
"cell_type": "markdown",
"id": "2e485f71",
"metadata": {},
"source": [
"generate some points with bi- gaussian distribution"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ccf6b042",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:30.926432Z",
"iopub.status.busy": "2026-05-19T20:24:30.926293Z",
"iopub.status.idle": "2026-05-19T20:24:31.153382Z",
"shell.execute_reply": "2026-05-19T20:24:31.152749Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_55:2:2: warning: 'data' shadows a declaration with the same name in the 'std' namespace; use '::data' to reference this declaration\n",
" std::vector data(n);\n",
" ^\n"
]
}
],
"source": [
"std::vector data(n);\n",
"for (int i = 0; i < n; ++i) {\n",
" if (i < 0.4*n) {\n",
" data[i] = gRandom->Gaus(2,1);\n",
" h1->Fill(data[i]);\n",
" }\n",
" else {\n",
" data[i] = gRandom->Gaus(7,1.5);\n",
" h1->Fill(data[i]);\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "4065d7d8",
"metadata": {},
"source": [
"scale histogram"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "41052958",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:31.155071Z",
"iopub.status.busy": "2026-05-19T20:24:31.154950Z",
"iopub.status.idle": "2026-05-19T20:24:31.362496Z",
"shell.execute_reply": "2026-05-19T20:24:31.361656Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : created default TCanvas with name c1\n"
]
}
],
"source": [
"h1->Scale(1./h1->Integral(),\"width\" );\n",
"h1->SetStats(false);\n",
"h1->SetTitle(\"Bi-Gaussian\");\n",
"h1->Draw();"
]
},
{
"cell_type": "markdown",
"id": "30605f22",
"metadata": {},
"source": [
"drawn true normalized density"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "eb495906",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:31.363938Z",
"iopub.status.busy": "2026-05-19T20:24:31.363818Z",
"iopub.status.idle": "2026-05-19T20:24:31.568352Z",
"shell.execute_reply": "2026-05-19T20:24:31.567708Z"
}
},
"outputs": [],
"source": [
"TF1 * f1 = new TF1(\"f1\",\"0.4*ROOT::Math::normal_pdf(x,1,2)+0.6*ROOT::Math::normal_pdf(x,1.5,7)\",xmin,xmax);\n",
"f1->SetLineColor(kGreen+2);\n",
"f1->Draw(\"SAME\");"
]
},
{
"cell_type": "markdown",
"id": "dbbad692",
"metadata": {},
"source": [
"create TKDE class"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ff3a6310",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:31.570126Z",
"iopub.status.busy": "2026-05-19T20:24:31.570004Z",
"iopub.status.idle": "2026-05-19T20:24:31.782106Z",
"shell.execute_reply": "2026-05-19T20:24:31.781812Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_64:3:27: error: reference to 'data' is ambiguous\n",
"TKDE * kde = new TKDE(n, &data[0], xmin,xmax, \"\", rho);\n",
" ^\n",
"input_line_55:2:22: note: candidate found by name lookup is 'data'\n",
" std::vector data(n);\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": [
"double rho = 1.0; //default value\n",
"TKDE * kde = new TKDE(n, &data[0], xmin,xmax, \"\", rho);"
]
},
{
"cell_type": "markdown",
"id": "ca445688",
"metadata": {},
"source": [
"kde->Draw(\"ConfidenceInterval@0.95 Same\");"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c79ef6e4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:31.789148Z",
"iopub.status.busy": "2026-05-19T20:24:31.789018Z",
"iopub.status.idle": "2026-05-19T20:24:31.996655Z",
"shell.execute_reply": "2026-05-19T20:24:31.995700Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_66:2:3: error: use of undeclared identifier 'kde'\n",
" (kde->Draw(\"SAME\"))\n",
" ^\n",
"Error in : Error evaluating expression (kde->Draw(\"SAME\"))\n",
"Execution of your code was aborted.\n"
]
}
],
"source": [
"kde->Draw(\"SAME\");\n",
"\n",
"TLegend * legend = new TLegend(0.6,0.7,0.9,0.95);\n",
"legend->AddEntry(f1,\"True function\");\n",
"legend->AddEntry(kde->GetDrawnFunction(),\"TKDE\");\n",
"legend->AddEntry(kde->GetDrawnLowerFunction(),\"TKDE - #sigma\");\n",
"legend->AddEntry(kde->GetDrawnUpperFunction(),\"TKDE + #sigma\");\n",
"legend->Draw();"
]
},
{
"cell_type": "markdown",
"id": "02370adc",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "30df7d9d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:24:31.998178Z",
"iopub.status.busy": "2026-05-19T20:24:31.998054Z",
"iopub.status.idle": "2026-05-19T20:24:32.220348Z",
"shell.execute_reply": "2026-05-19T20:24:32.219875Z"
}
},
"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
}