Geant4_10
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 // Pekka Kaitaniemi, CEA and Helsinki Institute of Physics
28 // Davide Mancusi, CEA
29 // Alain Boudard, CEA
30 // Sylvie Leray, CEA
31 // Joseph Cugnon, University of Liege
32 //
33 #define INCLXX_IN_GEANT4_MODE 1
34 
35 #include "globals.hh"
36 
37 /*
38  * G4INCLRandom.cc
39  *
40  * \date 7 June 2009
41  * \author Pekka Kaitaniemi
42  */
43 
44 #include "G4INCLRandom.hh"
45 #include "G4INCLGlobals.hh"
46 // #include <cassert>
47 
48 namespace G4INCL {
49 
50  namespace Random {
51 
52  namespace {
53 
54  G4ThreadLocal IRandomGenerator* theGenerator;
55 
56  }
57 
59  if(isInitialized()) {
60  INCL_ERROR("INCL random number generator already initialized." << std::endl);
61  } else {
62  theGenerator = aGenerator;
63  }
64  }
65 
66  void setSeeds(const SeedVector &sv) {
67  theGenerator->setSeeds(sv);
68  }
69 
71  return theGenerator->getSeeds();
72  }
73 
75  return theGenerator->flat();
76  }
77 
79  G4double r;
80  while( (r=shoot()) <= 0. )
81  ;
82  return r;
83  }
84 
86  G4double r;
87  while( (r=shoot()) >= 1. )
88  ;
89  return r;
90  }
91 
92  template<typename T>
93  T shootInteger(T n) {
94  return static_cast<T>(shoot1() * n);
95  }
96 
98  // generate a Gaussian random number with standard deviation sigma
99  // uses the flat() and flat0() methods
100  static G4ThreadLocal G4bool generated = false;
101  static G4ThreadLocal G4double u, v;
102 
103  if( !generated )
104  {
105  u = shoot0();
106  v = Math::twoPi*shoot();
107  generated = true;
108  return sigma*std::sqrt(-2*std::log(u))*std::cos(v);
109  }
110  else
111  {
112  generated = false;
113  return sigma*std::sqrt(-2*std::log(u))*std::sin(v);
114  }
115  }
116 
118 
119  const G4double ctheta = (1.-2.*shoot());
120  const G4double stheta = std::sqrt(1.-ctheta*ctheta);
121  const G4double phi = Math::twoPi*shoot();
122  return ThreeVector(
123  norm * stheta * std::cos(phi),
124  norm * stheta * std::sin(phi),
125  norm * ctheta);
126 
127  }
128 
130  return normVector( rmax*Math::pow13(shoot0()) );
131  }
132 
134  const G4double sigmax = sigma * Math::oneOverSqrtThree;
135  return ThreeVector(gauss(sigmax), gauss(sigmax), gauss(sigmax));
136  }
137 
138  std::pair<G4double,G4double> correlatedGaussian(const G4double corrCoeff, const G4double x0, const G4double sigma) {
139 // assert(corrCoeff<=1. && corrCoeff>=-1.);
140  G4double factor = 1.-corrCoeff*corrCoeff;
141  if(factor<=0.)
142  factor=0.;
143  const G4double x = gauss(sigma) + x0;
144  const G4double y = corrCoeff * x + gauss(sigma*std::sqrt(factor)) + x0;
145  return std::make_pair(x, y);
146  }
147 
148  std::pair<G4double,G4double> correlatedUniform(const G4double corrCoeff) {
149  std::pair<G4double,G4double> gaussians = correlatedGaussian(corrCoeff);
150  return std::make_pair(Math::gaussianCDF(gaussians.first), Math::gaussianCDF(gaussians.second));
151  }
152 
154  delete theGenerator;
155  theGenerator = 0;
156  }
157 
159  if(theGenerator == 0) return false;
160  return true;
161  }
162 
163  }
164 
165 }
ThreeVector gaussVector(G4double sigma=1.)
Generate Gaussianly-distributed ThreeVectors.
ThreeVector sphereVector(G4double rmax=1.)
void deleteGenerator()
shared_ptr< HepRandom > theGenerator
Definition: Random.cc:59
T shootInteger(T n)
Definition: G4INCLRandom.cc:93
#define INCL_ERROR(x)
Float_t norm
tuple x
Definition: test.py:50
#define G4ThreadLocal
Definition: tls.hh:52
G4double shoot0()
Definition: G4INCLRandom.cc:78
ThreeVector normVector(G4double norm=1.)
SeedVector getSeeds()
Definition: G4INCLRandom.cc:70
G4double shoot1()
Definition: G4INCLRandom.cc:85
Double_t y
Definition: plot.C:279
Char_t n[5]
std::pair< G4double, G4double > correlatedUniform(const G4double corrCoeff)
Generate pairs of correlated uniform random numbers.
bool G4bool
Definition: G4Types.hh:79
const G4double oneOverSqrtThree
tuple v
Definition: test.py:18
void setGenerator(G4INCL::IRandomGenerator *aGenerator)
Definition: G4INCLRandom.cc:58
jump r
Definition: plot.C:36
G4double gauss(G4double sigma=1.)
Definition: G4INCLRandom.cc:97
const G4double twoPi
G4double shoot()
Definition: G4INCLRandom.cc:74
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.
double Random(double min=0.0, double max=1.0)
Definition: UUtils.cc:69
G4double gaussianCDF(const G4double x)
Cumulative distribution function for Gaussian.
G4bool isInitialized()
void setSeeds(const SeedVector &sv)
Definition: G4INCLRandom.cc:66