{ "cells": [ { "cell_type": "markdown", "id": "d3d40b54", "metadata": {}, "source": [ "# hist006_TH1_bar_charts\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": "4b699033", "metadata": {}, "source": [ "Try to open first the file cernstaff.root in tutorials/io/tree directory" ] }, { "cell_type": "code", "execution_count": 1, "id": "42e8ea78", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:55.296104Z", "iopub.status.busy": "2026-05-19T20:11:55.296012Z", "iopub.status.idle": "2026-05-19T20:11:55.611237Z", "shell.execute_reply": "2026-05-19T20:11:55.610641Z" } }, "outputs": [], "source": [ "TString filedir = gROOT->GetTutorialDir();\n", "filedir += TString(\"/io/tree/\");\n", "TString filename = \"cernstaff.root\";" ] }, { "cell_type": "markdown", "id": "5010ce17", "metadata": {}, "source": [ "Note that `AccessPathName` returns 0 (false) on success!" ] }, { "cell_type": "code", "execution_count": 2, "id": "0ac68648", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:55.612950Z", "iopub.status.busy": "2026-05-19T20:11:55.612819Z", "iopub.status.idle": "2026-05-19T20:11:55.816085Z", "shell.execute_reply": "2026-05-19T20:11:55.815468Z" } }, "outputs": [], "source": [ "bool fileNotFound = gSystem->AccessPathName(filename); " ] }, { "cell_type": "markdown", "id": "8abe8a9b", "metadata": {}, "source": [ "If the file is not found try to generate it using the macro io/tree/tree500_cernbuild.C" ] }, { "cell_type": "code", "execution_count": 3, "id": "04249ec5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:55.817502Z", "iopub.status.busy": "2026-05-19T20:11:55.817388Z", "iopub.status.idle": "2026-05-19T20:11:56.101747Z", "shell.execute_reply": "2026-05-19T20:11:56.101116Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "******************************************************************************\n", "*Tree :T : CERN 1988 staff data *\n", "*Entries : 3354 : Total = 176339 bytes File Size = 19066 *\n", "* : : Tree compression factor = 2.16 *\n", "******************************************************************************\n", "*Br 0 :Category : Category/I *\n", "*Entries : 3354 : Total Size= 14073 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 1 :Flag : Flag/i *\n", "*Entries : 3354 : Total Size= 14049 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 2 :Age : Age/I *\n", "*Entries : 3354 : Total Size= 14043 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 3 :Service : Service/I *\n", "*Entries : 3354 : Total Size= 14067 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 4 :Children : Children/I *\n", "*Entries : 3354 : Total Size= 14073 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 5 :Grade : Grade/I *\n", "*Entries : 3354 : Total Size= 14055 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 6 :Step : Step/I *\n", "*Entries : 3354 : Total Size= 14049 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 7 :Hrweek : Hrweek/I *\n", "*Entries : 3354 : Total Size= 14061 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 8 :Cost : Cost/I *\n", "*Entries : 3354 : Total Size= 14049 bytes One basket in memory *\n", "*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *\n", "*............................................................................*\n", "*Br 9 :Division : Division/C *\n", "*Entries : 3354 : Total Size= 25326 bytes File Size = 9748 *\n", "*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.13 *\n", "*............................................................................*\n", "*Br 10 :Nation : Nation/C *\n", "*Entries : 3354 : Total Size= 24209 bytes File Size = 9318 *\n", "*Baskets : 1 : Basket Size= 32000 bytes Compression= 2.19 *\n", "*............................................................................*\n" ] } ], "source": [ "if (fileNotFound) {\n", " TString macroName = filedir + \"tree500_cernbuild.C\";\n", " if (!gInterpreter->IsLoaded(macroName)) gInterpreter->LoadMacro(macroName);\n", " gROOT->ProcessLineFast(\"tree500_cernbuild()\");\n", "}\n", "\n", "auto file = std::unique_ptr(TFile::Open(filename, \"READ\"));\n", "if (!file) {\n", " Error(\"hist006_TH1_bar_charts\", \"file cernstaff.root not found\");\n", " return;\n", "}" ] }, { "cell_type": "markdown", "id": "07e73f2e", "metadata": {}, "source": [ "Retrieve the TTree named \"T\" contained in the file" ] }, { "cell_type": "code", "execution_count": 4, "id": "b9624c56", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.103214Z", "iopub.status.busy": "2026-05-19T20:11:56.103068Z", "iopub.status.idle": "2026-05-19T20:11:56.307479Z", "shell.execute_reply": "2026-05-19T20:11:56.306906Z" } }, "outputs": [], "source": [ "auto tree = file->Get(\"T\");\n", "if (!tree) {\n", " Error(\"hist006_TH1_bar_charts\", \"Tree T is not present in file %s\", file->GetName());\n", " return;\n", "}\n", "tree->SetFillColor(45);" ] }, { "cell_type": "markdown", "id": "3264b337", "metadata": {}, "source": [ "Create the canvas to draw on" ] }, { "cell_type": "code", "execution_count": 5, "id": "3ef2cb16", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.316290Z", "iopub.status.busy": "2026-05-19T20:11:56.316127Z", "iopub.status.idle": "2026-05-19T20:11:56.521050Z", "shell.execute_reply": "2026-05-19T20:11:56.520480Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\",\"histograms with bars\", 700, 800);\n", "c1->SetFillColor(42);" ] }, { "cell_type": "markdown", "id": "c06a30f2", "metadata": {}, "source": [ "Divide it vertically in 2 sections" ] }, { "cell_type": "code", "execution_count": 6, "id": "33a97336", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.526227Z", "iopub.status.busy": "2026-05-19T20:11:56.526061Z", "iopub.status.idle": "2026-05-19T20:11:56.730075Z", "shell.execute_reply": "2026-05-19T20:11:56.729428Z" } }, "outputs": [], "source": [ "int ndivsX = 1;\n", "int ndivsY = 2;\n", "c1->Divide(ndivsX, ndivsY);" ] }, { "cell_type": "markdown", "id": "ccc04c0c", "metadata": {}, "source": [ "Horizontal bar chart" ] }, { "cell_type": "code", "execution_count": 7, "id": "3e0fc426", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.731589Z", "iopub.status.busy": "2026-05-19T20:11:56.731428Z", "iopub.status.idle": "2026-05-19T20:11:56.936220Z", "shell.execute_reply": "2026-05-19T20:11:56.935501Z" } }, "outputs": [], "source": [ "auto *curPad = c1->cd(1); // select top section. Section 1 is the first sub-section.\n", "curPad->SetGrid();\n", "curPad->SetLogx();\n", "curPad->SetFrameFillColor(33);" ] }, { "cell_type": "markdown", "id": "2bea5268", "metadata": {}, "source": [ "Use the \"hbar2\" option to draw the tree as a horizontal bar chart" ] }, { "cell_type": "code", "execution_count": 8, "id": "059d467c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.937837Z", "iopub.status.busy": "2026-05-19T20:11:56.937646Z", "iopub.status.idle": "2026-05-19T20:11:57.141647Z", "shell.execute_reply": "2026-05-19T20:11:57.140570Z" } }, "outputs": [], "source": [ "tree->Draw(\"Nation\",\"\",\"hbar2\");" ] }, { "cell_type": "markdown", "id": "1ecca5b0", "metadata": {}, "source": [ "Vertical bar chart" ] }, { "cell_type": "code", "execution_count": 9, "id": "4a2e2f94", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:57.143551Z", "iopub.status.busy": "2026-05-19T20:11:57.143399Z", "iopub.status.idle": "2026-05-19T20:11:57.347690Z", "shell.execute_reply": "2026-05-19T20:11:57.347146Z" } }, "outputs": [], "source": [ "curPad = c1->cd(2);\n", "curPad->SetGrid();\n", "curPad->SetFrameFillColor(33);" ] }, { "cell_type": "markdown", "id": "ee549a38", "metadata": {}, "source": [ "This line makes the TTree draw its \"Division\" branch to a new histogram called \"hDiv\".\n", "We use \"goff\" because we don't want to really draw it to screen but we are only interested\n", "in generating the histogram from it (which we'll display ourselves later)." ] }, { "cell_type": "code", "execution_count": 10, "id": "54b5cf2a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:57.348984Z", "iopub.status.busy": "2026-05-19T20:11:57.348872Z", "iopub.status.idle": "2026-05-19T20:11:57.551692Z", "shell.execute_reply": "2026-05-19T20:11:57.551173Z" } }, "outputs": [], "source": [ "tree->Draw(\"Division>>hDiv\",\"\",\"goff\");" ] }, { "cell_type": "markdown", "id": "d2e730fa", "metadata": {}, "source": [ "Retrieve the generated histogram" ] }, { "cell_type": "code", "execution_count": 11, "id": "63922519", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:57.552990Z", "iopub.status.busy": "2026-05-19T20:11:57.552878Z", "iopub.status.idle": "2026-05-19T20:11:57.756349Z", "shell.execute_reply": "2026-05-19T20:11:57.755844Z" } }, "outputs": [], "source": [ "TH1F *hDiv = file->Get(\"hDiv\");\n", "hDiv->SetStats(0);" ] }, { "cell_type": "markdown", "id": "785c0404", "metadata": {}, "source": [ "Clone the histogram into a new one called \"hDivFR\"." ] }, { "cell_type": "code", "execution_count": 12, "id": "511ae756", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:57.757673Z", "iopub.status.busy": "2026-05-19T20:11:57.757540Z", "iopub.status.idle": "2026-05-19T20:11:57.960333Z", "shell.execute_reply": "2026-05-19T20:11:57.959833Z" } }, "outputs": [], "source": [ "TH1F *hDivFR = static_cast(hDiv->Clone(\"hDivFR\"));" ] }, { "cell_type": "markdown", "id": "dd9e858a", "metadata": {}, "source": [ "Overwrite the contents of the newly-cloned histogram to only keep the entries matching our\n", "selection (second argument of TTree::Draw())." ] }, { "cell_type": "code", "execution_count": 13, "id": "92cc325b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:57.961558Z", "iopub.status.busy": "2026-05-19T20:11:57.961447Z", "iopub.status.idle": "2026-05-19T20:11:58.164313Z", "shell.execute_reply": "2026-05-19T20:11:58.163784Z" } }, "outputs": [], "source": [ "tree->Draw(\"Division>>hDivFR\",\"Nation==\\\"FR\\\"\",\"goff\");" ] }, { "cell_type": "markdown", "id": "db8c92b3", "metadata": {}, "source": [ "Now draw both histograms side-by-side (\"same\" option) as vertical bar charts (\"bar2\" option)" ] }, { "cell_type": "code", "execution_count": 14, "id": "cea9a1d2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:58.165591Z", "iopub.status.busy": "2026-05-19T20:11:58.165455Z", "iopub.status.idle": "2026-05-19T20:11:58.369025Z", "shell.execute_reply": "2026-05-19T20:11:58.368509Z" } }, "outputs": [], "source": [ "hDiv->SetBarWidth(0.45);\n", "hDiv->SetBarOffset(0.1);\n", "hDiv->SetFillColor(49);\n", "TH1 *h1 = hDiv->DrawCopy(\"bar2\");\n", "hDivFR->SetBarWidth(0.4);\n", "hDivFR->SetBarOffset(0.55);\n", "hDivFR->SetFillColor(50);\n", "TH1 *h2 = hDivFR->DrawCopy(\"bar2,same\");\n", "\n", "TLegend *legend = new TLegend(0.55,0.65,0.76,0.82);\n", "legend->AddEntry(h1,\"All nations\",\"f\");\n", "legend->AddEntry(h2,\"French only\",\"f\");\n", "legend->Draw();\n", "\n", "c1->cd();" ] }, { "cell_type": "markdown", "id": "cc005f61", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 15, "id": "ab3113fa", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:58.370265Z", "iopub.status.busy": "2026-05-19T20:11:58.370156Z", "iopub.status.idle": "2026-05-19T20:11:58.573502Z", "shell.execute_reply": "2026-05-19T20:11:58.573038Z" } }, "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 }