72 InterpolationType(IntType)
74 prepareTable(aProbFunc);
78 const double* aProbFunc,
84 InterpolationType(IntType)
86 prepareTable(aProbFunc);
90 const double* aProbFunc,
94 localEngine(anEngine),
96 InterpolationType(IntType)
98 prepareTable(aProbFunc);
101void RandGeneral::prepareTable(
const double* aProbFunc) {
107 "RandGeneral constructed with no bins - will use flat distribution\n";
108 useFlatDistribution();
112 theIntegralPdf.resize(nBins+1);
113 theIntegralPdf[0] = 0;
117 for ( ptn = 0; ptn<nBins; ++ptn ) {
118 weight = aProbFunc[ptn];
123 "RandGeneral constructed with negative-weight bin " << ptn <<
124 " = " << weight <<
" \n -- will substitute 0 weight \n";
128 theIntegralPdf[ptn+1] = theIntegralPdf[ptn] + weight;
131 if ( theIntegralPdf[nBins] <= 0 ) {
133 "RandGeneral constructed nothing in bins - will use flat distribution\n";
134 useFlatDistribution();
138 for ( ptn = 0; ptn < nBins+1; ++ptn ) {
139 theIntegralPdf[ptn] /= theIntegralPdf[nBins];
144 oneOverNbins = 1.0 / nBins;
148 if ( (InterpolationType != 0) && (InterpolationType != 1) ) {
150 "RandGeneral does not recognize IntType " << InterpolationType
151 <<
"\n Will use type 0 (continuous linear interpolation \n";
152 InterpolationType = 0;
157void RandGeneral::useFlatDistribution() {
162 theIntegralPdf.resize(2);
163 theIntegralPdf[0] = 0;
164 theIntegralPdf[1] = 1;
182double RandGeneral::mapRandom(
double rand)
const {
192 while (nabove > nbelow+1) {
193 middle = (nabove + nbelow+1)>>1;
194 if (rand >= theIntegralPdf[middle]) {
200 assert ( nabove == nbelow+1 );
201 assert ( theIntegralPdf[nbelow] <= rand );
202 assert ( theIntegralPdf[nabove] >= rand );
206 if ( InterpolationType == 1 ) {
208 return nbelow * oneOverNbins;
212 double binMeasure = theIntegralPdf[nabove] - theIntegralPdf[nbelow];
216 if ( binMeasure == 0 ) {
220 return (nbelow + .5) * oneOverNbins;
223 double binFraction = (rand - theIntegralPdf[nbelow]) / binMeasure;
225 return (nbelow + binFraction) * oneOverNbins;
231 const int size,
double* vect )
235 for (i=0; i<size; ++i) {
236 vect[i] =
shoot(anEngine);
244 for (i=0; i<size; ++i) {
250 int pr=os.precision(20);
251 std::vector<unsigned long> t(2);
252 os <<
" " <<
name() <<
"\n";
253 os <<
"Uvec" <<
"\n";
254 os << nBins <<
" " << oneOverNbins <<
" " << InterpolationType <<
"\n";
256 os << t[0] <<
" " << t[1] <<
"\n";
257 assert (
static_cast<int>(theIntegralPdf.size())==nBins+1);
258 for (
unsigned int i=0; i<theIntegralPdf.size(); ++i) {
260 os << theIntegralPdf[i] <<
" " << t[0] <<
" " << t[1] <<
"\n";
269 if (inName !=
name()) {
270 is.clear(std::ios::badbit | is.rdstate());
271 std::cerr <<
"Mismatch when expecting to read state of a "
272 <<
name() <<
" distribution\n"
273 <<
"Name found was " << inName
274 <<
"\nistream is left in the badbit state\n";
278 std::vector<unsigned long> t(2);
279 is >> nBins >> oneOverNbins >> InterpolationType;
281 theIntegralPdf.resize(nBins+1);
282 for (
unsigned int i=0; i<theIntegralPdf.size(); ++i) {
283 is >> theIntegralPdf[i] >> t[0] >> t[1];
289 is >> oneOverNbins >> InterpolationType;
290 theIntegralPdf.resize(nBins+1);
291 for (
unsigned int i=0; i<theIntegralPdf.size(); ++i) is >> theIntegralPdf[i];
static double longs2double(const std::vector< unsigned long > &v)
static std::vector< unsigned long > dto2longs(double d)
RandGeneral(const double *aProbFunc, int theProbSize, int IntType=0)
std::istream & get(std::istream &is)
std::ostream & put(std::ostream &os) const
void fireArray(const int size, double *vect)
HepRandomEngine & engine()
void shootArray(const int size, double *vect)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)