LArSoft  v08_41_00
Liquid Argon Software toolkit - http://larsoft.org/
rndm::NuRandomService Class Reference

An art service to assist in the distribution of guaranteed unique seeds to all engines within an art job. More...

#include "NuRandomService.h"

Classes

class  CLHEPengineSeeder
 Seeder_t functor setting the seed of a CLHEP::HepRandomEngine engine (untested!) More...
 

Public Types

using seed_t = art::detail::EngineCreator::seed_t
 
using engine_t = CLHEP::HepRandomEngine
 
using SeedMaster_t = SeedMaster< seed_t >
 type of object providing seeds More...
 
using EngineId = SeedMaster_t::EngineId
 type of random engine ID More...
 

Public Member Functions

 NuRandomService (const fhicl::ParameterSet &, art::ActivityRegistry &)
 
 NuRandomService (NuRandomService const &)=delete
 
NuRandomService const & operator= (NuRandomService const &)=delete
 
 NuRandomService (NuRandomService &&)=delete
 
NuRandomService const & operator= (NuRandomService &&)=delete
 
 ~NuRandomService ()=default
 
seed_t getSeed (std::string const &moduleLabel, std::string const &instanceName)
 Returns a seed for the engine with specified instance name. More...
 
seed_t getSeed (std::string instanceName="")
 Returns a seed for the engine with specified instance name. More...
 
seed_t getGlobalSeed (std::string instanceName)
 Returns a seed for the global engine with specified instance name. More...
 
seed_t getCurrentSeed (std::string instanceName) const
 Returns the last computed seed for specified engine of current module. More...
 
seed_t getCurrentSeed () const
 Returns the last computed seed for the default engine of current module. More...
 
seed_t getGlobalCurrentSeed (std::string instanceName) const
 Returns the last computed seed for the specified global engine. More...
 
template<class Stream >
void print (Stream &&out) const
 Prints known (EngineId,seed) pairs. More...
 
void print () const
 Prints to the framework Info logger. More...
 
Create and register an engine

The life time of the engine is managed by art::RandomNumberGenerator, while the seeding is managed by this service.

This is a replacement of art::detail::EngineCreator-derived classes. The use of createEngine() class of function is discouraged and this function might be removed in the future, because of its non-clean use of module interfaces. The recommended approach is more verbose; here an example for an engine with a non-default instance name:

std::string const instanceName = "instanceName";
// declare an engine; NuRandomService associates an (unknown) engine, in
// the current module and an instance name, with a seed (returned)
auto const seed = Seeds.declareEngine(instanceName);
// now create the engine (for example, use art); seed will be set
auto& engine = createEngine(seed, "HepJamesRandom", instanceName);
// finally, complete the registration; seed will be set again
Seeds.defineEngine(engine);

This is equivalent to the discouraged call

Seeds.createEngine(*this, "HepJamesRandom", "instanceName");
template<typename Module >
std::reference_wrapper< NuRandomService::engine_tcreateEngine (Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, std::string type, std::string instance, SeedAtom const &seedParam)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, std::string type, SeedAtom const &seedParam)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, SeedAtom const &seedParam)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, std::string type, std::string instance, fhicl::ParameterSet const &pset, std::string pname)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, std::string type, std::string instance, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, std::string type, fhicl::ParameterSet const &pset, std::string pname)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, std::string type, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, fhicl::ParameterSet const &pset, std::string pname)
 Creates an engine with art::RandomNumberGenerator service. More...
 
