diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h index 70110c19..86caa1cd 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.h @@ -64,81 +64,104 @@ 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); + 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 b50c8148..d4eb88fc 100644 --- a/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp +++ b/k4EDM4hep2LcioConv/src/k4EDM4hep2LcioConv.cpp @@ -21,10 +21,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); @@ -105,7 +106,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); @@ -117,8 +118,9 @@ 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)) { - lcio_tr->addTrack(lcio_tr_linked.value()); + for (const auto& [lcio_tr_linked, edm_tr_linked] : tracks_vec) { + if (const auto lcio_tr_linked = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_linked_tr, tracks_vec)) + lcio_tr->addTrack(lcio_tr_linked.value()); } } } @@ -130,10 +132,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); @@ -164,7 +167,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); @@ -177,11 +180,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); @@ -213,7 +217,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 { @@ -223,7 +227,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); @@ -236,10 +240,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); @@ -267,7 +273,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); @@ -280,9 +286,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); @@ -298,7 +305,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); @@ -311,11 +318,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); @@ -340,7 +348,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); @@ -353,9 +361,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); @@ -385,7 +394,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); @@ -398,10 +407,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); @@ -452,7 +462,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); @@ -460,14 +470,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()); } } } @@ -479,10 +488,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); @@ -508,13 +518,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); @@ -527,12 +536,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); @@ -593,7 +603,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); } } @@ -605,7 +614,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); } } @@ -618,14 +626,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); @@ -650,9 +657,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); @@ -693,7 +701,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); @@ -702,11 +710,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()); } } @@ -733,7 +741,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) { @@ -785,7 +794,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(); @@ -814,7 +823,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 @@ -822,19 +831,19 @@ 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; - } - } - } // SimCaloHit + // 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 + } // SimCaloHit // Fill missing SimTrackerHit collections for (auto& [lcio_strh, edm_strh] : collection_pairs.simtrackerhits) { @@ -861,5 +870,4 @@ namespace EDM4hep2LCIOConv { } return false; } - } // namespace EDM4hep2LCIOConv