Logo ROOT   6.07/09
Reference Guide
imt001_parBranchProcessing.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_multicore
3 /// Demonstrate how to activate and use the implicit parallelisation of TTree::GetEntry.
4 /// Such parallelisation creates one task per top-level branch of the tree being read.
5 /// In this example, most of the branches are floating point numbers, which are very fast to read.
6 /// This parallelisation can be used, though, on bigger trees with many (complex) branches, which
7 /// are more likely to benefit from speedup gains.
8 ///
9 /// \macro_code
10 ///
11 /// \author Enric Tejedor
12 /// \date 26/09/2016
13 
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  TFile *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  // Read the branches in parallel.
30  // Note that the interface does not change, the parallelisation is internal
31  for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
32  tree->GetEntry(i); // parallel read
33  }
34 
35  // IMT parallelisation can be disabled for a specific tree
36  tree->SetImplicitMT(false);
37 
38  // If now GetEntry is invoked on the tree, the reading is sequential
39  for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
40  tree->GetEntry(i); // sequential read
41  }
42 
43  // Parallel reading can be re-enabled
44  tree->SetImplicitMT(true);
45 
46  // IMT can be also disabled globally.
47  // As a result, no tree will run GetEntry in parallel
49 
50  // This is still sequential: the global flag is disabled, even if the
51  // flag for this particular tree is enabled
52  for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
53  tree->GetEntry(i); // sequential read
54  }
55 
56  return 0;
57 }
long long Long64_t
Definition: RtypesCore.h:69
void DisableImplicitMT()
Disables the implicit multi-threading in ROOT.
Definition: TROOT.cxx:532
void EnableImplicitMT(UInt_t numthreads=0)
Globally enables the implicit multi-threading in ROOT, activating the parallel execution of those met...
Definition: TROOT.cxx:518
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
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:5210
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:3871
void GetObject(const char *namecycle, T *&ptr)
Definition: file.py:1
Definition: tree.py:1
virtual Long64_t GetEntries() const
Definition: TTree.h:392
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual void SetImplicitMT(Bool_t enabled)
Definition: TTree.h:549