Skip to content

Commit

Permalink
add test for creating EventHeader with GaudiHive
Browse files Browse the repository at this point in the history
  • Loading branch information
m-fila committed Aug 20, 2024
1 parent 81470ef commit 53b4dc8
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ add_test_with_env(FunctionalCollectionMerger options/ExampleFunctionalCollection
add_test_with_env(FunctionalFilterFile options/ExampleFunctionalFilterFile.py)
add_test_with_env(FunctionalMetadata options/ExampleFunctionalMetadata.py)
add_test_with_env(FunctionalMetadataOldAlgorithm options/ExampleFunctionalMetadataOldAlgorithm.py)
add_test_with_env(createEventHeaderConcurrent options/createEventHeaderConcurrent.py)
add_test_with_env(checkEventHeaderConcurrent options/checkEventHeaderConcurrent.py PROPERTIES DEPENDS createEventHeaderConcurrent)


add_test(NAME FunctionalCheckFiles COMMAND python3 ${CMAKE_CURRENT_LIST_DIR}/options/CheckOutputFiles.py)
set_tests_properties(FunctionalCheckFiles PROPERTIES DEPENDS "FunctionalFile;FunctionalMTFile;FunctionalMultipleFile;FunctionalOutputCommands;FunctionalProducerAbsolutePath;FunctionalTransformerRuntimeEmpty;FunctionalMix;FunctionalMixIOSvc;FunctionalTransformerHist;FunctionalCollectionMerger;FunctionalFilterFile;FunctionalMetadata;FunctionalMetadataOldAlgorithm")
Expand Down
48 changes: 48 additions & 0 deletions test/k4FWCoreTest/options/checkEventHeaderConcurrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#
# 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 checking EventHeader created in GaudiHive
# The checks include whether a correct range of EventNumber is created
# and if there are no duplicates

from Gaudi.Configuration import *

from Configurables import ExampleEventHeaderCheck
from Configurables import EventDataSvc
from k4FWCore import ApplicationMgr, IOSvc


eventHeaderCreator = ExampleEventHeaderCheck(
"eventHeaderCreator",
runNumber=42,
expectedEventNumbers=list(range(42, 42 + 10)),
OutputLevel=DEBUG,
)

svc = IOSvc("IOSvc")
svc.input = "eventHeaderConcurrent.root"

ApplicationMgr(
TopAlg=[eventHeaderCreator],
EvtSel="NONE",
EvtMax=10,
ExtSvc=[EventDataSvc("EventDataSvc")],
MessageSvcType="InertMessageSvc",
OutputLevel=INFO,
)
59 changes: 59 additions & 0 deletions test/k4FWCoreTest/options/createEventHeaderConcurrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# 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 creating EventHeader created in GaudiHive

from Gaudi.Configuration import *

from Configurables import EventHeaderCreator
from Configurables import HiveSlimEventLoopMgr, HiveWhiteBoard, AvalancheSchedulerSvc
from k4FWCore import ApplicationMgr, IOSvc

evtslots = 6
threads = 6

whiteboard = HiveWhiteBoard(
"EventDataSvc",
EventSlots=evtslots,
ForceLeaves=True,
)

slimeventloopmgr = HiveSlimEventLoopMgr(
"HiveSlimEventLoopMgr", SchedulerName="AvalancheSchedulerSvc", OutputLevel=WARNING
)

scheduler = AvalancheSchedulerSvc(ThreadPoolSize=threads, ShowDataFlow=True, OutputLevel=WARNING)


eventHeaderCreator = EventHeaderCreator(
"eventHeaderCreator", runNumber=42, eventNumberOffset=42, OutputLevel=DEBUG
)

svc = IOSvc("IOSvc")
svc.output = "eventHeaderConcurrent.root"

ApplicationMgr(
TopAlg=[eventHeaderCreator],
EvtSel="NONE",
EvtMax=10,
ExtSvc=[whiteboard],
EventLoop=slimeventloopmgr,
MessageSvcType="InertMessageSvc",
OutputLevel=INFO,
)
68 changes: 68 additions & 0 deletions test/k4FWCoreTest/src/components/ExampleEventHeaderCheck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 "edm4hep/Constants.h"
#include "edm4hep/EventHeaderCollection.h"

#include "k4FWCore/Consumer.h"

#include <Gaudi/Property.h>

#include <fmt/core.h>

#include <iterator>
#include <mutex>
#include <stdexcept>
#include <string>

struct ExampleEventHeaderCheck final : k4FWCore::Consumer<void(const edm4hep::EventHeaderCollection&)> {
ExampleEventHeaderCheck(const std::string& name, ISvcLocator* svcLoc)
: Consumer(name, svcLoc, {KeyValues("EventHeaderName", {edm4hep::labels::EventHeader})}) {}

void operator()(const edm4hep::EventHeaderCollection& evtHeaderColl) const override {
if (evtHeaderColl.empty()) {
throw std::runtime_error("EventHeader collection is empty");
}
const auto evtHeader = evtHeaderColl[0];
if (!evtHeader.isAvailable()) {
throw std::runtime_error("Cannot get a valid EventHeader");
}

if (evtHeader.getRunNumber() != m_runNumber) {
throw std::runtime_error(fmt::format("Run number is not set correctly (expected {}, actual {})",
m_runNumber.value(), evtHeader.getRunNumber()));
}
if (std::find(std::begin(m_expectedEventNumbers), std::end(m_expectedEventNumbers), evtHeader.getEventNumber()) ==
std::end(m_expectedEventNumbers)) {
throw std::runtime_error(fmt::format("Event number {} is not in expected numbers", evtHeader.getEventNumber()));
}
auto lock = std::scoped_lock(m_mutex);
if (m_seenEventNumbers.contains(evtHeader.getEventNumber())) {
throw std::runtime_error(fmt::format("Event number {} is duplicated", evtHeader.getEventNumber()));
}
m_seenEventNumbers.insert(evtHeader.getEventNumber());
}

Gaudi::Property<int> m_runNumber{this, "runNumber", 0, "The expected run number"};
Gaudi::Property<std::vector<int>> m_expectedEventNumbers{this, "expectedEventNumbers", 0, "Expected event numbers"};
mutable std::mutex m_mutex;
mutable std::set<int> m_seenEventNumbers;
};

DECLARE_COMPONENT(ExampleEventHeaderCheck)

0 comments on commit 53b4dc8

Please sign in to comment.