From 715bc622aa7e88f4b89d443a008587eafe4b1e46 Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 23 Feb 2024 16:06:19 +0100 Subject: [PATCH] Convert all TrackerHit types (#49) * Also attach TrackerHitPlane to Tracks * Make sure to also TrackerHits correctly from EDM4hep to LCIO --- .../k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp | 5 ++ .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h | 6 +- .../k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp | 60 +++++++------------ tests/src/CompareEDM4hepLCIO.cc | 13 +++- tests/src/EDM4hep2LCIOUtilities.cc | 3 + 5 files changed, 44 insertions(+), 43 deletions(-) diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp index 065eb1ff..306e204c 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4EDM4hep2LcioConv.ipp @@ -734,6 +734,11 @@ namespace EDM4hep2LCIOConv { if (const auto lcio_trh = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, lookup_pairs.trackerHits)) { lcio_tr->addHit(lcio_trh.value()); } + else if ( + const auto lcio_trh = + k4EDM4hep2LcioConv::detail::mapLookupFrom(edm_tr_trh, lookup_pairs.trackerHitPlanes)) { + lcio_tr->addHit(lcio_trh.value()); + } } } } diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h index 2b92617c..8a415536 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h @@ -388,12 +388,12 @@ namespace LCIO2EDM4hepConv { /** * Resolve the relations for Tracks */ - template + template void resolveRelationsTracks( TrackMapT& tracksMap, const TrackHitMapT& trackerHitMap, - const TPCHitMapT&, - const THPlaneHitMapT&); + const THPlaneHitMapT&, + const TPCHitMapT&); /** * Resolve the relations for Vertices diff --git a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp index 0a0babbc..b8f3b596 100644 --- a/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp +++ b/k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.ipp @@ -782,16 +782,15 @@ namespace LCIO2EDM4hepConv { } } - template + template void resolveRelationsTracks( TrackMapT& tracksMap, const TrackHitMapT& trackerHitMap, - const TPCHitMapT&, - const THPlaneHitMapT&) + const THPlaneHitMapT& trackerHitPlaneMap, + const TPCHitMapT&) { for (auto& [lcio, edm] : tracksMap) { auto tracks = lcio->getTracks(); - auto trackerHits = lcio->getTrackerHits(); for (auto t : tracks) { if (t == nullptr) { continue; @@ -803,42 +802,29 @@ namespace LCIO2EDM4hepConv { // std::cerr << "Couldn't find tracks to add to Tracks Relations in edm" << std::endl; } } - for (auto th : trackerHits) { + const auto trackerHits = lcio->getTrackerHits(); + for (const auto th : trackerHits) { + bool found = false; if (th == nullptr) { continue; } - if (const auto trHit = k4EDM4hep2LcioConv::detail::mapLookupTo(th, trackerHitMap)) { - edm.addToTrackerHits(trHit.value()); + if (const auto typedTH = dynamic_cast(th)) { + if (const auto trHit = k4EDM4hep2LcioConv::detail::mapLookupTo(typedTH, trackerHitPlaneMap)) { + edm.addToTrackerHits(trHit.value()); + found = true; + } + } + else if (auto typedTH = dynamic_cast(th)) { + if (const auto trHit = k4EDM4hep2LcioConv::detail::mapLookupTo(typedTH, trackerHitMap)) { + edm.addToTrackerHits(trHit.value()); + found = true; + } + } + + if (!found) { + std::cerr << "Couldn't find a edm4hep TrackerHit for an LCIO TrackerHit when resolving " + << "relations for a Track" << std::endl; } - // 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; - // } - // } - // } } } } @@ -875,7 +861,7 @@ namespace LCIO2EDM4hepConv { 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); + resolveRelationsTracks(updateMaps.tracks, lookupMaps.trackerHits, lookupMaps.trackerHitPlanes, lookupMaps.tpcHits); resolveRelationsVertices(updateMaps.vertices, lookupMaps.recoParticles); } diff --git a/tests/src/CompareEDM4hepLCIO.cc b/tests/src/CompareEDM4hepLCIO.cc index 0b86d8d8..0d17ccd7 100644 --- a/tests/src/CompareEDM4hepLCIO.cc +++ b/tests/src/CompareEDM4hepLCIO.cc @@ -385,12 +385,19 @@ bool compare(const EVENT::Track* lcioElem, const edm4hep::Track& edm4hepElem, co } } + const auto& lcioHits = lcioElem->getTrackerHits(); const auto edmHits = edm4hepElem.getTrackerHits(); + ASSERT_COMPARE_VALS(lcioHits.size(), edmHits.size(), "number of tracker hits in Track"); int iHit = 0; for (const auto* lcioHit : lcioElem->getTrackerHits()) { - // In EDM4hep only TrackerHits can be used in Tracks, so here we also only - // compare those - if (dynamic_cast(lcioHit)) { + if (const auto typedHit = dynamic_cast(lcioHit)) { + if (!compareRelation( + typedHit, edmHits[iHit], objectMaps.trackerHitPlanes, "TrackerHit " + std::to_string(iHit) + " in Track")) { + return false; + } + iHit++; + } + else if (dynamic_cast(lcioHit)) { if (!compareRelation( lcioHit, edmHits[iHit], objectMaps.trackerHits, "TrackerHit " + std::to_string(iHit) + " in Track")) { return false; diff --git a/tests/src/EDM4hep2LCIOUtilities.cc b/tests/src/EDM4hep2LCIOUtilities.cc index 6f09e6b4..9a67f18b 100644 --- a/tests/src/EDM4hep2LCIOUtilities.cc +++ b/tests/src/EDM4hep2LCIOUtilities.cc @@ -195,6 +195,7 @@ edm4hep::TrackCollection createTracks( const int subdetectorhitnumbers, const int num_track_states, const edm4hep::TrackerHit3DCollection& trackerHits, + const edm4hep::TrackerHitPlaneCollection& trackerHitPlanes, const std::vector& link_trackerhit_idcs, const std::vector& track_link_tracks_idcs) { @@ -220,6 +221,7 @@ edm4hep::TrackCollection createTracks( for (auto& idx : link_trackerhit_idcs) { elem.addToTrackerHits(trackerHits[idx]); + elem.addToTrackerHits(trackerHitPlanes[idx]); } for (int j = 0; j < num_track_states; ++j) { @@ -342,6 +344,7 @@ podio::Frame createExampleEvent() test_config::nSubdetectorHitNumbers, test_config::nTrackStates, trackerHits, + trackerHitPlanes, test_config::trackTrackerHitIdcs, test_config::trackTrackIdcs), "tracks");