From b38c1d6f5e5a30e54df07191242421ed848ae8de Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 12 Jun 2024 17:13:36 +0200 Subject: [PATCH] Add a loadFrom method to facilitate usage in RDataFrame --- include/podio/GenericParameters.h | 16 ++++++++++++++++ src/RNTupleReader.cc | 4 +--- src/ROOTReader.cc | 4 +--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/podio/GenericParameters.h b/include/podio/GenericParameters.h index 9e0fb5e5d..04456920c 100644 --- a/include/podio/GenericParameters.h +++ b/include/podio/GenericParameters.h @@ -104,6 +104,10 @@ class GenericParameters { set>(key, std::move(values)); } + /// Load multiple key value pairs simultaneously + template + void loadFrom(std::vector keys, std::vector> values); + /// Get the number of elements stored under the given key for a type template > size_t getN(const std::string& key) const; @@ -272,5 +276,17 @@ std::vector> GenericParameters::getValues() const { } return values; } + +template +void GenericParameters::loadFrom(std::vector keys, std::vector> values) { + auto& map = getMap(); + auto& mtx = getMutex(); + + std::lock_guard lock{mtx}; + for (size_t i = 0; i < keys.size(); ++i) { + map.emplace(std::move(keys[i]), std::move(values[i])); + } +} + } // namespace podio #endif diff --git a/src/RNTupleReader.cc b/src/RNTupleReader.cc index a372f6c2f..43c8370aa 100644 --- a/src/RNTupleReader.cc +++ b/src/RNTupleReader.cc @@ -18,9 +18,7 @@ void RNTupleReader::readParams(const std::string& name, unsigned entNum, Generic auto keyView = m_readers[name][0]->GetView>(root_utils::getGPKeyName()); auto valueView = m_readers[name][0]->GetView>>(root_utils::getGPValueName()); - for (size_t i = 0; i < keyView(entNum).size(); ++i) { - params.getMap().emplace(std::move(keyView(entNum)[i]), std::move(valueView(entNum)[i])); - } + params.loadFrom(keyView(entNum), valueView(entNum)); } GenericParameters RNTupleReader::readEventMetaData(const std::string& name, unsigned entNum) { diff --git a/src/ROOTReader.cc b/src/ROOTReader.cc index 3e786fb43..60fa37018 100644 --- a/src/ROOTReader.cc +++ b/src/ROOTReader.cc @@ -75,9 +75,7 @@ void ROOTReader::readParams(ROOTReader::CategoryInfo& catInfo, podio::GenericPar valueBranch->SetAddress(storage.valuesPtr()); valueBranch->GetEntry(localEntry); - for (size_t i = 0; i < storage.keys.size(); ++i) { - params.getMap().emplace(std::move(storage.keys[i]), std::move(storage.values[i])); - } + params.loadFrom(std::move(storage.keys), std::move(storage.values)); } GenericParameters ROOTReader::readEntryParameters(ROOTReader::CategoryInfo& catInfo, bool reloadBranches,