Geant4  10.02.p03
Random.cc
Go to the documentation of this file.
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // HEP Random
6 // --- HepRandom ---
7 // class implementation file
8 // -----------------------------------------------------------------------
9 // This file is part of Geant4 (simulation toolkit for HEP).
10 
11 // =======================================================================
12 // Gabriele Cosmo - Created: 5th September 1995
13 // - Minor corrections: 31st October 1996
14 // - Added methods for engine status: 19th November 1996
15 // - HepRandom defined as singleton, constructors are
16 // kept public for backward compatibility: 27th Feb 1998
17 // - Relocated Poisson and Gauss data and simplified
18 // initialisation of static generator: 5th Jan 1999
19 // =======================================================================
20 
21 #include <assert.h>
23 #include "CLHEP/Random/Random.h"
25 #include "CLHEP/Utility/memory.h"
27 
28 // -----------------------------
29 // Static members initialisation
30 // -----------------------------
31 
32 #include "CLHEP/Random/SeedTable.h"
33 
34 namespace CLHEP {
35 
36 
37 namespace {
38 
39 struct defaults {
40  defaults( HepRandom & g, HepJamesRandom & e )
41  : theGenerator( &g, do_nothing_deleter() )
42  , theEngine ( &e, do_nothing_deleter() )
43  { }
44 
45  void resetEngine( HepRandomEngine * newEngine ) {
46  theEngine.reset( newEngine );
47  }
48 
49  void resetEngine( HepRandomEngine & newEngine ) {
50  theEngine.reset( &newEngine, do_nothing_deleter() );
51  }
52 
53  bool ensureInitialized() {
54  assert( theGenerator.get() != 0 && theEngine.get() != 0 );
55  return true;
56  }
57 
58  ~defaults()
59  { }
60 
61  std::shared_ptr<HepRandom > theGenerator;
62  std::shared_ptr<HepRandomEngine> theEngine;
63 }; // defaults
64 
65  defaults & theDefaults() {
66  static CLHEP_THREAD_LOCAL HepRandom theDefaultGenerator;
67  static CLHEP_THREAD_LOCAL HepJamesRandom theDefaultEngine;
68  static CLHEP_THREAD_LOCAL defaults theDefaults(theDefaultGenerator, theDefaultEngine);
69  return theDefaults;
70  }
71 
72 } // namespace
73 
74 //---------------------------- HepRandom ---------------------------------
75 
77 { }
78 
80 {
81  setTheSeed(seed);
82 }
83 
85 {
86  theDefaults().resetEngine( algorithm );
87 }
88 
90 {
91  theDefaults().resetEngine( algorithm );
92 }
93 
95 { }
96 
98 {
99  return theDefaults().theEngine->flat();
100 }
101 
102 void HepRandom::flatArray(const int size, double* vect)
103 {
104  theDefaults().theEngine->flatArray(size,vect);
105 }
106 
108  return flat();
109 }
110 
111 std::string HepRandom::name() const {return "HepRandom";}
113  std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n";
114  return *theDefaults().theEngine.get();
115 }
116 
117 std::ostream & operator<< (std::ostream & os, const HepRandom & dist) {
118  return dist.put(os);
119 }
120 
121 std::istream & operator>> (std::istream & is, HepRandom & dist) {
122  return dist.get(is);
123 }
124 
125 std::ostream & HepRandom::put(std::ostream & os) const {return os;}
126 std::istream & HepRandom::get(std::istream & is) {return is;}
127 
128 // --------------------------
129 // Static methods definitions
130 // --------------------------
131 
132 void HepRandom::setTheSeed(long seed, int lux)
133 {
134  theDefaults().theEngine->setSeed(seed,lux);
135 }
136 
138 {
139  return theDefaults().theEngine->getSeed();
140 }
141 
142 void HepRandom::setTheSeeds(const long* seeds, int aux)
143 {
144  theDefaults().theEngine->setSeeds(seeds,aux);
145 }
146 
148 {
149  return theDefaults().theEngine->getSeeds();
150 }
151 
152 void HepRandom::getTheTableSeeds(long* seeds, int index)
153 {
154  if ((index >= 0) && (index < 215)) {
155  seeds[0] = seedTable[index][0];
156  seeds[1] = seedTable[index][1];
157  }
158  else seeds = NULL;
159 }
160 
162 {
163  return theDefaults().theGenerator.get();
164 }
165 
167 {
168  return theDefaults().theEngine.get();
169 }
170 
172 {
173  theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
174 }
175 
176 void HepRandom::saveEngineStatus( const char filename[] )
177 {
178  theDefaults().theEngine->saveStatus( filename );
179 }
180 
181 void HepRandom::restoreEngineStatus( const char filename[] )
182 {
183  theDefaults().theEngine->restoreStatus( filename );
184 }
185 
186 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
187  os << *getTheEngine();
188  return os;
189 }
190 
191 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
192  is >> *getTheEngine();
193  return is;
194 }
195 
196 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
197  return StaticRandomStates::save(os);
198 }
199 
200 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
201  return StaticRandomStates::restore(is);
202 }
203 
205 {
206  theDefaults().theEngine->showStatus();
207 }
208 
210 {
211  return static_cast<int>( theDefaults().ensureInitialized() );
212 }
213 
214 } // namespace CLHEP
virtual std::ostream & put(std::ostream &os) const
Definition: Random.cc:125
static const long * getTheSeeds()
Definition: Random.cc:147
static HepRandom * getTheGenerator()
Definition: Random.cc:161
std::shared_ptr< HepRandom > theGenerator
Definition: Random.cc:61
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:181
Int_t index
std::shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:62
static void setTheSeeds(const long *seeds, int aux=-1)
Definition: Random.cc:142
virtual std::istream & get(std::istream &is)
Definition: RandomEngine.cc:61
void flatArray(const int size, double *vect)
Definition: Random.cc:102
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:132
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
std::ostream & operator<<(std::ostream &os, const HepRandom &dist)
Definition: Random.cc:117
virtual std::string name() const
Definition: Random.cc:111
virtual HepRandomEngine & engine()
Definition: Random.cc:112
static const double g
double flat()
Definition: G4AblaRandom.cc:47
static void showEngineStatus()
Definition: Random.cc:204
static const double lux
static std::istream & restore(std::istream &is)
#define CLHEP_THREAD_LOCAL
Definition: thread_local.h:25
virtual ~HepRandom()
Definition: Random.cc:94
double flat()
Definition: Random.cc:97
static long getTheSeed()
Definition: Random.cc:137
static int createInstance()
Definition: Random.cc:209
static std::ostream & save(std::ostream &os)
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:176
Definition: DoubConv.h:18
static std::ostream & saveStaticRandomStates(std::ostream &os)
Definition: Random.cc:196
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:171
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:186
std::istream & operator>>(std::istream &is, HepRandom &dist)
Definition: Random.cc:121
virtual double operator()()
Definition: Random.cc:107
virtual std::istream & get(std::istream &is)
Definition: Random.cc:126
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:191
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:152
static std::istream & restoreStaticRandomStates(std::istream &is)
Definition: Random.cc:200