DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4EventSeed.h
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : A.Sailer
11 //
12 //==========================================================================
13 #ifndef DDG4_PLUGINS_GEANT4EVENTSEED_H
14 #define DDG4_PLUGINS_GEANT4EVENTSEED_H
15 
16 // Framework include files
17 #include <DDG4/Geant4RunAction.h>
18 
19 // fallthrough only exists from c++17
20 #if defined __has_cpp_attribute
21  #if __has_cpp_attribute(fallthrough)
22  #define ATTR_FALLTHROUGH [[fallthrough]]
23  #else
24  #define ATTR_FALLTHROUGH
25  #endif
26 #else
27  #define ATTR_FALLTHROUGH
28 #endif
29 
30 
32 namespace dd4hep {
33 
35  namespace sim {
36 
52  class Geant4EventSeed: public Geant4RunAction {
54 
55  protected:
56  unsigned int m_initialSeed;
57  unsigned int m_runID;
58  std::string m_type;
60  public:
62  Geant4EventSeed(Geant4Context*, const std::string& );
64  virtual ~Geant4EventSeed();
66  void begin(const G4Run*);
68  void beginEvent(const G4Event*);
69  };
70 
71  /*
72 
73  Hashing for random seed as used in Marlin EventSeeder processor
74 
75  Original source by Bob Jenkins
76 
77  http://www.burtleburtle.net/bob/hash/doobs.html
78 
79  Hash a variable-length key into a 32-bit value
80 
81  */
82 
83 #define hashsize(n) ( 1U << (n) )
84 #define hashmask(n) ( hashsize ( n ) - 1 )
85 
86 
87  /*
88  --------------------------------------------------------------------
89  mix -- mix 3 32-bit values reversibly.
90  For every delta with one or two bits set, and the deltas of all three
91  high bits or all three low bits, whether the original value of a,b,c
92  is almost all zero or is uniformly distributed,
93  * If mix() is run forward or backward, at least 32 bits in a,b,c
94  have at least 1/4 probability of changing.
95  * If mix() is run forward, every bit of c will change between 1/3 and
96  2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
97  mix() was built out of 36 single-cycle latency instructions in a
98  structure that could supported 2x parallelism, like so:
99  a -= b;
100  a -= c; x = (c>>13);
101  b -= c; a ^= x;
102  b -= a; x = (a<<8);
103  c -= a; b ^= x;
104  c -= b; x = (b>>13);
105  ...
106  Unfortunately, superscalar Pentiums and Sparcs can't take advantage
107  of that parallelism. They've also turned some of those single-cycle
108  latency instructions into multi-cycle latency instructions. Still,
109  this is the fastest good hash I could find. There were about 2^^68
110  to choose from. I only looked at a billion or so.
111  --------------------------------------------------------------------
112  */
113 #define mix(a,b,c) \
114  { \
115  a -= b; a -= c; a ^= (c>>13); \
116  b -= c; b -= a; b ^= (a<<8); \
117  c -= a; c -= b; c ^= (b>>13); \
118  a -= b; a -= c; a ^= (c>>12); \
119  b -= c; b -= a; b ^= (a<<16); \
120  c -= a; c -= b; c ^= (b>>5); \
121  a -= b; a -= c; a ^= (c>>3); \
122  b -= c; b -= a; b ^= (a<<10); \
123  c -= a; c -= b; c ^= (b>>15); \
124  }
125 
126  /*
127  --------------------------------------------------------------------
128  jenkins_hash() -- hash a variable-length key into a 32-bit value
129  k : the key (the unaligned variable-length array of bytes)
130  len : the length of the key, counting by bytes
131  initval : can be any 4-byte value
132  Returns a 32-bit value. Every bit of the key affects every bit of
133  the return value. Every 1-bit and 2-bit delta achieves avalanche.
134  About 6*len+35 instructions.
135 
136  The best hash table sizes are powers of 2. There is no need to do
137  mod a prime (mod is sooo slow!). If you need less than 32 bits,
138  use a bitmask. For example, if you need only 10 bits, do
139  h = (h & hashmask(10));
140  In which case, the hash table should have hashsize(10) elements.
141 
142  If you are hashing n strings (ub1 **)k, do it like this:
143  for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
144 
145  By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
146  code any way you wish, private, educational, or commercial. It's free.
147 
148  See http://burtleburtle.net/bob/hash/evahash.html
149  Use for hash table lookup, or anything where one collision in 2^^32 is
150  acceptable. Do NOT use for cryptographic purposes.
151  --------------------------------------------------------------------
152  */
153  unsigned jenkins_hash ( unsigned char *k, unsigned length, unsigned initval )
154  {
155  unsigned a, b;
156  unsigned c = initval;
157  unsigned len = length;
158 
159  a = b = 0x9e3779b9;
160 
161  while ( len >= 12 ) {
162  a += ( k[0] + ( (unsigned)k[1] << 8 )
163  + ( (unsigned)k[2] << 16 )
164  + ( (unsigned)k[3] << 24 ) );
165  b += ( k[4] + ( (unsigned)k[5] << 8 )
166  + ( (unsigned)k[6] << 16 )
167  + ( (unsigned)k[7] << 24 ) );
168  c += ( k[8] + ( (unsigned)k[9] << 8 )
169  + ( (unsigned)k[10] << 16 )
170  + ( (unsigned)k[11] << 24 ) );
171 
172  mix ( a, b, c );
173 
174  k += 12;
175  len -= 12;
176  }
177 
178  c += length;
179 
180  switch ( len ) {
181  case 11: c += ( (unsigned)k[10] << 24 ); ATTR_FALLTHROUGH;
182  case 10: c += ( (unsigned)k[9] << 16 ); ATTR_FALLTHROUGH;
183  case 9 : c += ( (unsigned)k[8] << 8 ); ATTR_FALLTHROUGH;
184  /* First byte of c reserved for length */
185  case 8 : b += ( (unsigned)k[7] << 24 ); ATTR_FALLTHROUGH;
186  case 7 : b += ( (unsigned)k[6] << 16 ); ATTR_FALLTHROUGH;
187  case 6 : b += ( (unsigned)k[5] << 8 ); ATTR_FALLTHROUGH;
188  case 5 : b += k[4]; ATTR_FALLTHROUGH;
189  case 4 : a += ( (unsigned)k[3] << 24 ); ATTR_FALLTHROUGH;
190  case 3 : a += ( (unsigned)k[2] << 16 ); ATTR_FALLTHROUGH;
191  case 2 : a += ( (unsigned)k[1] << 8 ); ATTR_FALLTHROUGH;
192  case 1 : a += k[0];
193  }
194 
195  mix ( a, b, c );
196 
197  return c;
198  }
199 
201  unsigned int hash( unsigned int initialSeed, unsigned int eventNumber, unsigned int runNumber ){
202  unsigned int seed = 0;
203  unsigned char * c = (unsigned char *) &eventNumber ;
204  seed = jenkins_hash( c, sizeof eventNumber, seed) ;
205 
206  c = (unsigned char *) &runNumber ;
207  seed = jenkins_hash( c, sizeof runNumber, seed) ;
208 
209  c = (unsigned char *) &initialSeed ;
210  seed = jenkins_hash( c, sizeof initialSeed, seed) ;
211 
212  return seed;
213  }
214 
215  } // End namespace sim
216 } // End namespace dd4hep
217 
218 #endif // DDG4_PLUGINS_GEANT4EVENTSEED_H
dd4hep::sim::Geant4EventSeed::begin
void begin(const G4Run *)
begin-of-run callback
Definition: Geant4EventSeed.cpp:51
Geant4RunAction.h
dd4hep::sim::Geant4EventSeed::~Geant4EventSeed
virtual ~Geant4EventSeed()
Default destructor.
Definition: Geant4EventSeed.cpp:46
dd4hep::sim::Geant4EventSeed::Geant4EventSeed
Geant4EventSeed(Geant4Context *, const std::string &)
Standard constructor with initializing arguments.
Definition: Geant4EventSeed.cpp:34
dd4hep::sim::Geant4EventSeed::m_initialSeed
unsigned int m_initialSeed
Definition: Geant4EventSeed.h:56
mix
#define mix(a, b, c)
Definition: Geant4EventSeed.h:113
dd4hep::sim::Geant4EventSeed::beginEvent
void beginEvent(const G4Event *)
begin-of-event callback
Definition: Geant4EventSeed.cpp:65
ATTR_FALLTHROUGH
#define ATTR_FALLTHROUGH
Definition: Geant4EventSeed.h:27
dd4hep::sim::Geant4EventSeed::m_runID
unsigned int m_runID
Definition: Geant4EventSeed.h:57
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::hash
unsigned int hash(unsigned int initialSeed, unsigned int eventNumber, unsigned int runNumber)
calculate hash from initialSeed, eventID and runID
Definition: Geant4EventSeed.h:201
dd4hep::sim::Geant4EventSeed::m_type
std::string m_type
Definition: Geant4EventSeed.h:58
dd4hep::sim::jenkins_hash
unsigned jenkins_hash(unsigned char *k, unsigned length, unsigned initval)
Definition: Geant4EventSeed.h:153
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4EventSeed::m_initialised
bool m_initialised
Definition: Geant4EventSeed.h:59