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
29 changes: 29 additions & 0 deletions k4FWCore/components/EventHeaderCreator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#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;
}
33 changes: 33 additions & 0 deletions k4FWCore/components/EventHeaderCreator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef K4FWCORE_EVENTHEADERCREATOR
#define K4FWCORE_EVENTHEADERCREATOR

#include "k4FWCore/DataHandle.h"
#include "GaudiAlg/GaudiAlgorithm.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
6 changes: 6 additions & 0 deletions test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ set_test_env(Testk4runNoArguments)
set_tests_properties(Testk4runNoArguments
PROPERTIES PASS_REGULAR_EXPRESSION "Usage: k4run <options_file.py>, use --help to get a complete list of arguments")

add_test(NAME TestEventHeaderFiller
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
COMMAND ${K4RUN} options/cfillEventHeader.py)
BrieucF marked this conversation as resolved.
Show resolved Hide resolved
set_test_env(TestEventHeaderFiller)

add_test(NAME Testk4runCustomArguments
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
COMMAND ${K4RUN} --foo=42 options/TestArgs.py)
Expand All @@ -177,3 +182,4 @@ add_test(NAME Testk4runHelpOnly
set_test_env(Testk4runHelpOnly)
set_tests_properties(Testk4runHelpOnly
PROPERTIES PASS_REGULAR_EXPRESSION "show this help message and exit")

16 changes: 16 additions & 0 deletions test/k4FWCoreTest/options/createEventHeader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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 ApplicationMgr
ApplicationMgr(
TopAlg = [
eventHeaderCreator,
],
EvtSel = 'NONE',
EvtMax = 2,
StopOnSignal = True)
Loading