{ "cells": [ { "cell_type": "markdown", "id": "ef0a3d88", "metadata": {}, "source": [ "# hist009_TH1_normalize\n", "\n", "Image produced by `.x NormalizeHistogram.C`\n", "Two different methods of normalizing histograms\n", "are shown, each with the original histogram.\n", "next to the normalized one.\n", "\n", "\n", "\n", "**Author:** Advait Dhingra \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": "46a89cc0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:04.280775Z", "iopub.status.busy": "2026-05-19T20:12:04.280653Z", "iopub.status.idle": "2026-05-19T20:12:04.594754Z", "shell.execute_reply": "2026-05-19T20:12:04.594220Z" } }, "outputs": [], "source": [ "const std::array binsx{0, 5, 10, 20, 50, 100};\n", "TH1D *orig = new TH1D(\"orig\", \"Original histogram before normalization\", binsx.size() - 1, binsx.data());\n", "\n", "gStyle->SetTitleFontSize(0.06);" ] }, { "cell_type": "markdown", "id": "466e00b7", "metadata": {}, "source": [ "Filling histogram with random entries" ] }, { "cell_type": "code", "execution_count": 2, "id": "7aef3143", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:04.596754Z", "iopub.status.busy": "2026-05-19T20:12:04.596611Z", "iopub.status.idle": "2026-05-19T20:12:04.799949Z", "shell.execute_reply": "2026-05-19T20:12:04.799637Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_53:8:1: warning: 'norm' shadows a declaration with the same name in the 'std' namespace; use '::norm' to reference this declaration\n", "TH1D *norm = static_cast(orig->Clone(\"norm\"));\n", "^\n" ] } ], "source": [ "TRandom2 rand;\n", "for (int i = 0; i < 100000; ++i) {\n", " double r = rand.Rndm() * 100;\n", " orig->Fill(r);\n", "}\n", "\n", "TH1D *norm = static_cast(orig->Clone(\"norm\"));\n", "norm->SetTitle(\"Normalized Histogram\");" ] }, { "cell_type": "markdown", "id": "c01eb846", "metadata": {}, "source": [ "Normalizing the Histogram by scaling by 1 / the integral and taking width into account" ] }, { "cell_type": "code", "execution_count": 3, "id": "843adce9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:04.807353Z", "iopub.status.busy": "2026-05-19T20:12:04.807181Z", "iopub.status.idle": "2026-05-19T20:12:05.029826Z", "shell.execute_reply": "2026-05-19T20:12:05.017577Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_55:2:2: error: reference to 'norm' is ambiguous\n", " norm->Scale(1. / norm->Integral(), \"width\");\n", " ^\n", "input_line_53:8:7: note: candidate found by name lookup is 'norm'\n", "TH1D *norm = static_cast(orig->Clone(\"norm\"));\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/complex:2453:5: note: candidate found by name lookup is 'std::norm'\n", " norm(_Tp __x)\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/complex:959:5: note: candidate found by name lookup is 'std::norm'\n", " norm(const complex<_Tp>& __z)\n", " ^\n" ] } ], "source": [ "norm->Scale(1. / norm->Integral(), \"width\");" ] }, { "cell_type": "markdown", "id": "30a4c5c5", "metadata": {}, "source": [ "Drawing everything" ] }, { "cell_type": "code", "execution_count": 4, "id": "4a91fab0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:05.033422Z", "iopub.status.busy": "2026-05-19T20:12:05.033235Z", "iopub.status.idle": "2026-05-19T20:12:05.238531Z", "shell.execute_reply": "2026-05-19T20:12:05.237867Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "input_line_56:8:1: error: reference to 'norm' is ambiguous\n", "norm->Draw();\n", "^\n", "input_line_53:8:7: note: candidate found by name lookup is 'norm'\n", "TH1D *norm = static_cast(orig->Clone(\"norm\"));\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/complex:2453:5: note: candidate found by name lookup is 'std::norm'\n", " norm(_Tp __x)\n", " ^\n", "/usr/lib/gcc/x86_64-redhat-linux/14/../../../../include/c++/14/complex:959:5: note: candidate found by name lookup is 'std::norm'\n", " norm(const complex<_Tp>& __z)\n", " ^\n" ] } ], "source": [ "TCanvas *c1 = new TCanvas(\"c1\", \"Histogram Normalization\", 700, 900);\n", "c1->Divide(1, 2);\n", "\n", "c1->cd(1);\n", "orig->Draw();\n", "c1->cd(2);\n", "norm->Draw();" ] }, { "cell_type": "markdown", "id": "48a2fa03", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "2bd29372", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:05.240433Z", "iopub.status.busy": "2026-05-19T20:12:05.240267Z", "iopub.status.idle": "2026-05-19T20:12:05.503646Z", "shell.execute_reply": "2026-05-19T20:12:05.481767Z" } }, "outputs": [], "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 }