Skip to content

Commit

Permalink
Make the new MetadataSvc compatible with DataHandle based algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Aug 8, 2024
1 parent 8a5a885 commit c862fba
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 12 deletions.
43 changes: 32 additions & 11 deletions k4FWCore/include/k4FWCore/MetaDataHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
#ifndef K4FWCORE_METADATAHANDLE_H
#define K4FWCORE_METADATAHANDLE_H

#include <GaudiKernel/Bootstrap.h>
#include <GaudiKernel/DataHandle.h>
#include <GaudiKernel/Service.h>
#include <GaudiKernel/ServiceHandle.h>

#include "MetadataUtils.h"
#include "k4FWCore/IMetadataSvc.h"
#include "k4FWCore/PodioDataSvc.h"

template <typename T> class MetaDataHandle {
Expand Down Expand Up @@ -95,10 +102,17 @@ template <typename T> std::optional<T> MetaDataHandle<T>::get_optional() const {

//---------------------------------------------------------------------------
template <typename T> const T MetaDataHandle<T>::get() const {
const auto maybeVal = get_optional();
if (!maybeVal.has_value()) {
throw GaudiException("MetaDataHandle empty handle access",
"MetaDataHandle " + fullDescriptor() + " not (yet?) available", StatusCode::FAILURE);
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());
}
return maybeVal.value();
}
Expand All @@ -115,13 +129,20 @@ template <typename T> void MetaDataHandle<T>::put(T value) {
StatusCode::FAILURE);
// check whether we are in the proper State
// put is only allowed in the initalization
if (m_podio_data_service->targetFSMState() == Gaudi::StateMachine::RUNNING) {
throw GaudiException("MetaDataHandle policy violation", "Put cannot be used during the event loop",
StatusCode::FAILURE);

std::string full_descriptor = fullDescriptor();
// DataHandle based algorithms
if (m_podio_data_service) {
if (m_podio_data_service->targetFSMState() == Gaudi::StateMachine::RUNNING) {
throw GaudiException("MetaDataHandle policy violation", "Put cannot be used during the event loop",
StatusCode::FAILURE);
}
podio::Frame& frame = m_podio_data_service->getMetaDataFrame();
frame.putParameter(full_descriptor, value);
// Functional algorithms
} else {
k4FWCore::putParameter(full_descriptor, value);
}
std::string full_descriptor = fullDescriptor();
podio::Frame& frame = m_podio_data_service->getMetaDataFrame();
frame.putParameter(full_descriptor, value);
}

//---------------------------------------------------------------------------
Expand All @@ -145,7 +166,7 @@ template <typename T> void MetaDataHandle<T>::checkPodioDataSvc() {
if (cmd.find("genconf") != std::string::npos)
return;

if (nullptr == m_podio_data_service) {
if (!m_podio_data_service && !Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc")) {
std::cout << "ERROR: MetaDataHandles require the PodioDataSvc" << std::endl;
}
}
Expand Down
19 changes: 19 additions & 0 deletions k4FWCore/include/k4FWCore/MetadataUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ namespace k4FWCore {
}
metadataSvc->put<T>(name, value);
}
/// @brief Save a metadata parameter in the metadata frame. Overload for compatibility
/// with the MetadataHandle, don't use!
template <typename T> void putParameter(const std::string& name, const T& value) {
auto metadataSvc = Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false);
if (!metadataSvc) {
std::cout << "MetadataSvc not found" << std::endl;
return;
}
return metadataSvc->put<T>(name, value);
}
/// @brief Get a metadata parameter from the metadata frame
/// @param name The name of the parameter
/// @param alg The algorithm that is saving the parameter, typically "this"
Expand All @@ -50,6 +60,15 @@ namespace k4FWCore {
}
return metadataSvc->get<T>(name);
}
/// @brief Get a metadata parameter from the metadata frame. Overload for compatibility
/// with the MetadataHandle, don't use!
template <typename T> std::optional<T> getParameter(const std::string& name) {
auto metadataSvc = Gaudi::svcLocator()->service<IMetadataSvc>("MetadataSvc", false);
if (!metadataSvc) {
return std::nullopt;
}
return metadataSvc->get<T>(name);
}
} // namespace k4FWCore

