aegir
Phlex-based simulation framework for the SHiP experiment.
Loading...
Searching...
No Matches
seed_config.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 CERN for the benefit of the SHiP Collaboration
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5// seed_config.hpp — shared resolution of the optional `seed` config key
6//
7// Reproducibility is opt-in: without a configured seed, draw one at random
8// so independent jobs produce independent output, and log it so any run can
9// still be reproduced after the fact. Parsed as int64 so the full uint32
10// range survives — drawn seeds are logged as uint32 values, and pasting one
11// back must round-trip.
12
13#pragma once
14
15#include <spdlog/spdlog.h>
16
17#include <cstdint>
18#include <limits>
19#include <random>
20#include <stdexcept>
21#include <string>
22#include <utility>
23
24#include "phlex/configuration.hpp"
25
26namespace aegir {
27
28[[nodiscard]] inline std::uint32_t resolve_seed(
29 phlex::configuration const& config, std::string const& component) {
30 auto const configured_seed = config.get_if_present<std::int64_t>("seed");
31 if (configured_seed &&
32 (*configured_seed < 0 ||
33 std::cmp_greater(*configured_seed,
34 std::numeric_limits<std::uint32_t>::max()))) {
35 throw std::runtime_error(component + ": seed " +
36 std::to_string(*configured_seed) +
37 " is outside the valid range [0, 4294967295]");
38 }
39 auto const seed = configured_seed
40 ? static_cast<std::uint32_t>(*configured_seed)
41 : static_cast<std::uint32_t>(std::random_device{}());
42 if (!configured_seed) {
43 spdlog::info(
44 "{}: no seed configured — drew random seed {}; set 'seed: {}' to "
45 "reproduce this run",
46 component, seed, seed);
47 }
48 return seed;
49}
50
51} // namespace aegir
Definition hepmc_status.hpp:17
std::uint32_t resolve_seed(phlex::configuration const &config, std::string const &component)
Definition seed_config.hpp:28