Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions k4FWCore/components/MetadataSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include "podio/Frame.h"

#include <GaudiKernel/AnyDataWrapper.h>
#include <GaudiKernel/GaudiException.h>
#include <GaudiKernel/IDataProviderSvc.h>
#include <GaudiKernel/Service.h>
#include <GaudiKernel/StateMachine.h>

#include <memory>

Expand All @@ -43,6 +45,12 @@ StatusCode MetadataSvc::initialize() {

StatusCode MetadataSvc::finalize() { return Service::finalize(); }

void MetadataSvc::throwIfRunning() const {
if (targetFSMState() == Gaudi::StateMachine::RUNNING) {
throw GaudiException("putParameter cannot be called during the event loop", name(), StatusCode::FAILURE);
}
}

const podio::Frame* MetadataSvc::getFrame() const { return m_frame.get(); }
podio::Frame* MetadataSvc::getFrame() { return m_frame.get(); }
void MetadataSvc::setFrame(podio::Frame frame) { m_frame = std::make_unique<podio::Frame>(std::move(frame)); }
Expand Down
1 change: 1 addition & 0 deletions k4FWCore/components/MetadataSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class MetadataSvc : public extends<Service, IMetadataSvc> {
const podio::Frame* getFrame() const override;
podio::Frame* getFrame() override;
void setFrame(podio::Frame frame) override;
void throwIfRunning() const override;
};

#endif
3 changes: 3 additions & 0 deletions k4FWCore/include/k4FWCore/IMetadataSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IMetadataSvc : virtual public IInterface {

template <typename T>
void put(const std::string& name, const T& obj) {
throwIfRunning();
getFrameForWrite()->putParameter(name, obj);
}
Comment thread
jmcarcell marked this conversation as resolved.

Expand All @@ -50,6 +51,7 @@ class IMetadataSvc : virtual public IInterface {
protected:
virtual podio::Frame* getFrame() = 0;
virtual const podio::Frame* getFrame() const = 0;
virtual void throwIfRunning() const {}
Comment thread
jmcarcell marked this conversation as resolved.

private:
podio::Frame* getFrameForWrite() {
Expand All @@ -63,6 +65,7 @@ class IMetadataSvc : virtual public IInterface {
template <>
inline void IMetadataSvc::put<edm4hep::utils::ParticleIDMeta>(const std::string& collName,
const edm4hep::utils::ParticleIDMeta& pidMetaInfo) {
throwIfRunning();
edm4hep::utils::PIDHandler::setAlgoInfo(*getFrameForWrite(), collName, pidMetaInfo);
}

Expand Down
2 changes: 2 additions & 0 deletions test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ add_test_fwcore(FunctionalCollectionMerger options/ExampleFunctionalCollectionMe
add_test_fwcore(FunctionalFilterFile options/ExampleFunctionalFilterFile.py ADD_TO_CHECK_FILES)
add_test_fwcore(FunctionalMetadata options/ExampleFunctionalMetadata.py ADD_TO_CHECK_FILES PROPERTIES FIXTURES_SETUP FunctionalMetadataFile)
add_test_fwcore(FunctionalMetadataRead options/ExampleFunctionalMetadataRead.py PROPERTIES FIXTURES_REQUIRED FunctionalMetadataFile ADD_TO_CHECK_FILES)
add_test_fwcore(FunctionalMetadataEventLoop options/ExampleFunctionalMetadataEventLoop.py
PROPERTIES PASS_REGULAR_EXPRESSION "putParameter cannot be called during the event loop")
add_test_fwcore(FunctionalMetadataOldAlgorithm options/ExampleFunctionalMetadataOldAlgorithm.py ADD_TO_CHECK_FILES PROPERTIES FIXTURES_SETUP OldAlgorithmFile)
add_test_fwcore(createEventHeaderConcurrent options/createEventHeaderConcurrent.py ADD_TO_CHECK_FILES)
add_test_fwcore(FunctionalMetadataReadOldAlgorithm options/ExampleFunctionalMetadataReadOldAlgorithm.py PROPERTIES FIXTURES_REQUIRED OldAlgorithmFile ADD_TO_CHECK_FILES)
Expand Down
39 changes: 39 additions & 0 deletions test/k4FWCoreTest/options/ExampleFunctionalMetadataEventLoop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# 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.
#

# This test verifies that calling putParameter during the event loop raises an error

from Gaudi.Configuration import INFO
from Configurables import ExampleFunctionalMetadataProducer
from k4FWCore import ApplicationMgr
from Configurables import EventDataSvc, MetadataSvc

producer = ExampleFunctionalMetadataProducer(
"Producer",
OutputCollection=["MCParticles"],
PutMetadataInEventLoop=True,
)

ApplicationMgr(
TopAlg=[producer],
EvtSel="NONE",
EvtMax=3,
ExtSvc=[EventDataSvc("EventDataSvc"), MetadataSvc("MetadataSvc")],
OutputLevel=INFO,
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ struct ExampleFunctionalMetadataProducer final : k4FWCore::Producer<edm4hep::MCP
}

edm4hep::MCParticleCollection operator()() const override {
// Putting metadata in the main loop doesn't work
// k4FWCore::putParameter("EventMetadataInt", 5, this);
if (m_putMetadataInEventLoop) {
k4FWCore::putParameter("EventMetadataInt", 5, this);
}
auto coll = edm4hep::MCParticleCollection();
for (int i = 0; i < m_particleNum.value(); ++i) {
auto particle = coll.create();
Expand All @@ -66,6 +67,8 @@ struct ExampleFunctionalMetadataProducer final : k4FWCore::Producer<edm4hep::MCP
this, "PDGValues", {1, 2, 3, 4}, "Values of the PDG used for the particles"};

// Some properties for the configuration metadata
Gaudi::Property<bool> m_putMetadataInEventLoop{this, "PutMetadataInEventLoop", false,
"If true, attempt to put metadata during the event loop (will throw)"};
Gaudi::Property<int> m_intProp{this, "intProp", 42, "An integer property"};
Gaudi::Property<int> m_intProp2{this, "intProp2", 42, "An integer property"};
Gaudi::Property<float> m_floatProp{this, "floatProp", 3.14, "A float property"};
Expand Down
Loading