{ "cells": [ { "cell_type": "markdown", "id": "fe2247cd", "metadata": {}, "source": [ "# mp_readNtuplesFillHistosAndFit\n", "Read n-tuples in distinct workers, fill histograms, merge them and fit.\n", "We express parallelism with multiprocessing as it is done with multithreading\n", "in mt102_readNtuplesFillHistosAndFit.\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:17 PM." ] }, { "cell_type": "markdown", "id": "ca688b6a", "metadata": {}, "source": [ "No nuisance for batch execution" ] }, { "cell_type": "code", "execution_count": 1, "id": "fe1bf2aa", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:16.209504Z", "iopub.status.busy": "2026-05-19T20:17:16.209393Z", "iopub.status.idle": "2026-05-19T20:17:16.525736Z", "shell.execute_reply": "2026-05-19T20:17:16.524983Z" } }, "outputs": [], "source": [ "gROOT->SetBatch();" ] }, { "cell_type": "markdown", "id": "09f68675", "metadata": {}, "source": [ "---------------------------------------\n", "Perform the operation sequentially" ] }, { "cell_type": "code", "execution_count": 2, "id": "0c41853e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:16.527839Z", "iopub.status.busy": "2026-05-19T20:17:16.527715Z", "iopub.status.idle": "2026-05-19T20:17:17.936175Z", "shell.execute_reply": "2026-05-19T20:17:17.935803Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 129.456\n", "NDf = 125\n", "Edm = 4.32478e-10\n", "NCalls = 58\n", "Constant = 498625 +/- 136.705 \n", "Mean = -3.42949e-05 +/- 0.000223757 \n", "Sigma = 1.0001 +/- 0.000158812 \t (limited)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Info in : created default TCanvas with name c1\n" ] } ], "source": [ "TChain inputChain(\"multiCore\");\n", "inputChain.Add(\"mp101_multiCore_*.root\");\n", "if (inputChain.GetNtrees() <= 0) {\n", " Printf(\" No files in the TChain: did you run mp101_fillNtuples.C before?\");\n", " return 1;\n", "}\n", "TH1F outHisto(\"outHisto\", \"Random Numbers\", 128, -4, 4);\n", "inputChain.Draw(\"r >> outHisto\");\n", "outHisto.Fit(\"gaus\");" ] }, { "cell_type": "markdown", "id": "2e72c4f2", "metadata": {}, "source": [ "---------------------------------------\n", "We now go MP!\n", "TProcessExecutor offers an interface to directly process trees and chains without\n", "the need for the user to go through the low level implementation of a\n", "map-reduce." ] }, { "cell_type": "markdown", "id": "746738f0", "metadata": {}, "source": [ "We adapt our parallelisation to the number of input files" ] }, { "cell_type": "code", "execution_count": 3, "id": "ef656715", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:17.938335Z", "iopub.status.busy": "2026-05-19T20:17:17.938216Z", "iopub.status.idle": "2026-05-19T20:17:18.143409Z", "shell.execute_reply": "2026-05-19T20:17:18.142732Z" } }, "outputs": [], "source": [ "const auto nFiles = inputChain.GetListOfFiles()->GetEntries();" ] }, { "cell_type": "markdown", "id": "7ef45936", "metadata": {}, "source": [ "This is the function invoked during the processing of the trees." ] }, { "cell_type": "code", "execution_count": 4, "id": "3395882e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:18.145275Z", "iopub.status.busy": "2026-05-19T20:17:18.145151Z", "iopub.status.idle": "2026-05-19T20:17:18.347540Z", "shell.execute_reply": "2026-05-19T20:17:18.346826Z" } }, "outputs": [], "source": [ "auto workItem = [](TTreeReader &reader) {\n", " TTreeReaderValue randomRV(reader, \"r\");\n", " auto partialHisto = new TH1F(\"outHistoMP\", \"Random Numbers\", 128, -4, 4);\n", " while (reader.Next()) {\n", " partialHisto->Fill(*randomRV);\n", " }\n", " return partialHisto;\n", "};" ] }, { "cell_type": "markdown", "id": "d5591931", "metadata": {}, "source": [ "Create the pool of processes" ] }, { "cell_type": "code", "execution_count": 5, "id": "5e8f390a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:18.349708Z", "iopub.status.busy": "2026-05-19T20:17:18.349570Z", "iopub.status.idle": "2026-05-19T20:17:18.554717Z", "shell.execute_reply": "2026-05-19T20:17:18.554017Z" } }, "outputs": [], "source": [ "ROOT::TTreeProcessorMP workers(nFiles);" ] }, { "cell_type": "markdown", "id": "0495e85e", "metadata": {}, "source": [ "Process the TChain" ] }, { "cell_type": "code", "execution_count": 6, "id": "0a836a62", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:18.557134Z", "iopub.status.busy": "2026-05-19T20:17:18.557013Z", "iopub.status.idle": "2026-05-19T20:17:19.293856Z", "shell.execute_reply": "2026-05-19T20:17:19.293157Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "****************************************\n", "Minimizer is Minuit2 / Migrad\n", "Chi2 = 129.456\n", "NDf = 125\n", "Edm = 4.3374e-10\n", "NCalls = 50\n", "Constant = 498625 +/- 136.705 \n", "Mean = -3.42949e-05 +/- 0.000223757 \n", "Sigma = 1.0001 +/- 0.000158812 \t (limited)\n" ] } ], "source": [ "auto sumHistogram = workers.Process(inputChain, workItem, \"multiCore\");\n", "sumHistogram->Fit(\"gaus\", 0);\n", "\n", "return 0;" ] }, { "cell_type": "markdown", "id": "6d196f60", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "2cb2ed23", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:19.296009Z", "iopub.status.busy": "2026-05-19T20:17:19.295880Z", "iopub.status.idle": "2026-05-19T20:17:19.520430Z", "shell.execute_reply": "2026-05-19T20:17:19.519756Z" } }, "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 }