Skip to content

Commit

Permalink
Add conversion of associations to LCRelations (#189)
Browse files Browse the repository at this point in the history
* Add conversion of associations to LCRelations

* Move collection retrieval into separate function
  • Loading branch information
tmadlener committed Jul 19, 2024
1 parent af28b7d commit 5f04132
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion k4MarlinWrapper/k4MarlinWrapper/converters/EDM4hep2Lcio.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class EDM4hep2LcioTool : public GaudiTool, virtual public IEDMConverter {
void convertAdd(const std::string& e4h_coll_name, const std::string& lcio_coll_name, lcio::LCEventImpl* lcio_event,
CollectionPairMappings& collection_pairs,
std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections);
};

/// Get an EDM4hep collection by name, consulting either the podio based data
/// svc or the IOSvc
podio::CollectionBase* getEDM4hepCollection(const std::string& name) const;
};
#endif
35 changes: 27 additions & 8 deletions k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,10 @@ void EDM4hep2LcioTool::convertEventHeader(const std::string& e4h_coll_name, lcio
EDM4hep2LCIOConv::convertEventHeader(header_coll, lcio_event);
}

// 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,
std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections) {
const auto& metadata = m_podioDataSvc->getMetaDataFrame();
podio::CollectionBase* collPtr = nullptr;
podio::CollectionBase* EDM4hep2LcioTool::getEDM4hepCollection(const std::string& collName) const {
podio::CollectionBase* collPtr{nullptr};
DataObject* p;
auto sc = m_podioDataSvc->retrieveObject(e4h_coll_name, p);
auto sc = m_podioDataSvc->retrieveObject(collName, p);
if (sc.isFailure()) {
throw GaudiException("Collection not found", name(), StatusCode::FAILURE);
}
Expand All @@ -304,7 +300,17 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
collPtr = dynamic_cast<podio::CollectionBase*>(ptr->getData().get());
}
}
const auto fulltype = collPtr->getValueTypeName();

return collPtr;
}

// 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,
std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections) {
const auto& metadata = m_podioDataSvc->getMetaDataFrame();
const auto collPtr = getEDM4hepCollection(e4h_coll_name);
const auto fulltype = collPtr->getValueTypeName();

debug() << "Converting type " << fulltype << " from input " << e4h_coll_name << endmsg;

Expand Down Expand Up @@ -373,7 +379,15 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
CollectionPairMappings collection_pairs{};
std::vector<EDM4hep2LCIOConv::ParticleIDConvData> pidCollections{};

std::vector<std::tuple<std::string, const podio::CollectionBase*>> associations{};

for (const auto& [edm4hepName, lcioName] : collsToConvert) {
const auto coll = getEDM4hepCollection(edm4hepName);
if (coll->getTypeName().find("Association") != std::string_view::npos) {
debug() << edm4hepName << " is an association collection, converting it later" << endmsg;
associations.emplace_back(lcioName, coll);
continue;
}
debug() << "Converting collection " << edm4hepName << " (storing it as " << lcioName << ")" << endmsg;
if (!EDM4hep2LCIOConv::collectionExist(lcioName, lcio_event)) {
convertAdd(edm4hepName, lcioName, lcio_event, collection_pairs, pidCollections);
Expand Down Expand Up @@ -413,5 +427,10 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {

EDM4hep2LCIOConv::resolveRelations(collection_pairs, globalObjMap);

// Now we can convert the assocations and add them to the event
for (auto& [name, coll] : EDM4hep2LCIOConv::createLCRelationCollections(associations, globalObjMap)) {
lcio_event->addCollection(coll.release(), name);
}

return StatusCode::SUCCESS;
}

0 comments on commit 5f04132

Please sign in to comment.