diff --git a/k4EDM4hep2LcioConv/CMakeLists.txt b/k4EDM4hep2LcioConv/CMakeLists.txt index 951e232c..be82db61 100644 --- a/k4EDM4hep2LcioConv/CMakeLists.txt +++ b/k4EDM4hep2LcioConv/CMakeLists.txt @@ -14,7 +14,7 @@ target_link_libraries(k4EDM4hep2LcioConv PUBLIC set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES - PUBLIC_HEADER "include/${CMAKE_PROJECT_NAME}/k4EDM4hep2LcioConv.h;include/${CMAKE_PROJECT_NAME}/k4Lcio2EDM4hepConv.h" + PUBLIC_HEADER "include/${CMAKE_PROJECT_NAME}/k4EDM4hep2LcioConv.h;include/${CMAKE_PROJECT_NAME}/k4Lcio2EDM4hepConv.h;include/${CMAKE_PROJECT_NAME}/MappingUtils.h" ) install(TARGETS k4EDM4hep2LcioConv diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h index b2365ff0..9c88515c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/MappingUtils.h @@ -54,11 +54,11 @@ 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>> + template>> struct map_t_helper {}; template @@ -89,7 +89,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 +124,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 = key_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 2bbb5bbd..c5996ea9 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -104,29 +104,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. @@ -170,10 +174,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. @@ -183,92 +186,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. @@ -278,11 +275,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. @@ -298,17 +298,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; @@ -331,14 +334,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); @@ -380,51 +383,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 b18a48ca..c963a23d 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, " @@ -663,7 +659,8 @@ namespace LCIO2EDM4hepConv { return event; } - void resolveRelationsMCParticles(TypeMapT& mcparticlesMap) + template + void resolveRelationsMCParticles(MCParticleMapT& mcparticlesMap) { int edmnum = 1; for (auto& [lcio, edm] : mcparticlesMap) { @@ -671,15 +668,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, " @@ -691,9 +685,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, " @@ -704,18 +697,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, " @@ -725,11 +716,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) { @@ -739,8 +731,8 @@ namespace LCIO2EDM4hepConv { if (vertex == nullptr) { continue; } - if (const auto it = vertexMap.find(vertex); it != vertexMap.end()) { - edm.setStartVertex(it->second); + 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, " @@ -753,9 +745,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, " @@ -769,9 +760,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, " @@ -785,9 +775,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, " @@ -798,9 +787,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(); @@ -811,9 +799,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; @@ -823,9 +810,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; @@ -840,11 +826,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(); @@ -853,9 +840,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; @@ -865,9 +851,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" @@ -902,18 +887,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 " @@ -922,7 +905,8 @@ namespace LCIO2EDM4hepConv { } } - void resolveRelations(LcioEdmTypeMapping& typeMapping) + template + void resolveRelations(ObjectMappingT& typeMapping) { resolveRelationsMCParticles(typeMapping.mcParticles); resolveRelationsRecoParticles( @@ -934,8 +918,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; @@ -1039,8 +1024,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);