{ "cells": [ { "cell_type": "markdown", "id": "7eb68a0d", "metadata": {}, "source": [ "# mtbb_fillHistos\n", "Fill histograms in parallel and write them on file\n", "with a multithreaded approach using\n", "TThreadExecutor and TExecutor::Map.\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": "33839635", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:14.008502Z", "iopub.status.busy": "2026-05-19T20:11:14.008259Z", "iopub.status.idle": "2026-05-19T20:11:14.334710Z", "shell.execute_reply": "2026-05-19T20:11:14.333760Z" } }, "outputs": [], "source": [ "const UInt_t nNumbers = 20000000U;\n", "\n", "const UInt_t nThreads = 4U;" ] }, { "cell_type": "markdown", "id": "420e4c88", "metadata": {}, "source": [ "We define our work item" ] }, { "cell_type": "code", "execution_count": 2, "id": "3debc206", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:14.343004Z", "iopub.status.busy": "2026-05-19T20:11:14.342873Z", "iopub.status.idle": "2026-05-19T20:11:14.575962Z", "shell.execute_reply": "2026-05-19T20:11:14.557387Z" } }, "outputs": [], "source": [ "auto workItem = [](UInt_t workerID) {\n", " // One generator, file and ntuple per worker\n", " TRandom3 workerRndm(workerID); // Change the seed\n", " TFile f(Form(\"myFile_mtbb001_%u.root\", workerID), \"RECREATE\");\n", " TH1F h(Form(\"myHisto_%u\", workerID), \"The Histogram\", 64, -4, 4);\n", " for (UInt_t i = 0; i < nNumbers; ++i) {\n", " h.Fill(workerRndm.Gaus());\n", " }\n", " h.Write();\n", " return 0;\n", "};" ] }, { "cell_type": "markdown", "id": "c38e39c2", "metadata": {}, "source": [ "Create the pool of threads" ] }, { "cell_type": "code", "execution_count": 3, "id": "6e899b5c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:14.580395Z", "iopub.status.busy": "2026-05-19T20:11:14.580250Z", "iopub.status.idle": "2026-05-19T20:11:14.787082Z", "shell.execute_reply": "2026-05-19T20:11:14.786633Z" } }, "outputs": [], "source": [ "ROOT::TThreadExecutor pool(nThreads);" ] }, { "cell_type": "markdown", "id": "332ba117", "metadata": {}, "source": [ "Fill the pool with work" ] }, { "cell_type": "code", "execution_count": 4, "id": "9d0235e6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:14.796198Z", "iopub.status.busy": "2026-05-19T20:11:14.796065Z", "iopub.status.idle": "2026-05-19T20:11:15.774533Z", "shell.execute_reply": "2026-05-19T20:11:15.773831Z" } }, "outputs": [], "source": [ "pool.Map(workItem, ROOT::TSeqI(nThreads));\n", "return 0;" ] }, { "cell_type": "markdown", "id": "d9d84a25", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "794690f0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:11:15.776543Z", "iopub.status.busy": "2026-05-19T20:11:15.776388Z", "iopub.status.idle": "2026-05-19T20:11:15.979826Z", "shell.execute_reply": "2026-05-19T20:11:15.979226Z" } }, "outputs": [], "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 }