{ "cells": [ { "cell_type": "markdown", "id": "0bcdd3c3", "metadata": {}, "source": [ "# hist007_TH1_liveupdate\n", "Simple example illustrating how to use the C++ interpreter\n", "to fill histograms in a loop and show the graphics results\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": "code", "execution_count": 1, "id": "e93eabce", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.053716Z", "iopub.status.busy": "2026-05-19T20:11:56.053572Z", "iopub.status.idle": "2026-05-19T20:11:56.394105Z", "shell.execute_reply": "2026-05-19T20:11:56.393411Z" } }, "outputs": [], "source": [ "TCanvas *c1 = new TCanvas(\"c1\", \"Live update of histograms\", 200, 10, 600, 400);\n", "c1->SetGrid();" ] }, { "cell_type": "markdown", "id": "4e1bf110", "metadata": {}, "source": [ "Create some histograms." ] }, { "cell_type": "code", "execution_count": 2, "id": "275fa7b8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.399129Z", "iopub.status.busy": "2026-05-19T20:11:56.399001Z", "iopub.status.idle": "2026-05-19T20:11:56.602359Z", "shell.execute_reply": "2026-05-19T20:11:56.601856Z" } }, "outputs": [], "source": [ "auto *total = new TH1D(\"total\", \"This is the total distribution\", 100, -4, 4);\n", "auto *main = new TH1D(\"main\", \"Main contributor\", 100, -4, 4);\n", "auto *s1 = new TH1D(\"s1\", \"This is the first signal\", 100, -4, 4);\n", "auto *s2 = new TH1D(\"s2\", \"This is the second signal\", 100, -4, 4);\n", "total->Sumw2(); // store the sum of squares of weights" ] }, { "cell_type": "markdown", "id": "2bf15e39", "metadata": {}, "source": [ "set some style properties" ] }, { "cell_type": "code", "execution_count": 3, "id": "6cb25673", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.606468Z", "iopub.status.busy": "2026-05-19T20:11:56.606348Z", "iopub.status.idle": "2026-05-19T20:11:56.810276Z", "shell.execute_reply": "2026-05-19T20:11:56.809782Z" } }, "outputs": [], "source": [ "total->SetMarkerStyle(21); // shape of the markers (\\see EMarkerStyle)\n", "total->SetMarkerSize(0.7);\n", "main->SetFillColor(16);\n", "s1->SetFillColor(42);\n", "s2->SetFillColor(46);\n", "TSlider *slider = nullptr;" ] }, { "cell_type": "markdown", "id": "ddc70a58", "metadata": {}, "source": [ "Fill histograms randomly" ] }, { "cell_type": "code", "execution_count": 4, "id": "aa165cd5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:56.823576Z", "iopub.status.busy": "2026-05-19T20:11:56.823432Z", "iopub.status.idle": "2026-05-19T20:11:57.027475Z", "shell.execute_reply": "2026-05-19T20:11:57.026741Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "TRandom3 rng;\n", "const int kUPDATE = 500;\n", "for (int i = 0; i < 10000; i++) {\n", " float xmain = rng.Gaus(-1, 1.5);\n", " float xs1 = rng.Gaus(-0.5, 0.5);\n", " float xs2 = rng.Landau(1, 0.15);\n", " main->Fill(xmain);\n", " s1->Fill(xs1, 0.3);\n", " s2->Fill(xs2, 0.2);\n", " total->Fill(xmain);\n", " total->Fill(xs1, 0.3);\n", " total->Fill(xs2, 0.2);\n", " if (i && (i % kUPDATE) == 0) {\n", " if (i == kUPDATE) {\n", " total->Draw(\"e1p\");\n", " main->Draw(\"same\");\n", " s1->Draw(\"same\");\n", " s2->Draw(\"same\");\n", " c1->Update();\n", " slider = new TSlider(\"slider\", \"test\", 4.2, 0, 4.6, total->GetMaximum(), 38);\n", " slider->SetFillColor(46);\n", " }\n", " if (slider)\n", " slider->SetRange(0., 1. * i / 10000.);\n", " c1->Modified();\n", " c1->Update();\n", " }\n", "}\n", "slider->SetRange(0., 1.);\n", "c1->Modified();" ] }, { "cell_type": "markdown", "id": "82add515", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "25c7d651", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:57.029036Z", "iopub.status.busy": "2026-05-19T20:11:57.028919Z", "iopub.status.idle": "2026-05-19T20:11:57.234876Z", "shell.execute_reply": "2026-05-19T20:11:57.234240Z" } }, "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 }