template<typename Module >
std::reference_wrapper< engine_tcreateEngine (Module &module, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Creates an engine with art::RandomNumberGenerator service. More...
 
Register an existing engine

The life time of the engine is under user's control, while the seeding managed by this service.

seed_t registerEngine (SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
 Registers an existing engine with rndm::NuRandomService. More...
 
seed_t registerEngine (SeedMaster_t::Seeder_t seeder, std::string const instance, SeedAtom const &seedParam)
 Registers an existing engine with art::NuRandomService. More...
 
seed_t registerEngine (SeedMaster_t::Seeder_t seeder, SeedAtom const &seedParam)
 Registers an existing engine with art::NuRandomService. More...
 
seed_t registerEngine (SeedMaster_t::Seeder_t seeder, std::string instance, fhicl::ParameterSet const &pset, std::string pname)
 Registers an existing engine with art::NuRandomService. More...
 
seed_t registerEngine (SeedMaster_t::Seeder_t seeder, std::string instance, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Registers an existing engine with art::NuRandomService. More...
 
seed_t registerEngine (SeedMaster_t::Seeder_t seeder, fhicl::ParameterSet const &pset, std::string pname)
 Registers an existing engine with art::NuRandomService. More...
 
seed_t registerEngine (SeedMaster_t::Seeder_t seeder, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Registers an existing engine with art::NuRandomService. More...
 
seed_t registerEngine (CLHEP::HepRandomEngine &engine, std::string instance="")
 Registers an existing CLHEP engine with art::NuRandomService. More...
 
seed_t registerEngine (CLHEP::HepRandomEngine &engine, std::string instance, SeedAtom const &seedParam)
 Registers an existing CLHEP engine with art::NuRandomService. More...
 
seed_t registerEngine (CLHEP::HepRandomEngine &engine, std::string instance, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Registers an existing CLHEP engine with art::NuRandomService. More...
 
Declare the presence of an engine
seed_t declareEngine (std::string instance="")
 Declares the presence of an engine with a given instance name. More...
 
seed_t declareEngine (std::string instance, fhicl::ParameterSet const &pset, std::string pname)
 Declares the presence of an engine with a given instance name. More...
 
seed_t declareEngine (std::string instance, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Declares the presence of an engine with a given instance name. More...
 
seed_t declareEngine (fhicl::ParameterSet const &pset, std::string pname)
 Declares the presence of an engine with a default instance name. More...
 
seed_t declareEngine (fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 Declares the presence of an engine with a default instance name. More...
 
seed_t defineEngine (SeedMaster_t::Seeder_t seeder, std::string instance={})
 Defines a seeder for a previously declared engine. More...
 
seed_t defineEngine (CLHEP::HepRandomEngine &engine, std::string instance={})
 Defines a seeder for a previously declared engine. More...
 

Static Public Member Functions

static constexpr bool isSeedValid (seed_t seed)
 Returns whether the specified seed is valid. More...
 

Static Public Attributes

static constexpr seed_t InvalidSeed = SeedMaster_t::InvalidSeed
 An invalid seed. More...
 

Private Member Functions

seed_t registerEngineID (EngineId const &id, SeedMaster_t::Seeder_t seeder=SeedMaster_t::Seeder_t())
 Register an engine and seeds it with the seed from the master. More...
 
seed_t defineEngineID (EngineId const &id, SeedMaster_t::Seeder_t seeder)
 Set the seeder of an existing engine. More...
 
bool hasEngine (EngineId const &id) const
 Returns whether the specified engine is already registered. More...
 
seed_t getSeed (EngineId const &)
 
seed_t getEventSeed (EngineId const &)
 
seed_t reseedInstance (EngineId const &id)
 Reseeds the specified engine instance in the current module. More...
 
void reseedGlobal ()
 Reseed all the global engines. More...
 
seed_t prepareEngine (EngineId const &id, SeedMaster_t::Seeder_t seeder)
 Registers the engine ID into SeedMaster. More...
 
void ensureValidState (bool bGlobal=false) const
 
seed_t querySeed (EngineId const &id)
 Query a seed from the seed master. More...
 
std::pair< seed_t, bool > extractSeed (EngineId const &id, std::optional< seed_t > seed)
 
void freezeSeed (EngineId const &id, seed_t frozen_seed)
 Forces NuRandomService not to change the seed of the specified engine. More...
 
void registerEngineAndSeeder (EngineId const &id, SeedMaster_t::Seeder_t seeder)
 Registers an engine and its seeder. More...
 
seed_t seedEngine (EngineId const &id)
 Calls the seeder with the specified seed and engine ID. More...
 
void preModuleConstruction (art::ModuleDescription const &md)
 
void postModuleConstruction (art::ModuleDescription const &)
 
void preModuleBeginRun (art::ModuleContext const &mc)
 
void postModuleBeginRun (art::ModuleContext const &)
 
void preProcessEvent (art::Event const &evt, art::ScheduleContext)
 
void preModule (art::ModuleContext const &mc)
 
void postModule (art::ModuleContext const &)
 
void postProcessEvent (art::Event const &, art::ScheduleContext)
 
void preModuleEndJob (art::ModuleDescription const &md)
 
void postModuleEndJob (art::ModuleDescription const &)
 
void postEndJob ()
 
void reseedModule (std::string currentModule)
 Reseeds all the engines in the current module. More...
 
void reseedModule ()
 Reseeds all the engines in the current module. More...
 
EngineId qualify_engine_label (std::string moduleLabel, std::string instanceName) const
 Returns a fully qualified EngineId. More...
 
EngineId qualify_engine_label (std::string instanceName="") const
 Returns a fully qualified EngineId. More...
 
EngineId qualify_global_engine (std::string instanceName="") const
 Returns a fully qualified EngineId. More...
 

Static Private Member Functions

static std::optional< seed_treadSeedParameter (fhicl::ParameterSet const &pset, std::string pname)
 
static std::optional< seed_treadSeedParameter (fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
 
static std::optional< seed_treadSeedParameter (SeedAtom const &param)
 

Private Attributes

SeedMaster_t seeds
 Class managing the seeds. More...
 
NuRandomServiceHelper::ArtState state
 
int verbosity = 0
 Control the level of information messages. More...
 
bool bPrintEndOfJobSummary = false
 print a summary at the end of job More...
 

Detailed Description

An art service to assist in the distribution of guaranteed unique seeds to all engines within an art job.

See also
rndm::SeedMaster

rndm::NuRandomService centrally manages seeds for random generator engines.

The NuRandomService acts as an interface between art framework and the rndm::SeedMaster class.

The documentation is mantained in the rndm::SeedMaster class. The configuration of NuRandomService is exactly the same as SeedMaster's, and in art it's read from services.NuRandomService. The following documentation describes features of NuRandomService that are built on top of rndm::SeedMaster to have a more convenient interaction within the art framework.

Before asking NuRandomService for its seed, an engine must be in some way registered. Once the engine is registered, its original seed can be queried again by calling getSeed() methods.

Glossary

Here "engine" means a class that is able to generate random numbers according to a flat distribution. Both art and NuRandomService are module-based, that means that the engines are in the context of a specific module instance, and different module instances have independent engines. That is the reason why you don't need to specify anything about the module when creating or obtaining a random engine, and it is also the reason why engines outside module context are not supported by the framework.

Each module can need more than one engine. A module can have any number of engines, and each of them is identified by an "instance name" that is unique within the module. Nonetheless, most modules need just one engine. In that case, a default instance name can be used (that is an empty string).

A "seeder" is a callable object (e.g. a function) that sets the seed of a certain engine. The seeder is expected to find out by its own which engine it has to seed, and for that it is provided an engine ID.

Registration of a random generator engine

Registration must happen in art module constructor, in one of the following ways:

  • by asking this service to create an engine via art::RandomNumberGenerator (see createEngine() methods) [discouraged]
  • by registering an existing engine and its seeding function (see registerEngine() methods)
  • by just declaring that an engine exists (see declareEngine() and getSeed() methods) The first method and, when a seeder or an engine is provided, also the second method, set the seed of the engine they register (see below). In the third case, it is generally the caller's responsibility to seed the engine. The registration of an engine which has been only declared can be "completed" by calling defineEngine() to provide the actual seeder for that engine. The pair of calls declareEngine()/defineEngine() (or getSeed()/defineEngine()) is equivalent to a single call to registerEngine(), with the added flexibility of having the seed for the engine already available before the registration is completed.

The use of createEngine() class of function is discouraged and this function might be removed in the future, because of its non-clean use of module interfaces. The recommended approach is more prolix; here an example for an engine with a non-default instance name:

std::string const instanceName = "instanceName";
// declare an engine; NuRandomService associates an (unknown) engine, in
// the current module and an instance name, with a seed (returned)
auto const seed = Seeds.declareEngine(instanceName);
// now create the engine (for example, use art); seed will be set
auto& engine = createEngine(seed, "HepJamesRandom", instanceName);
// finally, complete the registration; seed will be set again
Seeds.defineEngine(engine);

This is equivalent to the discouraged call

Seeds.createEngine(*this, "HepJamesRandom", "instanceName");

Please read carefully the documentation of the method of your choice, since they have different requirements and apply to different usage patterns.

The registration must happen in the constructor of the module. That is because we don't want engines to be initialized in the middle of a job.

Setting the seed of an engine

rndm::NuRandomService is able to set the seed of an engine when the engine is registered via either:

  • createEngine() (creation of a new CLHEP engine)
  • registerEngine() (registration of an engine or a seeder function), if the registered seeder function is valid (non-null) or if a CLHEP engine is being registered (in which case the seeder is automatically created valid)
  • defineEngine() (registration of a seeder for an engine that was already declared), again if the seed is valid NuRandomService is not able to automatically set the seed of an engine if it was registered via either:
  • declareEngine() (declaration of the existence of an engine), that does not even require the engine to exist
  • getSeed() (query of a seed), when it (implicitly) declares an engine which had not been declared yet

If NuRandomService is able to set the seed, it will do so only once, as soon as it can. This means that if the policy allows the seed to be known immediately, the seed will be set on registration. In the case of a per-event policy that requires the presence of an event, the seed can be known only when the event is available, and NuRandomService will set the seed before the module the engine is associated with starts its main processing method (produce(), filter() or analyze()).

Changing the seeder

Currently, changing the seeder of an engine after the engine has been fully registered is not supported. As a consequence, changing the engine is also not supported.

Since only the seeder function is registered in NuRandomService, a seeder function that is flexible enough to change the engine it seeds may work around this limitation.

Querying the seed of an engine

If necessary, the seed that NuRandomService has assigned to an engine can be requested by one of the following two calls:

depending on whether the engine has a non-empty instance name, Note that this call implicitly "declares" the engine it refers to. A call not declaring anything is instead:

For most policies, the seed is set according to the configuration, once for all. In those cases, getSeed() will always return the same value. If the policy prescribes different seeds at different times, the method returns the seed that is assigned to the engine at the time of the call.

Also note that the seed assigned by NuRandomService might not match the current seed of the engine, if:

  • NuRandomService is not in charge of setting the seed of the engine, and the engine seed has not been set yet
  • the seed was reset directly after NuRandomService set the engine seed Both circumstances should be avoided.

Creating the engines independently of rndm::NuRandomService

A number of things must happen for an engine to correctly work with NuRandomService:

  • the engine instance needs to exist or be created
  • the engine must be "registered" into NuRandomService
  • the seed must be obtained from NuRandomService
  • the seed must be provided to the engine

A recipe for creating a ROOT engine and let NuRandomService take care of its seeds is:

// create the engine; ROOT will set some (temporary) seed
fRandom = std::make_unique<TRandom3>();
// declare the engine; NuRandomService associates its seeder, in
// the current module and an instance name, with a seed (returned);
// the seed is also set in the engine
Seeds.registerEngine(TRandomSeeder(fRandom), "instanceName");

Here fRandom is supposed to be a member function of the module. The TRandomSeeder object is an object that knows how to set the seed of the TRandom-derived ROOT generator passed as constructor argument. For an example of implementation, see the source code of NuRandomService.

Overriding the seed from rndm::NuRandomService at run time

NuRandomService (and SeedMaster, which the former relies upon) will decide which seed to give to each registered engine and, when possible (see above), will set that seed too.

All registration functions offer an extended signature to tell NuRandomService that if there is an explicitly configured seed, that should take precedence over the one automatically assigned by rndm::SeedMaster policy. This extended signature includes:

It is possible to have engines that are not associated with any module. If no module is current, an engine will be registered in a "global" context. This happens during service construction, and once the service construction phase is completed, no more global engines can be registered. Would one ever need to access the seed of such engine, a specific interface needs to be used: getGlobalSeed() to get the configured seed at the beginning of the job, or getGlobalCurrentSeed() to get the seed specific for the current event, if any. These are equivalent to the module context methods getSeed() and getCurrentSeed().

The art service RandomNumberGenerator does not support the creation of module-independent engines. The ownership of each global engine is by the using service, as well as the management of engine's lifetime. After an engine has been instantiated, it can be registered with registerEngine(). Likewise, a global engine can be declared first with declareEngine(), then instantiated, and then optionally defined with defineEngine(). This is completely analogous to the module-context engines. The only difference is that no createEngine() interface is available for global engines. Whether these methods create a global or module engine depends only on the context they are called in.

NuRandomService does not manage the engine life in any way. If a service owns an engine, it also needs a way to access it, as nothing equivalent to RandomNumberGenerator's getEngine() is provided. NuRandomService does manage the seeding of the registered engines, even the global ones. If the seed policy involves a event-dependent seed, all global engines are seeded together at the beginning of the event, before any module is executed. This mechanism relies on the fact that NuRandomService gets its preProcessEvent callback executed before the ones of the services that own any engine. This is guaranteed if the service constructors invoke NuRandomService before they register their callbacks.

For an example of usage, see GlobalEngineUserTestService service in the test suite. Here is an excerpt using a ROOT TRandom3 engine, that can be constructed without seed. In the service constructor, the code:

// create a new engine, that we own (engine be a class data member)
engine = std::make_unique<TRandom3>();
auto seed = Seeds.registerEngine
(rndm::NuRandomService::TRandomSeeder(engine.get()), "MyService");
// MyService callback registrations in the ActivityRegistry
// should follow the first call to NuRandomService!

where rndm::NuRandomService::TRandomSeeder is a seeder class for TRandom engines (optionally provided in NuRandomService.h, and very easy to write). This excerpt creates and owns a TRandom3 engine, and then it registers it to NuRandomService, which immediately sets its seed and will take care of reseeding the engine at each event, should the policy require it. The service will access the engine as a data member, and should it need the seed it will use:

Note that is a good idea to give the engine an instance name after the service name itself, since the instance name is global and it's the only thing distinguishing global engines, and name conflicts between different services may easily arise.

Definition at line 411 of file NuRandomService.h.

Member Typedef Documentation

using rndm::NuRandomService::engine_t = CLHEP::HepRandomEngine

Definition at line 414 of file NuRandomService.h.

type of random engine ID

Definition at line 418 of file NuRandomService.h.

type of object providing seeds

Definition at line 416 of file NuRandomService.h.

Constructor & Destructor Documentation

rndm::NuRandomService::NuRandomService ( const fhicl::ParameterSet ,
art::ActivityRegistry  
)
rndm::NuRandomService::NuRandomService ( NuRandomService const &  )
delete
rndm::NuRandomService::NuRandomService ( NuRandomService &&  )
delete
rndm::NuRandomService::~NuRandomService ( )
default

Member Function Documentation

template<typename Module >
std::reference_wrapper< NuRandomService::engine_t > rndm::NuRandomService::createEngine ( Module &  module,
std::string  type = "",
std::string  instance = "",
std::optional< seed_t > const  seed = std::nullopt 
)

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
instancethe name of the engine instance
seedthe seed to use for this engine (optional)
Returns
the seed used
See also
registerEngine()

This method creates a new engine by calling RandomNumberGenerator::createEngine() with the seed from getSeed(). The meaning of the two parameters is the same as in that function, including the somehow inconvenient order of the arguments.

The engine seed is set. If the seed optional parameter has a value, that value is used as seed. Otherwise, the seed is obtained from rndm::NuRandomService.

If the seed is not specified, it is obtained in a way equivalent to getSeed() (but multi-treading safe).

If also instance is not specified, the engine is created with no instance name (equivalent to an empty instance name).

If also type is not specified, the type of the engine is the default one from art::RandomNumberGenerator.

Note
The module parameter is needed since the interface to create an engine by art::RandomNumberGenerator service is private and open only to friends.

Definition at line 1339 of file NuRandomService.h.

Referenced by evwgh::WeightManager::Configure().

1343  {
1345  auto& engine = type.empty()
1346  ? module.createEngine(0)
1347  : module.createEngine(0, type, instance)
1348  ;
1349  registerEngineAndSeeder(id, CLHEPengineSeeder{engine});
1350  auto const [seedValue, frozen] = extractSeed(id, seed);
1351  engine.setSeed(seedValue, 0);
1352  mf::LogInfo("NuRandomService")
1353  << "Seeding " << type << " engine \"" << id.artName()
1354  << "\" with seed " << seedValue << ".";
1355  if (frozen) freezeSeed(id, seedValue);
1356  return engine;
1357  } // NuRandomService::createEngine(seed_t)
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
std::pair< seed_t, bool > extractSeed(EngineId const &id, std::optional< seed_t > seed)
const std::string instance
void freezeSeed(EngineId const &id, seed_t frozen_seed)
Forces NuRandomService not to change the seed of the specified engine.
void registerEngineAndSeeder(EngineId const &id, SeedMaster_t::Seeder_t seeder)
Registers an engine and its seeder.
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
SeedMaster_t::EngineId EngineId
type of random engine ID
long seed
Definition: chem4.cc:68
template<typename Module >
std::reference_wrapper< NuRandomService::engine_t > rndm::NuRandomService::createEngine ( Module &  module,
std::string  type,
std::string  instance,
SeedAtom const &  seedParam 
)

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
instancethe name of the engine instance
seedParamthe optional seed configuration parameter
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, std::optional<seed_t> const) with the difference that the seed is read from seedParam; if that optional parameter is not present, then the seed is obtained from rndm:::NuRandomService.

Definition at line 1362 of file NuRandomService.h.

1366  {
1367  return createEngine(module, type, instance, readSeedParameter(seedParam));
1368  } // NuRandomService::createEngine(ParameterSet)
const std::string instance
static std::optional< seed_t > readSeedParameter(fhicl::ParameterSet const &pset, std::string pname)
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
std::string  type,
SeedAtom const &  seedParam 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
seedParamthe optional seed configuration parameter
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, std::optional<seed_t> const) with the difference that the seed is read from seedParam; if that optional parameter is not present, then the seed is obtained from rndm:::NuRandomService. Also, the engine is always associated with an empty instance name.

Definition at line 631 of file NuRandomService.h.

632  { return createEngine(module, type, "", seedParam); }
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
SeedAtom const &  seedParam 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
seedParamthe optional seed configuration parameter
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, std::optional<seed_t> const) with the difference that the seed is read from seedParam; if that optional parameter is not present, then the seed is obtained from rndm:::NuRandomService. Also, the engine is always created of the default type and associated with an empty instance name.

Definition at line 651 of file NuRandomService.h.

652  { return createEngine(module, "", "", seedParam); }
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
std::string  type,
std::string  instance,
fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
instancethe name of the engine instance
psetparameter set to read parameters from
pnamename of the seed parameters
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, std::optional<seed_t> const) with the difference that the seed is retrieved from the specified configuration, looking for the first of the parameters in pname that is available. If no parameter is found, the seed is obtained from rndm::NuRandomService.

Definition at line 672 of file NuRandomService.h.

676  { return createEngine(module, type, instance, pset, { pname }); }
const std::string instance
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper< NuRandomService::engine_t > rndm::NuRandomService::createEngine ( Module &  module,
std::string  type,
std::string  instance,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
instancethe name of the engine instance
psetparameter set to read parameters from
pnamesnames of the seed parameters
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, fhicl::ParameterSet const&, std::string) with the difference that the seed can be specified by any of the parameters named in pnames: the first match will be used. As usual, if no parameter is found, the seed is obtained from rndm:::NuRandomService.

Definition at line 1373 of file NuRandomService.h.

References DECLARE_ART_SERVICE, and art::LEGACY.

1378  {
1379  return
1380  createEngine(module, type, instance, readSeedParameter(pset, pnames));
1381  }
const std::string instance
static std::optional< seed_t > readSeedParameter(fhicl::ParameterSet const &pset, std::string pname)
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
std::string  type,
fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
psetparameter set to read parameters from
pnamename of the seed parameters
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, fhicl::ParameterSet const&, std::string) with the difference that the engine is given no instance name.

Definition at line 716 of file NuRandomService.h.

720  { return createEngine(module, type, "", pset, pname); }
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
std::string  type,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
typethe type of engine
instancethe name of the engine instance
psetparameter set to read parameters from
pnamesnames of the seed parameters
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, fhicl::ParameterSet const&, std::string) with the difference that the engine is not given any instance name and that seed can be specified by any of the parameters named in pnames: the first match will be used. As usual, if no parameter is found, the seed is obtained from rndm:::NuRandomService.

Definition at line 740 of file NuRandomService.h.

744  { return createEngine(module, type, "", pset, pnames); }
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
instancethe name of the engine instance
psetparameter set to read parameters from
pnamename or names of the seed parameters
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, fhicl::ParameterSet const&, std::string) with the difference that the default engine is used (determined by art::RandomNumberGenerator) and no instance name is given to it.

Definition at line 761 of file NuRandomService.h.

765  { return createEngine(module, pset, { pname }); }
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
template<typename Module >
std::reference_wrapper<engine_t> rndm::NuRandomService::createEngine ( Module &  module,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)
inline

Creates an engine with art::RandomNumberGenerator service.

Parameters
modulemodule who will own the new engine
psetparameter set to read parameters from
pnamesnames of the seed parameters
Returns
the seed used
See also
registerEngine()

This method operates like createEngine(Module&, std::string, std::string, fhicl::ParameterSet const&, std::string) with the difference that the engine is created with the default type (determined in art::RandomNumberGenerator), not given any instance name and that seed can be specified by any of the parameters named in pnames: the first match will be used. As usual, if no parameter is found, the seed is obtained from rndm:::NuRandomService.

Definition at line 784 of file NuRandomService.h.

References seed.

788  { return createEngine(module, "", "", pset, pnames); }
std::reference_wrapper< NuRandomService::engine_t > createEngine(Module &module, std::string type="", std::string instance="", std::optional< seed_t > const seed=std::nullopt)
Creates an engine with art::RandomNumberGenerator service.
NuRandomService::seed_t rndm::NuRandomService::declareEngine ( std::string  instance = "")

Declares the presence of an engine with a given instance name.

Parameters
instancename of the instance of the engine (empty by default)
Returns
the seed assigned to the engine (may be invalid)

The existence of an engine with the specified instance name is recorded, and a seed is assigned to it. The engine will be identified by the instance name and by context information (the current module).

Differently from createEngine() and registerEngine(), the actual existence of a engine is not required. It is up to the user to manage the engine, if any at all, including the seeding.

Definition at line 159 of file NuRandomService_service.cc.

References registerEngine().

159  {
161  } // NuRandomService::declareEngine(string)
const std::string instance
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
std::function< void(EngineId const &, seed_t)> Seeder_t
type of a function setting a seed
Definition: SeedMaster.h:216
seed_t rndm::NuRandomService::declareEngine ( std::string  instance,
fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Declares the presence of an engine with a given instance name.

Parameters
instancename of the instance of the engine
psetparameter set where to find a possible fixed seed request
pnamethe name of the parameter for the fixed seed request
Returns
the seed assigned to the engine (may be invalid)

The existence of an engine with the specified instance name is recorded, and a seed is assigned to it. The engine will be identified by the instance name and by context information (the current module).

The preferred way to obtain the seed is from configuration. First, the seed is retrieved from the specified configuration, looking for the first of the parameters in pname that is available. If no parameter is found, the seed is obtained from NuRandomService.

Differently from createEngine() and registerEngine(), the actual existence of a engine is not required. It is up to the user to manage the engine, if any at all, including the seeding.

Definition at line 1080 of file NuRandomService.h.

1081  { return declareEngine(instance, pset, { pname }); }
const std::string instance
seed_t declareEngine(std::string instance="")
Declares the presence of an engine with a given instance name.
NuRandomService::seed_t rndm::NuRandomService::declareEngine ( std::string  instance,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)

Declares the presence of an engine with a given instance name.

Parameters
instancename of the instance of the engine
psetparameter set where to find a possible fixed seed request
pnamesname of the parameters for the fixed seed request
Returns
the seed assigned to the engine (may be invalid)
See also
declareEngine(std::string, fhicl::ParameterSet const&, std::string)

This method provides the same function as declareEngine(std::string, fhicl::ParameterSet const&, std::string), but it can pick the seed from the first parameter among the ones in pset whose name is in pnames.

Definition at line 164 of file NuRandomService_service.cc.

References defineEngine(), and registerEngine().

167  {
168  return registerEngine(SeedMaster_t::Seeder_t(), instance, pset, pnames);
169  } // NuRandomService::declareEngine(string, ParameterSet, init list)
const std::string instance
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
std::function< void(EngineId const &, seed_t)> Seeder_t
type of a function setting a seed
Definition: SeedMaster.h:216
seed_t rndm::NuRandomService::declareEngine ( fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Declares the presence of an engine with a default instance name.

Parameters
psetparameter set where to find a possible fixed seed request
pnamethe name of the parameter for the fixed seed request
Returns
the seed assigned to the engine (may be invalid)
See also
declareEngine(fhicl::ParameterSet const&, std::string)

This method provides the same function as declareEngine(std::string, fhicl::ParameterSet const&, std::string), but it gives the engine a empty instance name.

Definition at line 1112 of file NuRandomService.h.

1113  { return declareEngine("", pset, pname); }
seed_t declareEngine(std::string instance="")
Declares the presence of an engine with a given instance name.
seed_t rndm::NuRandomService::declareEngine ( fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)
inline

Declares the presence of an engine with a default instance name.

Parameters
psetparameter set where to find a possible fixed seed request
pnamesname of the parameters for the fixed seed request
Returns
the seed assigned to the engine (may be invalid)
See also
declareEngine(std::string, fhicl::ParameterSet const&, std::string)

This method provides the same function as declareEngine(std::string, fhicl::ParameterSet const&, std::string), but it can pick the seed from the first parameter among the ones in pset whose name is in pnames. Also, it gives the engine a empty instance name.

Definition at line 1128 of file NuRandomService.h.

1131  { return declareEngine("", pset, pnames); }
seed_t declareEngine(std::string instance="")
Declares the presence of an engine with a given instance name.
NuRandomService::seed_t rndm::NuRandomService::defineEngine ( SeedMaster_t::Seeder_t  seeder,
std::string  instance = {} 
)

Defines a seeder for a previously declared engine.

Parameters
seederseeder associated to the engine
instancename of engine instance (default: empty)
Returns
the seed assigned to the engine (may be invalid)
See also
declareEngine()

The seeder is the same object as in registerEngine(). This function can be used to finalise the declaration of an engine. If the engine was just declared with declareEngine() (as opposed to registered with registerEngine() or created with createEngine()), "defining" the engine will hook it to NuRandomService, that will take care of setting seeds automatically when needed. This step is not mandatory, but no automatic seeding will happen if it is omitted.

Definition at line 173 of file NuRandomService_service.cc.

References defineEngineID(), and qualify_engine_label().

Referenced by declareEngine().

174  {
176  } // NuRandomService::defineEngine(string, Seeder_t)
seed_t defineEngineID(EngineId const &id, SeedMaster_t::Seeder_t seeder)
Set the seeder of an existing engine.
const std::string instance
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
seed_t rndm::NuRandomService::defineEngine ( CLHEP::HepRandomEngine &  engine,
std::string  instance = {} 
)
inline

Defines a seeder for a previously declared engine.

Parameters
instancename of engine instance
engineCLHEP engine to be associated to the instance
Returns
the seed assigned to the engine (may be invalid)
See also
declareEngine()

This method operates on the default engine instance and performs the same operations as defineEngine(std::string, Seeder_t). A seeder is internally created for the CLHEP random engine.

Definition at line 1167 of file NuRandomService.h.

1167  {})
1168  { return defineEngine(CLHEPengineSeeder(engine), instance); }
const std::string instance
seed_t defineEngine(SeedMaster_t::Seeder_t seeder, std::string instance={})
Defines a seeder for a previously declared engine.
NuRandomService::seed_t rndm::NuRandomService::defineEngineID ( EngineId const &  id,
SeedMaster_t::Seeder_t  seeder 
)
private

Set the seeder of an existing engine.

Definition at line 190 of file NuRandomService_service.cc.

References ensureValidState(), hasEngine(), rndm::SeedMaster< SEED >::hasSeeder(), art::errors::LogicError, rndm::SeedMaster< SEED >::registerSeeder(), seed, seedEngine(), and seeds.

Referenced by defineEngine(), and registerEngineID().

191  {
192  if (!hasEngine(id)) {
194  << "Attempted to define engine '" << id.artName()
195  << "', that was not declared\n";
196  }
197 
198  if (seeds.hasSeeder(id)) {
200  << "Attempted to redefine engine '" << id.artName()
201  << "', that has already been defined\n";
202  }
203 
205 
206  seeds.registerSeeder(id, seeder);
207  seed_t const seed = seedEngine(id);
208  return seed;
209  } // NuRandomService::defineEngineID()
bool hasSeeder(EngineId const &id) const
Returns whether the specified engine has a valid seeder.
Definition: SeedMaster.h:281
seed_t seedEngine(EngineId const &id)
Calls the seeder with the specified seed and engine ID.
long seed
Definition: chem4.cc:68
void ensureValidState(bool bGlobal=false) const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
void registerSeeder(EngineId const &id, Seeder_t seeder)
Register the specified function to reseed the engine id.
Definition: SeedMaster.h:527
SeedMaster_t seeds
Class managing the seeds.
art::detail::EngineCreator::seed_t seed_t
void rndm::NuRandomService::ensureValidState ( bool  bGlobal = false) const
private

Definition at line 213 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::inModuleConstructor, rndm::NuRandomServiceHelper::ArtState::inServiceConstructor, art::errors::LogicError, rndm::NuRandomServiceHelper::ArtState::state(), and state.

Referenced by defineEngineID(), and registerEngineAndSeeder().

213  {
214  if (bGlobal) {
215  // registering engines may only happen in a service c'tor
216  // In all other cases, throw.
218  {
220  << "NuRandomService: not in a service constructor."
221  << " May not register \"global\" engines.\n";
222  }
223  }
224  else { // context-aware engine
225  // registering engines may only happen in a c'tor
226  // (disabling the ability to do that or from a beginRun method)
227  // In all other cases, throw.
229  // && (state.state() != NuRandomServiceHelper::ArtState::inModuleBeginRun)
230  )
231  {
233  << "NuRandomService: not in a module constructor."
234  << " May not register engines.\n";
235  }
236  } // if
237  } // NuRandomService::ensureValidState()
NuRandomServiceHelper::ArtState state
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
state_type state() const
Getters.
Definition: ArtState.h:112
auto rndm::NuRandomService::extractSeed ( EngineId const &  id,
std::optional< seed_t seed 
)
private

Helper to retrieve a seed including configuration.

Returns
the seed, and whether it is fixed (that is, from configuration)

Definition at line 117 of file NuRandomService_service.cc.

References InvalidSeed, and querySeed().

Referenced by querySeed(), and registerEngine().

118  {
119  // if we got a valid seed, use it as frozen
120  if (seed && (seed.value() != InvalidSeed))
121  return { seed.value(), true };
122 
123  // seed was not good enough; get the seed from the master
124  return { querySeed(id), false };
125  } // NuRandomService::extractSeed()
seed_t querySeed(EngineId const &id)
Query a seed from the seed master.
long seed
Definition: chem4.cc:68
static constexpr seed_t InvalidSeed
An invalid seed.
void rndm::NuRandomService::freezeSeed ( EngineId const &  id,
seed_t  frozen_seed 
)
private

Forces NuRandomService not to change the seed of the specified engine.

Definition at line 293 of file NuRandomService_service.cc.

References rndm::SeedMaster< SEED >::freezeSeed(), prepareEngine(), and seeds.

Referenced by registerEngine().

294  { seeds.freezeSeed(id, frozen_seed); }
void freezeSeed(EngineId const &id, seed_t seed)
Forces SeedMaster not to change the seed of a registered engine.
Definition: SeedMaster.h:548
SeedMaster_t seeds
Class managing the seeds.
seed_t rndm::NuRandomService::getCurrentSeed ( std::string  instanceName) const
inline

Returns the last computed seed for specified engine of current module.

Definition at line 506 of file NuRandomService.h.

References tca::seeds.

507  { return seeds.getCurrentSeed(qualify_engine_label(instanceName)); }
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
seed_t getCurrentSeed(EngineId const &id) const
Returns the last computed seed value for the specified engine ID.
Definition: SeedMaster.h:305
SeedMaster_t seeds
Class managing the seeds.
seed_t rndm::NuRandomService::getCurrentSeed ( ) const
inline

Returns the last computed seed for the default engine of current module.

Definition at line 510 of file NuRandomService.h.

References tca::seeds.

EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
seed_t getCurrentSeed(EngineId const &id) const
Returns the last computed seed value for the specified engine ID.
Definition: SeedMaster.h:305
SeedMaster_t seeds
Class managing the seeds.
seed_t rndm::NuRandomService::getEventSeed ( EngineId const &  )
private
seed_t rndm::NuRandomService::getGlobalCurrentSeed ( std::string  instanceName) const
inline

Returns the last computed seed for the specified global engine.

Definition at line 514 of file NuRandomService.h.

References instance, art::module, seed, and tca::seeds.

515  { return seeds.getCurrentSeed(qualify_global_engine(instanceName)); }
EngineId qualify_global_engine(std::string instanceName="") const
Returns a fully qualified EngineId.
seed_t getCurrentSeed(EngineId const &id) const
Returns the last computed seed value for the specified engine ID.
Definition: SeedMaster.h:305
SeedMaster_t seeds
Class managing the seeds.
auto rndm::NuRandomService::getGlobalSeed ( std::string  instanceName)

Returns a seed for the global engine with specified instance name.

Parameters
instanceNamename of the engine instance
Returns
a seed for the global engine with specified instance name
See also
getSeed()

A "global" engine is not bound to a specific execution context. The only context NuRandomService is aware of is the module, so this translates into engines that are not bound to any module. To instruct NuRandomService to ignore the current context (that may be a running module, or no running module at all), getGlobalSeed() is used instead of getSeed(), that will consider the context and in fact consider the absence of context an error.

The engine needs to have been registered before, in any of the supported ways. If it has not, this call will declare it with declareEngine() and no further registration will be allowed.

While this method can be called at any time, the registration of an engine can happen only at construction time, and it will make the call to this method fail if it is called at any other time.

Definition at line 85 of file NuRandomService_service.cc.

References getSeed(), rndm::SeedMasterHelper::EngineId::global, and MF_LOG_DEBUG.

85  {
86  EngineId ID(instanceName, EngineId::global);
87  MF_LOG_DEBUG("NuRandomService")
88  << "NuRandomService::getGlobalSeed(\"" << instanceName << "\")";
89  return getSeed(ID);
90  } // NuRandomService::getGlobalSeed()
static constexpr Global_t global
A constant to select a "global" flavour constructor.
Definition: EngineId.h:28
SeedMaster_t::EngineId EngineId
type of random engine ID
#define MF_LOG_DEBUG(id)
seed_t getSeed(std::string const &moduleLabel, std::string const &instanceName)
Returns a seed for the engine with specified instance name.
auto rndm::NuRandomService::getSeed ( std::string const &  moduleLabel,
std::string const &  instanceName 
)

Returns a seed for the engine with specified instance name.

Parameters
moduleLabellabel of the module the engine is assigned to
instanceNamename of the engine instance within that module
Returns
a seed for the engine with specified instance name
See also
getGlobalSeed()

The seed for an engine in the context of the specified module label is returned. If you need the seed for an engine outside that context, use getGlobalSeed() instead.

The engine needs to have been registered before, in any of the supported ways. If it has not, this call will declare it with declareEngine() and no further registration will be allowed.

While this method can be called at any time, the registration of an engine can happen only at construction time: if it is called at any other time and if the call triggers such registration as described above, it will make the call to this method fail.

Note
This method is thread-safe.

Definition at line 80 of file NuRandomService_service.cc.

References qualify_engine_label().

Referenced by getGlobalSeed(), getSeed(), and qualify_engine_label().

81  { return getSeed(qualify_engine_label(moduleLabel, instanceName)); }
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
seed_t getSeed(std::string const &moduleLabel, std::string const &instanceName)
Returns a seed for the engine with specified instance name.
auto rndm::NuRandomService::getSeed ( std::string  instanceName = "")

Returns a seed for the engine with specified instance name.

Parameters
instanceNamename of the engine instance
Returns
a seed for the engine with specified instance name
See also
getGlobalSeed()

The seed for an engine in the context of the current module is returned. See getSeed(std::string const&, std::string const&) for details.

Note
This method is not thread-safe.

Definition at line 72 of file NuRandomService_service.cc.

References getSeed(), and qualify_engine_label().

73  {
74  return getSeed(qualify_engine_label(instanceName));
75  } // NuRandomService::getSeed(string)
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
seed_t getSeed(std::string const &moduleLabel, std::string const &instanceName)
Returns a seed for the engine with specified instance name.
NuRandomService::seed_t rndm::NuRandomService::getSeed ( EngineId const &  id)
private

Definition at line 94 of file NuRandomService_service.cc.

References hasEngine(), querySeed(), and registerEngineID().

94  {
95 
96  // We require an engine to have been registered before we yield seeds;
97  // this should minimise unexpected conflicts.
98  if (hasEngine(id)) return querySeed(id); // ask the seed to seed master
99 
100  // if it hasn't been declared, we declare it now
101  // (this is for backward compatibility with the previous behaviour).
102  // registerEngineID() will eventually call this function again to get the
103  // seed... so we return it directly.
104  // Also note that this effectively "freezes" the engine since no seeder
105  // is specified.
106  return registerEngineID(id);
107 
108  } // NuRandomService::getSeed(EngineId)
seed_t querySeed(EngineId const &id)
Query a seed from the seed master.
seed_t registerEngineID(EngineId const &id, SeedMaster_t::Seeder_t seeder=SeedMaster_t::Seeder_t())
Register an engine and seeds it with the seed from the master.
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
bool rndm::NuRandomService::hasEngine ( EngineId const &  id) const
inlineprivate

Returns whether the specified engine is already registered.

Definition at line 1242 of file NuRandomService.h.

References rndm::SeedMaster< SEED >::hasEngine().

Referenced by defineEngineID(), getSeed(), and registerEngineAndSeeder().

1242 { return seeds.hasEngine(id); }
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
Definition: SeedMaster.h:277
SeedMaster_t seeds
Class managing the seeds.
static constexpr bool rndm::NuRandomService::isSeedValid ( seed_t  seed)
inlinestatic

Returns whether the specified seed is valid.

Definition at line 434 of file NuRandomService.h.

435  { return seed != InvalidSeed; }
long seed
Definition: chem4.cc:68
static constexpr seed_t InvalidSeed
An invalid seed.
NuRandomService const& rndm::NuRandomService::operator= ( NuRandomService const &  )
delete
NuRandomService const& rndm::NuRandomService::operator= ( NuRandomService &&  )
delete
void rndm::NuRandomService::postEndJob ( )
private

Definition at line 387 of file NuRandomService_service.cc.

References bPrintEndOfJobSummary, DEFINE_ART_SERVICE, print(), and verbosity.

387  {
388  if ((verbosity > 0) || bPrintEndOfJobSummary)
389  print(); // framework logger decides whether and where it shows up
390  } // NuRandomService::postEndJob()
bool bPrintEndOfJobSummary
print a summary at the end of job
int verbosity
Control the level of information messages.
void print() const
Prints to the framework Info logger.
void rndm::NuRandomService::postModule ( art::ModuleContext const &  )
private

Definition at line 368 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::reset_module(), rndm::NuRandomServiceHelper::ArtState::reset_state(), and state.

368  {
370  state.reset_state();
371  } // NuRandomService::postModule()
void reset_module()
Records the status of ART.
Definition: ArtState.h:91
void reset_state()
Resets the status to "something else" (inOther)
Definition: ArtState.h:74
NuRandomServiceHelper::ArtState state
void rndm::NuRandomService::postModuleBeginRun ( art::ModuleContext const &  )
private

Definition at line 343 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::reset_state(), and state.

343  {
344  state.reset_state();
345  } // NuRandomService::postModuleBeginRun()
void reset_state()
Resets the status to "something else" (inOther)
Definition: ArtState.h:74
NuRandomServiceHelper::ArtState state
void rndm::NuRandomService::postModuleConstruction ( art::ModuleDescription const &  )
private

Definition at line 334 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::reset_state(), and state.

334  {
335  state.reset_state();
336  } // NuRandomService::postModuleConstruction()
void reset_state()
Resets the status to "something else" (inOther)
Definition: ArtState.h:74
NuRandomServiceHelper::ArtState state
void rndm::NuRandomService::postModuleEndJob ( art::ModuleDescription const &  )
private

Definition at line 383 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::reset_state(), and state.

383  {
384  state.reset_state();
385  } // NuRandomService::preModuleBeginRun()
void reset_state()
Resets the status to "something else" (inOther)
Definition: ArtState.h:74
NuRandomServiceHelper::ArtState state
void rndm::NuRandomService::postProcessEvent ( art::Event const &  ,
art::ScheduleContext   
)
private

Definition at line 373 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::reset_event(), rndm::NuRandomServiceHelper::ArtState::reset_state(), and state.

373  {
374  state.reset_event();
375  state.reset_state();
376  } // NuRandomService::postProcessEvent()
void reset_state()
Resets the status to "something else" (inOther)
Definition: ArtState.h:74
NuRandomServiceHelper::ArtState state
void reset_event()
Records the status of ART.
Definition: ArtState.h:83
void rndm::NuRandomService::preModule ( art::ModuleContext const &  mc)
private

Definition at line 357 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::inModuleEvent, MF_LOG_DEBUG, art::ModuleContext::moduleDescription(), art::ModuleContext::moduleLabel(), reseedModule(), rndm::NuRandomServiceHelper::ArtState::set_module(), state, and rndm::NuRandomServiceHelper::ArtState::transit_to().

357  {
359  state.set_module(mc.moduleDescription());
360 
361  // Reseed all the engine of this module... maybe
362  // (that is, if the current policy alows it).
363  MF_LOG_DEBUG("NuRandomService") << "preModule(): will reseed engines for module '"
364  << mc.moduleLabel() << "'";
365  reseedModule(mc.moduleLabel());
366  } // NuRandomService::preModule()
state_type transit_to(state_type astate)
Records the new status of ART and returns the old one.
Definition: ArtState.h:63
void reseedModule()
Reseeds all the engines in the current module.
NuRandomServiceHelper::ArtState state
in event processing by a module
Definition: ArtState.h:41
#define MF_LOG_DEBUG(id)
void set_module(art::ModuleDescription const &desc)
Records the specified module description.
Definition: ArtState.h:86
void rndm::NuRandomService::preModuleBeginRun ( art::ModuleContext const &  mc)
private

Definition at line 338 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::inModuleBeginRun, art::ModuleContext::moduleDescription(), rndm::NuRandomServiceHelper::ArtState::set_module(), state, and rndm::NuRandomServiceHelper::ArtState::transit_to().

338  {
340  state.set_module(mc.moduleDescription());
341  } // NuRandomService::preModuleBeginRun()
state_type transit_to(state_type astate)
Records the new status of ART and returns the old one.
Definition: ArtState.h:63
NuRandomServiceHelper::ArtState state
void set_module(art::ModuleDescription const &desc)
Records the specified module description.
Definition: ArtState.h:86
void rndm::NuRandomService::preModuleConstruction ( art::ModuleDescription const &  md)
private

Definition at line 329 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::inModuleConstructor, rndm::NuRandomServiceHelper::ArtState::set_module(), state, and rndm::NuRandomServiceHelper::ArtState::transit_to().

329  {
331  state.set_module(md);
332  } // NuRandomService::preModuleConstruction()
state_type transit_to(state_type astate)
Records the new status of ART and returns the old one.
Definition: ArtState.h:63
NuRandomServiceHelper::ArtState state
void set_module(art::ModuleDescription const &desc)
Records the specified module description.
Definition: ArtState.h:86
void rndm::NuRandomService::preModuleEndJob ( art::ModuleDescription const &  md)
private

Definition at line 378 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::inEndJob, rndm::NuRandomServiceHelper::ArtState::set_module(), state, and rndm::NuRandomServiceHelper::ArtState::transit_to().

378  {
380  state.set_module(md);
381  } // NuRandomService::preModuleBeginRun()
state_type transit_to(state_type astate)
Records the new status of ART and returns the old one.
Definition: ArtState.h:63
NuRandomServiceHelper::ArtState state
void set_module(art::ModuleDescription const &desc)
Records the specified module description.
Definition: ArtState.h:86
NuRandomService::seed_t rndm::NuRandomService::prepareEngine ( EngineId const &  id,
SeedMaster_t::Seeder_t  seeder 
)
private

Registers the engine ID into SeedMaster.

Definition at line 299 of file NuRandomService_service.cc.

References querySeed(), and registerEngineAndSeeder().

Referenced by freezeSeed(), and registerEngineID().

300  {
301  registerEngineAndSeeder(id, seeder);
302  return querySeed(id);
303  } // NuRandomService::prepareEngine()
seed_t querySeed(EngineId const &id)
Query a seed from the seed master.
void registerEngineAndSeeder(EngineId const &id, SeedMaster_t::Seeder_t seeder)
Registers an engine and its seeder.
void rndm::NuRandomService::preProcessEvent ( art::Event const &  evt,
art::ScheduleContext   
)
private

Definition at line 347 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::inEvent, MF_LOG_DEBUG, rndm::SeedMaster< SEED >::onNewEvent(), reseedGlobal(), seeds, rndm::NuRandomServiceHelper::ArtState::set_event(), state, and rndm::NuRandomServiceHelper::ArtState::transit_to().

347  {
350  seeds.onNewEvent(); // inform the seed master that a new event has come
351 
352  MF_LOG_DEBUG("NuRandomService") << "preProcessEvent(): will reseed global engines";
353  reseedGlobal(); // why don't we do them all?!?
354 
355  } // NuRandomService::preProcessEvent()
state_type transit_to(state_type astate)
Records the new status of ART and returns the old one.
Definition: ArtState.h:63
void set_event(art::Event const &evt)
Records the specified event ID.
Definition: ArtState.h:77
NuRandomServiceHelper::ArtState state
void onNewEvent()
Prepares for a new event.
Definition: SeedMaster.h:726
void reseedGlobal()
Reseed all the global engines.
#define MF_LOG_DEBUG(id)
TCEvent evt
Definition: DataStructs.cxx:7
SeedMaster_t seeds
Class managing the seeds.
template<class Stream >
void rndm::NuRandomService::print ( Stream &&  out) const
inline

Prints known (EngineId,seed) pairs.

Definition at line 1175 of file NuRandomService.h.

References tca::seeds.

1176  { seeds.print(std::forward<Stream>(out)); }
void print(Stream &&) const
Prints known (EngineId,seed) pairs.
Definition: SeedMaster.h:587
SeedMaster_t seeds
Class managing the seeds.
void rndm::NuRandomService::print ( ) const
inline

Prints to the framework Info logger.

Definition at line 1179 of file NuRandomService.h.

References print(), and seed.

Referenced by postEndJob(), and print().

1179 { print(mf::LogInfo("NuRandomService")); }
void print() const
Prints to the framework Info logger.
NuRandomService::EngineId rndm::NuRandomService::qualify_engine_label ( std::string  moduleLabel,
std::string  instanceName 
) const
private

Returns a fully qualified EngineId.

Definition at line 63 of file NuRandomService_service.cc.

Referenced by defineEngine(), getSeed(), qualify_engine_label(), and registerEngine().

64  { return { moduleLabel, instanceName }; }
NuRandomService::EngineId rndm::NuRandomService::qualify_engine_label ( std::string  instanceName = "") const
private

Returns a fully qualified EngineId.

Definition at line 67 of file NuRandomService_service.cc.

References getSeed(), rndm::NuRandomServiceHelper::ArtState::moduleLabel(), qualify_engine_label(), and state.

68  { return qualify_engine_label( state.moduleLabel(), instanceName); }
std::string moduleLabel() const
Getters.
Definition: ArtState.h:122
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
NuRandomServiceHelper::ArtState state
EngineId rndm::NuRandomService::qualify_global_engine ( std::string  instanceName = "") const
inlineprivate

Returns a fully qualified EngineId.

Definition at line 1277 of file NuRandomService.h.

1278  { return EngineId(instanceName, EngineId::global); }
static constexpr Global_t global
A constant to select a "global" flavour constructor.
Definition: EngineId.h:28
SeedMaster_t::EngineId EngineId
type of random engine ID
NuRandomService::seed_t rndm::NuRandomService::querySeed ( EngineId const &  id)
private

Query a seed from the seed master.

Definition at line 111 of file NuRandomService_service.cc.

References extractSeed(), rndm::SeedMaster< SEED >::getSeed(), and seeds.

Referenced by extractSeed(), getSeed(), and prepareEngine().

111  {
112  return seeds.getSeed(id); // ask the seed to seed master
113  } // NuRandomService::querySeed()
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
Definition: SeedMaster.h:509
SeedMaster_t seeds
Class managing the seeds.
static std::optional<seed_t> rndm::NuRandomService::readSeedParameter ( fhicl::ParameterSet const &  pset,
std::string  pname 
)
inlinestaticprivate

Reads the seed from the first of the specified parameters available.

Returns
the value of the seed if found, no value otherwise

Definition at line 1285 of file NuRandomService.h.

References seed.

Referenced by readSeedParameter(), and registerEngine().

1286  { return readSeedParameter(pset, { pname }); }
static std::optional< seed_t > readSeedParameter(fhicl::ParameterSet const &pset, std::string pname)
std::optional< seed_t > rndm::NuRandomService::readSeedParameter ( fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)
staticprivate

Reads the seed from the first of the specified parameters available.

Returns
the value of the seed if found, no value otherwise

Definition at line 307 of file NuRandomService_service.cc.

References fhicl::ParameterSet::get_if_present(), readSeedParameter(), and seed.

310  {
311  seed_t seed;
312  for (std::string const& key: pnames)
313  if (pset.get_if_present(key, seed)) return { seed };
314  return std::nullopt;
315  } // NuRandomService::readSeedParameter(ParameterSet, strings)
long seed
Definition: chem4.cc:68
art::detail::EngineCreator::seed_t seed_t
std::optional< seed_t > rndm::NuRandomService::readSeedParameter ( SeedAtom const &  param)
staticprivate

Reads the seed from the first of the specified parameters available.

Returns
the value of the seed if found, no value otherwise

Definition at line 320 of file NuRandomService_service.cc.

References seed.

321  {
322  seed_t seed;
323  return param(seed)? std::make_optional(seed): std::nullopt;
324  } // NuRandomService::readSeedParameter(SeedAtom)
STL namespace.
long seed
Definition: chem4.cc:68
art::detail::EngineCreator::seed_t seed_t
NuRandomService::seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
std::string const  instance = "",
std::optional< seed_t > const  seed = std::nullopt 
)

