diff --git a/k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp b/k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp index eaa0ee8f..fc927e39 100644 --- a/k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp +++ b/k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp @@ -22,12 +22,20 @@ #include "k4FWCore/DataHandle.h" #include "k4FWCore/MetaDataHandle.h" +#include "GaudiKernel/AnyDataWrapper.h" + +#include + DECLARE_COMPONENT(EDM4hep2LcioTool); #ifdef EDM4HEP2LCIOCONV_NAMESPACE using namespace EDM4hep2LCIOConv; #endif +using namespace k4MarlinWrapper; + +using GlobalMapWrapper = AnyDataWrapper; + struct CollectionPairMappings { TrackMap tracks{}; TrackerHitMap trackerHits{}; @@ -327,9 +335,24 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) { debug() << "Event: " << lcio_event->getEventNumber() << " Run: " << lcio_event->getRunNumber() << endmsg; - k4MarlinWrapper::GlobalConvertedObjectsMap::update(collection_pairs); + // We want one "global" map that is created the first time it is use in the + // event. + // + // Technically getOrCreate is a thing in GaudiTool but that doesn't seem to + // easily work with the AnyDataWrapper we want to use here. So doing the two + // step process here + if (!exist(GlobalConvertedObjectsMap::TESpath.data())) { + debug() << "Creating GlobalconvertedObjectsMap for this event since it is not already in the EventStore" << endmsg; + auto globalObjMapWrapper = std::make_unique(GlobalConvertedObjectsMap{}); + put(std::move(globalObjMapWrapper), GlobalConvertedObjectsMap::TESpath.data()); + } + + auto globalObjMapWrapper = get(GlobalConvertedObjectsMap::TESpath.data()); + auto& globalObjMap = globalObjMapWrapper->getData(); + + globalObjMap.update(collection_pairs); - FillMissingCollections(collection_pairs, k4MarlinWrapper::GlobalConvertedObjectsMap::get()); + FillMissingCollections(collection_pairs, globalObjMap); return StatusCode::SUCCESS; } diff --git a/k4MarlinWrapper/src/components/GlobalConvertedObjectsMap.h b/k4MarlinWrapper/src/components/GlobalConvertedObjectsMap.h index 10621ab0..3e75f8f0 100644 --- a/k4MarlinWrapper/src/components/GlobalConvertedObjectsMap.h +++ b/k4MarlinWrapper/src/components/GlobalConvertedObjectsMap.h @@ -21,6 +21,8 @@ #include "k4EDM4hep2LcioConv/MappingUtils.h" +#include + namespace EVENT { class Track; class TrackerHit; @@ -91,43 +93,31 @@ namespace k4MarlinWrapper { ObjectMapT trackerHitPlanes{}; ObjectMapT particleIDs{}; - /** - * Get the singleton instance - */ - static GlobalConvertedObjectsMap& get() { - static GlobalConvertedObjectsMap globalMap{}; - return globalMap; - } - - // TODO: clear + constexpr static auto TESpath = std::string_view{"/Event/EDMConvGlobalObjMap"}; /** * Update the map with contents from one converter run */ - template static void update(const ObjectMap& localMap) { - auto& globalMap = get(); - updateMap(globalMap.tracks, localMap.tracks); - updateMap(globalMap.trackerHits, localMap.trackerHits); - updateMap(globalMap.simTrackerHits, localMap.simTrackerHits); - updateMap(globalMap.caloHits, localMap.caloHits); - updateMap(globalMap.rawCaloHits, localMap.rawCaloHits); - updateMap(globalMap.simCaloHits, localMap.simCaloHits); - updateMap(globalMap.tpcHits, localMap.tpcHits); - updateMap(globalMap.clusters, localMap.clusters); - updateMap(globalMap.vertices, localMap.vertices); - updateMap(globalMap.recoParticles, localMap.recoParticles); - updateMap(globalMap.mcParticles, localMap.mcParticles); + template void update(const ObjectMap& localMap) { + updateMap(tracks, localMap.tracks); + updateMap(trackerHits, localMap.trackerHits); + updateMap(simTrackerHits, localMap.simTrackerHits); + updateMap(caloHits, localMap.caloHits); + updateMap(rawCaloHits, localMap.rawCaloHits); + updateMap(simCaloHits, localMap.simCaloHits); + updateMap(tpcHits, localMap.tpcHits); + updateMap(clusters, localMap.clusters); + updateMap(vertices, localMap.vertices); + updateMap(recoParticles, localMap.recoParticles); + updateMap(mcParticles, localMap.mcParticles); if constexpr (det::is_detected_v) { - updateMap(globalMap.trackerHitPlanes, localMap.trackerHitPlanes); + updateMap(trackerHitPlanes, localMap.trackerHitPlanes); } if constexpr (det::is_detected_v) { - updateMap(globalMap.particleIDs, localMap.particleIDs); + updateMap(particleIDs, localMap.particleIDs); } } - - private: - GlobalConvertedObjectsMap() = default; }; } // namespace k4MarlinWrapper diff --git a/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp b/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp index 219c1a78..fd762d62 100644 --- a/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp +++ b/k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp @@ -40,8 +40,16 @@ #include #include +#include "GaudiKernel/AnyDataWrapper.h" + +#include + DECLARE_COMPONENT(Lcio2EDM4hepTool); +using namespace k4MarlinWrapper; + +using GlobalMapWrapper = AnyDataWrapper; + Lcio2EDM4hepTool::Lcio2EDM4hepTool(const std::string& type, const std::string& name, const IInterface* parent) : GaudiTool(type, name, parent), m_eds("EventDataSvc", "Lcio2EDM4hepTool") { declareInterface(this); @@ -189,11 +197,25 @@ StatusCode Lcio2EDM4hepTool::convertCollections(lcio::LCEventImpl* the_event) { } } - // Update the global conversion mapping - k4MarlinWrapper::GlobalConvertedObjectsMap::update(lcio2edm4hepMaps); + // We want one "global" map that is created the first time it is use in the + // event. + // + // Technically getOrCreate is a thing in GaudiTool but that doesn't seem to + // easily work with the AnyDataWrapper we want to use here. So doing the two + // step process here + if (!exist(GlobalConvertedObjectsMap::TESpath.data())) { + debug() << "Creating GlobalconvertedObjectsMap for this event since it is not already in the EventStore" << endmsg; + auto globalObjMapWrapper = std::make_unique(GlobalConvertedObjectsMap{}); + put(std::move(globalObjMapWrapper), GlobalConvertedObjectsMap::TESpath.data()); + } + + auto globalObjMapWrapper = get(GlobalConvertedObjectsMap::TESpath.data()); + auto& globalObjMap = globalObjMapWrapper->getData(); + + globalObjMap.update(lcio2edm4hepMaps); // Now we can resolve relations, subset collections and LCRelations - LCIO2EDM4hepConv::resolveRelations(lcio2edm4hepMaps, k4MarlinWrapper::GlobalConvertedObjectsMap::get()); + LCIO2EDM4hepConv::resolveRelations(lcio2edm4hepMaps, globalObjMap); for (const auto& [name, coll, type] : subsetColls) { registerCollection(name, LCIO2EDM4hepConv::fillSubset(coll, lcio2edm4hepMaps, type), coll);