Skip to content

Commit

Permalink
Make it possible to use a global map for relation resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Sep 15, 2023
1 parent ec46548 commit 10bde59
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ namespace EDM4hep2LCIOConv {
void convEventHeader(const edm4hep::EventHeaderCollection* const header_coll, lcio::LCEventImpl* const lcio_event);

template<typename ObjectMappingT = CollectionsPairVectors>
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<typename ObjectMappingT = CollectionsPairVectors, typename ObjectMappingU = CollectionsPairVectors>
void FillMissingCollections(ObjectMappingT& update_pairs, const ObjectMappingU& lookup_pairs);

bool collectionExist(const std::string& collection_name, const lcio::LCEventImpl* lcio_event);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,32 +705,37 @@ namespace EDM4hep2LCIOConv {
return mcparticles;
}

template<typename ObjectMappingT>
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<typename ObjectMappingT>
void FillMissingCollections(ObjectMappingT& collection_pairs)
template<typename ObjectMappingT, typename ObjectMappingU>
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());
}
}
}
}

// 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());
}
}
Expand All @@ -740,7 +745,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());
}
}
Expand All @@ -752,7 +757,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());
}
}
Expand All @@ -761,12 +766,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());
}
}
Expand All @@ -778,7 +783,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);
Expand All @@ -795,7 +800,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;
Expand All @@ -813,12 +818,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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ namespace LCIO2EDM4hepConv {
template<typename ObjectMappingT = LcioEdmTypeMapping>
void resolveRelations(ObjectMappingT& typeMapping);

template<typename ObjectMappingT, typename ObjectMappingU>
void resolveRelations(ObjectMappingT& updateMaps, const ObjectMappingU& lookupMaps);

/**
* Convert LCRelation collections into the corresponding Association collections in EDM4hep
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,14 +858,19 @@ namespace LCIO2EDM4hepConv {
template<typename ObjectMappingT>
void resolveRelations(ObjectMappingT& typeMapping)
{
resolveRelationsMCParticles(typeMapping.mcParticles);
resolveRelations(typeMapping, typeMapping);
}

template<typename ObjectMappingT, typename ObjectMappingU>
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<
Expand Down

0 comments on commit 10bde59

Please sign in to comment.