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