Skip to content

Commit

Permalink
Use a map instead of a vector
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Sep 20, 2023
1 parent d43e36c commit 0800dfb
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 90 deletions.
150 changes: 75 additions & 75 deletions k4FWCore/components/PodioInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,153 +209,153 @@ void PodioInput::fillReaders() {
[&](std::string_view collName) {
maybeRead<podio::UserDataCollection<uint64_t>>(collName);
};
m_readers["std::vector<edm4hep::MCParticleCollection>"] =
m_readers["std::map<std::string, edm4hep::MCParticleCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCParticleCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCParticleCollection*>>(collName);
};
m_readers["std::vector<edm4hep::SimTrackerHitCollection>"] =
m_readers["std::map<std::string, edm4hep::SimTrackerHitCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::SimTrackerHitCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::SimTrackerHitCollection*>>(collName);
};
m_readers["std::vector<edm4hep::CaloHitContributionCollection>"] =
m_readers["std::map<std::string, edm4hep::CaloHitContributionCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::CaloHitContributionCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::CaloHitContributionCollection*>>(collName);
};
m_readers["std::vector<edm4hep::SimCalorimeterHitCollection>"] =
m_readers["std::map<std::string, edm4hep::SimCalorimeterHitCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::SimCalorimeterHitCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::SimCalorimeterHitCollection*>>(collName);
};
m_readers["std::vector<edm4hep::RawCalorimeterHitCollection>"] =
m_readers["std::map<std::string, edm4hep::RawCalorimeterHitCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::RawCalorimeterHitCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::RawCalorimeterHitCollection*>>(collName);
};
m_readers["std::vector<edm4hep::CalorimeterHitCollection>"] =
m_readers["std::map<std::string, edm4hep::CalorimeterHitCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::CalorimeterHitCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::CalorimeterHitCollection*>>(collName);
};
m_readers["std::vector<edm4hep::ParticleIDCollection>"] =
m_readers["std::map<std::string, edm4hep::ParticleIDCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::ParticleIDCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::ParticleIDCollection*>>(collName);
};
m_readers["std::vector<edm4hep::ClusterCollection>"] =
m_readers["std::map<std::string, edm4hep::ClusterCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::ClusterCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::ClusterCollection*>>(collName);
};
m_readers["std::vector<edm4hep::TrackerHitCollection>"] =
m_readers["std::map<std::string, edm4hep::TrackerHitCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::TrackerHitCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::TrackerHitCollection*>>(collName);
};
m_readers["std::vector<edm4hep::TrackerHitPlaneCollection>"] =
m_readers["std::map<std::string, edm4hep::TrackerHitPlaneCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::TrackerHitPlaneCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::TrackerHitPlaneCollection*>>(collName);
};
m_readers["std::vector<edm4hep::RawTimeSeriesCollection>"] =
m_readers["std::map<std::string, edm4hep::RawTimeSeriesCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::RawTimeSeriesCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::RawTimeSeriesCollection*>>(collName);
};
m_readers["std::vector<edm4hep::TrackCollection>"] =
m_readers["std::map<std::string, edm4hep::TrackCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::TrackCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::TrackCollection*>>(collName);
};
m_readers["std::vector<edm4hep::VertexCollection>"] =
m_readers["std::map<std::string, edm4hep::VertexCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::VertexCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::VertexCollection*>>(collName);
};
m_readers["std::vector<edm4hep::ReconstructedParticleCollection>"] =
m_readers["std::map<std::string, edm4hep::ReconstructedParticleCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::ReconstructedParticleCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::ReconstructedParticleCollection*>>(collName);
};
m_readers["std::vector<edm4hep::MCRecoParticleAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::MCRecoParticleAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCRecoParticleAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCRecoParticleAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::MCRecoCaloAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::MCRecoCaloAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCRecoCaloAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCRecoCaloAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::MCRecoTrackerAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::MCRecoTrackerAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCRecoTrackerAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCRecoTrackerAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::MCRecoTrackerHitPlaneAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::MCRecoTrackerHitPlaneAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCRecoTrackerHitPlaneAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCRecoTrackerHitPlaneAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::MCRecoClusterParticleAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::MCRecoClusterParticleAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCRecoClusterParticleAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCRecoClusterParticleAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::MCRecoTrackParticleAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::MCRecoTrackParticleAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::MCRecoTrackParticleAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::MCRecoTrackParticleAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::RecoParticleVertexAssociationCollection>"] =
m_readers["std::map<std::string, edm4hep::RecoParticleVertexAssociationCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::RecoParticleVertexAssociationCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::RecoParticleVertexAssociationCollection*>>(collName);
};
m_readers["std::vector<edm4hep::SimPrimaryIonizationClusterCollection>"] =
m_readers["std::map<std::string, edm4hep::SimPrimaryIonizationClusterCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::SimPrimaryIonizationClusterCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::SimPrimaryIonizationClusterCollection*>>(collName);
};
m_readers["std::vector<edm4hep::TrackerPulseCollection>"] =
m_readers["std::map<std::string, edm4hep::TrackerPulseCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::TrackerPulseCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::TrackerPulseCollection*>>(collName);
};
m_readers["std::vector<edm4hep::RecIonizationClusterCollection>"] =
m_readers["std::map<std::string, edm4hep::RecIonizationClusterCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::RecIonizationClusterCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::RecIonizationClusterCollection*>>(collName);
};
m_readers["std::vector<edm4hep::TimeSeriesCollection>"] =
m_readers["std::map<std::string, edm4hep::TimeSeriesCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::TimeSeriesCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::TimeSeriesCollection*>>(collName);
};
m_readers["std::vector<edm4hep::RecDqdxCollection>"] =
m_readers["std::map<std::string, edm4hep::RecDqdxCollection>"] =
[&](std::string_view collName) {
maybeRead<std::vector<edm4hep::RecDqdxCollection*>>(collName);
maybeRead<std::map<std::string, edm4hep::RecDqdxCollection*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<int>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<int>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<int>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<int>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<float>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<float>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<float>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<float>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<double>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<double>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<double>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<double>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<int8_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<int8_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<int8_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<int8_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<int16_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<int16_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<int16_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<int16_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<int32_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<int32_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<int32_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<int32_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<int64_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<int64_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<int64_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<int64_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<uint8_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<uint8_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<uint8_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<uint8_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<uint16_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<uint16_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<uint16_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<uint16_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<uint32_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<uint32_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<uint32_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<uint32_t>*>>(collName);
};
m_readers["std::vector<podio::UserDataCollection<uint64_t>>"] =
m_readers["std::map<std::string, podio::UserDataCollection<uint64_t>>"] =
[&](std::string_view collName) {
maybeRead<std::vector<podio::UserDataCollection<uint64_t>*>>(collName);
maybeRead<std::map<std::string, podio::UserDataCollection<uint64_t>*>>(collName);
};
}

