Belle II Software light-2406-ragdoll
RandomNumbers Class Reference

The class for handling the random number generation. More...

#include <RandomNumbers.h>

Collaboration diagram for RandomNumbers:

Static Public Member Functions

static void initialize ()
 Initialize the random number generator with a unique random seed;.
 
static void initialize (const std::string &seed)
 Initialize the random number generator.
 
static bool isInitialized ()
 Truth that the random number generator has been initialized.
 
static void initializeBeginRun ()
 Initialize run independent random generator for begin run.
 
static void initializeEndRun ()
 Initialize run independent random generator for end run.
 
static void initializeEvent (bool force=false)
 Initialize event information.
 
static void useEventDependent ()
 Set Event dependent Random Generator as current one.
 
static RandomGeneratorgetEventRandomGenerator ()
 return reference to the event dependent random generator
 
static void barrier ()
 Increase random barrier.
 
static void setSeed (const std::string &seed)
 Sets the seed for the random number generator.
 
static std::string getSeed ()
 Get the random number generator seed.
 
static void exposePythonAPI ()
 Exposes methods of the RandomNumbers class to Python.
 

Static Private Attributes

static RandomGenerators_evtRng {nullptr}
 event dependent random generator to be used for event processing
 
static RandomGenerators_runRng {nullptr}
 event independent random generator to be used for begin/end run processing
 
static std::string s_initialSeed
 The random number generator seed set by the user.
 
static int s_barrierOffset
 barrier index offset to be used in begin/endRun.
 

Detailed Description

The class for handling the random number generation.

Definition at line 23 of file RandomNumbers.h.

Member Function Documentation

◆ barrier()

void barrier ( )
static

Increase random barrier.

current random generator will be reseeded with a different "barrier index" which makes it's state independent from previous calls

Definition at line 69 of file RandomNumbers.cc.

70{
71 auto* gen = dynamic_cast<RandomGenerator*>(gRandom);
72 if (!gen) {
73 B2ERROR("Random Generator gRandom is not Belle2::RandomGenerator, cannot increase barrier");
74 } else {
75 gen->barrier();
76 }
77}
Fast Random number Generator using on xorshift1024* [arXiv:1402.6246].

◆ exposePythonAPI()

void exposePythonAPI ( )
static

Exposes methods of the RandomNumbers class to Python.

Definition at line 150 of file RandomNumbers.cc.

151{
152 using namespace boost::python;
153
154 //don't show c++ signature in python doc to keep it simple
155 docstring_options options(true, true, false);
156 //Expose RandomNumbers class
157 def("set_random_seed", &setPythonSeed, args("seed"), R"DOCSTRING(
158Set the random seed. The argument can be any object and will be converted to a
159string using the builtin str() function and will be used to initialize the
160random generator.)DOCSTRING");
161 def("get_random_seed", &RandomNumbers::getSeed, "Return the current random seed");
162}
static std::string getSeed()
Get the random number generator seed.
Definition: RandomNumbers.h:92

◆ getEventRandomGenerator()

static RandomGenerator & getEventRandomGenerator ( )
inlinestatic

return reference to the event dependent random generator

Definition at line 73 of file RandomNumbers.h.

73{ return *s_evtRng; }
static RandomGenerator * s_evtRng
event dependent random generator to be used for event processing

◆ getSeed()

static std::string getSeed ( )
inlinestatic

Get the random number generator seed.

Returns
The initial random number generator seed.

Definition at line 92 of file RandomNumbers.h.

92{return s_initialSeed;};
static std::string s_initialSeed
The random number generator seed set by the user.

◆ initialize() [1/2]

void initialize ( )
static

Initialize the random number generator with a unique random seed;.

Definition at line 32 of file RandomNumbers.cc.

33{
34 //use std::random_device to get non deterministic initial seed to be used if
35 //user doesn't set one
36 std::random_device random_device;
37 std::stringstream seed;
38 for (int i = 0; i < 8; ++i) {
39 seed << std::hex << random_device();
40 }
41 initialize(seed.str());
42}
static void initialize()
Initialize the random number generator with a unique random seed;.

◆ initialize() [2/2]

void initialize ( const std::string &  seed)
static

Initialize the random number generator.

Parameters
seedThe random number generator seed.

Definition at line 44 of file RandomNumbers.cc.

