public static HistogramHolder createSimpleHistogram(int binWidth, List<Integer> theValues) {

    if (theValues == null || theValues.isEmpty()) {

      throw new IllegalArgumentException(
          "values and valueErrors cannot be null and must be the same length");
    }

    float[] values = new float[theValues.size()];
    for (int i = 0; i < theValues.size(); ++i) {
      int v = theValues.get(i).intValue();
      values[i] = v;
    }

    float[] valueErrors = Errors.populateYErrorsBySqrt(values);

    float[] minMax = MiscMath.calculateOuterRoundedMinAndMax(values);

    int nBins = (int) Math.ceil(((minMax[1] - minMax[0])) / binWidth);
    if (nBins < 0) {
      nBins *= -1;
    }

    float[] xHist = new float[nBins];
    int[] yHist = new int[nBins];

    Histogram.createHistogram(values, nBins, minMax[0], minMax[1], xHist, yHist, binWidth);

    float[] yHistFloat = new float[yHist.length];
    for (int i = 0; i < yHist.length; i++) {
      yHistFloat[i] = (float) yHist[i];
    }

    float[] yErrors = new float[xHist.length];
    float[] xErrors = new float[xHist.length];

    calulateHistogramBinErrors(xHist, yHist, values, valueErrors, xErrors, yErrors);

    HistogramHolder histogram = new HistogramHolder();
    histogram.setXHist(xHist);
    histogram.setYHist(yHist);
    histogram.setYHistFloat(yHistFloat);
    histogram.setYErrors(yErrors);
    histogram.setXErrors(xErrors);

    return histogram;
  }
  public static HistogramHolder createSimpleHistogram(GreyscaleImage img) {

    float[] values = new float[img.getNPixels()];
    int count = 0;
    for (int i = 0; i < img.getWidth(); ++i) {
      for (int j = 0; j < img.getHeight(); ++j) {
        int v = img.getValue(i, j);
        values[count] = v;
        count++;
      }
    }

    float[] errors = Errors.populateYErrorsBySqrt(values);

    HistogramHolder hist = createSimpleHistogram(values, errors);

    return hist;
  }
  public static HistogramHolder createSimpleHistogram(List<Integer> values) {

    if (values == null) {
      throw new IllegalArgumentException(
          "values and valueErrors cannot be null and must be the same length");
    }
    if (values.isEmpty()) {
      return null;
    }
    float[] v = new float[values.size()];
    for (int i = 0; i < values.size(); ++i) {
      v[i] = values.get(i).intValue();
    }
    float[] ve = Errors.populateYErrorsBySqrt(v);

    int nBins = (int) (2 * Math.pow(v.length, 0.3333));

    if (v.length == 1) {
      nBins = 1;
    }

    return createSimpleHistogram(nBins, v, ve);
  }
  public static HistogramHolder createHistogramAndRemovePreAndProceedingZeros(GreyscaleImage img) {

    float[] values = new float[img.getNPixels()];
    int count = 0;
    for (int i = 0; i < img.getWidth(); ++i) {
      for (int j = 0; j < img.getHeight(); ++j) {
        int v = img.getValue(i, j);
        values[count] = v;
        count++;
      }
    }

    float[] errors = Errors.populateYErrorsBySqrt(values);

    HistogramHolder hist = createSimpleHistogram(values, errors);

    if (hist.getXHist().length < 2) {
      return hist;
    }

    int n = hist.getXHist().length;

    // int yMax = MiscMath.findMax(hist.getYHist());
    // int yMin = MiscMath.findMin(hist.getYHist());

    int fIdx = 0;
    for (int i = 0; i < n; ++i) {
      fIdx = i;
      int y = hist.getYHist()[i];
      if (y > 0) {
        break;
      }
    }
    int lIdx = (n - 1);
    for (int i = (n - 1); i > -1; --i) {
      lIdx = i;
      int y = hist.getYHist()[i];
      if (y > 0) {
        break;
      }
    }

    if ((fIdx <= 1) && (lIdx >= (n - 2))) {
      return hist;
    }

    /*
    if there are a large number of bins, re-do the x idx limits using
    a lower limit larger than spurious counts... this may need revision
    */
    // TODO: consider comparing (lIdx - fIdx) > 30
    if (hist.getXHist().length > 30) {
      int tmp0 = fIdx;
      int tmp1 = lIdx;
      int xMaxYIdx = MiscMath.findYMaxIndex(hist.getYHist());
      float limit = (int) Math.ceil(Math.sqrt(hist.getYHist()[xMaxYIdx]));
      fIdx = 0;
      for (int i = 0; i < n; ++i) {
        fIdx = i;
        int y = hist.getYHist()[i];
        if (y > limit) {
          break;
        }
      }
      lIdx = (n - 1);
      for (int i = (n - 1); i > -1; --i) {
        lIdx = i;
        int y = hist.getYHist()[i];
        if (y > limit) {
          break;
        }
      }
      int maxDiffIdx = Math.max(xMaxYIdx - fIdx, lIdx - xMaxYIdx);
      fIdx = xMaxYIdx - 3 * maxDiffIdx;
      lIdx = xMaxYIdx + 3 * maxDiffIdx;

      if (fIdx < 0) {
        fIdx = tmp0;
      }
      if (lIdx > (n - 1)) {
        lIdx = tmp1;
      }
    }

    // re-do for smaller range
    float xBin = hist.getXHist()[1] - hist.getXHist()[0];

    float xf;
    if (fIdx == 0) {
      xf = hist.getXHist()[0] - (xBin / 2.f);
    } else {
      xf = hist.getXHist()[fIdx - 1] - (xBin / 2.f);
    }
    float xl;
    if (lIdx == (n - 1)) {
      xl = hist.getXHist()[lIdx] + (xBin / 2.f);
    } else {
      xl = hist.getXHist()[lIdx + 1] + (xBin / 2.f);
    }

    int n2 = 0;
    float[] values2 = new float[values.length];
    float[] errors2 = new float[values2.length];
    for (int i = 0; i < values2.length; ++i) {
      float v = values[i];
      if ((v >= xf) && (v <= xl)) {
        values2[n2] = v;
        errors2[n2] = errors[n2];
        n2++;
      }
    }
    values2 = Arrays.copyOf(values2, n2);
    errors2 = Arrays.copyOf(errors2, n2);

    hist = createSimpleHistogram(values2, errors2);

    return hist;
  }