Expand All @@ -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::map<std::string, " + std::string(m_podioDataSvc->getCollectionType(first)) + ">";
}
else {
type = m_podioDataSvc->getCollectionType(collName);
Expand Down
22 changes: 11 additions & 11 deletions k4FWCore/include/k4FWCore/PodioDataSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ class PodioDataSvc : public DataSvc {
const std::string_view getCollectionType(const std::string& collName);

template<typename T>
struct is_vector
struct is_map
{
static constexpr bool value = false;
};

template<template<typename...> class C, typename U>
struct is_vector<C<U>>

template <typename... Args>
struct is_map<std::map<Args...>>
{
static constexpr bool value =
std::is_same<C<U>,std::vector<U>>::value;
static constexpr bool value = true;
};

template <typename T>
std::enable_if_t<!is_vector<T>::value,StatusCode>
std::enable_if_t<!is_map<T>::value,StatusCode>
readCollection(const std::string& collName) {
const T* collection = static_cast<const T*>(m_eventframe.get(collName));
if (!collection) {
Expand All @@ -95,21 +95,21 @@ class PodioDataSvc : public DataSvc {
}

template <typename T>
std::enable_if_t<is_vector<T>::value,StatusCode>
std::enable_if_t<is_map<T>::value,StatusCode>
readCollection(const std::string& collName) {
std::istringstream iss(collName);
std::string token;
auto vec = new std::vector<typename T::value_type>();
auto map = new std::map<std::string, typename T::value_type::second_type>();
// Assume collName is a space-separated list of collection names
while (iss >> token) {
auto collection = dynamic_cast<typename T::value_type>(const_cast<podio::CollectionBase*>(m_eventframe.get(token)));
auto collection = dynamic_cast<typename T::value_type::second_type>(const_cast<podio::CollectionBase*>(m_eventframe.get(token)));
if (!collection) {
error() << "Collection " << token << " does not exist." << endmsg;
}
vec->push_back(collection);
(*map)[collName] = collection;
}
auto wrapper = new DataWrapper<T>;
wrapper->setData(vec);
wrapper->setData(map);
m_podio_datawrappers.push_back(wrapper);
return DataSvc::registerObject("/Event", "/" + collName, wrapper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Which type of collection we are reading
// When reading multiple collections
using colltype = std::vector<edm4hep::MCParticleCollection*>;
using colltype = std::map<std::string, edm4hep::MCParticleCollection*>;

struct ExampleFunctionalConsumerSeveralColls final : Gaudi::Functional::Consumer<void(const colltype& input), BaseClass_t> {
// The pair in KeyValue can be changed from python and it corresponds
Expand All @@ -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)) {
Expand All @@ -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";
}
}
Expand Down

0 comments on commit 0800dfb

Please sign in to comment.