Package edu.cnu.mdi.splot.pdata
Class HistoData
java.lang.Object
edu.cnu.mdi.splot.pdata.HistoData
Container for 1D histogram data.
Bin edges are stored in grid[] of length (numBins + 1).
Counts are stored in counts[] of length numBins.
Responsibilities
This class is responsible only for data storage and bin arithmetic:
- Filling, clearing, and querying bin counts
- Underflow and overflow tracking
- Basic statistics (mean, sigma, rms)
- Display preferences (rms vs sigma in legend, stat error flag)
Out-of-scope (separated into dedicated classes)
- Peak finding and fit-vector preparation — see
HistoFitPrep - Screen polygon and status-string rendering — see
HistoDrawingUtils
- Author:
- heddle
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(double value) Adds a single value to the histogram.voidaddAll(double[] values) Adds many values to the histogram in bulk.voidclear()Resets all counts and invalidates the statistics cache.booleanReturns whether sqrt(n) statistical error bars should be drawn.intConvenience delegation: finds the raw peak bin (maximum count).intfindPeakBin(int bin0, int bin1) Convenience delegation: finds the raw peak bin in a restricted range.double[]Computes (and caches) the mean, standard deviation, and RMS of the histogram, weighted by bin counts.intgetBin(double val) Returns the zero-based bin index for a given value.doublegetBinMaxX(int bin) Returns the right edge of a bin.doublegetBinMidValue(int bin) Returns the midpoint x value of a bin.doublegetBinMinX(int bin) Returns the left edge of a bin.doublegetBinWidth(int bin) Returns the width of a bin (right - left).longgetCount(int bin) Returns the count for a specific bin.long[]Returns the live counts array.long[]Returns a defensive copy of the counts array.longReturns the total number of in-range entries (excludes underflow/overflow).double[]Returns a defensive copy of the bin-edge grid.doublegetMaxX()Returns the right edge (maximum x) of the histogram range, i.e.doublegetMaxY()Returns the maximum y value, i.e.doublegetMinX()Returns the left edge (minimum x) of the histogram range, i.e.doublegetMinY()Returns the minimum y value.intReturns the number of bins.longReturns the number of values that fell above the histogram range.longReturns the total entry count including underflow and overflow.longReturns the number of values that fell below the histogram range.Returns a string describing the bin(s) with the maximum count (1-based indices).name()Returns the histogram name.prepareForFit(boolean includeZeroBins) Convenience delegation: prepares fit vectors over the full x-range.prepareForFit(boolean includeZeroBins, double xmin, double xmax, boolean poissonWeights) Convenience delegation: prepares fit vectors over a data-space x-range.prepareForFit(boolean includeZeroBins, int bin0, int bin1, boolean poissonWeights) Convenience delegation: prepares fit vectors over an inclusive bin-index range.prepareForFitAroundBestPeakGuarded(boolean includeZeroBins, int halfWindowBins, int smoothRadius, boolean ignoreZeroBins, boolean poissonWeights, int minPoints) Convenience delegation: finds the best peak using smoothing and robust plateau handling, then prepares fit vectors in a guarded window around it.prepareForFitAroundPeak(boolean includeZeroBins, int peakBin, int halfWindowBins, boolean poissonWeights) Convenience delegation: prepares fit vectors in a symmetric window around a given peak bin.voidsetCount(double val, int count) Sets the count for the bin that containsval.voidsetCountsForDeserialization(long[] countsSrc, long underCount, long overCount) Replaces the histogram counts from an external array.voidsetDrawStatisticalErrors(boolean statErr) Sets whether sqrt(n) statistical error bars are drawn on histogram bins.voidSets the histogram name.voidsetRmsInHistoLegend(boolean useRMS) Sets whether rms (rather than sigma) is shown in histogram legends.statStr()Returns a single-line status string showing mean and either rms or sigma, plus under/overflow counts.toString()booleanReturns whether rms (rather than sigma) is shown in histogram legends.
-
Constructor Details
-
HistoData
Creates a uniform-bin histogram.- Parameters:
name- histogram name (used as curve label)valMin- range minimum (must be strictly less thanvalMax)valMax- range maximumnumBins- number of bins (>= 1)- Throws:
IllegalArgumentException- ifnumBins < 1orvalMax <= valMin
-
HistoData
Creates a histogram with arbitrary (non-uniform) bin spacing.- Parameters:
name- histogram name (used as curve label)grid- bin-edge array in strictly ascending order (length>= 2)- Throws:
NullPointerException- ifgridisnullIllegalArgumentException- ifgrid.length < 2or the array is not strictly ascending
-
-
Method Details
-
name
Returns the histogram name.- Returns:
- histogram name (never
nullafter construction)
-
setName
Sets the histogram name.- Parameters:
name- new name
-
getNumberBins
public int getNumberBins()Returns the number of bins.- Returns:
grid.length - 1
-
getGridCopy
public double[] getGridCopy()Returns a defensive copy of the bin-edge grid.- Returns:
- copy of the
gridarray (length =getNumberBins()+ 1)
-
getMinX
public double getMinX()Returns the left edge (minimum x) of the histogram range, i.e.grid[0].- Returns:
- minimum x value
-
getMaxX
public double getMaxX()Returns the right edge (maximum x) of the histogram range, i.e.grid[grid.length - 1].- Returns:
- maximum x value
-
getMinY
public double getMinY()Returns the minimum y value. For histograms this is always0.- Returns:
0.0
-
getMaxY
public double getMaxY()Returns the maximum y value, i.e. the largest bin count. Returns at least1so that an empty histogram still has a drawable y range.- Returns:
- max bin count as a
double(>= 1)
-
getBinMidValue
public double getBinMidValue(int bin) Returns the midpoint x value of a bin.- Parameters:
bin- zero-based bin index- Returns:
- bin midpoint, or
Double.NaNifbinis out of range
-
getBinMinX
public double getBinMinX(int bin) Returns the left edge of a bin.- Parameters:
bin- zero-based bin index- Returns:
- left edge, or
Double.NaNifbinis out of range
-
getBinMaxX
public double getBinMaxX(int bin) Returns the right edge of a bin.- Parameters:
bin- zero-based bin index- Returns:
- right edge, or
Double.NaNifbinis out of range
-
getBinWidth
public double getBinWidth(int bin) Returns the width of a bin (right - left).- Parameters:
bin- zero-based bin index- Returns:
- bin width, or
Double.NaNifbinis out of range
-
getBin
public int getBin(double val) Returns the zero-based bin index for a given value.- Parameters:
val- the value to look up- Returns:
- bin index in
[0, numBins - 1], orUNDERFLOW/OVERFLOWfor out-of-range values
-
clear
public void clear()Resets all counts and invalidates the statistics cache. -
add
public void add(double value) Adds a single value to the histogram.- Parameters:
value- the value to fill
-
addAll
public void addAll(double[] values) Adds many values to the histogram in bulk.Equivalent to calling
add(double)for each element, but invalidates the statistics cache only once.- Parameters:
values- array of sample values (non-null)- Throws:
NullPointerException- ifvaluesisnull
-
setCount
public void setCount(double val, int count) Sets the count for the bin that containsval.Values outside the range are added to the under/overflow accumulators.
- Parameters:
val- value identifying the target bincount- count to assign
-
setCountsForDeserialization
public void setCountsForDeserialization(long[] countsSrc, long underCount, long overCount) Replaces the histogram counts from an external array.Intended for deserialization / persistence only; not for normal filling.
- Parameters:
countsSrc- source counts array (length must equalgetNumberBins())underCount- underflow count to restoreoverCount- overflow count to restore- Throws:
IllegalArgumentException- ifcountsSrclength mismatches
-
getCounts
public long[] getCounts()Returns the live counts array.The returned array is the internal storage — do not modify it. For a safe copy use
getCountsCopy().- Returns:
- live counts array (length =
getNumberBins())
-
getCountsCopy
public long[] getCountsCopy()Returns a defensive copy of the counts array.- Returns:
- copy of the counts array
-
getCount
public long getCount(int bin) Returns the count for a specific bin.- Parameters:
bin- zero-based bin index- Returns:
- count, or
0ifbinis out of range
-
getGoodCount
public long getGoodCount()Returns the total number of in-range entries (excludes underflow/overflow).- Returns:
- sum of all bin counts
-
getTotalCount
public long getTotalCount()Returns the total entry count including underflow and overflow.- Returns:
getGoodCount() + getUnderCount() + getOverCount()
-
getUnderCount
public long getUnderCount()Returns the number of values that fell below the histogram range.- Returns:
- underflow count
-
getOverCount
public long getOverCount()Returns the number of values that fell above the histogram range.- Returns:
- overflow count
-
getBasicStatistics
public double[] getBasicStatistics()Computes (and caches) the mean, standard deviation, and RMS of the histogram, weighted by bin counts.- Returns:
- array
[mean, stdDev, rms], allDouble.NaNif the histogram is empty
-
statStr
Returns a single-line status string showing mean and either rms or sigma, plus under/overflow counts. The choice between rms and sigma is controlled byuseRmsInHistoLegend().- Returns:
- formatted statistics string
-
maxBinString
Returns a string describing the bin(s) with the maximum count (1-based indices).- Returns:
- descriptive string, or
""if the histogram is empty
-
setRmsInHistoLegend
public void setRmsInHistoLegend(boolean useRMS) Sets whether rms (rather than sigma) is shown in histogram legends.- Parameters:
useRMS-trueto show rms;falseto show sigma
-
useRmsInHistoLegend
public boolean useRmsInHistoLegend()Returns whether rms (rather than sigma) is shown in histogram legends.- Returns:
trueif rms is shown
-
setDrawStatisticalErrors
public void setDrawStatisticalErrors(boolean statErr) Sets whether sqrt(n) statistical error bars are drawn on histogram bins.- Parameters:
statErr-trueto draw error bars
-
drawStatisticalErrors
public boolean drawStatisticalErrors()Returns whether sqrt(n) statistical error bars should be drawn.- Returns:
trueif statistical errors are drawn
-
prepareForFit
Convenience delegation: prepares fit vectors over the full x-range.Equivalent to
HistoFitPrep.prepareForFit(HistoData, boolean).- Parameters:
includeZeroBins- iftrue, bins with zero counts are included- Returns:
- fit vectors (
x,y,weights = null) - See Also:
-
prepareForFit
public FitVectors prepareForFit(boolean includeZeroBins, double xmin, double xmax, boolean poissonWeights) Convenience delegation: prepares fit vectors over a data-space x-range.- Parameters:
includeZeroBins- iftrue, bins with zero counts are includedxmin- minimum x to include (data space)xmax- maximum x to include (data space)poissonWeights- iftrue, include Poisson (1/count) weights- Returns:
- fit vectors
- See Also:
-
prepareForFit
public FitVectors prepareForFit(boolean includeZeroBins, int bin0, int bin1, boolean poissonWeights) Convenience delegation: prepares fit vectors over an inclusive bin-index range.- Parameters:
includeZeroBins- iftrue, bins with zero counts are includedbin0- inclusive start bin (clamped to valid range)bin1- inclusive end bin (clamped to valid range)poissonWeights- iftrue, include Poisson weights- Returns:
- fit vectors
- See Also:
-
prepareForFitAroundPeak
public FitVectors prepareForFitAroundPeak(boolean includeZeroBins, int peakBin, int halfWindowBins, boolean poissonWeights) Convenience delegation: prepares fit vectors in a symmetric window around a given peak bin.- Parameters:
includeZeroBins- iftrue, bins with zero counts are includedpeakBin- center bin of the window (0-based)halfWindowBins- half-width of the window in bins (>= 0)poissonWeights- iftrue, include Poisson weights- Returns:
- fit vectors
- See Also:
-
findPeakBin
public int findPeakBin()Convenience delegation: finds the raw peak bin (maximum count).- Returns:
- peak bin index (0-based), or
-1if there are no bins - See Also:
-
findPeakBin
public int findPeakBin(int bin0, int bin1) Convenience delegation: finds the raw peak bin in a restricted range.- Parameters:
bin0- inclusive start bin (0-based)bin1- inclusive end bin (0-based)- Returns:
- peak bin index (0-based), or
-1if there are no bins - See Also:
-
prepareForFitAroundBestPeakGuarded
public HistoFitPrep.FitWindowData prepareForFitAroundBestPeakGuarded(boolean includeZeroBins, int halfWindowBins, int smoothRadius, boolean ignoreZeroBins, boolean poissonWeights, int minPoints) Convenience delegation: finds the best peak using smoothing and robust plateau handling, then prepares fit vectors in a guarded window around it.- Parameters:
includeZeroBins- iftrue, include zero-count bins in fit vectorshalfWindowBins- requested half-window size (>= 0)smoothRadius- smoothing radius for peak detection (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped as peak candidatespoissonWeights- iftrue, include Poisson weightsminPoints- minimum points required after filtering (>= 1)- Returns:
- fit window data including metadata about the window that was used
- See Also:
-
toString
-