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"
25#include "ROOT/InternalTreeUtils.hxx" // RFriendInfo
26
27#include <functional>
28#include <memory>
29#include <vector>
30#include <limits>
31#include <RtypesCore.h> // Long64_t
32
33/** \class TTreeView
34 \brief A helper class that encapsulates a file and a tree.
35
36A helper class that encapsulates a TFile and a TTree, along with their names.
37It is used together with TTProcessor and ROOT::TThreadedObject, so that
38in the TTProcessor::Process method each thread can work on its own
39<TFile,TTree> pair.
40
41This class can also be used with a collection of file names or a TChain, in case
42the tree is stored in more than one file. A view will always contain only the
43current (active) tree and file objects.
44
45A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
46The latter makes a copy of a model object every time a new thread accesses
47the threaded object.
48*/
49
50namespace ROOT {
51
52namespace Internal {
53
54class TTreeView {
56
57 std::vector<std::unique_ptr<TChain>> fFriends; ///< Friends of the tree/chain, if present
58 std::unique_ptr<TEntryList> fEntryList; ///< TEntryList for fChain, if present
59 // NOTE: fFriends and fEntryList MUST come before fChain to be deleted after it, because neither friend trees nor
60 // entrylists are deregistered from the main tree at destruction (ROOT-9283 tracks the issue for friends).
61 std::unique_ptr<TChain> fChain; ///< Chain on which to operate
62
63 void MakeChain(const std::vector<std::string> &treeName, const std::vector<std::string> &fileNames,
64 const TreeUtils::RFriendInfo &friendInfo, const std::vector<Long64_t> &nEntries,
65 const std::vector<std::vector<Long64_t>> &friendEntries);
66
67public:
68 TTreeView() = default;
69 // no-op, we don't want to copy the local TChains
70 TTreeView(const TTreeView &) {}
71 std::unique_ptr<TTreeReader> GetTreeReader(Long64_t start, Long64_t end, const std::vector<std::string> &treeName,
72 const std::vector<std::string> &fileNames,
73 const TreeUtils::RFriendInfo &friendInfo, const TEntryList &entryList,
74 const std::vector<Long64_t> &nEntries,
75 const std::vector<std::vector<Long64_t>> &friendEntries);
76 void Reset();
77};
78} // End of namespace Internal
79
81private:
82 const std::vector<std::string> fFileNames; ///< Names of the files
83 const std::vector<std::string> fTreeNames; ///< TTree names (always same size and ordering as fFileNames)
84 /// User-defined selection of entry numbers to be processed, empty if none was provided
87 ROOT::TThreadExecutor fPool; ///<! Thread pool for processing.
88
89 /// Thread-local TreeViews
90 // Must be declared after fPool, for IMT to be initialized first!
92
93 std::vector<std::string> FindTreeNames();
94 static unsigned int fgTasksPerWorkerHint;
95
96 std::pair<Long64_t, Long64_t> fGlobalRange{0, std::numeric_limits<Long64_t>::max()};
97
98public:
100 const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
101 TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "",
102 UInt_t nThreads = 0u,
103 const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
104 TTreeProcessorMT(TTree &tree, const TEntryList &entries, UInt_t nThreads = 0u);
106 const std::pair<Long64_t, Long64_t> &globalRange = {0, std::numeric_limits<Long64_t>::max()});
107
108 void Process(std::function<void(TTreeReader &)> func);
109
110 static void SetTasksPerWorkerHint(unsigned int m);
111 static unsigned int GetTasksPerWorkerHint();
112};
113
114} // End of namespace ROOT
115
116#endif // defined TTreeProcessorMT
unsigned int UInt_t
Definition: RtypesCore.h:46
long long Long64_t
Definition: RtypesCore.h:80
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
TTreeView(const TTreeView &)
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.
ROOT::Internal::TreeUtils::RNoCleanupNotifier fNoCleanupNotifier
This class provides a simple interface to execute the same task multiple times in parallel threads,...
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.
TTreeProcessorMT(const std::vector< std::string_view > &filenames, std::string_view treename="", UInt_t nThreads=0u, const std::pair< Long64_t, Long64_t > &globalRange={0, std::numeric_limits< Long64_t >::max()})
const std::vector< std::string > fFileNames
Names of the files.
static unsigned int fgTasksPerWorkerHint
TTreeProcessorMT(TTree &tree, UInt_t nThreads=0u, const std::pair< Long64_t, Long64_t > &globalRange={0, std::numeric_limits< Long64_t >::max()})
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.
TTreeProcessorMT(std::string_view filename, std::string_view treename="", UInt_t nThreads=0u, const std::pair< Long64_t, Long64_t > &globalRange={0, std::numeric_limits< Long64_t >::max()})
static unsigned int GetTasksPerWorkerHint()
Retrieve the current value for the desired number of tasks per worker.
std::pair< Long64_t, Long64_t > fGlobalRange
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
basic_string_view< char > string_view
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Definition: RExports.h:167
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
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.
TMarker m
Definition: textangle.C:8