26 #include "CLHEP/Random/RandomEngine.h" 39 , verbosity(paramSet.
get<int>(
"verbosity", 0))
40 , bPrintEndOfJobSummary(paramSet.
get<bool>(
"endOfJobSummary",false))
42 state.transit_to(NuRandomServiceHelper::ArtState::inServiceConstructor);
50 iRegistry.
sPreModule.watch (
this, &NuRandomService::preModule );
51 iRegistry.
sPostModule.watch (
this, &NuRandomService::postModule );
55 iRegistry.
sPostEndJob.watch (
this, &NuRandomService::postEndJob );
62 NuRandomService::EngineId NuRandomService::qualify_engine_label
63 (std::string moduleLabel, std::string instanceName)
const 64 {
return { moduleLabel, instanceName }; }
67 (std::string instanceName )
const 68 {
return qualify_engine_label( state.moduleLabel(), instanceName); }
71 auto NuRandomService::getSeed
72 (std::string instanceName ) ->
seed_t 74 return getSeed(qualify_engine_label(instanceName));
79 auto NuRandomService::getSeed
80 (std::string
const& moduleLabel, std::string
const& instanceName) ->
seed_t 81 {
return getSeed(qualify_engine_label(moduleLabel, instanceName)); }
85 auto NuRandomService::getGlobalSeed(std::string instanceName) ->
seed_t {
86 EngineId ID(instanceName, EngineId::global);
88 <<
"NuRandomService::getGlobalSeed(\"" << instanceName <<
"\")";
98 if (hasEngine(
id))
return querySeed(
id);
106 return registerEngineID(
id);
112 return seeds.getSeed(
id);
116 auto NuRandomService::extractSeed
117 (
EngineId const&
id, std::optional<seed_t>
seed) -> std::pair<seed_t, bool>
120 if (seed && (seed.value() != InvalidSeed))
121 return { seed.value(),
true };
124 return { querySeed(
id),
false };
130 std::optional<seed_t>
const seed
132 EngineId id = qualify_engine_label(instance);
133 registerEngineIdAndSeeder(
id, seeder);
134 auto const [ seedValue, frozen ] = extractSeed(
id, seed);
136 if (frozen) freezeSeed(
id, seedValue);
146 return registerEngine(seeder, instance, readSeedParameter(pset, pnames));
155 return registerEngine(seeder, instance, readSeedParameter(seedParam));
175 return defineEngineID(qualify_engine_label(instance), seeder);
184 prepareEngine(
id, seeder);
185 return seedEngine(
id);
192 if (!hasEngine(
id)) {
194 <<
"Attempted to define engine '" <<
id.artName()
195 <<
"', that was not declared\n";
198 if (
seeds.hasSeeder(
id)) {
200 <<
"Attempted to redefine engine '" <<
id.artName()
201 <<
"', that has already been defined\n";
206 seeds.registerSeeder(
id, seeder);
207 seed_t const seed = seedEngine(
id);
213 void NuRandomService::ensureValidState(
bool bGlobal )
const {
217 if ( (state.state() != NuRandomServiceHelper::ArtState::inServiceConstructor))
220 <<
"NuRandomService: not in a service constructor." 221 <<
" May not register \"global\" engines.\n";
228 if ( (state.state() != NuRandomServiceHelper::ArtState::inModuleConstructor)
233 <<
"NuRandomService: not in a module constructor." 234 <<
" May not register engines.\n";
246 if (seed == InvalidSeed) {
248 <<
"No random seed specific to this event for engine '" <<
id <<
"'";
251 mf::LogInfo(
"NuRandomService") <<
"Random seed for this event, engine '" 252 <<
id <<
"': " <<
seed;
258 void NuRandomService::reseedModule(std::string currentModule) {
260 if (ID.moduleLabel != currentModule)
continue;
265 void NuRandomService::reseedModule() { reseedModule(state.moduleLabel()); }
268 void NuRandomService::reseedGlobal() {
270 if (!ID.isGlobal())
continue;
277 void NuRandomService::registerEngineIdAndSeeder
281 ensureValidState(
id.isGlobal());
285 <<
"NuRandomService: an engine with ID '" <<
id.artName()
286 <<
"' has already been created!\n";
288 seeds.registerNewSeeder(
id, seeder);
294 {
seeds.freezeSeed(
id, frozen_seed); }
301 registerEngineIdAndSeeder(
id, seeder);
302 return querySeed(
id);
307 std::optional<seed_t> NuRandomService::readSeedParameter(
309 std::initializer_list<std::string> pnames
312 for (std::string
const& key: pnames)
319 std::optional<seed_t> NuRandomService::readSeedParameter
323 return param(seed)? std::make_optional(seed): std::nullopt;
330 state.transit_to(NuRandomServiceHelper::ArtState::inModuleConstructor);
331 state.set_module(md);
339 state.transit_to(NuRandomServiceHelper::ArtState::inModuleBeginRun);
348 state.transit_to(NuRandomServiceHelper::ArtState::inEvent);
349 state.set_event(evt);
352 MF_LOG_DEBUG(
"NuRandomService") <<
"preProcessEvent(): will reseed global engines";
358 state.transit_to(NuRandomServiceHelper::ArtState::inModuleEvent);
363 MF_LOG_DEBUG(
"NuRandomService") <<
"preModule(): will reseed engines for module '" 369 state.reset_module();
379 state.transit_to(NuRandomServiceHelper::ArtState::inEndJob);
380 state.set_module(md);
387 void NuRandomService::postEndJob() {
388 if ((verbosity > 0) || bPrintEndOfJobSummary)
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleContext const &)> sPreModuleBeginRun
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleDescription const &)> sPreModuleConstruction
GlobalSignal< detail::SignalResponseType::LIFO, void()> sPostEndJob
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleContext const &)> sPreModule
const std::string instance
NuRandomService(const fhicl::ParameterSet &, art::ActivityRegistry &)
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleDescription const &)> sPostModuleConstruction
std::function< void(EngineId const &, seed_t)> Seeder_t
type of a function setting a seed
typename PolicyImpl_t::EventData_t EventData_t
type of data used for event seeds
Identifier for a engine, made of module name and optional instance name.
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleContext const &)> sPostModuleBeginRun
GlobalSignal< detail::SignalResponseType::FIFO, void(Event const &, ScheduleContext)> sPreProcessEvent
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
GlobalSignal< detail::SignalResponseType::FIFO, void(ModuleDescription const &)> sPreModuleEndJob
GlobalSignal< detail::SignalResponseType::LIFO, void(Event const &, ScheduleContext)> sPostProcessEvent
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
std::vector< TrajPoint > seeds
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleContext const &)> sPostModule
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
std::optional< T > get_if_present(std::string const &key) const
auto const & moduleDescription() const
auto const & moduleLabel() const
GlobalSignal< detail::SignalResponseType::LIFO, void(ModuleDescription const &)> sPostModuleEndJob
art::detail::EngineCreator::seed_t seed_t