45{
46 s_initialSeed = seed;
47 if (!s_evtRng) {
48 s_evtRng = new RandomGenerator("event generator");
49 }
50 if (!s_runRng) {
51 s_runRng = new RandomGenerator("independent generator");
52 }
53 auto* gen = dynamic_cast<RandomGenerator*>(gRandom);
54 if (!gen) {
55 delete gRandom;
56 B2DEBUG(100, "Replacing gRandom from " << gRandom << " to " << gen);
57 }
58 gRandom = s_evtRng;
60 s_evtRng->setSeed((const unsigned char*)seed.c_str(), seed.size());
61 s_runRng->setSeed((const unsigned char*)seed.c_str(), seed.size());
62}
@ c_independent
Don't use event info to generate state.
void setSeed(const unsigned char *seed, unsigned int n)
Set the seed information.
void setMode(EGeneratorMode mode)
Set the generator mode.
static RandomGenerator * s_runRng
event independent random generator to be used for begin/end run processing

◆ initializeBeginRun()

void initializeBeginRun ( )
static

Initialize run independent random generator for begin run.

Called by EventProcessor, should not be called by other users

Definition at line 85 of file RandomNumbers.cc.

86{
87 gRandom = s_runRng;
89 //This might be called in in main or output process. In that case we don't
90 //know how many random barriers came before but we can look at the s_evtRng
91 //as this was passed from the previous process and contains the current
92 //barrier number in the event flow. We save it in intializeEvent so let's use
93 //it now.
95};
void setBarrier(int barrierIndex)
manually set the barrier index to a fixed value
@ c_runDependent
Use experiment and run number to generate state.
static int s_barrierOffset
barrier index offset to be used in begin/endRun.

◆ initializeEndRun()

void initializeEndRun ( )
static

Initialize run independent random generator for end run.

Called by EventProcessor, should not be called by other users

Definition at line 97 of file RandomNumbers.cc.

98{
99 gRandom = s_runRng;
101 //We set the barrier index to it's minimum possible value: usually barrier
102 //index starts at 0 but for endRun we set it to a negative number large
103 //enough that there is no realistic chance that beginRun had the same barrier
104 //index.
106}

◆ initializeEvent()

void initializeEvent ( bool  force = false)
static

Initialize event information.

Called by EventProcessor, should not be called by other users

Parameters
forceif true force initialization even in parallel processing mode for all processes and ignore the generator state that might have been passed in the event stream. This is only used on HLT event processor

Definition at line 108 of file RandomNumbers.cc.

109{
111 //we pass the random generator to other processes in multiprocessing so we only
112 //want to initialize it in the input process (or if there is no multi processing)a
113 //However on HLT we do things differently and so we need to initialize on all
114 //worker processes. So the HLT event processor can request initialization in
115 //all cases.
117 // when in event loop we want an error if there is no EventMetaData
120 }
121 //Ok, now we either have reset the barrier to 0 if we are an input process or
122 //have decided to keep the state from the previous process. In any case, this
123 //is the start barrier we want to use for begin/end of run processing so we
124 //keep it.
126}
static bool isInputProcess()
Return true if the process is an input process.
Definition: ProcHandler.cc:228
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
@ c_eventDependent
Use experiment, run and event number to generate state.
void initialize()
set the State from event meta information like experiment, run, and event number.
int getBarrier() const
obtain the currently active barrier id
static void useEventDependent()
Set Event dependent Random Generator as current one.

◆ isInitialized()

bool isInitialized ( )
static

Truth that the random number generator has been initialized.

Definition at line 64 of file RandomNumbers.cc.

65{
66 return (s_evtRng != nullptr);
67}

◆ setSeed()

void setSeed ( const std::string &  seed)
static

Sets the seed for the random number generator.

Parameters
seedThe random number generator seed.

Definition at line 79 of file RandomNumbers.cc.

80{
81 initialize(seed);
82 B2INFO("The random number seed is set to \"" << seed << '"');
83}

◆ useEventDependent()

void useEventDependent ( )
static

Set Event dependent Random Generator as current one.

Called by EventProcessor, should not be called by other users

Definition at line 128 of file RandomNumbers.cc.

129{
130 gRandom = s_evtRng;
131}

Member Data Documentation

◆ s_barrierOffset

int s_barrierOffset
staticprivate

barrier index offset to be used in begin/endRun.

Obtained from event dependent generator

Definition at line 113 of file RandomNumbers.h.

◆ s_evtRng

RandomGenerator * s_evtRng {nullptr}
staticprivate

event dependent random generator to be used for event processing

Definition at line 106 of file RandomNumbers.h.

◆ s_initialSeed

std::string s_initialSeed
staticprivate

The random number generator seed set by the user.

initialized to a non-deterministic 64byte hex string if not set by user

Definition at line 111 of file RandomNumbers.h.

◆ s_runRng

RandomGenerator * s_runRng {nullptr}
staticprivate

event independent random generator to be used for begin/end run processing

Definition at line 108 of file RandomNumbers.h.


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