Class Histogram


  • public class Histogram
    extends Object
    A simple histogram class. It can be used to accumulate a histogram and calculate statistical information about it.
    Version:
    1.0 31 Aug 2001
    Author:
    Simon George
    • Constructor Summary

      Constructors 
      Constructor Description
      Histogram​(String name, int nbins, double min, double max)
      Constructor which sets name, number of bins, and range.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double area()
      Calculate area of histogram, excluding over/underflows, by summing the contents of all the bins
      int entries()
      Get number of entries in the histogram.
      void fill​(double x)
      Enter data into the histogram.
      double[] getArray()
      This method gives you the bin contents in the form of an array.
      boolean getDebug()
      Get debug flag.
      double max()
      Get upper end of histogram range
      double mean()
      The mean estimator is calculated from the average of the bin centres, weighted by their content.
      double min()
      Get lower end of histogram range
      String name()
      Get the name of the histogram.
      int numberOfBins()
      Get the number of bins in the histogram.
      double overflow()
      Get the height of the overflow bin.
      void setDebug​(boolean flag)
      Set debug flag.
      void show()
      Print the histogram data to the console.
      double underflow()
      Get the height of the underflow bin.
      void writeToFile​(String fileName)
      Save the histogram data to a file.
    • Constructor Detail

      • Histogram

        public Histogram​(String name,
                         int nbins,
                         double min,
                         double max)
        Constructor which sets name, number of bins, and range.
        Parameters:
        name - Give the histogram a text name
        nbins - the number of bins the histogram should have. The range specified by min and max will be divided up into this many bins.
        min - the minimum of the range covered by the histogram bins
        max - the maximum value of the range covered by the histogram bins
    • Method Detail

      • fill

        public void fill​(double x)
        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.
        Parameters:
        x - is the value to add in to the histogram
      • mean

        public double mean()
        The mean estimator is calculated from the average of the bin centres, weighted by their content. It does not include over/underflows.
        Returns:
        mean
      • area

        public double area()
        Calculate area of histogram, excluding over/underflows, by summing the contents of all the bins
        Returns:
        area under histogram
      • writeToFile

        public void writeToFile​(String fileName)
                         throws IOException
        Save the histogram data to a file. The file format is very simple, human-readable text so it can be imported into Excel or cut and pasted into other applications.
        Parameters:
        fileName - name of the file to write the histogram to. Note this must be valid for your operating system, e.g. a unix filename might not work under windows
        Throws:
        IOException - if file cannot be opened or written to.
      • show

        public void show()
        Print the histogram data to the console. Output is only basic, intended for debugging purposes. A good example of formatted output.
      • entries

        public int entries()
        Get number of entries in the histogram. This should correspond to the number of times the fill method has been used.
        Returns:
        number of entries
      • name

        public String name()
        Get the name of the histogram. The name is an arbitrary label for the user, and is set by the constructor.
        Returns:
        histogram name
      • numberOfBins

        public int numberOfBins()
        Get the number of bins in the histogram. The range of the histogram defined by min and max is divided into this many bins.
        Returns:
        number of bins
      • min

        public double min()
        Get lower end of histogram range
        Returns:
        minimum x value covered by histogram
      • max

        public double max()
        Get upper end of histogram range
        Returns:
        maximum x value covered by histogram
      • overflow

        public double overflow()
        Get the height of the overflow bin. Any value passed to the fill method which falls above the range of the histogram will be counted in the overflow bin.
        Returns:
        number of overflows
      • underflow

        public double underflow()
        Get the height of the underflow bin. Any value passed to the fill method which falls below the range of the histogram will be counted in the underflow bin.
        Returns:
        number of underflows
      • getArray

        public double[] getArray()
        This method gives you the bin contents in the form of an array. It might be useful for example if you want to use the histogram in some other way, for example to pass to a plotting package.
        Returns:
        array of bin heights
      • setDebug

        public void setDebug​(boolean flag)
        Set debug flag.
        Parameters:
        flag - debug flag (true or false)
      • getDebug

        public boolean getDebug()
        Get debug flag.
        Returns:
        value of debug flag (true or false)