Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
G4INCLRandom.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 // INCL++ intra-nuclear cascade model
27 // Alain Boudard, CEA-Saclay, France
28 // Joseph Cugnon, University of Liege, Belgium
29 // Jean-Christophe David, CEA-Saclay, France
30 // Pekka Kaitaniemi, CEA-Saclay, France, and Helsinki Institute of Physics, Finland
31 // Sylvie Leray, CEA-Saclay, France
32 // Davide Mancusi, CEA-Saclay, France
33 //
34 #define INCLXX_IN_GEANT4_MODE 1
35 
36 #include "globals.hh"
37 
38 /*
39  * G4INCLRandom.cc
40  *
41  * \date 7 June 2009
42  * \author Pekka Kaitaniemi
43  */
44 
45 #include "G4INCLRandom.hh"
46 #include "G4INCLGlobals.hh"
47 // #include <cassert>
48 
49 #include "G4INCLRanecu.hh"
50 #include "G4INCLRanecu3.hh"
51 #include "G4INCLGeant4Random.hh"
52 #include "G4INCLLogger.hh"
53 
54 namespace G4INCL {
55 
56  namespace Random {
57 
58  namespace {
59 
60  G4ThreadLocal IRandomGenerator* theGenerator = NULL;
61 
62 #ifdef INCL_COUNT_RND_CALLS
63  G4ThreadLocal unsigned long long nCalls;
64 #endif
65 
66  G4ThreadLocal SeedVector *savedSeeds = NULL;
67 
68  G4ThreadLocal Adapter *theAdapter = NULL;
69 
70  }
71 
73  if(isInitialized()) {
74  INCL_ERROR("INCL random number generator already initialized." << '\n');
75  } else {
76 #ifdef INCL_COUNT_RND_CALLS
77  nCalls = 0;
78 #endif
79  theGenerator = aGenerator;
80  }
81  if(!theAdapter)
82  theAdapter = new Adapter();
83  }
84 
85  void setSeeds(const SeedVector &sv) {
86  theGenerator->setSeeds(sv);
87  }
88 
90  return theGenerator->getSeeds();
91  }
92 
94 #ifdef INCL_COUNT_RND_CALLS
95  nCalls++;
96 #endif
97  return theGenerator->flat();
98  }
99 
101  G4double r;
102  while( (r=shoot()) <= 0. ) /* Loop checking, 10.07.2015, D.Mancusi */
103  ;
104  return r;
105  }
106 
108  G4double r;
109  while( (r=shoot()) >= 1. ) /* Loop checking, 10.07.2015, D.Mancusi */
110  ;
111  return r;
112  }
113 
115  // generate a Gaussian random number with standard deviation sigma
116  // uses the flat() and flat0() methods
117  static G4ThreadLocal G4bool generated = false;
118  static G4ThreadLocal G4double u, v;
119 
120  if( !generated )
121  {
122  u = shoot0();
123  v = Math::twoPi*shoot();
124  generated = true;
125  return sigma*std::sqrt(-2*std::log(u))*std::cos(v);
126  }
127  else
128  {
129  generated = false;
130  return sigma*std::sqrt(-2*std::log(u))*std::sin(v);
131  }
132  }
133 
135 
136  const G4double ctheta = (1.-2.*shoot());
137  const G4double stheta = std::sqrt(1.-ctheta*ctheta);
138  const G4double phi = Math::twoPi*shoot();
139  return ThreeVector(
140  norm * stheta * std::cos(phi),
141  norm * stheta * std::sin(phi),
142  norm * ctheta);
143 
144  }
145 
147  return normVector( rmax*Math::pow13(shoot0()) );
148  }
149 
151  const G4double sigmax = sigma * Math::oneOverSqrtThree;
152  return ThreeVector(gauss(sigmax), gauss(sigmax), gauss(sigmax));
153  }
154 
155  std::pair<G4double,G4double> correlatedGaussian(const G4double corrCoeff, const G4double x0, const G4double sigma) {
156 // assert(corrCoeff<=1. && corrCoeff>=-1.);
157  G4double factor = 1.-corrCoeff*corrCoeff;
158  if(factor<=0.)
159  factor=0.;
160  const G4double x = gauss(sigma) + x0;
161  const G4double y = corrCoeff * x + gauss(sigma*std::sqrt(factor)) + x0;
162  return std::make_pair(x, y);
163  }
164 
165  std::pair<G4double,G4double> correlatedUniform(const G4double corrCoeff) {
166  std::pair<G4double,G4double> gaussians = correlatedGaussian(corrCoeff);
167  return std::make_pair(Math::gaussianCDF(gaussians.first), Math::gaussianCDF(gaussians.second));
168  }
169 
171  delete theGenerator;
172  theGenerator = NULL;
173  delete savedSeeds;
174  savedSeeds = NULL;
175  delete theAdapter;
176  theAdapter = NULL;
177  }
178 
180  if(theGenerator == 0) return false;
181  return true;
182  }
183 
184 #ifdef INCL_COUNT_RND_CALLS
185  unsigned long long getNumberOfCalls() {
187  return nCalls;
188  }
189 #endif
190 
191  void saveSeeds() {
192  if(!savedSeeds)
193  savedSeeds = new SeedVector;
194 
195  (*savedSeeds) = theGenerator->getSeeds();
196  }
197 
199  if(!savedSeeds)
200  savedSeeds = new SeedVector;
201 
202  return *savedSeeds;
203  }
204 
205  void initialize(Config const * const
206 #ifndef INCLXX_IN_GEANT4_MODE
207  theConfig
208 #endif
209  ) {
210 #ifdef INCLXX_IN_GEANT4_MODE
212 #else // INCLXX_IN_GEANT4_MODE
213  RNGType rng = theConfig->getRNGType();
214  if(rng == RanecuType)
215  setGenerator(new Ranecu(theConfig->getRandomSeeds()));
216  else if(rng == Ranecu3Type)
217  setGenerator(new Ranecu3(theConfig->getRandomSeeds()));
218  else
219  setGenerator(NULL);
220 #endif // INCLXX_IN_GEANT4_MODE
221  }
222 
224  return shootInteger(n);
225  }
226 
227  Adapter const &getAdapter() {
228  return *theAdapter;
229  }
230 
231  }
232 
233 }
ThreeVector gaussVector(G4double sigma=1.)
Generate Gaussianly-distributed ThreeVectors.
ThreeVector sphereVector(G4double rmax=1.)
void deleteGenerator()
std::shared_ptr< HepRandom > theGenerator
Definition: Random.cc:72
T shootInteger(T n)
Definition: G4INCLRandom.hh:96
#define INCL_ERROR(x)
void initialize(Config const *const)
Initialize generator according to a Config object.
#define G4ThreadLocal
Definition: tls.hh:89
G4double shoot0()
int G4int
Definition: G4Types.hh:78
ThreeVector normVector(G4double norm=1.)
void saveSeeds()
Save the status of the random-number generator.
SeedVector getSeeds()
Definition: G4INCLRandom.cc:89
G4double shoot1()
std::pair< G4double, G4double > correlatedUniform(const G4double corrCoeff)
Generate pairs of correlated uniform random numbers.
bool G4bool
Definition: G4Types.hh:79
Extended Ranecu-type RNG class.
SeedVector getSavedSeeds()
Get the saved status of the random-number generator.
const G4double oneOverSqrtThree
void setGenerator(G4INCL::IRandomGenerator *aGenerator)
Definition: G4INCLRandom.cc:72
Adapter const & getAdapter()
G4double gauss(G4double sigma=1.)
const G4double twoPi
#define INCLXX_IN_GEANT4_MODE
Definition: G4INCLRandom.cc:34
G4double shoot()
Definition: G4INCLRandom.cc:93
G4int operator()(const G4int n) const
double G4double
Definition: G4Types.hh:76
G4double pow13(G4double x)
std::pair< G4double, G4double > correlatedGaussian(const G4double corrCoeff, const G4double x0=0., const G4double sigma=1.)
Generate pairs of correlated Gaussian random numbers.
G4double gaussianCDF(const G4double x)
Cumulative distribution function for Gaussian.
G4bool isInitialized()
void setSeeds(const SeedVector &sv)
Definition: G4INCLRandom.cc:85