Logo ROOT   6.12/07
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 
16 int imt001_parBranchProcessing()
17 {
18  // First enable implicit multi-threading globally, so that the implicit parallelisation is on.
19  // The parameter of the call specifies the number of threads to use.
20  int nthreads = 4;
21  ROOT::EnableImplicitMT(nthreads);
22 
23  // Open the file containing the tree
24  TFile *file = TFile::Open("http://root.cern.ch/files/h1/dstarmb.root");
25 
26  // Get the tree
27  TTree *tree = nullptr;
28  file->GetObject<TTree>("h42", tree);
29 
30  // Read the branches in parallel.
31  // Note that the interface does not change, the parallelisation is internal
32  for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
33  tree->GetEntry(i); // parallel read
34  }
35 
36  // IMT parallelisation can be disabled for a specific tree
37  tree->SetImplicitMT(false);
38 
39  // If now GetEntry is invoked on the tree, the reading is sequential
40  for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
41  tree->GetEntry(i); // sequential read
42  }
43 
44  // Parallel reading can be re-enabled
45  tree->SetImplicitMT(true);
46 
47  // IMT can be also disabled globally.
48  // As a result, no tree will run GetEntry in parallel
50 
51  // This is still sequential: the global flag is disabled, even if the
52  // flag for this particular tree is enabled
53  for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
54  tree->GetEntry(i); // sequential read
55  }
56 
57  return 0;
58 }
long long Long64_t
Definition: RtypesCore.h:69
void DisableImplicitMT()
Disables the implicit multi-threading in ROOT (see EnableImplicitMT).
Definition: TROOT.cxx:570
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
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:5330
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:3950
void GetObject(const char *namecycle, T *&ptr)
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:555
virtual Long64_t GetEntries() const
Definition: TTree.h:382
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:542