Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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"
25#include "ROOT/InternalTreeUtils.hxx" // RFriendInfo
26
27#include <functional>
28#include <memory>
29#include <vector>
30
31/** \class TTreeView
32 \brief A helper class that encapsulates a file and a tree.
33
34A helper class that encapsulates a TFile and a TTree, along with their names.
35It is used together with TTProcessor and ROOT::TThreadedObject, so that
36in the TTProcessor::Process method each thread can work on its own
37<TFile,TTree> pair.
38
39This class can also be used with a collection of file names or a TChain, in case
40the tree is stored in more than one file. A view will always contain only the
41current (active) tree and file objects.
42
43A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
44The latter makes a copy of a model object every time a new thread accesses
45the threaded object.
46*/
47
48namespace ROOT {
49namespace Internal {
50
51class TTreeView {
52private:
53 std::vector<std::unique_ptr<TChain>> fFriends; ///< Friends of the tree/chain, if present
54 std::unique_ptr<TEntryList> fEntryList; ///< TEntryList for fChain, if present
55 // NOTE: fFriends and fEntryList MUST come before fChain to be deleted after it, because neither friend trees nor
56 // entrylists are deregistered from the main tree at destruction (ROOT-9283 tracks the issue for friends).
57 std::unique_ptr<TChain> fChain; ///< Chain on which to operate
58
59 void MakeChain(const std::vector<std::string> &treeName, const std::vector<std::string> &fileNames,
60 const TreeUtils::RFriendInfo &friendInfo, const std::vector<Long64_t> &nEntries,
61 const std::vector<std::vector<Long64_t>> &friendEntries);
62
63public:
64 TTreeView() = default;
65 // no-op, we don't want to copy the local TChains
66 TTreeView(const TTreeView &) {}
67 std::unique_ptr<TTreeReader> GetTreeReader(Long64_t start, Long64_t end, const std::vector<std::string> &treeName,
68 const std::vector<std::string> &fileNames, const TreeUtils::RFriendInfo &friendInfo,
69 const TEntryList &entryList, const std::vector<Long64_t> &nEntries,
70 const std::vector<std::vector<Long64_t>> &friendEntries);
71 void Reset();
72};
73} // End of namespace Internal
74
76private:
77 const std::vector<std::string> fFileNames; ///< Names of the files
78 const std::vector<std::string> fTreeNames; ///< TTree names (always same size and ordering as fFileNames)
79 /// User-defined selection of entry numbers to be processed, empty if none was provided
82 ROOT::TThreadExecutor fPool; ///<! Thread pool for processing.
83
84 /// Thread-local TreeViews
85 // Must be declared after fPool, for IMT to be initialized first!
87
88 std::vector<std::string> FindTreeNames();
89 static unsigned int fgTasksPerWorkerHint;
90
91public:
92 TTreeProcessorMT(std::string_view filename, std::string_view treename = "", UInt_t nThreads = 0u);
93 TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "",
94 UInt_t nThreads = 0u);
95 TTreeProcessorMT(TTree &tree, const TEntryList &entries, UInt_t nThreads = 0u);
96 TTreeProcessorMT(TTree &tree, UInt_t nThreads = 0u);
97
98 void Process(std::function<void(TTreeReader &)> func);
99
100 static void SetTasksPerWorkerHint(unsigned int m);
101 static unsigned int GetTasksPerWorkerHint();
102};
103
104} // End of namespace ROOT
105
106#endif // defined TTreeProcessorMT
long long Long64_t
Definition RtypesCore.h:80
std::unique_ptr< TChain > fChain
Chain on which to operate.
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 Reset()
Clear the resources.
void MakeChain(const std::vector< std::string > &treeName, const std::vector< std::string > &fileNames, const TreeUtils::RFriendInfo &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.
std::unique_ptr< TTreeReader > GetTreeReader(Long64_t start, Long64_t end, const std::vector< std::string > &treeName, const std::vector< std::string > &fileNames, const TreeUtils::RFriendInfo &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.
This class provides a simple interface to execute the same task multiple times in parallel threads,...
A wrapper to make object instances thread private, lazily.
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)
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 unsigned int fgTasksPerWorkerHint
const Internal::TreeUtils::RFriendInfo fFriendInfo
ROOT::TThreadExecutor fPool
! Thread pool for processing.
TEntryList fEntryList
User-defined selection of entry numbers to be processed, empty if none was provided.
static void SetTasksPerWorkerHint(unsigned int m)
Set the hint for the desired number of tasks created per worker.
ROOT::TThreadedObject< ROOT::Internal::TTreeView > fTreeView
Thread-local TreeViews.
void Process(std::function< void(TTreeReader &)> func)
Process the entries of a TTree in parallel.
static unsigned int GetTasksPerWorkerHint()
Retrieve the current value for the desired number of tasks per worker.
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:44
A TTree represents a columnar dataset.
Definition TTree.h:79
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
UInt_t GetThreadPoolSize()
Returns the size of ROOT's thread pool.
Definition TROOT.cxx:565
Definition tree.py:1
Information about friend trees of a certain TTree or TChain object.
Defines the number of threads in some of ROOT's interfaces.
auto * m
Definition textangle.C:8