12 #ifndef ROOT_TTreeProcessorMT 13 #define ROOT_TTreeProcessorMT 63 using NameAlias = std::pair<std::string, std::string>;
80 if (fTreeName.empty()) {
82 std::unique_ptr<TFile>
f(
TFile::Open(fFileNames[0].c_str()));
85 const char *className = key->GetClassName();
86 if (strcmp(className,
"TTree") == 0) {
87 fTreeName = key->GetName();
91 if (fTreeName.empty()) {
92 auto msg =
"Cannot find any tree in file " + fFileNames[0];
93 throw std::runtime_error(msg);
100 void MakeChain(
const std::vector<Long64_t> &nEntries,
const std::vector<std::vector<Long64_t>> &friendEntries)
102 fChain.reset(
new TChain(fTreeName.c_str()));
103 const auto nFiles = fFileNames.size();
104 for (
auto i = 0u; i < nFiles; ++i) {
105 fChain->
Add(fFileNames[i].c_str(), nEntries[i]);
110 const auto nFriends = fFriendNames.size();
111 for (
auto i = 0u; i < nFriends; ++i) {
112 const auto &friendName = fFriendNames[i];
113 const auto &
name = friendName.first;
114 const auto &alias = friendName.second;
117 auto frChain = std::make_unique<TChain>(
name.c_str());
118 const auto nFileNames = fFriendFileNames[i].size();
119 for (
auto j = 0u; j < nFileNames; ++j)
120 frChain->Add(fFriendFileNames[i][j].c_str(), friendEntries[i][j]);
123 fChain->
AddFriend(frChain.get(), alias.c_str());
124 fFriends.emplace_back(std::move(frChain));
136 for (
auto fr : *friends) {
140 auto realName = frTree->
GetName();
143 fFriendNames.emplace_back(std::make_pair(realName, std::string(alias)));
145 fFriendNames.emplace_back(std::make_pair(realName,
""));
149 fFriendFileNames.emplace_back();
150 auto &fileNames = fFriendFileNames.back();
152 auto f = frTree->GetCurrentFile();
153 fileNames.emplace_back(
f->GetName());
155 auto frChain =
static_cast<TChain *
>(frTree);
156 for (
auto f : *(frChain->GetListOfFiles())) {
157 fileNames.emplace_back(
f->GetTitle());
167 auto elist = std::make_unique<TEntryList>();
170 if (entry >= start && entry < end)
172 }
while ((entry = fEntryList.
Next()) >= 0);
174 auto reader = std::make_unique<TTreeReader>(fChain.get(), elist.get());
175 return std::make_pair(std::move(
reader), std::move(elist));
180 auto reader = std::make_unique<TTreeReader>(fChain.get());
182 reader->SetEntriesRange(start, end);
195 fFileNames.emplace_back(fn);
196 GetTreeNameIfNeeded();
207 if (fns.size() > 0) {
209 fFileNames.emplace_back(fn);
212 auto msg =
"The provided list of file names is empty, cannot process tree " + fTreeName;
213 throw std::runtime_error(msg);
215 GetTreeNameIfNeeded();
230 for (
Long64_t i = 0; i < numEntries; ++i) {
241 fFileNames.emplace_back(fn);
244 fFriendNames.emplace_back(fn);
247 fFriendFileNames.emplace_back();
248 auto &fileNames = fFriendFileNames.back();
249 for (
auto &
name : ffn) {
250 fileNames.emplace_back(
name);
258 const std::vector<std::vector<Long64_t>> &friendEntries)
260 if (fChain ==
nullptr)
261 MakeChain(nEntries, friendEntries);
263 std::unique_ptr<TTreeReader>
reader;
264 std::unique_ptr<TEntryList> elist;
265 if (fEntryList.
GetN() > 0) {
266 std::tie(reader, elist) = MakeReaderWithEntryList(start, end);
268 reader = MakeReader(start, end);
272 return std::make_pair(std::move(reader), std::move(elist));
297 fLoadedEntries.pop_back();
298 if (fLoadedEntries.size() > 0) {
299 fChain->
LoadTree(fLoadedEntries.back());
310 return fFriendFileNames;
332 #endif // defined TTreeProcessorMT virtual const char * GetName() const
Returns name of object.
const std::vector< std::string > & GetFileNames() const
Get the filenames for this view.
virtual Long64_t Next()
Return the next non-zero entry index (next after fLastIndexQueried) this function is faster than GetE...
TTreeView(const std::vector< std::string_view > &fns, std::string_view tn)
Constructor based on a collection of file names.
virtual Long64_t GetN() const
TTreeReader is a simple, robust and fast interface to read values from a TTree, TChain or TNtuple...
Namespace for new ROOT classes and functions.
std::pair< std::string, std::string > NameAlias
std::pair< std::unique_ptr< TTreeReader >, std::unique_ptr< TEntryList > > TreeReaderEntryListPair
virtual TList * GetListOfFriends() const
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
TreeReaderEntryListPair MakeReaderWithEntryList(Long64_t start, Long64_t end)
std::string GetTreeName() const
Get the name of the tree of this view.
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
const std::vector< NameAlias > & GetFriendNames() const
virtual Long64_t LoadTree(Long64_t entry)
Find the tree which contains entry, and set it as the current tree.
Book space in a file, create I/O buffers, to fill them, (un)compress them.
std::string fTreeName
Name of the tree.
std::vector< std::string > fFileNames
Names of the files.
TTreeView(const TTreeView &view)
Copy constructor.
void StoreFriends(const TTree &tree, bool isTree)
Get and store the names, aliases and file names of the friends of the tree.
void PopTaskFirstEntry()
Restore the tree of the previous loaded entry, if any.
TTreeView(TTree &tree, TEntryList &entries)
Constructor based on a TTree and a TEntryList.
TreeReaderEntryListPair GetTreeReader(Long64_t start, Long64_t end, const std::vector< Long64_t > &nEntries, const std::vector< std::vector< Long64_t >> &friendEntries)
Get a TTreeReader for the current tree of this view.
void MakeChain(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...
virtual Long64_t GetEntry(Int_t index)
Return the number of the entry #index of this TEntryList in the TTree or TChain See also Next()...
if object destructor must call RecursiveRemove()
std::unique_ptr< TChain > fChain
Chain on which to operate.
const std::vector< std::vector< std::string > > & GetFriendFileNames() const
void GetTreeNameIfNeeded()
If not treeName was provided to the ctor, use the name of the first TTree in the first file...
virtual TFriendElement * AddFriend(const char *chainname, const char *dummy="")
Add a TFriendElement to the list of friends of this chain.
void PushTaskFirstEntry(Long64_t entry)
Push a new loaded entry to the stack.
ClustersAndEntries MakeClusters(const std::string &treename, const std::vector< std::string > &filenames)
Return a vector of cluster boundaries for the given tree and files.
basic_string_view< char > string_view
std::unique_ptr< TTreeReader > MakeReader(Long64_t start, Long64_t end)
ROOT::TThreadedObject< ROOT::Internal::TTreeView > treeView
! Thread-local TreeViews
std::pair< std::vector< EntryCluster >, std::vector< Long64_t > > ClustersAndEntries
virtual Bool_t Enter(Long64_t entry, TTree *tree=0)
Add entry #entry to the list.
std::vector< Long64_t > fLoadedEntries
! Per-task loaded entries (for task interleaving)
virtual TList * GetListOfKeys() const
A TFriendElement TF describes a TTree object TF in a file.
A chain is a collection of files containing TTree objects.
A TTree object has a header with a name and a title.
std::vector< NameAlias > fFriendNames
<name,alias> pairs of the friends of the tree/chain
A List of entry numbers in a TTree or TChain.
A class to process the entries of a TTree in parallel.
TEntryList fEntryList
User-defined selection of entry numbers to be processed, empty if none was provided.
std::vector< std::vector< std::string > > fFriendFileNames
Names of the files where friends are stored.
virtual Int_t Add(TChain *chain)
Add all files referenced by the passed chain to this chain.
virtual const char * GetFriendAlias(TTree *) const
If the 'tree' is a friend, this method returns its alias name.
TTreeView(std::string_view fn, std::string_view tn)
Constructor based on a file name.
std::vector< std::unique_ptr< TChain > > fFriends
Friends of the tree/chain.