14#include "TTreeCache.h"
23std::pair<TTree*, unsigned> ROOTReader::getLocalTreeAndEntry(
const std::string& treename) {
24 auto localEntry = m_chain->LoadTree(m_eventNumber);
25 auto* tree =
static_cast<TTree*
>(m_chain->GetFile()->Get(treename.c_str()));
26 return {tree, localEntry};
29GenericParameters* ROOTReader::readEventMetaData() {
30 auto* emd =
new GenericParameters();
31 auto [evt_metadatatree, entry] = getLocalTreeAndEntry(
"evt_metadata");
33 branch->SetAddress(&emd);
34 evt_metadatatree->GetEntry(entry);
37std::map<int, GenericParameters>* ROOTReader::readCollectionMetaData() {
38 auto* emd =
new std::map<int, GenericParameters>;
39 auto* col_metadatatree = getLocalTreeAndEntry(
"col_metadata").first;
41 branch->SetAddress(&emd);
42 col_metadatatree->GetEntry(0);
45std::map<int, GenericParameters>* ROOTReader::readRunMetaData() {
46 auto* emd =
new std::map<int, GenericParameters>;
47 auto* run_metadatatree = getLocalTreeAndEntry(
"run_metadata").first;
49 branch->SetAddress(&emd);
50 run_metadatatree->GetEntry(0);
54CollectionBase* ROOTReader::readCollection(
const std::string& name) {
57 std::find_if(begin(m_inputs), end(m_inputs), [&name](
const ROOTReader::Input& t) {
return t.second == name; });
58 if (p != end(m_inputs)) {
63 if (
const auto& info = m_storedClasses.find(name); info != m_storedClasses.end()) {
64 return getCollection(*info);
72CollectionBase* ROOTReader::getCollection(
const std::pair<std::string, CollectionInfo>& collInfo) {
73 const auto& name = collInfo.first;
74 const auto& [theClass, collectionClass, index] = collInfo.second;
75 auto& branches = m_collectionBranches[index];
77 auto* collection =
static_cast<CollectionBase*
>(collectionClass->New());
78 auto collBuffers = collection->getBuffers();
82 collBuffers.data = theClass->New();
84 collection->setSubsetCollection();
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) {
115 return readCollectionData(branches, collection, localEntry, name);
118CollectionBase* ROOTReader::readCollectionData(
const root_utils::CollectionBranches& branches,
119 CollectionBase* collection, Long64_t entry,
const std::string& name) {
122 branches.data->GetEntry(entry);
124 for (
auto* br : branches.refs) {
127 for (
auto* br : branches.vecs) {
132 const auto id = m_table->collectionID(name);
133 collection->setID(
id);
134 collection->prepareAfterRead();
136 m_inputs.emplace_back(std::make_pair(collection, name));
145 m_chain =
new TChain(
"events");
146 for (
const auto& filename : filenames) {
149 if (!m_chain->Add(filename.c_str(), -1)) {
151 throw std::runtime_error(
"File " + filename +
" couldn't be found");
158 auto metadatatree =
static_cast<TTree*
>(m_chain->GetFile()->Get(
"metadata"));
159 m_table = std::make_shared<podio::CollectionIDTable>();
160 auto* table = m_table.get();
161 metadatatree->SetBranchAddress(
"CollectionIDs", &table);
165 versionBranch->SetAddress(&versionPtr);
171 auto collectionInfo =
new std::vector<root_utils::CollectionInfoT>;
172 collInfoBranch->SetAddress(&collectionInfo);
173 metadatatree->GetEntry(0);
174 createCollectionBranches(*collectionInfo);
175 delete collectionInfo;
177 std::cout <<
"PODIO: Reconstructing CollectionTypeInfo branch from other sources in file: \'"
178 << m_chain->GetFile()->GetName() <<
"\'" << std::endl;
179 metadatatree->GetEntry(0);
181 createCollectionBranches(collectionInfo);
197 m_chain->GetEntry(m_eventNumber);
199 for (
auto inputs : m_inputs) {
200 inputs.first->prepareAfterRead();
209 return m_chain->GetFile()->IsOpen() && !m_chain->GetFile()->IsZombie();
218 return m_chain->GetEntries();
222 m_eventNumber = eventNumber;
226void ROOTReader::createCollectionBranches(
const std::vector<root_utils::CollectionInfoT>& collInfo) {
227 size_t collectionIndex{0};
229 for (
const auto& [collID, collType, isSubsetColl] : collInfo) {
232 const auto name = m_table->name(collID);
234 root_utils::CollectionBranches branches{};
235 const auto collectionClass = TClass::GetClass(collType.c_str());
239 if (!collectionClass) {
240 std::cerr <<
"PODIO: Cannot create the collection type \'" << collType <<
"\' stored in branch \'" << name
241 <<
"\'. Contents of this branch cannot be read." << std::endl;
248 collection->setSubsetCollection(isSubsetColl);
256 const auto buffers = collection->getBuffers();
257 for (
size_t i = 0; i < buffers.references->size(); ++i) {
262 for (
size_t i = 0; i < buffers.vectorMembers->size(); ++i) {
267 const std::string bufferClassName =
"std::vector<" + collection->getDataTypeName() +
">";
268 const auto bufferClass = isSubsetColl ? nullptr : TClass::GetClass(bufferClassName.c_str());
270 m_storedClasses.emplace(name, std::make_tuple(bufferClass, collectionClass, collectionIndex++));
271 m_collectionBranches.push_back(branches);
void openFile(const std::string &filename) override
void goToEvent(unsigned evnum) override
Preparing to read a given event.
unsigned getEntries() const override
Returns number of entries in the TTree.
void readEvent() override
Read all collections requested.
void openFiles(const std::vector< std::string > &filenames)
bool isValid() const override
Check if TFile is valid.
void closeFile() override
void endOfEvent() override
Preparing to read next event.
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)