Registers an existing engine with rndm::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
instancename of the engine
seedthe optional seed configuration parameter
Returns
the seed assigned to the engine (may be invalid)
See also
createEngine()

This function works similarly to createEngine(), but it uses an existing engine instead of creating a new one via art::RandomNumberGenerator service. The seeder function must be provided for the service to be of any use: registerEngine() will set the seed immediately, and the seeder function will be used to set the seed for policies that do that on each event. The instance name must also be unique, since for NuRandomService purposes the registered engine is no different from any other, created by art::RandomNumberGenerator or not.

Three standard functions are provided as seeders, for use with art::RandomNumberGenerator engines (RandomNumberGeneratorSeeder()), with a CLHEP::HepRandomEngine (CLHEPengineSeeder class) and with ROOT's TRandom (TRandomSeeder class). Note that CLHEP and ROOT classes are not compiled in NuRandomService by default, and the recommendation is to take their implementation as an example and create your own after them). Any seeder function with the prototype of NuRandomService::Seeder_t:

void Seeder(EngineId const&, seed_t);

or a functor with

void operator() (EngineId const&, seed_t);

can be used as seeder.

The engine seed will be set. If the seed optional parameter has a value, that value is used as seed. Otherwise, the seed is obtained from rndm::NuRandomService.

If the seed is not specified, it is obtained in a way equivalent to getSeed() (but multi-treading safe).

