From 493540c3ca4fc2108e01d9ac9b3c860f69847b62 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 30 Jun 2023 16:23:32 +0200 Subject: [PATCH 01/20] Remove const from lcio key types Preparation for more general mappnig structure --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 92 +++++++++---------- k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 86 ++++++++--------- 2 files changed, 89 insertions(+), 89 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 53f6c649..0e15ee45 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -68,19 +68,19 @@ namespace LCIO2EDM4hepConv { * way that makes the lookup from LCIO to EDM4hep easy. */ struct LcioEdmTypeMapping { - TypeMapT tracks {}; - TypeMapT trackerHits {}; - TypeMapT simTrackerHits {}; - TypeMapT caloHits {}; - TypeMapT rawCaloHits {}; - TypeMapT simCaloHits {}; - TypeMapT tpcHits {}; - TypeMapT clusters {}; - TypeMapT vertices {}; - TypeMapT recoParticles {}; - TypeMapT mcParticles {}; - TypeMapT trackerHitPlanes {}; - TypeMapT particleIDs {}; + TypeMapT tracks {}; + TypeMapT trackerHits {}; + TypeMapT simTrackerHits {}; + TypeMapT caloHits {}; + TypeMapT rawCaloHits {}; + TypeMapT simCaloHits {}; + TypeMapT tpcHits {}; + TypeMapT clusters {}; + TypeMapT vertices {}; + TypeMapT recoParticles {}; + TypeMapT mcParticles {}; + TypeMapT trackerHitPlanes {}; + TypeMapT particleIDs {}; }; using CollNamePair = std::tuple>; @@ -178,7 +178,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertMCParticles( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& mcparticlesMap); + TypeMapT& mcparticlesMap); /** * Convert a ReconstructedParticle collection and return the resulting collection. @@ -191,8 +191,8 @@ namespace LCIO2EDM4hepConv { std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap, - TypeMapT& particleIDMap); + TypeMapT& recoparticlesMap, + TypeMapT& particleIDMap); /** * Convert a Vertex collection and return the resulting collection. @@ -201,7 +201,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertVertices( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& vertexMap); + TypeMapT& vertexMap); /** * Convert a SimTrackerHit collection and return the resulting collection. @@ -210,7 +210,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertSimTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& SimTrHitMap); + TypeMapT& SimTrHitMap); /** * Convert a TPCHit collection and return the resulting collection. @@ -219,7 +219,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTPCHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap); + TypeMapT& TPCHitMap); /** * Convert a TrackerHit collection and return the resulting collection. @@ -228,7 +228,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitMap); + TypeMapT& TrackerHitMap); /** * Convert a TrackerHitPlane collection and return the resulting collection. @@ -237,7 +237,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTrackerHitPlanes( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitPlaneMap); + TypeMapT& TrackerHitPlaneMap); /** * Convert a Track collection and return the resulting collection. @@ -246,7 +246,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTracks( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TrackMap); + TypeMapT& TrackMap); /** * Convert a SimCalorimeterHit collection and return the resulting collection. @@ -255,7 +255,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertSimCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& SimCaloHitMap); + TypeMapT& SimCaloHitMap); /** * Convert a RawCalorimeterHit collection and return the resulting collection. @@ -264,7 +264,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertRawCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& rawCaloHitMap); + TypeMapT& rawCaloHitMap); /** * Convert a CalorimeterHit collection and return the resulting collection. @@ -273,7 +273,7 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& caloHitMap); + TypeMapT& caloHitMap); /** * Convert a Cluster collection and return the resulting collection. @@ -286,8 +286,8 @@ namespace LCIO2EDM4hepConv { std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap, - TypeMapT& particleIDMap); + TypeMapT& clusterMap, + TypeMapT& particleIDMap); /** * Create an EventHeaderCollection and fills it with the Metadata. @@ -304,7 +304,7 @@ namespace LCIO2EDM4hepConv { * matching types) */ template - auto handleSubsetColl(EVENT::LCCollection* lcioColl, const TypeMapT& elemMap) + auto handleSubsetColl(EVENT::LCCollection* lcioColl, const TypeMapT& elemMap) { auto edm4hepColl = std::make_unique(); edm4hepColl->setSubsetCollection(); @@ -326,7 +326,7 @@ namespace LCIO2EDM4hepConv { namespace detail { /// Helper function for generic map lookup template - std::optional mapLookup(const LCIOT* elem, const TypeMapT& map) + std::optional mapLookup(LCIOT* elem, const TypeMapT& map) { if (const auto& it = map.find(elem); it != map.end()) { return std::optional(it->second); @@ -354,8 +354,8 @@ namespace LCIO2EDM4hepConv { typename ToEDM4hepT> std::unique_ptr createAssociationCollection( EVENT::LCCollection* relations, - const TypeMapT& fromMap, - const TypeMapT& toMap) + const TypeMapT& fromMap, + const TypeMapT& toMap) { auto assocColl = std::make_unique(); auto relIter = UTIL::LCIterator(relations); @@ -398,50 +398,50 @@ namespace LCIO2EDM4hepConv { * The contributions are also attached to their corresponding SimCalorimeterHits. */ std::unique_ptr createCaloHitContributions( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap); + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap); /** * Resolve the relations for the MCParticles. */ - void resolveRelationsMCParticles(TypeMapT& mcparticlesMap); + void resolveRelationsMCParticles(TypeMapT& mcparticlesMap); /** * Resolve the relations for SimTrackerHits */ void resolveRelationsSimTrackerHits( - TypeMapT& SimTrHitMap, - const TypeMapT& mcparticlesMap); + TypeMapT& SimTrHitMap, + const TypeMapT& mcparticlesMap); /** * Resolve the relations for ReconstructedParticles */ void resolveRelationsRecoParticles( - TypeMapT& recoparticlesMap, - const TypeMapT& vertexMap, - const TypeMapT& clusterMap, - const TypeMapT& tracksMap); + TypeMapT& recoparticlesMap, + const TypeMapT& vertexMap, + const TypeMapT& clusterMap, + const TypeMapT& tracksMap); /** * Resolve the relations for Clusters */ void resolveRelationsClusters( - TypeMapT& clustersMap, - const TypeMapT& caloHitMap); + TypeMapT& clustersMap, + const TypeMapT& caloHitMap); /** * Resolve the relations for Tracks */ void resolveRelationsTracks( - TypeMapT& tracksMap, - const TypeMapT& trackerHitMap); + TypeMapT& tracksMap, + const TypeMapT& trackerHitMap); /** * Resolve the relations for Vertices */ void resolveRelationsVertices( - TypeMapT& vertexMap, - const TypeMapT& recoparticleMap); + TypeMapT& vertexMap, + const TypeMapT& recoparticleMap); template void convertObjectParameters(LCIOType* lcioobj, podio::Frame& event) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 19e411e9..4475a594 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -61,11 +61,11 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertMCParticles( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& mcparticlesMap) + TypeMapT& mcparticlesMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setPDG(rval->getPDG()); @@ -95,13 +95,13 @@ namespace LCIO2EDM4hepConv { std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap, - TypeMapT& particleIDMap) + TypeMapT& recoparticlesMap, + TypeMapT& particleIDMap) { auto dest = std::make_unique(); auto particleIDs = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setCharge(rval->getCharge()); @@ -161,11 +161,11 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertVertices( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& vertexMap) + TypeMapT& vertexMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary @@ -196,12 +196,12 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertSimTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& SimTrHitMap) + TypeMapT& SimTrHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -228,12 +228,12 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTPCHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap) + TypeMapT& TPCHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setCellID(rval->getCellID()); @@ -258,11 +258,11 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTrackerHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitMap) + TypeMapT& TrackerHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -291,12 +291,12 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTrackerHitPlanes( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitPlaneMap) + TypeMapT& TrackerHitPlaneMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -330,12 +330,12 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertTracks( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& TrackMap) + TypeMapT& TrackMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setType(rval->getType()); @@ -372,11 +372,11 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertSimCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& SimCaloHitMap) + TypeMapT& SimCaloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -400,12 +400,12 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertRawCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& rawCaloHitMap) + TypeMapT& rawCaloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); @@ -429,12 +429,12 @@ namespace LCIO2EDM4hepConv { std::unique_ptr convertCalorimeterHits( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& caloHitMap) + TypeMapT& caloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); uint64_t cellID = rval->getCellID1(); cellID = (cellID << 32) | rval->getCellID0(); @@ -460,14 +460,14 @@ namespace LCIO2EDM4hepConv { std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap, - TypeMapT& particleIDMap) + TypeMapT& clusterMap, + TypeMapT& particleIDMap) { auto particleIDs = std::make_unique(); auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); + auto* rval = static_cast(LCCollection->getElementAt(i)); auto lval = dest->create(); lval.setEnergy(rval->getEnergy()); @@ -563,8 +563,8 @@ namespace LCIO2EDM4hepConv { } std::unique_ptr createCaloHitContributions( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap) + TypeMapT& SimCaloHitMap, + const TypeMapT& mcparticlesMap) { auto contrCollection = std::make_unique(); for (auto& [lcioHit, edmHit] : SimCaloHitMap) { @@ -675,7 +675,7 @@ namespace LCIO2EDM4hepConv { return event; } - void resolveRelationsMCParticles(TypeMapT& mcparticlesMap) + void resolveRelationsMCParticles(TypeMapT& mcparticlesMap) { int edmnum = 1; for (auto& [lcio, edm] : mcparticlesMap) { @@ -717,8 +717,8 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsSimTrackerHits( - TypeMapT& SimTrHitMap, - TypeMapT& mcparticlesMap) + TypeMapT& SimTrHitMap, + TypeMapT& mcparticlesMap) { for (auto& [lcio, edm] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); @@ -738,10 +738,10 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsRecoParticles( - TypeMapT& recoparticlesMap, - const TypeMapT& vertexMap, - const TypeMapT& clusterMap, - const TypeMapT& tracksMap) + TypeMapT& recoparticlesMap, + const TypeMapT& vertexMap, + const TypeMapT& clusterMap, + const TypeMapT& tracksMap) { int edmnum = 1; for (auto& [lcio, edm] : recoparticlesMap) { @@ -810,8 +810,8 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsClusters( - TypeMapT& clustersMap, - const TypeMapT& caloHitMap) + TypeMapT& clustersMap, + const TypeMapT& caloHitMap) { for (auto& [lcio, edm] : clustersMap) { auto clusters = lcio->getClusters(); @@ -852,10 +852,10 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsTracks( - TypeMapT& tracksMap, - const TypeMapT& trackerHitMap, - const TypeMapT& TPCHitMap, - const TypeMapT& trackerhitplaneMap) + TypeMapT& tracksMap, + const TypeMapT& trackerHitMap, + const TypeMapT& TPCHitMap, + const TypeMapT& trackerhitplaneMap) { for (auto& [lcio, edm] : tracksMap) { auto tracks = lcio->getTracks(); @@ -914,8 +914,8 @@ namespace LCIO2EDM4hepConv { } void resolveRelationsVertices( - TypeMapT& vertexMap, - const TypeMapT& recoparticleMap) + TypeMapT& vertexMap, + const TypeMapT& recoparticleMap) { for (auto& [lcio, edm] : vertexMap) { auto recoparticle = lcio->getAssociatedParticle(); From c2dc53d02af42874991124af6fbfa639c1350460 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 30 Jun 2023 16:29:42 +0200 Subject: [PATCH 02/20] Move mapLookup to utils header with some additional functionality --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 72 +++++++++++++++++++ .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 18 ++--- 2 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h new file mode 100644 index 00000000..2b0a4e96 --- /dev/null +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -0,0 +1,72 @@ +#ifndef K4EDM4HEP2LCIOCONV_MAPPINGUTILS_H +#define K4EDM4HEP2LCIOCONV_MAPPINGUTILS_H + +#include +#include + +#if __has_include("experimental/type_traits.h") +#include +namespace det { + using namespace std::experimental; +} +#else +// Implement the minimal feature set we need +#include + +namespace det { + namespace detail { + template typename Op, typename... Args> + struct detector { + using value_t = std::false_type; + using type = DefT; + }; + + template typename Op, typename... Args> + struct detector>, Op, Args...> { + using value_t = std::true_type; + using type = Op; + }; + } // namespace detail + + struct nonesuch { + ~nonesuch() = delete; + nonesuch(const nonesuch&) = delete; + void operator=(const nonesuch&) = delete; + }; + + template typename Op, typename... Args> + using is_detected = typename detail::detector::value_t; + + template typename Op, typename... Args> + static constexpr bool is_detected_v = is_detected::value; +} // namespace det +#endif + +namespace k4EDM4hep2LCIOConv::detail { + /// Very minimal detector for whether T is a map or not. We simply assume that + /// if it has a key_type we can also call find on T. + template + using has_key_t = typename T::key_type; + + template typename MapT> + std::optional mapLookup(LcioT lcioObj, const MapT& map) + { + if constexpr (det::is_detected_v>) { + if (const auto& it = map.find(lcioObj); it != map.end()) { + return it->second; + } + } + else { + if (const auto& it = std::find_if( + map.begin(), map.end(), [&lcioObj](const auto& mapElem) { return std::get<0>(mapElem) == lcioObj; }); + it != map.end()) { + return it->second; + } + } + + return std::nullopt; + } + +} // namespace k4EDM4hep2LCIOConv::detail + +#endif // K4EDM4HEP2LCIOCONV_MAPPINGUTILS_H diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 0e15ee45..8baa248b 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -1,6 +1,8 @@ #ifndef K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H #define K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H +#include "MappingUtils.h" + // EDM4hep #include "edm4hep/CaloHitContributionCollection.h" #include "edm4hep/CalorimeterHitCollection.h" @@ -323,18 +325,6 @@ namespace LCIO2EDM4hepConv { return edm4hepColl; } - namespace detail { - /// Helper function for generic map lookup - template - std::optional mapLookup(LCIOT* elem, const TypeMapT& map) - { - if (const auto& it = map.find(elem); it != map.end()) { - return std::optional(it->second); - } - return std::nullopt; - } - } // namespace detail - /** * Create an Association collection from an LCRelations collection. Templated * on the From and To types as well as the direction of the relations in the @@ -365,8 +355,8 @@ namespace LCIO2EDM4hepConv { assoc.setWeight(rel->getWeight()); const auto lcioTo = static_cast(rel->getTo()); const auto lcioFrom = static_cast(rel->getFrom()); - const auto edm4hepTo = detail::mapLookup(lcioTo, toMap); - const auto edm4hepFrom = detail::mapLookup(lcioFrom, fromMap); + const auto edm4hepTo = k4EDM4hep2LCIOConv::detail::mapLookup(lcioTo, toMap); + const auto edm4hepFrom = k4EDM4hep2LCIOConv::detail::mapLookup(lcioFrom, fromMap); if (edm4hepTo.has_value() && edm4hepFrom.has_value()) { if constexpr (Reverse) { if constexpr (std::is_same_v) { From 71e56d21ca4e1485037aaa3e3b6ffb33bf80a14e Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 30 Jun 2023 18:44:01 +0200 Subject: [PATCH 03/20] Make EDM4hep -> LCIO conversion use mapLookupFrom --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 69 +++++- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 4 +- k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp | 197 +++++++----------- 3 files changed, 142 insertions(+), 128 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index 2b0a4e96..fddf3301 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -42,31 +42,84 @@ namespace det { } // namespace det #endif -namespace k4EDM4hep2LCIOConv::detail { +namespace k4EDM4hep2LcioConv::detail { /// Very minimal detector for whether T is a map or not. We simply assume that /// if it has a key_type we can also call find on T. template using has_key_t = typename T::key_type; - template typename MapT> - std::optional mapLookup(LcioT lcioObj, const MapT& map) + template + constexpr static bool is_map_t = det::is_detected_v; + + /// Helper struct to determine the key and mapped types for map-like types or + /// maps + template>> + struct map_t_helper {}; + + template + struct map_t_helper> { + using key_type = typename T::key_type; + using mapped_type = typename T::mapped_type; + }; + + template + struct map_t_helper> { + using key_type = typename std::tuple_element<0, typename T::value_type>::type; + using mapped_type = typename std::tuple_element<1, typename T::value_type>::type; + }; + + template + using key_t = typename map_t_helper::key_type; + + template + using mapped_t = typename map_t_helper::mapped_type; + + /** + * Find the mapped-to object in a map provided a key object + * + * NOTE: This will use a potentially more efficient lookup for actual map + * types (i.e. MapT::find). In that case it will have the time complexity of + * that. In case of a "map-like" (e.g. vector>) it will be O(N). + */ + template>>> + auto mapLookupTo(FromT keyObj, const MapT& map) -> std::optional> { - if constexpr (det::is_detected_v>) { - if (const auto& it = map.find(lcioObj); it != map.end()) { + if constexpr (is_map_t) { + if (const auto& it = map.find(keyObj); it != map.end()) { return it->second; } } else { if (const auto& it = std::find_if( - map.begin(), map.end(), [&lcioObj](const auto& mapElem) { return std::get<0>(mapElem) == lcioObj; }); + map.begin(), map.end(), [&keyObj](const auto& mapElem) { return std::get<0>(mapElem) == keyObj; }); it != map.end()) { - return it->second; + return std::get<1>(*it); } } return std::nullopt; } -} // namespace k4EDM4hep2LCIOConv::detail + /** + * Find the mapped-from (or key object) in a "map" provided a mapped-to object + * + * NOTE: This will always loop over potentially all elements in the provided + * map, so it is definitely O(N) regardless of the provided map type + */ + template>>> + auto mapLookupFrom(ToT mappedObj, const MapT& map) -> std::optional> + { + // In this case we cannot use a potential find method for an actual map, but + // looping over the map and doing the actual comparison will work + if (const auto& it = std::find_if( + map.begin(), map.end(), [&mappedObj](const auto& mapElem) { return std::get<1>(mapElem) == mappedObj; }); + it != map.end()) { + return std::get<0>(*it); + } + + return std::nullopt; + } + +} // namespace k4EDM4hep2LcioConv::detail #endif // K4EDM4HEP2LCIOCONV_MAPPINGUTILS_H diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 8baa248b..c8f76926 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -355,8 +355,8 @@ namespace LCIO2EDM4hepConv { assoc.setWeight(rel->getWeight()); const auto lcioTo = static_cast(rel->getTo()); const auto lcioFrom = static_cast(rel->getFrom()); - const auto edm4hepTo = k4EDM4hep2LCIOConv::detail::mapLookup(lcioTo, toMap); - const auto edm4hepFrom = k4EDM4hep2LCIOConv::detail::mapLookup(lcioFrom, fromMap); + const auto edm4hepTo = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioTo, toMap); + const auto edm4hepFrom = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioFrom, fromMap); if (edm4hepTo.has_value() && edm4hepFrom.has_value()) { if constexpr (Reverse) { if constexpr (std::is_same_v) { diff --git a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp index 32ceceea..ce8f5cd0 100644 --- a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp @@ -1,4 +1,6 @@ #include "k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h" +#include "EVENT/MCParticle.h" +#include "k4EDM4hep2LcioConv/MappingUtils.h" #include "IMPL/LCEventImpl.h" @@ -79,11 +81,8 @@ namespace EDM4hep2LCIOConv { // Link multiple associated TrackerHits if found in converted ones for (const auto& edm_rp_trh : edm_tr.getTrackerHits()) { if (edm_rp_trh.isAvailable()) { - for (const auto& [lcio_trh, edm_trh] : trackerhits_vec) { - if (edm_trh == edm_rp_trh) { - lcio_tr->addHit(lcio_trh); - break; - } + if (const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_trh, trackerhits_vec)) { + lcio_tr->addHit(lcio_trh.value()); } } } @@ -120,11 +119,8 @@ namespace EDM4hep2LCIOConv { for (const auto& edm_linked_tr : edm_tr.getTracks()) { if (edm_linked_tr.isAvailable()) { // Search the linked track in the converted vector - for (const auto& [lcio_tr_linked, edm_tr_linked] : tracks_vec) { - if (edm_tr_linked == edm_linked_tr) { - lcio_tr->addTrack(lcio_tr_linked); - break; - } + if (const auto lcio_tr_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_tr, tracks_vec)) { + lcio_tr->addTrack(lcio_tr_linked.value()); } } } @@ -219,16 +215,13 @@ namespace EDM4hep2LCIOConv { // Link converted MCParticle to the SimTrackerHit if found const auto edm_strh_mcp = edm_strh.getMCParticle(); if (edm_strh_mcp.isAvailable()) { - bool conv_found = false; - for (const auto& [lcio_mcp, edm_mcp] : mcparticles_vec) { - if (edm_strh_mcp == edm_mcp) { - lcio_strh->setMCParticle(lcio_mcp); - conv_found = true; - break; - } + if (const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, mcparticles_vec)) { + lcio_strh->setMCParticle(lcio_mcp.value()); + } + else { + // If MCParticle available, but not found in converted vec, add nullptr + lcio_strh->setMCParticle(nullptr); } - // If MCParticle available, but not found in converted vec, add nullptr - if (not conv_found) lcio_strh->setMCParticle(nullptr); } // Save intermediate simtrackerhits ref @@ -469,15 +462,14 @@ namespace EDM4hep2LCIOConv { } // Link associated clusters after converting all clusters - for (auto& [lcio_cluter, edm_cluster] : cluster_vec) { + for (auto& [lcio_clutser, edm_cluster] : cluster_vec) { for (const auto& edm_linked_cluster : edm_cluster.getClusters()) { if (edm_linked_cluster.isAvailable()) { // Search the linked track in the converted vector - for (const auto& [lcio_cluster_linked, edm_cluster_linked] : cluster_vec) { - if (edm_cluster_linked == edm_linked_cluster) { - lcio_cluter->addCluster(lcio_cluster_linked); - break; - } + if ( + const auto lcio_cluster_linked = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_cluster, cluster_vec)) { + lcio_clutser->addCluster(lcio_cluster_linked.value()); } } } @@ -514,16 +506,13 @@ namespace EDM4hep2LCIOConv { // Link sinlge associated Particle if found in converted ones edm4hep::ReconstructedParticle vertex_rp = edm_vertex.getAssociatedParticle(); if (vertex_rp.isAvailable()) { - bool conv_found = false; - for (const auto& [lcio_rp, edm_rp] : recoparticles_vec) { - if (edm_rp == vertex_rp) { - lcio_vertex->setAssociatedParticle(lcio_rp); - conv_found = true; - break; - } + if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(vertex_rp, recoparticles_vec)) { + lcio_vertex->setAssociatedParticle(lcio_rp.value()); + } + else { + // If recoparticle avilable, but not found in converted vec, add nullptr + lcio_vertex->setAssociatedParticle(nullptr); } - // If recoparticle avilable, but not found in converted vec, add nullptr - if (not conv_found) lcio_vertex->setAssociatedParticle(nullptr); } // Add LCIO and EDM4hep pair collections to vec @@ -602,47 +591,38 @@ namespace EDM4hep2LCIOConv { // Link sinlge associated Vertex if found in converted ones auto vertex = edm_rp.getStartVertex(); if (vertex.isAvailable()) { - bool conv_found = false; - for (const auto& [lcio_vertex, edm_vertex] : vertex_vec) { - if (edm_vertex == vertex) { - lcio_recp->setStartVertex(lcio_vertex); - conv_found = true; - break; - } + if (const auto lcio_vertex = k4EDM4hep2LcioConv::detail::mapLookupFrom(vertex, vertex_vec)) { + lcio_recp->setStartVertex(lcio_vertex.value()); + } + else { + // If particleID available, but not found in converted vec, add nullptr + lcio_recp->setStartVertex(nullptr); } - // If particleID available, but not found in converted vec, add nullptr - if (not conv_found) lcio_recp->setStartVertex(nullptr); } // Link multiple associated Tracks if found in converted ones for (const auto& edm_rp_tr : edm_rp.getTracks()) { if (edm_rp_tr.isAvailable()) { - bool conv_found = false; - for (const auto& [lcio_tr, edm_tr] : tracks_vec) { - if (edm_tr == edm_rp_tr) { - lcio_recp->addTrack(lcio_tr); - conv_found = true; - break; - } + if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, tracks_vec)) { + lcio_recp->addTrack(lcio_tr.value()); + } + else { + // If track available, but not found in converted vec, add nullptr + lcio_recp->addTrack(nullptr); } - // If track available, but not found in converted vec, add nullptr - if (not conv_found) lcio_recp->addTrack(nullptr); } } // Link multiple associated Clusters if found in converted ones for (const auto& edm_rp_cluster : edm_rp.getClusters()) { if (edm_rp_cluster.isAvailable()) { - bool conv_found = false; - for (const auto& [lcio_cluster, edm_cluster] : clusters_vec) { - if (edm_cluster == edm_rp_cluster) { - lcio_recp->addCluster(lcio_cluster); - conv_found = true; - break; - } + if (const auto lcio_cluster = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, clusters_vec)) { + lcio_recp->addCluster(lcio_cluster.value()); + } + else { + // If cluster available, but not found in converted vec, add nullptr + lcio_recp->addCluster(nullptr); } - // If cluster available, but not found in converted vec, add nullptr - if (not conv_found) lcio_recp->addCluster(nullptr); } } @@ -659,11 +639,8 @@ namespace EDM4hep2LCIOConv { for (const auto& edm_linked_rp : edm_rp.getParticles()) { if (edm_linked_rp.isAvailable()) { // Search the linked track in the converted vector - for (const auto& [lcio_rp_linked, edm_rp_linked] : recoparticles_vec) { - if (edm_rp_linked == edm_linked_rp) { - lcio_rp->addParticle(lcio_rp_linked); - break; - } + if (const auto lcio_rp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_rp, recoparticles_vec)) { + lcio_rp->addParticle(lcio_rp_linked.value()); } } } @@ -727,14 +704,12 @@ namespace EDM4hep2LCIOConv { // Add parent MCParticles after converting all MCparticles for (auto& [lcio_mcp, edm_mcp] : mc_particles_vec) { - for (const auto& emd_parent_mcp : edm_mcp.getParents()) { - if (emd_parent_mcp.isAvailable()) { + for (const auto& edm_parent_mcp : edm_mcp.getParents()) { + if (edm_parent_mcp.isAvailable()) { // Search for the parent mcparticle in the converted vector - for (const auto& [lcio_mcp_linked, edm_mcp_linked] : mc_particles_vec) { - if (edm_mcp_linked == emd_parent_mcp) { - lcio_mcp->addParent(lcio_mcp_linked); - break; - } + if ( + const auto lcio_mcp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_parent_mcp, mc_particles_vec)) { + lcio_mcp->addParent(lcio_mcp_linked.value()); } } } @@ -766,11 +741,9 @@ namespace EDM4hep2LCIOConv { for (auto& [lcio_tr, edm_tr] : collection_pairs.tracks) { if (lcio_tr->getTrackerHits().size() == 0) { for (const auto& edm_tr_trh : edm_tr.getTrackerHits()) { - for (const auto& [lcio_trh, edm_trh] : collection_pairs.trackerhits) { - if (edm_trh == edm_tr_trh) { - lcio_tr->addHit(lcio_trh); - break; - } + if ( + const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, collection_pairs.trackerhits)) { + lcio_tr->addHit(lcio_trh.value()); } } } @@ -781,10 +754,10 @@ namespace EDM4hep2LCIOConv { // Link Vertex if (lcio_rp->getStartVertex() == nullptr) { if (edm_rp.getStartVertex().isAvailable()) { - for (const auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { - if (edm_vertex == edm_rp.getStartVertex()) { - lcio_rp->setStartVertex(lcio_vertex); - } + if ( + const auto lcio_vertex = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp.getStartVertex(), collection_pairs.vertices)) { + lcio_rp->setStartVertex(lcio_vertex.value()); } } } @@ -793,11 +766,8 @@ namespace EDM4hep2LCIOConv { if (lcio_rp->getTracks().size() != edm_rp.tracks_size()) { assert(lcio_rp->getTracks().size() == 0); for (const auto& edm_rp_tr : edm_rp.getTracks()) { - for (const auto& [lcio_tr, edm_tr] : collection_pairs.tracks) { - if (edm_tr == edm_rp_tr) { - lcio_rp->addTrack(lcio_tr); - break; - } + if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, collection_pairs.tracks)) { + lcio_rp->addTrack(lcio_tr.value()); } } } @@ -806,11 +776,10 @@ namespace EDM4hep2LCIOConv { if (lcio_rp->getClusters().size() != edm_rp.clusters_size()) { assert(lcio_rp->getClusters().size() == 0); for (const auto& edm_rp_cluster : edm_rp.getClusters()) { - for (const auto& [lcio_cluster, edm_cluster] : collection_pairs.clusters) { - if (edm_cluster == edm_rp_cluster) { - lcio_rp->addCluster(lcio_cluster); - break; - } + if ( + const auto lcio_cluster = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, collection_pairs.clusters)) { + lcio_rp->addCluster(lcio_cluster.value()); } } } @@ -818,14 +787,13 @@ namespace EDM4hep2LCIOConv { } // reconstructed particles // Fill missing Vertices collections - for (auto& vertex_pair : collection_pairs.vertices) { + for (const auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { // Link Reconstructed Particles - if (vertex_pair.first->getAssociatedParticle() == nullptr) { - if (vertex_pair.second.getAssociatedParticle().isAvailable()) { - for (auto& rp_pair : collection_pairs.recoparticles) { - if (rp_pair.second == vertex_pair.second.getAssociatedParticle()) { - vertex_pair.first->setAssociatedParticle(rp_pair.first); - } + if (lcio_vertex->getAssociatedParticle() == nullptr) { + const auto edm_rp = edm_vertex.getAssociatedParticle(); + if (edm_rp.isAvailable()) { + if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, collection_pairs.recoparticles)) { + lcio_vertex->setAssociatedParticle(lcio_rp.value()); } } } @@ -848,25 +816,20 @@ namespace EDM4hep2LCIOConv { auto edm_contrib_mcp = contrib.getParticle(); std::array step_position { contrib.getStepPosition()[0], contrib.getStepPosition()[1], contrib.getStepPosition()[2]}; - bool mcp_found = false; + + EVENT::MCParticle* lcio_mcp = nullptr; if (edm_contrib_mcp.isAvailable()) { // if we have the MCParticle we look for its partner - for (auto& [lcio_mcp, edm_mcp] : collection_pairs.mcparticles) { - if (edm_mcp == edm_contrib_mcp) { - mcp_found = true; - lcio_sch->addMCParticleContribution( - lcio_mcp, contrib.getEnergy(), contrib.getTime(), contrib.getPDG(), step_position.data()); - break; - } - } // all mcparticles + lcio_mcp = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcparticles).value_or(nullptr); } else { // edm mcp available // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; } - if (not mcp_found) { - // we add contribution with nullptr - lcio_sch->addMCParticleContribution( - nullptr, contrib.getEnergy(), contrib.getTime(), contrib.getPDG(), step_position.data()); + // we add contribution with whatever lcio mc particle we found + lcio_sch->addMCParticleContribution( + lcio_mcp, contrib.getEnergy(), contrib.getTime(), contrib.getPDG(), step_position.data()); + if (!lcio_mcp) { // std::cout << "WARNING: No MCParticle found for this contribution." // << "Make Sure MCParticles are converted! " // << edm_contrib_mcp.id() @@ -882,13 +845,11 @@ namespace EDM4hep2LCIOConv { // Fill missing SimTrackerHit collections for (auto& [lcio_strh, edm_strh] : collection_pairs.simtrackerhits) { const auto lcio_strh_mcp = lcio_strh->getMCParticle(); - const auto edm_strh_mcp = edm_strh.getMCParticle(); if (lcio_strh_mcp == nullptr) { - for (const auto& [lcio_mcp, edm_mcp] : collection_pairs.mcparticles) { - if (edm_strh_mcp == edm_mcp) { - lcio_strh->setMCParticle(lcio_mcp); - break; - } + const auto edm_strh_mcp = edm_strh.getMCParticle(); + if ( + const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, collection_pairs.mcparticles)) { + lcio_strh->setMCParticle(lcio_mcp.value()); } } From fcff727b1d967ffda482dc19a4de69ece7f9f905 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 30 Jun 2023 18:52:21 +0200 Subject: [PATCH 04/20] Introduce type-alises for the two used map types --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 146 ++++++++++-------- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 2 +- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 4 +- k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp | 2 + 4 files changed, 84 insertions(+), 70 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index fddf3301..b2365ff0 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -3,6 +3,9 @@ #include #include +#include +#include +#include #if __has_include("experimental/type_traits.h") #include @@ -42,84 +45,93 @@ namespace det { } // namespace det #endif -namespace k4EDM4hep2LcioConv::detail { - /// Very minimal detector for whether T is a map or not. We simply assume that - /// if it has a key_type we can also call find on T. - template - using has_key_t = typename T::key_type; +namespace k4EDM4hep2LcioConv { - template - constexpr static bool is_map_t = det::is_detected_v; - - /// Helper struct to determine the key and mapped types for map-like types or - /// maps - template>> - struct map_t_helper {}; - - template - struct map_t_helper> { - using key_type = typename T::key_type; - using mapped_type = typename T::mapped_type; - }; + namespace detail { + /// Very minimal detector for whether T is a map or not. We simply assume that + /// if it has a key_type we can also call find on T. + template + using has_key_t = typename T::key_type; + + template + constexpr static bool is_map_t = det::is_detected_v; + + /// Helper struct to determine the key and mapped types for map-like types or + /// maps + template>> + struct map_t_helper {}; + + template + struct map_t_helper> { + using key_type = typename T::key_type; + using mapped_type = typename T::mapped_type; + }; - template - struct map_t_helper> { - using key_type = typename std::tuple_element<0, typename T::value_type>::type; - using mapped_type = typename std::tuple_element<1, typename T::value_type>::type; - }; + template + struct map_t_helper> { + using key_type = typename std::tuple_element<0, typename T::value_type>::type; + using mapped_type = typename std::tuple_element<1, typename T::value_type>::type; + }; - template - using key_t = typename map_t_helper::key_type; - - template - using mapped_t = typename map_t_helper::mapped_type; - - /** - * Find the mapped-to object in a map provided a key object - * - * NOTE: This will use a potentially more efficient lookup for actual map - * types (i.e. MapT::find). In that case it will have the time complexity of - * that. In case of a "map-like" (e.g. vector>) it will be O(N). - */ - template>>> - auto mapLookupTo(FromT keyObj, const MapT& map) -> std::optional> - { - if constexpr (is_map_t) { - if (const auto& it = map.find(keyObj); it != map.end()) { - return it->second; + template + using key_t = typename map_t_helper::key_type; + + template + using mapped_t = typename map_t_helper::mapped_type; + + /** + * Find the mapped-to object in a map provided a key object + * + * NOTE: This will use a potentially more efficient lookup for actual map + * types (i.e. MapT::find). In that case it will have the time complexity of + * that. In case of a "map-like" (e.g. vector>) it will be O(N). + */ + template>>> + auto mapLookupTo(FromT keyObj, const MapT& map) -> std::optional> + { + if constexpr (is_map_t) { + if (const auto& it = map.find(keyObj); it != map.end()) { + return it->second; + } } + else { + if (const auto& it = std::find_if( + map.begin(), map.end(), [&keyObj](const auto& mapElem) { return std::get<0>(mapElem) == keyObj; }); + it != map.end()) { + return std::get<1>(*it); + } + } + + return std::nullopt; } - else { + + /** + * Find the mapped-from (or key object) in a "map" provided a mapped-to object + * + * NOTE: This will always loop over potentially all elements in the provided + * map, so it is definitely O(N) regardless of the provided map type + */ + template>>> + auto mapLookupFrom(ToT mappedObj, const MapT& map) -> std::optional> + { + // In this case we cannot use a potential find method for an actual map, but + // looping over the map and doing the actual comparison will work if (const auto& it = std::find_if( - map.begin(), map.end(), [&keyObj](const auto& mapElem) { return std::get<0>(mapElem) == keyObj; }); + map.begin(), map.end(), [&mappedObj](const auto& mapElem) { return std::get<1>(mapElem) == mappedObj; }); it != map.end()) { - return std::get<1>(*it); + return std::get<0>(*it); } - } - return std::nullopt; - } - - /** - * Find the mapped-from (or key object) in a "map" provided a mapped-to object - * - * NOTE: This will always loop over potentially all elements in the provided - * map, so it is definitely O(N) regardless of the provided map type - */ - template>>> - auto mapLookupFrom(ToT mappedObj, const MapT& map) -> std::optional> - { - // In this case we cannot use a potential find method for an actual map, but - // looping over the map and doing the actual comparison will work - if (const auto& it = std::find_if( - map.begin(), map.end(), [&mappedObj](const auto& mapElem) { return std::get<1>(mapElem) == mappedObj; }); - it != map.end()) { - return std::get<0>(*it); + return std::nullopt; } + } // namespace detail + + template + using MapT = std::unordered_map; - return std::nullopt; - } + template + using VecMapT = std::vector>; -} // namespace k4EDM4hep2LcioConv::detail +} // namespace k4EDM4hep2LcioConv #endif // K4EDM4HEP2LCIOCONV_MAPPINGUTILS_H diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index a828af30..3f9e58c8 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -1,7 +1,7 @@ #ifndef K4EDM4HEP2LCIOCONV_H #define K4EDM4HEP2LCIOCONV_H -#include +#include "k4EDM4hep2LcioConv/MappingUtils.h" // EDM4hep #include diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index c8f76926..c2a6f271 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -1,7 +1,7 @@ #ifndef K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H #define K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H -#include "MappingUtils.h" +#include "k4EDM4hep2LcioConv/MappingUtils.h" // EDM4hep #include "edm4hep/CaloHitContributionCollection.h" @@ -63,7 +63,7 @@ namespace LCIO2EDM4hepConv { template - using TypeMapT = std::unordered_map; + using TypeMapT = k4EDM4hep2LcioConv::MapT; /** * Maping holding all the original and converted objects in a 1:1 mapping in a diff --git a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp index ce8f5cd0..b1443422 100644 --- a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp @@ -18,6 +18,8 @@ #endif #endif +#include + namespace EDM4hep2LCIOConv { // Convert EDM4hep Tracks to LCIO From a6e108ba6ad31a6108a1309b9878e89b4ad688dd Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 30 Jun 2023 19:31:03 +0200 Subject: [PATCH 05/20] Switch LCIO to EDM4hep conversion to generic functionality - Add generic mapInsert method - Make all functions that take a map templated on the map(s) - Make sure type deduction works for vector backed maps and actual maps - Default template map parameter such that shared library is built with that and can be easily linked against It is now possible to simply swap out the TypeMapT alias to switch to a vector backed map --- k4EDM4hep2LcioConv/CMakeLists.txt | 2 +- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 71 ++++- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 180 ++++++------ k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 260 +++++++++--------- 4 files changed, 292 insertions(+), 221 deletions(-) diff --git a/k4EDM4hep2LcioConv/CMakeLists.txt b/k4EDM4hep2LcioConv/CMakeLists.txt index 1e366d09..f351e06f 100644 --- a/k4EDM4hep2LcioConv/CMakeLists.txt +++ b/k4EDM4hep2LcioConv/CMakeLists.txt @@ -14,7 +14,7 @@ target_link_libraries(k4EDM4hep2LcioConv PUBLIC set_target_properties(${PROJECT_NAME} PROPERTIES - PUBLIC_HEADER "include/${PROJECT_NAME}/k4EDM4hep2LcioConv.h;include/${PROJECT_NAME}/k4Lcio2EDM4hepConv.h" + PUBLIC_HEADER "include/${PROJECT_NAME}/k4EDM4hep2LcioConv.h;include/${PROJECT_NAME}/k4Lcio2EDM4hepConv.h;include/${PROJECT_NAME}/MappingUtils.h" ) install(TARGETS k4EDM4hep2LcioConv diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index b2365ff0..ffa8da6f 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -54,12 +54,13 @@ namespace k4EDM4hep2LcioConv { using has_key_t = typename T::key_type; template - constexpr static bool is_map_t = det::is_detected_v; + constexpr static bool is_map_v = det::is_detected_v; /// Helper struct to determine the key and mapped types for map-like types or /// maps - template>> - struct map_t_helper {}; + template>> + struct map_t_helper { + }; template struct map_t_helper> { @@ -89,7 +90,7 @@ namespace k4EDM4hep2LcioConv { template>>> auto mapLookupTo(FromT keyObj, const MapT& map) -> std::optional> { - if constexpr (is_map_t) { + if constexpr (is_map_v) { if (const auto& it = map.find(keyObj); it != map.end()) { return it->second; } @@ -124,6 +125,68 @@ namespace k4EDM4hep2LcioConv { return std::nullopt; } + + enum class InsertMode { Unchecked, Checked }; + + /** + * Insert a key-value pair into a "map" + * + * safeInsert argument can be useld to check for existence of key first, + * before inserting. This is only useful for maps using a vector as backing, + * since the usual emplace already does this check and does not insert if a + * key already exists + */ + template, typename MappedT = mapped_t> + auto mapInsert(KeyT&& key, MappedT&& mapped, MapT& map, InsertMode insertMode = InsertMode::Unchecked) + { + if constexpr (is_map_v) { + return map.emplace(std::forward(key), std::forward(mapped)); + } + else { + if (insertMode == InsertMode::Checked) { + if (auto existing = mapLookupTo(key, map)) { + return std::make_pair(std::make_tuple(key, existing.value()), false); + } + } + return std::make_pair(map.emplace_back(std::forward(key), std::forward(mapped)), true); + } + } + + /** + * Helper function to get the Key from an Iterator (e.g. returned by + * mapInsert). This is necessary because map::emplace returns an iterator, + * while vector::emplace_back returns a reference to the inserted element. + * Simply providing two overloads here does the trick. + */ + template + auto getKey(const It& it) + { + return std::get<0>(it); + } + + template + auto getKey(const It* it) + { + return std::get<0>(*it); + } + + /** + * Helper function to get the Value from an Iterator (e.g. returned by + * mapInsert). This is necessary because map::emplace returns an iterator, + * while vector::emplace_back returns a reference to the inserted element. + * Simply providing two overloads here does the trick. + */ + template + auto getMapped(const It& it) + { + return std::get<1>(it); + } + + template + auto getMapped(const It* it) + { + return std::get<1>(*it); + } } // namespace detail template diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index c2a6f271..6df90c7b 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -111,29 +111,33 @@ namespace LCIO2EDM4hepConv { * Returns a vector of names and collections (since some LCIO collections will * result in more than one EDM4hep collection) */ + template std::vector - convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping); + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, ObjectMappingT& typeMapping); /** * Resolve all relations in all converted objects that are held in the map. * Dispatch to the correpsonding implementation for all the types that have * relations */ - void resolveRelations(LcioEdmTypeMapping& typeMapping); + template + void resolveRelations(ObjectMappingT& typeMapping); /** * Convert LCRelation collections into the corresponding Association collections in EDM4hep */ + template std::vector createAssociations( - const LcioEdmTypeMapping& typeMapping, + const ObjectMappingT& typeMapping, const std::vector>& LCRelation); /** * Convert a subset collection, dispatching to the correct function for the * type of the input collection */ + template std::unique_ptr - fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type); + fillSubset(EVENT::LCCollection* LCCollection, const ObjectMappingT& typeMapping, const std::string& type); /* * Converts a LCIntVec or LCFloatVec Collection into a podio::UserDataCollection of the appropriate type. @@ -177,10 +181,9 @@ namespace LCIO2EDM4hepConv { * Convert an MCParticle collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertMCParticles( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& mcparticlesMap); + template> + std::unique_ptr + convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, MCParticleMapT& mcparticlesMap); /** * Convert a ReconstructedParticle collection and return the resulting collection. @@ -190,92 +193,86 @@ namespace LCIO2EDM4hepConv { * part of the ReconstructedParticles in LCIO. The name of this collection is * _particleIDs */ + template< + typename RecoMapT = TypeMapT, + typename PIDMapT = TypeMapT> std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap, - TypeMapT& particleIDMap); + RecoMapT& recoparticlesMap, + PIDMapT& particleIDMap); /** * Convert a Vertex collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertVertices( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& vertexMap); + template> + std::unique_ptr + convertVertices(const std::string& name, EVENT::LCCollection* LCCollection, VertexMapT& vertexMap); /** * Convert a SimTrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertSimTrackerHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimTrHitMap); + template> + std::unique_ptr + convertSimTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, SimTrHitMapT& SimTrHitMap); /** * Convert a TPCHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTPCHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap); + template> + std::unique_ptr + convertTPCHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TPCHitMap); /** * Convert a TrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTrackerHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitMap); + template> + std::unique_ptr + convertTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitMap); /** * Convert a TrackerHitPlane collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTrackerHitPlanes( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitPlaneMap); + template> + std::unique_ptr + convertTrackerHitPlanes(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitPlaneMap); /** * Convert a Track collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertTracks( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackMap); + template> + std::unique_ptr + convertTracks(const std::string& name, EVENT::LCCollection* LCCollection, TrackMapT& TrackMap); /** * Convert a SimCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertSimCalorimeterHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimCaloHitMap); + template> + std::unique_ptr + convertSimCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& SimCaloHitMap); /** * Convert a RawCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertRawCalorimeterHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& rawCaloHitMap); + template> + std::unique_ptr + convertRawCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& rawCaloHitMap); /** * Convert a CalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - std::unique_ptr convertCalorimeterHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& caloHitMap); + template> + std::unique_ptr + convertCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& caloHitMap); /** * Convert a Cluster collection and return the resulting collection. @@ -285,11 +282,14 @@ namespace LCIO2EDM4hepConv { * part of the Cluster collection in LCIO. The name of this collection is * _particleIDs */ + template< + typename ClusterMapT = TypeMapT, + typename PIDMapT = TypeMapT> std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap, - TypeMapT& particleIDMap); + ClusterMapT& clusterMap, + PIDMapT& particleIDMap); /** * Create an EventHeaderCollection and fills it with the Metadata. @@ -305,17 +305,20 @@ namespace LCIO2EDM4hepConv { * NOTE: Users responsibility to call this with the right inputs (i.e. * matching types) */ - template - auto handleSubsetColl(EVENT::LCCollection* lcioColl, const TypeMapT& elemMap) + template< + typename CollT, + typename ObjectMapT, + typename LcioT = std::remove_pointer_t>, + typename Edm4hepT = k4EDM4hep2LcioConv::detail::mapped_t> + auto handleSubsetColl(EVENT::LCCollection* lcioColl, const ObjectMapT& elemMap) { auto edm4hepColl = std::make_unique(); edm4hepColl->setSubsetCollection(); UTIL::LCIterator lcioIter(lcioColl); while (const auto lcioElem = lcioIter.next()) { - const auto it = elemMap.find(lcioElem); - if (it != elemMap.end()) { - edm4hepColl->push_back(it->second); + if (auto edm4hepElem = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioElem, elemMap)) { + edm4hepColl->push_back(edm4hepElem.value()); } else { std::cerr << "Cannot find corresponding EDM4hep object for an LCIO object in a subset collection" << std::endl; @@ -338,14 +341,14 @@ namespace LCIO2EDM4hepConv { template< typename CollT, bool Reverse, - typename FromLCIOT, - typename ToLCIOT, - typename FromEDM4hepT, - typename ToEDM4hepT> - std::unique_ptr createAssociationCollection( - EVENT::LCCollection* relations, - const TypeMapT& fromMap, - const TypeMapT& toMap) + typename FromMapT, + typename ToMapT, + typename FromLCIOT = std::remove_pointer_t>, + typename ToLCIOT = std::remove_pointer_t>, + typename FromEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t, + typename ToEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t> + std::unique_ptr + createAssociationCollection(EVENT::LCCollection* relations, const FromMapT& fromMap, const ToMapT& toMap) { auto assocColl = std::make_unique(); auto relIter = UTIL::LCIterator(relations); @@ -387,51 +390,70 @@ namespace LCIO2EDM4hepConv { * has to be done this way, since the converted McParticles are needeed. * The contributions are also attached to their corresponding SimCalorimeterHits. */ + template< + typename HitMapT = TypeMapT, + typename MCParticleMapT = TypeMapT> std::unique_ptr createCaloHitContributions( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap); + HitMapT& SimCaloHitMap, + const MCParticleMapT& mcparticlesMap); /** * Resolve the relations for the MCParticles. */ - void resolveRelationsMCParticles(TypeMapT& mcparticlesMap); + template> + void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap); /** * Resolve the relations for SimTrackerHits */ - void resolveRelationsSimTrackerHits( - TypeMapT& SimTrHitMap, - const TypeMapT& mcparticlesMap); + template< + typename HitMapT = TypeMapT, + typename MCParticleMapT = TypeMapT> + void resolveRelationsSimTrackerHits(HitMapT& SimTrHitMap, const MCParticleMapT& mcparticlesMap); /** * Resolve the relations for ReconstructedParticles */ + template< + typename RecoParticleMapT = TypeMapT, + typename VertexMapT = TypeMapT, + typename ClusterMapT = TypeMapT, + typename TrackMapT = TypeMapT> void resolveRelationsRecoParticles( - TypeMapT& recoparticlesMap, - const TypeMapT& vertexMap, - const TypeMapT& clusterMap, - const TypeMapT& tracksMap); + RecoParticleMapT& recoparticlesMap, + const VertexMapT& vertexMap, + const ClusterMapT& clusterMap, + const TrackMapT& tracksMap); /** * Resolve the relations for Clusters */ - void resolveRelationsClusters( - TypeMapT& clustersMap, - const TypeMapT& caloHitMap); + template< + typename ClusterMapT = TypeMapT, + typename CaloHitMapT = TypeMapT> + void resolveRelationsClusters(ClusterMapT& clustersMap, const CaloHitMapT& caloHitMap); /** * Resolve the relations for Tracks */ + template< + typename TrackMapT = TypeMapT, + typename TrackHitMapT = TypeMapT, + typename TPCHitMapT = TypeMapT, + typename THPlaneHitMapT = TypeMapT> void resolveRelationsTracks( - TypeMapT& tracksMap, - const TypeMapT& trackerHitMap); + TrackMapT& tracksMap, + const TrackHitMapT& trackerHitMap, + const TPCHitMapT&, + const THPlaneHitMapT&); /** * Resolve the relations for Vertices */ - void resolveRelationsVertices( - TypeMapT& vertexMap, - const TypeMapT& recoparticleMap); + template< + typename VertexMapT = TypeMapT, + typename RecoParticleMapT = TypeMapT> + void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap); template void convertObjectParameters(LCIOType* lcioobj, podio::Frame& event) diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 4475a594..805d9c33 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -1,8 +1,10 @@ #include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" +#include "k4EDM4hep2LcioConv/MappingUtils.h" #include namespace LCIO2EDM4hepConv { + edm4hep::TrackState convertTrackState(const EVENT::TrackState* trackState) { auto edmtrackState = edm4hep::TrackState {}; @@ -58,10 +60,9 @@ namespace LCIO2EDM4hepConv { return result; } - std::unique_ptr convertMCParticles( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& mcparticlesMap) + template + std::unique_ptr + convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, MCParticleMapT& mcparticlesMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -81,9 +82,9 @@ namespace LCIO2EDM4hepConv { lval.setMomentum(Vector3fFrom(rval->getMomentum())); lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); - const auto [iterator, inserted] = mcparticlesMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, mcparticlesMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element" << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -92,11 +93,12 @@ namespace LCIO2EDM4hepConv { return dest; } + template std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& recoparticlesMap, - TypeMapT& particleIDMap) + RecoMapT& recoparticlesMap, + PIDMapT& particleIDMap) { auto dest = std::make_unique(); auto particleIDs = std::make_unique(); @@ -114,9 +116,9 @@ namespace LCIO2EDM4hepConv { lval.setReferencePoint(rval->getReferencePoint()); lval.setType(rval->getType()); - const auto [iterator, inserted] = recoparticlesMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, recoparticlesMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -126,13 +128,14 @@ namespace LCIO2EDM4hepConv { // particle collection in LCIO for (const auto lcioPid : rval->getParticleIDs()) { auto pid = convertParticleID(lcioPid); - const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); + const auto [pidIt, pidInserted] = k4EDM4hep2LcioConv::detail::mapInsert( + lcioPid, pid, particleIDMap, k4EDM4hep2LcioConv::detail::InsertMode::Checked); if (pidInserted) { lval.addToParticleIDs(pid); particleIDs->push_back(pid); } else { - lval.addToParticleIDs(pidIt->second); + lval.addToParticleIDs(k4EDM4hep2LcioConv::detail::getMapped(pidIt)); } } @@ -140,13 +143,13 @@ namespace LCIO2EDM4hepConv { if (lcioPidUsed == nullptr) { continue; } - if (const auto it = particleIDMap.find(lcioPidUsed); it != particleIDMap.end()) { - lval.setParticleIDUsed(it->second); + if (const auto edm4hepPid = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioPidUsed, particleIDMap)) { + lval.setParticleIDUsed(edm4hepPid.value()); } else { auto pid = convertParticleID(lcioPidUsed); particleIDs->push_back(pid); - particleIDMap.emplace(lcioPidUsed, pid); + k4EDM4hep2LcioConv::detail::mapInsert(lcioPidUsed, pid, particleIDMap); lval.setParticleIDUsed(pid); } } @@ -158,10 +161,9 @@ namespace LCIO2EDM4hepConv { return results; } - std::unique_ptr convertVertices( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& vertexMap) + template + std::unique_ptr + convertVertices(const std::string& name, EVENT::LCCollection* LCCollection, VertexMapT& vertexMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -182,9 +184,9 @@ namespace LCIO2EDM4hepConv { lval.addToParameters(v); } - const auto [iterator, inserted] = vertexMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, vertexMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -193,10 +195,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertSimTrackerHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimTrHitMap) + template + std::unique_ptr + convertSimTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, SimTrHitMapT& SimTrHitMap) { auto dest = std::make_unique(); @@ -214,9 +215,9 @@ namespace LCIO2EDM4hepConv { lval.setPosition(rval->getPosition()); lval.setMomentum(rval->getMomentum()); - const auto [iterator, inserted] = SimTrHitMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, SimTrHitMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -225,10 +226,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTPCHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TPCHitMap) + template + std::unique_ptr + convertTPCHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TPCHitMap) { auto dest = std::make_unique(); @@ -243,9 +243,9 @@ namespace LCIO2EDM4hepConv { for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { lval.addToAdcCounts(rval->getRawDataWord(j)); } - const auto [iterator, inserted] = TPCHitMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TPCHitMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -255,10 +255,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTrackerHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitMap) + template + std::unique_ptr + convertTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -277,9 +276,9 @@ namespace LCIO2EDM4hepConv { auto& m = rval->getCovMatrix(); lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - const auto [iterator, inserted] = TrackerHitMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackerHitMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -288,10 +287,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTrackerHitPlanes( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackerHitPlaneMap) + template + std::unique_ptr + convertTrackerHitPlanes(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitPlaneMap) { auto dest = std::make_unique(); @@ -315,9 +313,9 @@ namespace LCIO2EDM4hepConv { auto& m = rval->getCovMatrix(); lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - const auto [iterator, inserted] = TrackerHitPlaneMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackerHitPlaneMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -327,10 +325,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertTracks( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& TrackMap) + template + std::unique_ptr + convertTracks(const std::string& name, EVENT::LCCollection* LCCollection, TrackMapT& TrackMap) { auto dest = std::make_unique(); @@ -357,9 +354,9 @@ namespace LCIO2EDM4hepConv { quantities.value = rval->getdEdx(); quantities.error = rval->getdEdxError(); lval.addToDxQuantities(quantities); - const auto [iterator, inserted] = TrackMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -369,10 +366,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertSimCalorimeterHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& SimCaloHitMap) + template + std::unique_ptr + convertSimCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& SimCaloHitMap) { auto dest = std::make_unique(); for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { @@ -385,9 +381,9 @@ namespace LCIO2EDM4hepConv { lval.setEnergy(rval->getEnergy()); lval.setPosition(rval->getPosition()); - const auto [iterator, inserted] = SimCaloHitMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, SimCaloHitMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -397,10 +393,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertRawCalorimeterHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& rawCaloHitMap) + template + std::unique_ptr + convertRawCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& rawCaloHitMap) { auto dest = std::make_unique(); @@ -414,9 +409,9 @@ namespace LCIO2EDM4hepConv { lval.setAmplitude(rval->getAmplitude()); lval.setTimeStamp(rval->getTimeStamp()); - const auto [iterator, inserted] = rawCaloHitMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, rawCaloHitMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -426,10 +421,9 @@ namespace LCIO2EDM4hepConv { return dest; } - std::unique_ptr convertCalorimeterHits( - const std::string& name, - EVENT::LCCollection* LCCollection, - TypeMapT& caloHitMap) + template + std::unique_ptr + convertCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& caloHitMap) { auto dest = std::make_unique(); @@ -445,9 +439,9 @@ namespace LCIO2EDM4hepConv { lval.setTime(rval->getTime()); lval.setType(rval->getType()); - const auto [iterator, inserted] = caloHitMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, caloHitMap); if (!inserted) { - auto existing = iterator->second; + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); const auto existingId = existing.id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -457,11 +451,12 @@ namespace LCIO2EDM4hepConv { return dest; } + template std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, - TypeMapT& clusterMap, - TypeMapT& particleIDMap) + ClusterMapT& clusterMap, + PIDMapT& particleIDMap) { auto particleIDs = std::make_unique(); auto dest = std::make_unique(); @@ -480,9 +475,9 @@ namespace LCIO2EDM4hepConv { lval.setType(rval->getType()); lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); - const auto [iterator, inserted] = clusterMap.emplace(rval, lval); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, clusterMap); if (!inserted) { - auto existing = iterator->first; + auto existing = k4EDM4hep2LcioConv::detail::getKey(iterator); const auto existingId = existing->id(); std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name << " collection" << std::endl; @@ -492,13 +487,13 @@ namespace LCIO2EDM4hepConv { // collection in LCIO for (const auto lcioPid : rval->getParticleIDs()) { auto pid = convertParticleID(lcioPid); - const auto [pidIt, pidInserted] = particleIDMap.emplace(lcioPid, pid); + const auto [pidIt, pidInserted] = k4EDM4hep2LcioConv::detail::mapInsert(lcioPid, pid, particleIDMap); if (pidInserted) { lval.addToParticleIDs(pid); particleIDs->push_back(pid); } else { - lval.addToParticleIDs(pidIt->second); + lval.addToParticleIDs(k4EDM4hep2LcioConv::detail::getMapped(pidIt)); } } } @@ -509,8 +504,9 @@ namespace LCIO2EDM4hepConv { return results; } + template std::vector - convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, LcioEdmTypeMapping& typeMapping) + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, ObjectMappingT& typeMapping) { const auto& type = LCCollection->getTypeName(); std::vector retColls; @@ -562,9 +558,10 @@ namespace LCIO2EDM4hepConv { return retColls; } + template std::unique_ptr createCaloHitContributions( - TypeMapT& SimCaloHitMap, - const TypeMapT& mcparticlesMap) + HitMapT& SimCaloHitMap, + const MCParticleMapT& mcparticlesMap) { auto contrCollection = std::make_unique(); for (auto& [lcioHit, edmHit] : SimCaloHitMap) { @@ -579,9 +576,8 @@ namespace LCIO2EDM4hepConv { edm_contr.setStepPosition(lcioHit->getStepPosition(j)); auto lcioParticle = (lcioHit->getParticleCont(j)); if (lcioParticle != nullptr) { - const auto it = mcparticlesMap.find(lcioParticle); - if (it != mcparticlesMap.end()) { - edm_contr.setParticle(it->second); + if (const auto edm4hepParticle = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioParticle, mcparticlesMap)) { + edm_contr.setParticle(edm4hepParticle.value()); } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for a LCIO MCParticle, " @@ -675,7 +671,8 @@ namespace LCIO2EDM4hepConv { return event; } - void resolveRelationsMCParticles(TypeMapT& mcparticlesMap) + template + void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap) { int edmnum = 1; for (auto& [lcio, edm] : mcparticlesMap) { @@ -683,15 +680,12 @@ namespace LCIO2EDM4hepConv { auto daughters = lcio->getDaughters(); auto parents = lcio->getParents(); - int dnum = 1; for (auto d : daughters) { if (d == nullptr) { continue; } - const auto it = mcparticlesMap.find(d); - dnum++; - if (it != mcparticlesMap.end()) { - edm.addToDaughters(it->second); + if (const auto edmD = k4EDM4hep2LcioConv::detail::mapLookupTo(d, mcparticlesMap)) { + edm.addToDaughters(edmD.value()); } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " @@ -703,9 +697,8 @@ namespace LCIO2EDM4hepConv { if (p == nullptr) { continue; } - const auto it = mcparticlesMap.find(p); - if (it != mcparticlesMap.end()) { - edm.addToParents(it->second); + if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(p, mcparticlesMap)) { + edm.addToParents(edmP.value()); } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " @@ -716,18 +709,16 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsSimTrackerHits( - TypeMapT& SimTrHitMap, - TypeMapT& mcparticlesMap) + template + void resolveRelationsSimTrackerHits(HitMapT& SimTrHitMap, const MCParticleMapT& mcparticlesMap) { for (auto& [lcio, edm] : SimTrHitMap) { auto mcps = lcio->getMCParticle(); if (mcps == nullptr) { continue; } - const auto it = mcparticlesMap.find(mcps); - if (it != mcparticlesMap.end()) { - edm.setMCParticle(it->second); + if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(mcps, mcparticlesMap)) { + edm.setMCParticle(edmP.value()); } else { std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " @@ -737,11 +728,12 @@ namespace LCIO2EDM4hepConv { } } + template void resolveRelationsRecoParticles( - TypeMapT& recoparticlesMap, - const TypeMapT& vertexMap, - const TypeMapT& clusterMap, - const TypeMapT& tracksMap) + RecoParticleMapT& recoparticlesMap, + const VertexMapT& vertexMap, + const ClusterMapT& clusterMap, + const TrackMapT& tracksMap) { int edmnum = 1; for (auto& [lcio, edm] : recoparticlesMap) { @@ -764,9 +756,8 @@ namespace LCIO2EDM4hepConv { if (c == nullptr) { continue; } - const auto it = clusterMap.find(c); - if (it != clusterMap.end()) { - edm.addToClusters(it->second); + if (const auto edmC = k4EDM4hep2LcioConv::detail::mapLookupTo(c, clusterMap)) { + edm.addToClusters(edmC.value()); } else { std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " @@ -780,9 +771,8 @@ namespace LCIO2EDM4hepConv { if (t == nullptr) { continue; } - const auto it = tracksMap.find(t); - if (it != tracksMap.end()) { - edm.addToTracks(it->second); + if (const auto edmT = k4EDM4hep2LcioConv::detail::mapLookupTo(t, tracksMap)) { + edm.addToTracks(edmT.value()); } else { std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " @@ -796,9 +786,8 @@ namespace LCIO2EDM4hepConv { if (p == nullptr) { continue; } - const auto it = recoparticlesMap.find(p); - if (it != recoparticlesMap.end()) { - edm.addToParticles(it->second); + if (const auto edmReco = k4EDM4hep2LcioConv::detail::mapLookupTo(p, recoparticlesMap)) { + edm.addToParticles(edmReco.value()); } else { std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " @@ -809,9 +798,8 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsClusters( - TypeMapT& clustersMap, - const TypeMapT& caloHitMap) + template + void resolveRelationsClusters(ClusterMapT& clustersMap, const CaloHitMapT& caloHitMap) { for (auto& [lcio, edm] : clustersMap) { auto clusters = lcio->getClusters(); @@ -822,9 +810,8 @@ namespace LCIO2EDM4hepConv { if (c == nullptr) { continue; } - const auto it = clustersMap.find(c); - if (it != clustersMap.end()) { - edm.addToClusters(it->second); + if (const auto edmC = k4EDM4hep2LcioConv::detail::mapLookupTo(c, clustersMap)) { + edm.addToClusters(edmC.value()); } else { std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; @@ -834,9 +821,8 @@ namespace LCIO2EDM4hepConv { if (cal == nullptr) { continue; } - const auto it = caloHitMap.find(cal); - if (it != caloHitMap.end()) { - edm.addToHits(it->second); + if (const auto edmCaloHit = k4EDM4hep2LcioConv::detail::mapLookupTo(cal, caloHitMap)) { + edm.addToHits(edmCaloHit.value()); } else { std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; @@ -851,11 +837,12 @@ namespace LCIO2EDM4hepConv { } } + template void resolveRelationsTracks( - TypeMapT& tracksMap, - const TypeMapT& trackerHitMap, - const TypeMapT& TPCHitMap, - const TypeMapT& trackerhitplaneMap) + TrackMapT& tracksMap, + const TrackHitMapT& trackerHitMap, + const TPCHitMapT&, + const THPlaneHitMapT&) { for (auto& [lcio, edm] : tracksMap) { auto tracks = lcio->getTracks(); @@ -864,9 +851,8 @@ namespace LCIO2EDM4hepConv { if (t == nullptr) { continue; } - const auto it = tracksMap.find(t); - if (it != tracksMap.end()) { - edm.addToTracks(it->second); + if (const auto track = k4EDM4hep2LcioConv::detail::mapLookupTo(t, tracksMap)) { + edm.addToTracks(track.value()); } else { // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; @@ -876,9 +862,8 @@ namespace LCIO2EDM4hepConv { if (th == nullptr) { continue; } - const auto it = trackerHitMap.find(th); - if (it != trackerHitMap.end()) { - edm.addToTrackerHits(it->second); + if (const auto trHit = k4EDM4hep2LcioConv::detail::mapLookupTo(th, trackerHitMap)) { + edm.addToTrackerHits(trHit.value()); } // else { // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" @@ -913,18 +898,16 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelationsVertices( - TypeMapT& vertexMap, - const TypeMapT& recoparticleMap) + template + void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap) { for (auto& [lcio, edm] : vertexMap) { auto recoparticle = lcio->getAssociatedParticle(); if (recoparticle == nullptr) { continue; } - const auto it = recoparticleMap.find(recoparticle); - if (it != recoparticleMap.end()) { - edm.setAssociatedParticle(it->second); + if (const auto recoP = k4EDM4hep2LcioConv::detail::mapLookupTo(recoparticle, recoparticleMap)) { + edm.setAssociatedParticle(recoP.value()); } else { std::cerr << "Couldn't find associated Particle to add to Vertex " @@ -933,7 +916,8 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelations(LcioEdmTypeMapping& typeMapping) + template + void resolveRelations(ObjectMappingT& typeMapping) { resolveRelationsMCParticles(typeMapping.mcParticles); resolveRelationsRecoParticles( @@ -945,8 +929,9 @@ namespace LCIO2EDM4hepConv { resolveRelationsVertices(typeMapping.vertices, typeMapping.recoParticles); } + template std::vector createAssociations( - const LcioEdmTypeMapping& typeMapping, + const ObjectMappingT& typeMapping, const std::vector>& LCRelation) { std::vector assoCollVec; @@ -1050,8 +1035,9 @@ namespace LCIO2EDM4hepConv { return assoCollVec; } + template std::unique_ptr - fillSubset(EVENT::LCCollection* LCCollection, const LcioEdmTypeMapping& typeMapping, const std::string& type) + fillSubset(EVENT::LCCollection* LCCollection, const ObjectMappingT& typeMapping, const std::string& type) { if (type == "MCParticle") { return handleSubsetColl(LCCollection, typeMapping.mcParticles); From ba569860393f44b5a14efe4744db1bd67c0a4aaa Mon Sep 17 00:00:00 2001 From: tmadlener Date: Thu, 6 Jul 2023 13:56:43 +0200 Subject: [PATCH 06/20] Actually make getKey and getMapped usable with maps and vectors --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index ffa8da6f..fe0a66db 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -152,6 +152,11 @@ namespace k4EDM4hep2LcioConv { } } + /// Helper type alias that can be used to detect whether a T can be used + /// with std::get directly or whether it has to be dereferenced first + template + using std_get_usable = decltype(std::get<0>(std::declval())); + /** * Helper function to get the Key from an Iterator (e.g. returned by * mapInsert). This is necessary because map::emplace returns an iterator, @@ -161,13 +166,12 @@ namespace k4EDM4hep2LcioConv { template auto getKey(const It& it) { - return std::get<0>(it); - } - - template - auto getKey(const It* it) - { - return std::get<0>(*it); + if constexpr (det::is_detected_v) { + return std::get<0>(it); + } + else { + return std::get<0>(*it); + } } /** @@ -179,13 +183,12 @@ namespace k4EDM4hep2LcioConv { template auto getMapped(const It& it) { - return std::get<1>(it); - } - - template - auto getMapped(const It* it) - { - return std::get<1>(*it); + if constexpr (det::is_detected_v) { + return std::get<1>(it); + } + else { + return std::get<1>(*it); + } } } // namespace detail From 51ef89f634f8cf45a06ae9e29127e150ae7a4d20 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 13 Sep 2023 11:19:27 +0200 Subject: [PATCH 07/20] Add references and remove hand-rolled find --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 8 ++++---- k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp | 15 +++++---------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 3f9e58c8..031be563 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -91,18 +91,18 @@ namespace EDM4hep2LCIOConv { lcio::LCCollectionVec* convTrackerHits( const edm4hep::TrackerHitCollection* const trackerhits_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& trackerhits_vec); lcio::LCCollectionVec* convSimTrackerHits( const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& simtrackerhits_vec, const vec_pair& mcparticles_vec); lcio::LCCollectionVec* convCalorimeterHits( const edm4hep::CalorimeterHitCollection* const calohit_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& calo_hits_vec); lcio::LCCollectionVec* convRawCalorimeterHits( @@ -111,7 +111,7 @@ namespace EDM4hep2LCIOConv { lcio::LCCollectionVec* convSimCalorimeterHits( const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& sim_calo_hits_vec, const vec_pair& mcparticles); diff --git a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp index b1443422..82f86002 100644 --- a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp @@ -136,7 +136,7 @@ namespace EDM4hep2LCIOConv { // Add LCIO Collection Vector to LCIO event lcio::LCCollectionVec* convTrackerHits( const edm4hep::TrackerHitCollection* const trackerhits_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& trackerhits_vec) { auto* trackerhits = new lcio::LCCollectionVec(lcio::LCIO::TRACKERHIT); @@ -183,7 +183,7 @@ namespace EDM4hep2LCIOConv { // Add LCIO Collection Vector to LCIO event lcio::LCCollectionVec* convSimTrackerHits( const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& simtrackerhits_vec, const vec_pair& mcparticles_vec) { @@ -242,7 +242,7 @@ namespace EDM4hep2LCIOConv { // Add converted LCIO Collection Vector to LCIO event lcio::LCCollectionVec* convCalorimeterHits( const edm4hep::CalorimeterHitCollection* const calohit_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& calo_hits_vec) { auto* calohits = new lcio::LCCollectionVec(lcio::LCIO::CALORIMETERHIT); @@ -317,7 +317,7 @@ namespace EDM4hep2LCIOConv { // Add converted LCIO Collection Vector to LCIO event lcio::LCCollectionVec* convSimCalorimeterHits( const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, - const std::string cellIDstr, + const std::string& cellIDstr, vec_pair& sim_calo_hits_vec, const vec_pair& mcparticles) { @@ -862,12 +862,7 @@ namespace EDM4hep2LCIOConv { bool collectionExist(const std::string& collection_name, const lcio::LCEventImpl* lcio_event) { const auto* coll = lcio_event->getCollectionNames(); - for (int i = 0; i < coll->size(); ++i) { - if (collection_name == coll->at(i)) { - return true; - } - } - return false; + return std::find(coll->begin(), coll->end(), collection_name) != coll->end(); } std::unique_ptr convEvent(const podio::Frame& edmEvent, const podio::Frame& metadata) From f39108f1c2b5de7f5e91ee11d39c874777ce2886 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 5 Jul 2023 19:14:29 +0200 Subject: [PATCH 08/20] Switch EDM4hep to LCIO conversion to generic functionality - Make conversion functions templated on map - Default map to what was there before to make it usable as a shared library --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 90 +++++++++----- k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp | 116 +++++++++--------- 2 files changed, 117 insertions(+), 89 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 031be563..348506e8 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -68,81 +68,105 @@ namespace edm4hep { namespace EDM4hep2LCIOConv { template - using vec_pair = std::vector>; + using TypeMapT = k4EDM4hep2LcioConv::MapT; struct CollectionsPairVectors { - vec_pair tracks; - vec_pair trackerhits; - vec_pair simtrackerhits; - vec_pair calohits; - vec_pair rawcalohits; - vec_pair simcalohits; - vec_pair tpchits; - vec_pair clusters; - vec_pair vertices; - vec_pair recoparticles; - vec_pair mcparticles; + TypeMapT tracks {}; + TypeMapT trackerhits {}; + TypeMapT simtrackerhits {}; + TypeMapT calohits {}; + TypeMapT rawcalohits {}; + TypeMapT simcalohits {}; + TypeMapT tpchits {}; + TypeMapT clusters {}; + TypeMapT vertices {}; + TypeMapT recoparticles {}; + TypeMapT mcparticles {}; }; + template< + typename TrackMapT = TypeMapT, + typename TrackerHitMapT = TypeMapT> lcio::LCCollectionVec* convTracks( const edm4hep::TrackCollection* const tracks_coll, - vec_pair& tracks_vec, - const vec_pair& trackerhits_vec); + TrackMapT& tracks_vec, + const TrackerHitMapT& trackerhits_vec); + template> lcio::LCCollectionVec* convTrackerHits( const edm4hep::TrackerHitCollection* const trackerhits_coll, const std::string& cellIDstr, - vec_pair& trackerhits_vec); + TrackerHitMapT& trackerhits_vec); + template< + typename SimTrHitMapT = TypeMapT, + typename MCParticleMapT = TypeMapT> lcio::LCCollectionVec* convSimTrackerHits( const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, const std::string& cellIDstr, - vec_pair& simtrackerhits_vec, - const vec_pair& mcparticles_vec); + SimTrHitMapT& simtrackerhits_vec, + const MCParticleMapT& mcparticles_vec); + template> lcio::LCCollectionVec* convCalorimeterHits( const edm4hep::CalorimeterHitCollection* const calohit_coll, const std::string& cellIDstr, - vec_pair& calo_hits_vec); + CaloHitMapT& calo_hits_vec); + template> lcio::LCCollectionVec* convRawCalorimeterHits( const edm4hep::RawCalorimeterHitCollection* const rawcalohit_coll, - vec_pair& raw_calo_hits_vec); + RawCaloHitMapT& raw_calo_hits_vec); + template> lcio::LCCollectionVec* convSimCalorimeterHits( const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, const std::string& cellIDstr, - vec_pair& sim_calo_hits_vec, - const vec_pair& mcparticles); + SimCaloHitMapT& sim_calo_hits_vec, + const MCParticleMapT& mcparticles); + template> lcio::LCCollectionVec* convTPCHits( const edm4hep::RawTimeSeriesCollection* const tpchit_coll, - vec_pair& tpc_hits_vec); + TPCHitMapT& tpc_hits_vec); + template< + typename ClusterMapT = TypeMapT, + typename CaloHitMapT = TypeMapT> lcio::LCCollectionVec* convClusters( const edm4hep::ClusterCollection* const cluster_coll, - vec_pair& cluster_vec, - const vec_pair& calohits_vec); + ClusterMapT& cluster_vec, + const CaloHitMapT& calohits_vec); + template< + typename VertexMapT = TypeMapT, + typename RecoPartMapT = TypeMapT> lcio::LCCollectionVec* convVertices( const edm4hep::VertexCollection* const vertex_coll, - vec_pair& vertex_vec, - const vec_pair& recoparticles_vec); - + VertexMapT& vertex_vec, + const RecoPartMapT& recoparticles_vec); + + template< + typename RecoPartMapT = TypeMapT, + typename TrackMapT = TypeMapT, + typename VertexMapT = TypeMapT, + typename ClusterMapT = TypeMapT> lcio::LCCollectionVec* convReconstructedParticles( const edm4hep::ReconstructedParticleCollection* const recos_coll, - vec_pair& recoparticles_vec, - const vec_pair& tracks_vec, - const vec_pair& vertex_vec, - const vec_pair& clusters_vec); + RecoPartMapT& recoparticles_vec, + const TrackMapT& tracks_vec, + const VertexMapT& vertex_vec, + const ClusterMapT& clusters_vec); + template> lcio::LCCollectionVec* convMCParticles( const edm4hep::MCParticleCollection* const mcparticle_coll, - vec_pair& mc_particles_vec); + MCPartMapT& mc_particles_vec); void convEventHeader(const edm4hep::EventHeaderCollection* const header_coll, lcio::LCEventImpl* const lcio_event); - void FillMissingCollections(CollectionsPairVectors& collection_pairs); + template + void FillMissingCollections(ObjectMappingT& collection_pairs); bool collectionExist(const std::string& collection_name, const lcio::LCEventImpl* lcio_event); diff --git a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp index 82f86002..343ad404 100644 --- a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp @@ -25,10 +25,11 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep Tracks to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convTracks( const edm4hep::TrackCollection* const tracks_coll, - vec_pair& tracks_vec, - const vec_pair& trackerhits_vec) + TrackMapT& tracks_vec, + const TrackerHitMapT& trackerhits_vec) { auto* tracks = new lcio::LCCollectionVec(lcio::LCIO::TRACK); @@ -109,7 +110,7 @@ namespace EDM4hep2LCIOConv { } // Save intermediate tracks ref - tracks_vec.emplace_back(std::make_pair(lcio_tr, edm_tr)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_tr, edm_tr, tracks_vec); // Add to lcio tracks collection tracks->addElement(lcio_tr); @@ -121,9 +122,8 @@ namespace EDM4hep2LCIOConv { for (const auto& edm_linked_tr : edm_tr.getTracks()) { if (edm_linked_tr.isAvailable()) { // Search the linked track in the converted vector - if (const auto lcio_tr_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_tr, tracks_vec)) { + if (const auto lcio_tr_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_tr, tracks_vec)) lcio_tr->addTrack(lcio_tr_linked.value()); - } } } } @@ -134,10 +134,11 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep TrackerHits to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convTrackerHits( const edm4hep::TrackerHitCollection* const trackerhits_coll, const std::string& cellIDstr, - vec_pair& trackerhits_vec) + TrackerHitMapT& trackerhits_vec) { auto* trackerhits = new lcio::LCCollectionVec(lcio::LCIO::TRACKERHIT); @@ -168,7 +169,7 @@ namespace EDM4hep2LCIOConv { } // Save intermediate trackerhits ref - trackerhits_vec.emplace_back(std::make_pair(lcio_trh, edm_trh)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_trh, edm_trh, trackerhits_vec); // Add to lcio trackerhits collection trackerhits->addElement(lcio_trh); @@ -181,11 +182,12 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep SimTrackerHits to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convSimTrackerHits( const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, const std::string& cellIDstr, - vec_pair& simtrackerhits_vec, - const vec_pair& mcparticles_vec) + SimTrHitMapT& simtrackerhits_vec, + const MCParticleMapT& mcparticles_vec) { auto* simtrackerhits = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); @@ -217,7 +219,7 @@ namespace EDM4hep2LCIOConv { // Link converted MCParticle to the SimTrackerHit if found const auto edm_strh_mcp = edm_strh.getMCParticle(); if (edm_strh_mcp.isAvailable()) { - if (const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, mcparticles_vec)) { + if (const auto& lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, mcparticles_vec)) { lcio_strh->setMCParticle(lcio_mcp.value()); } else { @@ -227,7 +229,7 @@ namespace EDM4hep2LCIOConv { } // Save intermediate simtrackerhits ref - simtrackerhits_vec.emplace_back(std::make_pair(lcio_strh, edm_strh)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_strh, edm_strh, simtrackerhits_vec); // Add to lcio simtrackerhits collection simtrackerhits->addElement(lcio_strh); @@ -240,10 +242,12 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep Calorimeter Hits to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + + template lcio::LCCollectionVec* convCalorimeterHits( const edm4hep::CalorimeterHitCollection* const calohit_coll, const std::string& cellIDstr, - vec_pair& calo_hits_vec) + CaloHitMapT& calo_hits_vec) { auto* calohits = new lcio::LCCollectionVec(lcio::LCIO::CALORIMETERHIT); @@ -271,7 +275,7 @@ namespace EDM4hep2LCIOConv { // lcio_calohit->setRawHit(EVENT::LCObject* rawHit ); // Save Calorimeter Hits LCIO and EDM4hep collections - calo_hits_vec.emplace_back(std::make_pair(lcio_calohit, edm_calohit)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_calohit, edm_calohit, calo_hits_vec); // Add to lcio tracks collection calohits->addElement(lcio_calohit); @@ -284,9 +288,10 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep RAW Calorimeter Hits to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convRawCalorimeterHits( const edm4hep::RawCalorimeterHitCollection* const rawcalohit_coll, - vec_pair& raw_calo_hits_vec) + RawCaloHitMapT& raw_calo_hits_vec) { auto* rawcalohits = new lcio::LCCollectionVec(lcio::LCIO::RAWCALORIMETERHIT); @@ -302,7 +307,7 @@ namespace EDM4hep2LCIOConv { lcio_rawcalohit->setTimeStamp(edm_raw_calohit.getTimeStamp()); // Save Raw Calorimeter Hits LCIO and EDM4hep collections - raw_calo_hits_vec.emplace_back(std::make_pair(lcio_rawcalohit, edm_raw_calohit)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_rawcalohit, edm_raw_calohit, raw_calo_hits_vec); // Add to lcio tracks collection rawcalohits->addElement(lcio_rawcalohit); @@ -315,11 +320,12 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep Sim Calorimeter Hits to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convSimCalorimeterHits( const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, const std::string& cellIDstr, - vec_pair& sim_calo_hits_vec, - const vec_pair& mcparticles) + SimCaloHitMapT& sim_calo_hits_vec, + const MCParticleMapT& mcparticles) { auto* simcalohits = new lcio::LCCollectionVec(lcio::LCIO::SIMCALORIMETERHIT); @@ -344,7 +350,7 @@ namespace EDM4hep2LCIOConv { // MCParticles converted // Save Sim Calorimeter Hits LCIO and EDM4hep collections - sim_calo_hits_vec.emplace_back(std::make_pair(lcio_simcalohit, edm_sim_calohit)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_simcalohit, edm_sim_calohit, sim_calo_hits_vec); // Add to sim calo hits collection simcalohits->addElement(lcio_simcalohit); @@ -357,9 +363,10 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep TPC Hits to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convTPCHits( const edm4hep::RawTimeSeriesCollection* const tpchit_coll, - vec_pair& tpc_hits_vec) + TPCHitMapT& tpc_hits_vec) { auto* tpchits = new lcio::LCCollectionVec(lcio::LCIO::TPCHIT); @@ -389,7 +396,7 @@ namespace EDM4hep2LCIOConv { #endif // Save TPC Hits LCIO and EDM4hep collections - tpc_hits_vec.emplace_back(std::make_pair(lcio_tpchit, edm_tpchit)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_tpchit, edm_tpchit, tpc_hits_vec); // Add to lcio tracks collection tpchits->addElement(lcio_tpchit); @@ -402,10 +409,11 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep Clusters to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convClusters( const edm4hep::ClusterCollection* const cluster_coll, - vec_pair& cluster_vec, - const vec_pair& calohits_vec) + ClusterMapT& cluster_vec, + const CaloHitMapT& calohits_vec) { auto* clusters = new lcio::LCCollectionVec(lcio::LCIO::CLUSTER); @@ -456,7 +464,7 @@ namespace EDM4hep2LCIOConv { } // Add LCIO and EDM4hep pair collections to vec - cluster_vec.emplace_back(std::make_pair(lcio_cluster, edm_cluster)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_cluster, edm_cluster, cluster_vec); // Add to lcio tracks collection clusters->addElement(lcio_cluster); @@ -464,14 +472,13 @@ namespace EDM4hep2LCIOConv { } // Link associated clusters after converting all clusters - for (auto& [lcio_clutser, edm_cluster] : cluster_vec) { + for (auto& [lcio_cluster, edm_cluster] : cluster_vec) { for (const auto& edm_linked_cluster : edm_cluster.getClusters()) { if (edm_linked_cluster.isAvailable()) { - // Search the linked track in the converted vector if ( const auto lcio_cluster_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_cluster, cluster_vec)) { - lcio_clutser->addCluster(lcio_cluster_linked.value()); + lcio_cluster->addCluster(lcio_cluster_linked.value()); } } } @@ -483,10 +490,11 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep Vertices to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convVertices( const edm4hep::VertexCollection* const vertex_coll, - vec_pair& vertex_vec, - const vec_pair& recoparticles_vec) + VertexMapT& vertex_vec, + const RecoPartMapT& recoparticles_vec) { auto* vertices = new lcio::LCCollectionVec(lcio::LCIO::VERTEX); @@ -512,13 +520,12 @@ namespace EDM4hep2LCIOConv { lcio_vertex->setAssociatedParticle(lcio_rp.value()); } else { - // If recoparticle avilable, but not found in converted vec, add nullptr lcio_vertex->setAssociatedParticle(nullptr); } } // Add LCIO and EDM4hep pair collections to vec - vertex_vec.emplace_back(std::make_pair(lcio_vertex, edm_vertex)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_vertex, edm_vertex, vertex_vec); // Add to lcio tracks collection vertices->addElement(lcio_vertex); @@ -531,12 +538,13 @@ namespace EDM4hep2LCIOConv { // Convert EDM4hep RecoParticles to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convReconstructedParticles( const edm4hep::ReconstructedParticleCollection* const recos_coll, - vec_pair& recoparticles_vec, - const vec_pair& tracks_vec, - const vec_pair& vertex_vec, - const vec_pair& clusters_vec) + RecoPartMapT& recoparticles_vec, + const TrackMapT& tracks_vec, + const VertexMapT& vertex_vec, + const ClusterMapT& clusters_vec) { auto* recops = new lcio::LCCollectionVec(lcio::LCIO::RECONSTRUCTEDPARTICLE); @@ -597,7 +605,6 @@ namespace EDM4hep2LCIOConv { lcio_recp->setStartVertex(lcio_vertex.value()); } else { - // If particleID available, but not found in converted vec, add nullptr lcio_recp->setStartVertex(nullptr); } } @@ -609,7 +616,6 @@ namespace EDM4hep2LCIOConv { lcio_recp->addTrack(lcio_tr.value()); } else { - // If track available, but not found in converted vec, add nullptr lcio_recp->addTrack(nullptr); } } @@ -622,14 +628,13 @@ namespace EDM4hep2LCIOConv { lcio_recp->addCluster(lcio_cluster.value()); } else { - // If cluster available, but not found in converted vec, add nullptr lcio_recp->addCluster(nullptr); } } } // Add LCIO and EDM4hep pair collections to vec - recoparticles_vec.push_back(std::make_pair(lcio_recp, edm_rp)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_recp, edm_rp, recoparticles_vec); // Add to reconstructed particles collection recops->addElement(lcio_recp); @@ -654,9 +659,10 @@ namespace EDM4hep2LCIOConv { // Convert MC Particles to LCIO // Add converted LCIO ptr and original EDM4hep collection to vector of pairs // Add converted LCIO Collection Vector to LCIO event + template lcio::LCCollectionVec* convMCParticles( const edm4hep::MCParticleCollection* const mcparticle_coll, - vec_pair& mc_particles_vec) + MCPartMapT& mc_particles_vec) { auto* mcparticles = new lcio::LCCollectionVec(lcio::LCIO::MCPARTICLE); @@ -697,7 +703,7 @@ namespace EDM4hep2LCIOConv { lcio_mcp->setOverlay(edm_mcp.isOverlay()); // Add LCIO and EDM4hep pair collections to vec - mc_particles_vec.push_back(std::make_pair(lcio_mcp, edm_mcp)); + k4EDM4hep2LcioConv::detail::mapInsert(lcio_mcp, edm_mcp, mc_particles_vec); // Add to reconstructed particles collection mcparticles->addElement(lcio_mcp); @@ -706,11 +712,11 @@ namespace EDM4hep2LCIOConv { // Add parent MCParticles after converting all MCparticles for (auto& [lcio_mcp, edm_mcp] : mc_particles_vec) { - for (const auto& edm_parent_mcp : edm_mcp.getParents()) { - if (edm_parent_mcp.isAvailable()) { + for (const auto& emd_parent_mcp : edm_mcp.getParents()) { + if (emd_parent_mcp.isAvailable()) { // Search for the parent mcparticle in the converted vector if ( - const auto lcio_mcp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_parent_mcp, mc_particles_vec)) { + const auto lcio_mcp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(emd_parent_mcp, mc_particles_vec)) { lcio_mcp->addParent(lcio_mcp_linked.value()); } } @@ -737,7 +743,8 @@ namespace EDM4hep2LCIOConv { // Depending on the order of the collections in the parameters, // and for the mutual dependencies between some collections, // go over the possible missing associated collections and fill them. - void FillMissingCollections(CollectionsPairVectors& collection_pairs) + template + void FillMissingCollections(ObjectMappingT& collection_pairs) { // Fill missing Tracks collections for (auto& [lcio_tr, edm_tr] : collection_pairs.tracks) { @@ -789,7 +796,7 @@ namespace EDM4hep2LCIOConv { } // reconstructed particles // Fill missing Vertices collections - for (const auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { + for (auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { // Link Reconstructed Particles if (lcio_vertex->getAssociatedParticle() == nullptr) { const auto edm_rp = edm_vertex.getAssociatedParticle(); @@ -818,7 +825,7 @@ namespace EDM4hep2LCIOConv { auto edm_contrib_mcp = contrib.getParticle(); std::array step_position { contrib.getStepPosition()[0], contrib.getStepPosition()[1], contrib.getStepPosition()[2]}; - + bool mcp_found = false; EVENT::MCParticle* lcio_mcp = nullptr; if (edm_contrib_mcp.isAvailable()) { // if we have the MCParticle we look for its partner @@ -826,21 +833,18 @@ namespace EDM4hep2LCIOConv { k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcparticles).value_or(nullptr); } else { // edm mcp available - // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; + // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; } // we add contribution with whatever lcio mc particle we found lcio_sch->addMCParticleContribution( lcio_mcp, contrib.getEnergy(), contrib.getTime(), contrib.getPDG(), step_position.data()); - if (!lcio_mcp) { - // std::cout << "WARNING: No MCParticle found for this contribution." - // << "Make Sure MCParticles are converted! " - // << edm_contrib_mcp.id() - // << std::endl; - } + // if (!lcio_mcp) { + // std::cout << "WARNING: No MCParticle found for this contribution." + // << "Make Sure MCParticles are converted! " + // << edm_contrib_mcp.id() + // << std::endl; + // } } // all emd4hep contributions - - // We need to reset the energy to the original one, because adding - // contributions alters the energy in LCIO lcio_sch->setEnergy(edm_sch.getEnergy()); } // SimCaloHit From f3c85ea977f270a5753c326b52f61030e7be190a Mon Sep 17 00:00:00 2001 From: tmadlener Date: Tue, 12 Sep 2023 11:59:23 +0200 Subject: [PATCH 09/20] Rename TypeMapT to ObjectMapT for less confusion --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 60 ++++++------- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 90 +++++++++---------- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 348506e8..37e49466 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -68,89 +68,89 @@ namespace edm4hep { namespace EDM4hep2LCIOConv { template - using TypeMapT = k4EDM4hep2LcioConv::MapT; + using ObjectMapT = k4EDM4hep2LcioConv::MapT; struct CollectionsPairVectors { - TypeMapT tracks {}; - TypeMapT trackerhits {}; - TypeMapT simtrackerhits {}; - TypeMapT calohits {}; - TypeMapT rawcalohits {}; - TypeMapT simcalohits {}; - TypeMapT tpchits {}; - TypeMapT clusters {}; - TypeMapT vertices {}; - TypeMapT recoparticles {}; - TypeMapT mcparticles {}; + ObjectMapT tracks {}; + ObjectMapT trackerhits {}; + ObjectMapT simtrackerhits {}; + ObjectMapT calohits {}; + ObjectMapT rawcalohits {}; + ObjectMapT simcalohits {}; + ObjectMapT tpchits {}; + ObjectMapT clusters {}; + ObjectMapT vertices {}; + ObjectMapT recoparticles {}; + ObjectMapT mcparticles {}; }; template< - typename TrackMapT = TypeMapT, - typename TrackerHitMapT = TypeMapT> + typename TrackMapT = ObjectMapT, + typename TrackerHitMapT = ObjectMapT> lcio::LCCollectionVec* convTracks( const edm4hep::TrackCollection* const tracks_coll, TrackMapT& tracks_vec, const TrackerHitMapT& trackerhits_vec); - template> + template> lcio::LCCollectionVec* convTrackerHits( const edm4hep::TrackerHitCollection* const trackerhits_coll, const std::string& cellIDstr, TrackerHitMapT& trackerhits_vec); template< - typename SimTrHitMapT = TypeMapT, - typename MCParticleMapT = TypeMapT> + typename SimTrHitMapT = ObjectMapT, + typename MCParticleMapT = ObjectMapT> lcio::LCCollectionVec* convSimTrackerHits( const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, const std::string& cellIDstr, SimTrHitMapT& simtrackerhits_vec, const MCParticleMapT& mcparticles_vec); - template> + template> lcio::LCCollectionVec* convCalorimeterHits( const edm4hep::CalorimeterHitCollection* const calohit_coll, const std::string& cellIDstr, CaloHitMapT& calo_hits_vec); - template> + template> lcio::LCCollectionVec* convRawCalorimeterHits( const edm4hep::RawCalorimeterHitCollection* const rawcalohit_coll, RawCaloHitMapT& raw_calo_hits_vec); - template> + template> lcio::LCCollectionVec* convSimCalorimeterHits( const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, const std::string& cellIDstr, SimCaloHitMapT& sim_calo_hits_vec, const MCParticleMapT& mcparticles); - template> + template> lcio::LCCollectionVec* convTPCHits( const edm4hep::RawTimeSeriesCollection* const tpchit_coll, TPCHitMapT& tpc_hits_vec); template< - typename ClusterMapT = TypeMapT, - typename CaloHitMapT = TypeMapT> + typename ClusterMapT = ObjectMapT, + typename CaloHitMapT = ObjectMapT> lcio::LCCollectionVec* convClusters( const edm4hep::ClusterCollection* const cluster_coll, ClusterMapT& cluster_vec, const CaloHitMapT& calohits_vec); template< - typename VertexMapT = TypeMapT, - typename RecoPartMapT = TypeMapT> + typename VertexMapT = ObjectMapT, + typename RecoPartMapT = ObjectMapT> lcio::LCCollectionVec* convVertices( const edm4hep::VertexCollection* const vertex_coll, VertexMapT& vertex_vec, const RecoPartMapT& recoparticles_vec); template< - typename RecoPartMapT = TypeMapT, - typename TrackMapT = TypeMapT, - typename VertexMapT = TypeMapT, - typename ClusterMapT = TypeMapT> + typename RecoPartMapT = ObjectMapT, + typename TrackMapT = ObjectMapT, + typename VertexMapT = ObjectMapT, + typename ClusterMapT = ObjectMapT> lcio::LCCollectionVec* convReconstructedParticles( const edm4hep::ReconstructedParticleCollection* const recos_coll, RecoPartMapT& recoparticles_vec, @@ -158,7 +158,7 @@ namespace EDM4hep2LCIOConv { const VertexMapT& vertex_vec, const ClusterMapT& clusters_vec); - template> + template> lcio::LCCollectionVec* convMCParticles( const edm4hep::MCParticleCollection* const mcparticle_coll, MCPartMapT& mc_particles_vec); diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 6df90c7b..cb5775bf 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -63,26 +63,26 @@ namespace LCIO2EDM4hepConv { template - using TypeMapT = k4EDM4hep2LcioConv::MapT; + using ObjectMapT = k4EDM4hep2LcioConv::MapT; /** * Maping holding all the original and converted objects in a 1:1 mapping in a * way that makes the lookup from LCIO to EDM4hep easy. */ struct LcioEdmTypeMapping { - TypeMapT tracks {}; - TypeMapT trackerHits {}; - TypeMapT simTrackerHits {}; - TypeMapT caloHits {}; - TypeMapT rawCaloHits {}; - TypeMapT simCaloHits {}; - TypeMapT tpcHits {}; - TypeMapT clusters {}; - TypeMapT vertices {}; - TypeMapT recoParticles {}; - TypeMapT mcParticles {}; - TypeMapT trackerHitPlanes {}; - TypeMapT particleIDs {}; + ObjectMapT tracks {}; + ObjectMapT trackerHits {}; + ObjectMapT simTrackerHits {}; + ObjectMapT caloHits {}; + ObjectMapT rawCaloHits {}; + ObjectMapT simCaloHits {}; + ObjectMapT tpcHits {}; + ObjectMapT clusters {}; + ObjectMapT vertices {}; + ObjectMapT recoParticles {}; + ObjectMapT mcParticles {}; + ObjectMapT trackerHitPlanes {}; + ObjectMapT particleIDs {}; }; using CollNamePair = std::tuple>; @@ -181,7 +181,7 @@ namespace LCIO2EDM4hepConv { * Convert an MCParticle collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, MCParticleMapT& mcparticlesMap); @@ -194,8 +194,8 @@ namespace LCIO2EDM4hepConv { * _particleIDs */ template< - typename RecoMapT = TypeMapT, - typename PIDMapT = TypeMapT> + typename RecoMapT = ObjectMapT, + typename PIDMapT = ObjectMapT> std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, @@ -206,7 +206,7 @@ namespace LCIO2EDM4hepConv { * Convert a Vertex collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertVertices(const std::string& name, EVENT::LCCollection* LCCollection, VertexMapT& vertexMap); @@ -214,7 +214,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimTrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertSimTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, SimTrHitMapT& SimTrHitMap); @@ -222,7 +222,7 @@ namespace LCIO2EDM4hepConv { * Convert a TPCHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertTPCHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TPCHitMap); @@ -230,7 +230,7 @@ namespace LCIO2EDM4hepConv { * Convert a TrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitMap); @@ -238,7 +238,7 @@ namespace LCIO2EDM4hepConv { * Convert a TrackerHitPlane collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertTrackerHitPlanes(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitPlaneMap); @@ -246,7 +246,7 @@ namespace LCIO2EDM4hepConv { * Convert a Track collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertTracks(const std::string& name, EVENT::LCCollection* LCCollection, TrackMapT& TrackMap); @@ -254,7 +254,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertSimCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& SimCaloHitMap); @@ -262,7 +262,7 @@ namespace LCIO2EDM4hepConv { * Convert a RawCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertRawCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& rawCaloHitMap); @@ -270,7 +270,7 @@ namespace LCIO2EDM4hepConv { * Convert a CalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template> std::unique_ptr convertCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& caloHitMap); @@ -283,8 +283,8 @@ namespace LCIO2EDM4hepConv { * _particleIDs */ template< - typename ClusterMapT = TypeMapT, - typename PIDMapT = TypeMapT> + typename ClusterMapT = ObjectMapT, + typename PIDMapT = ObjectMapT> std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, @@ -391,8 +391,8 @@ namespace LCIO2EDM4hepConv { * The contributions are also attached to their corresponding SimCalorimeterHits. */ template< - typename HitMapT = TypeMapT, - typename MCParticleMapT = TypeMapT> + typename HitMapT = ObjectMapT, + typename MCParticleMapT = ObjectMapT> std::unique_ptr createCaloHitContributions( HitMapT& SimCaloHitMap, const MCParticleMapT& mcparticlesMap); @@ -400,25 +400,25 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for the MCParticles. */ - template> + template> void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap); /** * Resolve the relations for SimTrackerHits */ template< - typename HitMapT = TypeMapT, - typename MCParticleMapT = TypeMapT> + typename HitMapT = ObjectMapT, + typename MCParticleMapT = ObjectMapT> void resolveRelationsSimTrackerHits(HitMapT& SimTrHitMap, const MCParticleMapT& mcparticlesMap); /** * Resolve the relations for ReconstructedParticles */ template< - typename RecoParticleMapT = TypeMapT, - typename VertexMapT = TypeMapT, - typename ClusterMapT = TypeMapT, - typename TrackMapT = TypeMapT> + typename RecoParticleMapT = ObjectMapT, + typename VertexMapT = ObjectMapT, + typename ClusterMapT = ObjectMapT, + typename TrackMapT = ObjectMapT> void resolveRelationsRecoParticles( RecoParticleMapT& recoparticlesMap, const VertexMapT& vertexMap, @@ -429,18 +429,18 @@ namespace LCIO2EDM4hepConv { * Resolve the relations for Clusters */ template< - typename ClusterMapT = TypeMapT, - typename CaloHitMapT = TypeMapT> + typename ClusterMapT = ObjectMapT, + typename CaloHitMapT = ObjectMapT> void resolveRelationsClusters(ClusterMapT& clustersMap, const CaloHitMapT& caloHitMap); /** * Resolve the relations for Tracks */ template< - typename TrackMapT = TypeMapT, - typename TrackHitMapT = TypeMapT, - typename TPCHitMapT = TypeMapT, - typename THPlaneHitMapT = TypeMapT> + typename TrackMapT = ObjectMapT, + typename TrackHitMapT = ObjectMapT, + typename TPCHitMapT = ObjectMapT, + typename THPlaneHitMapT = ObjectMapT> void resolveRelationsTracks( TrackMapT& tracksMap, const TrackHitMapT& trackerHitMap, @@ -451,8 +451,8 @@ namespace LCIO2EDM4hepConv { * Resolve the relations for Vertices */ template< - typename VertexMapT = TypeMapT, - typename RecoParticleMapT = TypeMapT> + typename VertexMapT = ObjectMapT, + typename RecoParticleMapT = ObjectMapT> void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap); template From 1fa9044b800870fd8adfa29ab73d6d776d44def6 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 13 Sep 2023 13:40:55 +0200 Subject: [PATCH 10/20] Make EDM4hep to LCIO conversion a template library --- k4EDM4hep2LcioConv/CMakeLists.txt | 9 +- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 16 + .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp | 834 +++++++++++++++++ k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp | 867 +----------------- 4 files changed, 869 insertions(+), 857 deletions(-) create mode 100644 k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp diff --git a/k4EDM4hep2LcioConv/CMakeLists.txt b/k4EDM4hep2LcioConv/CMakeLists.txt index f351e06f..276e6ede 100644 --- a/k4EDM4hep2LcioConv/CMakeLists.txt +++ b/k4EDM4hep2LcioConv/CMakeLists.txt @@ -12,9 +12,16 @@ target_link_libraries(k4EDM4hep2LcioConv PUBLIC ${LCIO_LIBRARIES} EDM4HEP::edm4hep) +set(public_headers + include/${PROJECT_NAME}/k4EDM4hep2LcioConv.h + include/${PROJECT_NAME}/k4EDM4hep2LcioConv.ipp + include/${PROJECT_NAME}/k4Lcio2EDM4hepConv.h + include/${PROJECT_NAME}/MappingUtils.h +) + set_target_properties(${PROJECT_NAME} PROPERTIES - PUBLIC_HEADER "include/${PROJECT_NAME}/k4EDM4hep2LcioConv.h;include/${PROJECT_NAME}/k4Lcio2EDM4hepConv.h;include/${PROJECT_NAME}/MappingUtils.h" + PUBLIC_HEADER "${public_headers}" ) install(TARGETS k4EDM4hep2LcioConv diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 37e49466..52c06048 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -36,6 +36,20 @@ namespace edm4hep { #include #include +#if __has_include("edm4hep/EDM4hepVersion.h") +#include "edm4hep/EDM4hepVersion.h" +#else + // Copy the necessary parts from the header above to make whatever we need to work here +#define EDM4HEP_VERSION(major, minor, patch) ((UINT64_C(major) << 32) | (UINT64_C(minor) << 16) | (UINT64_C(patch))) + // v00-07-02 is the last version without that still has TPCHits +#if __has_include("edm4hep/TPCHitCollection.h") +#define EDM4HEP_BUILD_VERSION EDM4HEP_VERSION(0, 7, 2) +#else +// v00-09 is the last version without the capitalization change of the track vector members +#define EDM4HEP_BUILD_VERSION EDM4HEP_VERSION(0, 9, 0) +#endif +#endif + #include "podio/Frame.h" // LCIO @@ -179,4 +193,6 @@ namespace EDM4hep2LCIOConv { } // namespace EDM4hep2LCIOConv +#include "k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp" + #endif diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp new file mode 100644 index 00000000..2ac06db7 --- /dev/null +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp @@ -0,0 +1,834 @@ +#include "k4EDM4hep2LcioConv/MappingUtils.h" + +#include + +namespace EDM4hep2LCIOConv { + + // Convert EDM4hep Tracks to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convTracks( + const edm4hep::TrackCollection* const tracks_coll, + TrackMapT& tracks_vec, + const TrackerHitMapT& trackerhits_vec) + { + auto* tracks = new lcio::LCCollectionVec(lcio::LCIO::TRACK); + + // Loop over EDM4hep tracks converting them to lcio tracks. + for (const auto& edm_tr : (*tracks_coll)) { + if (edm_tr.isAvailable()) { + auto* lcio_tr = new lcio::TrackImpl(); + // The Type of the Tracks need to be set bitwise in LCIO since the setType(int) function is private for the LCIO + // TrackImpl and only a setTypeBit(bitnumber) function can be used to set the Type bit by bit. + int type = edm_tr.getType(); + for (int i = 0; i < sizeof(int) * 8; i++) { + lcio_tr->setTypeBit(i, type & (1 << i)); + } + lcio_tr->setChi2(edm_tr.getChi2()); + lcio_tr->setNdf(edm_tr.getNdf()); + lcio_tr->setdEdx(edm_tr.getDEdx()); + lcio_tr->setdEdxError(edm_tr.getDEdxError()); + lcio_tr->setRadiusOfInnermostHit(edm_tr.getRadiusOfInnermostHit()); + +#if EDM4HEP_BUILD_VERSION > EDM4HEP_VERSION(0, 9, 0) + // Loop over the hit Numbers in the track + lcio_tr->subdetectorHitNumbers().resize(edm_tr.subdetectorHitNumbers_size()); + for (int i = 0; i < edm_tr.subdetectorHitNumbers_size(); ++i) { + lcio_tr->subdetectorHitNumbers()[i] = edm_tr.getSubdetectorHitNumbers(i); + } + + // Pad until 50 hitnumbers are resized + const int hit_number_limit = 50; + if (edm_tr.subdetectorHitNumbers_size() < hit_number_limit) { + lcio_tr->subdetectorHitNumbers().resize(hit_number_limit); + for (int i = edm_tr.subdetectorHitNumbers_size(); i < hit_number_limit; ++i) { + lcio_tr->subdetectorHitNumbers()[i] = 0; + } + } +#else + // Loop over the hit Numbers in the track + lcio_tr->subdetectorHitNumbers().resize(edm_tr.subDetectorHitNumbers_size()); + for (int i = 0; i < edm_tr.subDetectorHitNumbers_size(); ++i) { + lcio_tr->subdetectorHitNumbers()[i] = edm_tr.getSubDetectorHitNumbers(i); + } + + // Pad until 50 hitnumbers are resized + const int hit_number_limit = 50; + if (edm_tr.subDetectorHitNumbers_size() < hit_number_limit) { + lcio_tr->subdetectorHitNumbers().resize(hit_number_limit); + for (int i = edm_tr.subDetectorHitNumbers_size(); i < hit_number_limit; ++i) { + lcio_tr->subdetectorHitNumbers()[i] = 0; + } + } +#endif + + // Link multiple associated TrackerHits if found in converted ones + for (const auto& edm_rp_trh : edm_tr.getTrackerHits()) { + if (edm_rp_trh.isAvailable()) { + if (const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_trh, trackerhits_vec)) { + lcio_tr->addHit(lcio_trh.value()); + } + } + } + + // Loop over the track states in the track + const podio::RelationRange edm_track_states = edm_tr.getTrackStates(); + for (const auto& tr_state : edm_track_states) { + const auto& cov = tr_state.covMatrix; + std::array refP = {tr_state.referencePoint.x, tr_state.referencePoint.y, tr_state.referencePoint.z}; + + auto* lcio_tr_state = new lcio::TrackStateImpl( + tr_state.location, + tr_state.D0, + tr_state.phi, + tr_state.omega, + tr_state.Z0, + tr_state.tanLambda, + cov.data(), + refP.data()); + + lcio_tr->addTrackState(lcio_tr_state); + } + + // Save intermediate tracks ref + k4EDM4hep2LcioConv::detail::mapInsert(lcio_tr, edm_tr, tracks_vec); + + // Add to lcio tracks collection + tracks->addElement(lcio_tr); + } + } + + // Link associated tracks after converting all tracks + for (auto& [lcio_tr, edm_tr] : tracks_vec) { + for (const auto& edm_linked_tr : edm_tr.getTracks()) { + if (edm_linked_tr.isAvailable()) { + // Search the linked track in the converted vector + if (const auto lcio_tr_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_tr, tracks_vec)) + lcio_tr->addTrack(lcio_tr_linked.value()); + } + } + } + + return tracks; + } + + // Convert EDM4hep TrackerHits to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convTrackerHits( + const edm4hep::TrackerHitCollection* const trackerhits_coll, + const std::string& cellIDstr, + TrackerHitMapT& trackerhits_vec) + { + auto* trackerhits = new lcio::LCCollectionVec(lcio::LCIO::TRACKERHIT); + + if (cellIDstr != "") { + lcio::CellIDEncoder idEnc(cellIDstr, trackerhits); + } + + // Loop over EDM4hep trackerhits converting them to lcio trackerhits + for (const auto& edm_trh : (*trackerhits_coll)) { + if (edm_trh.isAvailable()) { + auto* lcio_trh = new lcio::TrackerHitImpl(); + + uint64_t combined_value = edm_trh.getCellID(); + uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); + lcio_trh->setCellID0(combined_value_ptr[0]); + lcio_trh->setCellID1(combined_value_ptr[1]); + lcio_trh->setType(edm_trh.getType()); + std::array positions {edm_trh.getPosition()[0], edm_trh.getPosition()[1], edm_trh.getPosition()[2]}; + lcio_trh->setPosition(positions.data()); + lcio_trh->setCovMatrix(edm_trh.getCovMatrix().data()); + lcio_trh->setEDep(edm_trh.getEDep()); + lcio_trh->setEDepError(edm_trh.getEDepError()); + lcio_trh->setTime(edm_trh.getTime()); + lcio_trh->setQuality(edm_trh.getQuality()); + std::bitset type_bits = edm_trh.getQuality(); + for (int j = 0; j < sizeof(uint32_t); j++) { + lcio_trh->setQualityBit(j, (type_bits[j] == 0) ? 0 : 1); + } + + // Save intermediate trackerhits ref + k4EDM4hep2LcioConv::detail::mapInsert(lcio_trh, edm_trh, trackerhits_vec); + + // Add to lcio trackerhits collection + trackerhits->addElement(lcio_trh); + } + } + + return trackerhits; + } + + // Convert EDM4hep SimTrackerHits to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convSimTrackerHits( + const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, + const std::string& cellIDstr, + SimTrHitMapT& simtrackerhits_vec, + const MCParticleMapT& mcparticles_vec) + { + auto* simtrackerhits = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); + + if (cellIDstr != "") { + lcio::CellIDEncoder idEnc(cellIDstr, simtrackerhits); + } + + // Loop over EDM4hep simtrackerhits converting them to LCIO simtrackerhits + for (const auto& edm_strh : (*simtrackerhits_coll)) { + if (edm_strh.isAvailable()) { + auto* lcio_strh = new lcio::SimTrackerHitImpl(); + + uint64_t combined_value = edm_strh.getCellID(); + uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); + lcio_strh->setCellID0(combined_value_ptr[0]); + lcio_strh->setCellID1(combined_value_ptr[1]); + std::array positions { + edm_strh.getPosition()[0], edm_strh.getPosition()[1], edm_strh.getPosition()[2]}; + lcio_strh->setPosition(positions.data()); + lcio_strh->setEDep(edm_strh.getEDep()); + lcio_strh->setTime(edm_strh.getTime()); + lcio_strh->setMomentum(edm_strh.getMomentum()[0], edm_strh.getMomentum()[1], edm_strh.getMomentum()[2]); + lcio_strh->setPathLength(edm_strh.getPathLength()); + lcio_strh->setQuality(edm_strh.getQuality()); + // lcio_strh->setQualityBit( int bit , bool val=true ) ; + lcio_strh->setOverlay(edm_strh.isOverlay()); + lcio_strh->setProducedBySecondary(edm_strh.isProducedBySecondary()); + + // Link converted MCParticle to the SimTrackerHit if found + const auto edm_strh_mcp = edm_strh.getMCParticle(); + if (edm_strh_mcp.isAvailable()) { + if (const auto& lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, mcparticles_vec)) { + lcio_strh->setMCParticle(lcio_mcp.value()); + } + else { + // If MCParticle available, but not found in converted vec, add nullptr + lcio_strh->setMCParticle(nullptr); + } + } + + // Save intermediate simtrackerhits ref + k4EDM4hep2LcioConv::detail::mapInsert(lcio_strh, edm_strh, simtrackerhits_vec); + + // Add to lcio simtrackerhits collection + simtrackerhits->addElement(lcio_strh); + } + } + + return simtrackerhits; + } + + // Convert EDM4hep Calorimeter Hits to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convCalorimeterHits( + const edm4hep::CalorimeterHitCollection* const calohit_coll, + const std::string& cellIDstr, + CaloHitMapT& calo_hits_vec) + { + auto* calohits = new lcio::LCCollectionVec(lcio::LCIO::CALORIMETERHIT); + + if (cellIDstr != "") { + lcio::CellIDEncoder idEnc(cellIDstr, calohits); + } + + for (const auto& edm_calohit : (*calohit_coll)) { + if (edm_calohit.isAvailable()) { + auto* lcio_calohit = new lcio::CalorimeterHitImpl(); + + uint64_t combined_value = edm_calohit.getCellID(); + uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); + lcio_calohit->setCellID0(combined_value_ptr[0]); + lcio_calohit->setCellID1(combined_value_ptr[1]); + lcio_calohit->setEnergy(edm_calohit.getEnergy()); + lcio_calohit->setEnergyError(edm_calohit.getEnergyError()); + lcio_calohit->setTime(edm_calohit.getTime()); + std::array positions { + edm_calohit.getPosition()[0], edm_calohit.getPosition()[1], edm_calohit.getPosition()[2]}; + lcio_calohit->setPosition(positions.data()); + lcio_calohit->setType(edm_calohit.getType()); + + // TODO + // lcio_calohit->setRawHit(EVENT::LCObject* rawHit ); + + // Save Calorimeter Hits LCIO and EDM4hep collections + k4EDM4hep2LcioConv::detail::mapInsert(lcio_calohit, edm_calohit, calo_hits_vec); + + // Add to lcio tracks collection + calohits->addElement(lcio_calohit); + } + } + + return calohits; + } + + // Convert EDM4hep RAW Calorimeter Hits to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convRawCalorimeterHits( + const edm4hep::RawCalorimeterHitCollection* const rawcalohit_coll, + RawCaloHitMapT& raw_calo_hits_vec) + { + auto* rawcalohits = new lcio::LCCollectionVec(lcio::LCIO::RAWCALORIMETERHIT); + + for (const auto& edm_raw_calohit : (*rawcalohit_coll)) { + if (edm_raw_calohit.isAvailable()) { + auto* lcio_rawcalohit = new lcio::RawCalorimeterHitImpl(); + + uint64_t combined_value = edm_raw_calohit.getCellID(); + uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); + lcio_rawcalohit->setCellID0(combined_value_ptr[0]); + lcio_rawcalohit->setCellID1(combined_value_ptr[1]); + lcio_rawcalohit->setAmplitude(edm_raw_calohit.getAmplitude()); + lcio_rawcalohit->setTimeStamp(edm_raw_calohit.getTimeStamp()); + + // Save Raw Calorimeter Hits LCIO and EDM4hep collections + k4EDM4hep2LcioConv::detail::mapInsert(lcio_rawcalohit, edm_raw_calohit, raw_calo_hits_vec); + + // Add to lcio tracks collection + rawcalohits->addElement(lcio_rawcalohit); + } + } + + return rawcalohits; + } + + // Convert EDM4hep Sim Calorimeter Hits to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convSimCalorimeterHits( + const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, + const std::string& cellIDstr, + SimCaloHitMapT& sim_calo_hits_vec, + const MCParticleMapT& mcparticles) + { + auto* simcalohits = new lcio::LCCollectionVec(lcio::LCIO::SIMCALORIMETERHIT); + + if (cellIDstr != "") { + lcio::CellIDEncoder idEnc(cellIDstr, simcalohits); + } + + for (const auto& edm_sim_calohit : (*simcalohit_coll)) { + if (edm_sim_calohit.isAvailable()) { + auto* lcio_simcalohit = new lcio::SimCalorimeterHitImpl(); + + uint64_t combined_value = edm_sim_calohit.getCellID(); + uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); + lcio_simcalohit->setCellID0(combined_value_ptr[0]); + lcio_simcalohit->setCellID1(combined_value_ptr[1]); + lcio_simcalohit->setEnergy(edm_sim_calohit.getEnergy()); + std::array positions { + edm_sim_calohit.getPosition()[0], edm_sim_calohit.getPosition()[1], edm_sim_calohit.getPosition()[2]}; + lcio_simcalohit->setPosition(positions.data()); + + // Contributions are converted in FillMissingCollections to make it a higher probability that we have the + // MCParticles converted + + // Save Sim Calorimeter Hits LCIO and EDM4hep collections + k4EDM4hep2LcioConv::detail::mapInsert(lcio_simcalohit, edm_sim_calohit, sim_calo_hits_vec); + + // Add to sim calo hits collection + simcalohits->addElement(lcio_simcalohit); + } + } + + return simcalohits; + } + + // Convert EDM4hep TPC Hits to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convTPCHits( + const edm4hep::RawTimeSeriesCollection* const tpchit_coll, + TPCHitMapT& tpc_hits_vec) + { + auto* tpchits = new lcio::LCCollectionVec(lcio::LCIO::TPCHIT); + + for (const auto& edm_tpchit : (*tpchit_coll)) { + if (edm_tpchit.isAvailable()) { + auto* lcio_tpchit = new lcio::TPCHitImpl(); + +#warning "unsigned long long conversion to int" + lcio_tpchit->setCellID(edm_tpchit.getCellID()); + lcio_tpchit->setTime(edm_tpchit.getTime()); + lcio_tpchit->setCharge(edm_tpchit.getCharge()); + lcio_tpchit->setQuality(edm_tpchit.getQuality()); + + std::vector rawdata; +#if EDM4HEP_BUILD_VERSION > EDM4HEP_VERSION(0, 7, 2) + for (int i = 0; i < edm_tpchit.adcCounts_size(); ++i) { + rawdata.push_back(edm_tpchit.getAdcCounts(i)); + } + + lcio_tpchit->setRawData(rawdata.data(), edm_tpchit.adcCounts_size()); +#else + for (int i = 0; i < edm_tpchit.rawDataWords_size(); ++i) { + rawdata.push_back(edm_tpchit.getRawDataWords(i)); + } + + lcio_tpchit->setRawData(rawdata.data(), edm_tpchit.rawDataWords_size()); +#endif + + // Save TPC Hits LCIO and EDM4hep collections + k4EDM4hep2LcioConv::detail::mapInsert(lcio_tpchit, edm_tpchit, tpc_hits_vec); + + // Add to lcio tracks collection + tpchits->addElement(lcio_tpchit); + } + } + + return tpchits; + } + + // Convert EDM4hep Clusters to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convClusters( + const edm4hep::ClusterCollection* const cluster_coll, + ClusterMapT& cluster_vec, + const CaloHitMapT& calohits_vec) + { + auto* clusters = new lcio::LCCollectionVec(lcio::LCIO::CLUSTER); + + // Loop over EDM4hep clusters converting them to lcio clusters + for (const auto& edm_cluster : (*cluster_coll)) { + if (edm_cluster.isAvailable()) { + auto* lcio_cluster = new lcio::ClusterImpl(); + + std::bitset type_bits = edm_cluster.getType(); + for (int j = 0; j < sizeof(uint32_t); j++) { + lcio_cluster->setTypeBit(j, (type_bits[j] == 0) ? false : true); + } + lcio_cluster->setEnergy(edm_cluster.getEnergy()); + lcio_cluster->setEnergyError(edm_cluster.getEnergyError()); + + std::array edm_cluster_pos = { + edm_cluster.getPosition().x, edm_cluster.getPosition().y, edm_cluster.getPosition().z}; + lcio_cluster->setPosition(edm_cluster_pos.data()); + + lcio_cluster->setPositionError(edm_cluster.getPositionError().data()); + lcio_cluster->setITheta(edm_cluster.getITheta()); + lcio_cluster->setIPhi(edm_cluster.getPhi()); + std::array edm_cluster_dir_err = { + edm_cluster.getPosition().x, edm_cluster.getPosition().y, edm_cluster.getPosition().z}; + lcio_cluster->setDirectionError(edm_cluster_dir_err.data()); + + EVENT::FloatVec shape_vec {}; + for (auto& param : edm_cluster.getShapeParameters()) { + shape_vec.push_back(param); + } + lcio_cluster->setShape(shape_vec); + + // Convert ParticleIDs associated to the recoparticle + for (const auto& edm_pid : edm_cluster.getParticleIDs()) { + if (edm_pid.isAvailable()) { + auto* lcio_pid = new lcio::ParticleIDImpl; + + lcio_pid->setType(edm_pid.getType()); + lcio_pid->setPDG(edm_pid.getPDG()); + lcio_pid->setLikelihood(edm_pid.getLikelihood()); + lcio_pid->setAlgorithmType(edm_pid.getAlgorithmType()); + for (const auto& param : edm_pid.getParameters()) { + lcio_pid->addParameter(param); + } + + lcio_cluster->addParticleID(lcio_pid); + } + } + + // Add LCIO and EDM4hep pair collections to vec + k4EDM4hep2LcioConv::detail::mapInsert(lcio_cluster, edm_cluster, cluster_vec); + + // Add to lcio tracks collection + clusters->addElement(lcio_cluster); + } + } + + // Link associated clusters after converting all clusters + for (auto& [lcio_cluster, edm_cluster] : cluster_vec) { + for (const auto& edm_linked_cluster : edm_cluster.getClusters()) { + if (edm_linked_cluster.isAvailable()) { + if ( + const auto lcio_cluster_linked = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_cluster, cluster_vec)) { + lcio_cluster->addCluster(lcio_cluster_linked.value()); + } + } + } + } + + return clusters; + } + + // Convert EDM4hep Vertices to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convVertices( + const edm4hep::VertexCollection* const vertex_coll, + VertexMapT& vertex_vec, + const RecoPartMapT& recoparticles_vec) + { + auto* vertices = new lcio::LCCollectionVec(lcio::LCIO::VERTEX); + + // Loop over EDM4hep vertex converting them to lcio vertex + for (const auto& edm_vertex : (*vertex_coll)) { + if (edm_vertex.isAvailable()) { + auto* lcio_vertex = new lcio::VertexImpl(); + lcio_vertex->setPrimary(edm_vertex.getPrimary()); + lcio_vertex->setAlgorithmType(std::to_string(edm_vertex.getAlgorithmType())); + lcio_vertex->setChi2(edm_vertex.getChi2()); + lcio_vertex->setProbability(edm_vertex.getProbability()); + lcio_vertex->setPosition(edm_vertex.getPosition()[0], edm_vertex.getPosition()[1], edm_vertex.getPosition()[2]); + lcio_vertex->setCovMatrix(edm_vertex.getCovMatrix().data()); + + for (auto& param : edm_vertex.getParameters()) { + lcio_vertex->addParameter(param); + } + + // Link sinlge associated Particle if found in converted ones + edm4hep::ReconstructedParticle vertex_rp = edm_vertex.getAssociatedParticle(); + if (vertex_rp.isAvailable()) { + if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(vertex_rp, recoparticles_vec)) { + lcio_vertex->setAssociatedParticle(lcio_rp.value()); + } + else { + lcio_vertex->setAssociatedParticle(nullptr); + } + } + + // Add LCIO and EDM4hep pair collections to vec + k4EDM4hep2LcioConv::detail::mapInsert(lcio_vertex, edm_vertex, vertex_vec); + + // Add to lcio tracks collection + vertices->addElement(lcio_vertex); + } + } + + return vertices; + } + + // Convert EDM4hep RecoParticles to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convReconstructedParticles( + const edm4hep::ReconstructedParticleCollection* const recos_coll, + RecoPartMapT& recoparticles_vec, + const TrackMapT& tracks_vec, + const VertexMapT& vertex_vec, + const ClusterMapT& clusters_vec) + { + auto* recops = new lcio::LCCollectionVec(lcio::LCIO::RECONSTRUCTEDPARTICLE); + + for (const auto& edm_rp : (*recos_coll)) { + auto* lcio_recp = new lcio::ReconstructedParticleImpl; + if (edm_rp.isAvailable()) { + lcio_recp->setType(edm_rp.getType()); + float m[3] = {edm_rp.getMomentum()[0], edm_rp.getMomentum()[1], edm_rp.getMomentum()[2]}; + lcio_recp->setMomentum(m); + lcio_recp->setEnergy(edm_rp.getEnergy()); + lcio_recp->setCovMatrix(edm_rp.getCovMatrix().data()); // TODO Check lower or upper + lcio_recp->setMass(edm_rp.getMass()); + lcio_recp->setCharge(edm_rp.getCharge()); + float rp[3] = {edm_rp.getReferencePoint()[0], edm_rp.getReferencePoint()[1], edm_rp.getReferencePoint()[2]}; + lcio_recp->setReferencePoint(rp); + lcio_recp->setGoodnessOfPID(edm_rp.getGoodnessOfPID()); + + // Convert ParticleIDs associated to the recoparticle + for (const auto& edm_pid : edm_rp.getParticleIDs()) { + if (edm_pid.isAvailable()) { + auto* lcio_pid = new lcio::ParticleIDImpl; + + lcio_pid->setType(edm_pid.getType()); + lcio_pid->setPDG(edm_pid.getPDG()); + lcio_pid->setLikelihood(edm_pid.getLikelihood()); + lcio_pid->setAlgorithmType(edm_pid.getAlgorithmType()); + for (const auto& param : edm_pid.getParameters()) { + lcio_pid->addParameter(param); + } + + lcio_recp->addParticleID(lcio_pid); + } + } + + // Link sinlge associated Particle + auto edm_pid_used = edm_rp.getParticleIDUsed(); + if (edm_pid_used.isAvailable()) { + for (const auto& lcio_pid : lcio_recp->getParticleIDs()) { + bool is_same = true; + is_same = is_same && (lcio_pid->getType() == edm_pid_used.getType()); + is_same = is_same && (lcio_pid->getPDG() == edm_pid_used.getPDG()); + is_same = is_same && (lcio_pid->getLikelihood() == edm_pid_used.getLikelihood()); + is_same = is_same && (lcio_pid->getAlgorithmType() == edm_pid_used.getAlgorithmType()); + for (int i = 0; i < edm_pid_used.parameters_size(); ++i) { + is_same = is_same && (edm_pid_used.getParameters(i) == lcio_pid->getParameters()[i]); + } + if (is_same) { + lcio_recp->setParticleIDUsed(lcio_pid); + break; + } + } + } + + // Link sinlge associated Vertex if found in converted ones + auto vertex = edm_rp.getStartVertex(); + if (vertex.isAvailable()) { + if (const auto lcio_vertex = k4EDM4hep2LcioConv::detail::mapLookupFrom(vertex, vertex_vec)) { + lcio_recp->setStartVertex(lcio_vertex.value()); + } + else { + lcio_recp->setStartVertex(nullptr); + } + } + + // Link multiple associated Tracks if found in converted ones + for (const auto& edm_rp_tr : edm_rp.getTracks()) { + if (edm_rp_tr.isAvailable()) { + if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, tracks_vec)) { + lcio_recp->addTrack(lcio_tr.value()); + } + else { + lcio_recp->addTrack(nullptr); + } + } + } + + // Link multiple associated Clusters if found in converted ones + for (const auto& edm_rp_cluster : edm_rp.getClusters()) { + if (edm_rp_cluster.isAvailable()) { + if (const auto lcio_cluster = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, clusters_vec)) { + lcio_recp->addCluster(lcio_cluster.value()); + } + else { + lcio_recp->addCluster(nullptr); + } + } + } + + // Add LCIO and EDM4hep pair collections to vec + k4EDM4hep2LcioConv::detail::mapInsert(lcio_recp, edm_rp, recoparticles_vec); + + // Add to reconstructed particles collection + recops->addElement(lcio_recp); + } + } + + // Link associated recopartilces after converting all recoparticles + for (auto& [lcio_rp, edm_rp] : recoparticles_vec) { + for (const auto& edm_linked_rp : edm_rp.getParticles()) { + if (edm_linked_rp.isAvailable()) { + // Search the linked track in the converted vector + if (const auto lcio_rp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_rp, recoparticles_vec)) { + lcio_rp->addParticle(lcio_rp_linked.value()); + } + } + } + } + + return recops; + } + + // Convert MC Particles to LCIO + // Add converted LCIO ptr and original EDM4hep collection to vector of pairs + // Add converted LCIO Collection Vector to LCIO event + template + lcio::LCCollectionVec* convMCParticles( + const edm4hep::MCParticleCollection* const mcparticle_coll, + MCPartMapT& mc_particles_vec) + { + auto* mcparticles = new lcio::LCCollectionVec(lcio::LCIO::MCPARTICLE); + + for (const auto& edm_mcp : (*mcparticle_coll)) { + auto* lcio_mcp = new lcio::MCParticleImpl; + if (edm_mcp.isAvailable()) { + lcio_mcp->setPDG(edm_mcp.getPDG()); + lcio_mcp->setGeneratorStatus(edm_mcp.getGeneratorStatus()); + // Note LCIO sets some Bits during writing which makes a trivial integer conversion afterwards not work + int status = edm_mcp.getSimulatorStatus(); + lcio_mcp->setSimulatorStatus(status); + + double vertex[3] = {edm_mcp.getVertex()[0], edm_mcp.getVertex()[1], edm_mcp.getVertex()[2]}; + lcio_mcp->setVertex(vertex); + lcio_mcp->setTime(edm_mcp.getTime()); + double endpoint[3] = {edm_mcp.getEndpoint()[0], edm_mcp.getEndpoint()[1], edm_mcp.getEndpoint()[2]}; + lcio_mcp->setEndpoint(endpoint); + double momentum[3] = {edm_mcp.getMomentum()[0], edm_mcp.getMomentum()[1], edm_mcp.getMomentum()[2]}; + lcio_mcp->setMomentum(momentum); + float momentumEndpoint[3] = { + edm_mcp.getMomentumAtEndpoint()[0], edm_mcp.getMomentumAtEndpoint()[1], edm_mcp.getMomentumAtEndpoint()[2]}; + lcio_mcp->setMomentumAtEndpoint(momentumEndpoint); +#warning "double to float" + lcio_mcp->setMass(edm_mcp.getMass()); + lcio_mcp->setCharge(edm_mcp.getCharge()); + float spin[3] = {edm_mcp.getSpin()[0], edm_mcp.getSpin()[1], edm_mcp.getSpin()[2]}; + lcio_mcp->setSpin(spin); + int colorflow[2] = {edm_mcp.getColorFlow()[0], edm_mcp.getColorFlow()[1]}; + lcio_mcp->setColorFlow(colorflow); + + lcio_mcp->setCreatedInSimulation(edm_mcp.isCreatedInSimulation()); + lcio_mcp->setBackscatter(edm_mcp.isBackscatter()); + lcio_mcp->setVertexIsNotEndpointOfParent(edm_mcp.vertexIsNotEndpointOfParent()); + lcio_mcp->setDecayedInTracker(edm_mcp.isDecayedInTracker()); + lcio_mcp->setDecayedInCalorimeter(edm_mcp.isDecayedInCalorimeter()); + lcio_mcp->setHasLeftDetector(edm_mcp.hasLeftDetector()); + lcio_mcp->setStopped(edm_mcp.isStopped()); + lcio_mcp->setOverlay(edm_mcp.isOverlay()); + + // Add LCIO and EDM4hep pair collections to vec + k4EDM4hep2LcioConv::detail::mapInsert(lcio_mcp, edm_mcp, mc_particles_vec); + + // Add to reconstructed particles collection + mcparticles->addElement(lcio_mcp); + } + } + + // Add parent MCParticles after converting all MCparticles + for (auto& [lcio_mcp, edm_mcp] : mc_particles_vec) { + for (const auto& emd_parent_mcp : edm_mcp.getParents()) { + if (emd_parent_mcp.isAvailable()) { + // Search for the parent mcparticle in the converted vector + if ( + const auto lcio_mcp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(emd_parent_mcp, mc_particles_vec)) { + lcio_mcp->addParent(lcio_mcp_linked.value()); + } + } + } + } + + return mcparticles; + } + + // Depending on the order of the collections in the parameters, + // and for the mutual dependencies between some collections, + // go over the possible missing associated collections and fill them. + template + void FillMissingCollections(ObjectMappingT& collection_pairs) + { + // Fill missing Tracks collections + for (auto& [lcio_tr, edm_tr] : collection_pairs.tracks) { + if (lcio_tr->getTrackerHits().size() == 0) { + for (const auto& edm_tr_trh : edm_tr.getTrackerHits()) { + if ( + const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, collection_pairs.trackerhits)) { + lcio_tr->addHit(lcio_trh.value()); + } + } + } + } + + // Fill missing ReconstructedParticle collections + for (auto& [lcio_rp, edm_rp] : collection_pairs.recoparticles) { + // Link Vertex + if (lcio_rp->getStartVertex() == nullptr) { + if (edm_rp.getStartVertex().isAvailable()) { + if ( + const auto lcio_vertex = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp.getStartVertex(), collection_pairs.vertices)) { + lcio_rp->setStartVertex(lcio_vertex.value()); + } + } + } + + // Link Tracks + if (lcio_rp->getTracks().size() != edm_rp.tracks_size()) { + assert(lcio_rp->getTracks().size() == 0); + for (const auto& edm_rp_tr : edm_rp.getTracks()) { + if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, collection_pairs.tracks)) { + lcio_rp->addTrack(lcio_tr.value()); + } + } + } + + // Link Clusters + if (lcio_rp->getClusters().size() != edm_rp.clusters_size()) { + assert(lcio_rp->getClusters().size() == 0); + for (const auto& edm_rp_cluster : edm_rp.getClusters()) { + if ( + const auto lcio_cluster = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, collection_pairs.clusters)) { + lcio_rp->addCluster(lcio_cluster.value()); + } + } + } + + } // reconstructed particles + + // Fill missing Vertices collections + for (auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { + // Link Reconstructed Particles + if (lcio_vertex->getAssociatedParticle() == nullptr) { + const auto edm_rp = edm_vertex.getAssociatedParticle(); + if (edm_rp.isAvailable()) { + if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, collection_pairs.recoparticles)) { + lcio_vertex->setAssociatedParticle(lcio_rp.value()); + } + } + } + + } // vertices + + // Fill SimCaloHit collections with contributions + // + // We loop over all pairs of lcio and edm4hep simcalo hits and add the contributions, by now MCParticle + // collection(s) should be converted! + for (auto& [lcio_sch, edm_sch] : collection_pairs.simcalohits) { + // add associated Contributions (MCParticles) + for (int i = 0; i < edm_sch.contributions_size(); ++i) { + const auto& contrib = edm_sch.getContributions(i); + if (not contrib.isAvailable()) { + // We need a logging library independent of Gaudi for this! + // std::cout << "WARNING: CaloHit Contribution is not available!" << std::endl; + continue; + } + auto edm_contrib_mcp = contrib.getParticle(); + std::array step_position { + contrib.getStepPosition()[0], contrib.getStepPosition()[1], contrib.getStepPosition()[2]}; + bool mcp_found = false; + EVENT::MCParticle* lcio_mcp = nullptr; + if (edm_contrib_mcp.isAvailable()) { + // if we have the MCParticle we look for its partner + lcio_mcp = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcparticles).value_or(nullptr); + } + else { // edm mcp available + // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; + } + // we add contribution with whatever lcio mc particle we found + lcio_sch->addMCParticleContribution( + lcio_mcp, contrib.getEnergy(), contrib.getTime(), contrib.getPDG(), step_position.data()); + // if (!lcio_mcp) { + // std::cout << "WARNING: No MCParticle found for this contribution." + // << "Make Sure MCParticles are converted! " + // << edm_contrib_mcp.id() + // << std::endl; + // } + } // all emd4hep contributions + // We need to reset the energy to the original one, because adding + // contributions alters the energy in LCIO + lcio_sch->setEnergy(edm_sch.getEnergy()); + } // SimCaloHit + + // Fill missing SimTrackerHit collections + for (auto& [lcio_strh, edm_strh] : collection_pairs.simtrackerhits) { + const auto lcio_strh_mcp = lcio_strh->getMCParticle(); + if (lcio_strh_mcp == nullptr) { + const auto edm_strh_mcp = edm_strh.getMCParticle(); + if ( + const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, collection_pairs.mcparticles)) { + lcio_strh->setMCParticle(lcio_mcp.value()); + } + } + + } // SimTrackerHits + } + +} // namespace EDM4hep2LCIOConv diff --git a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp index 343ad404..9da6717c 100644 --- a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp @@ -1,731 +1,8 @@ #include "k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h" #include "EVENT/MCParticle.h" -#include "k4EDM4hep2LcioConv/MappingUtils.h" - -#include "IMPL/LCEventImpl.h" - -#if __has_include("edm4hep/EDM4hepVersion.h") -#include "edm4hep/EDM4hepVersion.h" -#else -// Copy the necessary parts from the header above to make whatever we need to work here -#define EDM4HEP_VERSION(major, minor, patch) ((UINT64_C(major) << 32) | (UINT64_C(minor) << 16) | (UINT64_C(patch))) -// v00-07-02 is the last version without that still has TPCHits -#if __has_include("edm4hep/TPCHitCollection.h") -#define EDM4HEP_BUILD_VERSION EDM4HEP_VERSION(0, 7, 2) -#else -// v00-09 is the last version without the capitalization change of the track vector members -#define EDM4HEP_BUILD_VERSION EDM4HEP_VERSION(0, 9, 0) -#endif -#endif - -#include namespace EDM4hep2LCIOConv { - // Convert EDM4hep Tracks to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convTracks( - const edm4hep::TrackCollection* const tracks_coll, - TrackMapT& tracks_vec, - const TrackerHitMapT& trackerhits_vec) - { - auto* tracks = new lcio::LCCollectionVec(lcio::LCIO::TRACK); - - // Loop over EDM4hep tracks converting them to lcio tracks. - for (const auto& edm_tr : (*tracks_coll)) { - if (edm_tr.isAvailable()) { - auto* lcio_tr = new lcio::TrackImpl(); - // The Type of the Tracks need to be set bitwise in LCIO since the setType(int) function is private for the LCIO - // TrackImpl and only a setTypeBit(bitnumber) function can be used to set the Type bit by bit. - int type = edm_tr.getType(); - for (int i = 0; i < sizeof(int) * 8; i++) { - lcio_tr->setTypeBit(i, type & (1 << i)); - } - lcio_tr->setChi2(edm_tr.getChi2()); - lcio_tr->setNdf(edm_tr.getNdf()); - lcio_tr->setdEdx(edm_tr.getDEdx()); - lcio_tr->setdEdxError(edm_tr.getDEdxError()); - lcio_tr->setRadiusOfInnermostHit(edm_tr.getRadiusOfInnermostHit()); - -#if EDM4HEP_BUILD_VERSION > EDM4HEP_VERSION(0, 9, 0) - // Loop over the hit Numbers in the track - lcio_tr->subdetectorHitNumbers().resize(edm_tr.subdetectorHitNumbers_size()); - for (int i = 0; i < edm_tr.subdetectorHitNumbers_size(); ++i) { - lcio_tr->subdetectorHitNumbers()[i] = edm_tr.getSubdetectorHitNumbers(i); - } - - // Pad until 50 hitnumbers are resized - const int hit_number_limit = 50; - if (edm_tr.subdetectorHitNumbers_size() < hit_number_limit) { - lcio_tr->subdetectorHitNumbers().resize(hit_number_limit); - for (int i = edm_tr.subdetectorHitNumbers_size(); i < hit_number_limit; ++i) { - lcio_tr->subdetectorHitNumbers()[i] = 0; - } - } -#else - // Loop over the hit Numbers in the track - lcio_tr->subdetectorHitNumbers().resize(edm_tr.subDetectorHitNumbers_size()); - for (int i = 0; i < edm_tr.subDetectorHitNumbers_size(); ++i) { - lcio_tr->subdetectorHitNumbers()[i] = edm_tr.getSubDetectorHitNumbers(i); - } - - // Pad until 50 hitnumbers are resized - const int hit_number_limit = 50; - if (edm_tr.subDetectorHitNumbers_size() < hit_number_limit) { - lcio_tr->subdetectorHitNumbers().resize(hit_number_limit); - for (int i = edm_tr.subDetectorHitNumbers_size(); i < hit_number_limit; ++i) { - lcio_tr->subdetectorHitNumbers()[i] = 0; - } - } -#endif - - // Link multiple associated TrackerHits if found in converted ones - for (const auto& edm_rp_trh : edm_tr.getTrackerHits()) { - if (edm_rp_trh.isAvailable()) { - if (const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_trh, trackerhits_vec)) { - lcio_tr->addHit(lcio_trh.value()); - } - } - } - - // Loop over the track states in the track - const podio::RelationRange edm_track_states = edm_tr.getTrackStates(); - for (const auto& tr_state : edm_track_states) { - const auto& cov = tr_state.covMatrix; - std::array refP = {tr_state.referencePoint.x, tr_state.referencePoint.y, tr_state.referencePoint.z}; - - auto* lcio_tr_state = new lcio::TrackStateImpl( - tr_state.location, - tr_state.D0, - tr_state.phi, - tr_state.omega, - tr_state.Z0, - tr_state.tanLambda, - cov.data(), - refP.data()); - - lcio_tr->addTrackState(lcio_tr_state); - } - - // Save intermediate tracks ref - k4EDM4hep2LcioConv::detail::mapInsert(lcio_tr, edm_tr, tracks_vec); - - // Add to lcio tracks collection - tracks->addElement(lcio_tr); - } - } - - // Link associated tracks after converting all tracks - for (auto& [lcio_tr, edm_tr] : tracks_vec) { - for (const auto& edm_linked_tr : edm_tr.getTracks()) { - if (edm_linked_tr.isAvailable()) { - // Search the linked track in the converted vector - if (const auto lcio_tr_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_tr, tracks_vec)) - lcio_tr->addTrack(lcio_tr_linked.value()); - } - } - } - - return tracks; - } - - // Convert EDM4hep TrackerHits to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convTrackerHits( - const edm4hep::TrackerHitCollection* const trackerhits_coll, - const std::string& cellIDstr, - TrackerHitMapT& trackerhits_vec) - { - auto* trackerhits = new lcio::LCCollectionVec(lcio::LCIO::TRACKERHIT); - - if (cellIDstr != "") { - lcio::CellIDEncoder idEnc(cellIDstr, trackerhits); - } - - // Loop over EDM4hep trackerhits converting them to lcio trackerhits - for (const auto& edm_trh : (*trackerhits_coll)) { - if (edm_trh.isAvailable()) { - auto* lcio_trh = new lcio::TrackerHitImpl(); - - uint64_t combined_value = edm_trh.getCellID(); - uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); - lcio_trh->setCellID0(combined_value_ptr[0]); - lcio_trh->setCellID1(combined_value_ptr[1]); - lcio_trh->setType(edm_trh.getType()); - std::array positions {edm_trh.getPosition()[0], edm_trh.getPosition()[1], edm_trh.getPosition()[2]}; - lcio_trh->setPosition(positions.data()); - lcio_trh->setCovMatrix(edm_trh.getCovMatrix().data()); - lcio_trh->setEDep(edm_trh.getEDep()); - lcio_trh->setEDepError(edm_trh.getEDepError()); - lcio_trh->setTime(edm_trh.getTime()); - lcio_trh->setQuality(edm_trh.getQuality()); - std::bitset type_bits = edm_trh.getQuality(); - for (int j = 0; j < sizeof(uint32_t); j++) { - lcio_trh->setQualityBit(j, (type_bits[j] == 0) ? 0 : 1); - } - - // Save intermediate trackerhits ref - k4EDM4hep2LcioConv::detail::mapInsert(lcio_trh, edm_trh, trackerhits_vec); - - // Add to lcio trackerhits collection - trackerhits->addElement(lcio_trh); - } - } - - return trackerhits; - } - - // Convert EDM4hep SimTrackerHits to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convSimTrackerHits( - const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, - const std::string& cellIDstr, - SimTrHitMapT& simtrackerhits_vec, - const MCParticleMapT& mcparticles_vec) - { - auto* simtrackerhits = new lcio::LCCollectionVec(lcio::LCIO::SIMTRACKERHIT); - - if (cellIDstr != "") { - lcio::CellIDEncoder idEnc(cellIDstr, simtrackerhits); - } - - // Loop over EDM4hep simtrackerhits converting them to LCIO simtrackerhits - for (const auto& edm_strh : (*simtrackerhits_coll)) { - if (edm_strh.isAvailable()) { - auto* lcio_strh = new lcio::SimTrackerHitImpl(); - - uint64_t combined_value = edm_strh.getCellID(); - uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); - lcio_strh->setCellID0(combined_value_ptr[0]); - lcio_strh->setCellID1(combined_value_ptr[1]); - std::array positions { - edm_strh.getPosition()[0], edm_strh.getPosition()[1], edm_strh.getPosition()[2]}; - lcio_strh->setPosition(positions.data()); - lcio_strh->setEDep(edm_strh.getEDep()); - lcio_strh->setTime(edm_strh.getTime()); - lcio_strh->setMomentum(edm_strh.getMomentum()[0], edm_strh.getMomentum()[1], edm_strh.getMomentum()[2]); - lcio_strh->setPathLength(edm_strh.getPathLength()); - lcio_strh->setQuality(edm_strh.getQuality()); - // lcio_strh->setQualityBit( int bit , bool val=true ) ; - lcio_strh->setOverlay(edm_strh.isOverlay()); - lcio_strh->setProducedBySecondary(edm_strh.isProducedBySecondary()); - - // Link converted MCParticle to the SimTrackerHit if found - const auto edm_strh_mcp = edm_strh.getMCParticle(); - if (edm_strh_mcp.isAvailable()) { - if (const auto& lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, mcparticles_vec)) { - lcio_strh->setMCParticle(lcio_mcp.value()); - } - else { - // If MCParticle available, but not found in converted vec, add nullptr - lcio_strh->setMCParticle(nullptr); - } - } - - // Save intermediate simtrackerhits ref - k4EDM4hep2LcioConv::detail::mapInsert(lcio_strh, edm_strh, simtrackerhits_vec); - - // Add to lcio simtrackerhits collection - simtrackerhits->addElement(lcio_strh); - } - } - - return simtrackerhits; - } - - // Convert EDM4hep Calorimeter Hits to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - - template - lcio::LCCollectionVec* convCalorimeterHits( - const edm4hep::CalorimeterHitCollection* const calohit_coll, - const std::string& cellIDstr, - CaloHitMapT& calo_hits_vec) - { - auto* calohits = new lcio::LCCollectionVec(lcio::LCIO::CALORIMETERHIT); - - if (cellIDstr != "") { - lcio::CellIDEncoder idEnc(cellIDstr, calohits); - } - - for (const auto& edm_calohit : (*calohit_coll)) { - if (edm_calohit.isAvailable()) { - auto* lcio_calohit = new lcio::CalorimeterHitImpl(); - - uint64_t combined_value = edm_calohit.getCellID(); - uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); - lcio_calohit->setCellID0(combined_value_ptr[0]); - lcio_calohit->setCellID1(combined_value_ptr[1]); - lcio_calohit->setEnergy(edm_calohit.getEnergy()); - lcio_calohit->setEnergyError(edm_calohit.getEnergyError()); - lcio_calohit->setTime(edm_calohit.getTime()); - std::array positions { - edm_calohit.getPosition()[0], edm_calohit.getPosition()[1], edm_calohit.getPosition()[2]}; - lcio_calohit->setPosition(positions.data()); - lcio_calohit->setType(edm_calohit.getType()); - - // TODO - // lcio_calohit->setRawHit(EVENT::LCObject* rawHit ); - - // Save Calorimeter Hits LCIO and EDM4hep collections - k4EDM4hep2LcioConv::detail::mapInsert(lcio_calohit, edm_calohit, calo_hits_vec); - - // Add to lcio tracks collection - calohits->addElement(lcio_calohit); - } - } - - return calohits; - } - - // Convert EDM4hep RAW Calorimeter Hits to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convRawCalorimeterHits( - const edm4hep::RawCalorimeterHitCollection* const rawcalohit_coll, - RawCaloHitMapT& raw_calo_hits_vec) - { - auto* rawcalohits = new lcio::LCCollectionVec(lcio::LCIO::RAWCALORIMETERHIT); - - for (const auto& edm_raw_calohit : (*rawcalohit_coll)) { - if (edm_raw_calohit.isAvailable()) { - auto* lcio_rawcalohit = new lcio::RawCalorimeterHitImpl(); - - uint64_t combined_value = edm_raw_calohit.getCellID(); - uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); - lcio_rawcalohit->setCellID0(combined_value_ptr[0]); - lcio_rawcalohit->setCellID1(combined_value_ptr[1]); - lcio_rawcalohit->setAmplitude(edm_raw_calohit.getAmplitude()); - lcio_rawcalohit->setTimeStamp(edm_raw_calohit.getTimeStamp()); - - // Save Raw Calorimeter Hits LCIO and EDM4hep collections - k4EDM4hep2LcioConv::detail::mapInsert(lcio_rawcalohit, edm_raw_calohit, raw_calo_hits_vec); - - // Add to lcio tracks collection - rawcalohits->addElement(lcio_rawcalohit); - } - } - - return rawcalohits; - } - - // Convert EDM4hep Sim Calorimeter Hits to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convSimCalorimeterHits( - const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, - const std::string& cellIDstr, - SimCaloHitMapT& sim_calo_hits_vec, - const MCParticleMapT& mcparticles) - { - auto* simcalohits = new lcio::LCCollectionVec(lcio::LCIO::SIMCALORIMETERHIT); - - if (cellIDstr != "") { - lcio::CellIDEncoder idEnc(cellIDstr, simcalohits); - } - - for (const auto& edm_sim_calohit : (*simcalohit_coll)) { - if (edm_sim_calohit.isAvailable()) { - auto* lcio_simcalohit = new lcio::SimCalorimeterHitImpl(); - - uint64_t combined_value = edm_sim_calohit.getCellID(); - uint32_t* combined_value_ptr = reinterpret_cast(&combined_value); - lcio_simcalohit->setCellID0(combined_value_ptr[0]); - lcio_simcalohit->setCellID1(combined_value_ptr[1]); - lcio_simcalohit->setEnergy(edm_sim_calohit.getEnergy()); - std::array positions { - edm_sim_calohit.getPosition()[0], edm_sim_calohit.getPosition()[1], edm_sim_calohit.getPosition()[2]}; - lcio_simcalohit->setPosition(positions.data()); - - // Contributions are converted in FillMissingCollections to make it a higher probability that we have the - // MCParticles converted - - // Save Sim Calorimeter Hits LCIO and EDM4hep collections - k4EDM4hep2LcioConv::detail::mapInsert(lcio_simcalohit, edm_sim_calohit, sim_calo_hits_vec); - - // Add to sim calo hits collection - simcalohits->addElement(lcio_simcalohit); - } - } - - return simcalohits; - } - - // Convert EDM4hep TPC Hits to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convTPCHits( - const edm4hep::RawTimeSeriesCollection* const tpchit_coll, - TPCHitMapT& tpc_hits_vec) - { - auto* tpchits = new lcio::LCCollectionVec(lcio::LCIO::TPCHIT); - - for (const auto& edm_tpchit : (*tpchit_coll)) { - if (edm_tpchit.isAvailable()) { - auto* lcio_tpchit = new lcio::TPCHitImpl(); - -#warning "unsigned long long conversion to int" - lcio_tpchit->setCellID(edm_tpchit.getCellID()); - lcio_tpchit->setTime(edm_tpchit.getTime()); - lcio_tpchit->setCharge(edm_tpchit.getCharge()); - lcio_tpchit->setQuality(edm_tpchit.getQuality()); - - std::vector rawdata; -#if EDM4HEP_BUILD_VERSION > EDM4HEP_VERSION(0, 7, 2) - for (int i = 0; i < edm_tpchit.adcCounts_size(); ++i) { - rawdata.push_back(edm_tpchit.getAdcCounts(i)); - } - - lcio_tpchit->setRawData(rawdata.data(), edm_tpchit.adcCounts_size()); -#else - for (int i = 0; i < edm_tpchit.rawDataWords_size(); ++i) { - rawdata.push_back(edm_tpchit.getRawDataWords(i)); - } - - lcio_tpchit->setRawData(rawdata.data(), edm_tpchit.rawDataWords_size()); -#endif - - // Save TPC Hits LCIO and EDM4hep collections - k4EDM4hep2LcioConv::detail::mapInsert(lcio_tpchit, edm_tpchit, tpc_hits_vec); - - // Add to lcio tracks collection - tpchits->addElement(lcio_tpchit); - } - } - - return tpchits; - } - - // Convert EDM4hep Clusters to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convClusters( - const edm4hep::ClusterCollection* const cluster_coll, - ClusterMapT& cluster_vec, - const CaloHitMapT& calohits_vec) - { - auto* clusters = new lcio::LCCollectionVec(lcio::LCIO::CLUSTER); - - // Loop over EDM4hep clusters converting them to lcio clusters - for (const auto& edm_cluster : (*cluster_coll)) { - if (edm_cluster.isAvailable()) { - auto* lcio_cluster = new lcio::ClusterImpl(); - - std::bitset type_bits = edm_cluster.getType(); - for (int j = 0; j < sizeof(uint32_t); j++) { - lcio_cluster->setTypeBit(j, (type_bits[j] == 0) ? false : true); - } - lcio_cluster->setEnergy(edm_cluster.getEnergy()); - lcio_cluster->setEnergyError(edm_cluster.getEnergyError()); - - std::array edm_cluster_pos = { - edm_cluster.getPosition().x, edm_cluster.getPosition().y, edm_cluster.getPosition().z}; - lcio_cluster->setPosition(edm_cluster_pos.data()); - - lcio_cluster->setPositionError(edm_cluster.getPositionError().data()); - lcio_cluster->setITheta(edm_cluster.getITheta()); - lcio_cluster->setIPhi(edm_cluster.getPhi()); - std::array edm_cluster_dir_err = { - edm_cluster.getPosition().x, edm_cluster.getPosition().y, edm_cluster.getPosition().z}; - lcio_cluster->setDirectionError(edm_cluster_dir_err.data()); - - EVENT::FloatVec shape_vec {}; - for (auto& param : edm_cluster.getShapeParameters()) { - shape_vec.push_back(param); - } - lcio_cluster->setShape(shape_vec); - - // Convert ParticleIDs associated to the recoparticle - for (const auto& edm_pid : edm_cluster.getParticleIDs()) { - if (edm_pid.isAvailable()) { - auto* lcio_pid = new lcio::ParticleIDImpl; - - lcio_pid->setType(edm_pid.getType()); - lcio_pid->setPDG(edm_pid.getPDG()); - lcio_pid->setLikelihood(edm_pid.getLikelihood()); - lcio_pid->setAlgorithmType(edm_pid.getAlgorithmType()); - for (const auto& param : edm_pid.getParameters()) { - lcio_pid->addParameter(param); - } - - lcio_cluster->addParticleID(lcio_pid); - } - } - - // Add LCIO and EDM4hep pair collections to vec - k4EDM4hep2LcioConv::detail::mapInsert(lcio_cluster, edm_cluster, cluster_vec); - - // Add to lcio tracks collection - clusters->addElement(lcio_cluster); - } - } - - // Link associated clusters after converting all clusters - for (auto& [lcio_cluster, edm_cluster] : cluster_vec) { - for (const auto& edm_linked_cluster : edm_cluster.getClusters()) { - if (edm_linked_cluster.isAvailable()) { - if ( - const auto lcio_cluster_linked = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_cluster, cluster_vec)) { - lcio_cluster->addCluster(lcio_cluster_linked.value()); - } - } - } - } - - return clusters; - } - - // Convert EDM4hep Vertices to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convVertices( - const edm4hep::VertexCollection* const vertex_coll, - VertexMapT& vertex_vec, - const RecoPartMapT& recoparticles_vec) - { - auto* vertices = new lcio::LCCollectionVec(lcio::LCIO::VERTEX); - - // Loop over EDM4hep vertex converting them to lcio vertex - for (const auto& edm_vertex : (*vertex_coll)) { - if (edm_vertex.isAvailable()) { - auto* lcio_vertex = new lcio::VertexImpl(); - lcio_vertex->setPrimary(edm_vertex.getPrimary()); - lcio_vertex->setAlgorithmType(std::to_string(edm_vertex.getAlgorithmType())); - lcio_vertex->setChi2(edm_vertex.getChi2()); - lcio_vertex->setProbability(edm_vertex.getProbability()); - lcio_vertex->setPosition(edm_vertex.getPosition()[0], edm_vertex.getPosition()[1], edm_vertex.getPosition()[2]); - lcio_vertex->setCovMatrix(edm_vertex.getCovMatrix().data()); - - for (auto& param : edm_vertex.getParameters()) { - lcio_vertex->addParameter(param); - } - - // Link sinlge associated Particle if found in converted ones - edm4hep::ReconstructedParticle vertex_rp = edm_vertex.getAssociatedParticle(); - if (vertex_rp.isAvailable()) { - if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(vertex_rp, recoparticles_vec)) { - lcio_vertex->setAssociatedParticle(lcio_rp.value()); - } - else { - lcio_vertex->setAssociatedParticle(nullptr); - } - } - - // Add LCIO and EDM4hep pair collections to vec - k4EDM4hep2LcioConv::detail::mapInsert(lcio_vertex, edm_vertex, vertex_vec); - - // Add to lcio tracks collection - vertices->addElement(lcio_vertex); - } - } - - return vertices; - } - - // Convert EDM4hep RecoParticles to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convReconstructedParticles( - const edm4hep::ReconstructedParticleCollection* const recos_coll, - RecoPartMapT& recoparticles_vec, - const TrackMapT& tracks_vec, - const VertexMapT& vertex_vec, - const ClusterMapT& clusters_vec) - { - auto* recops = new lcio::LCCollectionVec(lcio::LCIO::RECONSTRUCTEDPARTICLE); - - for (const auto& edm_rp : (*recos_coll)) { - auto* lcio_recp = new lcio::ReconstructedParticleImpl; - if (edm_rp.isAvailable()) { - lcio_recp->setType(edm_rp.getType()); - float m[3] = {edm_rp.getMomentum()[0], edm_rp.getMomentum()[1], edm_rp.getMomentum()[2]}; - lcio_recp->setMomentum(m); - lcio_recp->setEnergy(edm_rp.getEnergy()); - lcio_recp->setCovMatrix(edm_rp.getCovMatrix().data()); // TODO Check lower or upper - lcio_recp->setMass(edm_rp.getMass()); - lcio_recp->setCharge(edm_rp.getCharge()); - float rp[3] = {edm_rp.getReferencePoint()[0], edm_rp.getReferencePoint()[1], edm_rp.getReferencePoint()[2]}; - lcio_recp->setReferencePoint(rp); - lcio_recp->setGoodnessOfPID(edm_rp.getGoodnessOfPID()); - - // Convert ParticleIDs associated to the recoparticle - for (const auto& edm_pid : edm_rp.getParticleIDs()) { - if (edm_pid.isAvailable()) { - auto* lcio_pid = new lcio::ParticleIDImpl; - - lcio_pid->setType(edm_pid.getType()); - lcio_pid->setPDG(edm_pid.getPDG()); - lcio_pid->setLikelihood(edm_pid.getLikelihood()); - lcio_pid->setAlgorithmType(edm_pid.getAlgorithmType()); - for (const auto& param : edm_pid.getParameters()) { - lcio_pid->addParameter(param); - } - - lcio_recp->addParticleID(lcio_pid); - } - } - - // Link sinlge associated Particle - auto edm_pid_used = edm_rp.getParticleIDUsed(); - if (edm_pid_used.isAvailable()) { - for (const auto& lcio_pid : lcio_recp->getParticleIDs()) { - bool is_same = true; - is_same = is_same && (lcio_pid->getType() == edm_pid_used.getType()); - is_same = is_same && (lcio_pid->getPDG() == edm_pid_used.getPDG()); - is_same = is_same && (lcio_pid->getLikelihood() == edm_pid_used.getLikelihood()); - is_same = is_same && (lcio_pid->getAlgorithmType() == edm_pid_used.getAlgorithmType()); - for (int i = 0; i < edm_pid_used.parameters_size(); ++i) { - is_same = is_same && (edm_pid_used.getParameters(i) == lcio_pid->getParameters()[i]); - } - if (is_same) { - lcio_recp->setParticleIDUsed(lcio_pid); - break; - } - } - } - - // Link sinlge associated Vertex if found in converted ones - auto vertex = edm_rp.getStartVertex(); - if (vertex.isAvailable()) { - if (const auto lcio_vertex = k4EDM4hep2LcioConv::detail::mapLookupFrom(vertex, vertex_vec)) { - lcio_recp->setStartVertex(lcio_vertex.value()); - } - else { - lcio_recp->setStartVertex(nullptr); - } - } - - // Link multiple associated Tracks if found in converted ones - for (const auto& edm_rp_tr : edm_rp.getTracks()) { - if (edm_rp_tr.isAvailable()) { - if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, tracks_vec)) { - lcio_recp->addTrack(lcio_tr.value()); - } - else { - lcio_recp->addTrack(nullptr); - } - } - } - - // Link multiple associated Clusters if found in converted ones - for (const auto& edm_rp_cluster : edm_rp.getClusters()) { - if (edm_rp_cluster.isAvailable()) { - if (const auto lcio_cluster = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, clusters_vec)) { - lcio_recp->addCluster(lcio_cluster.value()); - } - else { - lcio_recp->addCluster(nullptr); - } - } - } - - // Add LCIO and EDM4hep pair collections to vec - k4EDM4hep2LcioConv::detail::mapInsert(lcio_recp, edm_rp, recoparticles_vec); - - // Add to reconstructed particles collection - recops->addElement(lcio_recp); - } - } - - // Link associated recopartilces after converting all recoparticles - for (auto& [lcio_rp, edm_rp] : recoparticles_vec) { - for (const auto& edm_linked_rp : edm_rp.getParticles()) { - if (edm_linked_rp.isAvailable()) { - // Search the linked track in the converted vector - if (const auto lcio_rp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_rp, recoparticles_vec)) { - lcio_rp->addParticle(lcio_rp_linked.value()); - } - } - } - } - - return recops; - } - - // Convert MC Particles to LCIO - // Add converted LCIO ptr and original EDM4hep collection to vector of pairs - // Add converted LCIO Collection Vector to LCIO event - template - lcio::LCCollectionVec* convMCParticles( - const edm4hep::MCParticleCollection* const mcparticle_coll, - MCPartMapT& mc_particles_vec) - { - auto* mcparticles = new lcio::LCCollectionVec(lcio::LCIO::MCPARTICLE); - - for (const auto& edm_mcp : (*mcparticle_coll)) { - auto* lcio_mcp = new lcio::MCParticleImpl; - if (edm_mcp.isAvailable()) { - lcio_mcp->setPDG(edm_mcp.getPDG()); - lcio_mcp->setGeneratorStatus(edm_mcp.getGeneratorStatus()); - // Note LCIO sets some Bits during writing which makes a trivial integer conversion afterwards not work - int status = edm_mcp.getSimulatorStatus(); - lcio_mcp->setSimulatorStatus(status); - - double vertex[3] = {edm_mcp.getVertex()[0], edm_mcp.getVertex()[1], edm_mcp.getVertex()[2]}; - lcio_mcp->setVertex(vertex); - lcio_mcp->setTime(edm_mcp.getTime()); - double endpoint[3] = {edm_mcp.getEndpoint()[0], edm_mcp.getEndpoint()[1], edm_mcp.getEndpoint()[2]}; - lcio_mcp->setEndpoint(endpoint); - double momentum[3] = {edm_mcp.getMomentum()[0], edm_mcp.getMomentum()[1], edm_mcp.getMomentum()[2]}; - lcio_mcp->setMomentum(momentum); - float momentumEndpoint[3] = { - edm_mcp.getMomentumAtEndpoint()[0], edm_mcp.getMomentumAtEndpoint()[1], edm_mcp.getMomentumAtEndpoint()[2]}; - lcio_mcp->setMomentumAtEndpoint(momentumEndpoint); -#warning "double to float" - lcio_mcp->setMass(edm_mcp.getMass()); - lcio_mcp->setCharge(edm_mcp.getCharge()); - float spin[3] = {edm_mcp.getSpin()[0], edm_mcp.getSpin()[1], edm_mcp.getSpin()[2]}; - lcio_mcp->setSpin(spin); - int colorflow[2] = {edm_mcp.getColorFlow()[0], edm_mcp.getColorFlow()[1]}; - lcio_mcp->setColorFlow(colorflow); - - lcio_mcp->setCreatedInSimulation(edm_mcp.isCreatedInSimulation()); - lcio_mcp->setBackscatter(edm_mcp.isBackscatter()); - lcio_mcp->setVertexIsNotEndpointOfParent(edm_mcp.vertexIsNotEndpointOfParent()); - lcio_mcp->setDecayedInTracker(edm_mcp.isDecayedInTracker()); - lcio_mcp->setDecayedInCalorimeter(edm_mcp.isDecayedInCalorimeter()); - lcio_mcp->setHasLeftDetector(edm_mcp.hasLeftDetector()); - lcio_mcp->setStopped(edm_mcp.isStopped()); - lcio_mcp->setOverlay(edm_mcp.isOverlay()); - - // Add LCIO and EDM4hep pair collections to vec - k4EDM4hep2LcioConv::detail::mapInsert(lcio_mcp, edm_mcp, mc_particles_vec); - - // Add to reconstructed particles collection - mcparticles->addElement(lcio_mcp); - } - } - - // Add parent MCParticles after converting all MCparticles - for (auto& [lcio_mcp, edm_mcp] : mc_particles_vec) { - for (const auto& emd_parent_mcp : edm_mcp.getParents()) { - if (emd_parent_mcp.isAvailable()) { - // Search for the parent mcparticle in the converted vector - if ( - const auto lcio_mcp_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(emd_parent_mcp, mc_particles_vec)) { - lcio_mcp->addParent(lcio_mcp_linked.value()); - } - } - } - } - - return mcparticles; - } - // The EventHeaderCollection should be of length 1 void convEventHeader(const edm4hep::EventHeaderCollection* const header_coll, lcio::LCEventImpl* const lcio_event) { @@ -740,128 +17,6 @@ namespace EDM4hep2LCIOConv { lcio_event->setWeight(header.getWeight()); } - // Depending on the order of the collections in the parameters, - // and for the mutual dependencies between some collections, - // go over the possible missing associated collections and fill them. - template - void FillMissingCollections(ObjectMappingT& collection_pairs) - { - // Fill missing Tracks collections - for (auto& [lcio_tr, edm_tr] : collection_pairs.tracks) { - if (lcio_tr->getTrackerHits().size() == 0) { - for (const auto& edm_tr_trh : edm_tr.getTrackerHits()) { - if ( - const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, collection_pairs.trackerhits)) { - lcio_tr->addHit(lcio_trh.value()); - } - } - } - } - - // Fill missing ReconstructedParticle collections - for (auto& [lcio_rp, edm_rp] : collection_pairs.recoparticles) { - // Link Vertex - if (lcio_rp->getStartVertex() == nullptr) { - if (edm_rp.getStartVertex().isAvailable()) { - if ( - const auto lcio_vertex = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp.getStartVertex(), collection_pairs.vertices)) { - lcio_rp->setStartVertex(lcio_vertex.value()); - } - } - } - - // Link Tracks - if (lcio_rp->getTracks().size() != edm_rp.tracks_size()) { - assert(lcio_rp->getTracks().size() == 0); - for (const auto& edm_rp_tr : edm_rp.getTracks()) { - if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, collection_pairs.tracks)) { - lcio_rp->addTrack(lcio_tr.value()); - } - } - } - - // Link Clusters - if (lcio_rp->getClusters().size() != edm_rp.clusters_size()) { - assert(lcio_rp->getClusters().size() == 0); - for (const auto& edm_rp_cluster : edm_rp.getClusters()) { - if ( - const auto lcio_cluster = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, collection_pairs.clusters)) { - lcio_rp->addCluster(lcio_cluster.value()); - } - } - } - - } // reconstructed particles - - // Fill missing Vertices collections - for (auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { - // Link Reconstructed Particles - if (lcio_vertex->getAssociatedParticle() == nullptr) { - const auto edm_rp = edm_vertex.getAssociatedParticle(); - if (edm_rp.isAvailable()) { - if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, collection_pairs.recoparticles)) { - lcio_vertex->setAssociatedParticle(lcio_rp.value()); - } - } - } - - } // vertices - - // Fill SimCaloHit collections with contributions - // - // We loop over all pairs of lcio and edm4hep simcalo hits and add the contributions, by now MCParticle - // collection(s) should be converted! - for (auto& [lcio_sch, edm_sch] : collection_pairs.simcalohits) { - // add associated Contributions (MCParticles) - for (int i = 0; i < edm_sch.contributions_size(); ++i) { - const auto& contrib = edm_sch.getContributions(i); - if (not contrib.isAvailable()) { - // We need a logging library independent of Gaudi for this! - // std::cout << "WARNING: CaloHit Contribution is not available!" << std::endl; - continue; - } - auto edm_contrib_mcp = contrib.getParticle(); - std::array step_position { - contrib.getStepPosition()[0], contrib.getStepPosition()[1], contrib.getStepPosition()[2]}; - bool mcp_found = false; - EVENT::MCParticle* lcio_mcp = nullptr; - if (edm_contrib_mcp.isAvailable()) { - // if we have the MCParticle we look for its partner - lcio_mcp = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcparticles).value_or(nullptr); - } - else { // edm mcp available - // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; - } - // we add contribution with whatever lcio mc particle we found - lcio_sch->addMCParticleContribution( - lcio_mcp, contrib.getEnergy(), contrib.getTime(), contrib.getPDG(), step_position.data()); - // if (!lcio_mcp) { - // std::cout << "WARNING: No MCParticle found for this contribution." - // << "Make Sure MCParticles are converted! " - // << edm_contrib_mcp.id() - // << std::endl; - // } - } // all emd4hep contributions - lcio_sch->setEnergy(edm_sch.getEnergy()); - } // SimCaloHit - - // Fill missing SimTrackerHit collections - for (auto& [lcio_strh, edm_strh] : collection_pairs.simtrackerhits) { - const auto lcio_strh_mcp = lcio_strh->getMCParticle(); - if (lcio_strh_mcp == nullptr) { - const auto edm_strh_mcp = edm_strh.getMCParticle(); - if ( - const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, collection_pairs.mcparticles)) { - lcio_strh->setMCParticle(lcio_mcp.value()); - } - } - - } // SimTrackerHits - } - // Check if a collection is already in the event by its name bool collectionExist(const std::string& collection_name, const lcio::LCEventImpl* lcio_event) { @@ -881,48 +36,48 @@ namespace EDM4hep2LCIOConv { const auto& cellIDStr = metadata.getParameter(podio::collMetadataParamName(name, "CellIDEncoding")); if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convTracks(coll, objectMappings.tracks, objectMappings.trackerhits); + auto lcColl = convTracks(coll, objectMappings.tracks, objectMappings.trackerHits); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convTrackerHits(coll, cellIDStr, objectMappings.trackerhits); + auto lcColl = convTrackerHits(coll, cellIDStr, objectMappings.trackerHits); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convSimTrackerHits(coll, cellIDStr, objectMappings.simtrackerhits, objectMappings.mcparticles); + auto lcColl = convSimTrackerHits(coll, cellIDStr, objectMappings.simTrackerHits, objectMappings.mcParticles); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convCalorimeterHits(coll, cellIDStr, objectMappings.calohits); + auto lcColl = convCalorimeterHits(coll, cellIDStr, objectMappings.caloHits); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convRawCalorimeterHits(coll, objectMappings.rawcalohits); + auto lcColl = convRawCalorimeterHits(coll, objectMappings.rawCaloHits); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convSimCalorimeterHits(coll, cellIDStr, objectMappings.simcalohits, objectMappings.mcparticles); + auto lcColl = convSimCalorimeterHits(coll, cellIDStr, objectMappings.simCaloHits, objectMappings.mcParticles); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convTPCHits(coll, objectMappings.tpchits); + auto lcColl = convTPCHits(coll, objectMappings.tpcHits); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convClusters(coll, objectMappings.clusters, objectMappings.calohits); + auto lcColl = convClusters(coll, objectMappings.clusters, objectMappings.caloHits); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convVertices(coll, objectMappings.vertices, objectMappings.recoparticles); + auto lcColl = convVertices(coll, objectMappings.vertices, objectMappings.recoParticles); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { - auto lcColl = convMCParticles(coll, objectMappings.mcparticles); + auto lcColl = convMCParticles(coll, objectMappings.mcParticles); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { auto lcColl = convReconstructedParticles( - coll, objectMappings.recoparticles, objectMappings.tracks, objectMappings.vertices, objectMappings.clusters); + coll, objectMappings.recoParticles, objectMappings.tracks, objectMappings.vertices, objectMappings.clusters); lcioEvent->addCollection(lcColl, name); } else if (auto coll = dynamic_cast(edmCollection)) { From f2bc3b12ab376154a319cc7262b52caa3ccf7b2c Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 13 Sep 2023 13:49:10 +0200 Subject: [PATCH 11/20] Make LCIO to EDM4hep conversoin a template library --- k4EDM4hep2LcioConv/CMakeLists.txt | 1 + .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 119 +- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp | 1068 +++++++++++++++++ k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp | 939 --------------- 4 files changed, 1074 insertions(+), 1053 deletions(-) create mode 100644 k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp diff --git a/k4EDM4hep2LcioConv/CMakeLists.txt b/k4EDM4hep2LcioConv/CMakeLists.txt index 276e6ede..a223e865 100644 --- a/k4EDM4hep2LcioConv/CMakeLists.txt +++ b/k4EDM4hep2LcioConv/CMakeLists.txt @@ -16,6 +16,7 @@ set(public_headers include/${PROJECT_NAME}/k4EDM4hep2LcioConv.h include/${PROJECT_NAME}/k4EDM4hep2LcioConv.ipp include/${PROJECT_NAME}/k4Lcio2EDM4hepConv.h + include/${PROJECT_NAME}/k4Lcio2EDM4hepConv.ipp include/${PROJECT_NAME}/MappingUtils.h ) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index cb5775bf..530f5f44 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -175,7 +175,7 @@ namespace LCIO2EDM4hepConv { * putting them into a collection and of creating the LCIO to EDM4hep mapping * is done in the conversion of the ReconstructedParticles. */ - edm4hep::MutableParticleID convertPaticleID(const EVENT::ParticleID* pid); + edm4hep::MutableParticleID convertParticleID(const EVENT::ParticleID* pid); /** * Convert an MCParticle collection and return the resulting collection. @@ -310,23 +310,7 @@ namespace LCIO2EDM4hepConv { typename ObjectMapT, typename LcioT = std::remove_pointer_t>, typename Edm4hepT = k4EDM4hep2LcioConv::detail::mapped_t> - auto handleSubsetColl(EVENT::LCCollection* lcioColl, const ObjectMapT& elemMap) - { - auto edm4hepColl = std::make_unique(); - edm4hepColl->setSubsetCollection(); - - UTIL::LCIterator lcioIter(lcioColl); - while (const auto lcioElem = lcioIter.next()) { - if (auto edm4hepElem = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioElem, elemMap)) { - edm4hepColl->push_back(edm4hepElem.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep object for an LCIO object in a subset collection" << std::endl; - } - } - - return edm4hepColl; - } + auto handleSubsetColl(EVENT::LCCollection* lcioColl, const ObjectMapT& elemMap); /** * Create an Association collection from an LCRelations collection. Templated @@ -348,42 +332,7 @@ namespace LCIO2EDM4hepConv { typename FromEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t, typename ToEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t> std::unique_ptr - createAssociationCollection(EVENT::LCCollection* relations, const FromMapT& fromMap, const ToMapT& toMap) - { - auto assocColl = std::make_unique(); - auto relIter = UTIL::LCIterator(relations); - - while (const auto rel = relIter.next()) { - auto assoc = assocColl->create(); - assoc.setWeight(rel->getWeight()); - const auto lcioTo = static_cast(rel->getTo()); - const auto lcioFrom = static_cast(rel->getFrom()); - const auto edm4hepTo = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioTo, toMap); - const auto edm4hepFrom = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioFrom, fromMap); - if (edm4hepTo.has_value() && edm4hepFrom.has_value()) { - if constexpr (Reverse) { - if constexpr (std::is_same_v) { - assoc.setVertex(*edm4hepTo); - } - else { - assoc.setSim(*edm4hepTo); - } - assoc.setRec(*edm4hepFrom); - } - else { - if constexpr (std::is_same_v) { - assoc.setVertex(*edm4hepFrom); - } - else { - assoc.setSim(*edm4hepFrom); - } - assoc.setRec(*edm4hepTo); - } - } - } - - return assocColl; - } + createAssociationCollection(EVENT::LCCollection* relations, const FromMapT& fromMap, const ToMapT& toMap); /** * Creates the CaloHitContributions for all SimCaloHits. @@ -455,66 +404,8 @@ namespace LCIO2EDM4hepConv { typename RecoParticleMapT = ObjectMapT> void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap); - template - void convertObjectParameters(LCIOType* lcioobj, podio::Frame& event) - { - const auto& params = lcioobj->getParameters(); - // handle srting params - EVENT::StringVec keys; - const auto stringKeys = params.getStringKeys(keys); - for (int i = 0; i < stringKeys.size(); i++) { - EVENT::StringVec sValues; - const auto stringVals = params.getStringVals(stringKeys[i], sValues); - event.putParameter(stringKeys[i], stringVals); - } - // handle float params - EVENT::StringVec fkeys; - const auto floatKeys = params.getFloatKeys(fkeys); - for (int i = 0; i < floatKeys.size(); i++) { - EVENT::FloatVec fValues; - const auto floatVals = params.getFloatVals(floatKeys[i], fValues); - event.putParameter(floatKeys[i], floatVals); - } - // handle int params - EVENT::StringVec ikeys; - const auto intKeys = params.getIntKeys(ikeys); - for (int i = 0; i < intKeys.size(); i++) { - EVENT::IntVec iValues; - const auto intVals = params.getIntVals(intKeys[i], iValues); - event.putParameter(intKeys[i], intVals); - } - // handle double params - EVENT::StringVec dkeys; - const auto dKeys = params.getDoubleKeys(dkeys); - for (int i = 0; i < dKeys.size(); i++) { - EVENT::DoubleVec dValues; - const auto dVals = params.getDoubleVals(dKeys[i], dValues); - event.putParameter(dKeys[i], dVals); - } - } - - template - std::vector convertLCVec(const std::string& name, EVENT::LCCollection* LCCollection) - { - auto dest = std::make_unique>(); - auto vecSizes = std::make_unique>(); - if (LCCollection->getNumberOfElements() > 0) { - vecSizes->push_back(0); - } - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - const auto* rval = static_cast(LCCollection->getElementAt(i)); - for (unsigned j = 0; j < rval->size(); j++) { - dest->push_back((*rval)[j]); - } - vecSizes->push_back(dest->size()); - } - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "_VecLenghts", std::move(vecSizes)); - return results; - } - } // namespace LCIO2EDM4hepConv +#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp" + #endif // K4EDM4HEP2LCIOCONV_K4LCIO2EDM4HEPCONV_H diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp new file mode 100644 index 00000000..34f66bf0 --- /dev/null +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp @@ -0,0 +1,1068 @@ +#include "k4EDM4hep2LcioConv/MappingUtils.h" + +namespace LCIO2EDM4hepConv { + template + void convertObjectParameters(LCIOType* lcioobj, podio::Frame& event) + { + const auto& params = lcioobj->getParameters(); + // handle srting params + EVENT::StringVec keys; + const auto stringKeys = params.getStringKeys(keys); + for (int i = 0; i < stringKeys.size(); i++) { + EVENT::StringVec sValues; + const auto stringVals = params.getStringVals(stringKeys[i], sValues); + event.putParameter(stringKeys[i], stringVals); + } + // handle float params + EVENT::StringVec fkeys; + const auto floatKeys = params.getFloatKeys(fkeys); + for (int i = 0; i < floatKeys.size(); i++) { + EVENT::FloatVec fValues; + const auto floatVals = params.getFloatVals(floatKeys[i], fValues); + event.putParameter(floatKeys[i], floatVals); + } + // handle int params + EVENT::StringVec ikeys; + const auto intKeys = params.getIntKeys(ikeys); + for (int i = 0; i < intKeys.size(); i++) { + EVENT::IntVec iValues; + const auto intVals = params.getIntVals(intKeys[i], iValues); + event.putParameter(intKeys[i], intVals); + } + // handle double params + EVENT::StringVec dkeys; + const auto dKeys = params.getDoubleKeys(dkeys); + for (int i = 0; i < dKeys.size(); i++) { + EVENT::DoubleVec dValues; + const auto dVals = params.getDoubleVals(dKeys[i], dValues); + event.putParameter(dKeys[i], dVals); + } + } + + template + std::vector convertLCVec(const std::string& name, EVENT::LCCollection* LCCollection) + { + auto dest = std::make_unique>(); + auto vecSizes = std::make_unique>(); + if (LCCollection->getNumberOfElements() > 0) { + vecSizes->push_back(0); + } + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + const auto* rval = static_cast(LCCollection->getElementAt(i)); + for (unsigned j = 0; j < rval->size(); j++) { + dest->push_back((*rval)[j]); + } + vecSizes->push_back(dest->size()); + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_VecLenghts", std::move(vecSizes)); + return results; + } + + template + auto handleSubsetColl(EVENT::LCCollection* lcioColl, const ObjectMapT& elemMap) + { + auto edm4hepColl = std::make_unique(); + edm4hepColl->setSubsetCollection(); + + UTIL::LCIterator lcioIter(lcioColl); + while (const auto lcioElem = lcioIter.next()) { + if (auto edm4hepElem = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioElem, elemMap)) { + edm4hepColl->push_back(edm4hepElem.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep object for an LCIO object in a subset collection" << std::endl; + } + } + + return edm4hepColl; + } + + template + std::unique_ptr + convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, MCParticleMapT& mcparticlesMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setPDG(rval->getPDG()); + lval.setGeneratorStatus(rval->getGeneratorStatus()); + lval.setSimulatorStatus(rval->getSimulatorStatus()); + lval.setCharge(rval->getCharge()); + lval.setTime(rval->getTime()); + lval.setMass(rval->getMass()); + lval.setSpin(edm4hep::Vector3f(rval->getSpin())); + lval.setColorFlow(edm4hep::Vector2i(rval->getColorFlow())); + lval.setVertex(edm4hep::Vector3d(rval->getVertex())); + lval.setEndpoint(edm4hep::Vector3d(rval->getEndpoint())); + lval.setMomentum(Vector3fFrom(rval->getMomentum())); + lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, mcparticlesMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element" << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + template + std::vector convertReconstructedParticles( + const std::string& name, + EVENT::LCCollection* LCCollection, + RecoMapT& recoparticlesMap, + PIDMapT& particleIDMap) + { + auto dest = std::make_unique(); + auto particleIDs = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setCharge(rval->getCharge()); + auto& m = rval->getCovMatrix(); // 10 parameters + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); + lval.setEnergy(rval->getEnergy()); + lval.setGoodnessOfPID(rval->getGoodnessOfPID()); + lval.setMass(rval->getMass()); + lval.setMomentum(Vector3fFrom(rval->getMomentum())); + lval.setReferencePoint(rval->getReferencePoint()); + lval.setType(rval->getType()); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, recoparticlesMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + + // Need to convert the particle IDs here, since they are part of the reco + // particle collection in LCIO + for (const auto lcioPid : rval->getParticleIDs()) { + auto pid = convertParticleID(lcioPid); + const auto [pidIt, pidInserted] = k4EDM4hep2LcioConv::detail::mapInsert( + lcioPid, pid, particleIDMap, k4EDM4hep2LcioConv::detail::InsertMode::Checked); + if (pidInserted) { + lval.addToParticleIDs(pid); + particleIDs->push_back(pid); + } + else { + lval.addToParticleIDs(k4EDM4hep2LcioConv::detail::getMapped(pidIt)); + } + } + + const auto lcioPidUsed = rval->getParticleIDUsed(); + if (lcioPidUsed == nullptr) { + continue; + } + if (const auto edm4hepPid = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioPidUsed, particleIDMap)) { + lval.setParticleIDUsed(edm4hepPid.value()); + } + else { + auto pid = convertParticleID(lcioPidUsed); + particleIDs->push_back(pid); + k4EDM4hep2LcioConv::detail::mapInsert(lcioPidUsed, pid, particleIDMap); + lval.setParticleIDUsed(pid); + } + } + + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_particleIDs", std::move(particleIDs)); + return results; + } + + template + std::unique_ptr + convertVertices(const std::string& name, EVENT::LCCollection* LCCollection, VertexMapT& vertexMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary + lval.setChi2(rval->getChi2()); + lval.setProbability(rval->getProbability()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getCovMatrix(); // 6 parameters + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + // FIXME: the algorithm type in LCIO is a string, but an integer is expected + // lval.setAlgorithmType(rval->getAlgorithmType()); + // lval.setAssociatedParticle(); //set it when convert ReconstructedParticle + // + for (auto v : rval->getParameters()) { + lval.addToParameters(v); + } + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, vertexMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + template + std::unique_ptr + convertSimTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, SimTrHitMapT& SimTrHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEDep(rval->getEDep()); + lval.setTime(rval->getTime()); + lval.setPathLength(rval->getPathLength()); + lval.setQuality(rval->getQuality()); + lval.setPosition(rval->getPosition()); + lval.setMomentum(rval->getMomentum()); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, SimTrHitMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + template + std::unique_ptr + convertTPCHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TPCHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setCellID(rval->getCellID()); + lval.setTime(rval->getTime()); + lval.setCharge(rval->getCharge()); + lval.setQuality(rval->getQuality()); + for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { + lval.addToAdcCounts(rval->getRawDataWord(j)); + } + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TPCHitMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + template + std::unique_ptr + convertTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setType(rval->getType()); + lval.setQuality(rval->getQuality()); + lval.setTime(rval->getTime()); + lval.setEDep(rval->getEDep()); + lval.setEDepError(rval->getEDepError()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getCovMatrix(); + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackerHitMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + return dest; + } + + template + std::unique_ptr + convertTrackerHitPlanes(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitPlaneMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setType(rval->getType()); + lval.setQuality(rval->getQuality()); + lval.setTime(rval->getTime()); + lval.setEDep(rval->getEDep()); + lval.setEDepError(rval->getEDepError()); + lval.setPosition(rval->getPosition()); + lval.setU({rval->getU()[0], rval->getU()[1]}); + lval.setV({rval->getV()[0], rval->getV()[1]}); + lval.setDu(rval->getdU()); + lval.setDv(rval->getdV()); + auto& m = rval->getCovMatrix(); + lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackerHitPlaneMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + template + std::unique_ptr + convertTracks(const std::string& name, EVENT::LCCollection* LCCollection, TrackMapT& TrackMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setType(rval->getType()); + lval.setChi2(rval->getChi2()); + lval.setNdf(rval->getNdf()); + lval.setDEdx(rval->getdEdx()); + lval.setDEdxError(rval->getdEdxError()); + lval.setRadiusOfInnermostHit(rval->getRadiusOfInnermostHit()); + + auto subdetectorHitNum = rval->getSubdetectorHitNumbers(); + for (auto hitNum : subdetectorHitNum) { + lval.addToSubdetectorHitNumbers(hitNum); + } + auto& trackStates = rval->getTrackStates(); + for (auto& trackState : trackStates) { + lval.addToTrackStates(convertTrackState(trackState)); + } + auto quantities = edm4hep::Quantity {}; + quantities.value = rval->getdEdx(); + quantities.error = rval->getdEdxError(); + lval.addToDxQuantities(quantities); + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + template + std::unique_ptr + convertSimCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& SimCaloHitMap) + { + auto dest = std::make_unique(); + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEnergy(rval->getEnergy()); + lval.setPosition(rval->getPosition()); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, SimCaloHitMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + template + std::unique_ptr + convertRawCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& rawCaloHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setAmplitude(rval->getAmplitude()); + lval.setTimeStamp(rval->getTimeStamp()); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, rawCaloHitMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + template + std::unique_ptr + convertCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& caloHitMap) + { + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + uint64_t cellID = rval->getCellID1(); + cellID = (cellID << 32) | rval->getCellID0(); + lval.setCellID(cellID); + lval.setEnergy(rval->getEnergy()); + lval.setEnergyError(rval->getEnergyError()); + lval.setPosition(rval->getPosition()); + lval.setTime(rval->getTime()); + lval.setType(rval->getType()); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, caloHitMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); + const auto existingId = existing.id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + } + + return dest; + } + + template + std::vector convertClusters( + const std::string& name, + EVENT::LCCollection* LCCollection, + ClusterMapT& clusterMap, + PIDMapT& particleIDMap) + { + auto particleIDs = std::make_unique(); + auto dest = std::make_unique(); + + for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { + auto* rval = static_cast(LCCollection->getElementAt(i)); + auto lval = dest->create(); + + lval.setEnergy(rval->getEnergy()); + lval.setEnergyError(rval->getEnergyError()); + lval.setITheta(rval->getITheta()); + lval.setPhi(rval->getIPhi()); + lval.setPosition(rval->getPosition()); + auto& m = rval->getPositionError(); + lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); + lval.setType(rval->getType()); + lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); + + const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, clusterMap); + if (!inserted) { + auto existing = k4EDM4hep2LcioConv::detail::getKey(iterator); + const auto existingId = existing->id(); + std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name + << " collection" << std::endl; + } + + // Need to convert the particle IDs here, since they are part of the cluster + // collection in LCIO + for (const auto lcioPid : rval->getParticleIDs()) { + auto pid = convertParticleID(lcioPid); + const auto [pidIt, pidInserted] = k4EDM4hep2LcioConv::detail::mapInsert(lcioPid, pid, particleIDMap); + if (pidInserted) { + lval.addToParticleIDs(pid); + particleIDs->push_back(pid); + } + else { + lval.addToParticleIDs(k4EDM4hep2LcioConv::detail::getMapped(pidIt)); + } + } + } + std::vector results; + results.reserve(2); + results.emplace_back(name, std::move(dest)); + results.emplace_back(name + "_particleIDs", std::move(particleIDs)); + return results; + } + + template + std::vector + convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, ObjectMappingT& typeMapping) + { + const auto& type = LCCollection->getTypeName(); + std::vector retColls; + if (type == "MCParticle") { + retColls.emplace_back(name, convertMCParticles(name, LCCollection, typeMapping.mcParticles)); + } + else if (type == "ReconstructedParticle") { + return convertReconstructedParticles(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); + } + else if (type == "Vertex") { + retColls.emplace_back(name, convertVertices(name, LCCollection, typeMapping.vertices)); + } + else if (type == "Track") { + retColls.emplace_back(name, convertTracks(name, LCCollection, typeMapping.tracks)); + } + else if (type == "Cluster") { + return convertClusters(name, LCCollection, typeMapping.clusters, typeMapping.particleIDs); + } + else if (type == "SimCalorimeterHit") { + retColls.emplace_back(name, convertSimCalorimeterHits(name, LCCollection, typeMapping.simCaloHits)); + } + else if (type == "RawCalorimeterHit") { + retColls.emplace_back(name, convertRawCalorimeterHits(name, LCCollection, typeMapping.rawCaloHits)); + } + else if (type == "CalorimeterHit") { + retColls.emplace_back(name, convertCalorimeterHits(name, LCCollection, typeMapping.caloHits)); + } + else if (type == "SimTrackerHit") { + retColls.emplace_back(name, convertSimTrackerHits(name, LCCollection, typeMapping.simTrackerHits)); + } + else if (type == "TPCHit") { + retColls.emplace_back(name, convertTPCHits(name, LCCollection, typeMapping.tpcHits)); + } + else if (type == "TrackerHit") { + retColls.emplace_back(name, convertTrackerHits(name, LCCollection, typeMapping.trackerHits)); + } + else if (type == "TrackerHitPlane") { + retColls.emplace_back(name, convertTrackerHitPlanes(name, LCCollection, typeMapping.trackerHitPlanes)); + } + else if (type == "LCIntVec") { + return convertLCVec(name, LCCollection); + } + else if (type == "LCFloatVec") { + return convertLCVec(name, LCCollection); + } + else if (type != "LCRelation") { + std::cerr << type << " is a collection type for which no known conversion exists." << std::endl; + } + return retColls; + } + + template + std::unique_ptr createCaloHitContributions( + HitMapT& SimCaloHitMap, + const MCParticleMapT& mcparticlesMap) + { + auto contrCollection = std::make_unique(); + for (auto& [lcioHit, edmHit] : SimCaloHitMap) { + auto NMCParticle = lcioHit->getNMCParticles(); + for (unsigned j = 0; j < NMCParticle; j++) { + auto edm_contr = contrCollection->create(); + edmHit.addToContributions(edm_contr); + + edm_contr.setPDG(lcioHit->getPDGCont(j)); + edm_contr.setTime(lcioHit->getTimeCont(j)); + edm_contr.setEnergy(lcioHit->getEnergyCont(j)); + edm_contr.setStepPosition(lcioHit->getStepPosition(j)); + auto lcioParticle = (lcioHit->getParticleCont(j)); + if (lcioParticle != nullptr) { + if (const auto edm4hepParticle = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioParticle, mcparticlesMap)) { + edm_contr.setParticle(edm4hepParticle.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for a LCIO MCParticle, " + << "while trying to build CaloHitContributions " << std::endl; + } + } + } + } + return contrCollection; + } + + template + void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap) + { + int edmnum = 1; + for (auto& [lcio, edm] : mcparticlesMap) { + edmnum++; + auto daughters = lcio->getDaughters(); + auto parents = lcio->getParents(); + + for (auto d : daughters) { + if (d == nullptr) { + continue; + } + if (const auto edmD = k4EDM4hep2LcioConv::detail::mapLookupTo(d, mcparticlesMap)) { + edm.addToDaughters(edmD.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " + "while trying to resolve the daughters of MCParticles" + << std::endl; + } + } + for (auto p : parents) { + if (p == nullptr) { + continue; + } + if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(p, mcparticlesMap)) { + edm.addToParents(edmP.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the parents of MCParticles Collections" + << std::endl; + } + } + } + } + + template + void resolveRelationsSimTrackerHits(HitMapT& SimTrHitMap, const MCParticleMapT& mcparticlesMap) + { + for (auto& [lcio, edm] : SimTrHitMap) { + auto mcps = lcio->getMCParticle(); + if (mcps == nullptr) { + continue; + } + if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(mcps, mcparticlesMap)) { + edm.setMCParticle(edmP.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " + "while trying to resolve the SimTrackHit Relations" + << std::endl; + } + } + } + + template + void resolveRelationsRecoParticles( + RecoParticleMapT& recoparticlesMap, + const VertexMapT& vertexMap, + const ClusterMapT& clusterMap, + const TrackMapT& tracksMap) + { + int edmnum = 1; + for (auto& [lcio, edm] : recoparticlesMap) { + edmnum++; + + const auto& vertex = lcio->getStartVertex(); + if (vertex != nullptr) { + if (const auto edmV = k4EDM4hep2LcioConv::detail::mapLookupTo(vertex, vertexMap)) { + edm.setStartVertex(edmV.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " + "while trying to resolve the ReconstructedParticle Relations " + << std::endl; + } + } + + auto clusters = lcio->getClusters(); + for (auto c : clusters) { + if (c == nullptr) { + continue; + } + if (const auto edmC = k4EDM4hep2LcioConv::detail::mapLookupTo(c, clusterMap)) { + edm.addToClusters(edmC.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + + auto tracks = lcio->getTracks(); + for (auto t : tracks) { + if (t == nullptr) { + continue; + } + if (const auto edmT = k4EDM4hep2LcioConv::detail::mapLookupTo(t, tracksMap)) { + edm.addToTracks(edmT.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " + "while trying to resolve the ReconstructedParticle Relations" + << std::endl; + } + } + + auto parents = lcio->getParticles(); + for (auto p : parents) { + if (p == nullptr) { + continue; + } + if (const auto edmReco = k4EDM4hep2LcioConv::detail::mapLookupTo(p, recoparticlesMap)) { + edm.addToParticles(edmReco.value()); + } + else { + std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " + "while trying to resolve the ReconstructedParticles parents Relations" + << std::endl; + } + } + } + } + + template + void resolveRelationsClusters(ClusterMapT& clustersMap, const CaloHitMapT& caloHitMap) + { + for (auto& [lcio, edm] : clustersMap) { + auto clusters = lcio->getClusters(); + auto calohits = lcio->getCalorimeterHits(); + auto shape = lcio->getShape(); + auto subdetectorEnergies = lcio->getSubdetectorEnergies(); + for (auto c : clusters) { + if (c == nullptr) { + continue; + } + if (const auto edmC = k4EDM4hep2LcioConv::detail::mapLookupTo(c, clustersMap)) { + edm.addToClusters(edmC.value()); + } + else { + std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; + } + } + for (auto cal : calohits) { + if (cal == nullptr) { + continue; + } + if (const auto edmCaloHit = k4EDM4hep2LcioConv::detail::mapLookupTo(cal, caloHitMap)) { + edm.addToHits(edmCaloHit.value()); + } + else { + std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; + } + } + for (auto s : shape) { + edm.addToShapeParameters(s); + } + for (auto subE : subdetectorEnergies) { + edm.addToSubdetectorEnergies(subE); + } + } + } + + template + void resolveRelationsTracks( + TrackMapT& tracksMap, + const TrackHitMapT& trackerHitMap, + const TPCHitMapT&, + const THPlaneHitMapT&) + { + for (auto& [lcio, edm] : tracksMap) { + auto tracks = lcio->getTracks(); + auto trackerHits = lcio->getTrackerHits(); + for (auto t : tracks) { + if (t == nullptr) { + continue; + } + if (const auto track = k4EDM4hep2LcioConv::detail::mapLookupTo(t, tracksMap)) { + edm.addToTracks(track.value()); + } + else { + // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; + } + } + for (auto th : trackerHits) { + if (th == nullptr) { + continue; + } + if (const auto trHit = k4EDM4hep2LcioConv::detail::mapLookupTo(th, trackerHitMap)) { + edm.addToTrackerHits(trHit.value()); + } + // else { + // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" + // << " This is due to it being a TrackerHitPlane or TPCHit" << std::endl; + + // // This Code looks for the trackerHit in the TPCHit Map aswell as the + // // trackerHitPlane Map. Those relations can not be set for a track in + // // edm4HEP. In all tests the missing trackerHits were located in + // // either of these maps. + // const auto tpchit = dynamic_cast(th); + // const auto trackerhitplane = dynamic_cast(th); + // if (tpchit != nullptr) { + // const auto it = TPCHitMap.find(tpchit); + // if (it != TPCHitMap.end()) { + // std::cout << "trackerHit found in TPCHit map !" << std::endl; + // } + // else { + // std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; + // } + // } + // else if (trackerhitplane != nullptr) { + // const auto it = trackerhitplaneMap.find(trackerhitplane); + // if (it != trackerhitplaneMap.end()) { + // std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; + // } + // else { + // std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; + // } + // } + // } + } + } + } + + template + void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap) + { + for (auto& [lcio, edm] : vertexMap) { + auto recoparticle = lcio->getAssociatedParticle(); + if (recoparticle == nullptr) { + continue; + } + if (const auto recoP = k4EDM4hep2LcioConv::detail::mapLookupTo(recoparticle, recoparticleMap)) { + edm.setAssociatedParticle(recoP.value()); + } + else { + std::cerr << "Couldn't find associated Particle to add to Vertex " + << "Relations in edm" << std::endl; + } + } + } + + template + void resolveRelations(ObjectMappingT& typeMapping) + { + resolveRelationsMCParticles(typeMapping.mcParticles); + resolveRelationsRecoParticles( + typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); + resolveRelationsSimTrackerHits(typeMapping.simTrackerHits, typeMapping.mcParticles); + resolveRelationsClusters(typeMapping.clusters, typeMapping.caloHits); + resolveRelationsTracks( + typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); + resolveRelationsVertices(typeMapping.vertices, typeMapping.recoParticles); + } + + template< + typename CollT, + bool Reverse, + typename FromMapT, + typename ToMapT, + typename FromLCIOT, + typename ToLCIOT, + typename FromEDM4hepT, + typename ToEDM4hepT> + std::unique_ptr + createAssociationCollection(EVENT::LCCollection* relations, const FromMapT& fromMap, const ToMapT& toMap) + { + auto assocColl = std::make_unique(); + auto relIter = UTIL::LCIterator(relations); + + while (const auto rel = relIter.next()) { + auto assoc = assocColl->create(); + assoc.setWeight(rel->getWeight()); + const auto lcioTo = static_cast(rel->getTo()); + const auto lcioFrom = static_cast(rel->getFrom()); + const auto edm4hepTo = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioTo, toMap); + const auto edm4hepFrom = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioFrom, fromMap); + if (edm4hepTo.has_value() && edm4hepFrom.has_value()) { + if constexpr (Reverse) { + if constexpr (std::is_same_v) { + assoc.setVertex(*edm4hepTo); + } + else { + assoc.setSim(*edm4hepTo); + } + assoc.setRec(*edm4hepFrom); + } + else { + if constexpr (std::is_same_v) { + assoc.setVertex(*edm4hepFrom); + } + else { + assoc.setSim(*edm4hepFrom); + } + assoc.setRec(*edm4hepTo); + } + } + } + + return assocColl; + } + + template + std::vector createAssociations( + const ObjectMappingT& typeMapping, + const std::vector>& LCRelation) + { + std::vector assoCollVec; + for (const auto& [name, relations] : LCRelation) { + const auto& params = relations->getParameters(); + + const auto& fromType = params.getStringVal("FromType"); + const auto& toType = params.getStringVal("ToType"); + if (fromType.empty() || toType.empty()) { + std::cerr << "LCRelation collection " << name << " has missing FromType or ToType parameters. " + << "Cannot convert it without this information." << std::endl; + continue; + } + + if (fromType == "MCParticle" && toType == "ReconstructedParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.recoParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.recoParticles, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.caloHits, typeMapping.simCaloHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simCaloHits, typeMapping.caloHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Cluster" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.clusters, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "MCParticle" && toType == "Cluster") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.clusters); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "MCParticle" && toType == "Track") { + auto mc_a = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.tracks); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Track" && toType == "MCParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.tracks, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.trackerHits, typeMapping.simTrackerHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simTrackerHits, typeMapping.trackerHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { + auto mc_a = createAssociationCollection( + relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { + auto mc_a = createAssociationCollection( + relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "ReconstructedParticle" && toType == "Vertex") { + auto mc_a = createAssociationCollection( + relations, typeMapping.recoParticles, typeMapping.vertices); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "Vertex" && toType == "ReconstructedParticle") { + auto mc_a = createAssociationCollection( + relations, typeMapping.vertices, typeMapping.recoParticles); + assoCollVec.emplace_back(name, std::move(mc_a)); + } + else if (fromType == "CalorimeterHit" && toType == "MCParticle") { + auto assoc = createAssociationCollection( + relations, typeMapping.caloHits, typeMapping.mcParticles); + assoCollVec.emplace_back(name, std::move(assoc)); + } + else if (fromType == "MCParticle" && toType == "CalorimeterHit") { + auto assoc = createAssociationCollection( + relations, typeMapping.mcParticles, typeMapping.caloHits); + assoCollVec.emplace_back(name, std::move(assoc)); + } + else { + std::cout << "Relation from: " << fromType << " to: " << toType << " (" << name + << ") is not beeing handled during creation of associations" << std::endl; + } + } + + return assoCollVec; + } + + template + std::unique_ptr + fillSubset(EVENT::LCCollection* LCCollection, const ObjectMappingT& typeMapping, const std::string& type) + { + if (type == "MCParticle") { + return handleSubsetColl(LCCollection, typeMapping.mcParticles); + } + else if (type == "ReconstructedParticle") { + return handleSubsetColl(LCCollection, typeMapping.recoParticles); + } + else if (type == "Vertex") { + return handleSubsetColl(LCCollection, typeMapping.vertices); + } + else if (type == "Track") { + return handleSubsetColl(LCCollection, typeMapping.tracks); + } + else if (type == "Cluster") { + return handleSubsetColl(LCCollection, typeMapping.clusters); + } + else if (type == "SimCalorimeterHit") { + return handleSubsetColl(LCCollection, typeMapping.simCaloHits); + } + else if (type == "RawCalorimeterHit") { + return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); + } + else if (type == "CalorimeterHit") { + return handleSubsetColl(LCCollection, typeMapping.caloHits); + } + else if (type == "SimTrackerHit") { + return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); + } + else if (type == "TPCHit") { + return handleSubsetColl(LCCollection, typeMapping.tpcHits); + } + else if (type == "TrackerHit") { + return handleSubsetColl(LCCollection, typeMapping.trackerHits); + } + else if (type == "TrackerHitPlane") { + return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); + } + else { + return nullptr; + } + } + +} // namespace LCIO2EDM4hepConv diff --git a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp index 805d9c33..aac54441 100644 --- a/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4Lcio2EDM4hepConv.cpp @@ -1,5 +1,4 @@ #include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h" -#include "k4EDM4hep2LcioConv/MappingUtils.h" #include @@ -60,535 +59,6 @@ namespace LCIO2EDM4hepConv { return result; } - template - std::unique_ptr - convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, MCParticleMapT& mcparticlesMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setPDG(rval->getPDG()); - lval.setGeneratorStatus(rval->getGeneratorStatus()); - lval.setSimulatorStatus(rval->getSimulatorStatus()); - lval.setCharge(rval->getCharge()); - lval.setTime(rval->getTime()); - lval.setMass(rval->getMass()); - lval.setSpin(edm4hep::Vector3f(rval->getSpin())); - lval.setColorFlow(edm4hep::Vector2i(rval->getColorFlow())); - lval.setVertex(edm4hep::Vector3d(rval->getVertex())); - lval.setEndpoint(edm4hep::Vector3d(rval->getEndpoint())); - lval.setMomentum(Vector3fFrom(rval->getMomentum())); - lval.setMomentumAtEndpoint(Vector3fFrom(rval->getMomentumAtEndpoint())); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, mcparticlesMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element" << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - template - std::vector convertReconstructedParticles( - const std::string& name, - EVENT::LCCollection* LCCollection, - RecoMapT& recoparticlesMap, - PIDMapT& particleIDMap) - { - auto dest = std::make_unique(); - auto particleIDs = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setCharge(rval->getCharge()); - auto& m = rval->getCovMatrix(); // 10 parameters - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9]}); - lval.setEnergy(rval->getEnergy()); - lval.setGoodnessOfPID(rval->getGoodnessOfPID()); - lval.setMass(rval->getMass()); - lval.setMomentum(Vector3fFrom(rval->getMomentum())); - lval.setReferencePoint(rval->getReferencePoint()); - lval.setType(rval->getType()); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, recoparticlesMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - - // Need to convert the particle IDs here, since they are part of the reco - // particle collection in LCIO - for (const auto lcioPid : rval->getParticleIDs()) { - auto pid = convertParticleID(lcioPid); - const auto [pidIt, pidInserted] = k4EDM4hep2LcioConv::detail::mapInsert( - lcioPid, pid, particleIDMap, k4EDM4hep2LcioConv::detail::InsertMode::Checked); - if (pidInserted) { - lval.addToParticleIDs(pid); - particleIDs->push_back(pid); - } - else { - lval.addToParticleIDs(k4EDM4hep2LcioConv::detail::getMapped(pidIt)); - } - } - - const auto lcioPidUsed = rval->getParticleIDUsed(); - if (lcioPidUsed == nullptr) { - continue; - } - if (const auto edm4hepPid = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioPidUsed, particleIDMap)) { - lval.setParticleIDUsed(edm4hepPid.value()); - } - else { - auto pid = convertParticleID(lcioPidUsed); - particleIDs->push_back(pid); - k4EDM4hep2LcioConv::detail::mapInsert(lcioPidUsed, pid, particleIDMap); - lval.setParticleIDUsed(pid); - } - } - - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "_particleIDs", std::move(particleIDs)); - return results; - } - - template - std::unique_ptr - convertVertices(const std::string& name, EVENT::LCCollection* LCCollection, VertexMapT& vertexMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setPrimary(rval->isPrimary() ? 1 : 0); // 1 for primary and 0 for not primary - lval.setChi2(rval->getChi2()); - lval.setProbability(rval->getProbability()); - lval.setPosition(rval->getPosition()); - auto& m = rval->getCovMatrix(); // 6 parameters - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - // FIXME: the algorithm type in LCIO is a string, but an integer is expected - // lval.setAlgorithmType(rval->getAlgorithmType()); - // lval.setAssociatedParticle(); //set it when convert ReconstructedParticle - // - for (auto v : rval->getParameters()) { - lval.addToParameters(v); - } - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, vertexMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - template - std::unique_ptr - convertSimTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, SimTrHitMapT& SimTrHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setEDep(rval->getEDep()); - lval.setTime(rval->getTime()); - lval.setPathLength(rval->getPathLength()); - lval.setQuality(rval->getQuality()); - lval.setPosition(rval->getPosition()); - lval.setMomentum(rval->getMomentum()); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, SimTrHitMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - template - std::unique_ptr - convertTPCHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TPCHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setCellID(rval->getCellID()); - lval.setTime(rval->getTime()); - lval.setCharge(rval->getCharge()); - lval.setQuality(rval->getQuality()); - for (unsigned j = 0, M = rval->getNRawDataWords(); j < M; j++) { - lval.addToAdcCounts(rval->getRawDataWord(j)); - } - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TPCHitMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - template - std::unique_ptr - convertTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setType(rval->getType()); - lval.setQuality(rval->getQuality()); - lval.setTime(rval->getTime()); - lval.setEDep(rval->getEDep()); - lval.setEDepError(rval->getEDepError()); - lval.setPosition(rval->getPosition()); - auto& m = rval->getCovMatrix(); - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackerHitMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - return dest; - } - - template - std::unique_ptr - convertTrackerHitPlanes(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitPlaneMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setType(rval->getType()); - lval.setQuality(rval->getQuality()); - lval.setTime(rval->getTime()); - lval.setEDep(rval->getEDep()); - lval.setEDepError(rval->getEDepError()); - lval.setPosition(rval->getPosition()); - lval.setU({rval->getU()[0], rval->getU()[1]}); - lval.setV({rval->getV()[0], rval->getV()[1]}); - lval.setDu(rval->getdU()); - lval.setDv(rval->getdV()); - auto& m = rval->getCovMatrix(); - lval.setCovMatrix({m[0], m[1], m[2], m[3], m[4], m[5]}); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackerHitPlaneMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - template - std::unique_ptr - convertTracks(const std::string& name, EVENT::LCCollection* LCCollection, TrackMapT& TrackMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setType(rval->getType()); - lval.setChi2(rval->getChi2()); - lval.setNdf(rval->getNdf()); - lval.setDEdx(rval->getdEdx()); - lval.setDEdxError(rval->getdEdxError()); - lval.setRadiusOfInnermostHit(rval->getRadiusOfInnermostHit()); - - auto subdetectorHitNum = rval->getSubdetectorHitNumbers(); - for (auto hitNum : subdetectorHitNum) { - lval.addToSubdetectorHitNumbers(hitNum); - } - auto& trackStates = rval->getTrackStates(); - for (auto& trackState : trackStates) { - lval.addToTrackStates(convertTrackState(trackState)); - } - auto quantities = edm4hep::Quantity {}; - quantities.value = rval->getdEdx(); - quantities.error = rval->getdEdxError(); - lval.addToDxQuantities(quantities); - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, TrackMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - template - std::unique_ptr - convertSimCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& SimCaloHitMap) - { - auto dest = std::make_unique(); - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setEnergy(rval->getEnergy()); - lval.setPosition(rval->getPosition()); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, SimCaloHitMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - template - std::unique_ptr - convertRawCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& rawCaloHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setAmplitude(rval->getAmplitude()); - lval.setTimeStamp(rval->getTimeStamp()); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, rawCaloHitMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - template - std::unique_ptr - convertCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& caloHitMap) - { - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - uint64_t cellID = rval->getCellID1(); - cellID = (cellID << 32) | rval->getCellID0(); - lval.setCellID(cellID); - lval.setEnergy(rval->getEnergy()); - lval.setEnergyError(rval->getEnergyError()); - lval.setPosition(rval->getPosition()); - lval.setTime(rval->getTime()); - lval.setType(rval->getType()); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, caloHitMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getMapped(iterator); - const auto existingId = existing.id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - } - - return dest; - } - - template - std::vector convertClusters( - const std::string& name, - EVENT::LCCollection* LCCollection, - ClusterMapT& clusterMap, - PIDMapT& particleIDMap) - { - auto particleIDs = std::make_unique(); - auto dest = std::make_unique(); - - for (unsigned i = 0, N = LCCollection->getNumberOfElements(); i < N; ++i) { - auto* rval = static_cast(LCCollection->getElementAt(i)); - auto lval = dest->create(); - - lval.setEnergy(rval->getEnergy()); - lval.setEnergyError(rval->getEnergyError()); - lval.setITheta(rval->getITheta()); - lval.setPhi(rval->getIPhi()); - lval.setPosition(rval->getPosition()); - auto& m = rval->getPositionError(); - lval.setPositionError({m[0], m[1], m[2], m[3], m[4], m[5]}); - lval.setType(rval->getType()); - lval.setDirectionError(Vector3fFrom(rval->getDirectionError())); - - const auto [iterator, inserted] = k4EDM4hep2LcioConv::detail::mapInsert(rval, lval, clusterMap); - if (!inserted) { - auto existing = k4EDM4hep2LcioConv::detail::getKey(iterator); - const auto existingId = existing->id(); - std::cerr << "EDM4hep element " << existingId << " did not get inserted. It belongs to the " << name - << " collection" << std::endl; - } - - // Need to convert the particle IDs here, since they are part of the cluster - // collection in LCIO - for (const auto lcioPid : rval->getParticleIDs()) { - auto pid = convertParticleID(lcioPid); - const auto [pidIt, pidInserted] = k4EDM4hep2LcioConv::detail::mapInsert(lcioPid, pid, particleIDMap); - if (pidInserted) { - lval.addToParticleIDs(pid); - particleIDs->push_back(pid); - } - else { - lval.addToParticleIDs(k4EDM4hep2LcioConv::detail::getMapped(pidIt)); - } - } - } - std::vector results; - results.reserve(2); - results.emplace_back(name, std::move(dest)); - results.emplace_back(name + "_particleIDs", std::move(particleIDs)); - return results; - } - - template - std::vector - convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, ObjectMappingT& typeMapping) - { - const auto& type = LCCollection->getTypeName(); - std::vector retColls; - if (type == "MCParticle") { - retColls.emplace_back(name, convertMCParticles(name, LCCollection, typeMapping.mcParticles)); - } - else if (type == "ReconstructedParticle") { - return convertReconstructedParticles(name, LCCollection, typeMapping.recoParticles, typeMapping.particleIDs); - } - else if (type == "Vertex") { - retColls.emplace_back(name, convertVertices(name, LCCollection, typeMapping.vertices)); - } - else if (type == "Track") { - retColls.emplace_back(name, convertTracks(name, LCCollection, typeMapping.tracks)); - } - else if (type == "Cluster") { - return convertClusters(name, LCCollection, typeMapping.clusters, typeMapping.particleIDs); - } - else if (type == "SimCalorimeterHit") { - retColls.emplace_back(name, convertSimCalorimeterHits(name, LCCollection, typeMapping.simCaloHits)); - } - else if (type == "RawCalorimeterHit") { - retColls.emplace_back(name, convertRawCalorimeterHits(name, LCCollection, typeMapping.rawCaloHits)); - } - else if (type == "CalorimeterHit") { - retColls.emplace_back(name, convertCalorimeterHits(name, LCCollection, typeMapping.caloHits)); - } - else if (type == "SimTrackerHit") { - retColls.emplace_back(name, convertSimTrackerHits(name, LCCollection, typeMapping.simTrackerHits)); - } - else if (type == "TPCHit") { - retColls.emplace_back(name, convertTPCHits(name, LCCollection, typeMapping.tpcHits)); - } - else if (type == "TrackerHit") { - retColls.emplace_back(name, convertTrackerHits(name, LCCollection, typeMapping.trackerHits)); - } - else if (type == "TrackerHitPlane") { - retColls.emplace_back(name, convertTrackerHitPlanes(name, LCCollection, typeMapping.trackerHitPlanes)); - } - else if (type == "LCIntVec") { - return convertLCVec(name, LCCollection); - } - else if (type == "LCFloatVec") { - return convertLCVec(name, LCCollection); - } - else if (type != "LCRelation") { - std::cerr << type << " is a collection type for which no known conversion exists." << std::endl; - } - return retColls; - } - - template - std::unique_ptr createCaloHitContributions( - HitMapT& SimCaloHitMap, - const MCParticleMapT& mcparticlesMap) - { - auto contrCollection = std::make_unique(); - for (auto& [lcioHit, edmHit] : SimCaloHitMap) { - auto NMCParticle = lcioHit->getNMCParticles(); - for (unsigned j = 0; j < NMCParticle; j++) { - auto edm_contr = contrCollection->create(); - edmHit.addToContributions(edm_contr); - - edm_contr.setPDG(lcioHit->getPDGCont(j)); - edm_contr.setTime(lcioHit->getTimeCont(j)); - edm_contr.setEnergy(lcioHit->getEnergyCont(j)); - edm_contr.setStepPosition(lcioHit->getStepPosition(j)); - auto lcioParticle = (lcioHit->getParticleCont(j)); - if (lcioParticle != nullptr) { - if (const auto edm4hepParticle = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioParticle, mcparticlesMap)) { - edm_contr.setParticle(edm4hepParticle.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for a LCIO MCParticle, " - << "while trying to build CaloHitContributions " << std::endl; - } - } - } - } - return contrCollection; - } - std::unique_ptr createEventHeader(const EVENT::LCEvent* evt) { auto headerColl = std::make_unique(); @@ -671,415 +141,6 @@ namespace LCIO2EDM4hepConv { return event; } - template - void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap) - { - int edmnum = 1; - for (auto& [lcio, edm] : mcparticlesMap) { - edmnum++; - auto daughters = lcio->getDaughters(); - auto parents = lcio->getParents(); - - for (auto d : daughters) { - if (d == nullptr) { - continue; - } - if (const auto edmD = k4EDM4hep2LcioConv::detail::mapLookupTo(d, mcparticlesMap)) { - edm.addToDaughters(edmD.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for an LCIO MCParticle, " - "while trying to resolve the daughters of MCParticles" - << std::endl; - } - } - for (auto p : parents) { - if (p == nullptr) { - continue; - } - if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(p, mcparticlesMap)) { - edm.addToParents(edmP.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the parents of MCParticles Collections" - << std::endl; - } - } - } - } - - template - void resolveRelationsSimTrackerHits(HitMapT& SimTrHitMap, const MCParticleMapT& mcparticlesMap) - { - for (auto& [lcio, edm] : SimTrHitMap) { - auto mcps = lcio->getMCParticle(); - if (mcps == nullptr) { - continue; - } - if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(mcps, mcparticlesMap)) { - edm.setMCParticle(edmP.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep MCParticle for the LCIO MCParticle, " - "while trying to resolve the SimTrackHit Relations" - << std::endl; - } - } - } - - template - void resolveRelationsRecoParticles( - RecoParticleMapT& recoparticlesMap, - const VertexMapT& vertexMap, - const ClusterMapT& clusterMap, - const TrackMapT& tracksMap) - { - int edmnum = 1; - for (auto& [lcio, edm] : recoparticlesMap) { - edmnum++; - - const auto vertex = lcio->getStartVertex(); - if (vertex) { - if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { - edm.setStartVertex(it->second); - } - else { - std::cerr << "Cannot find corresponding EDM4hep Vertex for a LCIO Vertex, " - "while trying to resolve the ReconstructedParticle Relations " - << std::endl; - } - } - - const auto& clusters = lcio->getClusters(); - for (auto c : clusters) { - if (c == nullptr) { - continue; - } - if (const auto edmC = k4EDM4hep2LcioConv::detail::mapLookupTo(c, clusterMap)) { - edm.addToClusters(edmC.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep Cluster for a LCIO Cluster, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; - } - } - - const auto& tracks = lcio->getTracks(); - for (auto t : tracks) { - if (t == nullptr) { - continue; - } - if (const auto edmT = k4EDM4hep2LcioConv::detail::mapLookupTo(t, tracksMap)) { - edm.addToTracks(edmT.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep Tracks for a LCIO Tracks, " - "while trying to resolve the ReconstructedParticle Relations" - << std::endl; - } - } - - auto parents = lcio->getParticles(); - for (auto p : parents) { - if (p == nullptr) { - continue; - } - if (const auto edmReco = k4EDM4hep2LcioConv::detail::mapLookupTo(p, recoparticlesMap)) { - edm.addToParticles(edmReco.value()); - } - else { - std::cerr << "Cannot find corresponding EDM4hep RecoParticle for a LCIO RecoParticle, " - "while trying to resolve the ReconstructedParticles parents Relations" - << std::endl; - } - } - } - } - - template - void resolveRelationsClusters(ClusterMapT& clustersMap, const CaloHitMapT& caloHitMap) - { - for (auto& [lcio, edm] : clustersMap) { - auto clusters = lcio->getClusters(); - auto calohits = lcio->getCalorimeterHits(); - auto shape = lcio->getShape(); - auto subdetectorEnergies = lcio->getSubdetectorEnergies(); - for (auto c : clusters) { - if (c == nullptr) { - continue; - } - if (const auto edmC = k4EDM4hep2LcioConv::detail::mapLookupTo(c, clustersMap)) { - edm.addToClusters(edmC.value()); - } - else { - std::cerr << "Couldn't find cluster to add to Relations in edm" << std::endl; - } - } - for (auto cal : calohits) { - if (cal == nullptr) { - continue; - } - if (const auto edmCaloHit = k4EDM4hep2LcioConv::detail::mapLookupTo(cal, caloHitMap)) { - edm.addToHits(edmCaloHit.value()); - } - else { - std::cerr << "Couldn't find CaloHit to add to Relations for Clusters in edm" << std::endl; - } - } - for (auto s : shape) { - edm.addToShapeParameters(s); - } - for (auto subE : subdetectorEnergies) { - edm.addToSubdetectorEnergies(subE); - } - } - } - - template - void resolveRelationsTracks( - TrackMapT& tracksMap, - const TrackHitMapT& trackerHitMap, - const TPCHitMapT&, - const THPlaneHitMapT&) - { - for (auto& [lcio, edm] : tracksMap) { - auto tracks = lcio->getTracks(); - auto trackerHits = lcio->getTrackerHits(); - for (auto t : tracks) { - if (t == nullptr) { - continue; - } - if (const auto track = k4EDM4hep2LcioConv::detail::mapLookupTo(t, tracksMap)) { - edm.addToTracks(track.value()); - } - else { - // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; - } - } - for (auto th : trackerHits) { - if (th == nullptr) { - continue; - } - if (const auto trHit = k4EDM4hep2LcioConv::detail::mapLookupTo(th, trackerHitMap)) { - edm.addToTrackerHits(trHit.value()); - } - // else { - // std::cerr << "Couldn't find trackerHit to add to Relations for tracks in edm\n" - // << " This is due to it being a TrackerHitPlane or TPCHit" << std::endl; - - // // This Code looks for the trackerHit in the TPCHit Map aswell as the - // // trackerHitPlane Map. Those relations can not be set for a track in - // // edm4HEP. In all tests the missing trackerHits were located in - // // either of these maps. - // const auto tpchit = dynamic_cast(th); - // const auto trackerhitplane = dynamic_cast(th); - // if (tpchit != nullptr) { - // const auto it = TPCHitMap.find(tpchit); - // if (it != TPCHitMap.end()) { - // std::cout << "trackerHit found in TPCHit map !" << std::endl; - // } - // else { - // std::cerr << "TRACKERHIT also could not be found in TPCHit Map" << std::endl; - // } - // } - // else if (trackerhitplane != nullptr) { - // const auto it = trackerhitplaneMap.find(trackerhitplane); - // if (it != trackerhitplaneMap.end()) { - // std::cout << "trackerHit found in TrackerHitPlane map !" << std::endl; - // } - // else { - // std::cerr << "TRACKERHIT also could not be found in TrackerHitPlane Map" << std::endl; - // } - // } - // } - } - } - } - - template - void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap) - { - for (auto& [lcio, edm] : vertexMap) { - auto recoparticle = lcio->getAssociatedParticle(); - if (recoparticle == nullptr) { - continue; - } - if (const auto recoP = k4EDM4hep2LcioConv::detail::mapLookupTo(recoparticle, recoparticleMap)) { - edm.setAssociatedParticle(recoP.value()); - } - else { - std::cerr << "Couldn't find associated Particle to add to Vertex " - << "Relations in edm" << std::endl; - } - } - } - - template - void resolveRelations(ObjectMappingT& typeMapping) - { - resolveRelationsMCParticles(typeMapping.mcParticles); - resolveRelationsRecoParticles( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); - resolveRelationsSimTrackerHits(typeMapping.simTrackerHits, typeMapping.mcParticles); - resolveRelationsClusters(typeMapping.clusters, typeMapping.caloHits); - resolveRelationsTracks( - typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); - resolveRelationsVertices(typeMapping.vertices, typeMapping.recoParticles); - } - - template - std::vector createAssociations( - const ObjectMappingT& typeMapping, - const std::vector>& LCRelation) - { - std::vector assoCollVec; - for (const auto& [name, relations] : LCRelation) { - const auto& params = relations->getParameters(); - - const auto& fromType = params.getStringVal("FromType"); - const auto& toType = params.getStringVal("ToType"); - if (fromType.empty() || toType.empty()) { - std::cerr << "LCRelation collection " << name << " has missing FromType or ToType parameters. " - << "Cannot convert it without this information." << std::endl; - continue; - } - - if (fromType == "MCParticle" && toType == "ReconstructedParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.recoParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.caloHits, typeMapping.simCaloHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.simCaloHits, typeMapping.caloHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Cluster" && toType == "MCParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.clusters, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "MCParticle" && toType == "Cluster") { - auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.clusters); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "MCParticle" && toType == "Track") { - auto mc_a = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.tracks); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Track" && toType == "MCParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.tracks, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHits, typeMapping.simTrackerHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { - auto mc_a = createAssociationCollection( - relations, typeMapping.simTrackerHits, typeMapping.trackerHitPlanes); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { - auto mc_a = createAssociationCollection( - relations, typeMapping.trackerHitPlanes, typeMapping.simTrackerHits); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "ReconstructedParticle" && toType == "Vertex") { - auto mc_a = createAssociationCollection( - relations, typeMapping.recoParticles, typeMapping.vertices); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "Vertex" && toType == "ReconstructedParticle") { - auto mc_a = createAssociationCollection( - relations, typeMapping.vertices, typeMapping.recoParticles); - assoCollVec.emplace_back(name, std::move(mc_a)); - } - else if (fromType == "CalorimeterHit" && toType == "MCParticle") { - auto assoc = createAssociationCollection( - relations, typeMapping.caloHits, typeMapping.mcParticles); - assoCollVec.emplace_back(name, std::move(assoc)); - } - else if (fromType == "MCParticle" && toType == "CalorimeterHit") { - auto assoc = createAssociationCollection( - relations, typeMapping.mcParticles, typeMapping.caloHits); - assoCollVec.emplace_back(name, std::move(assoc)); - } - else { - std::cout << "Relation from: " << fromType << " to: " << toType << " (" << name - << ") is not beeing handled during creation of associations" << std::endl; - } - } - - return assoCollVec; - } - - template - std::unique_ptr - fillSubset(EVENT::LCCollection* LCCollection, const ObjectMappingT& typeMapping, const std::string& type) - { - if (type == "MCParticle") { - return handleSubsetColl(LCCollection, typeMapping.mcParticles); - } - else if (type == "ReconstructedParticle") { - return handleSubsetColl(LCCollection, typeMapping.recoParticles); - } - else if (type == "Vertex") { - return handleSubsetColl(LCCollection, typeMapping.vertices); - } - else if (type == "Track") { - return handleSubsetColl(LCCollection, typeMapping.tracks); - } - else if (type == "Cluster") { - return handleSubsetColl(LCCollection, typeMapping.clusters); - } - else if (type == "SimCalorimeterHit") { - return handleSubsetColl(LCCollection, typeMapping.simCaloHits); - } - else if (type == "RawCalorimeterHit") { - return handleSubsetColl(LCCollection, typeMapping.rawCaloHits); - } - else if (type == "CalorimeterHit") { - return handleSubsetColl(LCCollection, typeMapping.caloHits); - } - else if (type == "SimTrackerHit") { - return handleSubsetColl(LCCollection, typeMapping.simTrackerHits); - } - else if (type == "TPCHit") { - return handleSubsetColl(LCCollection, typeMapping.tpcHits); - } - else if (type == "TrackerHit") { - return handleSubsetColl(LCCollection, typeMapping.trackerHits); - } - else if (type == "TrackerHitPlane") { - return handleSubsetColl(LCCollection, typeMapping.trackerHitPlanes); - } - else { - return nullptr; - } - } - podio::Frame convertRunHeader(EVENT::LCRunHeader* rheader) { podio::Frame runHeaderFrame; From 47786b8a227e7d26343c5e5d50a9a6e6a2f557d0 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 13 Sep 2023 13:55:37 +0200 Subject: [PATCH 12/20] Restore original behavior to not break marlinwrapper --- .../include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 52c06048..d40f955b 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -82,7 +82,10 @@ namespace edm4hep { namespace EDM4hep2LCIOConv { template - using ObjectMapT = k4EDM4hep2LcioConv::MapT; + using ObjectMapT = k4EDM4hep2LcioConv::VecMapT; + + template + using vec_pair [[deprecated("Use a more descriptive alias")]] = ObjectMapT; struct CollectionsPairVectors { ObjectMapT tracks {}; From d2f26646199afc6cdcd9e95525cb8dc29dd016f2 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 13 Sep 2023 14:13:08 +0200 Subject: [PATCH 13/20] Fix docstring --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index fe0a66db..a1367b59 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -131,10 +131,10 @@ namespace k4EDM4hep2LcioConv { /** * Insert a key-value pair into a "map" * - * safeInsert argument can be useld to check for existence of key first, - * before inserting. This is only useful for maps using a vector as backing, - * since the usual emplace already does this check and does not insert if a - * key already exists + * The InsertMode argument can be use to check whether the Key already + * exists in the map before inserting. This is only useful for maps using a + * vector as backing, since the usual emplace already does this check and + * does not insert if a key already exists */ template, typename MappedT = mapped_t> auto mapInsert(KeyT&& key, MappedT&& mapped, MapT& map, InsertMode insertMode = InsertMode::Unchecked) From 2f70c7dbbda86af7288fa08ec9cba535d4c4e369 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Thu, 14 Sep 2023 09:56:39 +0200 Subject: [PATCH 14/20] Make object map names consistent in both directions --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 16 ++++++++-------- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index d40f955b..867a360a 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -89,16 +89,16 @@ namespace EDM4hep2LCIOConv { struct CollectionsPairVectors { ObjectMapT tracks {}; - ObjectMapT trackerhits {}; - ObjectMapT simtrackerhits {}; - ObjectMapT calohits {}; - ObjectMapT rawcalohits {}; - ObjectMapT simcalohits {}; - ObjectMapT tpchits {}; + ObjectMapT trackerHits {}; + ObjectMapT simTrackerHits {}; + ObjectMapT caloHits {}; + ObjectMapT rawCaloHits {}; + ObjectMapT simCaloHits {}; + ObjectMapT tpcHits {}; ObjectMapT clusters {}; ObjectMapT vertices {}; - ObjectMapT recoparticles {}; - ObjectMapT mcparticles {}; + ObjectMapT recoParticles {}; + ObjectMapT mcParticles {}; }; template< diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp index 2ac06db7..c25d353b 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp @@ -718,7 +718,7 @@ namespace EDM4hep2LCIOConv { if (lcio_tr->getTrackerHits().size() == 0) { for (const auto& edm_tr_trh : edm_tr.getTrackerHits()) { if ( - const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, collection_pairs.trackerhits)) { + const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, collection_pairs.trackerHits)) { lcio_tr->addHit(lcio_trh.value()); } } @@ -726,7 +726,7 @@ namespace EDM4hep2LCIOConv { } // Fill missing ReconstructedParticle collections - for (auto& [lcio_rp, edm_rp] : collection_pairs.recoparticles) { + for (auto& [lcio_rp, edm_rp] : collection_pairs.recoParticles) { // Link Vertex if (lcio_rp->getStartVertex() == nullptr) { if (edm_rp.getStartVertex().isAvailable()) { @@ -768,7 +768,7 @@ namespace EDM4hep2LCIOConv { if (lcio_vertex->getAssociatedParticle() == nullptr) { const auto edm_rp = edm_vertex.getAssociatedParticle(); if (edm_rp.isAvailable()) { - if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, collection_pairs.recoparticles)) { + if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, collection_pairs.recoParticles)) { lcio_vertex->setAssociatedParticle(lcio_rp.value()); } } @@ -780,7 +780,7 @@ namespace EDM4hep2LCIOConv { // // We loop over all pairs of lcio and edm4hep simcalo hits and add the contributions, by now MCParticle // collection(s) should be converted! - for (auto& [lcio_sch, edm_sch] : collection_pairs.simcalohits) { + for (auto& [lcio_sch, edm_sch] : collection_pairs.simCaloHits) { // add associated Contributions (MCParticles) for (int i = 0; i < edm_sch.contributions_size(); ++i) { const auto& contrib = edm_sch.getContributions(i); @@ -797,7 +797,7 @@ namespace EDM4hep2LCIOConv { if (edm_contrib_mcp.isAvailable()) { // if we have the MCParticle we look for its partner lcio_mcp = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcparticles).value_or(nullptr); + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcParticles).value_or(nullptr); } else { // edm mcp available // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; @@ -818,12 +818,12 @@ namespace EDM4hep2LCIOConv { } // SimCaloHit // Fill missing SimTrackerHit collections - for (auto& [lcio_strh, edm_strh] : collection_pairs.simtrackerhits) { + for (auto& [lcio_strh, edm_strh] : collection_pairs.simTrackerHits) { const auto lcio_strh_mcp = lcio_strh->getMCParticle(); if (lcio_strh_mcp == nullptr) { const auto edm_strh_mcp = edm_strh.getMCParticle(); if ( - const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, collection_pairs.mcparticles)) { + const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, collection_pairs.mcParticles)) { lcio_strh->setMCParticle(lcio_mcp.value()); } } From 70de7687656dc6e424b1bbe51ddebea26ad33137 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Thu, 14 Sep 2023 16:10:17 +0200 Subject: [PATCH 15/20] Make enable_if conditions less restrictive --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index a1367b59..780dbf28 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -6,6 +6,7 @@ #include #include #include +#include #if __has_include("experimental/type_traits.h") #include @@ -14,8 +15,6 @@ namespace det { } #else // Implement the minimal feature set we need -#include - namespace det { namespace detail { template typename Op, typename... Args> @@ -80,6 +79,30 @@ namespace k4EDM4hep2LcioConv { template using mapped_t = typename map_t_helper::mapped_type; + /// bool constant to determine whether type T is a valid type to be used as + /// a key in the generic mapping functionality defined below. In this case + /// it checks for type equality or makes sure that KeyT is a base of T or + /// vice versa. This is designed specifically for the uses cases here, where + /// the LCIO types (pointers) are used as key types. + template + constexpr static bool is_valid_key_type_v = + std::is_same_v || std::is_base_of_v, std::remove_pointer_t> || + std::is_base_of_v, std::remove_pointer_t>; + + /// Detector and corresponding bool constant to detect whether two types are + /// equality comparable. c++20 would have a concept for this. + template + using has_operator_eq = decltype(std::declval() == std::declval()); + + template + constexpr static bool is_eq_comparable = det::is_detected_v; + + /// bool constant to determine whether a type T is a valid type to be used + /// as a mapped type in the generic mapping functionality defined below. In + /// this case this it checks T is equality copmarable with MappedT + template + constexpr static bool is_valid_mapped_type_v = is_eq_comparable; + /** * Find the mapped-to object in a map provided a key object * @@ -87,7 +110,7 @@ namespace k4EDM4hep2LcioConv { * types (i.e. MapT::find). In that case it will have the time complexity of * that. In case of a "map-like" (e.g. vector>) it will be O(N). */ - template>>> + template>>> auto mapLookupTo(FromT keyObj, const MapT& map) -> std::optional> { if constexpr (is_map_v) { @@ -112,7 +135,7 @@ namespace k4EDM4hep2LcioConv { * NOTE: This will always loop over potentially all elements in the provided * map, so it is definitely O(N) regardless of the provided map type */ - template>>> + template>>> auto mapLookupFrom(ToT mappedObj, const MapT& map) -> std::optional> { // In this case we cannot use a potential find method for an actual map, but @@ -145,7 +168,10 @@ namespace k4EDM4hep2LcioConv { else { if (insertMode == InsertMode::Checked) { if (auto existing = mapLookupTo(key, map)) { - return std::make_pair(std::make_tuple(key, existing.value()), false); + // Explicitly casting to the actual key type here to make return + // type deductoin work even in cases where we have a Map + // but the KeyT has been deduced as Derived* + return std::make_pair(std::make_tuple(key_t(key), existing.value()), false); } } return std::make_pair(map.emplace_back(std::forward(key), std::forward(mapped)), true); From 09568d5b15f2f5c912da35715fd7de3b458b4991 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Thu, 14 Sep 2023 16:54:49 +0200 Subject: [PATCH 16/20] Make it possible to use a global map for relation resolving --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 7 +++- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp | 36 ++++++++++--------- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 3 ++ .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp | 19 ++++++---- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 867a360a..64a5a0ce 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -183,7 +183,12 @@ namespace EDM4hep2LCIOConv { void convEventHeader(const edm4hep::EventHeaderCollection* const header_coll, lcio::LCEventImpl* const lcio_event); template - void FillMissingCollections(ObjectMappingT& collection_pairs); + void FillMissingCollections(ObjectMappingT& update_pairs); + + /// Update the relations of the objects in the update_pairs map, by linking + /// them according to the contents of the lookup_pairs map + template + void FillMissingCollections(ObjectMappingT& update_pairs, const ObjectMappingU& lookup_pairs); bool collectionExist(const std::string& collection_name, const lcio::LCEventImpl* lcio_event); diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp index c25d353b..f0dd49a7 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp @@ -707,18 +707,23 @@ namespace EDM4hep2LCIOConv { return mcparticles; } + template + void FillMissingCollections(ObjectMappingT& collection_pairs) + { + FillMissingCollections(collection_pairs, collection_pairs); + } + // Depending on the order of the collections in the parameters, // and for the mutual dependencies between some collections, // go over the possible missing associated collections and fill them. - template - void FillMissingCollections(ObjectMappingT& collection_pairs) + template + void FillMissingCollections(ObjectMappingT& update_pairs, const ObjectMappingU& lookup_pairs) { // Fill missing Tracks collections - for (auto& [lcio_tr, edm_tr] : collection_pairs.tracks) { + for (auto& [lcio_tr, edm_tr] : update_pairs.tracks) { if (lcio_tr->getTrackerHits().size() == 0) { for (const auto& edm_tr_trh : edm_tr.getTrackerHits()) { - if ( - const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, collection_pairs.trackerHits)) { + if (const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, lookup_pairs.trackerHits)) { lcio_tr->addHit(lcio_trh.value()); } } @@ -726,13 +731,13 @@ namespace EDM4hep2LCIOConv { } // Fill missing ReconstructedParticle collections - for (auto& [lcio_rp, edm_rp] : collection_pairs.recoParticles) { + for (auto& [lcio_rp, edm_rp] : update_pairs.recoParticles) { // Link Vertex if (lcio_rp->getStartVertex() == nullptr) { if (edm_rp.getStartVertex().isAvailable()) { if ( const auto lcio_vertex = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp.getStartVertex(), collection_pairs.vertices)) { + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp.getStartVertex(), lookup_pairs.vertices)) { lcio_rp->setStartVertex(lcio_vertex.value()); } } @@ -742,7 +747,7 @@ namespace EDM4hep2LCIOConv { if (lcio_rp->getTracks().size() != edm_rp.tracks_size()) { assert(lcio_rp->getTracks().size() == 0); for (const auto& edm_rp_tr : edm_rp.getTracks()) { - if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, collection_pairs.tracks)) { + if (const auto lcio_tr = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_tr, lookup_pairs.tracks)) { lcio_rp->addTrack(lcio_tr.value()); } } @@ -754,7 +759,7 @@ namespace EDM4hep2LCIOConv { for (const auto& edm_rp_cluster : edm_rp.getClusters()) { if ( const auto lcio_cluster = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, collection_pairs.clusters)) { + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp_cluster, lookup_pairs.clusters)) { lcio_rp->addCluster(lcio_cluster.value()); } } @@ -763,12 +768,12 @@ namespace EDM4hep2LCIOConv { } // reconstructed particles // Fill missing Vertices collections - for (auto& [lcio_vertex, edm_vertex] : collection_pairs.vertices) { + for (auto& [lcio_vertex, edm_vertex] : update_pairs.vertices) { // Link Reconstructed Particles if (lcio_vertex->getAssociatedParticle() == nullptr) { const auto edm_rp = edm_vertex.getAssociatedParticle(); if (edm_rp.isAvailable()) { - if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, collection_pairs.recoParticles)) { + if (const auto lcio_rp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_rp, lookup_pairs.recoParticles)) { lcio_vertex->setAssociatedParticle(lcio_rp.value()); } } @@ -780,7 +785,7 @@ namespace EDM4hep2LCIOConv { // // We loop over all pairs of lcio and edm4hep simcalo hits and add the contributions, by now MCParticle // collection(s) should be converted! - for (auto& [lcio_sch, edm_sch] : collection_pairs.simCaloHits) { + for (auto& [lcio_sch, edm_sch] : update_pairs.simCaloHits) { // add associated Contributions (MCParticles) for (int i = 0; i < edm_sch.contributions_size(); ++i) { const auto& contrib = edm_sch.getContributions(i); @@ -797,7 +802,7 @@ namespace EDM4hep2LCIOConv { if (edm_contrib_mcp.isAvailable()) { // if we have the MCParticle we look for its partner lcio_mcp = - k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, collection_pairs.mcParticles).value_or(nullptr); + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_contrib_mcp, lookup_pairs.mcParticles).value_or(nullptr); } else { // edm mcp available // std::cout << "WARNING: edm4hep contribution is not available!" << std::endl; @@ -818,12 +823,11 @@ namespace EDM4hep2LCIOConv { } // SimCaloHit // Fill missing SimTrackerHit collections - for (auto& [lcio_strh, edm_strh] : collection_pairs.simTrackerHits) { + for (auto& [lcio_strh, edm_strh] : update_pairs.simTrackerHits) { const auto lcio_strh_mcp = lcio_strh->getMCParticle(); if (lcio_strh_mcp == nullptr) { const auto edm_strh_mcp = edm_strh.getMCParticle(); - if ( - const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, collection_pairs.mcParticles)) { + if (const auto lcio_mcp = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_strh_mcp, lookup_pairs.mcParticles)) { lcio_strh->setMCParticle(lcio_mcp.value()); } } diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 530f5f44..8802318c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -123,6 +123,9 @@ namespace LCIO2EDM4hepConv { template void resolveRelations(ObjectMappingT& typeMapping); + template + void resolveRelations(ObjectMappingT& updateMaps, const ObjectMappingU& lookupMaps); + /** * Convert LCRelation collections into the corresponding Association collections in EDM4hep */ diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp index 34f66bf0..ee3da7ed 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp @@ -857,14 +857,19 @@ namespace LCIO2EDM4hepConv { template void resolveRelations(ObjectMappingT& typeMapping) { - resolveRelationsMCParticles(typeMapping.mcParticles); + resolveRelations(typeMapping, typeMapping); + } + + template + void resolveRelations(ObjectMappingT& updateMaps, const ObjectMappingU& lookupMaps) + { + resolveRelationsMCParticles(updateMaps.mcParticles); resolveRelationsRecoParticles( - typeMapping.recoParticles, typeMapping.vertices, typeMapping.clusters, typeMapping.tracks); - resolveRelationsSimTrackerHits(typeMapping.simTrackerHits, typeMapping.mcParticles); - resolveRelationsClusters(typeMapping.clusters, typeMapping.caloHits); - resolveRelationsTracks( - typeMapping.tracks, typeMapping.trackerHits, typeMapping.tpcHits, typeMapping.trackerHitPlanes); - resolveRelationsVertices(typeMapping.vertices, typeMapping.recoParticles); + updateMaps.recoParticles, lookupMaps.vertices, lookupMaps.clusters, lookupMaps.tracks); + resolveRelationsSimTrackerHits(updateMaps.simTrackerHits, lookupMaps.mcParticles); + resolveRelationsClusters(updateMaps.clusters, lookupMaps.caloHits); + resolveRelationsTracks(updateMaps.tracks, lookupMaps.trackerHits, lookupMaps.tpcHits, lookupMaps.trackerHitPlanes); + resolveRelationsVertices(updateMaps.vertices, lookupMaps.recoParticles); } template< From 12f72b36e9476edab44539a56b17511ae494a882 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 27 Sep 2023 17:02:39 +0200 Subject: [PATCH 17/20] Remove specifying unnecessary default types for template parameters --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h | 38 ++++------- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 66 +++++++------------ 2 files changed, 36 insertions(+), 68 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 64a5a0ce..95013594 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -101,73 +101,61 @@ namespace EDM4hep2LCIOConv { ObjectMapT mcParticles {}; }; - template< - typename TrackMapT = ObjectMapT, - typename TrackerHitMapT = ObjectMapT> + template lcio::LCCollectionVec* convTracks( const edm4hep::TrackCollection* const tracks_coll, TrackMapT& tracks_vec, const TrackerHitMapT& trackerhits_vec); - template> + template lcio::LCCollectionVec* convTrackerHits( const edm4hep::TrackerHitCollection* const trackerhits_coll, const std::string& cellIDstr, TrackerHitMapT& trackerhits_vec); - template< - typename SimTrHitMapT = ObjectMapT, - typename MCParticleMapT = ObjectMapT> + template lcio::LCCollectionVec* convSimTrackerHits( const edm4hep::SimTrackerHitCollection* const simtrackerhits_coll, const std::string& cellIDstr, SimTrHitMapT& simtrackerhits_vec, const MCParticleMapT& mcparticles_vec); - template> + template lcio::LCCollectionVec* convCalorimeterHits( const edm4hep::CalorimeterHitCollection* const calohit_coll, const std::string& cellIDstr, CaloHitMapT& calo_hits_vec); - template> + template lcio::LCCollectionVec* convRawCalorimeterHits( const edm4hep::RawCalorimeterHitCollection* const rawcalohit_coll, RawCaloHitMapT& raw_calo_hits_vec); - template> + template lcio::LCCollectionVec* convSimCalorimeterHits( const edm4hep::SimCalorimeterHitCollection* const simcalohit_coll, const std::string& cellIDstr, SimCaloHitMapT& sim_calo_hits_vec, const MCParticleMapT& mcparticles); - template> + template lcio::LCCollectionVec* convTPCHits( const edm4hep::RawTimeSeriesCollection* const tpchit_coll, TPCHitMapT& tpc_hits_vec); - template< - typename ClusterMapT = ObjectMapT, - typename CaloHitMapT = ObjectMapT> + template lcio::LCCollectionVec* convClusters( const edm4hep::ClusterCollection* const cluster_coll, ClusterMapT& cluster_vec, const CaloHitMapT& calohits_vec); - template< - typename VertexMapT = ObjectMapT, - typename RecoPartMapT = ObjectMapT> + template lcio::LCCollectionVec* convVertices( const edm4hep::VertexCollection* const vertex_coll, VertexMapT& vertex_vec, const RecoPartMapT& recoparticles_vec); - template< - typename RecoPartMapT = ObjectMapT, - typename TrackMapT = ObjectMapT, - typename VertexMapT = ObjectMapT, - typename ClusterMapT = ObjectMapT> + template lcio::LCCollectionVec* convReconstructedParticles( const edm4hep::ReconstructedParticleCollection* const recos_coll, RecoPartMapT& recoparticles_vec, @@ -175,19 +163,19 @@ namespace EDM4hep2LCIOConv { const VertexMapT& vertex_vec, const ClusterMapT& clusters_vec); - template> + template lcio::LCCollectionVec* convMCParticles( const edm4hep::MCParticleCollection* const mcparticle_coll, MCPartMapT& mc_particles_vec); void convEventHeader(const edm4hep::EventHeaderCollection* const header_coll, lcio::LCEventImpl* const lcio_event); - template + template void FillMissingCollections(ObjectMappingT& update_pairs); /// Update the relations of the objects in the update_pairs map, by linking /// them according to the contents of the lookup_pairs map - template + template void FillMissingCollections(ObjectMappingT& update_pairs, const ObjectMappingU& lookup_pairs); bool collectionExist(const std::string& collection_name, const lcio::LCEventImpl* lcio_event); diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 8802318c..8f7ffffc 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -111,7 +111,7 @@ namespace LCIO2EDM4hepConv { * Returns a vector of names and collections (since some LCIO collections will * result in more than one EDM4hep collection) */ - template + template std::vector convertCollection(const std::string& name, EVENT::LCCollection* LCCollection, ObjectMappingT& typeMapping); @@ -120,7 +120,7 @@ namespace LCIO2EDM4hepConv { * Dispatch to the correpsonding implementation for all the types that have * relations */ - template + template void resolveRelations(ObjectMappingT& typeMapping); template @@ -129,7 +129,7 @@ namespace LCIO2EDM4hepConv { /** * Convert LCRelation collections into the corresponding Association collections in EDM4hep */ - template + template std::vector createAssociations( const ObjectMappingT& typeMapping, const std::vector>& LCRelation); @@ -138,7 +138,7 @@ namespace LCIO2EDM4hepConv { * Convert a subset collection, dispatching to the correct function for the * type of the input collection */ - template + template std::unique_ptr fillSubset(EVENT::LCCollection* LCCollection, const ObjectMappingT& typeMapping, const std::string& type); @@ -184,7 +184,7 @@ namespace LCIO2EDM4hepConv { * Convert an MCParticle collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, MCParticleMapT& mcparticlesMap); @@ -196,9 +196,7 @@ namespace LCIO2EDM4hepConv { * part of the ReconstructedParticles in LCIO. The name of this collection is * _particleIDs */ - template< - typename RecoMapT = ObjectMapT, - typename PIDMapT = ObjectMapT> + template std::vector convertReconstructedParticles( const std::string& name, EVENT::LCCollection* LCCollection, @@ -209,7 +207,7 @@ namespace LCIO2EDM4hepConv { * Convert a Vertex collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertVertices(const std::string& name, EVENT::LCCollection* LCCollection, VertexMapT& vertexMap); @@ -217,7 +215,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimTrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertSimTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, SimTrHitMapT& SimTrHitMap); @@ -225,7 +223,7 @@ namespace LCIO2EDM4hepConv { * Convert a TPCHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertTPCHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TPCHitMap); @@ -233,7 +231,7 @@ namespace LCIO2EDM4hepConv { * Convert a TrackerHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertTrackerHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitMap); @@ -241,7 +239,7 @@ namespace LCIO2EDM4hepConv { * Convert a TrackerHitPlane collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertTrackerHitPlanes(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& TrackerHitPlaneMap); @@ -249,7 +247,7 @@ namespace LCIO2EDM4hepConv { * Convert a Track collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertTracks(const std::string& name, EVENT::LCCollection* LCCollection, TrackMapT& TrackMap); @@ -257,7 +255,7 @@ namespace LCIO2EDM4hepConv { * Convert a SimCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertSimCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& SimCaloHitMap); @@ -265,7 +263,7 @@ namespace LCIO2EDM4hepConv { * Convert a RawCalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertRawCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& rawCaloHitMap); @@ -273,7 +271,7 @@ namespace LCIO2EDM4hepConv { * Convert a CalorimeterHit collection and return the resulting collection. * Simultaneously populates the mapping from LCIO to EDM4hep objects. */ - template> + template std::unique_ptr convertCalorimeterHits(const std::string& name, EVENT::LCCollection* LCCollection, HitMapT& caloHitMap); @@ -285,9 +283,7 @@ namespace LCIO2EDM4hepConv { * part of the Cluster collection in LCIO. The name of this collection is * _particleIDs */ - template< - typename ClusterMapT = ObjectMapT, - typename PIDMapT = ObjectMapT> + template std::vector convertClusters( const std::string& name, EVENT::LCCollection* LCCollection, @@ -342,9 +338,7 @@ namespace LCIO2EDM4hepConv { * has to be done this way, since the converted McParticles are needeed. * The contributions are also attached to their corresponding SimCalorimeterHits. */ - template< - typename HitMapT = ObjectMapT, - typename MCParticleMapT = ObjectMapT> + template std::unique_ptr createCaloHitContributions( HitMapT& SimCaloHitMap, const MCParticleMapT& mcparticlesMap); @@ -352,25 +346,19 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for the MCParticles. */ - template> + template void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap); /** * Resolve the relations for SimTrackerHits */ - template< - typename HitMapT = ObjectMapT, - typename MCParticleMapT = ObjectMapT> + template void resolveRelationsSimTrackerHits(HitMapT& SimTrHitMap, const MCParticleMapT& mcparticlesMap); /** * Resolve the relations for ReconstructedParticles */ - template< - typename RecoParticleMapT = ObjectMapT, - typename VertexMapT = ObjectMapT, - typename ClusterMapT = ObjectMapT, - typename TrackMapT = ObjectMapT> + template void resolveRelationsRecoParticles( RecoParticleMapT& recoparticlesMap, const VertexMapT& vertexMap, @@ -380,19 +368,13 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for Clusters */ - template< - typename ClusterMapT = ObjectMapT, - typename CaloHitMapT = ObjectMapT> + template void resolveRelationsClusters(ClusterMapT& clustersMap, const CaloHitMapT& caloHitMap); /** * Resolve the relations for Tracks */ - template< - typename TrackMapT = ObjectMapT, - typename TrackHitMapT = ObjectMapT, - typename TPCHitMapT = ObjectMapT, - typename THPlaneHitMapT = ObjectMapT> + template void resolveRelationsTracks( TrackMapT& tracksMap, const TrackHitMapT& trackerHitMap, @@ -402,9 +384,7 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for Vertices */ - template< - typename VertexMapT = ObjectMapT, - typename RecoParticleMapT = ObjectMapT> + template void resolveRelationsVertices(VertexMapT& vertexMap, const RecoParticleMapT& recoparticleMap); } // namespace LCIO2EDM4hepConv From 809cef9e41dae5ca1a69ca77ce0e0c8197e63268 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 27 Sep 2023 17:34:09 +0200 Subject: [PATCH 18/20] Improve log output for subset collection conversion --- .../include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp index ee3da7ed..5212136d 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp @@ -73,7 +73,8 @@ namespace LCIO2EDM4hepConv { edm4hepColl->push_back(edm4hepElem.value()); } else { - std::cerr << "Cannot find corresponding EDM4hep object for an LCIO object in a subset collection" << std::endl; + std::cerr << "Cannot find corresponding EDM4hep object for an LCIO object in a subset collection of type " + << Edm4hepT::collection_type::valueTypeName << std::endl; } } From 547fa394139fa506338b39d47b5fadf4594bc64a Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 27 Sep 2023 18:22:37 +0200 Subject: [PATCH 19/20] Make the association coll creation work with all map types --- .../include/k4EDM4hep2LcioConv/MappingUtils.h | 26 +++++++++++++++++++ .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp | 4 +-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index 780dbf28..6e60d84c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -79,6 +79,32 @@ namespace k4EDM4hep2LcioConv { template using mapped_t = typename map_t_helper::mapped_type; + template + using has_object_type = typename T::object_type; + + /// Detector for whether a type T is a Mutable user facing type. + template + constexpr static bool is_mutable_v = det::is_detected_v; + + /// Helper struct to determine the Mutable type for a user facing type + /// NOTE: Not SFINAE safe for anything that is not a podio generated class + template>> + struct mutable_t_helper { + }; + + template + struct mutable_t_helper> { + using type = T; + }; + + template + struct mutable_t_helper> { + using type = typename T::mutable_type; + }; + + template + using mutable_t = typename mutable_t_helper::type; + /// bool constant to determine whether type T is a valid type to be used as /// a key in the generic mapping functionality defined below. In this case /// it checks for type equality or makes sure that KeyT is a base of T or diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp index 5212136d..d8de47f5 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp @@ -897,7 +897,7 @@ namespace LCIO2EDM4hepConv { const auto edm4hepFrom = k4EDM4hep2LcioConv::detail::mapLookupTo(lcioFrom, fromMap); if (edm4hepTo.has_value() && edm4hepFrom.has_value()) { if constexpr (Reverse) { - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v, edm4hep::MutableVertex>) { assoc.setVertex(*edm4hepTo); } else { @@ -906,7 +906,7 @@ namespace LCIO2EDM4hepConv { assoc.setRec(*edm4hepFrom); } else { - if constexpr (std::is_same_v) { + if constexpr (std::is_same_v, edm4hep::MutableVertex>) { assoc.setVertex(*edm4hepFrom); } else { From ec2f31f6f1c3e3995a4ba9ef43bda8b7fedad4c7 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Wed, 27 Sep 2023 18:34:41 +0200 Subject: [PATCH 20/20] Pass separate lookup maps for resolving MC and reco particle Necessary to build hierarchies that might span several collections --- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 12 +++++++--- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp | 22 ++++++++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 8f7ffffc..e3acdcee 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -346,8 +346,8 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for the MCParticles. */ - template - void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap); + template + void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap, const MCParticleLookupMapT& lookupMap); /** * Resolve the relations for SimTrackerHits @@ -358,9 +358,15 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for ReconstructedParticles */ - template + template< + typename RecoParticleMapT, + typename RecoParticleLookupMapT, + typename VertexMapT, + typename ClusterMapT, + typename TrackMapT> void resolveRelationsRecoParticles( RecoParticleMapT& recoparticlesMap, + const RecoParticleLookupMapT& recoLookupMap, const VertexMapT& vertexMap, const ClusterMapT& clusterMap, const TrackMapT& tracksMap); diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp index d8de47f5..4b1e5914 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp @@ -610,8 +610,8 @@ namespace LCIO2EDM4hepConv { return contrCollection; } - template - void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap) + template + void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap, const MCParticleLookupMapT& lookupMap) { int edmnum = 1; for (auto& [lcio, edm] : mcparticlesMap) { @@ -623,7 +623,7 @@ namespace LCIO2EDM4hepConv { if (d == nullptr) { continue; } - if (const auto edmD = k4EDM4hep2LcioConv::detail::mapLookupTo(d, mcparticlesMap)) { + if (const auto edmD = k4EDM4hep2LcioConv::detail::mapLookupTo(d, lookupMap)) { edm.addToDaughters(edmD.value()); } else { @@ -636,7 +636,7 @@ namespace LCIO2EDM4hepConv { if (p == nullptr) { continue; } - if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(p, mcparticlesMap)) { + if (const auto edmP = k4EDM4hep2LcioConv::detail::mapLookupTo(p, lookupMap)) { edm.addToParents(edmP.value()); } else { @@ -667,9 +667,15 @@ namespace LCIO2EDM4hepConv { } } - template + template< + typename RecoParticleMapT, + typename RecoParticleLookupMapT, + typename VertexMapT, + typename ClusterMapT, + typename TrackMapT> void resolveRelationsRecoParticles( RecoParticleMapT& recoparticlesMap, + const RecoParticleLookupMapT& recoLookupMap, const VertexMapT& vertexMap, const ClusterMapT& clusterMap, const TrackMapT& tracksMap) @@ -725,7 +731,7 @@ namespace LCIO2EDM4hepConv { if (p == nullptr) { continue; } - if (const auto edmReco = k4EDM4hep2LcioConv::detail::mapLookupTo(p, recoparticlesMap)) { + if (const auto edmReco = k4EDM4hep2LcioConv::detail::mapLookupTo(p, recoLookupMap)) { edm.addToParticles(edmReco.value()); } else { @@ -864,9 +870,9 @@ namespace LCIO2EDM4hepConv { template void resolveRelations(ObjectMappingT& updateMaps, const ObjectMappingU& lookupMaps) { - resolveRelationsMCParticles(updateMaps.mcParticles); + resolveRelationsMCParticles(updateMaps.mcParticles, lookupMaps.mcParticles); resolveRelationsRecoParticles( - updateMaps.recoParticles, lookupMaps.vertices, lookupMaps.clusters, lookupMaps.tracks); + updateMaps.recoParticles, lookupMaps.recoParticles, lookupMaps.vertices, lookupMaps.clusters, lookupMaps.tracks); resolveRelationsSimTrackerHits(updateMaps.simTrackerHits, lookupMaps.mcParticles); resolveRelationsClusters(updateMaps.clusters, lookupMaps.caloHits); resolveRelationsTracks(updateMaps.tracks, lookupMaps.trackerHits, lookupMaps.tpcHits, lookupMaps.trackerHitPlanes);