Skip to content

Commit

Permalink
store ids, event numbers and names in a map
Browse files Browse the repository at this point in the history
  • Loading branch information
m-fila committed Feb 27, 2025
1 parent 409f93e commit 0c24ed4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
9 changes: 6 additions & 3 deletions k4FWCore/components/UniqueIDGenSvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ size_t UniqueIDGenSvc::getUniqueID(event_num_t evt_num, run_num_t run_num, const
auto hash = std::hash<std::bitset<seed_digits + event_num_digits + run_num_digits + name_digits>>{}(combined_bits);

if (m_checkDuplicates) {
bool inserted = [this, hash]() {
auto [it, inserted] = [=, this, &name]() {
std::lock_guard<std::mutex> lock(m_mutex);
return m_uniqueIDs.insert(hash).second;
return m_uniqueIDs.insert({hash, {evt_num, run_num, name}});
}();
if (!inserted) {
const auto& [id_evt, id_run, id_name] = it->second;
throw std::runtime_error(
fmt::format("Duplicate event number, run number and algorithm name: {}, {}, {}", evt_num, run_num, name));
fmt::format("Duplicate ID for event number, run number and algorithm name: {}, {}, \"{}\". "
"ID already assigned to: {}, {}, \"{}\"",
evt_num, run_num, name, id_evt, id_run, id_name));
}
}

Expand Down
11 changes: 6 additions & 5 deletions k4FWCore/components/UniqueIDGenSvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
#include "GaudiKernel/Service.h"
#include "k4Interface/IUniqueIDGenSvc.h"

#include <cstddef>
#include <cstdint>
#include <functional>
#include <string>
#include <unordered_set>
#include <unordered_map>

/** @class UniqueIDGenSvc
* Generate unique, reproducible numbers using
Expand All @@ -41,10 +42,10 @@ class UniqueIDGenSvc : public extends<Service, IUniqueIDGenSvc> {
size_t getUniqueID(const event_num_t evt_num, const run_num_t run_num, const std::string& name) const override;

private:
Gaudi::Property<seed_t> m_seed{this, "Seed", {123456789}};
mutable std::unordered_set<size_t, std::identity> m_uniqueIDs;
mutable std::mutex m_mutex;
Gaudi::Property<bool> m_checkDuplicates{
Gaudi::Property<seed_t> m_seed{this, "Seed", {123456789}};
mutable std::unordered_map<size_t, std::tuple<event_num_t, run_num_t, std::string>, std::identity> m_uniqueIDs;
mutable std::mutex m_mutex;
Gaudi::Property<bool> m_checkDuplicates{
this, "CheckDuplicates", m_isDebugBuild,
"Caches obtained ID and throws an exception if a duplicate would be returned"};
#ifdef NDEBUG
Expand Down
2 changes: 1 addition & 1 deletion test/k4FWCoreTest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ add_test(NAME checkKeepDropSwitch
set_test_env(checkKeepDropSwitch)
set_property(TEST checkKeepDropSwitch APPEND PROPERTY DEPENDS ReadExampleEventData)
add_test_with_env(TestUniqueIDGenSvc options/TestUniqueIDGenSvc.py -n 1)
add_test_with_env(TestUniqueIDGenSvcRepeated options/TestUniqueIDGenSvc.py -n 2 PROPERTIES PASS_REGULAR_EXPRESSION "Duplicate event number, run number and algorithm name")
add_test_with_env(TestUniqueIDGenSvcRepeated options/TestUniqueIDGenSvc.py -n 2 PROPERTIES PASS_REGULAR_EXPRESSION "Duplicate ID for event number, run number and algorithm name")
add_test_with_env(TestEventHeaderFiller options/createEventHeader.py)
add_test_with_env(EventHeaderCheck options/runEventHeaderCheck.py PROPERTIES DEPENDS TestEventHeaderFiller)
add_test(NAME TestExec WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} COMMAND python options/TestExec.py)
Expand Down

0 comments on commit 0c24ed4

Please sign in to comment.