{ "cells": [ { "cell_type": "markdown", "id": "21fa6db0", "metadata": {}, "source": [ "# mp_parallelHistoFill\n", "Fill histogram in parallel with a multiprocessing approach\n", "using TProcessExecutor 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:10 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "2ea016ef", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:01.119163Z", "iopub.status.busy": "2026-05-19T20:11:01.119045Z", "iopub.status.idle": "2026-05-19T20:11:01.733556Z", "shell.execute_reply": "2026-05-19T20:11:01.733099Z" } }, "outputs": [], "source": [ "const UInt_t poolSize = 4U;" ] }, { "cell_type": "code", "execution_count": 2, "id": "d5cd9aec", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:01.756629Z", "iopub.status.busy": "2026-05-19T20:11:01.756439Z", "iopub.status.idle": "2026-05-19T20:11:02.604153Z", "shell.execute_reply": "2026-05-19T20:11:02.589008Z" } }, "outputs": [], "source": [ "TDirectory::TContext ctx{nullptr}; // Don't register histograms to the current directory\n", "ROOT::TProcessExecutor 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": "0838df70", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 3, "id": "8498dcc9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:02.635273Z", "iopub.status.busy": "2026-05-19T20:11:02.635078Z", "iopub.status.idle": "2026-05-19T20:11:02.939250Z", "shell.execute_reply": "2026-05-19T20:11:02.922980Z" } }, "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 }