{ "cells": [ { "cell_type": "markdown", "id": "a2630978", "metadata": {}, "source": [ "# hist013_TH1_rebin\n", "\n", "This tutorial illustrates how to:\n", " - create a variable bin-width histogram with a binning such\n", " that the population per bin is about the same.\n", " - rebin a variable bin-width histogram into another one.\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": "markdown", "id": "f996588f", "metadata": {}, "source": [ "create a fix bin histogram" ] }, { "cell_type": "code", "execution_count": 1, "id": "8793ad59", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:11.248719Z", "iopub.status.busy": "2026-05-19T20:12:11.248573Z", "iopub.status.idle": "2026-05-19T20:12:11.725787Z", "shell.execute_reply": "2026-05-19T20:12:11.725150Z" } }, "outputs": [], "source": [ "TH1F *h = new TH1F(\"h\", \"test rebin\", 100, -3, 3);\n", "Int_t nentries = 1000;\n", "h->FillRandom(\"gaus\", nentries);\n", "Double_t xbins[1001];\n", "Int_t k = 0;\n", "TAxis *axis = h->GetXaxis();\n", "for (Int_t i = 1; i <= 100; i++) {\n", " Int_t y = (Int_t)h->GetBinContent(i);\n", " if (y <= 0)\n", " continue;\n", " Double_t dx = axis->GetBinWidth(i) / y;\n", " Double_t xmin = axis->GetBinLowEdge(i);\n", " for (Int_t j = 0; j < y; j++) {\n", " xbins[k] = xmin + j * dx;\n", " k++;\n", " }\n", "}\n", "xbins[k] = axis->GetXmax();" ] }, { "cell_type": "markdown", "id": "2fac8d96", "metadata": {}, "source": [ "create a variable bin-width histogram out of fix bin histogram\n", "new rebinned histogram should have about 10 entries per bin" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3a0191c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:11.739241Z", "iopub.status.busy": "2026-05-19T20:12:11.739104Z", "iopub.status.idle": "2026-05-19T20:12:11.943251Z", "shell.execute_reply": "2026-05-19T20:12:11.942646Z" } }, "outputs": [], "source": [ "TH1F *hnew = new TH1F(\"hnew\", \"rebinned\", k, xbins);\n", "hnew->FillRandom(\"gaus\", 10 * nentries);" ] }, { "cell_type": "markdown", "id": "adc97b03", "metadata": {}, "source": [ "rebin hnew keeping only 50% of the bins" ] }, { "cell_type": "code", "execution_count": 3, "id": "f980c3f2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:11.945491Z", "iopub.status.busy": "2026-05-19T20:12:11.945356Z", "iopub.status.idle": "2026-05-19T20:12:12.149678Z", "shell.execute_reply": "2026-05-19T20:12:12.148863Z" } }, "outputs": [], "source": [ "Double_t xbins2[501];\n", "Int_t kk = 0;\n", "for (Int_t j = 0; j < k; j += 2) {\n", " xbins2[kk] = xbins[j];\n", " kk++;\n", "}\n", "xbins2[kk] = xbins[k];\n", "TH1F *hnew2 = (TH1F *)hnew->Rebin(kk, \"hnew2\", xbins2);" ] }, { "cell_type": "markdown", "id": "1213c3e8", "metadata": {}, "source": [ "draw the 3 histograms" ] }, { "cell_type": "code", "execution_count": 4, "id": "41816de3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:12.151562Z", "iopub.status.busy": "2026-05-19T20:12:12.151431Z", "iopub.status.idle": "2026-05-19T20:12:12.355783Z", "shell.execute_reply": "2026-05-19T20:12:12.355004Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\", \"c1\", 800, 1000);\n", "c1->Divide(1, 3);\n", "c1->cd(1);\n", "h->Draw();\n", "c1->cd(2);\n", "hnew->Draw();\n", "c1->cd(3);\n", "hnew2->Draw();" ] }, { "cell_type": "markdown", "id": "e848a9eb", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "cc632e32", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:12.357986Z", "iopub.status.busy": "2026-05-19T20:12:12.357864Z", "iopub.status.idle": "2026-05-19T20:12:12.577538Z", "shell.execute_reply": "2026-05-19T20:12:12.576933Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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 }