32 std::unordered_set<TTree *> &analysedTrees,
const std::string friendName =
"")
34 if (!analysedTrees.insert(&t).second) {
40 for (
auto branchObj : *branches) {
42 if (bNamesReg.insert(
name).second) {
43 bNames.emplace_back(
name);
44 }
else if (!friendName.empty()) {
47 const auto longName = friendName +
"." +
name;
48 if (bNamesReg.insert(longName).second)
49 bNames.emplace_back(longName);
59 for (
auto friendTreeObj : *friendTrees) {
61 auto friendTree = friendElement->
GetTree();
62 const std::string frName(friendElement->GetName());
75 std::unordered_set<std::string> bNamesSet;
76 std::vector<std::string> bNames;
77 std::unordered_set<TTree *> analysedTrees;
90 std::vector<std::string> filenames;
93 if (
auto chain =
dynamic_cast<const TChain *
>(&
tree)) {
94 const auto *chainFiles = chain->GetListOfFiles();
96 throw std::runtime_error(
"Could not retrieve a list of files from the input TChain.");
100 const auto nfiles = chainFiles->GetEntries();
102 throw std::runtime_error(
"The list of files associated with the input TChain is empty.");
104 filenames.reserve(nfiles);
105 for (
const auto *
f : *chainFiles)
106 filenames.emplace_back(
f->GetTitle());
110 throw std::runtime_error(
"The input TTree is not linked to any file, "
111 "in-memory-only trees are not supported.");
114 filenames.emplace_back(
f->GetName());
171 const auto *friends =
tree.GetListOfFriends();
172 if (!friends || friends->GetEntries() == 0)
175 std::vector<std::pair<std::string, std::string>> friendNames;
176 std::vector<std::vector<std::string>> friendFileNames;
177 std::vector<std::vector<std::string>> friendChainSubNames;
178 std::vector<std::vector<std::int64_t>> nEntriesPerTreePerFriend;
179 std::vector<std::unique_ptr<TVirtualIndex>> treeIndexes;
182 auto nFriends = friends->GetEntries();
183 friendNames.reserve(nFriends);
184 friendFileNames.reserve(nFriends);
185 friendChainSubNames.reserve(nFriends);
186 nEntriesPerTreePerFriend.reserve(nFriends);
188 for (
auto fr : *friends) {
194 friendFileNames.emplace_back();
195 auto &fileNames = friendFileNames.back();
199 friendChainSubNames.emplace_back();
200 auto &chainSubNames = friendChainSubNames.back();
203 nEntriesPerTreePerFriend.emplace_back();
204 auto &nEntriesInThisFriend = nEntriesPerTreePerFriend.back();
207 const auto *alias_c =
tree.GetFriendAlias(frTree);
208 const std::string alias = alias_c !=
nullptr ? alias_c :
"";
210 auto *treeIndex = frTree->GetTreeIndex();
211 treeIndexes.emplace_back(
static_cast<TVirtualIndex *
>(treeIndex ? treeIndex->Clone() :
nullptr));
214 if (
auto frChain =
dynamic_cast<const TChain *
>(frTree)) {
225 const auto *chainFiles = frChain->GetListOfFiles();
226 if (!chainFiles || chainFiles->GetEntries() == 0) {
227 throw std::runtime_error(
"A TChain in the list of friends does not contain any file. "
228 "Friends with no associated files are not supported.");
232 auto nFiles = chainFiles->GetEntries();
233 fileNames.reserve(nFiles);
234 chainSubNames.reserve(nFiles);
235 nEntriesInThisFriend.reserve(nFiles);
238 friendNames.emplace_back(std::make_pair(frChain->GetName(), alias));
242 for (
const auto *
f : *chainFiles) {
244 auto thisTreeName =
f->GetName();
245 auto thisFileName =
f->GetTitle();
247 chainSubNames.emplace_back(thisTreeName);
248 fileNames.emplace_back(thisFileName);
250 if (retrieveEntries) {
251 std::unique_ptr<TFile> thisFile{
TFile::Open(thisFileName,
"READ_WITHOUT_GLOBALREGISTRATION")};
252 if (!thisFile || thisFile->IsZombie())
253 throw std::runtime_error(std::string(
"GetFriendInfo: Could not open file \"") + thisFileName +
"\"");
254 TTree *thisTree = thisFile->Get<
TTree>(thisTreeName);
256 throw std::runtime_error(std::string(
"GetFriendInfo: Could not retrieve TTree \"") + thisTreeName +
257 "\" from file \"" + thisFileName +
"\"");
258 nEntriesInThisFriend.emplace_back(thisTree->
GetEntries());
264 nEntriesInThisFriend.emplace_back(maxEntries);
270 friendNames.emplace_back(std::make_pair(realName, alias));
273 const auto *
f = frTree->GetCurrentFile();
275 throw std::runtime_error(
"A TTree in the list of friends is not linked to any file. "
276 "Friends with no associated files are not supported.");
277 fileNames.emplace_back(
f->GetName());
280 nEntriesInThisFriend.emplace_back(frTree->GetEntries());
285 std::move(friendChainSubNames), std::move(nEntriesPerTreePerFriend),
286 std::move(treeIndexes));
306 if (
auto chain =
dynamic_cast<const TChain *
>(&
tree)) {
307 const auto *chainFiles = chain->GetListOfFiles();
308 if (!chainFiles || chainFiles->GetEntries() == 0) {
309 throw std::runtime_error(
"The input TChain does not contain any file.");
311 std::vector<std::string> treeNames;
312 for (
const auto *
f : *chainFiles)
313 treeNames.emplace_back(
f->GetName());
319 if (
const auto *treeDir =
tree.GetDirectory()) {
324 if (
dynamic_cast<const TFile *
>(treeDir)) {
325 return {
tree.GetName()};
327 std::string fullPath = treeDir->GetPath();
328 fullPath = fullPath.substr(fullPath.rfind(
":/") + 1);
330 fullPath +=
tree.GetName();
335 return {
tree.GetName()};
351 TObjArray *subBranches = branch->GetListOfBranches();
353 TObjArray *leaves = branch->GetListOfLeaves();
375 std::vector<std::unique_ptr<TChain>> friends;
377 friends.reserve(nFriends);
379 for (std::size_t i = 0u; i < nFriends; ++i) {
380 const auto &thisFriendName = finfo.
fFriendNames[i].first;
387 if (thisFriendChainSubNames.empty()) {
389 frChain->Add(thisFriendFileNames[0].c_str(), thisFriendEntries[0]);
393 for (std::size_t j = 0u; j < thisFriendFileNames.size(); ++j) {
394 frChain->Add((thisFriendFileNames[j] +
"?#" + thisFriendChainSubNames[j]).c_str(), thisFriendEntries[j]);
400 auto *copyOfIndex =
static_cast<TVirtualIndex *
>(treeIndex->Clone());
401 copyOfIndex->
SetTree(frChain.get());
402 frChain->SetTreeIndex(copyOfIndex);
405 friends.emplace_back(std::move(frChain));
429 std::string basename;
431 const auto slashpos = glob.rfind(
'/');
433 if (slashpos != std::string::npos) {
435 dirname = glob.substr(0, slashpos);
436 basename = glob.substr(slashpos + 1);
452 TRegexp re(basename.c_str(),
true);
456 if (!strcmp(
file,
".") || !strcmp(
file,
".."))
470 std::vector<std::string> ret;
471 ret.reserve(
l.GetEntries());
472 for (
const auto *tobjstr : ROOT::RangeStaticCast<const TObjString *>(
l)) {
473 ret.push_back(tobjstr->GetName());
477 throw std::runtime_error(
"ExpandGlob: could not open directory '" + dirname +
"'.");
static void GetTopLevelBranchNamesImpl(TTree &t, std::unordered_set< std::string > &bNamesReg, std::vector< std::string > &bNames, std::unordered_set< TTree * > &analysedTrees, const std::string friendName="")
R__EXTERN TSystem * gSystem
A chain is a collection of files containing TTree objects.
@ kWithoutGlobalRegistration
const char * GetName() const override
Return name of this collection.
A ROOT file is composed of a header, followed by consecutive data records (TKey instances) with a wel...
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
A TFriendElement TF describes a TTree object TF in a file.
virtual TTree * GetTree()
Return pointer to friend TTree.
Int_t GetEntries() const override
Return the number of objects in array (i.e.
TObject * At(Int_t idx) const override
Collectable string class.
Mother of all ROOT objects.
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
@ kMustCleanup
if object destructor must call RecursiveRemove()
Regular expression class.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
virtual void FreeDirectory(void *dirp)
Free a directory.
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
virtual const char * WorkingDirectory()
Return working directory.
A TTree represents a columnar dataset.
virtual Long64_t GetEntries() const
virtual TObjArray * GetListOfBranches()
virtual TList * GetListOfFriends() const
static constexpr Long64_t kMaxEntries
Abstract interface for Tree Index.
virtual void SetTree(TTree *T)=0
Different standalone functions to work with trees and tuples, not reqiuired to be a member of any cla...
std::vector< std::string > GetTreeFullPaths(const TTree &tree)
std::vector< std::string > GetTopLevelBranchNames(TTree &t)
Get all the top-level branches names, including the ones of the friend trees.
std::unique_ptr< TChain > MakeChainForMT(const std::string &name="", const std::string &title="")
Create a TChain object with options that avoid common causes of thread contention.
std::vector< std::unique_ptr< TChain > > MakeFriends(const ROOT::TreeUtils::RFriendInfo &finfo)
Create friends from the main TTree.
ROOT::TreeUtils::RFriendInfo GetFriendInfo(const TTree &tree, bool retrieveEntries=false)
std::vector< std::string > ExpandGlob(const std::string &glob)
Expands input glob into a collection of full paths to files.
void ClearMustCleanupBits(TObjArray &arr)
Reset the kMustCleanup bit of a TObjArray of TBranch objects (e.g.
std::vector< std::string > GetFileNamesFromTree(const TTree &tree)
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
Information about friend trees of a certain TTree or TChain object.
std::vector< std::pair< std::string, std::string > > fFriendNames
Pairs of names and aliases of each friend tree/chain.
std::vector< std::vector< std::int64_t > > fNEntriesPerTreePerFriend
Number of entries contained in each tree of each friend.
std::vector< std::vector< std::string > > fFriendChainSubNames
Names of the subtrees of a friend TChain.
std::vector< std::unique_ptr< TVirtualIndex > > fTreeIndexInfos
Information on the friend's TTreeIndexes.
std::vector< std::vector< std::string > > fFriendFileNames
Names of the files where each friend is stored.