From 139757b7e2cd2313feaa2a8425409a391d32a534 Mon Sep 17 00:00:00 2001 From: wiechula Date: Fri, 1 Dec 2023 09:12:22 +0100 Subject: [PATCH] Add possibility to chain Trees in sub dirs of files --- Detectors/TPC/base/include/TPCBase/Utils.h | 2 +- Detectors/TPC/base/src/Utils.cxx | 32 ++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Detectors/TPC/base/include/TPCBase/Utils.h b/Detectors/TPC/base/include/TPCBase/Utils.h index da61047d3c8fd..b72c2a20fb45f 100644 --- a/Detectors/TPC/base/include/TPCBase/Utils.h +++ b/Detectors/TPC/base/include/TPCBase/Utils.h @@ -67,7 +67,7 @@ void mergeCalPads(std::string_view outputFileName, std::string_view inputFileNam /// \param command command to run /// \param treeName name of the tree in the chain /// \param treeTitle title of the tree -TChain* buildChain(std::string_view command, std::string_view treeName, std::string_view treeTitle); +TChain* buildChain(std::string_view command, std::string_view treeName, std::string_view treeTitle = "", bool checkSubDir = false); } // namespace utils } // namespace o2::tpc diff --git a/Detectors/TPC/base/src/Utils.cxx b/Detectors/TPC/base/src/Utils.cxx index 64a2805e5375f..5ddd649166b43 100644 --- a/Detectors/TPC/base/src/Utils.cxx +++ b/Detectors/TPC/base/src/Utils.cxx @@ -10,6 +10,7 @@ // or submit itself to any jurisdiction. #include +#include #include #include #include @@ -27,6 +28,7 @@ #include "TChain.h" #include "TGrid.h" +#include "CommonUtils/StringUtils.h" #include "Framework/Logger.h" #include "TPCBase/Mapper.h" #include "TPCBase/Utils.h" @@ -255,7 +257,7 @@ void utils::mergeCalPads(std::string_view outputFileName, std::string_view input } //______________________________________________________________________________ -TChain* utils::buildChain(std::string_view command, std::string_view treeName, std::string_view treeTitle) +TChain* utils::buildChain(std::string_view command, std::string_view treeName, std::string_view treeTitle, bool checkSubDir) { const TString files = gSystem->GetFromPipe(command.data()); std::unique_ptr arrFiles(files.Tokenize("\n")); @@ -266,10 +268,30 @@ TChain* utils::buildChain(std::string_view command, std::string_view treeName, s auto c = new TChain(treeName.data(), treeTitle.data()); for (const auto o : *arrFiles) { - LOGP(info, "Adding file '{}'", o->GetName()); - c->AddFile(o->GetName()); - if (std::string_view(o->GetName()).find("alien") == 0) { - TGrid::Connect("alien"); + if (o2::utils::Str::beginsWith(o->GetName(), "alien://") && !gGrid && !TGrid::Connect("alien://")) { + LOGP(fatal, "could not open alien connection to read {}", o->GetName()); + } + + if (checkSubDir) { + std::unique_ptr f(TFile::Open(o->GetName())); + if (!f->IsOpen() || f->IsZombie()) { + continue; + } + for (auto ok : *f->GetListOfKeys()) { + auto k = static_cast(ok); + if (std::string_view(k->GetClassName()) != "TDirectoryFile") { + continue; + } + auto df = f->Get(k->GetName()); + if (df->GetListOfKeys() && df->GetListOfKeys()->FindObject(treeName.data())) { + const auto fullTreePath = fmt::format("{}/{}", df->GetName(), treeName); + c->AddFile(o->GetName(), TTree::kMaxEntries, fullTreePath.data()); + LOGP(info, "Adding file '{}', with tree {}", o->GetName(), fullTreePath); + } + } + } else { + LOGP(info, "Adding file '{}'", o->GetName()); + c->AddFile(o->GetName()); } }