Logo ROOT   6.14/05
Reference Guide
imt001_parBranchProcessing.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// \notebook
4 /// Demonstrate how to activate and use the implicit parallelisation of TTree::GetEntry.
5 /// Such parallelisation creates one task per top-level branch of the tree being read.
6 /// In this example, most of the branches are floating point numbers, which are very fast to read.
7 /// This parallelisation can be used, though, on bigger trees with many (complex) branches, which
8 /// are more likely to benefit from speedup gains.
9 ///
10 /// \macro_code
11 ///
12 /// \date 26/09/2016
13 /// \author Enric Tejedor
14 
15 int imt001_parBranchProcessing()
16 {
17  // First enable implicit multi-threading globally, so that the implicit parallelisation is on.
18  // The parameter of the call specifies the number of threads to use.
19  int nthreads = 4;
20  ROOT::EnableImplicitMT(nthreads);
21 
22  // Open the file containing the tree
23  auto file = TFile::Open("http://root.cern.ch/files/h1/dstarmb.root");
24 
25  // Get the tree
26  TTree *tree = nullptr;
27  file->GetObject<TTree>("h42", tree);
28 
29  const auto nEntries = tree->GetEntries();
30 
31  // Read the branches in parallel.
32  // Note that the interface does not change, the parallelisation is internal
33  for (auto i : ROOT::TSeqUL(nEntries)) {
34  tree->GetEntry(i); // parallel read
35  }
36 
37  // IMT parallelisation can be disabled for a specific tree
38  tree->SetImplicitMT(false);
39 
40  // If now GetEntry is invoked on the tree, the reading is sequential
41  for (auto i : ROOT::TSeqUL(nEntries)) {
42  tree->GetEntry(i); // sequential read
43  }
44 
45  // Parallel reading can be re-enabled
46  tree->SetImplicitMT(true);
47 
48  // IMT can be also disabled globally.
49  // As a result, no tree will run GetEntry in parallel
51 
52  // This is still sequential: the global flag is disabled, even if the
53  // flag for this particular tree is enabled
54  for (auto i : ROOT::TSeqUL(nEntries)) {
55  tree->GetEntry(i); // sequential read
56  }
57 
58  return 0;
59 }
void DisableImplicitMT()
Disables the implicit multi-threading in ROOT (see EnableImplicitMT).
Definition: TROOT.cxx:593
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5363
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3976
void EnableImplicitMT(UInt_t numthreads=0)
Enable ROOT&#39;s implicit multi-threading for all objects and methods that provide an internal paralleli...
Definition: TROOT.cxx:576
A pseudo container class which is a generator of indices.
Definition: TSeq.hxx:66
virtual Long64_t GetEntries() const
Definition: TTree.h:384
Definition: file.py:1
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:70
virtual void SetImplicitMT(Bool_t enabled)
Definition: TTree.h:546