{
"cells": [
{
"cell_type": "markdown",
"id": "54c7e74c",
"metadata": {},
"source": [
"# hist017_TH1_smooth\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Rene Brun \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:12 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "fd48729c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:12:19.407884Z",
"iopub.status.busy": "2026-05-19T20:12:19.407770Z",
"iopub.status.idle": "2026-05-19T20:12:19.726160Z",
"shell.execute_reply": "2026-05-19T20:12:19.725520Z"
}
},
"outputs": [],
"source": [
"int ipad = 1;\n",
"TCanvas *c1 = 0;"
]
},
{
"cell_type": "markdown",
"id": "2c1b0025",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "83bb0878",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:12:19.727925Z",
"iopub.status.busy": "2026-05-19T20:12:19.727799Z",
"iopub.status.idle": "2026-05-19T20:12:19.747892Z",
"shell.execute_reply": "2026-05-19T20:12:19.747390Z"
}
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void smooth_hist(const char *fname, double xmin, double xmax, int n1, int n2)\n",
"{\n",
"\n",
" std::cout << \"smoothing a \" << fname << \" histogram\" << std::endl;\n",
"\n",
" TH1D *h1 = new TH1D(\"h1\", \"h1\", 100, xmin, xmax);\n",
" TH1D *h2 = new TH1D(\"h2\", \"h2\", 100, xmin, xmax);\n",
" h1->FillRandom(fname, n1);\n",
"\n",
" TH1D *h1_s = new TH1D(*h1);\n",
" h1_s->SetName(\"h1_s\");\n",
" h1_s->Smooth();\n",
"\n",
" h2->FillRandom(fname, n2);\n",
"\n",
" double p1 = h1->Chi2Test(h2, \"\");\n",
" double p2 = h1_s->Chi2Test(h2, \"UU\");\n",
" if (p2 < p1)\n",
" Error(\"testSmooth\", \"TH1::Smooth is not working correctly - a worst chi2 is obtained\");\n",
"\n",
" std::cout << \" chi2 test non-smoothed histo \" << p1 << std::endl;\n",
" std::cout << \" chi2 test smoothed histo \" << p2 << std::endl;\n",
"\n",
" double a1 = h1->AndersonDarlingTest(h2);\n",
" double a2 = h1_s->AndersonDarlingTest(h2);\n",
"\n",
" std::cout << \" AD test non-smoothed histo \" << a1 << std::endl;\n",
" std::cout << \" AD test smoothed histo \" << a2 << std::endl;\n",
"\n",
" double k1 = h1->KolmogorovTest(h2);\n",
" double k2 = h1_s->KolmogorovTest(h2);\n",
"\n",
" std::cout << \" KS test non-smoothed histo \" << k1 << std::endl;\n",
" std::cout << \" KS test smoothed histo \" << k2 << std::endl;\n",
"\n",
" c1->cd(ipad++);\n",
" h1->Draw(\"E\");\n",
" h1_s->SetLineColor(kRed);\n",
" h1_s->Draw(\"same\");\n",
" h2->Scale(double(n1) / n2);\n",
" h2->SetLineColor(kGreen);\n",
" h2->Draw(\"same\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "0f90fcdc",
"metadata": {},
"source": [
" Arguments are defined. "
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ff481df9",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:12:19.749200Z",
"iopub.status.busy": "2026-05-19T20:12:19.749082Z",
"iopub.status.idle": "2026-05-19T20:12:19.953123Z",
"shell.execute_reply": "2026-05-19T20:12:19.952530Z"
}
},
"outputs": [],
"source": [
"int n1 = 1000;\n",
"int n2 = 1000000;"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e574b4e7",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:12:19.954801Z",
"iopub.status.busy": "2026-05-19T20:12:19.954681Z",
"iopub.status.idle": "2026-05-19T20:12:20.402640Z",
"shell.execute_reply": "2026-05-19T20:12:20.402131Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"smoothing a gaus histogram\n",
" chi2 test non-smoothed histo 5.15629e-11\n",
" chi2 test smoothed histo 1\n",
" AD test non-smoothed histo 0.70651\n",
" AD test smoothed histo 0.488314\n",
" KS test non-smoothed histo 0.933145\n",
" KS test smoothed histo 0.770148\n",
"smoothing a landau histogram\n",
" chi2 test non-smoothed histo 0.966464\n",
" chi2 test smoothed histo 1\n",
" AD test non-smoothed histo 0.553608\n",
" AD test smoothed histo 0.318308\n",
" KS test non-smoothed histo 0.7146\n",
" KS test smoothed histo 0.764859\n",
"smoothing a expo histogram\n",
" chi2 test non-smoothed histo 0.364215\n",
" chi2 test smoothed histo 0.999855\n",
" AD test non-smoothed histo 0.870351\n",
" AD test smoothed histo 0.623785\n",
" KS test non-smoothed histo 0.966402\n",
" KS test smoothed histo 0.799221\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : There is a bin in h1 with less than 1 event.\n",
"\n",
"Info in : There is a bin in h1 with less than 1 event.\n",
"\n",
"Info in : There is a bin in h1 with less than 1 event.\n",
"\n",
"Info in : There is a bin in h1 with less than 1 event.\n",
"\n",
"Info in : There is a bin in h1 with less than 1 event.\n",
"\n",
"Info in : There is a bin in h1 with less than 1 event.\n",
"\n"
]
}
],
"source": [
"TDirectory::TContext ctx{nullptr}; // Don't register histograms to the current directory\n",
"\n",
"c1 = new TCanvas();\n",
"c1->Divide(1, 3);\n",
"\n",
"smooth_hist(\"gaus\", -5, 5, n1, n2);\n",
"smooth_hist(\"landau\", -5, 15, n1, n2);\n",
"smooth_hist(\"expo\", -5, 0, n1, n2);"
]
},
{
"cell_type": "markdown",
"id": "fe7ac314",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "dbb12afa",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:12:20.404201Z",
"iopub.status.busy": "2026-05-19T20:12:20.404082Z",
"iopub.status.idle": "2026-05-19T20:12:20.608443Z",
"shell.execute_reply": "2026-05-19T20:12:20.607739Z"
}
},
"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
}