ATLAS Offline Software
RNGWrapper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include "CLHEP/Random/RandomEngine.h"
7 #include "CxxUtils/MurmurHash2.h"
8 #include "CxxUtils/crc64.h"
9 #include "crc_combine.h"
10 
16  uint32_t theHash = static_cast<uint32_t>(ev);
17  if (0 != offset) theHash=crc_combine(theHash, offset);
18  uint32_t runNumber = static_cast<uint32_t>(run);
19  theHash=crc_combine(theHash, runNumber);
20  theHash=crc_combine(theHash, algName);
21  return theHash;
22 }
23 
26 size_t ATHRNG::calculateSeedMC20(const std::string& algName, uint64_t ev, uint64_t run) {
27  auto algHash = std::hash<std::string>{}(algName);
28  auto evHash = std::hash<uint64_t>{}(ev);
29  auto runHash = std::hash<uint64_t>{}(run);
30  auto hsh = evHash ^ (runHash + (evHash << 6) + (evHash >> 2));
31  hsh = hsh ^ (algHash + (hsh << 6) + (hsh >> 2));
32  return hsh;
33 }
34 
37 void ATHRNG::calculateSeedsMC21(long* seeds, const std::string& algName, uint64_t ev, uint64_t run, uint64_t offset) {
38  //RanecuEngine only takes the first seed, so the first seed should be good on it's own
39  //using 64bit crc which is already performing quite well on it's own
40  uint64_t theHash = CxxUtils::crc64(algName);
41  theHash = CxxUtils::crc64addint(theHash,ev);
42  theHash = CxxUtils::crc64addint(theHash,offset);
43  theHash = CxxUtils::crc64addint(theHash,run);
44 
45  size_t iseed=0;
46  seeds[iseed]=(long)theHash; //CLHEP takes a zero terminated array for seeding. Avoid 0
47  if(seeds[iseed]!=0) ++iseed; //if zero, make the seed vector shorter, which should in itself already cause a different seeding
48  seeds[iseed]=(long)ev; //CLHEP takes a zero terminated array for seeding. Avoid ev==0
49  if(seeds[iseed]!=0) ++iseed; //Avoid 0
50  seeds[iseed]=(long)(ev >> 32); //explicitly split out the upper 32bit as dSFMTEngine only takes 32bits.
51  if(seeds[iseed]!=0) ++iseed; //Avoid 0
52  seeds[iseed]=(long)run;
53  if(seeds[iseed]!=0) ++iseed; //Avoid 0
54  uint64_t algHash = CxxUtils::MurmurHash64A ( algName.data(), algName.size(), 0 );
55  seeds[iseed]=(long)algHash;
56  if(seeds[iseed]!=0) ++iseed; //Avoid 0
57  seeds[iseed]=(long)offset;
58  if(seeds[iseed]!=0) ++iseed; //Avoid 0
59  seeds[iseed]=0; //CLHEP takes a zero terminated array for seeding, so end with 0
60  return;
61 }
62 
64  long seeds[7];
66  return seeds[0];
67 }
68 
69 ATHRNG::RNGWrapper::RNGWrapper(const factoryFunc& genFact, size_t nSlots)
70 {
71  // Construct the random engines; one per event slot.
72  m_engines.reserve(nSlots);
73  for(size_t t = 0; t < nSlots; t++){
74  m_engines.emplace_back(genFact());
75  }
76  m_evtSeeded.resize (nSlots, EventContext::INVALID_CONTEXT_EVT);
77 }
78 
80 {
81  // Clean up the allocated engines
82  for(auto engPtr : m_engines) {
83  delete engPtr;
84  }
85 }
86 
87 void ATHRNG::RNGWrapper::setSeedMC20(const std::string& algName, size_t slot,
89  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
90 {
91  auto hsh = calculateSeedMC20(algName, ev, run);
92  setSeed(slot, hsh, evt);
93 }
94 
95 void ATHRNG::RNGWrapper::setSeedMC21(const std::string& algName, size_t slot,
97  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
98 {
99  long seeds[7]; //CLHEP uses long for seeding
101  setSeeds(slot, seeds, evt);
102 }
103 
104 void ATHRNG::RNGWrapper::setSeedLegacy(const std::string& algName, size_t slot,
106  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
107 {
108  if(seeding==MC16Seeding) {
109  // Use MC16 legacy seeding
110  setSeedMC16(algName, slot, ev, run, offset, evt);
111  return;
112  }
113  if(seeding==MC20Seeding) {
114  // Use MC20 seeding
115  setSeedMC20(algName, slot, ev, run, evt);
116  return;
117  }
118  // Use MC21 seeding
119  setSeedMC21(algName, slot, ev, run, offset, evt);
120 }
121 
123  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
124 {
126  setSeed(slot, theHash, evt);
127 }
128 
129 void ATHRNG::RNGWrapper::setSeed(size_t slot, size_t seed,
130  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
131 {
132  m_engines[slot]->setSeed(seed, 0);
133  if (evt == EventContext::INVALID_CONTEXT_EVT) {
134  evt = Gaudi::Hive::currentContext().evt();
135  }
136  m_evtSeeded[slot] = evt;
137 }
138 
139 void ATHRNG::RNGWrapper::setSeeds(size_t slot, const long * seeds,
140  EventContext::ContextEvt_t evt /*= EventContext::INVALID_CONTEXT_EVT*/)
141 {
142  m_engines[slot]->setSeeds(seeds, 0);
143  if (evt == EventContext::INVALID_CONTEXT_EVT) {
144  evt = Gaudi::Hive::currentContext().evt();
145  }
146  m_evtSeeded[slot] = evt;
147 }
ATHRNG::RNGWrapper::setSeed
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition: RNGWrapper.h:169
CxxUtils::MurmurHash64A
uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
Definition: MurmurHash2.cxx:103
ATHRNG::RNGWrapper::SeedingOptionType
SeedingOptionType
Options for seeding option=0 is setSeed as in MC20 option=1 is setSeedLegacy as in MC16 option=2 is s...
Definition: RNGWrapper.h:97
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
ATHRNG::RNGWrapper::setSeedLegacy
void setSeedLegacy(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, uint64_t offset, SeedingOptionType seeding, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:104
xAOD::JetAlgorithmType::algName
const std::string & algName(ID id)
Converts a JetAlgorithmType::ID into a string.
Definition: JetContainerInfo.cxx:67
ATHRNG::RNGWrapper::setSeedMC21
void setSeedMC21(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, uint64_t offset=0, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:95
run
int run(int argc, char *argv[])
Definition: ttree2hdf5.cxx:28
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
crc_combine.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATHRNG::RNGWrapper::RNGWrapper
RNGWrapper(const factoryFunc &genFact, size_t nSlots)
Constructor takes a factory function which can instantiate a CLHEP::HepRandomEngine and the number of...
Definition: RNGWrapper.cxx:69
ATHRNG::RNGWrapper::setSeeds
void setSeeds(size_t slot, const long *seeds, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the seed value(s) directly for a specified slot.
Definition: RNGWrapper.cxx:139
crc_combine
uint32_t crc_combine(uint32_t seed, uint32_t v)
using crc32 for architecture independence in combining the seeds
Definition: AthenaKernel/src/crc_combine.h:11
ev
int ev
Definition: globals.cxx:25
ATHRNG::RNGWrapper::m_engines
std::vector< CLHEP::HepRandomEngine * > m_engines
Vector of random engines, ordered by slot number.
Definition: RNGWrapper.h:153
ATHRNG::calculateSeedMC16
size_t calculateSeedMC16(const std::string &algName, uint64_t ev, uint64_t run, uint32_t offset=0)
Helper methods.
Definition: RNGWrapper.cxx:15
CxxUtils::crc64addint
uint64_t crc64addint(uint64_t crc, uint64_t x)
Extend a previously-calculated CRC to include an int.
Definition: crc64.cxx:732
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
run
Definition: run.py:1
ATHRNG::RNGWrapper::~RNGWrapper
~RNGWrapper()
Definition: RNGWrapper.cxx:79
CxxUtils::crc64
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition: crc64.cxx:696
ATHRNG::RNGWrapper::setSeedMC20
void setSeedMC20(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:87
RNGWrapper.h
ATHRNG::calculateSeedMC20
size_t calculateSeedMC20(const std::string &algName, uint64_t ev, uint64_t run)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:26
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
ATHRNG::RNGWrapper::m_evtSeeded
std::vector< EventContext::ContextEvt_t > m_evtSeeded
Event counter when the engine was last seeded.
Definition: RNGWrapper.h:156
ATHRNG::calculateSeedsMC21
void calculateSeedsMC21(long *seeds, const std::string &algName, uint64_t ev, uint64_t run, uint64_t offset=0)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:37
ATHRNG::RNGWrapper::factoryFunc
std::function< CLHEP::HepRandomEngine *(void)> factoryFunc
Definition: RNGWrapper.h:58
crc64.h
A crc-64 implementation, using pclmul where possible.
ATHRNG::calculateSeedsPython
long calculateSeedsPython(const std::string &algName, uint64_t ev, uint64_t run, uint64_t offset=0)
Definition: RNGWrapper.cxx:63
MurmurHash2.h
Implementation of MurmurHash2.
ATHRNG::RNGWrapper::setSeedMC16
void setSeedMC16(const std::string &algName, size_t slot, uint64_t ev, uint64_t run, uint32_t offset=0, EventContext::ContextEvt_t evt=EventContext::INVALID_CONTEXT_EVT)
Set the random seed using a string (e.g.
Definition: RNGWrapper.cxx:122