#endif // FWCORE_METADATAUTILS_H
3 changes: 2 additions & 1 deletion test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ add_test_with_env(FunctionalTransformerHist options/ExampleFunctionalTransformer
add_test_with_env(FunctionalCollectionMerger options/ExampleFunctionalCollectionMerger.py)
add_test_with_env(FunctionalFilterFile options/ExampleFunctionalFilterFile.py)
add_test_with_env(FunctionalMetadata options/ExampleFunctionalMetadata.py)
add_test_with_env(FunctionalMetadataOldAlgorithm options/ExampleFunctionalMetadataOldAlgorithm.py)

add_test(NAME FunctionalCheckFiles COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/options/CheckOutputFiles.py)
set_tests_properties(FunctionalCheckFiles PROPERTIES DEPENDS "FunctionalFile;FunctionalMTFile;FunctionalMultipleFile;FunctionalOutputCommands;FunctionalProducerAbsolutePath;FunctionalTransformerRuntimeEmpty;FunctionalMix;FunctionalMixIOSvc;FunctionalTransformerHist;FunctionalCollectionMerger;FunctionalFilterFile;FunctionalMetadata")
set_tests_properties(FunctionalCheckFiles PROPERTIES DEPENDS "FunctionalFile;FunctionalMTFile;FunctionalMultipleFile;FunctionalOutputCommands;FunctionalProducerAbsolutePath;FunctionalTransformerRuntimeEmpty;FunctionalMix;FunctionalMixIOSvc;FunctionalTransformerHist;FunctionalCollectionMerger;FunctionalFilterFile;FunctionalMetadata;FunctionalMetadataOldAlgorithm")

# Do this after checking the files not to overwrite them
add_test_with_env(FunctionalFile_toolong options/ExampleFunctionalFile.py -n 999 PROPERTIES DEPENDS FunctionalCheckFiles PASS_REGULAR_EXPRESSION
Expand Down
9 changes: 9 additions & 0 deletions test/k4FWCoreTest/options/CheckOutputFiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,12 @@ def check_events(filename, number):
):
if metadata.get_parameter(key) != value:
raise RuntimeError(f"Metadata parameter {key} does not match expected value")

reader = podio.root_io.Reader("functional_metadata_old_algorithm.root")
metadata = reader.get("metadata")[0]
for key, value in zip(
["SimTrackerHits__CellIDEncoding"],
["M:3,S-1:3,I:9,J:9,K-1:6"],
):
if metadata.get_parameter(key) != value:
raise RuntimeError(f"Metadata parameter {key} does not match expected value")
42 changes: 42 additions & 0 deletions test/k4FWCoreTest/options/ExampleFunctionalMetadataOldAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# Copyright (c) 2014-2024 Key4hep-Project.
#
# This file is part of Key4hep.
# See https://key4hep.github.io/key4hep-doc/ for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from Gaudi.Configuration import INFO
from Configurables import k4FWCoreTest_cellID_writer, k4FWCoreTest_cellID_reader
from k4FWCore import ApplicationMgr, IOSvc

iosvc = IOSvc()
iosvc.output = "functional_metadata_old_algorithm.root"

producer = k4FWCoreTest_cellID_writer()
consumer = k4FWCoreTest_cellID_reader()


# out = PodioOutput("out")
# out.filename = "output_k4test_exampledata_cellid.root"
# out.outputCommands = ["keep *"]


ApplicationMgr(
TopAlg=[producer, consumer],
EvtSel="NONE",
EvtMax=10,
ExtSvc=[],
OutputLevel=INFO,
StopOnSignal=True,
)

0 comments on commit c862fba

Please sign in to comment.