Skip to content

Commit 1c02d64

Browse files
committed
Homogenize behavior of TTree and RNTuple reader
1 parent 9cd7256 commit 1c02d64

File tree

4 files changed

+30
-26
lines changed

4 files changed

+30
-26
lines changed

include/podio/ROOTReader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ class ROOTReader {
195195
const std::vector<std::string>& collsToRead);
196196

197197
/// Get / read the buffers at index iColl in the passed category information
198-
podio::CollectionReadBuffers getCollectionBuffers(CategoryInfo& catInfo, size_t iColl, bool reloadBranches,
199-
unsigned int localEntry);
198+
std::optional<podio::CollectionReadBuffers> getCollectionBuffers(CategoryInfo& catInfo, size_t iColl,
199+
bool reloadBranches, unsigned int localEntry);
200200

201201
std::unique_ptr<TChain> m_metaChain{nullptr}; ///< The metadata tree
202202
std::unordered_map<std::string, CategoryInfo> m_categories{}; ///< All categories

src/RNTupleReader.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ std::unique_ptr<ROOTFrameData> RNTupleReader::readEntry(const std::string& categ
185185
const auto& collType = coll.dataType;
186186
const auto& bufferFactory = podio::CollectionBufferFactory::instance();
187187
const auto maybeBuffers = bufferFactory.createBuffers(collType, coll.schemaVersion, coll.isSubset);
188-
const auto collBuffers = maybeBuffers.value_or(podio::CollectionReadBuffers{});
189188

190189
if (!maybeBuffers) {
191-
std::cout << "WARNING: Buffers couldn't be created for collection " << coll.name << " of type " << coll.dataType
190+
std::cerr << "WARNING: Buffers couldn't be created for collection " << coll.name << " of type " << coll.dataType
192191
<< " and schema version " << coll.schemaVersion << std::endl;
193192
return nullptr;
194193
}
194+
const auto& collBuffers = maybeBuffers.value();
195195

196196
if (coll.isSubset) {
197197
const auto brName = root_utils::subsetBranch(coll.name);

src/ROOTReader.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ std::unique_ptr<ROOTFrameData> ROOTReader::readEntry(ROOTReader::CategoryInfo& c
133133
if (!collsToRead.empty() && std::ranges::find(collsToRead, catInfo.storedClasses[i].name) == collsToRead.end()) {
134134
continue;
135135
}
136-
buffers.emplace(catInfo.storedClasses[i].name, getCollectionBuffers(catInfo, i, reloadBranches, localEntry));
136+
auto collBuffers = getCollectionBuffers(catInfo, i, reloadBranches, localEntry);
137+
if (!collBuffers) {
138+
return nullptr;
139+
}
140+
buffers.emplace(catInfo.storedClasses[i].name, collBuffers.value());
137141
}
138142

139143
auto parameters = readEntryParameters(catInfo, reloadBranches, localEntry);
@@ -142,17 +146,22 @@ std::unique_ptr<ROOTFrameData> ROOTReader::readEntry(ROOTReader::CategoryInfo& c
142146
return std::make_unique<ROOTFrameData>(std::move(buffers), catInfo.table, std::move(parameters));
143147
}
144148

145-
podio::CollectionReadBuffers ROOTReader::getCollectionBuffers(ROOTReader::CategoryInfo& catInfo, size_t iColl,
146-
bool reloadBranches, unsigned int localEntry) {
149+
std::optional<podio::CollectionReadBuffers> ROOTReader::getCollectionBuffers(ROOTReader::CategoryInfo& catInfo,
150+
size_t iColl, bool reloadBranches,
151+
unsigned int localEntry) {
147152
const auto& name = catInfo.storedClasses[iColl].name;
148153
const auto& [collType, isSubsetColl, schemaVersion, index] = catInfo.storedClasses[iColl].info;
149154
auto& branches = catInfo.branches[index];
150155

151156
const auto& bufferFactory = podio::CollectionBufferFactory::instance();
152157
auto maybeBuffers = bufferFactory.createBuffers(collType, schemaVersion, isSubsetColl);
153158

154-
// TODO: Error handling of empty optional
155-
auto collBuffers = maybeBuffers.value_or(podio::CollectionReadBuffers{});
159+
if (!maybeBuffers) {
160+
std::cerr << "WARNING: Buffers couldn't be created for collection " << name << " of type " << collType
161+
<< " and schema version " << schemaVersion << std::endl;
162+
return std::nullopt;
163+
}
164+
auto collBuffers = maybeBuffers.value();
156165

157166
if (reloadBranches) {
158167
root_utils::resetBranches(catInfo.chain.get(), branches, name);

tools/src/podio-dump-tool.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,26 +248,21 @@ int main(int argc, char* argv[]) {
248248
// We strip the executable name off directly for parsing
249249
const auto args = parseArgs({argv + 1, argv + argc});
250250

251-
try {
252-
auto reader = podio::makeReader(args.inputFile);
253-
if (!args.dumpEDM.empty()) {
254-
return dumpEDMDefinition(reader, args.dumpEDM);
255-
}
251+
auto reader = podio::makeReader(args.inputFile);
252+
if (!args.dumpEDM.empty()) {
253+
return dumpEDMDefinition(reader, args.dumpEDM);
254+
}
256255

257-
printGeneralInfo(reader, args.inputFile);
256+
printGeneralInfo(reader, args.inputFile);
258257

259-
for (const auto event : args.events) {
260-
try {
261-
const auto& frame = reader.readFrame(args.category, event);
262-
printFrame(frame, args.category, event, args.detailed);
263-
} catch (std::runtime_error& err) {
264-
fmt::println(stderr, "{}", err.what());
265-
return 1;
266-
}
258+
for (const auto event : args.events) {
259+
try {
260+
const auto& frame = reader.readFrame(args.category, event);
261+
printFrame(frame, args.category, event, args.detailed);
262+
} catch (std::runtime_error& err) {
263+
fmt::println(stderr, "{}", err.what());
264+
return 1;
267265
}
268-
} catch (const std::bad_function_call&) {
269-
fmt::println(stderr, "ERROR: Cannot create the necessary structures to read a collection from file");
270-
return 1;
271266
}
272267

273268
return 0;

0 commit comments

Comments
 (0)