Skip to content

Commit

Permalink
Adapt the on-the-fly conversion to deal with reversed ParticleID rela…
Browse files Browse the repository at this point in the history
…tions (#181)

* Add ParticleID meta information to EDM4hep from LCIO

* Add ParticleID metadata conversion from EDM4hep to LCIO
  • Loading branch information
tmadlener authored May 1, 2024
1 parent 05ba104 commit 0493cfd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
6 changes: 5 additions & 1 deletion k4MarlinWrapper/k4MarlinWrapper/converters/EDM4hep2Lcio.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ using RawCaloHitMap = ObjMapT<lcio::RawCalorimeterHitImpl*, edm4hep::RawCalor
using TPCHitMap = ObjMapT<lcio::TPCHitImpl*, edm4hep::RawTimeSeries>;
using RecoParticleMap = ObjMapT<lcio::ReconstructedParticleImpl*, edm4hep::ReconstructedParticle>;
using MCParticleMap = ObjMapT<lcio::MCParticleImpl*, edm4hep::MCParticle>;
using ParticleIDMap = ObjMapT<lcio::ParticleIDImpl*, edm4hep::ParticleID>;

struct CollectionPairMappings;

Expand Down Expand Up @@ -99,13 +100,16 @@ class EDM4hep2LcioTool : public GaudiTool, virtual public IEDMConverter {
void convertReconstructedParticles(RecoParticleMap& recoparticles_vec, const std::string& e4h_coll_name,
const std::string& lcio_coll_name, lcio::LCEventImpl* lcio_event);

void convertParticleIDs(ParticleIDMap& pidMap, const std::string& e4h_coll_name, int32_t algoId);

void convertMCParticles(MCParticleMap& mc_particles_vec, const std::string& e4h_coll_name,
const std::string& lcio_coll_name, lcio::LCEventImpl* lcio_event);

void convertEventHeader(const std::string& e4h_coll_name, lcio::LCEventImpl* lcio_event);

void convertAdd(const std::string& e4h_coll_name, const std::string& lcio_coll_name, lcio::LCEventImpl* lcio_event,
CollectionPairMappings& collection_pairs);
CollectionPairMappings& collection_pairs,
std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections);
};

#endif
39 changes: 33 additions & 6 deletions k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct CollectionPairMappings {
VertexMap vertices{};
RecoParticleMap recoParticles{};
MCParticleMap mcParticles{};
ParticleIDMap particleIDs{};
};

EDM4hep2LcioTool::EDM4hep2LcioTool(const std::string& type, const std::string& name, const IInterface* parent)
Expand Down Expand Up @@ -100,6 +101,12 @@ void EDM4hep2LcioTool::convertTrackerHits(TrackerHitMap& trackerhits_vec, const
lcio_event->addCollection(conv_trackerhits.release(), lcio_coll_name);
}

void EDM4hep2LcioTool::convertParticleIDs(ParticleIDMap& pidMap, const std::string& e4h_coll_name, int32_t algoId) {
DataHandle<edm4hep::ParticleIDCollection> pidHandle{e4h_coll_name, Gaudi::DataHandle::Reader, this};

EDM4hep2LCIOConv::convertParticleIDs(pidHandle.get(), pidMap, algoId);
}

// Convert EDM4hep SimTrackerHits to LCIO
// Add converted LCIO ptr and original EDM4hep collection to vector of pairs
// Add LCIO Collection Vector to LCIO event
Expand Down Expand Up @@ -254,8 +261,10 @@ void EDM4hep2LcioTool::convertEventHeader(const std::string& e4h_coll_name, lcio

// Select the appropiate method to convert a collection given its type
void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::string& lcio_coll_name,
lcio::LCEventImpl* lcio_event, CollectionPairMappings& collection_pairs) {
lcio::LCEventImpl* lcio_event, CollectionPairMappings& collection_pairs,
std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections) {
const auto& evtFrame = m_podioDataSvc->getEventFrame();
const auto& metadata = m_podioDataSvc->getMetaDataFrame();
const auto collPtr = evtFrame.get(e4h_coll_name);
if (!collPtr) {
error() << "No collection with name: " << e4h_coll_name << " available for conversion" << endmsg;
Expand Down Expand Up @@ -288,7 +297,12 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
convertReconstructedParticles(collection_pairs.recoParticles, e4h_coll_name, lcio_coll_name, lcio_event);
} else if (fulltype == "edm4hep::EventHeader") {
convertEventHeader(e4h_coll_name, lcio_event);
} else if (fulltype == "edm4hep::CaloHitContribution") {
} else if (fulltype == "edm4hep::ParticleID") {
pidCollections.emplace_back(e4h_coll_name, static_cast<const edm4hep::ParticleIDCollection*>(collPtr),
edm4hep::utils::PIDHandler::getAlgoInfo(metadata, e4h_coll_name));
}

else if (fulltype == "edm4hep::CaloHitContribution") {
debug() << "CaloHitContribution collection cannot be converted standalone. "
<< "SimCalorimeterHit collection to be converted in order to be able to attach to them" << endmsg;
} else {
Expand All @@ -304,7 +318,8 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
// Parse property parameters and convert the indicated collections.
// Use the collection names in the parameters to read and write them
StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
const auto collections = m_podioDataSvc->getEventFrame().getAvailableCollections();
const auto& edmEvent = m_podioDataSvc->getEventFrame();
const auto collections = edmEvent.getAvailableCollections();
// Start off with the pre-defined collection name mappings
auto collsToConvert{m_collNames.value()};
// We *always* want to convert the EventHeader
Expand All @@ -318,18 +333,30 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
}
}

