Logo ROOT  
Reference Guide
TTreeProcessorMT.hxx
Go to the documentation of this file.
1// @(#)root/thread:$Id$
2// Authors: Enric Tejedor, Enrico Guiraud CERN 05/06/2018
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#ifndef ROOT_TTreeProcessorMT
13#define ROOT_TTreeProcessorMT
14
15#include "TKey.h"
16#include "TTree.h"
17#include "TFile.h"
18#include "TChain.h"
19#include "TEntryList.h"
20#include "TTreeReader.h"
21#include "TError.h"
22#include "TEntryList.h"
23#include "TFriendElement.h"
24#include "ROOT/RMakeUnique.hxx"
27
28#include <functional>
29#include <utility> // std::pair
30#include <vector>
31
32/** \class TTreeView
33 \brief A helper class that encapsulates a file and a tree.
34
35A helper class that encapsulates a TFile and a TTree, along with their names.
36It is used together with TTProcessor and ROOT::TThreadedObject, so that
37in the TTProcessor::Process method each thread can work on its own
38<TFile,TTree> pair.
39
40This class can also be used with a collection of file names or a TChain, in case
41the tree is stored in more than one file. A view will always contain only the
42current (active) tree and file objects.
43
44A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
45The latter makes a copy of a model object every time a new thread accesses
46the threaded object.
47*/
48
49namespace ROOT {
50namespace Internal {
51/// Names, aliases, and file names of a TTree's or TChain's friends
52using NameAlias = std::pair<std::string, std::string>;
53struct FriendInfo {
54 /// Pairs of names and aliases of friend trees/chains
55 std::vector<Internal::NameAlias> fFriendNames;
56 /// Names of the files where each friend is stored. fFriendFileNames[i] is the list of files for friend with
57 /// name fFriendNames[i]
58 std::vector<std::vector<std::string>> fFriendFileNames;
59};
60
61class TTreeView {
62private:
63 std::vector<std::unique_ptr<TChain>> fFriends; ///< Friends of the tree/chain, if present
64 std::unique_ptr<TEntryList> fEntryList; ///< TEntryList for fChain, if present
65 // NOTE: fFriends and fEntryList MUST come before fChain to be deleted after it, because neither friend trees nor
66 // entrylists are deregistered from the main tree at destruction (ROOT-9283 tracks the issue for friends).
67 std::unique_ptr<TChain> fChain; ///< Chain on which to operate
68
69 void MakeChain(const std::vector<std::string> &treeName, const std::vector<std::string> &fileNames,
70 const FriendInfo &friendInfo, const std::vector<Long64_t> &nEntries,
71 const std::vector<std::vector<Long64_t>> &friendEntries);
72
73public:
74 TTreeView() = default;
75 // no-op, we don't want to copy the local TChains
76 TTreeView(const TTreeView &) {}
77 std::unique_ptr<TTreeReader> GetTreeReader(Long64_t start, Long64_t end, const std::vector<std::string> &treeName,
78 const std::vector<std::string> &fileNames, const FriendInfo &friendInfo,
79 const TEntryList &entryList, const std::vector<Long64_t> &nEntries,
80 const std::vector<std::vector<Long64_t>> &friendEntries);
81};
82} // End of namespace Internal
83
85private:
86 const std::vector<std::string> fFileNames; ///< Names of the files
87 const std::vector<std::string> fTreeNames; ///< TTree names (always same size and ordering as fFileNames)
88 /// User-defined selection of entry numbers to be processed, empty if none was provided
91 ROOT::TThreadExecutor fPool; ///<! Thread pool for processing.
92
93 /// Thread-local TreeViews
94 // Must be declared after fPool, for IMT to be initialized first!
96
98 std::vector<std::string> FindTreeNames();
99 static unsigned int fgMaxTasksPerFilePerWorker;
100
101public:
102 TTreeProcessorMT(std::string_view filename, std::string_view treename = "", UInt_t nThreads = 0u);
103 TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "",
104 UInt_t nThreads = 0u);
105 TTreeProcessorMT(TTree &tree, const TEntryList &entries, UInt_t nThreads = 0u);
106 TTreeProcessorMT(TTree &tree, UInt_t nThreads = 0u);
107
108 void Process(std::function<void(TTreeReader &)> func);
109 static void SetMaxTasksPerFilePerWorker(unsigned int m);
110 static unsigned int GetMaxTasksPerFilePerWorker();
111};
112
113} // End of namespace ROOT
114
115#endif // defined TTreeProcessorMT
long long Long64_t
Definition: RtypesCore.h:71
TTreeView(const TTreeView &)
std::unique_ptr< TChain > fChain
Chain on which to operate.
std::unique_ptr< TTreeReader > GetTreeReader(Long64_t start, Long64_t end, const std::vector< std::string > &treeName, const std::vector< std::string > &fileNames, const FriendInfo &friendInfo, const TEntryList &entryList, const std::vector< Long64_t > &nEntries, const std::vector< std::vector< Long64_t > > &friendEntries)
Get a TTreeReader for the current tree of this view.
std::vector< std::unique_ptr< TChain > > fFriends
Friends of the tree/chain, if present.
std::unique_ptr< TEntryList > fEntryList
TEntryList for fChain, if present.
void MakeChain(const std::vector< std::string > &treeName, const std::vector< std::string > &fileNames, const FriendInfo &friendInfo, const std::vector< Long64_t > &nEntries, const std::vector< std::vector< Long64_t > > &friendEntries)
Construct fChain, also adding friends if needed and injecting knowledge of offsets if available.
This class provides a simple interface to execute the same task multiple times in parallel,...
A class to process the entries of a TTree in parallel.
const std::vector< std::string > fTreeNames
TTree names (always same size and ordering as fFileNames)
static unsigned int GetMaxTasksPerFilePerWorker()
Sets the maximum number of tasks created per file, per worker.
std::vector< std::string > FindTreeNames()
Retrieve the names of the TTrees in each of the input files, throw if a TTree cannot be found.
const std::vector< std::string > fFileNames
Names of the files.
static void SetMaxTasksPerFilePerWorker(unsigned int m)
Sets the maximum number of tasks created per file, per worker.
Internal::FriendInfo GetFriendInfo(TTree &tree)
Get and store the names, aliases and file names of the friends of the tree.
static unsigned int fgMaxTasksPerFilePerWorker
ROOT::TThreadExecutor fPool
! Thread pool for processing.
TEntryList fEntryList
User-defined selection of entry numbers to be processed, empty if none was provided.
ROOT::TThreadedObject< ROOT::Internal::TTreeView > fTreeView
Thread-local TreeViews.
TTreeProcessorMT(std::string_view filename, std::string_view treename="", UInt_t nThreads=0u)
Constructor based on a file name.
void Process(std::function< void(TTreeReader &)> func)
Process the entries of a TTree in parallel.
const Internal::FriendInfo fFriendInfo
A List of entry numbers in a TTree or TChain.
Definition: TEntryList.h:26
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:43
A TTree represents a columnar dataset.
Definition: TTree.h:78
basic_string_view< char > string_view
std::pair< std::string, std::string > NameAlias
Names, aliases, and file names of a TTree's or TChain's friends.
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:151
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Definition: StringConv.hxx:21
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.
Definition: TROOT.cxx:564
Definition: tree.py:1
std::vector< std::vector< std::string > > fFriendFileNames
Names of the files where each friend is stored.
std::vector< Internal::NameAlias > fFriendNames
Pairs of names and aliases of friend trees/chains.
Defines the number of threads in some of ROOT's interfaces.
auto * m
Definition: textangle.C:8