Skip to content

Commit

Permalink
Fix accessing metadata with MetaDataHandle when MetadataSvc is pr…
Browse files Browse the repository at this point in the history
…esent (key4hep#231)
  • Loading branch information
m-fila authored Sep 4, 2024
1 parent ddd3ff3 commit 94dd0d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
32 changes: 11 additions & 21 deletions k4FWCore/include/k4FWCore/MetaDataHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

template <typename T> class MetaDataHandle {
public:
MetaDataHandle();
MetaDataHandle(const std::string& descriptor, Gaudi::DataHandle::Mode a);
MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor, Gaudi::DataHandle::Mode a);

Expand Down Expand Up @@ -89,25 +88,20 @@ MetaDataHandle<T>::MetaDataHandle(const Gaudi::DataHandle& handle, const std::st

//---------------------------------------------------------------------------
template <typename T> std::optional<T> MetaDataHandle<T>::get_optional() const {
const auto& frame = m_podio_data_service->getMetaDataFrame();
return frame.getParameter<T>(fullDescriptor());
if (m_podio_data_service) {
return m_podio_data_service->getMetaDataFrame().getParameter<T>(fullDescriptor());
}
return k4FWCore::getParameter<T>(fullDescriptor());
}

//---------------------------------------------------------------------------
template <typename T> const T MetaDataHandle<T>::get() const {
std::optional<T> maybeVal;
// DataHandle based algorithms
if (m_podio_data_service) {
maybeVal = get_optional();
if (!maybeVal.has_value()) {
throw GaudiException("MetaDataHandle empty handle access",
"MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
}
// Functional algorithms
} else {
maybeVal = k4FWCore::getParameter<T>(fullDescriptor());
auto optional_parameter = get_optional();
if (!optional_parameter.has_value()) {
throw GaudiException("MetaDataHandle empty handle access",
"MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
}
return maybeVal.value();
return optional_parameter.value();
}

//---------------------------------------------------------------------------
Expand Down Expand Up @@ -159,12 +153,8 @@ template <typename T> void MetaDataHandle<T>::checkPodioDataSvc() {
if (cmd.find("genconf") != std::string::npos)
return;

// The proper check would be the following:
// if (!m_podio_data_service && !Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc")) {
// However, it seems there is always a service called "MetadataSvc" from Gaudi,
// so the check will always pass
if (!m_podio_data_service) {
std::cout << "Warning: MetaDataHandles require the PodioDataSvc (ignore if using IOSvc)" << std::endl;
if (!m_podio_data_service && !Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false)) {
std::cout << "Warning: MetaDataHandles require the PodioDataSvc or for compatibility the MetadataSvc" << std::endl;
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,17 @@ StatusCode k4FWCoreTest_cellID_reader::execute(const EventContext&) const {
return StatusCode::FAILURE;
}

auto optCellIDstr = m_cellIDHandle.get_optional();

if (!optCellIDstr.has_value()) {
error() << "ERROR: cellID is empty but was expected to hold a value" << endmsg;
return StatusCode::FAILURE;
}

if (optCellIDstr.value() != cellIDstr) {
error() << "ERROR: metadata accessed with by optional and value differs" << endmsg;
return StatusCode::FAILURE;
}

return StatusCode::SUCCESS;
}

0 comments on commit 94dd0d0

Please sign in to comment.