If also instance is not specified, the engine is created with no instance name (equivalent to an empty instance name).

Definition at line 128 of file NuRandomService_service.cc.

References extractSeed(), freezeSeed(), qualify_engine_label(), registerEngineAndSeeder(), and seedEngine().

Referenced by evgen::ActiveVolumeVertexSampler::ActiveVolumeVertexSampler(), declareEngine(), evgen::MARLEYHelper::MARLEYHelper(), and registerEngine().

131  {
133  registerEngineAndSeeder(id, seeder);
134  auto const [ seedValue, frozen ] = extractSeed(id, seed);
135  seedEngine(id); // seed it before freezing
136  if (frozen) freezeSeed(id, seedValue);
137  return seedValue;
138  } // NuRandomService::registerEngine(Seeder_t, string, ParameterSet, init list)
std::pair< seed_t, bool > extractSeed(EngineId const &id, std::optional< seed_t > seed)
const std::string instance
seed_t seedEngine(EngineId const &id)
Calls the seeder with the specified seed and engine ID.
void freezeSeed(EngineId const &id, seed_t frozen_seed)
Forces NuRandomService not to change the seed of the specified engine.
void registerEngineAndSeeder(EngineId const &id, SeedMaster_t::Seeder_t seeder)
Registers an engine and its seeder.
EngineId qualify_engine_label(std::string moduleLabel, std::string instanceName) const
Returns a fully qualified EngineId.
SeedMaster_t::EngineId EngineId
type of random engine ID
long seed
Definition: chem4.cc:68
NuRandomService::seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
std::string const  instance,
SeedAtom const &  seedParam 
)

