Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
22 #include "CLHEP/Random/Random.h"
24 #include "CLHEP/Utility/memory.h"
25 
26 // -----------------------------
27 // Static members initialisation
28 // -----------------------------
29 
30 #include "CLHEP/Random/SeedTable.h"
31 
32 namespace CLHEP {
33 
34 
35 namespace {
36 
37 struct defaults {
38  defaults( HepRandom & g, HepJamesRandom & e )
39  : theGenerator( &g, do_nothing_deleter() )
40  , theEngine ( &e, do_nothing_deleter() )
41  { }
42 
43  void resetEngine( HepRandomEngine * newEngine ) {
44  theEngine.reset( newEngine );
45  }
46 
47  void resetEngine( HepRandomEngine & newEngine ) {
48  theEngine.reset( &newEngine, do_nothing_deleter() );
49  }
50 
51  bool ensureInitialized() {
52  assert( theGenerator.get() != 0 && theEngine.get() != 0 );
53  return true;
54  }
55 
56  ~defaults()
57  { }
58 
59  shared_ptr<HepRandom > theGenerator;
60  shared_ptr<HepRandomEngine> theEngine;
61 }; // defaults
62 
63  inline
64  defaults & theDefaults() {
65  static HepRandom theDefaultGenerator;
66  static HepJamesRandom theDefaultEngine;
67  static defaults theDefaults(theDefaultGenerator, theDefaultEngine);
68  return theDefaults;
69  }
70 
71 } // namespace
72 
73 //---------------------------- HepRandom ---------------------------------
74 
76 { }
77 
79 {
80  setTheSeed(seed);
81 }
82 
84 {
85  theDefaults().resetEngine( algorithm );
86 }
87 
89 {
90  theDefaults().resetEngine( algorithm );
91 }
92 
94 { }
95 
97 {
98  return theDefaults().theEngine->flat();
99 }
100 
101 void HepRandom::flatArray(const int size, double* vect)
102 {
103  theDefaults().theEngine->flatArray(size,vect);
104 }
105 
107  return flat();
108 }
109 
110 std::string HepRandom::name() const {return "HepRandom";}
112  std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n";
113  return *theDefaults().theEngine.get();
114 }
115 
116 std::ostream & operator<< (std::ostream & os, const HepRandom & dist) {
117  return dist.put(os);
118 }
119 
120 std::istream & operator>> (std::istream & is, HepRandom & dist) {
121  return dist.get(is);
122 }
123 
124 std::ostream & HepRandom::put(std::ostream & os) const {return os;}
125 std::istream & HepRandom::get(std::istream & is) {return is;}
126 
127 // --------------------------
128 // Static methods definitions
129 // --------------------------
130 
131 void HepRandom::setTheSeed(long seed, int lux)
132 {
133  theDefaults().theEngine->setSeed(seed,lux);
134 }
135 
137 {
138  return theDefaults().theEngine->getSeed();
139 }
140 
141 void HepRandom::setTheSeeds(const long* seeds, int aux)
142 {
143  theDefaults().theEngine->setSeeds(seeds,aux);
144 }
145 
147 {
148  return theDefaults().theEngine->getSeeds();
149 }
150 
151 void HepRandom::getTheTableSeeds(long* seeds, int index)
152 {
153  if ((index >= 0) && (index < 215)) {
154  seeds[0] = seedTable[index][0];
155  seeds[1] = seedTable[index][1];
156  }
157  else seeds = NULL;
158 }
159 
161 {
162  return theDefaults().theGenerator.get();
163 }
164 
166 {
167  return theDefaults().theEngine.get();
168 }
169 
171 {
172  theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
173 }
174 
175 void HepRandom::saveEngineStatus( const char filename[] )
176 {
177  theDefaults().theEngine->saveStatus( filename );
178 }
179 
180 void HepRandom::restoreEngineStatus( const char filename[] )
181 {
182  theDefaults().theEngine->restoreStatus( filename );
183 }
184 
185 std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
186  os << *getTheEngine();
187  return os;
188 }
189 
190 std::istream& HepRandom::restoreFullState ( std::istream & is ) {
191  is >> *getTheEngine();
192  return is;
193 }
194 
195 std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
196  return StaticRandomStates::save(os);
197 }
198 
199 std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
200  return StaticRandomStates::restore(is);
201 }
202 
204 {
205  theDefaults().theEngine->showStatus();
206 }
207 
209 {
210  return static_cast<int>( theDefaults().ensureInitialized() );
211 }
212 
213 } // namespace CLHEP
static const long * getTheSeeds()
Definition: Random.cc:146
static HepRandom * getTheGenerator()
Definition: Random.cc:160
shared_ptr< HepRandom > theGenerator
Definition: Random.cc:59
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:180
shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:60
static void setTheSeeds(const long *seeds, int aux=-1)
Definition: Random.cc:141
#define assert(x)
Definition: mymalloc.cc:1309
virtual std::istream & get(std::istream &is)
Definition: RandomEngine.cc:61
static const long seedTable[215][2]
Definition: Random.h:157
void flatArray(const int size, double *vect)
Definition: Random.cc:101
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:131
static HepRandomEngine * getTheEngine()
Definition: Random.cc:165
virtual std::ostream & put(std::ostream &os) const
Definition: Random.cc:124
std::ostream & operator<<(std::ostream &os, const HepRandom &dist)
Definition: Random.cc:116
virtual HepRandomEngine & engine()
Definition: Random.cc:111
static void showEngineStatus()
Definition: Random.cc:203
static std::istream & restore(std::istream &is)
virtual ~HepRandom()
Definition: Random.cc:93
double flat()
Definition: Random.cc:96
static long getTheSeed()
Definition: Random.cc:136
static int createInstance()
Definition: Random.cc:208
static std::ostream & save(std::ostream &os)
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:175
static std::ostream & saveStaticRandomStates(std::ostream &os)
Definition: Random.cc:195
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:170
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:185
std::istream & operator>>(std::istream &is, HepRandom &dist)
Definition: Random.cc:120
virtual double operator()()
Definition: Random.cc:106
virtual std::string name() const
Definition: Random.cc:110
virtual std::istream & get(std::istream &is)
Definition: Random.cc:125
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:190
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:151
static std::istream & restoreStaticRandomStates(std::istream &is)
Definition: Random.cc:199