// ---------------------------------------------------------------------------- // Utility functions to fill histograms with user defined random distributions. // // The first function will fill a histogram referred to by HepHist1D& hist // with num samplings drawn from the CLHEP Random distribution referred to // by HepRandom& dist. It is assumed that the user has already instantiated // the HepRandom distribution with the desired shape parameters as well as // the HepHist1D histogram with the desired range and binning. // // The second function will fill a histogram referred to by HepHist1D& subhist // with num samplings drawn from the histogram referred to by HepHist1D& // refhist. It is assumed that the reference histogram exists and represents // a reasonably high statistics thing with the desired shape parameters. // Neither the range nor the binning of the subsample histogram is required to // be the same as that for the reference histogram. // // A valid question here is, "Why is this thing in ZMtools and not in // either CLHEP/Random or HepTuple?" The answer is that we don't want // either of those to be dependent on the other. That's a nice sentiment // but putting it here makes ZMtools dependent on both CLHEP and HepTuple. // Is that a good compromise? You should live so long! // // History: // 21-Dec-1999 JMM Initial draft. // // ---------------------------------------------------------------------------- #include "ZMtools/fillRandom.h" ZM_BEGIN_NAMESPACE( zmht ) /* namespace zmht { */ void fillRandom( HepRandom& dist, HepHist1D& hist, int num ) { for( int i = 0; i < num; ++i ) hist.accumulate( (float)dist() ); } void fillRandom( HepHist1D& refhist, HepHist1D& subhist, int num ) { TripleRand Triple; // Collect the data from the reference histogram and feed it to RandGeneral int nbins = refhist.nBins(); float refmin = refhist.min(); float refmax = refhist.max(); float range = refmax - refmin; float* contents = new float[nbins]; refhist.getContents( contents ); double* dcontents = new double[nbins]; for( int i = 0; i < nbins; ++i ) dcontents[i] = (double)contents[i]; delete [] contents; RandGeneral Reference( Triple, dcontents, nbins ); // RandGeneral produces samples scaled to the interval (0,1). // Transform them back onto (refmin,refmax) and accumulate. double sample; for( int i = 0; i < num; ++i ) { sample = range*Reference() + refmin; subhist.accumulate( (float)sample ); } delete [] dcontents; } ZM_END_NAMESPACE( zmht ) /* } // namespace zmht */