Skip to content

Commit

Permalink
Switch from Association to Link and use non-deprecated methods (#86)
Browse files Browse the repository at this point in the history
* Move to non-deprecated link types

* Use Link also in type name checks

* Rename Association to Link in function names and docstrings

* Switch to non-deprecated methods
  • Loading branch information
tmadlener authored Jul 31, 2024
1 parent 7ef18e3 commit d3b531d
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

// EDM4hep
#include <edm4hep/CaloHitContributionCollection.h>
#include <edm4hep/CaloHitMCParticleLinkCollection.h>
#include <edm4hep/CaloHitSimCaloHitLinkCollection.h>
#include <edm4hep/CalorimeterHitCollection.h>
#include <edm4hep/ClusterCollection.h>
#include <edm4hep/EventHeaderCollection.h>
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/MCRecoCaloAssociationCollection.h>
#include <edm4hep/MCRecoCaloParticleAssociationCollection.h>
#include <edm4hep/MCRecoParticleAssociationCollection.h>
#include <edm4hep/MCRecoTrackParticleAssociationCollection.h>
#include <edm4hep/MCRecoTrackerAssociationCollection.h>
#include <edm4hep/ParticleIDCollection.h>
#include <edm4hep/RawCalorimeterHitCollection.h>
#include <edm4hep/RawTimeSeriesCollection.h>
#include <edm4hep/RecDqdxCollection.h>
#include <edm4hep/RecoParticleVertexAssociationCollection.h>
#include <edm4hep/RecoMCParticleLinkCollection.h>
#include <edm4hep/ReconstructedParticleCollection.h>
#include <edm4hep/SimCalorimeterHitCollection.h>
#include <edm4hep/SimTrackerHitCollection.h>
#include <edm4hep/TrackCollection.h>
#include <edm4hep/TrackMCParticleLinkCollection.h>
#include <edm4hep/TrackerHitSimTrackerHitLinkCollection.h>
#include <edm4hep/VertexRecoParticleLinkCollection.h>
#if __has_include("edm4hep/TrackerHit3DCollection.h")
#include "edm4hep/TrackerHit3DCollection.h"
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "k4EDM4hep2LcioConv/MappingUtils.h"

#include <edm4hep/MCRecoCaloAssociationCollection.h>
#include <edm4hep/MCRecoCaloParticleAssociationCollection.h>
#include <edm4hep/MCRecoClusterParticleAssociationCollection.h>
#include <edm4hep/MCRecoParticleAssociationCollection.h>
#include <edm4hep/MCRecoTrackParticleAssociationCollection.h>
#include <edm4hep/MCRecoTrackerAssociationCollection.h>
#include <edm4hep/RecoParticleVertexAssociationCollection.h>
#include <edm4hep/CaloHitMCParticleLinkCollection.h>
#include <edm4hep/CaloHitSimCaloHitLinkCollection.h>
#include <edm4hep/ClusterMCParticleLinkCollection.h>
#include <edm4hep/RecoMCParticleLinkCollection.h>
#include <edm4hep/TrackMCParticleLinkCollection.h>
#include <edm4hep/TrackerHitSimTrackerHitLinkCollection.h>
#include <edm4hep/VertexRecoParticleLinkCollection.h>

#include <UTIL/LCRelationNavigator.h>

Expand Down Expand Up @@ -786,26 +786,26 @@ std::vector<std::tuple<std::string, std::unique_ptr<lcio::LCCollection>>> create
relationColls.reserve(associationCollections.size());

for (const auto& [name, coll] : associationCollections) {
if (const auto assocs = dynamic_cast<const edm4hep::MCRecoParticleAssociationCollection*>(coll)) {
if (const auto assocs = dynamic_cast<const edm4hep::RecoMCParticleLinkCollection*>(coll)) {
relationColls.emplace_back(name,
createLCRelationCollection(*assocs, objectMaps.recoParticles, objectMaps.mcParticles));
} else if (const auto assocs = dynamic_cast<const edm4hep::MCRecoCaloAssociationCollection*>(coll)) {
} else if (const auto assocs = dynamic_cast<const edm4hep::CaloHitSimCaloHitLinkCollection*>(coll)) {
relationColls.emplace_back(name,
createLCRelationCollection(*assocs, objectMaps.caloHits, objectMaps.simCaloHits));
} else if (const auto assocs = dynamic_cast<const edm4hep::MCRecoTrackerAssociationCollection*>(coll)) {
} else if (const auto assocs = dynamic_cast<const edm4hep::TrackerHitSimTrackerHitLinkCollection*>(coll)) {
relationColls.emplace_back(
name, createLCRelationCollection(*assocs, objectMaps.trackerHits, objectMaps.simTrackerHits));
} else if (const auto assocs = dynamic_cast<const edm4hep::MCRecoCaloParticleAssociationCollection*>(coll)) {
} else if (const auto assocs = dynamic_cast<const edm4hep::CaloHitMCParticleLinkCollection*>(coll)) {
relationColls.emplace_back(name,
createLCRelationCollection(*assocs, objectMaps.caloHits, objectMaps.mcParticles));
} else if (const auto assocs = dynamic_cast<const edm4hep::MCRecoClusterParticleAssociationCollection*>(coll)) {
} else if (const auto assocs = dynamic_cast<const edm4hep::ClusterMCParticleLinkCollection*>(coll)) {
relationColls.emplace_back(name,
createLCRelationCollection(*assocs, objectMaps.clusters, objectMaps.mcParticles));
} else if (const auto assocs = dynamic_cast<const edm4hep::MCRecoTrackParticleAssociationCollection*>(coll)) {
} else if (const auto assocs = dynamic_cast<const edm4hep::TrackMCParticleLinkCollection*>(coll)) {
relationColls.emplace_back(name, createLCRelationCollection(*assocs, objectMaps.tracks, objectMaps.mcParticles));
} else if (const auto assocs = dynamic_cast<const edm4hep::RecoParticleVertexAssociationCollection*>(coll)) {
} else if (const auto assocs = dynamic_cast<const edm4hep::VertexRecoParticleLinkCollection*>(coll)) {
relationColls.emplace_back(name,
createLCRelationCollection(*assocs, objectMaps.recoParticles, objectMaps.vertices));
createLCRelationCollection(*assocs, objectMaps.vertices, objectMaps.recoParticles));
} else {
std::cerr << "Trying to create an LCRelation collection from a " << coll->getTypeName()
<< " which is not supported" << std::endl;
Expand Down Expand Up @@ -856,7 +856,7 @@ std::unique_ptr<lcio::LCCollection> createLCRelationCollection(const AssocCollT&
auto lcioRel = new lcio::LCRelationImpl{};
lcioRel->setWeight(assoc.getWeight());

const auto edm4hepFrom = assoc.getRec();
const auto edm4hepFrom = assoc.getFrom();
const auto lcioFrom = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm4hepFrom, fromMap);
if (lcioFrom) {
lcioRel->setFrom(lcioFrom.value());
Expand All @@ -865,24 +865,13 @@ std::unique_ptr<lcio::LCCollection> createLCRelationCollection(const AssocCollT&
<< std::endl;
}

if constexpr (std::is_same_v<AssocCollT, edm4hep::RecoParticleVertexAssociationCollection>) {
const auto edm4hepTo = assoc.getVertex();
const auto lcioTo = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm4hepTo, toMap);
if (lcioTo) {
lcioRel->setTo(lcioTo.value());
} else {
std::cerr << "Cannot find an objects for building an LCRelation of type " << detail::getTypeName<ToLCIOT>()
<< std::endl;
}
const auto edm4hepTo = assoc.getTo();
const auto lcioTo = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm4hepTo, toMap);
if (lcioTo) {
lcioRel->setTo(lcioTo.value());
} else {
const auto edm4hepTo = assoc.getSim();
const auto lcioTo = k4EDM4hep2LcioConv::detail::mapLookupFrom(edm4hepTo, toMap);
if (lcioTo) {
lcioRel->setTo(lcioTo.value());
} else {
std::cerr << "Cannot find an objects for building an LCRelation of type " << detail::getTypeName<ToLCIOT>()
<< std::endl;
}
std::cerr << "Cannot find an objects for building an LCRelation of type " << detail::getTypeName<ToLCIOT>()
<< std::endl;
}

lcioColl->addElement(lcioRel);
Expand Down
54 changes: 35 additions & 19 deletions k4EDM4hep2LcioConv/include/k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

// EDM4hep
#include "edm4hep/CaloHitContributionCollection.h"
#include "edm4hep/CaloHitMCParticleLinkCollection.h"
#include "edm4hep/CaloHitSimCaloHitLinkCollection.h"
#include "edm4hep/CalorimeterHitCollection.h"
#include "edm4hep/ClusterCollection.h"
#include "edm4hep/ClusterMCParticleLinkCollection.h"
#include "edm4hep/EventHeaderCollection.h"
#include "edm4hep/MCParticleCollection.h"
#include "edm4hep/MCRecoCaloAssociationCollection.h"
#include "edm4hep/MCRecoCaloParticleAssociationCollection.h"
#include "edm4hep/MCRecoClusterParticleAssociationCollection.h"
#include "edm4hep/MCRecoParticleAssociationCollection.h"
#include "edm4hep/MCRecoTrackParticleAssociationCollection.h"
#include "edm4hep/MCRecoTrackerAssociationCollection.h"
#include "edm4hep/ParticleIDCollection.h"
#include "edm4hep/RawCalorimeterHitCollection.h"
#include "edm4hep/RawTimeSeriesCollection.h"
#include "edm4hep/RecoParticleVertexAssociationCollection.h"
#include "edm4hep/RecoMCParticleLinkCollection.h"
#include "edm4hep/ReconstructedParticleCollection.h"
#include "edm4hep/SimCalorimeterHitCollection.h"
#include "edm4hep/SimTrackerHitCollection.h"
#include "edm4hep/TrackCollection.h"
#include "edm4hep/TrackMCParticleLinkCollection.h"
#include "edm4hep/TrackerHitSimTrackerHitLinkCollection.h"
#include "edm4hep/VertexRecoParticleLinkCollection.h"
#if __has_include("edm4hep/TrackerHit3DCollection.h")
#include "edm4hep/TrackerHit3DCollection.h"
#else
Expand Down Expand Up @@ -138,13 +138,19 @@ template <typename ObjectMappingT, typename ObjectMappingU>
void resolveRelations(ObjectMappingT& updateMaps, const ObjectMappingU& lookupMaps);

/**
* Convert LCRelation collections into the corresponding Association collections
* in EDM4hep
* Convert LCRelation collections into the corresponding Link collections in
* EDM4hep
*/
template <typename ObjectMappingT>
std::vector<CollNamePair>
std::vector<CollNamePair> createLinks(const ObjectMappingT& typeMapping,
const std::vector<std::pair<std::string, EVENT::LCCollection*>>& LCRelation);

template <typename ObjectMappingT>
[[deprecated("use createLinks instead")]] std::vector<CollNamePair>
createAssociations(const ObjectMappingT& typeMapping,
const std::vector<std::pair<std::string, EVENT::LCCollection*>>& LCRelation);
const std::vector<std::pair<std::string, EVENT::LCCollection*>>& LCRelation) {
return createLinks(typeMapping, LCRelation);
}

/**
* Convert a subset collection, dispatching to the correct function for the
Expand Down Expand Up @@ -226,7 +232,7 @@ convertMCParticles(const std::string& name, EVENT::LCCollection* LCCollection, M
* part of the ReconstructedParticles in LCIO. The name of this collection is
* <name>_PID_<pid_algo_name> (see getPIDCollName)
*
* @note: Also populates one (partially filled) RecoParticleVertexAssociation
* @note: Also populates one (partially filled) VertexRecoParticleLink
* collection for keeping the startVertex information
*/
template <typename RecoMapT>
Expand All @@ -237,7 +243,7 @@ std::vector<CollNamePair> convertReconstructedParticles(const std::string& name,
* Convert a Vertex collection and return the resulting collection.
* Simultaneously populates the mapping from LCIO to EDM4hep objects.
*
* @note: Also creates a (partially filled) RecoParticleVertexAssociation
* @note: Also creates a (partially filled) VertexRecoParticleLink
* collection for keeping the associatedParticle information
*/
template <typename VertexMapT>
Expand Down Expand Up @@ -336,10 +342,10 @@ template <typename CollT, typename ObjectMapT,
auto handleSubsetColl(EVENT::LCCollection* lcioColl, const ObjectMapT& elemMap);

/**
* Create an Association collection from an LCRelations collection. Templated
* Create an Link collection from an LCRelations collection. Templated
* on the From and To types as well as the direction of the relations in the
* input LCRelations collection with respect to the order in which they are
* mentioned in the Association collection of EDM4hep (since those are not
* mentioned in the Link collection of EDM4hep (since those are not
* directed).
*
* Necessary inputs apart from the LCRelations collection are the correct LCIO
Expand All @@ -350,8 +356,18 @@ template <typename CollT, bool Reverse, typename FromMapT, typename ToMapT,
typename ToLCIOT = std::remove_pointer_t<k4EDM4hep2LcioConv::detail::key_t<ToMapT>>,
typename FromEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t<FromMapT>,
typename ToEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t<ToMapT>>
std::unique_ptr<CollT> createAssociationCollection(EVENT::LCCollection* relations, const FromMapT& fromMap,
const ToMapT& toMap);
std::unique_ptr<CollT> createLinkCollection(EVENT::LCCollection* relations, const FromMapT& fromMap,
const ToMapT& toMap);

template <typename CollT, bool Reverse, typename FromMapT, typename ToMapT,
typename FromLCIOT = std::remove_pointer_t<k4EDM4hep2LcioConv::detail::key_t<FromMapT>>,
typename ToLCIOT = std::remove_pointer_t<k4EDM4hep2LcioConv::detail::key_t<ToMapT>>,
typename FromEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t<FromMapT>,
typename ToEDM4hepT = k4EDM4hep2LcioConv::detail::mapped_t<ToMapT>>
[[deprecated("use createLinkCollection instead")]] std::unique_ptr<CollT>
createAssociationCollection(EVENT::LCCollection* relations, const FromMapT& fromMap, const ToMapT& toMap) {
return createLinkCollection<CollT, Reverse>(relations, fromMap, toMap);
}

/**
* Creates the CaloHitContributions for all SimCaloHits.
Expand Down Expand Up @@ -404,8 +420,8 @@ void resolveRelationsVertices(VertexMapT& vertexMap, URecoParticleMapT& updateRP
const LURecoParticleMapT& lookupRPMap);

template <typename VertexMapT, typename RecoParticleMapT>
void finalizeRecoParticleVertexAssociations(edm4hep::RecoParticleVertexAssociationCollection& associations,
const VertexMapT& vertexMap, const RecoParticleMapT& recoParticleMap);
void finalizeVertexRecoParticleLinks(edm4hep::VertexRecoParticleLinkCollection& associations,
const VertexMapT& vertexMap, const RecoParticleMapT& recoParticleMap);

/**
* Go from chi^2 and probability (1 - CDF(chi^2, ndf)) to ndf by a binary search
Expand Down
Loading

0 comments on commit d3b531d

Please sign in to comment.