Demonstrate how to activate and use the implicit parallelisation of TTree::GetEntry. Such parallelisation creates one task per top-level branch of the tree being read. In this example, most of the branches are floating point numbers, which are very fast to read. This parallelisation can be used, though, on bigger trees with many (complex) branches, which are more likely to benefit from speedup gains.

Author: Enric Tejedor
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Sunday, October 02, 2022 at 09:33 AM.

First enable implicit multi-threading globally, so that the implicit parallelisation is on. The parameter of the call specifies the number of threads to use.

In [1]:
int nthreads = 4;

Open the file containing the tree

In [2]:
auto file = TFile::Open("");

Get the tree

In [3]:
auto tree = file->Get<TTree>("h42");

const auto nEntries = tree->GetEntries();

Read the branches in parallel. Note that the interface does not change, the parallelisation is internal

In [4]:
for (auto i : ROOT::TSeqUL(nEntries)) {
   tree->GetEntry(i); // parallel read

IMT parallelisation can be disabled for a specific tree

In [5]:

If now GetEntry is invoked on the tree, the reading is sequential

In [6]:
for (auto i : ROOT::TSeqUL(nEntries)) {
   tree->GetEntry(i); // sequential read

Parallel reading can be re-enabled

In [7]:

IMT can be also disabled globally. As a result, no tree will run GetEntry in parallel

In [8]:

This is still sequential: the global flag is disabled, even if the flag for this particular tree is enabled

In [9]:
for (auto i : ROOT::TSeqUL(nEntries)) {
   tree->GetEntry(i); // sequential read

return 0;

Draw all canvases

In [10]: