Geant4  10.00.p01
G4MTRandGaussQ.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 
31 #include <cmath> // for log()
32 #include <CLHEP/Units/PhysicalConstants.h>
33 
34 #include "G4MTRandGaussQ.hh"
35 
37 {
38 }
39 
41  : G4MTRandGauss(right)
42 {
43 }
44 
46 {
48 }
49 
51 {
52  return transformQuick(localEngine->flat()) * stdDev + mean;
53 }
54 
55 void G4MTRandGaussQ::shootArray( const G4int size, G4double* vect,
56  G4double mean, G4double stdDev )
57 {
58  for (G4int i=0; i<size; ++i)
59  vect[i] = shoot(mean,stdDev);
60 }
61 
62 void G4MTRandGaussQ::shootArray( CLHEP::HepRandomEngine* anEngine,
63  const G4int size, G4double* vect,
64  G4double mean, G4double stdDev )
65 {
66  for (G4int i=0; i<size; ++i)
67  vect[i] = shoot(anEngine,mean,stdDev);
68 }
69 
70 void G4MTRandGaussQ::fireArray( const G4int size, G4double* vect)
71 {
72  for (G4int i=0; i<size; ++i)
73  vect[i] = fire( defaultMean, defaultStdDev );
74 }
75 
76 void G4MTRandGaussQ::fireArray( const G4int size, G4double* vect,
77  G4double mean, G4double stdDev )
78 {
79  for (G4int i=0; i<size; ++i)
80  vect[i] = fire( mean, stdDev );
81 }
82 
83 //
84 // Table of errInts, for use with transform(r) and quickTransform(r)
85 //
86 
87 // Since all these are this is static to this compilation unit only, the
88 // info is establised a priori and not at each invocation.
89 
90 // The main data is of course the gaussQTables table; the rest is all
91 // bookkeeping to know what the tables mean.
92 
93 #define Table0size 250
94 #define Table1size 1000
95 #define TableSize (Table0size+Table1size)
96 
97 #define Table0step (2.0E-6)
98 #define Table1step (5.0E-4)
99 
100 #define Table0scale (1.0/Table1step)
101 
102 #define Table0offset 0
103 #define Table1offset (Table0size)
104 
105  // Here comes the big (5K bytes) table, kept in a file ---
106 
107 static const G4float gaussTables [TableSize] = {
108 #include "gaussQTables.cdat"
109 };
110 
112 {
113  G4double sign = +1.0; // We always compute a negative number of
114  // sigmas. For r > 0 we will multiply by
115  // sign = -1 to return a positive number.
116  if ( r > .5 ) {
117  r = 1-r;
118  sign = -1.0;
119  }
120 
121  register G4int index;
122  G4double dx;
123 
124  if ( r >= Table1step ) {
125  index = G4int((Table1size<<1) * r); // 1 to Table1size
126  if (index == Table1size) return 0.0;
127  dx = (Table1size<<1) * r - index; // fraction of way to next bin
128  index += Table1offset-1;
129  } else if ( r > Table0step ) {
130  G4double rr = r * Table0scale;
131  index = G4int(Table0size * rr); // 1 to Table0size
132  dx = Table0size * rr - index; // fraction of way to next bin
133  index += Table0offset-1;
134  } else { // r <= Table0step - not in tables
135  return sign*transformSmall(r);
136  }
137 
138  G4double y0 = gaussTables [index++];
139  G4double y1 = gaussTables [index];
140 
141  return (G4float) (sign * ( y1 * dx + y0 * (1.0-dx) ));
142 
143 } // transformQuick()
144 
146 {
147  // Solve for -v in the asymtotic formula
148  //
149  // errInt (-v) = exp(-v*v/2) 1 1*3 1*3*5
150  // ------------ * (1 - ---- + ---- - ----- + ... )
151  // v*sqrt(2*pi) v**2 v**4 v**6
152 
153  // The value of r (=errInt(-v)) supplied is going to less than 2.0E-13,
154  // which is such that v < -7.25. Since the value of r is meaningful only
155  // to an absolute error of 1E-16 (double precision accuracy for a number
156  // which on the high side could be of the form 1-epsilon), computing
157  // v to more than 3-4 digits of accuracy is suspect; however, to ensure
158  // smoothness with the table generator (which uses quite a few terms) we
159  // also use terms up to 1*3*5* ... *13/v**14, and insist on accuracy of
160  // solution at the level of 1.0e-7.
161 
162  // This routine is called less than one time in a million firings, so
163  // speed is of no concern. As a matter of technique, we terminate the
164  // iterations in case they would be infinite, but this should not happen.
165 
166  G4double eps = 1.0e-7;
167  G4double guess = 7.5;
168  G4double v;
169 
170  for ( G4int i = 1; i < 50; i++ ) {
171  G4double vn2 = 1.0/(guess*guess);
172  G4double s = -13*11*9*7*5*3 * vn2*vn2*vn2*vn2*vn2*vn2*vn2;
173  s += 11*9*7*5*3 * vn2*vn2*vn2*vn2*vn2*vn2;
174  s += -9*7*5*3 * vn2*vn2*vn2*vn2*vn2;
175  s += 7*5*3 * vn2*vn2*vn2*vn2;
176  s += -5*3 * vn2*vn2*vn2;
177  s += 3 * vn2*vn2 - vn2 + 1.0;
178  v = std::sqrt ( 2.0 * std::log ( s / (r*guess*std::sqrt(CLHEP::twopi)) ) );
179  if ( std::fabs(v-guess) < eps ) break;
180  guess = v;
181  }
182  return -v;
183 
184 } // transformSmall()
185 
186 #endif
G4double fire()
G4double defaultStdDev
#define Table0scale
Definition: RandGaussQ.cc:81
#define Table0step
G4double defaultMean
virtual ~G4MTRandGaussQ()
float G4float
Definition: G4Types.hh:77
#define Table1step
static const G4double eps
#define Table1size
virtual G4double operator()()
int G4int
Definition: G4Types.hh:78
#define TableSize
G4MTRandGaussQ(CLHEP::HepRandomEngine &anEngine, G4double mean=0.0, G4double stdDev=1.0)
static const double s
Definition: G4SIunits.hh:150
static G4double transformQuick(G4double r)
static G4double shoot()
static G4double transformSmall(G4double r)
#define Table0offset
static const double gaussTables[2 *TableSize]
#define Table0size
static void shootArray(const G4int size, G4double *vect, G4double mean=0.0, G4double stdDev=1.0)
#define Table1offset
double G4double
Definition: G4Types.hh:76
void fireArray(const G4int size, G4double *vect)
G4int sign(const T t)
A simple sign function that allows us to port fortran code to c++ more easily.
CLHEP::HepRandomEngine * localEngine