{
"cells": [
{
"cell_type": "markdown",
"id": "a700ea2a",
"metadata": {},
"source": [
"# mp_processSelector\n",
"Illustrate the usage of the multiprocessing TTreeProcessorMP\n",
"and TSelector interfaces with the h1analysis.C example.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Anda Chelba, Gerardo Ganis \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": null,
"id": "3130e344",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cpp -d\n",
"\n",
"#include \"TString.h\"\n",
"#include \"TROOT.h\"\n",
"#include \"TChain.h\"\n",
"#include \"TBranch.h\"\n",
"#include \"TFileCollection.h\"\n",
"#include \"TH1F.h\"\n",
"#include \"TTreeReader.h\"\n",
"#include \"ROOT/TTreeProcessorMP.hxx\"\n",
"\n",
"#include \n",
"\n",
"const auto file0 = \"root://eospublic.cern.ch//eos/root-eos/h1/dstarmb.root\";\n",
"const std::vector files = {file0,\n",
" \"root://eospublic.cern.ch//eos/root-eos/h1/dstarp1a.root\",\n",
" \"root://eospublic.cern.ch//eos/root-eos/h1/dstarp1b.root\",\n",
" \"root://eospublic.cern.ch//eos/root-eos/h1/dstarp2.root\"};"
]
},
{
"cell_type": "markdown",
"id": "ff3e0888",
"metadata": {},
"source": [
"MacOSX may generate connection to WindowServer errors"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6379828",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gROOT->SetBatch(kTRUE);\n",
"\n",
"TString selectorPath = gROOT->GetTutorialDir();\n",
"selectorPath += \"/analysis/tree/h1analysis.C+\";\n",
"std::cout << \"selector used is: \" << selectorPath << \"\\n\";\n",
"auto sel = TSelector::GetSelector(selectorPath);"
]
},
{
"cell_type": "markdown",
"id": "8307add1",
"metadata": {},
"source": [
"The following code generates a crash when Davix is used for HTTP\n",
"Davix does not seem fork-safe; the problem has been reported to the\n",
"Davix developers. For the time being we disable this part.\n",
"To repoduce the problem, uncomment the next line.\n",
"\n",
"#define __reproduce_davix"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f98fa495",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"#if defined(__reproduce_davix)\n",
"auto fp = std::make_unique(TFile::Open(file0));\n",
"auto tree = fp->Get(\"h42\");\n",
"#endif\n",
"\n",
"ROOT::TTreeProcessorMP pool(3);\n",
"\n",
"TList *out = nullptr;\n",
"#if defined(__reproduce_davix)"
]
},
{
"cell_type": "markdown",
"id": "252dd1fc",
"metadata": {},
"source": [
"TTreeProcessorMP::Process with a single tree"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8d681828",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"out = pool.Process(*tree, *sel);\n",
"sel->GetOutputList()->Delete();\n",
"#endif"
]
},
{
"cell_type": "markdown",
"id": "ff7b22cb",
"metadata": {},
"source": [
"TTreeProcessorMP::Process with single file name and tree name\n",
"Note: we have less files than workers here"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "957068cc",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"out = pool.Process(file0, *sel, \"h42\");\n",
"sel->GetOutputList()->Delete();"
]
},
{
"cell_type": "markdown",
"id": "2f24bb20",
"metadata": {},
"source": [
"Prepare datasets: vector of files, TFileCollection"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb6d2803",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TChain ch;\n",
"TFileCollection fc;\n",
"for (auto &&file : files) {\n",
" fc.Add(new TFileInfo(file.c_str()));\n",
" ch.Add(file.c_str());\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "3f78da54",
"metadata": {},
"source": [
"TTreeProcessorMP::Process with vector of files and tree name\n",
"Note: we have more files than workers here (different behaviour)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b9ac6aa",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"out = pool.Process(files, *sel, \"h42\");\n",
"sel->GetOutputList()->Delete();"
]
},
{
"cell_type": "markdown",
"id": "3937e82b",
"metadata": {},
"source": [
"TTreeProcessorMP::Process with TFileCollection, no tree name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28315597",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"out = pool.Process(fc, *sel);\n",
"sel->GetOutputList()->Delete();"
]
},
{
"cell_type": "markdown",
"id": "4c22fd34",
"metadata": {},
"source": [
"TTreeProcessorMP::Process with TChain, no tree name"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f95d785d",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"out = pool.Process(ch, *sel);\n",
"sel->GetOutputList()->Delete();\n",
"\n",
"return 0;"
]
}
],
"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
}