13#include "TTreeCache.h"
16#include <unordered_map>
20std::tuple<std::vector<root_utils::CollectionBranches>, std::vector<std::pair<std::string, detail::CollectionInfo>>>
22 const std::vector<root_utils::CollectionInfoT>& collInfo);
24GenericParameters ROOTFrameReader::readEventMetaData(ROOTFrameReader::CategoryInfo& catInfo) {
26 auto& paramBranches = catInfo.branches.back();
27 auto* branch = paramBranches.data;
29 GenericParameters params;
31 branch->SetAddress(&emd);
32 branch->GetEntry(catInfo.entry);
37 auto& catInfo = getCategoryInfo(name);
42 auto& catInfo = getCategoryInfo(name);
43 catInfo.entry = entNum;
51 if (catInfo.entry >= catInfo.chain->GetEntries()) {
56 for (
size_t i = 0; i < catInfo.storedClasses.size(); ++i) {
57 buffers.emplace(catInfo.storedClasses[i].first, getCollectionBuffers(catInfo, i));
60 auto parameters = readEventMetaData(catInfo);
63 return std::make_unique<ROOTFrameData>(std::move(buffers), catInfo.table, std::move(parameters));
68 const auto& name = catInfo.storedClasses[iColl].first;
69 const auto& [theClass, collectionClass, index] = catInfo.storedClasses[iColl].second;
70 auto& branches = catInfo.branches[index];
76 const bool isSubsetColl = theClass ==
nullptr;
78 collBuffers.data = theClass->New();
84 collection->setSubsetCollection(isSubsetColl);
86 auto tmpBuffers = collection->createBuffers();
87 collBuffers.createCollection = std::move(tmpBuffers.createCollection);
88 collBuffers.recast = std::move(tmpBuffers.recast);
90 if (
auto* refs = tmpBuffers.references) {
93 if (
auto* vminfo = tmpBuffers.vectorMembers) {
95 collBuffers.vectorMembers->reserve(vminfo->size());
97 for (
const auto& [type, _] : (*vminfo)) {
98 const auto* vecClass = TClass::GetClass((
"vector<" + type +
">").c_str());
99 collBuffers.vectorMembers->emplace_back(type, vecClass->New());
104 const auto localEntry = catInfo.chain->LoadTree(catInfo.entry);
109 if (localEntry == 0) {
113 if (
auto* refCollections = collBuffers.references) {
114 for (
size_t i = 0; i < refCollections->size(); ++i) {
121 if (
auto* vecMembers = collBuffers.vectorMembers) {
122 for (
size_t i = 0; i < vecMembers->size(); ++i) {
133 collBuffers.recast(collBuffers);
138ROOTFrameReader::CategoryInfo& ROOTFrameReader::getCategoryInfo(
const std::string& category) {
139 if (
auto it = m_categories.find(category); it != m_categories.end()) {
142 if (it->second.table ==
nullptr) {
143 initCategory(it->second, category);
150 static auto invalidCategory = CategoryInfo{
nullptr};
152 return invalidCategory;
155void ROOTFrameReader::initCategory(CategoryInfo& catInfo,
const std::string& category) {
156 catInfo.table = std::make_shared<podio::CollectionIDTable>();
157 auto* table = catInfo.table.get();
159 tableBranch->SetAddress(&table);
160 tableBranch->GetEntry(0);
163 auto collInfo =
new std::vector<root_utils::CollectionInfoT>();
164 collInfoBranch->SetAddress(&collInfo);
165 collInfoBranch->GetEntry(0);
167 std::tie(catInfo.branches, catInfo.storedClasses) =
173 root_utils::CollectionBranches paramBranches{};
175 catInfo.branches.push_back(paramBranches);
179 auto* branches = metaChain->GetListOfBranches();
180 std::vector<std::string> brNames;
181 brNames.reserve(branches->GetEntries());
183 for (
int i = 0; i < branches->GetEntries(); ++i) {
184 const std::string name = branches->At(i)->GetName();
185 const auto fUnder = name.find(
"___");
186 if (fUnder != std::string::npos) {
187 brNames.emplace_back(name.substr(0, fUnder));
191 std::sort(brNames.begin(), brNames.end());
192 brNames.erase(std::unique(brNames.begin(), brNames.end()), brNames.end());
201 m_metaChain = std::make_unique<TChain>(root_utils::metaTreeName);
206 for (
const auto& filename : filenames) {
207 if (!m_metaChain->Add(filename.c_str(), -1)) {
208 throw std::runtime_error(
"File " + filename +
" couldn't be found");
214 versionBranch->SetAddress(&versionPtr);
215 versionBranch->GetEntry(0);
222 edmDefBranch->SetAddress(&datamodelDefs);
223 edmDefBranch->GetEntry(0);
225 delete datamodelDefs;
232 for (
const auto& cat : m_availCategories) {
233 auto [it, _] = m_categories.try_emplace(cat, std::make_unique<TChain>(cat.c_str()));
234 for (
const auto& fn : filenames) {
235 it->second.chain->Add(fn.c_str());
241 if (
auto it = m_categories.find(name); it != m_categories.end()) {
242 return it->second.chain->GetEntries();
249 std::vector<std::string_view> cats;
250 cats.reserve(m_categories.size());
251 for (
const auto& [cat, _] : m_categories) {
252 cats.emplace_back(cat);
257std::tuple<std::vector<root_utils::CollectionBranches>, std::vector<std::pair<std::string, detail::CollectionInfo>>>
259 const std::vector<root_utils::CollectionInfoT>& collInfo) {
261 size_t collectionIndex{0};
262 std::vector<root_utils::CollectionBranches> collBranches;
263 collBranches.reserve(collInfo.size() + 1);
264 std::vector<std::pair<std::string, detail::CollectionInfo>> storedClasses;
265 storedClasses.reserve(collInfo.size());
267 for (
const auto& [collID, collType, isSubsetColl] : collInfo) {
270 const auto name = idTable.
name(collID);
273 const auto collectionClass = TClass::GetClass(collType.c_str());
279 collection->setSubsetCollection(isSubsetColl);
287 const auto buffers = collection->getBuffers();
288 for (
size_t i = 0; i < buffers.references->size(); ++i) {
293 for (
size_t i = 0; i < buffers.vectorMembers->size(); ++i) {
298 const std::string bufferClassName =
"std::vector<" + collection->getDataTypeName() +
">";
299 const auto bufferClass = isSubsetColl ? nullptr : TClass::GetClass(bufferClassName.c_str());
301 storedClasses.emplace_back(name, std::make_tuple(bufferClass, collectionClass, collectionIndex++));
302 collBranches.push_back(branches);
305 return {collBranches, storedClasses};
const std::string name(int collectionID) const
return name for given collection ID
std::vector< std::tuple< std::string, std::string > > MapType
The "map" type that is used internally.
std::unordered_map< std::string, podio::CollectionReadBuffers > BufferMap
void openFile(const std::string &filename)
std::unique_ptr< podio::ROOTFrameData > readNextEntry(const std::string &name)
void openFiles(const std::vector< std::string > &filenames)
std::vector< std::string_view > getAvailableCategories() const
Get the names of all the availalable Frame categories in the current file(s)
unsigned getEntries(const std::string &name) const
Returns number of entries for the given name.
std::unique_ptr< podio::ROOTFrameData > readEntry(const std::string &name, const unsigned entry)
std::string refBranch(const std::string &name, size_t index)
std::string vecBranch(const std::string &name, size_t index)
TBranch * getBranch(Tree *chain, const char *name)
void setCollectionAddresses(const BufferT &collBuffers, const CollectionBranches &branches)
std::string idTableName(const std::string &category)
void readBranchesData(const CollectionBranches &branches, Long64_t entry)
std::string collInfoName(const std::string &category)
std::tuple< std::vector< root_utils::CollectionBranches >, std::vector< std::pair< std::string, detail::CollectionInfo > > > createCollectionBranches(TChain *chain, const podio::CollectionIDTable &idTable, const std::vector< root_utils::CollectionInfoT > &collInfo)
std::vector< std::pair< std::string, void * > > VectorMembersInfo
std::vector< std::string > getAvailableCategories(TChain *metaChain)
std::vector< UVecPtr< podio::ObjectID > > CollRefCollection