Geant4  10.02.p01
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 #if __clang__
30  #if ((defined(G4MULTITHREADED) && !defined(G4USE_STD11)) || \
31  !__has_feature(cxx_thread_local))
32  #define CLANG_NOSTDTLS
33  #endif
34 #endif
35 
36 #if (defined(G4MULTITHREADED) && \
37  (!defined(G4USE_STD11) || (defined(CLANG_NOSTDTLS) || defined(__INTEL_COMPILER))))
38 
39 #include <CLHEP/Random/StaticRandomStates.h>
40 #include <CLHEP/Random/JamesRandom.h>
41 
42 #include "G4MTHepRandom.hh"
43 #include "G4Threading.hh"
44 #include "G4AutoLock.hh"
45 
46 G4ThreadLocal CLHEP::HepRandomEngine* G4MTHepRandom::theEngine = 0;
49 
50 //---------------------------- HepRandom ---------------------------------
51 
53 : deleteEngine(false)
54 {
55  createInstance();
56 }
57 
59 : deleteEngine(false)
60 {
61  createInstance();
62  setTheSeed(seed);
63 }
64 
65 G4MTHepRandom::G4MTHepRandom(CLHEP::HepRandomEngine & algorithm)
66 : deleteEngine(false)
67 {
68  theGenerator = this;
69  theEngine = &algorithm;
70  isActive = 1;
71 }
72 
73 G4MTHepRandom::G4MTHepRandom(CLHEP::HepRandomEngine * algorithm)
74 : deleteEngine(true)
75 {
76  createInstance();
77  theEngine = algorithm;
78 }
79 
81 {
82  if ( deleteEngine ) { delete theEngine; theEngine = 0; }
83  //if ( theGenerator ) { delete theGenerator; theGenerator = 0; }
84 }
85 
87 {
88  return theEngine->flat();
89 }
90 
91 void G4MTHepRandom::flatArray(const G4int size, G4double* vect)
92 {
93  theEngine->flatArray(size,vect);
94 }
95 
97 {
98  return flat();
99 }
100 
101 std::ostream & operator<< (std::ostream & os, const G4MTHepRandom & dist)
102 {
103  return dist.put(os);
104 }
105 
106 std::istream & operator>> (std::istream & is, G4MTHepRandom & dist)
107 {
108  return dist.get(is);
109 }
110 
111 std::ostream & G4MTHepRandom::put(std::ostream & os) const
112 {
113  return os;
114 }
115 std::istream & G4MTHepRandom::get(std::istream & is)
116 {
117  return is;
118 }
119 
120 // --------------------------
121 // Static methods definitions
122 // --------------------------
123 
125 {
126  createInstance();
127  theEngine->setSeed(seed,lux);
128 }
129 
131 {
132  createInstance();
133  return theEngine->getSeed();
134 }
135 
136 void G4MTHepRandom::setTheSeeds(const G4long* seeds, G4int aux)
137 {
138  createInstance();
139  theEngine->setSeeds(seeds,aux);
140 }
141 
143 {
144  createInstance();
145  return theEngine->getSeeds();
146 }
147 
149 {
151  return theGenerator;
152 }
153 
154 CLHEP::HepRandomEngine * G4MTHepRandom::getTheEngine()
155 {
157  return theEngine;
158 }
159 
160 void G4MTHepRandom::setTheEngine (CLHEP::HepRandomEngine* theNewEngine)
161 {
162  theEngine = theNewEngine;
163 }
164 
165 void G4MTHepRandom::saveEngineStatus( const char filename[] )
166 {
167  createInstance();
168  theEngine->saveStatus( filename );
169 }
170 
171 void G4MTHepRandom::restoreEngineStatus( const char filename[] )
172 {
173  createInstance();
174  theEngine->restoreStatus( filename );
175 }
176 
177 std::ostream& G4MTHepRandom::saveFullState ( std::ostream & os )
178 {
179  os << *getTheEngine();
180  return os;
181 }
182 
183 std::istream& G4MTHepRandom::restoreFullState ( std::istream & is )
184 {
185  is >> *getTheEngine();
186  return is;
187 }
188 
189 std::ostream& G4MTHepRandom::saveStaticRandomStates ( std::ostream & os )
190 {
191  return CLHEP::StaticRandomStates::save(os);
192 }
193 
194 std::istream& G4MTHepRandom::restoreStaticRandomStates ( std::istream & is )
195 {
196  return CLHEP::StaticRandomStates::restore(is);
197 }
198 
200 {
201  createInstance();
202  theEngine->showStatus();
203 }
204 
206 {
208  if (theGenerator) return 1; // should always be true
209  return 0;
210 }
211 
212 namespace {
213  G4Mutex jamesrandom = G4MUTEX_INITIALIZER;
214 }
215 
217 {
218  if (isActive) return isActive;
219  isActive = 1;
220 
221 
222  static G4ThreadLocal CLHEP::HepJamesRandom *defaultEngine = 0;
223  if (!defaultEngine) {
224  G4AutoLock l(&jamesrandom);
225  defaultEngine = new CLHEP::HepJamesRandom;
226  }
227 
228  if ( !theEngine ) { theEngine = defaultEngine; }
229  if ( !theGenerator ) { theGenerator = new G4MTHepRandom( theEngine ); }
230 
231  return 1;
232 }
233 
234 #endif
static G4long getTheSeed()
static CLHEP::HepRandomEngine * getTheEngine()
static const double lux
Definition: G4SIunits.hh:324
virtual ~G4MTHepRandom()
std::shared_ptr< HepRandom > theGenerator
Definition: Random.cc:61
static G4MTHepRandom * getTheGenerator()
static G4ThreadLocal G4MTHepRandom * theGenerator
std::shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:62
static G4int createInstance()
static void setTheSeeds(const G4long *seeds, G4int aux=-1)
static void setTheEngine(CLHEP::HepRandomEngine *theNewEngine)
long G4long
Definition: G4Types.hh:80
static void saveEngineStatus(const char filename[]="Config.conf")
#define G4ThreadLocal
Definition: tls.hh:89
int G4int
Definition: G4Types.hh:78
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
static void setTheSeed(G4long seed, G4int lux=3)
static std::istream & restoreFullState(std::istream &is)
static std::ostream & saveStaticRandomStates(std::ostream &os)
std::istream & operator>>(std::istream &is, HepAxisAngle &aa)
Definition: AxisAngle.cc:95
static std::istream & restoreStaticRandomStates(std::istream &is)
static G4int createInstanceOnce()
static void showEngineStatus()
static std::ostream & saveFullState(std::ostream &os)
static G4ThreadLocal G4int isActive
std::ostream & operator<<(std::ostream &ostr, const G4String &astr)
Definition: pyG4String.cc:40
G4int G4Mutex
Definition: G4Threading.hh:173
G4double flat()
virtual std::ostream & put(std::ostream &os) const
static void restoreEngineStatus(const char filename[]="Config.conf")
virtual std::istream & get(std::istream &is)
static const G4long * getTheSeeds()
virtual G4double operator()()
double G4double
Definition: G4Types.hh:76
void flatArray(const G4int size, G4double *vect)
static G4ThreadLocal CLHEP::HepRandomEngine * theEngine