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 "TTreeReader.h"
20#include "TError.h"
21#include "TEntryList.h"
22#include "TFriendElement.h"
23#include "ROOT/RMakeUnique.hxx"
25
26#include <string.h>
27#include <functional>
28#include <vector>
29
30/** \class TTreeView
31 \brief A helper class that encapsulates a file and a tree.
32
33A helper class that encapsulates a TFile and a TTree, along with their names.
34It is used together with TTProcessor and ROOT::TThreadedObject, so that
35in the TTProcessor::Process method each thread can work on its own
36<TFile,TTree> pair.
37
38This class can also be used with a collection of file names or a TChain, in case
39the tree is stored in more than one file. A view will always contain only the
40current (active) tree and file objects.
41
42A copy constructor is defined for TTreeView to work with ROOT::TThreadedObject.
43The latter makes a copy of a model object every time a new thread accesses
44the threaded object.
45*/
46
47namespace ROOT {
48namespace Internal {
49/// Names, aliases, and file names of a TTree's or TChain's friends
50using NameAlias = std::pair<std::string, std::string>;
51struct FriendInfo {
52 /// Pairs of names and aliases of friend trees/chains
53 std::vector<Internal::NameAlias> fFriendNames;
54 /// Names of the files where each friend is stored. fFriendFileNames[i] is the list of files for friend with
55 /// name fFriendNames[i]
56 std::vector<std::vector<std::string>> fFriendFileNames;
57};
58
59class TTreeView {
60public:
61 using TreeReaderEntryListPair = std::pair<std::unique_ptr<TTreeReader>, std::unique_ptr<TEntryList>>;
62
63private:
64 // NOTE: fFriends must come before fChain to be deleted after it, see ROOT-9281 for more details
65 std::vector<std::unique_ptr<TChain>> fFriends; ///< Friends of the tree/chain
66 std::unique_ptr<TChain> fChain; ///< Chain on which to operate
67
68 void MakeChain(const std::vector<std::string> &treeName, const std::vector<std::string> &fileNames,
69 const FriendInfo &friendInfo, const std::vector<Long64_t> &nEntries,
70 const std::vector<std::vector<Long64_t>> &friendEntries);
72 std::unique_ptr<TTreeReader> MakeReader(Long64_t start, Long64_t end);
73
74public:
75 TTreeView() = default;
76 // no-op, we don't want to copy the local TChains
77 TTreeView(const TTreeView &) {}
78 TreeReaderEntryListPair GetTreeReader(Long64_t start, Long64_t end, const std::vector<std::string> &treeName,
79 const std::vector<std::string> &fileNames, const FriendInfo &friendInfo,
80 TEntryList entryList, const std::vector<Long64_t> &nEntries,
81 const std::vector<std::vector<Long64_t>> &friendEntries);
82};
83} // End of namespace Internal
84
86private:
87 const std::vector<std::string> fFileNames; ///< Names of the files
88 const std::vector<std::string> fTreeNames; ///< TTree names (always same size and ordering as fFileNames)
89 /// User-defined selection of entry numbers to be processed, empty if none was provided
90 const TEntryList fEntryList; // const to be sure to avoid race conditions among TTreeViews
92
94
96 std::vector<std::string> FindTreeNames();
97 static unsigned int fgMaxTasksPerFilePerWorker;
98
99public:
100 TTreeProcessorMT(std::string_view filename, std::string_view treename = "");
101 TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "");
102 TTreeProcessorMT(TTree &tree, const TEntryList &entries);
104
105 void Process(std::function<void(TTreeReader &)> func);
106 static void SetMaxTasksPerFilePerWorker(unsigned int m);
107 static unsigned int GetMaxTasksPerFilePerWorker();
108};
109
110} // End of namespace ROOT
111
112#endif // defined TTreeProcessorMT
long long Long64_t
Definition: RtypesCore.h:69
std::pair< std::unique_ptr< TTreeReader >, std::unique_ptr< TEntryList > > TreeReaderEntryListPair
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.
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.
std::unique_ptr< TTreeReader > MakeReader(Long64_t start, Long64_t end)
TreeReaderEntryListPair MakeReaderWithEntryList(TEntryList &globalList, Long64_t start, Long64_t end)
TreeReaderEntryListPair GetTreeReader(Long64_t start, Long64_t end, const std::vector< std::string > &treeName, const std::vector< std::string > &fileNames, const FriendInfo &friendInfo, 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.
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
TTreeProcessorMT(std::string_view filename, std::string_view treename="")
Constructor based on a file name.
const 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
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:72
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
VSD Structures.
Definition: StringConv.hxx:21
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.
auto * m
Definition: textangle.C:8