Logo ROOT   6.10/09
Reference Guide
TTreeProcessorMT.cxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Enric Tejedor, CERN 12/09/2016
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2016, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class ROOT::TTreeProcessorMT
13  \brief A class to process the entries of a TTree in parallel.
14 
15 By means of its Process method, ROOT::TTreeProcessorMT provides a way to process the
16 entries of a TTree in parallel. When invoking TTreeProcessor::Process, the user
17 passes a function whose only parameter is a TTreeReader. The function iterates
18 on a subrange of entries by using that TTreeReader.
19 
20 The implementation of ROOT::TTreeProcessorMT parallelizes the processing of the subranges,
21 each corresponding to a cluster in the TTree. This is possible thanks to the use
22 of a ROOT::TThreadedObject, so that each thread works with its own TFile and TTree
23 objects.
24 */
25 
26 #include "TROOT.h"
28 #include "ROOT/TThreadExecutor.hxx"
29 
30 using namespace ROOT;
31 
32 ////////////////////////////////////////////////////////////////////////
33 /// Constructor based on a file name.
34 /// \param[in] filename Name of the file containing the tree to process.
35 /// \param[in] treename Name of the tree to process. If not provided,
36 /// the implementation will automatically search for a
37 /// tree in the file.
38 TTreeProcessorMT::TTreeProcessorMT(std::string_view filename, std::string_view treename) : treeView(filename, treename) {}
39 
40 ////////////////////////////////////////////////////////////////////////
41 /// Constructor based on a collection of file names.
42 /// \param[in] filenames Collection of the names of the files containing the tree to process.
43 /// \param[in] treename Name of the tree to process. If not provided,
44 /// the implementation will automatically search for a
45 /// tree in the collection of files.
46 TTreeProcessorMT::TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename) : treeView(filenames, treename) {}
47 
48 ////////////////////////////////////////////////////////////////////////
49 /// Constructor based on a TTree.
50 /// \param[in] tree Tree or chain of files containing the tree to process.
52 
53 ////////////////////////////////////////////////////////////////////////
54 /// Constructor based on a TTree and a TEntryList.
55 /// \param[in] tree Tree or chain of files containing the tree to process.
56 /// \param[in] entries List of entry numbers to process.
57 TTreeProcessorMT::TTreeProcessorMT(TTree &tree, TEntryList &entries) : treeView(tree, entries) {}
58 
59 //////////////////////////////////////////////////////////////////////////////
60 /// Process the entries of a TTree in parallel. The user-provided function
61 /// receives a TTreeReader which can be used to iterate on a subrange of
62 /// entries
63 /// ~~~{.cpp}
64 /// TTreeProcessorMT::Process([](TTreeReader& readerSubRange) {
65 /// // Select branches to read
66 /// while (readerSubRange.next()) {
67 /// // Use content of current entry
68 /// }
69 /// });
70 /// ~~~
71 /// The user needs to be aware that each of the subranges can potentially
72 /// be processed in parallel. This means that the code of the user function
73 /// should be thread safe.
74 ///
75 /// \param[in] func User-defined function that processes a subrange of entries
77 {
78  // Enable this IMT use case (activate its locks)
80 
81  // Iterate over the collection of files
82  std::vector<std::tuple<Long64_t, Long64_t, size_t>> vTuple;
83  for (size_t i = 0; i < treeView->GetNumFiles(); ++i) {
84  treeView->SetCurrent(i);
85  auto clusterIter = treeView->GetClusterIterator();
86  Long64_t start = 0, end = 0;
87  // Iterate over the clusters in the current file and generate a task for each of them
88  while ((start = clusterIter()) < treeView->GetEntries()) {
89  end = clusterIter.GetNextEntry();
90  vTuple.emplace_back(start, end, i);
91  }
92  }
93 
94  auto mapFunction = [this, &func](const std::tuple<Long64_t, Long64_t, size_t> &t) {
95  treeView->SetCurrent(std::get<2>(t));
96  auto tr = treeView->GetTreeReader(std::get<0>(t), std::get<1>(t));
97  func(*tr);
98  };
99 
100  // Assume number of threads has been initialized via ROOT::EnableImplicitMT
101  TThreadExecutor pool;
102  pool.Foreach(mapFunction, vTuple);
103 }
void Foreach(F func, unsigned nTimes)
Execute func (with no arguments) nTimes in parallel.
Long64_t GetNextEntry()
Definition: TTree.h:274
long long Long64_t
Definition: RtypesCore.h:69
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Definition: TTreeReader.h:43
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
size_t GetNumFiles() const
Get the number of files of this view.
std::unique_ptr< TTreeReader > GetTreeReader(Long64_t start, Long64_t end)
Get a TTreeReader for the current tree of this view.
void SetCurrent(unsigned int i)
Set the current file and tree of this view.
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:146
This class provides a simple interface to execute the same task multiple times in parallel...
TTree::TClusterIterator GetClusterIterator()
Get the cluster iterator for the current tree of this view, starting from entry zero.
Long64_t GetEntries() const
Get the number of entries of the current tree of this view.
void Process(std::function< void(TTreeReader &)> func)
Process the entries of a TTree in parallel.
double func(double *x, double *p)
Definition: stressTF1.cxx:213
ROOT::TThreadedObject< ROOT::Internal::TTreeView > treeView
! Threaded object with <file,tree> per thread
Definition: tree.py:1
TTreeProcessorMT(std::string_view filename, std::string_view treename="")
Constructor based on a file name.
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:25