48 const FriendInfo &friendInfo,
const std::vector<Long64_t> &nEntries,
49 const std::vector<std::vector<Long64_t>> &friendEntries)
51 const std::vector<NameAlias> &friendNames = friendInfo.
fFriendNames;
52 const std::vector<std::vector<std::string>> &friendFileNames = friendInfo.
fFriendFileNames;
55 const auto nFiles = fileNames.size();
56 for (
auto i = 0u; i < nFiles; ++i) {
57 fChain->Add(fileNames[i].c_str(), nEntries[i]);
62 const auto nFriends = friendNames.size();
63 for (
auto i = 0u; i < nFriends; ++i) {
64 const auto &friendName = friendNames[i];
65 const auto &
name = friendName.first;
66 const auto &alias = friendName.second;
69 auto frChain = std::make_unique<TChain>(
name.c_str());
70 const auto nFileNames = friendFileNames[i].size();
71 for (
auto j = 0u; j < nFileNames; ++j)
72 frChain->Add(friendFileNames[i][j].c_str(), friendEntries[i][j]);
75 fChain->AddFriend(frChain.get(), alias.c_str());
76 fFriends.emplace_back(std::move(frChain));
86 std::vector<TEntryList*> globalEntryLists;
87 auto innerLists = globalList.
GetLists();
89 if (globalList.
GetN()) {
90 globalEntryLists.emplace_back(&globalList);
93 for (
auto lp : *innerLists) {
94 auto lpAsTEntryList =
static_cast<TEntryList *
>(lp);
95 if (lpAsTEntryList->GetN()) {
96 globalEntryLists.emplace_back(lpAsTEntryList);
101 auto localList = std::make_unique<TEntryList>();
103 for (
auto gl : globalEntryLists) {
107 auto tmp_list =
new TEntryList(gl->GetName(), gl->GetTitle(), gl->GetFileName(), gl->GetTreeName());
112 }
else if (entry >= start) {
113 tmp_list->Enter(entry);
115 }
while ((entry = gl->Next()) >= 0);
117 if (tmp_list->GetN() > 0) {
118 localList->Add(tmp_list);
124 auto reader = std::make_unique<TTreeReader>(
fChain.get(), localList.get());
125 return std::make_pair(std::move(reader), std::move(localList));
130 auto reader = std::make_unique<TTreeReader>(
fChain.get());
131 reader->SetEntriesRange(start, end);
140 const std::vector<Long64_t> &nEntries,
const std::vector<std::vector<Long64_t>> &friendEntries)
142 const bool usingLocalEntries = friendInfo.
fFriendNames.empty() && entryList.
GetN() == 0;
143 if (
fChain ==
nullptr || (usingLocalEntries && fileNames[0] !=
fChain->GetListOfFiles()->At(0)->GetTitle()))
144 MakeChain(treeName, fileNames, friendInfo, nEntries, friendEntries);
146 std::unique_ptr<TTreeReader> reader;
147 std::unique_ptr<TEntryList> localList;
148 if (entryList.
GetN() > 0) {
155 return std::make_pair(std::move(reader), std::move(localList));
167 const auto nFileNames = fileNames.size();
168 std::vector<std::vector<EntryCluster>> clustersPerFile;
169 std::vector<Long64_t> entriesPerFile;
170 entriesPerFile.reserve(nFileNames);
172 for (
const auto &fileName : fileNames) {
173 auto fileNameC = fileName.c_str();
175 if (!
f ||
f->IsZombie()) {
176 Error(
"TTreeProcessorMT::Process",
"An error occurred while opening file %s: skipping it.", fileNameC);
177 clustersPerFile.emplace_back(std::vector<EntryCluster>());
178 entriesPerFile.emplace_back(0ULL);
182 f->GetObject(treeName.c_str(), t);
185 Error(
"TTreeProcessorMT::Process",
"An error occurred while getting tree %s from file %s: skipping this file.",
186 treeName.c_str(), fileNameC);
187 clustersPerFile.emplace_back(std::vector<EntryCluster>());
188 entriesPerFile.emplace_back(0ULL);
196 std::vector<EntryCluster> clusters;
197 while ((start = clusterIter()) < entries) {
198 end = clusterIter.GetNextEntry();
200 clusters.emplace_back(EntryCluster{start + offset, end + offset});
203 clustersPerFile.emplace_back(std::move(clusters));
204 entriesPerFile.emplace_back(entries);
219 std::vector<std::vector<EntryCluster>> eventRangesPerFile(clustersPerFile.size());
220 auto clustersPerFileIt = clustersPerFile.begin();
221 auto eventRangesPerFileIt = eventRangesPerFile.begin();
222 for (; clustersPerFileIt != clustersPerFile.end(); clustersPerFileIt++, eventRangesPerFileIt++) {
223 const auto clustersInThisFileSize = clustersPerFileIt->size();
224 const auto nFolds = clustersInThisFileSize / maxTasksPerFile;
229 clustersPerFileIt->begin(), clustersPerFileIt->end(),
230 [&eventRangesPerFileIt](
const EntryCluster &clust) { eventRangesPerFileIt->emplace_back(clust); });
235 auto nReminderClusters = clustersInThisFileSize % maxTasksPerFile;
236 const auto clustersInThisFile = *clustersPerFileIt;
237 for (
auto i = 0ULL; i < clustersInThisFileSize; ++i) {
238 const auto start = clustersInThisFile[i].start;
243 if (nReminderClusters > 0) {
247 const auto end = clustersInThisFile[i].end;
248 eventRangesPerFileIt->emplace_back(EntryCluster({start, end}));
252 return std::make_pair(std::move(eventRangesPerFile), std::move(entriesPerFile));
257static std::vector<std::vector<Long64_t>>
259 const std::vector<std::vector<std::string>> &friendFileNames)
261 std::vector<std::vector<Long64_t>> friendEntries;
262 const auto nFriends = friendNames.size();
263 for (
auto i = 0u; i < nFriends; ++i) {
264 std::vector<Long64_t> nEntries;
265 const auto &thisFriendName = friendNames[i].first;
266 const auto &thisFriendFiles = friendFileNames[i];
267 for (
const auto &fname : thisFriendFiles) {
270 f->GetObject(thisFriendName.c_str(), t);
273 friendEntries.emplace_back(std::move(nEntries));
276 return friendEntries;
284 if (0 == strcmp(
"TChain",
tree.ClassName())) {
285 auto &chain =
dynamic_cast<const TChain &
>(
tree);
287 if (files && 0 != files->GetEntries()) {
293 if (
auto motherDir =
tree.GetDirectory()) {
299 if (motherDir->InheritsFrom(
"TFile")) {
300 return tree.GetName();
302 std::string fullPath(motherDir->GetPath());
304 fullPath +=
tree.GetName();
309 return tree.GetName();
322 std::vector<Internal::NameAlias> friendNames;
323 std::vector<std::vector<std::string>> friendFileNames;
325 const auto friends =
tree.GetListOfFriends();
329 for (
auto fr : *friends) {
333 const auto realName = frTree->
GetName();
334 const auto alias =
tree.GetFriendAlias(frTree);
336 friendNames.emplace_back(std::make_pair(realName, std::string(alias)));
338 friendNames.emplace_back(std::make_pair(realName,
""));
342 friendFileNames.emplace_back();
343 auto &fileNames = friendFileNames.back();
346 const auto frChain =
static_cast<TChain *
>(frTree);
347 for (
auto f : *(frChain->GetListOfFiles())) {
348 fileNames.emplace_back(
f->GetTitle());
351 const auto f = frTree->GetCurrentFile();
353 throw std::runtime_error(
"Friend trees with no associated file are not supported.");
354 fileNames.emplace_back(
f->GetName());
365 std::string treeName;
368 throw std::runtime_error(
"Empty list of files and no tree name provided");
372 TIter next(
f->GetListOfKeys());
373 while (
TKey *key = (
TKey *)next()) {
374 const char *className = key->GetClassName();
375 if (strcmp(className,
"TTree") == 0) {
376 treeName = key->GetName();
380 if (treeName.empty())
381 throw std::runtime_error(
"Cannot find any tree in file " +
fFileNames[0]);
393 : fFileNames({std::string(filename)}), fTreeName(treename.empty() ? FindTreeName() : treename), fFriendInfo()
400 throw std::runtime_error(
"The provided list of file names is empty");
402 std::vector<std::string> strings;
403 strings.reserve(views.size());
404 for (
const auto &
v : views)
405 strings.emplace_back(
v);
416 : fFileNames(
CheckAndConvert(filenames)), fTreeName(treename.empty() ? FindTreeName() : treename), fFriendInfo()
422 std::vector<std::string> filenames;
429 throw std::runtime_error(
"The provided chain of files is empty");
430 filenames.reserve(nFiles);
431 for (
auto f : *filelist)
432 filenames.emplace_back(
f->GetTitle());
436 const auto msg =
"The specified TTree is not linked to any file, in-memory-only trees are not supported.";
437 throw std::runtime_error(msg);
440 filenames.emplace_back(
f->GetName());
452 fFriendInfo(GetFriendInfo(
tree))
485 const bool hasFriends = !friendNames.empty();
487 const bool shouldRetrieveAllClusters = hasFriends || hasEntryList;
488 const auto clustersAndEntries =
490 const auto &clusters = clustersAndEntries.first;
491 const auto &entries = clustersAndEntries.second;
494 const auto friendEntries =
499 using Internal::EntryCluster;
500 auto processFile = [&](std::size_t fileIdx) {
502 const auto &theseFiles = shouldRetrieveAllClusters ?
fFileNames : std::vector<std::string>({
fFileNames[fileIdx]});
504 const auto theseClustersAndEntries =
508 const auto &thisFileClusters = shouldRetrieveAllClusters ? clusters[fileIdx] : theseClustersAndEntries.first[0];
511 const auto &theseEntries =
512 shouldRetrieveAllClusters ? entries : std::vector<Long64_t>({theseClustersAndEntries.second[0]});
514 auto processCluster = [&](
const Internal::EntryCluster &
c) {
515 std::unique_ptr<TTreeReader> reader;
516 std::unique_ptr<TEntryList> elist;
522 pool.
Foreach(processCluster, thisFileClusters);
525 std::vector<std::size_t> fileIdxs(
fFileNames.size());
526 std::iota(fileIdxs.begin(), fileIdxs.end(), 0u);
531 pool.
Foreach(processFile, fileIdxs);
void Error(const char *location, const char *msgfmt,...)
std::vector< std::string > GetFilesFromTree(TTree &tree)
std::vector< std::string > CheckAndConvert(const std::vector< std::string_view > &views)
std::pair< std::unique_ptr< TTreeReader >, std::unique_ptr< TEntryList > > TreeReaderEntryListPair
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)
This class provides a simple interface to execute the same task multiple times in parallel,...
void Foreach(F func, unsigned nTimes, unsigned nChunks=0)
Execute func (with no arguments) nTimes in parallel.
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.
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 chain is a collection of files containing TTree objects.
TObjArray * GetListOfFiles() const
A List of entry numbers in a TTree or TChain.
virtual TList * GetLists() const
virtual Long64_t GetN() const
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose, Int_t netopt=0)
Create / open a file.
A TFriendElement TF describes a TTree object TF in a file.
Book space in a file, create I/O buffers, to fill them, (un)compress them.
virtual const char * GetName() const
Returns name of object.
Int_t GetEntries() const
Return the number of objects in array (i.e.
TObject * At(Int_t idx) const
virtual const char * GetName() const
Returns name of object.
@ kMustCleanup
if object destructor must call RecursiveRemove()
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
A TTree represents a columnar dataset.
virtual TClusterIterator GetClusterIterator(Long64_t firstentry)
Return an iterator over the cluster of baskets starting at firstentry.
virtual Long64_t GetEntries() const
basic_string_view< char > string_view
std::pair< std::vector< std::vector< EntryCluster > >, std::vector< Long64_t > > ClustersAndEntries
Return a vector of cluster boundaries for the given tree and files.
static std::vector< std::vector< Long64_t > > GetFriendEntries(const std::vector< std::pair< std::string, std::string > > &friendNames, const std::vector< std::vector< std::string > > &friendFileNames)
Return a vector containing the number of entries of each file of each friend TChain.
static ClustersAndEntries MakeClusters(const std::string &treeName, const std::vector< std::string > &fileNames)
static std::string GetTreeFullPath(const TTree &tree)
Return the full path of the tree.
void function(const Char_t *name_, T fun, const Char_t *docstring=0)
Namespace for new ROOT classes and functions.
UInt_t GetImplicitMTPoolSize()
Returns the size of the pool used for implicit multi-threading.
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.