Skip to content

Commit

Permalink
Use TES to move global map between tools instead of singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Sep 27, 2023
1 parent 5822950 commit 318e057
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 32 deletions.
27 changes: 25 additions & 2 deletions k4MarlinWrapper/src/components/EDM4hep2Lcio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@
#include "k4FWCore/DataHandle.h"
#include "k4FWCore/MetaDataHandle.h"

#include "GaudiKernel/AnyDataWrapper.h"

#include <memory>

DECLARE_COMPONENT(EDM4hep2LcioTool);

#ifdef EDM4HEP2LCIOCONV_NAMESPACE
using namespace EDM4hep2LCIOConv;
#endif

using namespace k4MarlinWrapper;

using GlobalMapWrapper = AnyDataWrapper<GlobalConvertedObjectsMap>;

struct CollectionPairMappings {
TrackMap tracks{};
TrackerHitMap trackerHits{};
Expand Down Expand Up @@ -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<GlobalMapWrapper>(GlobalConvertedObjectsMap::TESpath.data())) {
debug() << "Creating GlobalconvertedObjectsMap for this event since it is not already in the EventStore" << endmsg;
auto globalObjMapWrapper = std::make_unique<GlobalMapWrapper>(GlobalConvertedObjectsMap{});
put(std::move(globalObjMapWrapper), GlobalConvertedObjectsMap::TESpath.data());
}

auto globalObjMapWrapper = get<GlobalMapWrapper>(GlobalConvertedObjectsMap::TESpath.data());
auto& globalObjMap = globalObjMapWrapper->getData();

globalObjMap.update(collection_pairs);

FillMissingCollections(collection_pairs, k4MarlinWrapper::GlobalConvertedObjectsMap::get());
FillMissingCollections(collection_pairs, globalObjMap);

return StatusCode::SUCCESS;
}
44 changes: 17 additions & 27 deletions k4MarlinWrapper/src/components/GlobalConvertedObjectsMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "k4EDM4hep2LcioConv/MappingUtils.h"

#include <string_view>

namespace EVENT {
class Track;
class TrackerHit;
Expand Down Expand Up @@ -91,43 +93,31 @@ namespace k4MarlinWrapper {
ObjectMapT<EVENT::TrackerHitPlane*, edm4hep::TrackerHitPlane> trackerHitPlanes{};
ObjectMapT<EVENT::ParticleID*, edm4hep::ParticleID> 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 <typename ObjectMap> 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 <typename ObjectMap> 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<detail::has_trkhit_plane, ObjectMap>) {
updateMap(globalMap.trackerHitPlanes, localMap.trackerHitPlanes);
updateMap(trackerHitPlanes, localMap.trackerHitPlanes);
}
if constexpr (det::is_detected_v<detail::has_particle_id, ObjectMap>) {
updateMap(globalMap.particleIDs, localMap.particleIDs);
updateMap(particleIDs, localMap.particleIDs);
}
}

private:
GlobalConvertedObjectsMap() = default;
};
} // namespace k4MarlinWrapper

Expand Down
28 changes: 25 additions & 3 deletions k4MarlinWrapper/src/components/Lcio2EDM4hep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@
#include <k4FWCore/DataHandle.h>
#include <k4FWCore/MetaDataHandle.h>

#include "GaudiKernel/AnyDataWrapper.h"

#include <memory>

DECLARE_COMPONENT(Lcio2EDM4hepTool);

using namespace k4MarlinWrapper;

using GlobalMapWrapper = AnyDataWrapper<GlobalConvertedObjectsMap>;

Lcio2EDM4hepTool::Lcio2EDM4hepTool(const std::string& type, const std::string& name, const IInterface* parent)
: GaudiTool(type, name, parent), m_eds("EventDataSvc", "Lcio2EDM4hepTool") {
declareInterface<IEDMConverter>(this);
Expand Down Expand Up @@ -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<GlobalMapWrapper>(GlobalConvertedObjectsMap::TESpath.data())) {
debug() << "Creating GlobalconvertedObjectsMap for this event since it is not already in the EventStore" << endmsg;
auto globalObjMapWrapper = std::make_unique<GlobalMapWrapper>(GlobalConvertedObjectsMap{});
put(std::move(globalObjMapWrapper), GlobalConvertedObjectsMap::TESpath.data());
}

auto globalObjMapWrapper = get<GlobalMapWrapper>(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);
Expand Down

0 comments on commit 318e057

Please sign in to comment.