CollectionPairMappings collection_pairs{};
CollectionPairMappings collection_pairs{};
std::vector<EDM4hep2LCIOConv::ParticleIDConvData> pidCollections{};

for (const auto& [edm4hepName, lcioName] : collsToConvert) {
debug() << "Converting collection " << edm4hepName << " (storing it as " << lcioName << ")" << endmsg;
if (!EDM4hep2LCIOConv::collectionExist(lcioName, lcio_event)) {
convertAdd(edm4hepName, lcioName, lcio_event, collection_pairs);
convertAdd(edm4hepName, lcioName, lcio_event, collection_pairs, pidCollections);
} else {
debug() << " Collection " << lcioName << " already in place, skipping conversion. " << endmsg;
}
}

debug() << "Event: " << lcio_event->getEventNumber() << " Run: " << lcio_event->getRunNumber() << endmsg;

// Deal with
EDM4hep2LCIOConv::sortParticleIDs(pidCollections);
for (const auto& pidCollMeta : pidCollections) {
const auto algoId = attachParticleIDMetaData(lcio_event, edmEvent, pidCollMeta);
if (!algoId.has_value()) {
warning() << "Could not determine algorithm type for ParticleID collection " << pidCollMeta.name
<< " for setting consistent metadata" << endmsg;
}
convertParticleIDs(collection_pairs.particleIDs, pidCollMeta.name, algoId.value_or(-1));
}

// We want one "global" map that is created the first time it is use in the
// event.
//
Expand Down
17 changes: 17 additions & 0 deletions k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "k4EDM4hep2LcioConv/k4Lcio2EDM4hepConv.h"

#include <edm4hep/utils/ParticleIDUtils.h>

#include <k4FWCore/DataHandle.h>
#include <k4FWCore/MetaDataHandle.h>

Expand Down Expand Up @@ -154,6 +156,8 @@ StatusCode Lcio2EDM4hepTool::convertCollections(lcio::LCEventImpl* the_event) {
// were empty
bool needCaloHitContribs = false;

std::map<std::string, edm4hep::utils::ParticleIDMeta> pidInfos{};

for (const auto& [lcioName, edm4hepName] : collsToConvert) {
try {
auto* lcio_coll = the_event->getCollection(lcioName);
Expand All @@ -174,6 +178,13 @@ StatusCode Lcio2EDM4hepTool::convertCollections(lcio::LCEventImpl* the_event) {
if (lcio_coll_type_str == "LCRelation") {
lcRelationColls.emplace_back(std::make_pair(edm4hepName, lcio_coll));
}
if (lcio_coll_type_str == "ReconstructedParticle") {
// Collect the ParticleID meta information because that has to go to the
// ParticleID collections
for (const auto& pidInfo : LCIO2EDM4hepConv::getPIDMetaInfo(lcio_coll)) {
pidInfos.try_emplace(LCIO2EDM4hepConv::getPIDCollName(lcioName, pidInfo.algoName), pidInfo);
}
}

needCaloHitContribs = (lcio_coll_type_str == "SimCalorimeterHit") && !lcio_coll->isSubset();

Expand All @@ -191,6 +202,12 @@ StatusCode Lcio2EDM4hepTool::convertCollections(lcio::LCEventImpl* the_event) {
}
}

// Set the ParticleID meta information
auto& metadataFrame = m_podioDataSvc->getMetaDataFrame();
for (const auto& [collName, pidInfo] : pidInfos) {
edm4hep::utils::PIDHandler::setAlgoInfo(metadataFrame, collName, pidInfo);
}

// We want one "global" map that is created the first time it is use in the
// event.
//
Expand Down

0 comments on commit 0493cfd

Please sign in to comment.