30 #if ((defined(G4MULTITHREADED) && !defined(G4USE_STD11)) || \
31 !__has_feature(cxx_thread_local)) || !__has_feature(c_atomic)
32 #define CLANG_NOSTDTLS
36 #if (defined(G4MULTITHREADED) && \
37 (!defined(G4USE_STD11) || (defined(CLANG_NOSTDTLS) || defined(__INTEL_COMPILER))))
48 : deleteEngine(false),
50 InterpolationType(IntType)
53 prepareTable(aProbFunc);
60 : localEngine(&anEngine),
63 InterpolationType(IntType)
65 prepareTable(aProbFunc);
72 : localEngine(anEngine),
75 InterpolationType(IntType)
77 prepareTable(aProbFunc);
80 void G4MTRandGeneral::prepareTable(
const G4double* aProbFunc)
87 "G4MTRandGeneral constructed with no bins - will use flat distribution\n";
88 useFlatDistribution();
92 theIntegralPdf.resize(nBins+1);
93 theIntegralPdf[0] = 0;
97 for ( ptn = 0; ptn<nBins; ++ptn ) {
98 weight = aProbFunc[ptn];
103 "G4MTRandGeneral constructed with negative-weight bin " << ptn <<
104 " = " << weight <<
" \n -- will substitute 0 weight \n";
108 theIntegralPdf[ptn+1] = theIntegralPdf[ptn] + weight;
111 if ( theIntegralPdf[nBins] <= 0 ) {
113 "G4MTRandGeneral constructed nothing in bins - will use flat distribution\n";
114 useFlatDistribution();
118 for ( ptn = 0; ptn < nBins+1; ++ptn ) {
119 theIntegralPdf[ptn] /= theIntegralPdf[nBins];
124 oneOverNbins = 1.0 / nBins;
128 if ( (InterpolationType != 0) && (InterpolationType != 1) ) {
130 "G4MTRandGeneral does not recognize IntType " << InterpolationType
131 <<
"\n Will use type 0 (continuous linear interpolation \n";
132 InterpolationType = 0;
137 void G4MTRandGeneral::useFlatDistribution()
143 theIntegralPdf.resize(2);
144 theIntegralPdf[0] = 0;
145 theIntegralPdf[1] = 1;
157 if ( deleteEngine )
delete localEngine;
172 G4int nabove = nBins;
175 while (nabove > nbelow+1) {
176 middle = (nabove + nbelow+1)>>1;
177 if (rand >= theIntegralPdf[middle]) {
189 if ( InterpolationType == 1 ) {
191 return nbelow * oneOverNbins;
195 G4double binMeasure = theIntegralPdf[nabove] - theIntegralPdf[nbelow];
199 if ( binMeasure == 0 ) {
203 return (nbelow + .5) * oneOverNbins;
206 G4double binFraction = (rand - theIntegralPdf[nbelow]) / binMeasure;
208 return (nbelow + binFraction) * oneOverNbins;
217 for (
G4int i=0; i<size; ++i)
218 vect[i] =
shoot(anEngine);
223 for (
G4int i=0; i<size; ++i)
static CLHEP::HepRandomEngine * getTheEngine()
void shootArray(const G4int size, G4double *vect)
void fireArray(const G4int size, G4double *vect)
G4MTRandGeneral(const G4double *aProbFunc, G4int theProbSize, G4int IntType=0)
virtual ~G4MTRandGeneral()