From 4bfe939fe35c3b141e25286d4fe041820fc5387b Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 28 Oct 2024 12:48:58 +0100 Subject: [PATCH 1/4] add EventCounter algorithm --- k4FWCore/components/EventCounter.cpp | 39 +++++++++++++ test/k4FWCoreTest/CMakeLists.txt | 2 + test/k4FWCoreTest/options/TestEventCounter.py | 58 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 k4FWCore/components/EventCounter.cpp create mode 100644 test/k4FWCoreTest/options/TestEventCounter.py diff --git a/k4FWCore/components/EventCounter.cpp b/k4FWCore/components/EventCounter.cpp new file mode 100644 index 00000000..59955a4e --- /dev/null +++ b/k4FWCore/components/EventCounter.cpp @@ -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. + */ + +#include "Gaudi/Accumulators.h" +#include "Gaudi/Functional/Consumer.h" + +class EventCounter final : public Gaudi::Functional::Consumer { +public: + using Consumer::Consumer; + + void operator()(const EventContext& ctx) const override { + ++m_count; + if ((m_frequency > 0) && (ctx.evt() % m_frequency == 0)) { + info() << "Processing event " << ctx.evt() << endmsg; + } + } + +private: + mutable Gaudi::Accumulators::Counter<> m_count{this, "count"}; + Gaudi::Property m_frequency{this, "Frequency", 1, "How often to print the event number"}; +}; + +DECLARE_COMPONENT(EventCounter) diff --git a/test/k4FWCoreTest/CMakeLists.txt b/test/k4FWCoreTest/CMakeLists.txt index 6d48d076..f1edba33 100644 --- a/test/k4FWCoreTest/CMakeLists.txt +++ b/test/k4FWCoreTest/CMakeLists.txt @@ -139,6 +139,8 @@ add_test_with_env(Testk4runCustomArguments options/TestArgs.py --foo=42 PROPERTI add_test_with_env(Testk4runVerboseOutput options/TestArgs.py --verbose PROPERTIES PASS_REGULAR_EXPRESSION " VERBOSE ") add_test_with_env(Testk4runHelpOnly options/TestArgs.py --help PROPERTIES PASS_REGULAR_EXPRESSION "show this help message and exit") +add_test_with_env(TestEventCounter options/TestEventCounter.py) +set_tests_properties(TestEventCounter PROPERTIES FAIL_REGULAR_EXPRESSION "Processing event 1;Processing event 3" PASS_REGULAR_EXPRESSION "Processing event 0.*Processing event 2;Processing event 2.*Processing event 0") add_test_with_env(FunctionalMemory options/ExampleFunctionalMemory.py) add_test_with_env(FunctionalMTMemory options/ExampleFunctionalMTMemory.py) diff --git a/test/k4FWCoreTest/options/TestEventCounter.py b/test/k4FWCoreTest/options/TestEventCounter.py new file mode 100644 index 00000000..96e93e12 --- /dev/null +++ b/test/k4FWCoreTest/options/TestEventCounter.py @@ -0,0 +1,58 @@ +# +# 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 is an example of counting events with EventCounter algorithm. +# During execution the EventCounter algorithms will printout current event number depending on the frequency. +# The summary about total number of events passed through that algorithms is handled by MessageSvcSink + +from Gaudi.Configuration import INFO, WARNING +from Configurables import EventCounter +from Configurables import HiveSlimEventLoopMgr, HiveWhiteBoard, AvalancheSchedulerSvc +from k4FWCore import ApplicationMgr + +evtslots = 4 +threads = 4 + +whiteboard = HiveWhiteBoard( + "EventDataSvc", + EventSlots=evtslots, + ForceLeaves=True, +) + +slimeventloopmgr = HiveSlimEventLoopMgr( + "HiveSlimEventLoopMgr", + SchedulerName="AvalancheSchedulerSvc", + Warnings=False, + OutputLevel=WARNING, +) + +scheduler = AvalancheSchedulerSvc(ThreadPoolSize=threads, ShowDataFlow=True, OutputLevel=WARNING) + +counter_1 = EventCounter("SilentEventCounter", Frequency=0, OutputLevel=INFO) +counter_2 = EventCounter("EventCounter", Frequency=2, OutputLevel=INFO) + + +mgr = ApplicationMgr( + TopAlg=[counter_1, counter_2], + EvtSel="NONE", + EvtMax=4, + ExtSvc=[whiteboard, "Gaudi::Monitoring::MessageSvcSink"], + EventLoop=slimeventloopmgr, + OutputLevel=INFO, +) From 403d9731c4ef08b655cf00f25df89e70b2f4abde Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 28 Oct 2024 16:51:26 +0100 Subject: [PATCH 2/4] change type of frequency property --- k4FWCore/components/EventCounter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/k4FWCore/components/EventCounter.cpp b/k4FWCore/components/EventCounter.cpp index 59955a4e..758061f4 100644 --- a/k4FWCore/components/EventCounter.cpp +++ b/k4FWCore/components/EventCounter.cpp @@ -26,14 +26,14 @@ class EventCounter final : public Gaudi::Functional::Consumer 0) && (ctx.evt() % m_frequency == 0)) { + if (m_frequency && (ctx.evt() % m_frequency == 0)) { info() << "Processing event " << ctx.evt() << endmsg; } } private: mutable Gaudi::Accumulators::Counter<> m_count{this, "count"}; - Gaudi::Property m_frequency{this, "Frequency", 1, "How often to print the event number"}; + Gaudi::Property m_frequency{this, "Frequency", 1, "How often to print the event number"}; }; DECLARE_COMPONENT(EventCounter) From dd5db6b05f948d80291966a1f57dfccb2a21d203 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 28 Oct 2024 23:22:47 +0100 Subject: [PATCH 3/4] add finalize printout --- k4FWCore/components/EventCounter.cpp | 5 +++++ test/k4FWCoreTest/CMakeLists.txt | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/k4FWCore/components/EventCounter.cpp b/k4FWCore/components/EventCounter.cpp index 758061f4..f1bd0310 100644 --- a/k4FWCore/components/EventCounter.cpp +++ b/k4FWCore/components/EventCounter.cpp @@ -31,6 +31,11 @@ class EventCounter final : public Gaudi::Functional::Consumer m_count{this, "count"}; Gaudi::Property m_frequency{this, "Frequency", 1, "How often to print the event number"}; diff --git a/test/k4FWCoreTest/CMakeLists.txt b/test/k4FWCoreTest/CMakeLists.txt index f1edba33..e8457b2f 100644 --- a/test/k4FWCoreTest/CMakeLists.txt +++ b/test/k4FWCoreTest/CMakeLists.txt @@ -140,7 +140,8 @@ add_test_with_env(Testk4runVerboseOutput options/TestArgs.py --verbose PROPERTIE add_test_with_env(Testk4runHelpOnly options/TestArgs.py --help PROPERTIES PASS_REGULAR_EXPRESSION "show this help message and exit") add_test_with_env(TestEventCounter options/TestEventCounter.py) -set_tests_properties(TestEventCounter PROPERTIES FAIL_REGULAR_EXPRESSION "Processing event 1;Processing event 3" PASS_REGULAR_EXPRESSION "Processing event 0.*Processing event 2;Processing event 2.*Processing event 0") +set_tests_properties(TestEventCounter PROPERTIES FAIL_REGULAR_EXPRESSION "Processing event 1;Processing event 3" + PASS_REGULAR_EXPRESSION "(Processing event 0.*Processing event 2|Processing event 2.*Processing event 0).*Processed 4 events.*Processed 4 events") add_test_with_env(FunctionalMemory options/ExampleFunctionalMemory.py) add_test_with_env(FunctionalMTMemory options/ExampleFunctionalMTMemory.py) From 7b23cd25039e5d99df781dbc09bbcfee6c9fd7da Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila <37295697+m-fila@users.noreply.github.com> Date: Wed, 30 Oct 2024 15:18:41 +0100 Subject: [PATCH 4/4] Update test/k4FWCoreTest/options/TestEventCounter.py Co-authored-by: Juan Miguel Carceller <22276694+jmcarcell@users.noreply.github.com> --- test/k4FWCoreTest/options/TestEventCounter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/k4FWCoreTest/options/TestEventCounter.py b/test/k4FWCoreTest/options/TestEventCounter.py index 96e93e12..e23b7b0b 100644 --- a/test/k4FWCoreTest/options/TestEventCounter.py +++ b/test/k4FWCoreTest/options/TestEventCounter.py @@ -52,7 +52,9 @@ TopAlg=[counter_1, counter_2], EvtSel="NONE", EvtMax=4, - ExtSvc=[whiteboard, "Gaudi::Monitoring::MessageSvcSink"], + # Add "MessageSvcSink" for a table at the end with a statistical summary of the counter data + # ExtSvc=[whiteboard, "Gaudi::Monitoring::MessageSvcSink"], + ExtSvc=[whiteboard], EventLoop=slimeventloopmgr, OutputLevel=INFO, )