Geant4_10
G4MTHepRandom.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id:$
28 //
29 #ifdef G4MULTITHREADED
30 
33 
34 #include "G4MTHepRandom.hh"
35 #include "G4Threading.hh"
36 #include "G4AutoLock.hh"
37 
38 G4ThreadLocal CLHEP::HepRandomEngine* G4MTHepRandom::theEngine = 0;
39 G4ThreadLocal G4MTHepRandom* G4MTHepRandom::theGenerator = 0;
40 G4ThreadLocal G4int G4MTHepRandom::isActive = 0 ;
41 
42 //---------------------------- HepRandom ---------------------------------
43 
45 : deleteEngine(false)
46 {
47  createInstance();
48 }
49 
51 : deleteEngine(false)
52 {
53  createInstance();
54  setTheSeed(seed);
55 }
56 
58 : deleteEngine(false)
59 {
60  theGenerator = this;
61  theEngine = &algorithm;
62  isActive = 1;
63 }
64 
66 : deleteEngine(true)
67 {
68  createInstance();
69  theEngine = algorithm;
70 }
71 
73 {
74  if ( deleteEngine ) { delete theEngine; theEngine = 0; }
75  //if ( theGenerator ) { delete theGenerator; theGenerator = 0; }
76 }
77 
79 {
80  return theEngine->flat();
81 }
82 
83 void G4MTHepRandom::flatArray(const G4int size, G4double* vect)
84 {
85  theEngine->flatArray(size,vect);
86 }
87 
89 {
90  return flat();
91 }
92 
93 std::ostream & operator<< (std::ostream & os, const G4MTHepRandom & dist)
94 {
95  return dist.put(os);
96 }
97 
98 std::istream & operator>> (std::istream & is, G4MTHepRandom & dist)
99 {
100  return dist.get(is);
101 }
102 
103 std::ostream & G4MTHepRandom::put(std::ostream & os) const
104 {
105  return os;
106 }
107 std::istream & G4MTHepRandom::get(std::istream & is)
108 {
109  return is;
110 }
111 
112 // --------------------------
113 // Static methods definitions
114 // --------------------------
115 
117 {
118  createInstance();
119  theEngine->setSeed(seed,lux);
120 }
121 
123 {
124  createInstance();
125  return theEngine->getSeed();
126 }
127 
128 void G4MTHepRandom::setTheSeeds(const G4long* seeds, G4int aux)
129 {
130  createInstance();
131  theEngine->setSeeds(seeds,aux);
132 }
133 
135 {
136  createInstance();
137  return theEngine->getSeeds();
138 }
139 
141 {
142  if (!isActive) { isActive = G4MTHepRandom::createInstanceOnce(); }
143  return theGenerator;
144 }
145 
147 {
148  if (!isActive) { isActive = G4MTHepRandom::createInstanceOnce(); }
149  return theEngine;
150 }
151 
153 {
154  theEngine = theNewEngine;
155 }
156 
157 void G4MTHepRandom::saveEngineStatus( const char filename[] )
158 {
159  createInstance();
160  theEngine->saveStatus( filename );
161 }
162 
163 void G4MTHepRandom::restoreEngineStatus( const char filename[] )
164 {
165  createInstance();
166  theEngine->restoreStatus( filename );
167 }
168 
169 std::ostream& G4MTHepRandom::saveFullState ( std::ostream & os )
170 {
171  os << *getTheEngine();
172  return os;
173 }
174 
175 std::istream& G4MTHepRandom::restoreFullState ( std::istream & is )
176 {
177  is >> *getTheEngine();
178  return is;
179 }
180 
181 std::ostream& G4MTHepRandom::saveStaticRandomStates ( std::ostream & os )
182 {
184 }
185 
186 std::istream& G4MTHepRandom::restoreStaticRandomStates ( std::istream & is )
187 {
189 }
190 
192 {
193  createInstance();
194  theEngine->showStatus();
195 }
196 
198 {
199  if (!isActive) { isActive = G4MTHepRandom::createInstanceOnce(); }
200  if (theGenerator) return 1; // should always be true
201  return 0;
202 }
203 
204 namespace {
205  G4Mutex jamesrandom = G4MUTEX_INITIALIZER;
206 }
207 
209 {
210  if (isActive) return isActive;
211  isActive = 1;
212 
213 
214  static G4ThreadLocal CLHEP::HepJamesRandom *defaultEngine = 0;
215  if (!defaultEngine) {
216  G4AutoLock l(&jamesrandom);
217  defaultEngine = new CLHEP::HepJamesRandom;
218  }
219 
220  if ( !theEngine ) { theEngine = defaultEngine; }
221  if ( !theGenerator ) { theGenerator = new G4MTHepRandom( theEngine ); }
222 
223  return 1;
224 }
225 
226 #endif
static G4long getTheSeed()
static CLHEP::HepRandomEngine * getTheEngine()
virtual ~G4MTHepRandom()
virtual void showStatus() const =0
shared_ptr< HepRandom > theGenerator
Definition: Random.cc:59
static G4MTHepRandom * getTheGenerator()
shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:60
static G4int createInstance()
static void setTheSeeds(const G4long *seeds, G4int aux=-1)
static void setTheEngine(CLHEP::HepRandomEngine *theNewEngine)
long G4long
Definition: G4Types.hh:80
virtual double flat()=0
static void saveEngineStatus(const char filename[]="Config.conf")
#define G4ThreadLocal
Definition: tls.hh:52
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:158
const long * getSeeds() const
Definition: RandomEngine.h:116
static void setTheSeed(G4long seed, G4int lux=3)
static std::istream & restoreFullState(std::istream &is)
static std::ostream & saveStaticRandomStates(std::ostream &os)
static std::istream & restoreStaticRandomStates(std::istream &is)
virtual void restoreStatus(const char filename[]="Config.conf")=0
static G4int createInstanceOnce()
virtual void setSeed(long seed, int)=0
static void showEngineStatus()
static std::ostream & saveFullState(std::ostream &os)
long getSeed() const
Definition: RandomEngine.h:113
static std::istream & restore(std::istream &is)
G4int G4Mutex
Definition: G4Threading.hh:156
G4double flat()
virtual void setSeeds(const long *seeds, int)=0
virtual std::ostream & put(std::ostream &os) const
static std::ostream & save(std::ostream &os)
static void restoreEngineStatus(const char filename[]="Config.conf")
std::ostream & operator<<(std::ostream &, const BasicVector3D< float > &)
std::istream & operator>>(std::istream &, BasicVector3D< float > &)
virtual std::istream & get(std::istream &is)
static const G4long * getTheSeeds()
virtual void saveStatus(const char filename[]="Config.conf") const =0
virtual G4double operator()()
double G4double
Definition: G4Types.hh:76
void flatArray(const G4int size, G4double *vect)
virtual void flatArray(const int size, double *vect)=0