2626#include " edm4hep/utils/ParticleIDUtils.h"
2727
2828#include " k4FWCore/DataHandle.h"
29- #include " k4FWCore/FunctionalUtils.h"
30- #include " k4FWCore/MetaDataHandle.h"
31- #include " k4FWCore/PodioDataSvc.h"
29+ #include < k4FWCore/MetadataUtils.h>
3230
3331#include " GaudiKernel/AnyDataWrapper.h"
3432#include " GaudiKernel/IDataManagerSvc.h"
3533#include " GaudiKernel/IDataProviderSvc.h"
3634
37- #include < functional>
3835#include < memory>
3936
4037DECLARE_COMPONENT (EDM4hep2LcioTool);
@@ -69,16 +66,6 @@ StatusCode EDM4hep2LcioTool::initialize() {
6966 return StatusCode::FAILURE ;
7067 }
7168
72- m_podioDataSvc = dynamic_cast <PodioDataSvc*>(m_eventDataSvc.get ());
73-
74- if (!m_podioDataSvc) {
75- m_metadataSvc = service (" MetadataSvc" , false );
76- if (!m_metadataSvc) {
77- error () << " Could not retrieve MetadataSvc" << endmsg;
78- return StatusCode::FAILURE ;
79- }
80- }
81-
8269 return AlgTool::initialize ();
8370}
8471
@@ -107,11 +94,10 @@ void EDM4hep2LcioTool::convertTrackerHits(TrackerHitMap& trackerhits_vec, const
10794 this };
10895 const auto trackerhits_coll = trackerhits_handle.get ();
10996
110- k4FWCore::MetaDataHandle<std::string> cellIDStrHandle{ trackerhits_handle, edm4hep::labels::CellIDEncoding,
111- Gaudi::DataHandle::Reader} ;
97+ const auto cellIDencoding = k4FWCore::getCellIDEncoding ( trackerhits_handle. objKey (), this );
98+ debug () << " Retrieved CellID encoding: " << cellIDencoding. value_or ( " <not-present> " ) << endmsg ;
11299
113- auto conv_trackerhits =
114- EDM4hep2LCIOConv::convertTrackerHits (trackerhits_coll, cellIDStrHandle.get (" " ), trackerhits_vec);
100+ auto conv_trackerhits = EDM4hep2LCIOConv::convertTrackerHits (trackerhits_coll, cellIDencoding, trackerhits_vec);
115101
116102 // Add all trackerhits to event
117103 lcio_event->addCollection (conv_trackerhits.release (), lcio_coll_name);
@@ -129,11 +115,10 @@ void EDM4hep2LcioTool::convertTrackerHitPlanes(TrackerHitPlaneMap& trackerhits_v
129115 this };
130116 const auto trackerhits_coll = trackerhits_handle.get ();
131117
132- k4FWCore::MetaDataHandle<std::string> cellIDStrHandle{ trackerhits_handle, edm4hep::labels::CellIDEncoding,
133- Gaudi::DataHandle::Reader} ;
118+ const auto cellIDencoding = k4FWCore::getCellIDEncoding ( trackerhits_handle. objKey (), this );
119+ debug () << " Retrieved CellID encoding: " << cellIDencoding. value_or ( " <not-present> " ) << endmsg ;
134120
135- auto conv_trackerhits =
136- EDM4hep2LCIOConv::convertTrackerHitPlanes (trackerhits_coll, cellIDStrHandle.get (" " ), trackerhits_vec);
121+ auto conv_trackerhits = EDM4hep2LCIOConv::convertTrackerHitPlanes (trackerhits_coll, cellIDencoding, trackerhits_vec);
137122
138123 // Add all trackerhits to event
139124 lcio_event->addCollection (conv_trackerhits.release (), lcio_coll_name);
@@ -148,12 +133,11 @@ void EDM4hep2LcioTool::convertSimTrackerHits(SimTrackerHitMap& simtrackerhits_ve
148133 this };
149134 const auto simtrackerhits_coll = simtrackerhits_handle.get ();
150135
151- k4FWCore::MetaDataHandle<std::string> cellIDHandle{simtrackerhits_handle, edm4hep::labels::CellIDEncoding,
152- Gaudi::DataHandle::Reader};
153- const auto cellIDstr = cellIDHandle.get ();
136+ const auto cellIDencoding = k4FWCore::getCellIDEncoding (simtrackerhits_handle.objKey (), this );
137+ debug () << " Retrieved CellID encoding: " << cellIDencoding.value_or (" <not-present>" ) << endmsg;
154138
155139 auto conv_simtrackerhits =
156- EDM4hep2LCIOConv::convertSimTrackerHits (simtrackerhits_coll, cellIDstr , simtrackerhits_vec);
140+ EDM4hep2LCIOConv::convertSimTrackerHits (simtrackerhits_coll, cellIDencoding , simtrackerhits_vec);
157141
158142 // Add all simtrackerhits to event
159143 lcio_event->addCollection (conv_simtrackerhits.release (), lcio_coll_name);
@@ -168,11 +152,10 @@ void EDM4hep2LcioTool::convertCalorimeterHits(CaloHitMap& calo_hits_vec, const s
168152 this };
169153 const auto calohit_coll = calohit_handle.get ();
170154
171- k4FWCore::MetaDataHandle<std::string> cellIDHandle{calohit_handle, edm4hep::labels::CellIDEncoding,
172- Gaudi::DataHandle::Reader};
173- const auto cellIDstr = cellIDHandle.get ();
155+ const auto cellIDencoding = k4FWCore::getCellIDEncoding (calohit_handle.objKey (), this );
156+ debug () << " Retrieved CellID encoding: " << cellIDencoding.value_or (" <not-present>" ) << endmsg;
174157
175- auto conv_calohits = EDM4hep2LCIOConv::convertCalorimeterHits (calohit_coll, cellIDstr , calo_hits_vec);
158+ auto conv_calohits = EDM4hep2LCIOConv::convertCalorimeterHits (calohit_coll, cellIDencoding , calo_hits_vec);
176159
177160 // Add all Calorimeter Hits to event
178161 lcio_event->addCollection (conv_calohits.release (), lcio_coll_name);
@@ -202,12 +185,12 @@ void EDM4hep2LcioTool::convertSimCalorimeterHits(SimCaloHitMap& sim_calo_hits_ve
202185 Gaudi::DataHandle::Reader, this };
203186 const auto simcalohit_coll = sim_calohit_handle.get ();
204187
205- k4FWCore::MetaDataHandle<std::string> cellIDHandle{sim_calohit_handle, edm4hep::labels::CellIDEncoding,
206- Gaudi::DataHandle::Reader};
207- const auto cellIDstr = cellIDHandle.get ();
188+ const auto cellIDencoding = k4FWCore::getCellIDEncoding (sim_calohit_handle.objKey (), this );
189+ debug () << " Retrieved CellID encoding: " << cellIDencoding.value_or (" <not-present>" ) << endmsg;
208190
209191 // TODO mcparticles_vdc
210- auto conv_simcalohits = EDM4hep2LCIOConv::convertSimCalorimeterHits (simcalohit_coll, cellIDstr, sim_calo_hits_vec);
192+ auto conv_simcalohits =
193+ EDM4hep2LCIOConv::convertSimCalorimeterHits (simcalohit_coll, cellIDencoding, sim_calo_hits_vec);
211194
212195 // Add all Sim Calorimeter Hits to event
213196 lcio_event->addCollection (conv_simcalohits.release (), lcio_coll_name);
@@ -337,7 +320,6 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
337320 lcio::LCEventImpl* lcio_event, CollectionPairMappings& collection_pairs,
338321 std::vector<EDM4hep2LCIOConv::ParticleIDConvData>& pidCollections,
339322 std::vector<EDM4hep2LCIOConv::TrackDqdxConvData>& dQdxCollections) {
340- const auto & metadata = m_podioDataSvc->getMetaDataFrame ();
341323 const auto collPtr = getEDM4hepCollection (e4h_coll_name);
342324 const auto fulltype = collPtr->getValueTypeName ();
343325
@@ -370,13 +352,7 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
370352 } else if (fulltype == " edm4hep::EventHeader" ) {
371353 convertEventHeader (e4h_coll_name, lcio_event);
372354 } else if (fulltype == " edm4hep::ParticleID" ) {
373- std::optional<edm4hep::utils::ParticleIDMeta> pidInfo;
374- if (m_podioDataSvc) {
375- pidInfo = edm4hep::utils::PIDHandler::getAlgoInfo (metadata, e4h_coll_name);
376- } else {
377- pidInfo = m_metadataSvc->get <edm4hep::utils::ParticleIDMeta>(e4h_coll_name);
378- }
379-
355+ const auto pidInfo = k4FWCore::getParameter<edm4hep::utils::ParticleIDMeta>(e4h_coll_name, this );
380356 pidCollections.emplace_back (e4h_coll_name, static_cast <const edm4hep::ParticleIDCollection*>(collPtr), pidInfo);
381357 } else if (fulltype == " edm4hep::RecDqDx" ) {
382358 dQdxCollections.emplace_back (e4h_coll_name, static_cast <const edm4hep::RecDqdxCollection*>(collPtr));
@@ -395,45 +371,10 @@ void EDM4hep2LcioTool::convertAdd(const std::string& e4h_coll_name, const std::s
395371 }
396372}
397373
398- const podio::Frame& EDM4hep2LcioTool::getEDM4hepEvent () const {
399- debug () << " Retrieving EDM4hep event (Frame)" << endmsg;
400- if (m_podioDataSvc) {
401- debug () << " Getting it from PodioDataSvc" << endmsg;
402- return m_podioDataSvc->getEventFrame ();
403- } else {
404- debug () << " Trying to get it from TES" << endmsg;
405- DataObject* p;
406- StatusCode code = m_eventDataSvc->retrieveObject (" /Event" + k4FWCore::frameLocation, p);
407- if (code.isSuccess ()) {
408- auto * frame = dynamic_cast <AnyDataWrapper<podio::Frame>*>(p);
409- return frame->getData ();
410- }
411- }
412-
413- // We can do this because the following assumptions are true:
414- // - We only end up here if we are using the IOSvc and we are NOT reading
415- // EDM4hep data. Otherwise the Reader will be scheduled as FIRST algorithm,
416- // most importantly BEFORE any of the wrapped Marlin processors to which
417- // this converter is attached.
418- // - The empty Frame we introduce into the TES here does not interfere with
419- // the Writer for EDM4hep output (which is always scheduled last), as that
420- // will simply get this Frame instead of creating an empty one itself
421- // - There are no scheduling issues / race conditions, since the
422- // MarlinProcessorWrapper algorithm is not re-entrant and can thus not be
423- // run in parallel
424- debug () << " Could not retrieve Frame from expected location. Registering a new empty Frame into the TES" << endmsg;
425- auto tmp = new AnyDataWrapper<podio::Frame>(podio::Frame ());
426- if (m_eventDataSvc->registerObject (" /Event" + k4FWCore::frameLocation, tmp).isFailure ()) {
427- error () << " Could not retrieve Frame from expected location in TES and could not register a new one" << endmsg;
428- throw std::runtime_error (" Could not get EDM4hep event (Frame) for conversions" );
429- }
430- return tmp->getData ();
431- }
432-
433374// Parse property parameters and convert the indicated collections.
434375// Use the collection names in the parameters to read and write them
435376StatusCode EDM4hep2LcioTool::convertCollections (lcio::LCEventImpl* lcio_event) {
436- const auto & edmEvent = getEDM4hepEvent ();
377+ const auto & edmEvent = getEDM4hepEvent (this );
437378 // use m_collsToConvert to detect whether we run the first time and cache the
438379 // results as we can assume that all the events have the same contents
439380 if (m_collsToConvert.empty ()) {
@@ -451,16 +392,6 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
451392
452393 if (m_convertAll) {
453394 info () << " Converting all collections from EDM4hep to LCIO" << endmsg;
454- if (m_podioDataSvc) {
455- // If we have the PodioDataSvc get the collections available from frame
456- for (const auto & name : edmEvent.getAvailableCollections ()) {
457- const auto & [_, inserted] = collNameMapping.emplace (name, name);
458- debug () << fmt::format (" Adding '{}' from Frame to conversion? {}" , name, inserted) << endmsg;
459- }
460- }
461- // Always check the contents of the TES because algorithms that do not use
462- // the PodioDataSvc (e.g. all Functional ones) go to the TES directly and
463- // the PodioDataSvc Frame doesn't now about them.
464395 std::optional<std::map<uint32_t , std::string>> idToNameOpt (std::move (m_idToName));
465396 for (const auto & name : getAvailableCollectionsFromStore (this , idToNameOpt)) {
466397 const auto & [_, inserted] = collNameMapping.emplace (name, name);
@@ -500,21 +431,16 @@ StatusCode EDM4hep2LcioTool::convertCollections(lcio::LCEventImpl* lcio_event) {
500431 for (const auto & pidCollMeta : pidCollections) {
501432 auto algoId = attachParticleIDMetaData (lcio_event, edmEvent, pidCollMeta);
502433 if (!algoId.has_value ()) {
503- // Now go over the collections that have been produced in a functional algorithm (if any)
504- bool found = false ;
505- if (!m_podioDataSvc) {
506- const auto id = (*pidCollMeta.coll )[0 ].getParticle ().id ().collectionID ;
507- if (auto it = m_idToName.find (id); it != m_idToName.end ()) {
508- auto name = it->second ;
509- if (pidCollMeta.metadata .has_value ()) {
510- UTIL ::PIDHandler pidHandler (lcio_event->getCollection (name));
511- algoId =
512- pidHandler.addAlgorithm (pidCollMeta.metadata .value ().algoName , pidCollMeta.metadata .value ().paramNames );
513- found = true ;
514- }
434+ // Check if we can figure out the collection from information on the TES
435+ const auto id = (*pidCollMeta.coll )[0 ].getParticle ().id ().collectionID ;
436+ if (auto it = m_idToName.find (id); it != m_idToName.end ()) {
437+ auto name = it->second ;
438+ if (pidCollMeta.metadata .has_value ()) {
439+ UTIL ::PIDHandler pidHandler (lcio_event->getCollection (name));
440+ algoId =
441+ pidHandler.addAlgorithm (pidCollMeta.metadata .value ().algoName , pidCollMeta.metadata .value ().paramNames );
515442 }
516- }
517- if (!found) {
443+ } else {
518444 warning () << " Could not determine algorithm type for ParticleID collection " << pidCollMeta.name
519445 << " for setting consistent metadata" << endmsg;
520446 }
0 commit comments