PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
rootUtils.h
Go to the documentation of this file.
1#ifndef PODIO_ROOT_UTILS_H // NOLINT(llvm-header-guard): internal headers confuse clang-tidy
2#define PODIO_ROOT_UTILS_H // NOLINT(llvm-header-guard): internal headers confuse clang-tidy
3
8
9#include "TBranch.h"
10#include "TChain.h"
11#include "TClass.h"
12#include "TTree.h"
13
14#include <iostream>
15#include <string>
16#include <string_view>
17#include <tuple>
18#include <vector>
19
20namespace podio::root_utils {
21/**
22 * The name of the meta data tree in podio ROOT files. This tree mainly stores
23 * meta data that is necessary for ROOT based I/O.
24 */
25constexpr static auto metaTreeName = "podio_metadata";
26
27/**
28 * The name of the branch in the TTree for each frame for storing the
29 * GenericParameters
30 */
31constexpr static auto paramBranchName = "PARAMETERS";
32
33/**
34 * The name of the branch into which we store the build version of podio at the
35 * time of writing the file
36 */
37constexpr static auto versionBranchName = "PodioBuildVersion";
38
39/**
40 * The name of the branch in which all the EDM names and their definitions are
41 * stored in the meta data tree.
42 */
43constexpr static auto edmDefBranchName = "EDMDefinitions";
44
45/**
46 * Name of the branch for storing the idTable for a given category in the meta
47 * data tree
48 */
49inline std::string idTableName(const std::string& category) {
50 constexpr static auto suffix = "___idTable";
51 return category + suffix;
52}
53
54/**
55 * Name of the branch for storing the collection info for a given category in
56 * the meta data tree
57 */
58inline std::string collInfoName(const std::string& category) {
59 constexpr static auto suffix = "___CollectionTypeInfo";
60 return category + suffix;
61}
62
63// Workaround slow branch retrieval for 6.22/06 performance degradation
64// see: https://root-forum.cern.ch/t/serious-degradation-of-i-o-performance-from-6-20-04-to-6-22-06/43584/10
65template <class Tree>
66TBranch* getBranch(Tree* chain, const char* name) {
67 return static_cast<TBranch*>(chain->GetListOfBranches()->FindObject(name));
68}
69
70template <typename Tree>
71TBranch* getBranch(Tree* chain, const std::string& name) {
72 return getBranch(chain, name.c_str());
73}
74
75inline std::string refBranch(const std::string& name, size_t index) {
76 return name + "#" + std::to_string(index);
77}
78
79inline std::string vecBranch(const std::string& name, size_t index) {
80 return name + "_" + std::to_string(index);
81}
82
83template <typename BufferT>
84inline void setCollectionAddresses(const BufferT& collBuffers, const CollectionBranches& branches) {
85
86 if (auto buffer = collBuffers.data) {
87 branches.data->SetAddress(buffer);
88 }
89
90 if (auto refCollections = collBuffers.references) {
91 for (size_t i = 0; i < refCollections->size(); ++i) {
92 branches.refs[i]->SetAddress(&(*refCollections)[i]);
93 }
94 }
95
96 if (auto vecMembers = collBuffers.vectorMembers) {
97 for (size_t i = 0; i < vecMembers->size(); ++i) {
98 branches.vecs[i]->SetAddress((*vecMembers)[i].second);
99 }
100 }
101}
102
103// A collection of additional information that describes the collection: the
104// collectionID, the collection (data) type, and whether it is a subset
105// collection
106using CollectionInfoT = std::tuple<int, std::string, bool>;
107
108inline void readBranchesData(const CollectionBranches& branches, Long64_t entry) {
109 // Read all data
110 if (branches.data) {
111 branches.data->GetEntry(entry);
112 }
113 for (auto* br : branches.refs) {
114 br->GetEntry(entry);
115 }
116 for (auto* br : branches.vecs) {
117 br->GetEntry(entry);
118 }
119}
120
121/**
122 * reconstruct the collection info from information that is available from other
123 * trees in the file.
124 *
125 * NOTE: This function is only supposed to be called if there is no
126 * "CollectionTypeInfo" branch in the metadata tree, as it assumes that the file
127 * has been written with podio previous to #197 where there were no subset
128 * collections
129 */
130inline auto reconstructCollectionInfo(TTree* eventTree, podio::CollectionIDTable const& idTable) {
131 std::vector<CollectionInfoT> collInfo;
132
133 for (size_t iColl = 0; iColl < idTable.names().size(); ++iColl) {
134 const auto collID = idTable.ids()[iColl];
135 const auto& name = idTable.names()[iColl];
136
137 if (auto branch = getBranch(eventTree, name.c_str())) {
138 const std::string_view bufferClassName = branch->GetClassName();
139 // this comes with vector<...Data>, where we only care about the ...
140 std::string_view dataClass = bufferClassName;
141 dataClass.remove_suffix(5);
142 const auto collClass = std::string(dataClass.substr(7)) + "Collection";
143 // Assume that there are no subset collections in "old files"
144 collInfo.emplace_back(collID, std::move(collClass), false);
145 } else {
146 std::cerr << "Problems reconstructing collection info for collection: \'" << name << "\'\n";
147 }
148 }
149
150 return collInfo;
151}
152
153} // namespace podio::root_utils
154
155#endif
const std::vector< int > & ids() const
return the ids
const std::vector< std::string > & names() const
return registered names
auto reconstructCollectionInfo(TTree *eventTree, podio::CollectionIDTable const &idTable)
Definition: rootUtils.h:130
std::string refBranch(const std::string &name, size_t index)
Definition: rootUtils.h:75
std::tuple< int, std::string, bool > CollectionInfoT
Definition: rootUtils.h:106
std::string vecBranch(const std::string &name, size_t index)
Definition: rootUtils.h:79
TBranch * getBranch(Tree *chain, const char *name)
Definition: rootUtils.h:66
void setCollectionAddresses(const BufferT &collBuffers, const CollectionBranches &branches)
Definition: rootUtils.h:84
std::string idTableName(const std::string &category)
Definition: rootUtils.h:49
void readBranchesData(const CollectionBranches &branches, Long64_t entry)
Definition: rootUtils.h:108
std::string collInfoName(const std::string &category)
Definition: rootUtils.h:58