From 57e9e7f7305ab3f73a2175542695b91e4badcbfe Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Wed, 5 Jun 2024 13:17:25 +0200 Subject: [PATCH] Make MetaDataHandle::get throw in case the value is not available and add more overloads (#195) * Cleanup unnecessary includes * Return to always returning a value or throwing an exception * Add get_optional overload for retrieving the optional value * Let exception propagate in tests --- k4FWCore/include/k4FWCore/MetaDataHandle.h | 64 ++++++++++++++++--- .../components/k4FWCoreTest_cellID_reader.cpp | 4 +- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/k4FWCore/include/k4FWCore/MetaDataHandle.h b/k4FWCore/include/k4FWCore/MetaDataHandle.h index 20b917c6..c3d55b09 100644 --- a/k4FWCore/include/k4FWCore/MetaDataHandle.h +++ b/k4FWCore/include/k4FWCore/MetaDataHandle.h @@ -19,15 +19,9 @@ #ifndef K4FWCORE_METADATAHANDLE_H #define K4FWCORE_METADATAHANDLE_H -// GAUDI -#include "Gaudi/Algorithm.h" - #include "k4FWCore/PodioDataSvc.h" -#include "podio/GenericParameters.h" - -#include "GaudiKernel/MsgStream.h" -#include +#include "podio/podioVersion.h" template class MetaDataHandle { public: @@ -36,12 +30,35 @@ template class MetaDataHandle { MetaDataHandle(const Gaudi::DataHandle& handle, const std::string& descriptor, Gaudi::DataHandle::Mode a); ~MetaDataHandle(); + /// Get the value that is stored in this MetaDataHandle + /// + /// @returns The value for this MetaDataHandle + /// + /// @throws GaudiException in case the value is not (yet) available const T get() const; - void put(T); + + /// 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 + /// + /// @returns The value stored in the Handle or the default value + const T get(const T& defaultValue) const; + + /// Set the value for this MetaDataHandle + /// + /// @note This can only be called during initalize and/or finalize but not + /// during execute for algorithms that use it + void put(T); private: std::string fullDescriptor() const; - void checkPodioDataSvc(); + + void checkPodioDataSvc(); private: ServiceHandle m_eds; @@ -73,9 +90,36 @@ MetaDataHandle::MetaDataHandle(const Gaudi::DataHandle& handle, const std::st } //--------------------------------------------------------------------------- -template const T MetaDataHandle::get() const { +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 maybeVal = get_optional(); + if (!maybeVal.has_value()) { + 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 { + 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 4e6ac18e..464f738c 100644 --- a/test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp +++ b/test/k4FWCoreTest/src/components/k4FWCoreTest_cellID_reader.cpp @@ -41,10 +41,10 @@ StatusCode k4FWCoreTest_cellID_reader::initialize() { StatusCode k4FWCoreTest_cellID_reader::execute(const EventContext&) const { const auto simtrackerhits_coll = m_simTrackerHitReaderHandle.get(); - auto collID = simtrackerhits_coll->getID(); 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; }