Geant4  10.02.p02
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>
22 #include "CLHEP/Random/JamesRandom.h"
23 #include "CLHEP/Random/Random.h"
24 #include "CLHEP/Random/StaticRandomStates.h"
25 #include "CLHEP/Utility/memory.h"
26 #include "CLHEP/Utility/thread_local.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 
76 HepRandom::HepRandom()
77 { }
78 
79 HepRandom::HepRandom(long seed)
80 {
81  setTheSeed(seed);
82 }
83 
84 HepRandom::HepRandom(HepRandomEngine & algorithm)
85 {
86  theDefaults().resetEngine( algorithm );
87 }
88 
89 HepRandom::HepRandom(HepRandomEngine * algorithm)
90 {
91  theDefaults().resetEngine( algorithm );
92 }
93 
94 HepRandom::~HepRandom()
95 { }
96 
97 double HepRandom::flat()
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 
107 double HepRandom::operator()() {
108  return flat();
109 }
110 
111 std::string HepRandom::name() const {return "HepRandom";}
112 HepRandomEngine & HepRandom::engine() {
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 
137 long HepRandom::getTheSeed()
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 
147 const long* HepRandom::getTheSeeds ()
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 
161 HepRandom * HepRandom::getTheGenerator()
162 {
163  return theDefaults().theGenerator.get();
164 }
165 
166 HepRandomEngine * HepRandom::getTheEngine()
167 {
168  return theDefaults().theEngine.get();
169 }
170 
171 void HepRandom::setTheEngine (HepRandomEngine* theNewEngine)
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 
204 void HepRandom::showEngineStatus()
205 {
206  theDefaults().theEngine->showStatus();
207 }
208 
209 int HepRandom::createInstance()
210 {
211  return static_cast<int>( theDefaults().ensureInitialized() );
212 }
213 
214 } // namespace CLHEP
static const double lux
Definition: G4SIunits.hh:324
std::shared_ptr< HepRandom > theGenerator
Definition: Random.cc:61
std::shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:62
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:180
std::ostream & operator<<(std::ostream &os, const HepAxisAngle &aa)
Definition: AxisAngle.cc:85