16#include "TTreeCache.h"
18#include <unordered_map>
23 if (name != m_categoryName) {
29std::unique_ptr<podio::ROOTFrameData> ROOTLegacyReader::readEntry(
const std::string& name,
unsigned entry) {
30 if (name != m_categoryName) {
33 m_eventNumber = entry;
37std::unique_ptr<podio::ROOTFrameData> ROOTLegacyReader::readEntry() {
39 for (
const auto& collInfo : m_storedClasses) {
40 buffers.emplace(collInfo.first, getCollectionBuffers(collInfo));
43 auto parameters = readEventMetaData();
46 return std::make_unique<ROOTFrameData>(std::move(buffers), m_table, std::move(parameters));
50ROOTLegacyReader::getCollectionBuffers(
const std::pair<std::string, detail::CollectionInfo>& collInfo) {
51 const auto& name = collInfo.first;
52 const auto& [theClass, collectionClass, index] = collInfo.second;
53 auto& branches = m_collectionBranches[index];
59 const bool isSubsetColl = theClass ==
nullptr;
61 collBuffers.data = theClass->New();
67 collection->setSubsetCollection(isSubsetColl);
69 auto tmpBuffers = collection->createBuffers();
70 collBuffers.createCollection = std::move(tmpBuffers.createCollection);
71 collBuffers.recast = std::move(tmpBuffers.recast);
73 if (
auto* refs = tmpBuffers.references) {
76 if (
auto* vminfo = tmpBuffers.vectorMembers) {
78 collBuffers.vectorMembers->reserve(vminfo->size());
80 for (
const auto& [type, _] : (*vminfo)) {
81 const auto* vecClass = TClass::GetClass((
"vector<" + type +
">").c_str());
82 collBuffers.vectorMembers->emplace_back(type, vecClass->New());
87 const auto localEntry = m_chain->LoadTree(m_eventNumber);
92 if (localEntry == 0) {
96 if (
auto* refCollections = collBuffers.references) {
97 for (
size_t i = 0; i < refCollections->size(); ++i) {
104 if (
auto* vecMembers = collBuffers.vectorMembers) {
105 for (
size_t i = 0; i < vecMembers->size(); ++i) {
116 collBuffers.recast(collBuffers);
122 GenericParameters params;
123 auto [tree, entry] = getLocalTreeAndEntry(
"evt_metadata");
126 branch->SetAddress(&emd);
127 branch->GetEntry(entry);
136 m_chain = std::make_unique<TChain>(
"events");
137 for (
const auto& filename : filenames) {
140 if (!m_chain->Add(filename.c_str(), -1)) {
141 throw std::runtime_error(
"File " + filename +
" couldn't be found");
148 auto metadatatree =
static_cast<TTree*
>(m_chain->GetFile()->Get(
"metadata"));
149 m_table = std::make_shared<CollectionIDTable>();
150 auto* table = m_table.get();
151 metadatatree->SetBranchAddress(
"CollectionIDs", &table);
155 versionBranch->SetAddress(&versionPtr);
161 auto collectionInfo =
new std::vector<root_utils::CollectionInfoT>;
162 collInfoBranch->SetAddress(&collectionInfo);
163 metadatatree->GetEntry(0);
164 createCollectionBranches(*collectionInfo);
165 delete collectionInfo;
167 std::cout <<
"PODIO: Reconstructing CollectionTypeInfo branch from other sources in file: \'"
168 << m_chain->GetFile()->GetName() <<
"\'" << std::endl;
169 metadatatree->GetEntry(0);
171 createCollectionBranches(collectionInfo);
179 if (name != m_categoryName) {
182 return m_chain->GetEntries();
185void ROOTLegacyReader::createCollectionBranches(
const std::vector<root_utils::CollectionInfoT>& collInfo) {
186 size_t collectionIndex{0};
188 for (
const auto& [collID, collType, isSubsetColl] : collInfo) {
191 const auto name = m_table->name(collID);
193 root_utils::CollectionBranches branches{};
194 const auto collectionClass = TClass::GetClass(collType.c_str());
200 collection->setSubsetCollection(isSubsetColl);
208 const auto buffers = collection->getBuffers();
209 for (
size_t i = 0; i < buffers.references->size(); ++i) {
214 for (
size_t i = 0; i < buffers.vectorMembers->size(); ++i) {
219 const std::string bufferClassName =
"std::vector<" + collection->getDataTypeName() +
">";
220 const auto bufferClass = isSubsetColl ? nullptr : TClass::GetClass(bufferClassName.c_str());
222 m_storedClasses.emplace_back(name, std::make_tuple(bufferClass, collectionClass, collectionIndex++));
223 m_collectionBranches.push_back(branches);
227std::pair<TTree*, unsigned> ROOTLegacyReader::getLocalTreeAndEntry(
const std::string& treename) {
228 auto localEntry = m_chain->LoadTree(m_eventNumber);
229 auto* tree =
static_cast<TTree*
>(m_chain->GetFile()->Get(treename.c_str()));
230 return {tree, localEntry};
234 return {m_categoryName};
std::unordered_map< std::string, podio::CollectionReadBuffers > BufferMap
std::unique_ptr< podio::ROOTFrameData > readEntry(const std::string &, const unsigned entry)
void openFile(const std::string &filename)
void openFiles(const std::vector< std::string > &filenames)
std::unique_ptr< podio::ROOTFrameData > readNextEntry(const std::string &)
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 &) const
Returns number of entries for a given category.
auto reconstructCollectionInfo(TTree *eventTree, podio::CollectionIDTable const &idTable)
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)
void readBranchesData(const CollectionBranches &branches, Long64_t entry)
std::vector< std::pair< std::string, void * > > VectorMembersInfo
std::vector< UVecPtr< podio::ObjectID > > CollRefCollection