Geant4  10.01.p01
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 "CLHEP/Random/JamesRandom.h"
22 #include "CLHEP/Random/Random.h"
23 #include "CLHEP/Random/StaticRandomStates.h"
24 #include "CLHEP/Utility/memory.h"
25 #include "CLHEP/Utility/thread_local.h"
26 
27 // -----------------------------
28 // Static members initialisation
29 // -----------------------------
30 
31 #include "CLHEP/Random/SeedTable.h"
32 
33 namespace CLHEP {
34 
35 
36 namespace {
37 
38 struct defaults {
39  defaults( HepRandom & g, HepJamesRandom & e )
40  : theGenerator( &g, do_nothing_deleter() )
41  , theEngine ( &e, do_nothing_deleter() )
42  { }
43 
44  void resetEngine( HepRandomEngine * newEngine ) {
45  theEngine.reset( newEngine );
46  }
47 
48  void resetEngine( HepRandomEngine & newEngine ) {
49  theEngine.reset( &newEngine, do_nothing_deleter() );
50  }
51 
52  bool ensureInitialized() {
53  assert( theGenerator.get() != 0 && theEngine.get() != 0 );
54  return true;
55  }
56 
57  ~defaults()
58  { }
59 
60  shared_ptr<HepRandom > theGenerator;
61  shared_ptr<HepRandomEngine> theEngine;
62 }; // defaults
63 
64  defaults & theDefaults() {
65  static CLHEP_THREAD_LOCAL HepRandom theDefaultGenerator;
66  static CLHEP_THREAD_LOCAL HepJamesRandom theDefaultEngine;
67  static CLHEP_THREAD_LOCAL defaults theDefaults(theDefaultGenerator, theDefaultEngine);
68  return theDefaults;
69  }
70 
71 } // namespace
72 
73 //---------------------------- HepRandom ---------------------------------
74 
75 HepRandom::HepRandom()
76 { }
77 
78 HepRandom::HepRandom(long seed)
79 {
80  setTheSeed(seed);
81 }
82 
83 HepRandom::HepRandom(HepRandomEngine & algorithm)
84 {
85  theDefaults().resetEngine( algorithm );
86 }
87 
88 HepRandom::HepRandom(HepRandomEngine * algorithm)
89 {
90  theDefaults().resetEngine( algorithm );
91 }
92 
93 HepRandom::~HepRandom()
94 { }
95 
96 double HepRandom::flat()
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 
106 double HepRandom::operator()() {
107  return flat();
108 }
109 
110 std::string HepRandom::name() const {return "HepRandom";}
111 HepRandomEngine & HepRandom::engine() {
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 
136 long HepRandom::getTheSeed()
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 
146 const long* HepRandom::getTheSeeds ()
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 
160 HepRandom * HepRandom::getTheGenerator()
161 {
162  return theDefaults().theGenerator.get();
163 }
164 
165 HepRandomEngine * HepRandom::getTheEngine()
166 {
167  return theDefaults().theEngine.get();
168 }
169 
170 void HepRandom::setTheEngine (HepRandomEngine* theNewEngine)
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 
203 void HepRandom::showEngineStatus()
204 {
205  theDefaults().theEngine->showStatus();
206 }
207 
208 int HepRandom::createInstance()
209 {
210  return static_cast<int>( theDefaults().ensureInitialized() );
211 }
212 
213 } // namespace CLHEP
static const double lux
Definition: G4SIunits.hh:291
shared_ptr< HepRandom > theGenerator
Definition: Random.cc:60
shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:61
G4String name
Definition: TRTMaterials.hh:40
static int engine(pchar, pchar, double &, pchar &, const dic_type &)
Definition: Evaluator.cc:358
std::istream & operator>>(std::istream &is, HepAxisAngle &aa)
Definition: AxisAngle.cc:95
double flat()
Definition: G4AblaRandom.cc:47
static const double g
Definition: G4SIunits.hh:162
std::ostream & operator<<(std::ostream &os, const HepAxisAngle &aa)
Definition: AxisAngle.cc:85