29 #ifdef G4MULTITHREADED
40 : deleteEngine(false),
42 InterpolationType(IntType)
45 prepareTable(aProbFunc);
52 : localEngine(&anEngine),
55 InterpolationType(IntType)
57 prepareTable(aProbFunc);
64 : localEngine(anEngine),
67 InterpolationType(IntType)
69 prepareTable(aProbFunc);
72 void G4MTRandGeneral::prepareTable(
const G4double* aProbFunc)
79 "G4MTRandGeneral constructed with no bins - will use flat distribution\n";
80 useFlatDistribution();
84 theIntegralPdf.resize(nBins+1);
85 theIntegralPdf[0] = 0;
89 for ( ptn = 0; ptn<nBins; ++ptn ) {
90 weight = aProbFunc[ptn];
95 "G4MTRandGeneral constructed with negative-weight bin " << ptn <<
96 " = " << weight <<
" \n -- will substitute 0 weight \n";
100 theIntegralPdf[ptn+1] = theIntegralPdf[ptn] +
weight;
103 if ( theIntegralPdf[nBins] <= 0 ) {
105 "G4MTRandGeneral constructed nothing in bins - will use flat distribution\n";
106 useFlatDistribution();
110 for ( ptn = 0; ptn < nBins+1; ++ptn ) {
111 theIntegralPdf[ptn] /= theIntegralPdf[nBins];
116 oneOverNbins = 1.0 / nBins;
120 if ( (InterpolationType != 0) && (InterpolationType != 1) ) {
122 "G4MTRandGeneral does not recognize IntType " << InterpolationType
123 <<
"\n Will use type 0 (continuous linear interpolation \n";
124 InterpolationType = 0;
129 void G4MTRandGeneral::useFlatDistribution()
135 theIntegralPdf.resize(2);
136 theIntegralPdf[0] = 0;
137 theIntegralPdf[1] = 1;
149 if ( deleteEngine )
delete localEngine;
164 G4int nabove = nBins;
167 while (nabove > nbelow+1) {
168 middle = (nabove + nbelow+1)>>1;
169 if (rand >= theIntegralPdf[middle]) {
181 if ( InterpolationType == 1 ) {
183 return nbelow * oneOverNbins;
187 G4double binMeasure = theIntegralPdf[nabove] - theIntegralPdf[nbelow];
191 if ( binMeasure == 0 ) {
195 return (nbelow + .5) * oneOverNbins;
198 G4double binFraction = (rand - theIntegralPdf[nbelow]) / binMeasure;
200 return (nbelow + binFraction) * oneOverNbins;
209 for (
register G4int i=0; i<size; ++i)
210 vect[i] =
shoot(anEngine);
215 for (
register 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()