diff --git a/k4FWCore/include/k4FWCore/MetaDataHandle.h b/k4FWCore/include/k4FWCore/MetaDataHandle.h index b3b863d8..c3d55b09 100644 --- a/k4FWCore/include/k4FWCore/MetaDataHandle.h +++ b/k4FWCore/include/k4FWCore/MetaDataHandle.h @@ -21,6 +21,8 @@ #include "k4FWCore/PodioDataSvc.h" +#include "podio/podioVersion.h" + template class MetaDataHandle { public: MetaDataHandle(); @@ -35,6 +37,12 @@ template class MetaDataHandle { /// @throws GaudiException in case the value is not (yet) available const T get() const; + /// Get the (optional) value that is stored in this MetaDataHandle + /// + /// @returns An optional that contains the value if it was available from the + /// data store and is not engaged otherwise + std::optional get_optional() const; + /// Get the value that is stored in the MetaDataHandle or the provided default /// value in case that is not available /// @@ -81,20 +89,37 @@ MetaDataHandle::MetaDataHandle(const Gaudi::DataHandle& handle, const std::st checkPodioDataSvc(); } +//--------------------------------------------------------------------------- +template std::optional MetaDataHandle::get_optional() const { + const auto& frame = m_podio_data_service->getMetaDataFrame(); +#if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0) + return frame.getParameter(fullDescriptor()); +#else + // explicitly make a copy here and move it into the optional below + auto val = frame.getParameter(fullDescriptor()); + // This is what happens if we have a non-present key, so we can use it as a + // way to determine if the parameter was (likely) unset. For more details see + // https://github.com/AIDASoft/podio/issues/576 + if (val == T{}) { + return std::nullopt; + } + return std::optional(std::move(val)); +#endif +} + //--------------------------------------------------------------------------- template const T MetaDataHandle::get() const { - const auto& frame = m_podio_data_service->getMetaDataFrame(); - const auto maybeVal = frame.getParameter(fullDescriptor()); + const auto maybeVal = get_optional(); if (!maybeVal.has_value()) { - throw GaudiException("MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE); + throw GaudiException("MetaDataHandle empty handle access", + "MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE); } return maybeVal.value(); } //--------------------------------------------------------------------------- template const T MetaDataHandle::get(const T& defaultValue) const { - const auto& frame = m_podio_data_service->getMetaDataFrame(); - return frame.getParameter(fullDescriptor()).value_or(defaultValue); + return get_optional().value_or(defaultValue); } //--------------------------------------------------------------------------- diff --git a/test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp b/test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp index 25b7568e..baa8f593 100644 --- a/test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp +++ b/test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp @@ -43,13 +43,10 @@ StatusCode k4FWCoreTest_cellID_reader::initialize() { StatusCode k4FWCoreTest_cellID_reader::execute(const EventContext&) const { const auto simtrackerhits_coll = m_simTrackerHitReaderHandle.get(); -#if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0) - const auto cellIDstr = m_cellIDHandle.get().value_or(""); -#else - const auto cellIDstr = m_cellIDHandle.get(); -#endif + const auto cellIDstr = m_cellIDHandle.get(""); + if (cellIDstr != cellIDtest) { - error() << "ERROR cellID is: " << cellIDstr << endmsg; + error() << "ERROR cellID is: " << cellIDstr << "expected (" << cellIDtest << ")" << endmsg; return StatusCode::FAILURE; }