From d2ee44d38122a401c6ffa3ee6de5f67ddc8c1275 Mon Sep 17 00:00:00 2001 From: Brieuc Francois Date: Wed, 6 Sep 2023 09:15:47 +0200 Subject: [PATCH] Add a Gaudi Algorithm filling EventHeader eventNumber and runNumber (#133) --- k4FWCore/CMakeLists.txt | 3 +- k4FWCore/components/EventHeaderCreator.cpp | 50 ++++++++++++++++++ k4FWCore/components/EventHeaderCreator.h | 52 +++++++++++++++++++ test/k4FWCoreTest/CMakeLists.txt | 7 +++ .../k4FWCoreTest/options/createEventHeader.py | 43 +++++++++++++++ 5 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 k4FWCore/components/EventHeaderCreator.cpp create mode 100644 k4FWCore/components/EventHeaderCreator.h create mode 100644 test/k4FWCoreTest/options/createEventHeader.py diff --git a/k4FWCore/CMakeLists.txt b/k4FWCore/CMakeLists.txt index 00a8d07b..495712d2 100644 --- a/k4FWCore/CMakeLists.txt +++ b/k4FWCore/CMakeLists.txt @@ -22,6 +22,7 @@ limitations under the License. ################################################################################ find_package(podio 0.16.3 REQUIRED) +find_package(EDM4HEP) gaudi_install(SCRIPTS) gaudi_install(PYTHON) @@ -40,7 +41,7 @@ target_include_directories(k4FWCore PUBLIC file(GLOB k4fwcore_plugin_sources components/*.cpp) gaudi_add_module(k4FWCorePlugins SOURCES ${k4fwcore_plugin_sources} - LINK Gaudi::GaudiAlgLib Gaudi::GaudiKernel k4FWCore k4FWCore::k4Interface ROOT::Core ROOT::RIO ROOT::Tree) + LINK Gaudi::GaudiAlgLib Gaudi::GaudiKernel k4FWCore k4FWCore::k4Interface ROOT::Core ROOT::RIO ROOT::Tree EDM4HEP::edm4hep) target_include_directories(k4FWCorePlugins PUBLIC $ $) diff --git a/k4FWCore/components/EventHeaderCreator.cpp b/k4FWCore/components/EventHeaderCreator.cpp new file mode 100644 index 00000000..076ae85a --- /dev/null +++ b/k4FWCore/components/EventHeaderCreator.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014-2023 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 "EventHeaderCreator.h" +#include "edm4hep/EventHeaderCollection.h" + +DECLARE_COMPONENT(EventHeaderCreator) + +EventHeaderCreator::EventHeaderCreator(const std::string& name, ISvcLocator* svcLoc) : GaudiAlgorithm(name, svcLoc) { + declareProperty("EventHeaderCollection", m_headerCol, + "Name of the EventHeaderCollection that will be stored in the output root file."); +} + +StatusCode EventHeaderCreator::initialize() { + if (GaudiAlgorithm::initialize().isFailure()) + return StatusCode::FAILURE; + return StatusCode::SUCCESS; +} + +StatusCode EventHeaderCreator::execute() { + static int eventNumber = 0; + debug() << "Filling EventHeader with runNumber " << int(m_runNumber) << " and eventNumber " + << eventNumber + m_eventNumberOffset << endmsg; + auto headers = m_headerCol.createAndPut(); + auto header = headers->create(); + header.setRunNumber(m_runNumber); + header.setEventNumber(eventNumber++ + m_eventNumberOffset); + return StatusCode::SUCCESS; +} + +StatusCode EventHeaderCreator::finalize() { + if (GaudiAlgorithm::finalize().isFailure()) + return StatusCode::FAILURE; + return StatusCode::SUCCESS; +} diff --git a/k4FWCore/components/EventHeaderCreator.h b/k4FWCore/components/EventHeaderCreator.h new file mode 100644 index 00000000..b663a85f --- /dev/null +++ b/k4FWCore/components/EventHeaderCreator.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2014-2023 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. + */ +#ifndef K4FWCORE_EVENTHEADERCREATOR +#define K4FWCORE_EVENTHEADERCREATOR + +#include "GaudiAlg/GaudiAlgorithm.h" +#include "k4FWCore/DataHandle.h" + +/*** + * Algortihm that creates an EventHeader collection and fills it with eventNumber and runNumber + */ + +namespace edm4hep { + class EventHeaderCollection; +} + +class EventHeaderCreator : public GaudiAlgorithm { +public: + EventHeaderCreator(const std::string& name, ISvcLocator* svcLoc); + + virtual StatusCode initialize(); + virtual StatusCode execute(); + virtual StatusCode finalize(); + +private: + // Run number value (fixed for the entire job, to be set by the job submitter) + Gaudi::Property m_runNumber{this, "runNumber", 1, "Run number value"}; + // Event number offset, use it if you want two separated jobs with the same run number + Gaudi::Property m_eventNumberOffset{ + this, "eventNumberOffset", 0, + "Event number offset, eventNumber will be filled with 'event_index + eventNumberOffset'"}; + // datahandle for the EventHeader + DataHandle m_headerCol{"EventHeader", Gaudi::DataHandle::Writer, this}; +}; + +#endif diff --git a/test/k4FWCoreTest/CMakeLists.txt b/test/k4FWCoreTest/CMakeLists.txt index 6033bcdf..a536490f 100644 --- a/test/k4FWCoreTest/CMakeLists.txt +++ b/test/k4FWCoreTest/CMakeLists.txt @@ -159,6 +159,13 @@ add_test(NAME CreateLegacyExampleEventData COMMAND ${K4RUN} options/createLegacyExampleEventData.py) set_test_env(CreateLegacyExampleEventData) + +add_test(NAME TestEventHeaderFiller + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + COMMAND ${K4RUN} options/createEventHeader.py) +set_test_env(TestEventHeaderFiller) + + add_test(NAME Testk4runNoArguments WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} COMMAND ${K4RUN}) diff --git a/test/k4FWCoreTest/options/createEventHeader.py b/test/k4FWCoreTest/options/createEventHeader.py new file mode 100644 index 00000000..c347aa07 --- /dev/null +++ b/test/k4FWCoreTest/options/createEventHeader.py @@ -0,0 +1,43 @@ +# +# Copyright (c) 2014-2023 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 * + +from Configurables import EventHeaderCreator +eventHeaderCreator = EventHeaderCreator("eventHeaderCreator", + runNumber = 42, + eventNumberOffset = 42, + OutputLevel=DEBUG) + +from Configurables import k4DataSvc +podioevent = k4DataSvc("EventDataSvc") + +from Configurables import PodioOutput +out = PodioOutput("out") +out.filename = "eventHeader.root" + +from Configurables import ApplicationMgr +ApplicationMgr( + TopAlg = [ + eventHeaderCreator, + out, + ], + EvtSel = 'NONE', + EvtMax = 2, + ExtSvc = [podioevent], + StopOnSignal = True)