import java.io.*; // for IOException, PrintWriter, FileOutputStream import java.text.*; // for DecimalFormat // To compile: javac Histogram.java // Build docs: javadoc -author -version Histogram.java /** A simple histogram class. It can be used to accumulate a * histogram and calculate statistical information about it. * @author Simon George * @version 1.0 31 Aug 2001 */ public class Histogram { /** * Constructor which sets name, number of bins, and range. * @param name Give the histogram a text name * @param nbins the number of bins the histogram should have. The * range specified by min and max will be divided up into this * many bins. * @param min the minimum of the range covered by the histogram bins * @param max the maximum value of the range covered by the histogram bins */ public Histogram(String name, int nbins, double min, double max){ m_nbins = nbins; m_min = min; m_max = max; m_name = name; m_hist = new double[m_nbins]; m_underflow = 0; m_overflow = 0; } /** Enter data into the histogram. The fill method takes the given * value, works out which bin this corresponds to, and increments * this bin by one. * @param x is the value to add in to the histogram */ public void fill(double x){ // use findBin method to work out which bin x falls in BinInfo bin = findBin(x); // check the result of findBin in case it was an overflow or underflow if (bin.isUnderflow){ m_underflow++; } if (bin.isOverflow){ m_overflow++; } if (bin.isInRange){ m_hist[bin.index]++; } // print out some debug information if the flag is set if (m_debug) { System.out.println("debug: fill: value " + x + " # underflows " + m_underflow + " # overflows " + m_overflow + " bin index " + bin.index); } // count the number of entries made by the fill method m_entries++; } /** Private class used internally to store info about which bin of * the histogram to use for a number to be filled. */ private class BinInfo { public int index; public boolean isUnderflow; public boolean isOverflow; public boolean isInRange; } /** Private internal utility method to figure out which bin of the * histogram a number falls in. * @return info on which bin x falls in. */ private BinInfo findBin(double x){ BinInfo bin = new BinInfo(); bin.isInRange = false; bin.isUnderflow = false; bin.isOverflow = false; // first check if x is outside the range of the normal histogram bins if (x < m_min){ bin.isUnderflow = true; } else if (x > m_max){ bin.isOverflow = true; } else { // search for histogram bin into which x falls double binWidth = (m_max - m_min)/m_nbins; for (int i=0; i