From 0800dfbc9d026fbce705f21da424d4485f4724c3 Mon Sep 17 00:00:00 2001 From: jmcarcell Date: Wed, 20 Sep 2023 13:41:58 +0200 Subject: [PATCH] Use a map instead of a vector --- k4FWCore/components/PodioInput.cpp | 150 +++++++++--------- k4FWCore/include/k4FWCore/PodioDataSvc.h | 22 +-- .../ExampleFunctionalConsumerSeveralColls.cpp | 8 +- 3 files changed, 90 insertions(+), 90 deletions(-) diff --git a/k4FWCore/components/PodioInput.cpp b/k4FWCore/components/PodioInput.cpp index 9045729f..66248ed8 100644 --- a/k4FWCore/components/PodioInput.cpp +++ b/k4FWCore/components/PodioInput.cpp @@ -209,153 +209,153 @@ void PodioInput::fillReaders() { [&](std::string_view collName) { maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector"] = + m_readers["std::map"] = [&](std::string_view collName) { - maybeRead>(collName); + maybeRead>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; - m_readers["std::vector>"] = + m_readers["std::map>"] = [&](std::string_view collName) { - maybeRead*>>(collName); + maybeRead*>>(collName); }; } @@ -377,7 +377,7 @@ void PodioInput::operator()() const { std::string name; if (collName.find(" ") != std::string::npos) { auto first = collName.substr(0, collName.find(" ")); - type = "std::vector<" + std::string(m_podioDataSvc->getCollectionType(first)) + ">"; + type = "std::mapgetCollectionType(first)) + ">"; } else { type = m_podioDataSvc->getCollectionType(collName); diff --git a/k4FWCore/include/k4FWCore/PodioDataSvc.h b/k4FWCore/include/k4FWCore/PodioDataSvc.h index 4f699558..5e8b493e 100644 --- a/k4FWCore/include/k4FWCore/PodioDataSvc.h +++ b/k4FWCore/include/k4FWCore/PodioDataSvc.h @@ -69,20 +69,20 @@ class PodioDataSvc : public DataSvc { const std::string_view getCollectionType(const std::string& collName); template - struct is_vector + struct is_map { static constexpr bool value = false; }; - template class C, typename U> - struct is_vector> + + template + struct is_map> { - static constexpr bool value = - std::is_same,std::vector>::value; + static constexpr bool value = true; }; template - std::enable_if_t::value,StatusCode> + std::enable_if_t::value,StatusCode> readCollection(const std::string& collName) { const T* collection = static_cast(m_eventframe.get(collName)); if (!collection) { @@ -95,21 +95,21 @@ class PodioDataSvc : public DataSvc { } template - std::enable_if_t::value,StatusCode> + std::enable_if_t::value,StatusCode> readCollection(const std::string& collName) { std::istringstream iss(collName); std::string token; - auto vec = new std::vector(); + auto map = new std::map(); // Assume collName is a space-separated list of collection names while (iss >> token) { - auto collection = dynamic_cast(const_cast(m_eventframe.get(token))); + auto collection = dynamic_cast(const_cast(m_eventframe.get(token))); if (!collection) { error() << "Collection " << token << " does not exist." << endmsg; } - vec->push_back(collection); + (*map)[collName] = collection; } auto wrapper = new DataWrapper; - wrapper->setData(vec); + wrapper->setData(map); m_podio_datawrappers.push_back(wrapper); return DataSvc::registerObject("/Event", "/" + collName, wrapper); } diff --git a/test/k4FWCoreTest/src/components/ExampleFunctionalConsumerSeveralColls.cpp b/test/k4FWCoreTest/src/components/ExampleFunctionalConsumerSeveralColls.cpp index c9ea4bb0..1b110088 100644 --- a/test/k4FWCoreTest/src/components/ExampleFunctionalConsumerSeveralColls.cpp +++ b/test/k4FWCoreTest/src/components/ExampleFunctionalConsumerSeveralColls.cpp @@ -10,7 +10,7 @@ // Which type of collection we are reading // When reading multiple collections -using colltype = std::vector; +using colltype = std::map; struct ExampleFunctionalConsumerSeveralColls final : Gaudi::Functional::Consumer { // The pair in KeyValue can be changed from python and it corresponds @@ -25,9 +25,9 @@ struct ExampleFunctionalConsumerSeveralColls final : Gaudi::Functional::Consumer // we get from the input void operator()(const colltype& input) const override { int i = 0; - for (auto ptr : input) { + for (auto& [key, coll] : input) { if (i == 0) { - for (const auto& particle : *ptr) { + for (const auto& particle : *coll) { if ((particle.getPDG() != 1 + i) || (particle.getGeneratorStatus() != 2 + i) || (particle.getSimulatorStatus() != 3 + i) || (particle.getCharge() != 4 + i) || (particle.getTime() != 5 + i) || (particle.getMass() != 6 + i)) { @@ -37,7 +37,7 @@ struct ExampleFunctionalConsumerSeveralColls final : Gaudi::Functional::Consumer } } else { - if (ptr->size() != 0) { + if (coll->size() != 0) { fatal() << "Wrong data in MCParticle collection"; } }