Skip to content

Commit

Permalink
Add ParticleID metadata conversion from EDM4hep to LCIO
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Apr 15, 2024
1 parent 303860b commit 010ab67
Show file tree
Hide file tree
Showing 2 changed files with 38 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

0 comments on commit 010ab67

Please sign in to comment.