Skip to content

Commit db109c8

Browse files
jmcarcelltmadlener
andauthored
Clean up the ROOT readers and writers (#777)
* Clean up the ROOT readers and writers - Add const where possible - Use functions from `std::ranges` when possible - Do not use `new` and `delete` when it's not needed (`ROOTReader.cc`) - Clean up includes - Change some indexes to size_t instead of int to avoid implicit conversions (doesn't change anything) * Fix gcc13 * Fix pre-commit * Fix pre-commit * Add missing & Co-authored-by: Thomas Madlener <thomas.madlener@desy.de> * Remove unnecessary check for m_fileVersion and add a few * after auto --------- Co-authored-by: jmcarcell <jmcarcell@users.noreply.github.com> Co-authored-by: Thomas Madlener <thomas.madlener@desy.de>
1 parent e271749 commit db109c8

File tree

8 files changed

+121
-115
lines changed

8 files changed

+121
-115
lines changed

include/podio/RNTupleReader.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define PODIO_RNTUPLEREADER_H
33

44
#include "podio/ROOTFrameData.h"
5-
#include "podio/SchemaEvolution.h"
65
#include "podio/podioVersion.h"
76
#include "podio/utilities/DatamodelRegistryIOHelpers.h"
87
#include "podio/utilities/RootHelpers.h"
@@ -150,10 +149,11 @@ class RNTupleReader {
150149
/**
151150
* Read and reconstruct the generic parameters of the Frame
152151
*/
153-
GenericParameters readEventMetaData(const std::string& name, unsigned localEntry, unsigned readerIndex);
152+
GenericParameters readEventMetaData(const std::string& name, const unsigned localEntry, const unsigned readerIndex);
154153

155154
template <typename T>
156-
void readParams(const std::string& name, unsigned entNum, unsigned readerIndex, GenericParameters& params);
155+
void readParams(const std::string& name, const unsigned entNum, const unsigned readerIndex,
156+
GenericParameters& params);
157157

158158
std::unique_ptr<root_compat::RNTupleReader> m_metadata{};
159159

include/podio/RNTupleWriter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define PODIO_RNTUPLEWRITER_H
33

44
#include "podio/Frame.h"
5-
#include "podio/SchemaEvolution.h"
65
#include "podio/utilities/DatamodelRegistryIOHelpers.h"
76
#include "podio/utilities/RootHelpers.h"
87

include/podio/ROOTReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "TChain.h"
1010

1111
#include <memory>
12+
#include <optional>
1213
#include <string>
1314
#include <string_view>
1415
#include <tuple>

include/podio/ROOTWriter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef PODIO_ROOTWRITER_H
22
#define PODIO_ROOTWRITER_H
33

4-
#include "podio/CollectionIDTable.h"
54
#include "podio/utilities/DatamodelRegistryIOHelpers.h"
65
#include "podio/utilities/RootHelpers.h"
76

src/RNTupleReader.cc

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "podio/RNTupleReader.h"
22
#include "podio/CollectionBufferFactory.h"
33
#include "podio/CollectionBuffers.h"
4-
#include "podio/CollectionIDTable.h"
54
#include "podio/DatamodelRegistry.h"
65
#include "podio/GenericParameters.h"
76
#include "podio/utilities/RootHelpers.h"
@@ -11,7 +10,11 @@
1110

1211
#include <algorithm>
1312
#include <cstdint>
13+
#include <iostream>
1414
#include <memory>
15+
#include <string>
16+
#include <tuple>
17+
#include <vector>
1518

1619
// Adjust for the move of this out of ROOT v7 in
1720
// https://github.com/root-project/root/pull/17281
@@ -24,15 +27,16 @@ using ROOT::Experimental::RException;
2427
namespace podio {
2528

2629
template <typename T>
27-
void RNTupleReader::readParams(const std::string& name, unsigned localEntry, unsigned readerIndex,
30+
void RNTupleReader::readParams(const std::string& name, const unsigned localEntry, const unsigned readerIndex,
2831
GenericParameters& params) {
2932
auto keyView = m_readers[name][readerIndex]->GetView<std::vector<std::string>>(root_utils::getGPKeyName<T>());
3033
auto valueView = m_readers[name][readerIndex]->GetView<std::vector<std::vector<T>>>(root_utils::getGPValueName<T>());
3134

3235
params.loadFrom(keyView(localEntry), valueView(localEntry));
3336
}
3437

35-
GenericParameters RNTupleReader::readEventMetaData(const std::string& name, unsigned localEntry, unsigned readerIndex) {
38+
GenericParameters RNTupleReader::readEventMetaData(const std::string& name, const unsigned localEntry,
39+
const unsigned readerIndex) {
3640
GenericParameters params;
3741

3842
readParams<int>(name, localEntry, readerIndex, params);
@@ -44,11 +48,11 @@ GenericParameters RNTupleReader::readEventMetaData(const std::string& name, unsi
4448
}
4549

4650
bool RNTupleReader::initCategory(const std::string& category) {
47-
if (std::find(m_availableCategories.begin(), m_availableCategories.end(), category) == m_availableCategories.end()) {
51+
if (std::ranges::find(m_availableCategories, category) == m_availableCategories.end()) {
4852
return false;
4953
}
5054
// Assume that the metadata is the same in all files
51-
auto filename = m_filenames[0];
55+
const auto& filename = m_filenames[0];
5256

5357
auto collInfo = m_metadata_readers[filename]->GetView<std::vector<root_utils::CollectionWriteInfo>>(
5458
{root_utils::collInfoName(category)});
@@ -66,7 +70,7 @@ void RNTupleReader::openFile(const std::string& filename) {
6670
void RNTupleReader::openFiles(const std::vector<std::string>& filenames) {
6771

6872
m_filenames.insert(m_filenames.end(), filenames.begin(), filenames.end());
69-
for (auto& filename : filenames) {
73+
for (const auto& filename : filenames) {
7074
if (m_metadata_readers.find(filename) == m_metadata_readers.end()) {
7175
m_metadata_readers[filename] = root_compat::RNTupleReader::Open(root_utils::metaTreeName, filename);
7276
}
@@ -75,7 +79,7 @@ void RNTupleReader::openFiles(const std::vector<std::string>& filenames) {
7579
m_metadata = root_compat::RNTupleReader::Open(root_utils::metaTreeName, filenames[0]);
7680

7781
auto versionView = m_metadata->GetView<std::vector<uint16_t>>(root_utils::versionBranchName);
78-
auto version = versionView(0);
82+
const auto version = versionView(0);
7983

8084
m_fileVersion = podio::version::Version{version[0], version[1], version[2]};
8185

@@ -85,7 +89,7 @@ void RNTupleReader::openFiles(const std::vector<std::string>& filenames) {
8589
for (const auto& [name, _] : edm) {
8690
try {
8791
auto edmVersionView = m_metadata->GetView<std::vector<uint16_t>>(root_utils::edmVersionBranchName(name));
88-
auto edmVersion = edmVersionView(0);
92+
const auto edmVersion = edmVersionView(0);
8993
edmVersions.emplace_back(name, podio::version::Version{edmVersion[0], edmVersion[1], edmVersion[2]});
9094
} catch (const RException&) {
9195
}
@@ -100,7 +104,7 @@ unsigned RNTupleReader::getEntries(const std::string& name) {
100104
if (m_readers.find(name) == m_readers.end()) {
101105
m_readerEntries[name].reserve(m_filenames.size() + 1);
102106
m_readerEntries[name].push_back(0);
103-
for (auto& filename : m_filenames) {
107+
for (const auto& filename : m_filenames) {
104108
try {
105109
m_readers[name].emplace_back(root_compat::RNTupleReader::Open(name, filename));
106110
m_readerEntries[name].push_back(m_readerEntries[name].back() + m_readers[name].back()->GetNEntries());
@@ -159,25 +163,25 @@ std::unique_ptr<ROOTFrameData> RNTupleReader::readEntry(const std::string& categ
159163
// m_readerEntries contains the accumulated entries for all the readers
160164
// therefore, the first number that is lower or equal to the entry number
161165
// is at the index of the reader that contains the entry
162-
auto upper = std::ranges::upper_bound(m_readerEntries[category], entNum);
163-
auto localEntry = entNum - *(upper - 1);
164-
auto readerIndex = upper - 1 - m_readerEntries[category].begin();
166+
const auto upper = std::ranges::upper_bound(m_readerEntries[category], entNum);
167+
const auto localEntry = entNum - *(upper - 1);
168+
const auto readerIndex = upper - 1 - m_readerEntries[category].begin();
165169

166170
ROOTFrameData::BufferMap buffers;
167171
// We need to create a non-bare entry here, because the entries for the
168172
// parameters are not explicitly (re)set and we need them default initialized.
169173
// In principle we would only need a bare entry for the collection data, since
170174
// we set all the fields there in any case.
171-
auto dentry = m_readers[category][readerIndex]->GetModel().CreateEntry();
175+
const auto dentry = m_readers[category][readerIndex]->GetModel().CreateEntry();
172176

173177
for (const auto& coll : collInfo) {
174178
if (!collsToRead.empty() && std::ranges::find(collsToRead, coll.name) == collsToRead.end()) {
175179
continue;
176180
}
177181
const auto& collType = coll.dataType;
178182
const auto& bufferFactory = podio::CollectionBufferFactory::instance();
179-
auto maybeBuffers = bufferFactory.createBuffers(collType, coll.schemaVersion, coll.isSubset);
180-
auto collBuffers = maybeBuffers.value_or(podio::CollectionReadBuffers{});
183+
const auto maybeBuffers = bufferFactory.createBuffers(collType, coll.schemaVersion, coll.isSubset);
184+
const auto collBuffers = maybeBuffers.value_or(podio::CollectionReadBuffers{});
181185

182186
if (!maybeBuffers) {
183187
std::cout << "WARNING: Buffers couldn't be created for collection " << coll.name << " of type " << coll.dataType
@@ -186,8 +190,8 @@ std::unique_ptr<ROOTFrameData> RNTupleReader::readEntry(const std::string& categ
186190
}
187191

188192
if (coll.isSubset) {
189-
auto brName = root_utils::subsetBranch(coll.name);
190-
auto vec = new std::vector<podio::ObjectID>;
193+
const auto brName = root_utils::subsetBranch(coll.name);
194+
const auto vec = new std::vector<podio::ObjectID>;
191195
dentry->BindRawPtr(brName, vec);
192196
collBuffers.references->at(0) = std::unique_ptr<std::vector<podio::ObjectID>>(vec);
193197
} else {
@@ -196,7 +200,7 @@ std::unique_ptr<ROOTFrameData> RNTupleReader::readEntry(const std::string& categ
196200
const auto relVecNames = podio::DatamodelRegistry::instance().getRelationNames(collType);
197201
for (size_t j = 0; j < relVecNames.relations.size(); ++j) {
198202
const auto relName = relVecNames.relations[j];
199-
auto vec = new std::vector<podio::ObjectID>;
203+
const auto vec = new std::vector<podio::ObjectID>;
200204
const auto brName = root_utils::refBranch(coll.name, relName);
201205
dentry->BindRawPtr(brName, vec);
202206
collBuffers.references->at(j) = std::unique_ptr<std::vector<podio::ObjectID>>(vec);

src/RNTupleWriter.cc

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "podio/RNTupleWriter.h"
22
#include "podio/DatamodelRegistry.h"
3-
#include "podio/SchemaEvolution.h"
43
#include "podio/podioVersion.h"
54
#include "podio/utilities/RootHelpers.h"
65
#include "rootUtils.h"
@@ -67,7 +66,7 @@ void RNTupleWriter::writeFrame(const podio::Frame& frame, const std::string& cat
6766

6867
// Use the writer as proxy to check whether this category has been initialized
6968
// already and do so if not
70-
const bool new_category = (catInfo.writer == nullptr);
69+
const bool new_category = catInfo.writer == nullptr;
7170
if (new_category) {
7271
// This is the minimal information that we need for now
7372
catInfo.names = podio::utils::sortAlphabeticaly(collsToWrite);
@@ -78,7 +77,7 @@ void RNTupleWriter::writeFrame(const podio::Frame& frame, const std::string& cat
7877
// Only loop over the collections that were requested in the first Frame of
7978
// this category
8079
for (const auto& name : catInfo.names) {
81-
auto* coll = frame.getCollectionForWrite(name);
80+
const auto* coll = frame.getCollectionForWrite(name);
8281
if (!coll) {
8382
// Make sure all collections that we want to write are actually available
8483
// NOLINTNEXTLINE(performance-inefficient-string-concatenation)
@@ -105,37 +104,37 @@ void RNTupleWriter::writeFrame(const podio::Frame& frame, const std::string& cat
105104
}
106105
}
107106

108-
auto entry = m_categories[category].writer->GetModel().CreateBareEntry();
107+
const auto entry = m_categories[category].writer->GetModel().CreateBareEntry();
109108

110109
RNTupleWriteOptions options;
111110
options.SetCompression(ROOT::RCompressionSetting::EDefaults::kUseGeneralPurpose);
112111

113112
for (const auto& [name, coll] : collections) {
114-
auto collBuffers = coll->getBuffers();
113+
const auto collBuffers = coll->getBuffers();
115114
if (collBuffers.vecPtr) {
116115
entry->BindRawPtr(name, static_cast<void*>(collBuffers.vecPtr));
117116
}
118117

119118
if (coll->isSubsetCollection()) {
120-
auto& refColl = (*collBuffers.references)[0];
119+
const auto& refColl = (*collBuffers.references)[0];
121120
const auto brName = root_utils::subsetBranch(name);
122121
entry->BindRawPtr(brName, refColl.get());
123122

124123
} else {
125124

126125
const auto relVecNames = podio::DatamodelRegistry::instance().getRelationNames(coll->getValueTypeName());
127-
if (auto refColls = collBuffers.references) {
128-
int i = 0;
129-
for (auto& c : (*refColls)) {
126+
if (const auto refColls = collBuffers.references) {
127+
size_t i = 0;
128+
for (const auto& c : *refColls) {
130129
const auto brName = root_utils::refBranch(name, relVecNames.relations[i]);
131130
entry->BindRawPtr(brName, c.get());
132131
++i;
133132
}
134133
}
135134

136-
if (auto vmInfo = collBuffers.vectorMembers) {
137-
int i = 0;
138-
for (auto& [type, vec] : (*vmInfo)) {
135+
if (const auto vmInfo = collBuffers.vectorMembers) {
136+
size_t i = 0;
137+
for (const auto& [type, vec] : *vmInfo) {
139138
const auto typeName = "vector<" + type + ">";
140139
const auto brName = root_utils::vecBranch(name, relVecNames.vectorMembers[i]);
141140
auto ptr = *static_cast<std::vector<int>**>(vec);
@@ -163,41 +162,41 @@ std::unique_ptr<root_compat::RNTupleModel>
163162
RNTupleWriter::createModels(const std::vector<root_utils::StoreCollection>& collections) {
164163
auto model = root_compat::RNTupleModel::CreateBare();
165164

166-
for (auto& [name, coll] : collections) {
165+
for (const auto& [name, coll] : collections) {
167166
// For the first entry in each category we also record the datamodel
168167
// definition
169168
m_datamodelCollector.registerDatamodelDefinition(coll, name);
170169

171170
const auto collBuffers = coll->getBuffers();
172171

173172
if (collBuffers.vecPtr) {
174-
auto collClassName = "std::vector<" + std::string(coll->getDataTypeName()) + ">";
173+
const auto collClassName = "std::vector<" + std::string(coll->getDataTypeName()) + ">";
175174
auto field = RFieldBase::Create(name, collClassName).Unwrap();
176175
model->AddField(std::move(field));
177176
}
178177

179178
if (coll->isSubsetCollection()) {
180179
const auto brName = root_utils::subsetBranch(name);
181-
auto collClassName = "vector<podio::ObjectID>";
180+
const auto collClassName = "vector<podio::ObjectID>";
182181
auto field = RFieldBase::Create(brName, collClassName).Unwrap();
183182
model->AddField(std::move(field));
184183
} else {
185184

186185
const auto relVecNames = podio::DatamodelRegistry::instance().getRelationNames(coll->getValueTypeName());
187-
if (auto refColls = collBuffers.references) {
188-
int i = 0;
189-
for (auto& c [[maybe_unused]] : (*refColls)) {
186+
if (const auto refColls = collBuffers.references) {
187+
size_t i = 0;
188+
for (const auto& c [[maybe_unused]] : *refColls) {
190189
const auto brName = root_utils::refBranch(name, relVecNames.relations[i]);
191-
auto collClassName = "vector<podio::ObjectID>";
190+
const auto collClassName = "vector<podio::ObjectID>";
192191
auto field = RFieldBase::Create(brName, collClassName).Unwrap();
193192
model->AddField(std::move(field));
194193
++i;
195194
}
196195
}
197196

198-
if (auto vminfo = collBuffers.vectorMembers) {
199-
int i = 0;
200-
for (auto& [type, vec] : (*vminfo)) {
197+
if (const auto vminfo = collBuffers.vectorMembers) {
198+
size_t i = 0;
199+
for (const auto& [type, vec] : *vminfo) {
201200
const auto typeName = "vector<" + type + ">";
202201
const auto brName = root_utils::vecBranch(name, relVecNames.vectorMembers[i]);
203202
auto field = RFieldBase::Create(brName, typeName).Unwrap();
@@ -227,46 +226,48 @@ RNTupleWriter::createModels(const std::vector<root_utils::StoreCollection>& coll
227226
}
228227

229228
RNTupleWriter::CategoryInfo& RNTupleWriter::getCategoryInfo(const std::string& category) {
230-
if (auto it = m_categories.find(category); it != m_categories.end()) {
229+
if (const auto it = m_categories.find(category); it != m_categories.end()) {
231230
return it->second;
232231
}
233232

234-
auto [it, _] = m_categories.try_emplace(category, CategoryInfo{});
233+
const auto [it, _] = m_categories.try_emplace(category, CategoryInfo{});
235234
return it->second;
236235
}
237236

238237
void RNTupleWriter::finish() {
239238
auto metadata = root_compat::RNTupleModel::Create();
240239

241-
auto podioVersion = podio::version::build_version;
240+
const auto podioVersion = podio::version::build_version;
242241
auto versionField = metadata->MakeField<std::vector<uint16_t>>(root_utils::versionBranchName);
243242
*versionField = {podioVersion.major, podioVersion.minor, podioVersion.patch};
244243

245-
auto edmDefinitions = m_datamodelCollector.getDatamodelDefinitionsToWrite();
244+
const auto edmDefinitions = m_datamodelCollector.getDatamodelDefinitionsToWrite();
246245
for (const auto& [name, _] : edmDefinitions) {
247-
auto edmVersion = DatamodelRegistry::instance().getDatamodelVersion(name);
246+
const auto edmVersion = DatamodelRegistry::instance().getDatamodelVersion(name);
248247
if (edmVersion) {
249-
auto edmVersionField = metadata->MakeField<std::vector<uint16_t>>(root_utils::edmVersionBranchName(name).c_str());
248+
const auto edmVersionField = metadata->MakeField<std::vector<uint16_t>>(root_utils::edmVersionBranchName(name));
250249
*edmVersionField = {edmVersion->major, edmVersion->minor, edmVersion->patch};
251250
}
252251
}
253252

254-
auto edmField = metadata->MakeField<std::vector<std::tuple<std::string, std::string>>>(root_utils::edmDefBranchName);
253+
const auto edmField =
254+
metadata->MakeField<std::vector<std::tuple<std::string, std::string>>>(root_utils::edmDefBranchName);
255255
*edmField = std::move(edmDefinitions);
256256

257-
auto availableCategoriesField = metadata->MakeField<std::vector<std::string>>(root_utils::availableCategories);
258-
for (auto& [c, _] : m_categories) {
257+
const auto availableCategoriesField = metadata->MakeField<std::vector<std::string>>(root_utils::availableCategories);
258+
for (const auto& [c, _] : m_categories) {
259259
availableCategoriesField->push_back(c);
260260
}
261261

262-
for (auto& [category, collInfo] : m_categories) {
263-
auto collInfoField =
262+
for (const auto& [category, collInfo] : m_categories) {
263+
const auto collInfoField =
264264
metadata->MakeField<std::vector<root_utils::CollectionWriteInfo>>({root_utils::collInfoName(category)});
265265
*collInfoField = collInfo.collInfo;
266266
}
267267

268268
metadata->Freeze();
269-
auto metadataWriter = root_compat::RNTupleWriter::Append(std::move(metadata), root_utils::metaTreeName, *m_file, {});
269+
const auto metadataWriter =
270+
root_compat::RNTupleWriter::Append(std::move(metadata), root_utils::metaTreeName, *m_file, {});
270271

271272
metadataWriter->Fill();
272273

0 commit comments

Comments
 (0)