{ "cells": [ { "cell_type": "markdown", "id": "085946d3", "metadata": {}, "source": [ "# hist004_TH1_labels\n", "\n", "A TH1 can have named bins that are filled with the method overload TH1::Fill(const char*, double)\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:11 PM." ] }, { "cell_type": "markdown", "id": "a5af0ad3", "metadata": {}, "source": [ "Create the histogram" ] }, { "cell_type": "code", "execution_count": 1, "id": "9c6e4003", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:52.666440Z", "iopub.status.busy": "2026-05-19T20:11:52.666318Z", "iopub.status.idle": "2026-05-19T20:11:52.995860Z", "shell.execute_reply": "2026-05-19T20:11:52.995457Z" } }, "outputs": [], "source": [ "const std::array people{\"Jean\", \"Pierre\", \"Marie\", \"Odile\", \"Sebastien\", \"Fons\", \"Rene\",\n", " \"Nicolas\", \"Xavier\", \"Greg\", \"Bjarne\", \"Anton\", \"Otto\", \"Eddy\",\n", " \"Peter\", \"Pasha\", \"Philippe\", \"Suzanne\", \"Jeff\", \"Valery\"};" ] }, { "cell_type": "markdown", "id": "8a7b83b1", "metadata": {}, "source": [ "Start with an arbitrary amount of bins and an arbitrary range, but this will be extended thanks to SetCanExtend()." ] }, { "cell_type": "code", "execution_count": 2, "id": "b6221643", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:53.003368Z", "iopub.status.busy": "2026-05-19T20:11:53.003224Z", "iopub.status.idle": "2026-05-19T20:11:53.207097Z", "shell.execute_reply": "2026-05-19T20:11:53.206449Z" } }, "outputs": [], "source": [ "int nBins = 3;\n", "double rangeMin = 0.0;\n", "double rangeMax = 3.0;\n", "auto *h = new TH1D(\"h\", \"test\", nBins, rangeMin, rangeMax);" ] }, { "cell_type": "markdown", "id": "358eb8a2", "metadata": {}, "source": [ "Disable the default stats box when drawing this histogram" ] }, { "cell_type": "code", "execution_count": 3, "id": "8a280043", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:53.209032Z", "iopub.status.busy": "2026-05-19T20:11:53.208919Z", "iopub.status.idle": "2026-05-19T20:11:53.412655Z", "shell.execute_reply": "2026-05-19T20:11:53.411953Z" } }, "outputs": [], "source": [ "h->SetStats(0);\n", "h->SetFillColor(38);" ] }, { "cell_type": "markdown", "id": "7c7e328c", "metadata": {}, "source": [ "Allow both axes to extend past the initial range we gave in the constructor" ] }, { "cell_type": "code", "execution_count": 4, "id": "b2808efd", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:53.414366Z", "iopub.status.busy": "2026-05-19T20:11:53.414253Z", "iopub.status.idle": "2026-05-19T20:11:53.618001Z", "shell.execute_reply": "2026-05-19T20:11:53.617322Z" } }, "outputs": [], "source": [ "h->SetCanExtend(TH1::kAllAxes);" ] }, { "cell_type": "markdown", "id": "6c5dc270", "metadata": {}, "source": [ "Fill the Y axis with arbitrary values, a random amount per bin" ] }, { "cell_type": "code", "execution_count": 5, "id": "9ea22f3b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:53.619544Z", "iopub.status.busy": "2026-05-19T20:11:53.619430Z", "iopub.status.idle": "2026-05-19T20:11:53.821293Z", "shell.execute_reply": "2026-05-19T20:11:53.820900Z" } }, "outputs": [], "source": [ "TRandom3 rng;\n", "for (int i = 0; i < 5000; i++) {\n", " int r = rng.Rndm() * 20;\n", " // `Fill()` called with a const char* as the first argument will add a value to the bin with that name,\n", " // creating it if it doesn't exist yet.\n", " h->Fill(people[r], 1);\n", "}" ] }, { "cell_type": "markdown", "id": "5478fa14", "metadata": {}, "source": [ "Remove empty bins" ] }, { "cell_type": "code", "execution_count": 6, "id": "5da29eb1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:53.834378Z", "iopub.status.busy": "2026-05-19T20:11:53.834254Z", "iopub.status.idle": "2026-05-19T20:11:54.037911Z", "shell.execute_reply": "2026-05-19T20:11:54.037488Z" } }, "outputs": [], "source": [ "h->LabelsDeflate();\n", "\n", "auto *c1 = new TCanvas(\"c1\", \"demo bin labels\", 10, 10, 900, 500);" ] }, { "cell_type": "markdown", "id": "04878a4e", "metadata": {}, "source": [ "Enable the grid in the plot" ] }, { "cell_type": "code", "execution_count": 7, "id": "c66aabaa", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:54.049273Z", "iopub.status.busy": "2026-05-19T20:11:54.049141Z", "iopub.status.idle": "2026-05-19T20:11:54.268117Z", "shell.execute_reply": "2026-05-19T20:11:54.267555Z" } }, "outputs": [], "source": [ "c1->SetGrid();\n", "c1->SetTopMargin(0.15);" ] }, { "cell_type": "markdown", "id": "be5aad62", "metadata": {}, "source": [ "Draw the histogram" ] }, { "cell_type": "code", "execution_count": 8, "id": "38961e35", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:54.270219Z", "iopub.status.busy": "2026-05-19T20:11:54.270099Z", "iopub.status.idle": "2026-05-19T20:11:54.474160Z", "shell.execute_reply": "2026-05-19T20:11:54.473554Z" } }, "outputs": [], "source": [ "h->Draw();" ] }, { "cell_type": "markdown", "id": "a873f75c", "metadata": {}, "source": [ "Draw a boxed text\n", "\"brNDC\" = coordinates draw bottom-right shadow and pass the coordinates in normalized device coordinates" ] }, { "cell_type": "code", "execution_count": 9, "id": "456c7270", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:54.476056Z", "iopub.status.busy": "2026-05-19T20:11:54.475937Z", "iopub.status.idle": "2026-05-19T20:11:54.684725Z", "shell.execute_reply": "2026-05-19T20:11:54.679229Z" } }, "outputs": [], "source": [ "auto *pt = new TPaveText(0.7, 0.85, 0.98, 0.98, \"brNDC\");\n", "pt->SetFillColor(18);\n", "pt->SetTextAlign(12);\n", "pt->AddText(\"Use the axis Context Menu LabelsOption\");\n", "pt->AddText(\" \\\"a\\\" to sort by alphabetic order\");\n", "pt->AddText(\" \\\">\\\" to sort by decreasing values\");\n", "pt->AddText(\" \\\"<\\\" to sort by increasing values\");\n", "pt->Draw();" ] }, { "cell_type": "markdown", "id": "88df9cd5", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 10, "id": "a6eb23bc", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:54.696397Z", "iopub.status.busy": "2026-05-19T20:11:54.696247Z", "iopub.status.idle": "2026-05-19T20:11:54.900250Z", "shell.execute_reply": "2026-05-19T20:11:54.899670Z" } }, "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 }