From cb37738e4cfcda655082b0fe354c3ea615f2eded Mon Sep 17 00:00:00 2001 From: tmadlener Date: Fri, 26 May 2023 17:28:45 +0200 Subject: [PATCH] Use more conversion lib functionality Reduce necessary type information for DataHandles to minimum --- .../k4MarlinWrapper/converters/Lcio2EDM4hep.h | 33 +--- .../src/components/Lcio2EDM4hep.cpp | 178 ++---------------- 2 files changed, 22 insertions(+), 189 deletions(-) diff --git a/k4MarlinWrapper/k4MarlinWrapper/converters/Lcio2EDM4hep.h b/k4MarlinWrapper/k4MarlinWrapper/converters/Lcio2EDM4hep.h index 99f67892..2638230a 100644 --- a/k4MarlinWrapper/k4MarlinWrapper/converters/Lcio2EDM4hep.h +++ b/k4MarlinWrapper/k4MarlinWrapper/converters/Lcio2EDM4hep.h @@ -55,37 +55,10 @@ class Lcio2EDM4hepTool : public GaudiTool, virtual public IEDMConverter { // ********************************** bool collectionExist(const std::string& collection_name); - /** - * Convert the collection data and return the mapping of LCIO to EDM4hep - * objects, the LCRelation collections (and their names) as well as the subset - * collections (with their names and types). These data are necessary for - * resolving relations and creating association collections. - * - * The converted collections are put into the TES (but the contained objects - * have no relations set yet). - */ - std::tuple>, - std::vector>> - convertCollectionData(const std::map& collsToConvert, lcio::LCEventImpl* the_event); - - /** - * Create the subset collections and put them into the TES. - */ - void createSubsetColls(const std::vector>& subsetColls, - const LCIO2EDM4hepConv::LcioEdmTypeMapping& lcio2edm4hepMaps); - - /** - * Create the association collections from the LCRelation collections and put - * them into the TES. - */ - void createAssociations(const std::vector>& lcRelationColls, - const LCIO2EDM4hepConv::LcioEdmTypeMapping& lcio2edm4hepMaps); - /** * Register a collection into the TES. If the lcioColl is not a nullptr also * convert the metadata from the input lcio collection. */ - template void registerCollection(std::tuple> namedColl, EVENT::LCCollection* lcioColl = nullptr); @@ -93,9 +66,9 @@ class Lcio2EDM4hepTool : public GaudiTool, virtual public IEDMConverter { * Register a collection into the TES. If the lcioColl is not a nullptr also * convert the metadata from the input lcio collection. */ - template - void registerCollection(const std::string& name, std::unique_ptr&& coll, EVENT::LCCollection* lcioColl = nullptr) { - registerCollection(std::make_tuple(name, std::move(coll)), lcioColl); + void registerCollection(const std::string& name, std::unique_ptr&& coll, + EVENT::LCCollection* lcioColl = nullptr) { + registerCollection(std::make_tuple(name, std::move(coll)), lcioColl); } }; diff --git a/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp b/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp index 275afc33..b7996376 100644 --- a/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp +++ b/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp @@ -50,7 +50,6 @@ bool Lcio2EDM4hepTool::collectionExist(const std::string& collection_name) { return false; } -template void Lcio2EDM4hepTool::registerCollection( std::tuple> namedColl, EVENT::LCCollection* lcioColl) { auto& [name, e4hColl] = namedColl; @@ -58,8 +57,8 @@ void Lcio2EDM4hepTool::registerCollection( error() << "Could not convert collection " << name << endmsg; return; } - DataHandle handle{name, Gaudi::DataHandle::Writer, this}; - handle.put(static_cast(e4hColl.release())); + DataHandle handle{name, Gaudi::DataHandle::Writer, this}; + handle.put(e4hColl.release()); // Convert metadata if (lcioColl != nullptr) { @@ -85,7 +84,7 @@ void Lcio2EDM4hepTool::registerCollection( StatusCode Lcio2EDM4hepTool::convertCollections(lcio::LCEventImpl* the_event) { // Convert Event Header outside the collections loop if (!collectionExist("EventHeader")) { - registerCollection("EventHeader", LCIO2EDM4hepConv::createEventHeader(the_event)); + registerCollection("EventHeader", LCIO2EDM4hepConv::createEventHeader(the_event)); } // Start off with the pre-defined collection name mappings @@ -99,24 +98,9 @@ StatusCode Lcio2EDM4hepTool::convertCollections(lcio::LCEventImpl* the_event) { } } - auto [lcio2edm4hepMaps, lcRelationColls, subsetColls] = convertCollectionData(collsToConvert, the_event); + auto lcio2edm4hepMaps = LCIO2EDM4hepConv::LcioEdmTypeMapping{}; - // Now we can resolve relations, subset collections and LCRelations - LCIO2EDM4hepConv::resolveRelations(lcio2edm4hepMaps); - createSubsetColls(subsetColls, lcio2edm4hepMaps); - - return StatusCode::SUCCESS; -} - -std::tuple>, - std::vector>> -Lcio2EDM4hepTool::convertCollectionData(const std::map& collsToConvert, - lcio::LCEventImpl* the_event) { - using namespace LCIO2EDM4hepConv; - - auto lcio2edm4hepMaps = LcioEdmTypeMapping{}; - - std::vector> lcRelationColls{}; + std::vector> lcRelationColls{}; std::vector> subsetColls{}; for (const auto& [lcioName, edm4hepName] : collsToConvert) { @@ -131,42 +115,15 @@ Lcio2EDM4hepTool::convertCollectionData(const std::map continue; } if (lcio_coll_type_str == "LCRelation") { - lcRelationColls.emplace_back(std::make_tuple(edm4hepName, lcio_coll)); + lcRelationColls.emplace_back(std::make_pair(edm4hepName, lcio_coll)); } - if (lcio_coll_type_str == "ReconstructedParticle") { - auto e4hColls = convertReconstructedParticles(edm4hepName, lcio_coll, lcio2edm4hepMaps.recoParticles, - lcio2edm4hepMaps.particleIDs); - registerCollection(std::move(e4hColls[0])); - registerCollection(std::move(e4hColls[1])); - } else if (lcio_coll_type_str == "MCParticle") { - registerCollection(edm4hepName, convertMCParticles(edm4hepName, lcio_coll, lcio2edm4hepMaps.mcParticles)); - } else if (lcio_coll_type_str == "Vertex") { - registerCollection(edm4hepName, convertVertices(edm4hepName, lcio_coll, lcio2edm4hepMaps.vertices)); - } else if (lcio_coll_type_str == "Track") { - registerCollection(edm4hepName, convertTracks(edm4hepName, lcio_coll, lcio2edm4hepMaps.tracks)); - } else if (lcio_coll_type_str == "TrackerHit") { - registerCollection(edm4hepName, convertTrackerHits(edm4hepName, lcio_coll, lcio2edm4hepMaps.trackerHits), - lcio_coll); - } else if (lcio_coll_type_str == "TrackerHitPlane") { - registerCollection( - edm4hepName, convertTrackerHitPlanes(edm4hepName, lcio_coll, lcio2edm4hepMaps.trackerHitPlanes), lcio_coll); - } else if (lcio_coll_type_str == "SimTrackerHit") { - registerCollection(edm4hepName, convertSimTrackerHits(edm4hepName, lcio_coll, lcio2edm4hepMaps.simTrackerHits), - lcio_coll); - } else if (lcio_coll_type_str == "SimCalorimeterHit") { - registerCollection(edm4hepName, convertSimCalorimeterHits(edm4hepName, lcio_coll, lcio2edm4hepMaps.simCaloHits), - lcio_coll); - } else if (lcio_coll_type_str == "RawCalorimeterHit") { - registerCollection(edm4hepName, - convertRawCalorimeterHits(edm4hepName, lcio_coll, lcio2edm4hepMaps.rawCaloHits)); - } else if (lcio_coll_type_str == "TPCHit") { - registerCollection(edm4hepName, convertTPCHits(edm4hepName, lcio_coll, lcio2edm4hepMaps.tpcHits)); - } else if (lcio_coll_type_str == "Cluster") { - registerCollection(edm4hepName, convertClusters(edm4hepName, lcio_coll, lcio2edm4hepMaps.clusters)); - // TODO: Particle IDs related to Clusters. Needs converter support! - } else { - error() << lcio_coll_type_str << ": conversion type not supported." << endmsg; + for (auto&& e4hColl : LCIO2EDM4hepConv::convertCollection(edm4hepName, lcio_coll, lcio2edm4hepMaps)) { + if (std::get<1>(e4hColl)) { + registerCollection(std::move(e4hColl)); + } else { + error() << "Could not convert collection " << lcioName << " (type: " << lcio_coll_type_str << ")" << endmsg; + } } } catch (const lcio::DataNotAvailableException& ex) { warning() << "LCIO Collection " << lcioName << " not found in the event, skipping conversion to EDM4hep" @@ -175,113 +132,16 @@ Lcio2EDM4hepTool::convertCollectionData(const std::map } } - return std::make_tuple(std::move(lcio2edm4hepMaps), std::move(lcRelationColls), std::move(subsetColls)); -} - -void Lcio2EDM4hepTool::createSubsetColls( - const std::vector>& subsetColls, - const LCIO2EDM4hepConv::LcioEdmTypeMapping& lcio2edm4hepMaps) { - using namespace LCIO2EDM4hepConv; + // Now we can resolve relations, subset collections and LCRelations + LCIO2EDM4hepConv::resolveRelations(lcio2edm4hepMaps); for (const auto& [name, coll, type] : subsetColls) { - if (type == "MCParticle") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.mcParticles)); - } else if (type == "ReconstructedParticle") { - registerCollection( - name, handleSubsetColl(coll, lcio2edm4hepMaps.recoParticles)); - } else if (type == "Vertex") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.vertices)); - } else if (type == "Track") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.tracks)); - } else if (type == "Cluster") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.clusters)); - } else if (type == "SimCalorimeterHit") { - registerCollection(name, - handleSubsetColl(coll, lcio2edm4hepMaps.simCaloHits)); - } else if (type == "RawCalorimeterHit") { - registerCollection(name, - handleSubsetColl(coll, lcio2edm4hepMaps.rawCaloHits)); - } else if (type == "CalorimeterHit") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.caloHits)); - } else if (type == "SimTrackerHit") { - registerCollection(name, - handleSubsetColl(coll, lcio2edm4hepMaps.simTrackerHits)); - } else if (type == "TPCHit") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.tpcHits)); - } else if (type == "TrackerHit") { - registerCollection(name, handleSubsetColl(coll, lcio2edm4hepMaps.trackerHits)); - } else if (type == "TrackerHitPlane") { - registerCollection(name, - handleSubsetColl(coll, lcio2edm4hepMaps.trackerHitPlanes)); - } else { - error() << type << ": conversion type not supported." << endmsg; - } + registerCollection(name, LCIO2EDM4hepConv::fillSubset(coll, lcio2edm4hepMaps, type)); } -} - -void Lcio2EDM4hepTool::createAssociations( - const std::vector>& lcRelationColls, - const LCIO2EDM4hepConv::LcioEdmTypeMapping& lcio2edm4hepMaps) { - using namespace LCIO2EDM4hepConv; - - for (const auto& [name, lcioColl] : lcRelationColls) { - const auto& params = lcioColl->getParameters(); - const auto& fromType = params.getStringVal("FromType"); - const auto& toType = params.getStringVal("ToType"); - - if (fromType.empty() || toType.empty()) { - error() << "LCRelation collection " << name << " has missing FromType or ToType parameters. " - << "Cannot convert it without this information." << endmsg; - continue; - } - - using namespace LCIO2EDM4hepConv; - if (fromType == "MCParticle" && toType == "ReconstructedParticle") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.mcParticles, lcio2edm4hepMaps.recoParticles)); - } else if (fromType == "ReconstructedParticle" && toType == "MCParticle") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.recoParticles, lcio2edm4hepMaps.mcParticles)); - } else if (fromType == "CalorimeterHit" && toType == "SimCalorimeterHit") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.caloHits, lcio2edm4hepMaps.simCaloHits)); - } else if (fromType == "SimCalorimeterHit" && toType == "CalorimeterHit") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.simCaloHits, lcio2edm4hepMaps.caloHits)); - } else if (fromType == "Cluster" && toType == "MCParticle") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.clusters, lcio2edm4hepMaps.mcParticles)); - } else if (fromType == "MCParticle" && toType == "Cluster") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.mcParticles, lcio2edm4hepMaps.clusters)); - } else if (fromType == "MCParticle" && toType == "Track") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.mcParticles, lcio2edm4hepMaps.tracks)); - } else if (fromType == "Track" && toType == "MCParticle") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.tracks, lcio2edm4hepMaps.mcParticles)); - } else if (fromType == "TrackerHit" && toType == "SimTrackerHit") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.trackerHits, lcio2edm4hepMaps.simTrackerHits)); - } else if (fromType == "SimTrackerHit" && toType == "TrackerHit") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.simTrackerHits, lcio2edm4hepMaps.trackerHits)); - } else if (fromType == "SimTrackerHit" && toType == "TrackerHitPlane") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.simTrackerHits, lcio2edm4hepMaps.trackerHitPlanes)); - } else if (fromType == "TrackerHitPlane" && toType == "SimTrackerHit") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.trackerHitPlanes, lcio2edm4hepMaps.simTrackerHits)); - } else if (fromType == "ReconstructedParticle" && toType == "Vertex") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.recoParticles, lcio2edm4hepMaps.vertices)); - } else if (fromType == "Vertex" && toType == "ReconstructedParticle") { - registerCollection(name, createAssociationCollection( - lcioColl, lcio2edm4hepMaps.vertices, lcio2edm4hepMaps.recoParticles)); - } else { - error() << "Relation from: " << fromType << " to: " << toType << " (" << name - << ") is not beeing handled during creation of associations" << endmsg; - } + for (auto&& assocColl : LCIO2EDM4hepConv::createAssociations(lcio2edm4hepMaps, lcRelationColls)) { + registerCollection(std::move(assocColl)); } + + return StatusCode::SUCCESS; }