{
"cells": [
{
"cell_type": "markdown",
"id": "3fa0020a",
"metadata": {},
"source": [
"# mt_fillHistos\n",
"Fill histograms in parallel and write them on file\n",
"with a multithreaded approach using std::thread.\n",
"This is the simplest meaningful example which shows \n",
"ROOT thread awareness.\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": "56c76c41",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:10.987269Z",
"iopub.status.busy": "2026-05-19T20:11:10.987100Z",
"iopub.status.idle": "2026-05-19T20:11:11.508672Z",
"shell.execute_reply": "2026-05-19T20:11:11.487013Z"
}
},
"outputs": [],
"source": [
"const UInt_t nNumbers = 20000000U;\n",
"\n",
"const UInt_t nWorkers = 4U;"
]
},
{
"cell_type": "markdown",
"id": "3f2371c4",
"metadata": {},
"source": [
"The first, fundamental operation to be performed in order to make ROOT\n",
"thread-aware."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "eeb198ab",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:11.530263Z",
"iopub.status.busy": "2026-05-19T20:11:11.530104Z",
"iopub.status.idle": "2026-05-19T20:11:11.744528Z",
"shell.execute_reply": "2026-05-19T20:11:11.743881Z"
}
},
"outputs": [],
"source": [
"ROOT::EnableThreadSafety();"
]
},
{
"cell_type": "markdown",
"id": "a31309a4",
"metadata": {},
"source": [
"We define our work item"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "bd0670c3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:11.746157Z",
"iopub.status.busy": "2026-05-19T20:11:11.746034Z",
"iopub.status.idle": "2026-05-19T20:11:11.948759Z",
"shell.execute_reply": "2026-05-19T20:11:11.947572Z"
}
},
"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_mt001_%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",
"};"
]
},
{
"cell_type": "markdown",
"id": "785f07ea",
"metadata": {},
"source": [
"Create the collection which will hold the threads, our \"pool\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "068ce2e0",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:11.950193Z",
"iopub.status.busy": "2026-05-19T20:11:11.950065Z",
"iopub.status.idle": "2026-05-19T20:11:12.153903Z",
"shell.execute_reply": "2026-05-19T20:11:12.153207Z"
}
},
"outputs": [],
"source": [
"std::vector workers;"
]
},
{
"cell_type": "markdown",
"id": "472c7c66",
"metadata": {},
"source": [
"Fill the \"pool\" with workers"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a485c20f",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:12.155258Z",
"iopub.status.busy": "2026-05-19T20:11:12.155141Z",
"iopub.status.idle": "2026-05-19T20:11:12.365083Z",
"shell.execute_reply": "2026-05-19T20:11:12.364707Z"
}
},
"outputs": [],
"source": [
"for (auto workerID : ROOT::TSeqI(nWorkers)) {\n",
" workers.emplace_back(workItem, workerID);\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "097fe654",
"metadata": {},
"source": [
"Now join them"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ef935a1a",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:12.401447Z",
"iopub.status.busy": "2026-05-19T20:11:12.401299Z",
"iopub.status.idle": "2026-05-19T20:11:13.325028Z",
"shell.execute_reply": "2026-05-19T20:11:13.318362Z"
}
},
"outputs": [],
"source": [
"for (auto &&worker : workers)\n",
" worker.join();\n",
"\n",
"return 0;"
]
},
{
"cell_type": "markdown",
"id": "35f0ac0e",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "44721299",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:11:13.330409Z",
"iopub.status.busy": "2026-05-19T20:11:13.330262Z",
"iopub.status.idle": "2026-05-19T20:11:13.543307Z",
"shell.execute_reply": "2026-05-19T20:11:13.542827Z"
}
},
"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
}