Logo ROOT   6.18/05
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::string &treeName, const std::vector<std::string> &fileNames, const FriendInfo &friendInfo,
69 const std::vector<Long64_t> &nEntries, const std::vector<std::vector<Long64_t>> &friendEntries);
71 std::unique_ptr<TTreeReader> MakeReader(Long64_t start, Long64_t end);
72
73public:
74 TTreeView() = default;
75 // no-op, we don't want to copy the local TChains
76 TTreeView(const TTreeView &) {}
77 TreeReaderEntryListPair GetTreeReader(Long64_t start, Long64_t end, const std::string &treeName,
78 const std::vector<std::string> &fileNames, const FriendInfo &friendInfo,
79 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::string fTreeName; ///< Name of the tree
88 /// User-defined selection of entry numbers to be processed, empty if none was provided
89 const TEntryList fEntryList; // const to be sure to avoid race conditions among TTreeViews
91
93
95 std::string FindTreeName();
96 static unsigned int fgMaxTasksPerFilePerWorker;
97
98public:
99 TTreeProcessorMT(std::string_view filename, std::string_view treename = "");
100 TTreeProcessorMT(const std::vector<std::string_view> &filenames, std::string_view treename = "");
101 TTreeProcessorMT(TTree &tree, const TEntryList &entries);
103
104 void Process(std::function<void(TTreeReader &)> func);
105 static void SetMaxTasksPerFilePerWorker(unsigned int m);
106 static unsigned int GetMaxTasksPerFilePerWorker();
107};
108
109} // End of namespace ROOT
110
111#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.
TreeReaderEntryListPair GetTreeReader(Long64_t start, Long64_t end, const 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.
std::unique_ptr< TTreeReader > MakeReader(Long64_t start, Long64_t end)
void MakeChain(const 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.
TreeReaderEntryListPair MakeReaderWithEntryList(TEntryList &globalList, Long64_t start, Long64_t end)
A class to process the entries of a TTree in parallel.
static unsigned int GetMaxTasksPerFilePerWorker()
Sets the maximum number of tasks created per file, per worker.
const std::string fTreeName
Name of the tree.
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
std::string FindTreeName()
Retrieve the name of the first TTree in the first input file, else throw.
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:71
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
Namespace for new ROOT classes and functions.
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