File indexing completed on 2026-08-16 09:15:50
0001 #ifndef PODIO_READER_H
0002 #define PODIO_READER_H
0003
0004 #include "podio/Frame.h"
0005 #include "podio/podioVersion.h"
0006 #include "podio/utilities/ReaderUtils.h"
0007
0008 namespace podio {
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021 class Reader {
0022 private:
0023 struct ReaderConcept {
0024 virtual ~ReaderConcept() = default;
0025
0026 virtual podio::Frame readNextFrame(const std::string& name, const std::vector<std::string>& collsToRead) = 0;
0027 virtual podio::Frame readFrame(const std::string& name, size_t index,
0028 const std::vector<std::string>& collsToRead) = 0;
0029 virtual size_t getEntries(const std::string& name) const = 0;
0030 virtual podio::version::Version currentFileVersion() const = 0;
0031 virtual std::optional<podio::version::Version> currentFileVersion(const std::string& name) const = 0;
0032 virtual std::vector<std::string_view> getAvailableCategories() const = 0;
0033 virtual const std::string_view getDatamodelDefinition(const std::string& name) const = 0;
0034 virtual std::vector<std::string> getAvailableDatamodels() const = 0;
0035 };
0036
0037 private:
0038 template <typename T>
0039 struct ReaderModel final : ReaderConcept {
0040 ReaderModel(std::unique_ptr<T> reader) : m_reader(std::move(reader)) {
0041 }
0042 ReaderModel(const ReaderModel&) = delete;
0043 ReaderModel& operator=(const ReaderModel&) = delete;
0044 ReaderModel(ReaderModel&&) = default;
0045 ReaderModel& operator=(ReaderModel&&) = default;
0046
0047 ~ReaderModel() override = default;
0048
0049 podio::Frame readNextFrame(const std::string& name, const std::vector<std::string>& collsToRead) override {
0050 auto maybeFrame = m_reader->readNextEntry(name, collsToRead);
0051 if (maybeFrame) {
0052 return maybeFrame;
0053 }
0054 throw std::runtime_error("Failed reading category " + name + " (reading beyond bounds?)");
0055 }
0056
0057 podio::Frame readFrame(const std::string& name, size_t index,
0058 const std::vector<std::string>& collsToRead) override {
0059 auto maybeFrame = m_reader->readEntry(name, index, collsToRead);
0060 if (maybeFrame) {
0061 return maybeFrame;
0062 }
0063 throw std::runtime_error("Failed reading category " + name + " at frame " + std::to_string(index) +
0064 " (reading beyond bounds?)");
0065 }
0066 size_t getEntries(const std::string& name) const override {
0067 return m_reader->getEntries(name);
0068 }
0069 podio::version::Version currentFileVersion() const override {
0070 return m_reader->currentFileVersion();
0071 }
0072
0073 std::optional<podio::version::Version> currentFileVersion(const std::string& name) const override {
0074 return m_reader->currentFileVersion(name);
0075 }
0076
0077 std::vector<std::string_view> getAvailableCategories() const override {
0078 return m_reader->getAvailableCategories();
0079 }
0080
0081 const std::string_view getDatamodelDefinition(const std::string& name) const override {
0082 return m_reader->getDatamodelDefinition(name);
0083 }
0084
0085 std::vector<std::string> getAvailableDatamodels() const override {
0086 return m_reader->getAvailableDatamodels();
0087 }
0088
0089 std::unique_ptr<T> m_reader;
0090 };
0091
0092 std::unique_ptr<ReaderConcept> m_self{nullptr};
0093
0094 public:
0095
0096
0097
0098
0099 template <typename T>
0100 Reader(std::unique_ptr<T> actualReader);
0101
0102 Reader(const Reader&) = delete;
0103 Reader& operator=(const Reader&) = delete;
0104 Reader(Reader&&) = default;
0105 Reader& operator=(Reader&&) = default;
0106 ~Reader() = default;
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 podio::Frame readNextFrame(const std::string& name, const std::vector<std::string>& collsToRead = {}) {
0119 return m_self->readNextFrame(name, collsToRead);
0120 }
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 podio::Frame readNextEvent(const std::vector<std::string>& collsToRead = {}) {
0131 return readNextFrame(podio::Category::Event, collsToRead);
0132 }
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 podio::Frame readFrame(const std::string& name, size_t index, const std::vector<std::string>& collsToRead = {}) {
0146 return m_self->readFrame(name, index, collsToRead);
0147 }
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 podio::Frame readEvent(size_t index, const std::vector<std::string>& collsToRead = {}) {
0159 return readFrame(podio::Category::Event, index, collsToRead);
0160 }
0161
0162
0163
0164
0165
0166
0167 size_t getEntries(const std::string& name) const {
0168 return m_self->getEntries(name);
0169 }
0170
0171
0172
0173
0174 size_t getEvents() const {
0175 return getEntries(podio::Category::Event);
0176 }
0177
0178
0179
0180
0181
0182 podio::version::Version currentFileVersion() const {
0183 return m_self->currentFileVersion();
0184 }
0185
0186
0187
0188
0189
0190
0191
0192
0193 std::optional<podio::version::Version> currentFileVersion(const std::string& name) const {
0194 return m_self->currentFileVersion(name);
0195 }
0196
0197
0198
0199
0200 std::vector<std::string_view> getAvailableCategories() const {
0201 return m_self->getAvailableCategories();
0202 }
0203
0204
0205
0206
0207
0208
0209 const std::string_view getDatamodelDefinition(const std::string& name) const {
0210 return m_self->getDatamodelDefinition(name);
0211 }
0212
0213
0214
0215
0216 std::vector<std::string> getAvailableDatamodels() const {
0217 return m_self->getAvailableDatamodels();
0218 }
0219
0220 std::optional<std::map<std::string, SizeStats>> getSizeStats(std::string_view category);
0221 };
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 Reader makeReader(const std::string& filename);
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 Reader makeReader(const std::vector<std::string>& filenames);
0253
0254 }
0255
0256 #endif