Registers an existing engine with art::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
instancename of the engine
seedParamthe optional seed configuration parameter
Returns
the seed assigned to the engine (may be invalid)
See also
registerEngine()

This method works similarly to registerEngine(SeedMaster_t::Seeder_t, std::string const, std::optional<seed_t> const, with the difference that the seed is read from seedParam; if that optional parameter is not present, then the seed is obtained from rndm:::NuRandomService.

Definition at line 150 of file NuRandomService_service.cc.

References readSeedParameter(), and registerEngine().

154  {
155  return registerEngine(seeder, instance, readSeedParameter(seedParam));
156  } // NuRandomService::registerEngine(Seeder_t, string, ParameterSet, init list)
const std::string instance
static std::optional< seed_t > readSeedParameter(fhicl::ParameterSet const &pset, std::string pname)
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
SeedAtom const &  seedParam 
)
inline

Registers an existing engine with art::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
seedParamthe optional seed configuration parameter
Returns
the seed assigned to the engine (may be invalid)
See also
registerEngine()

This method works similarly to registerEngine(SeedMaster_t::Seeder_t, std::string const, std::optional<seed_t> const, with the difference that the seed is read from seedParam; if that optional parameter is not present, then the seed is obtained from rndm:::NuRandomService. In addition, the engine is always registered with no instance name.

Definition at line 890 of file NuRandomService.h.

891  { return registerEngine(seeder, "", seedParam); }
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
std::string  instance,
fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Registers an existing engine with art::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
instancename of the engine
psetparameter set to read parameters from
pnamename of the optional seed parameter
Returns
the seed assigned to the engine (may be invalid)
See also
createEngine()

This method works similarly to registerEngine(SeedMaster_t::Seeder_t, std::string const, std::optional<seed_t> const, but the preferred way to obtain the seed is from the configuration parameter named pname in pset. If that parameter is found, the seed is obtained from rndm::NuRandomService.

Definition at line 908 of file NuRandomService.h.

912  { return registerEngine(seeder, instance, pset, { pname }); }
const std::string instance
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
NuRandomService::seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
std::string  instance,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)

Registers an existing engine with art::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
instancename of the engine
psetparameter set to read parameters from
pnamesnames of the seed parameters
Returns
the seed assigned to the engine (may be invalid)
See also
createEngine()

This method works similarly to registerEngine(SeedMaster_t::Seeder_t, std::string const, std::optional<seed_t> const, but the preferred way to obtain the seed is from the first available configuration parameter in pset among the ones listed in pnames. If that parameter is found, the seed is obtained from rndm::NuRandomService.

Definition at line 141 of file NuRandomService_service.cc.

References readSeedParameter(), and registerEngine().

145  {
146  return registerEngine(seeder, instance, readSeedParameter(pset, pnames));
147  } // NuRandomService::registerEngine(Seeder_t, string, ParameterSet, init list)
const std::string instance
static std::optional< seed_t > readSeedParameter(fhicl::ParameterSet const &pset, std::string pname)
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
fhicl::ParameterSet const &  pset,
std::string  pname 
)
inline

Registers an existing engine with art::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
psetparameter set to read parameters from
pnamename of the optional seed parameter
Returns
the seed assigned to the engine (may be invalid)
See also
createEngine()

This method works similarly to registerEngine(SeedMaster_t::Seeder_t, std::string const, std::optional<seed_t> const, but the preferred way to obtain the seed is from the configuration parameter named pname in pset. If that parameter is found, the seed is obtained from rndm::NuRandomService. Also, the engine is assigned no instance name.

Definition at line 950 of file NuRandomService.h.

954  { return registerEngine(seeder, "", pset, pname); }
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( SeedMaster_t::Seeder_t  seeder,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)
inline

Registers an existing engine with art::NuRandomService.

Parameters
seederfunction used to set the seed of the existing engine
psetparameter set to read parameters from
pnamesnames of the seed parameters
Returns
the seed assigned to the engine (may be invalid)
See also
createEngine()

This method works similarly to registerEngine(SeedMaster_t::Seeder_t, std::string const, std::optional<seed_t> const, but the preferred way to obtain the seed is from the first available configuration parameter in pset among the ones listed in pnames. If that parameter is found, the seed is obtained from rndm::NuRandomService. Also, the engine is assigned no instance name.

Definition at line 971 of file NuRandomService.h.

975  { return registerEngine(seeder, "", pset, pnames); }
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( CLHEP::HepRandomEngine &  engine,
std::string  instance = "" 
)
inline

Registers an existing CLHEP engine with art::NuRandomService.

Parameters
enginea reference to the CLHEP random generator engine
instance(default: none) name of the engine
Returns
the seed assigned to the engine (may be invalid)

The specified engine is not managed. It may be owned by art::RandomNumberGenerator service.

The engine is expected to be valid as long as this service performs reseeding.

Definition at line 991 of file NuRandomService.h.

992  { return registerEngine(CLHEPengineSeeder(engine), instance); }
const std::string instance
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( CLHEP::HepRandomEngine &  engine,
std::string  instance,
SeedAtom const &  seedParam 
)
inline

Registers an existing CLHEP engine with art::NuRandomService.

Parameters
enginea reference to the CLHEP random generator engine
instancename of the engine
seedParamthe optional seed configuration parameter
Returns
the seed assigned to the engine (may be invalid)

The specified engine is not managed. It may be owned by art::RandomNumberGenerator service.

The engine is expected to be valid as long as this service performs reseeding.

Definition at line 1007 of file NuRandomService.h.

1011  { return registerEngine(CLHEPengineSeeder(engine), instance, seedParam); }
const std::string instance
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
seed_t rndm::NuRandomService::registerEngine ( CLHEP::HepRandomEngine &  engine,
std::string  instance,
fhicl::ParameterSet const &  pset,
std::initializer_list< std::string >  pnames 
)
inline

Registers an existing CLHEP engine with art::NuRandomService.

Parameters
enginea reference to the CLHEP random generator engine
instancename of the engine
psetparameter set to read parameters from
pnamesnames of the seed parameters
Returns
the seed assigned to the engine (may be invalid)

The specified engine is not managed. It may be owned by art::RandomNumberGenerator service.

The engine is expected to be valid as long as this service performs reseeding.

Definition at line 1027 of file NuRandomService.h.

1031  {
1032  return registerEngine
1033  (CLHEPengineSeeder(engine), instance, pset, pnames);
1034  }
const std::string instance
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string const instance="", std::optional< seed_t > const seed=std::nullopt)
Registers an existing engine with rndm::NuRandomService.
void rndm::NuRandomService::registerEngineAndSeeder ( EngineId const &  id,
SeedMaster_t::Seeder_t  seeder 
)
private

Registers an engine and its seeder.

Definition at line 278 of file NuRandomService_service.cc.

References ensureValidState(), hasEngine(), art::errors::LogicError, rndm::SeedMaster< SEED >::registerNewSeeder(), and seeds.

Referenced by prepareEngine(), registerEngine(), and reseedGlobal().

279  {
280  // Are we being called from the right place?
281  ensureValidState(id.isGlobal());
282 
283  if (hasEngine(id)) {
285  << "NuRandomService: an engine with ID '" << id.artName()
286  << "' has already been created!\n";
287  }
288  seeds.registerNewSeeder(id, seeder);
289  } // NuRandomService::registerEngineAndSeeder()
void registerNewSeeder(EngineId const &id, Seeder_t seeder)
Register the specified function to reseed the engine id.
Definition: SeedMaster.h:536
void ensureValidState(bool bGlobal=false) const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
SeedMaster_t seeds
Class managing the seeds.
NuRandomService::seed_t rndm::NuRandomService::registerEngineID ( EngineId const &  id,
SeedMaster_t::Seeder_t  seeder = SeedMaster_t::Seeder_t() 
)
private

Register an engine and seeds it with the seed from the master.

Definition at line 180 of file NuRandomService_service.cc.

References defineEngineID(), prepareEngine(), and seedEngine().

Referenced by getSeed().

183  {
184  prepareEngine(id, seeder);
185  return seedEngine(id);
186  } // NuRandomService::registerEngineID()
seed_t seedEngine(EngineId const &id)
Calls the seeder with the specified seed and engine ID.
seed_t prepareEngine(EngineId const &id, SeedMaster_t::Seeder_t seeder)
Registers the engine ID into SeedMaster.
void rndm::NuRandomService::reseedGlobal ( )
private

Reseed all the global engines.

Definition at line 268 of file NuRandomService_service.cc.

References rndm::SeedMaster< SEED >::engineIDsRange(), registerEngineAndSeeder(), reseedInstance(), and seeds.

Referenced by preProcessEvent().

268  {
269  for (EngineId const& ID: seeds.engineIDsRange()) {
270  if (!ID.isGlobal()) continue; // not global? neeext!!
271  reseedInstance(ID);
272  } // for
273  } // NuRandomService::reseedGlobal()
SeedMaster_t::EngineId EngineId
type of random engine ID
seed_t reseedInstance(EngineId const &id)
Reseeds the specified engine instance in the current module.
EngineInfoIteratorBox engineIDsRange() const
Returns an object to iterate in range-for through configured engine IDs.
Definition: SeedMaster.h:365
SeedMaster_t seeds
Class managing the seeds.
NuRandomService::seed_t rndm::NuRandomService::reseedInstance ( EngineId const &  id)
private

Reseeds the specified engine instance in the current module.

Parameters
instancethe name of the engine instance
Returns
the seed set, or InvalidSeed if no reseeding happened

Definition at line 241 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::getEventSeedInputData(), InvalidSeed, rndm::SeedMaster< SEED >::reseedEvent(), seed, seeds, and state.

Referenced by reseedGlobal(), and reseedModule().

241  {
242  // get all the information on the current process, event and module from
243  // ArtState:
245  seed_t const seed = seeds.reseedEvent(id, data);
246  if (seed == InvalidSeed) {
247  mf::LogDebug("NuRandomService")
248  << "No random seed specific to this event for engine '" << id << "'";
249  }
250  else {
251  mf::LogInfo("NuRandomService") << "Random seed for this event, engine '"
252  << id << "': " << seed;
253  }
254  return seed;
255  } // NuRandomService::reseedInstance()
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
EventSeedInputData getEventSeedInputData() const
Definition: ArtState.h:128
long seed
Definition: chem4.cc:68
typename PolicyImpl_t::EventData_t EventData_t
type of data used for event seeds
Definition: SeedMaster.h:255
NuRandomServiceHelper::ArtState state
static constexpr seed_t InvalidSeed
An invalid seed.
seed_t reseedEvent(EngineId const &id, EventData_t const &data)
Reseeds the specified engine with an event seed (if any)
Definition: SeedMaster.h:572
MaybeLogger_< ELseverityLevel::ELsev_success, false > LogDebug
SeedMaster_t seeds
Class managing the seeds.
art::detail::EngineCreator::seed_t seed_t
void rndm::NuRandomService::reseedModule ( std::string  currentModule)
private

Reseeds all the engines in the current module.

Definition at line 258 of file NuRandomService_service.cc.

References rndm::SeedMaster< SEED >::engineIDsRange(), reseedInstance(), and seeds.

258  {
259  for (EngineId const& ID: seeds.engineIDsRange()) {
260  if (ID.moduleLabel != currentModule) continue; // not our module? neeext!!
261  reseedInstance(ID);
262  } // for
263  } // NuRandomService::reseedModule(string)
SeedMaster_t::EngineId EngineId
type of random engine ID
seed_t reseedInstance(EngineId const &id)
Reseeds the specified engine instance in the current module.
EngineInfoIteratorBox engineIDsRange() const
Returns an object to iterate in range-for through configured engine IDs.
Definition: SeedMaster.h:365
SeedMaster_t seeds
Class managing the seeds.
void rndm::NuRandomService::reseedModule ( )
private

Reseeds all the engines in the current module.

Definition at line 265 of file NuRandomService_service.cc.

References rndm::NuRandomServiceHelper::ArtState::moduleLabel(), and state.

Referenced by preModule().

std::string moduleLabel() const
Getters.
Definition: ArtState.h:122
void reseedModule()
Reseeds all the engines in the current module.
NuRandomServiceHelper::ArtState state
seed_t rndm::NuRandomService::seedEngine ( EngineId const &  id)
inlineprivate

Calls the seeder with the specified seed and engine ID.

Definition at line 1311 of file NuRandomService.h.

References tca::evt, and rndm::SeedMaster< SEED >::reseed().

Referenced by defineEngineID(), registerEngine(), and registerEngineID().

1311 { return seeds.reseed(id); }
seed_t reseed(EngineId const &id)
Reseeds the specified engine with a global seed (if any)
Definition: SeedMaster.h:558
SeedMaster_t seeds
Class managing the seeds.

Member Data Documentation

bool rndm::NuRandomService::bPrintEndOfJobSummary = false
private

print a summary at the end of job

Definition at line 1229 of file NuRandomService.h.

Referenced by postEndJob().

constexpr seed_t rndm::NuRandomService::InvalidSeed = SeedMaster_t::InvalidSeed
static

An invalid seed.

Definition at line 421 of file NuRandomService.h.

Referenced by extractSeed(), and reseedInstance().

SeedMaster_t rndm::NuRandomService::seeds
private
NuRandomServiceHelper::ArtState rndm::NuRandomService::state
private

Helper to track state of art.

The state is updated by NuRandomService itself, and therefore knows only about what it is notified about, when it is notified about. For example, service construction phase may start before the service was even constructed, but the state will be updated only on NuRandomService construction.

Definition at line 1225 of file NuRandomService.h.

Referenced by ensureValidState(), postModule(), postModuleBeginRun(), postModuleConstruction(), postModuleEndJob(), postProcessEvent(), preModule(), preModuleBeginRun(), preModuleConstruction(), preModuleEndJob(), preProcessEvent(), qualify_engine_label(), reseedInstance(), and reseedModule().

int rndm::NuRandomService::verbosity = 0
private

Control the level of information messages.

Definition at line 1228 of file NuRandomService.h.

Referenced by postEndJob().


The documentation for this class was generated from the following files: