{ "cells": [ { "cell_type": "markdown", "id": "cd94b7a0", "metadata": {}, "source": [ "# mtbb_parallelHistoFill\n", "Fill histogram in parallel with a multithreaded approach\n", "using TThreadExecutor and TExecutor::MapReduce.\n", "\n", "\n", "\n", "\n", "**Author:** Danilo Piparo. \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": "1bb3e13b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:13.946751Z", "iopub.status.busy": "2026-05-19T20:11:13.946619Z", "iopub.status.idle": "2026-05-19T20:11:14.285929Z", "shell.execute_reply": "2026-05-19T20:11:14.280560Z" } }, "outputs": [], "source": [ "const UInt_t poolSize = 4U;" ] }, { "cell_type": "code", "execution_count": 2, "id": "0985acc8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:14.295439Z", "iopub.status.busy": "2026-05-19T20:11:14.295287Z", "iopub.status.idle": "2026-05-19T20:11:14.714150Z", "shell.execute_reply": "2026-05-19T20:11:14.713807Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n", "Warning in : Replacing existing TH1: myHist (Potential memory leak).\n" ] } ], "source": [ "TDirectory::TContext ctx{nullptr}; // Don't register histograms to the current directory\n", "ROOT::TThreadExecutor pool(poolSize);\n", "auto fillRandomHisto = [](int seed = 0) {\n", " TRandom3 rndm(seed);\n", " auto h = new TH1F(\"myHist\", \"Filled in parallel\", 128, -8, 8);\n", " for (auto i : ROOT::TSeqI(1000000)) {\n", " h->Fill(rndm.Gaus(0, 1));\n", " }\n", " return h;\n", "};\n", "\n", "auto seeds = ROOT::TSeqI(23);\n", "ROOT::ExecutorUtils::ReduceObjects redfunc;\n", "auto sumRandomHisto = pool.MapReduce(fillRandomHisto, seeds, redfunc);\n", "\n", "auto c = new TCanvas();\n", "sumRandomHisto->Draw();\n", "return 0;" ] }, { "cell_type": "markdown", "id": "2dbefd30", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 3, "id": "266563d8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:14.716237Z", "iopub.status.busy": "2026-05-19T20:11:14.716113Z", "iopub.status.idle": "2026-05-19T20:11:14.930034Z", "shell.execute_reply": "2026-05-19T20:11:14.929635Z" } }, "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 }