9 #include "framework/core/RandomGenerator.h"
10 #include "framework/utilities/sha3hash/Hash.h"
11 #include "framework/logging/Logger.h"
12 #include "framework/datastore/StoreObjPtr.h"
13 #include "framework/dataobjects/EventMetaData.h"
25 template<
class T>
void addValueToBuffer(std::vector<unsigned char>& buffer,
const T& value)
27 static_assert(std::is_integral<T>::value,
"integral type required");
28 constexpr
int bytes =
sizeof(T);
29 for (
int i = bytes - 1; i >= 0; --i) {
30 buffer.push_back((value >> (i * 8)) & 0xFF);
38 SetName(name.c_str());
39 SetTitle(
"Belle2 Random Generator");
46 std::copy_n(seed, n,
m_seed.data());
62 std::vector<unsigned char> buffer;
74 B2ERROR(
"No EventMetaData, cannot set state of RandomGenerator from event data");
77 addValueToBuffer(buffer, evt->getExperiment());
78 addValueToBuffer(buffer, evt->getRun());
81 addValueToBuffer(buffer, evt->getEvent());
86 hash.
update(buffer.size(), buffer.data());
94 #ifndef LOG_NO_B2DEBUG
96 std::stringstream info;
97 info <<
"Random Generator '" << GetName() <<
"' State info:\n";
98 info <<
" seed (" << std::dec <<
m_seed.size() <<
"):\n ";
99 for (
auto c :
m_seed) { info << std::setw(2) << std::setfill(
'0') << std::hex << (int)c <<
" "; }
100 info <<
"\n event info (mode=" <<
m_mode <<
"): \n";
101 info <<
" barrier:" << std::dec <<
m_barrier;
104 info <<
" EXP:" << evt->getExperiment() <<
" RUN:" << evt->getRun();
106 info <<
" EVT:" << evt->getEvent();
109 info <<
"\n event bytes (" << std::dec << buffer.size() <<
"):\n ";
110 for (
auto c : buffer) { info << std::setw(2) << std::setfill(
'0') << std::hex << (int)c <<
" "; }
111 info <<
"\n state (index=" <<
m_index <<
"): ";
112 for (
int i = 0; i < 16; ++i) {
113 info << ((i % 4 == 0) ?
"\n " :
" ") << std::setw(16) << std::setfill(
'0') << std::hex <<
m_state[i];
123 RndmArray(n /
sizeof(ULong64_t), (ULong64_t*)array);
124 const Int_t remainder = n %
sizeof(ULong64_t);
129 std::copy_n((
unsigned char*)&r, remainder, array + (n - remainder - 1));
@ c_Debug
Debug: for code development.
static LogSystem & Instance()
Static method to get a reference to the LogSystem instance.
void setState(int barrier)
Set the state of the random number generator.
@ c_eventDependent
Use experiment, run and event number to generate state.
@ c_independent
Don't use event info to generate state.
uint64_t m_state[16]
Internal state of the random number generator.
unsigned int m_index
currently active index in the internal state
int m_barrier
current barrier index.
EGeneratorMode m_mode
Current generator mode.
void setSeed(const unsigned char *seed, unsigned int n)
Set the seed information.
std::vector< unsigned char > m_seed
seed information
RandomGenerator(const std::string &name="Belle2 Random Generator")
Default constructor, does not initialize the generator.
void barrier()
increase the barrier index.
Simple interface to calculate SHAKE256 hash sum (FIPS 202 draft) with variable size from given data i...
@ c_SHAKE256
variable hash size with up to 256 bit collision resistance
Type-safe access to single objects in the data store.
void update(int n, unsigned char *buff)
update the internal state by adding n bytes of data from buff
uint64_t random64()
Generate one 64bit unsigned integer between 0 and UINT64_MAX (both inclusive).
void RndmArray(Int_t n, Float_t *array)
Fill an array of floats with random values in (0,1), both limits excluded.
void getHash(int n, unsigned char *buff)
obtain the hash value with a length of n bytes into buff
Abstract base class for different kinds of events.