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

Add a Gaudi Algorithm filling EventHeader eventNumber and runNumber #133

Merged
merged 9 commits into from
Sep 6, 2023
3 changes: 2 additions & 1 deletion k4FWCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
################################################################################

find_package(podio 0.16.3 REQUIRED)
find_package(EDM4HEP)

gaudi_install(SCRIPTS)
gaudi_install(PYTHON)
Expand All @@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
Expand Down
50 changes: 50 additions & 0 deletions k4FWCore/components/EventHeaderCreator.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
52 changes: 52 additions & 0 deletions k4FWCore/components/EventHeaderCreator.h
Original file line number Diff line number Diff line change
@@ -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<int> 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<int> m_eventNumberOffset{
this, "eventNumberOffset", 0,
"Event number offset, eventNumber will be filled with 'event_index + eventNumberOffset'"};
// datahandle for the EventHeader
DataHandle<edm4hep::EventHeaderCollection> m_headerCol{"EventHeader", Gaudi::DataHandle::Writer, this};
};

#endif
7 changes: 7 additions & 0 deletions test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
43 changes: 43 additions & 0 deletions test/k4FWCoreTest/options/createEventHeader.py
Original file line number Diff line number Diff line change
@@ -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 *

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid question (and I should know this), does this produce the expected output file if we don't use our Key4hep data service (k4DataSvc)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand you question correctly: this steering file does not produce any output file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, you are obviously right. Could you make it produce an output file by adding the necessary algorithm. Mainly to make everyone see how a complete example looks like and so that they then also have an output file that they can look at after running the tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, here it is

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)
Loading