Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix accessing metadata with MetaDataHandle when MetadataSvc is present #231

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading