Class HistoFitPrep
This class extracts the peak-finding and fit-vector building logic that
was previously embedded in HistoData, keeping HistoData
focused on data storage.
Fit-vector layout
All prepare* methods return a FitVectors (or the richer
HistoFitPrep.FitWindowData subtype) whose arrays follow this convention:
x[i]— bin center (mid-point of bini)y[i]— bin count as adoublew[i]— optional Poisson weight1/countforcount > 0,1.0for zero bins (when weights are requested and the bin is included)
Peak-finding strategies
findPeakBin(HistoData)— raw maximum (first occurrence on ties)findPeakBin(HistoData, int, int)— raw maximum in a restricted bin rangefindPeakBinSmoothed(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)— flat moving-average smoothingfindPeakBinTriangularSmoothed(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)— triangular-kernel smoothingfindPeakBinBest(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)— triangular smoothing plus plateau-handling (preferred general-purpose strategy)
- Author:
- heddle (original), refactored for extraction
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAFitVectorsthat also records which peak bin and window were actually used. -
Method Summary
Modifier and TypeMethodDescriptionstatic intReturns the zero-based index of the bin with the highest count (raw maximum).static intfindPeakBin(HistoData h, int bin0, int bin1) Returns the zero-based index of the bin with the highest count within an inclusive bin range.static intfindPeakBinBest(HistoData h, int smoothRadius, boolean ignoreZeroBins) Convenience overload offindPeakBinBest(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)over the full histogram range.static intfindPeakBinBest(HistoData h, int bin0, int bin1, int smoothRadius, boolean ignoreZeroBins) Finds the best peak using triangular smoothing plus robust plateau handling.static intfindPeakBinSmoothed(HistoData h, int radius, boolean ignoreZeroBins) Convenience overload offindPeakBinSmoothed(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)over the full histogram range.static intfindPeakBinSmoothed(HistoData h, int bin0, int bin1, int radius, boolean ignoreZeroBins) Finds the peak bin using a flat moving-average smoothing kernel.static intfindPeakBinTriangularSmoothed(HistoData h, int radius, boolean ignoreZeroBins) Convenience overload offindPeakBinTriangularSmoothed(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)over the full histogram range.static intfindPeakBinTriangularSmoothed(HistoData h, int bin0, int bin1, int radius, boolean ignoreZeroBins) Finds the peak bin using a triangular (linear-decay) smoothing kernel.static FitVectorsprepareForFit(HistoData h, boolean includeZeroBins) Prepares fit vectors over the full x-range using unit weights.static FitVectorsprepareForFit(HistoData h, boolean includeZeroBins, double xmin, double xmax, boolean poissonWeights) Prepares fit vectors over a data-space x-range.static FitVectorsprepareForFit(HistoData h, boolean includeZeroBins, int bin0, int bin1, boolean poissonWeights) Prepares fit vectors over an inclusive bin-index range.static HistoFitPrep.FitWindowDataprepareForFitAroundBestPeakGuarded(HistoData h, boolean includeZeroBins, int halfWindowBins, int smoothRadius, boolean ignoreZeroBins, boolean poissonWeights, int minPoints) Prepares fit vectors around the best peak with edge-safety and minimum-points guarantees.static HistoFitPrep.FitWindowDataprepareForFitAroundBestPeakGuarded(HistoData h, boolean includeZeroBins, int searchBin0, int searchBin1, int halfWindowBins, int smoothRadius, boolean ignoreZeroBins, boolean poissonWeights, int minPoints) Prepares fit vectors around the best peak within a restricted search range, with edge-safety and minimum-points guarantees.static FitVectorsprepareForFitAroundPeak(HistoData h, boolean includeZeroBins, int peakBin, int halfWindowBins, boolean poissonWeights) Prepares fit vectors in a symmetric window around a given peak bin.
-
Method Details
-
prepareForFit
Prepares fit vectors over the full x-range using unit weights.- Parameters:
h- source histogram (non-null)includeZeroBins- iftrue, bins with zero count are included- Returns:
- fit vectors (
weights = null)
-
prepareForFit
public static FitVectors prepareForFit(HistoData h, boolean includeZeroBins, double xmin, double xmax, boolean poissonWeights) Prepares fit vectors over a data-space x-range.- Parameters:
h- source histogram (non-null)includeZeroBins- iftrue, include bins with zero countxmin- minimum x to include (data space, inclusive)xmax- maximum x to include (data space, inclusive)poissonWeights- iftrue, include Poisson (1/count) weights- Returns:
- fit vectors;
weightsarray isnullunlesspoissonWeightsistrue
-
prepareForFit
public static FitVectors prepareForFit(HistoData h, boolean includeZeroBins, int bin0, int bin1, boolean poissonWeights) Prepares fit vectors over an inclusive bin-index range.- Parameters:
h- source histogram (non-null)includeZeroBins- iftrue, include bins with zero countbin0- inclusive start bin (0-based; clamped to valid range)bin1- inclusive end bin (0-based; clamped to valid range)poissonWeights- iftrue, include Poisson weights- Returns:
- fit vectors
-
prepareForFitAroundPeak
public static FitVectors prepareForFitAroundPeak(HistoData h, boolean includeZeroBins, int peakBin, int halfWindowBins, boolean poissonWeights) Prepares fit vectors in a symmetric window around a given peak bin.- Parameters:
h- source histogram (non-null)includeZeroBins- iftrue, include bins with zero countpeakBin- center bin of the window (0-based)halfWindowBins- half-width of the window in bins (>= 0)poissonWeights- iftrue, include Poisson weights- Returns:
- fit vectors
- Throws:
IllegalArgumentException- ifhalfWindowBins < 0
-
findPeakBin
Returns the zero-based index of the bin with the highest count (raw maximum). On ties, the first occurrence is returned.- Parameters:
h- source histogram (non-null)- Returns:
- peak bin index, or
-1if the histogram has no bins
-
findPeakBin
Returns the zero-based index of the bin with the highest count within an inclusive bin range. On ties, the first occurrence is returned.- Parameters:
h- source histogram (non-null)bin0- inclusive start bin (0-based; clamped)bin1- inclusive end bin (0-based; clamped)- Returns:
- peak bin index in the range, or
-1if there are no bins
-
findPeakBinSmoothed
public static int findPeakBinSmoothed(HistoData h, int bin0, int bin1, int radius, boolean ignoreZeroBins) Finds the peak bin using a flat moving-average smoothing kernel.For each candidate bin
iin[bin0, bin1], the score is the average count over[i-radius, i+radius]. The bin with the highest score wins; on a tie the bin with the higher raw count is preferred. Falls back tofindPeakBin(HistoData, int, int)if no candidate is found.- Parameters:
h- source histogram (non-null)bin0- inclusive start bin for the search (0-based; clamped)bin1- inclusive end bin for the search (0-based; clamped)radius- smoothing half-width in bins (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped as candidates- Returns:
- peak bin index, or
-1if there are no bins - Throws:
IllegalArgumentException- ifradius < 0
-
findPeakBinSmoothed
Convenience overload offindPeakBinSmoothed(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)over the full histogram range.- Parameters:
h- source histogram (non-null)radius- smoothing half-width in bins (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped- Returns:
- peak bin index, or
-1if there are no bins
-
findPeakBinTriangularSmoothed
public static int findPeakBinTriangularSmoothed(HistoData h, int bin0, int bin1, int radius, boolean ignoreZeroBins) Finds the peak bin using a triangular (linear-decay) smoothing kernel.Bins closer to the candidate bin
icontribute more to the score. Weight of binjis(radius + 1 - |j - i|). On a tie, prefer the bin with the higher raw count; if still tied, prefer the bin closer to the center of the search range.- Parameters:
h- source histogram (non-null)bin0- inclusive start bin (0-based; clamped)bin1- inclusive end bin (0-based; clamped)radius- smoothing half-width (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped- Returns:
- peak bin index, or
-1if there are no bins - Throws:
IllegalArgumentException- ifradius < 0
-
findPeakBinTriangularSmoothed
Convenience overload offindPeakBinTriangularSmoothed(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)over the full histogram range.- Parameters:
h- source histogram (non-null)radius- smoothing half-width (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped- Returns:
- peak bin index, or
-1if there are no bins
-
findPeakBinBest
public static int findPeakBinBest(HistoData h, int bin0, int bin1, int smoothRadius, boolean ignoreZeroBins) Finds the best peak using triangular smoothing plus robust plateau handling.When multiple adjacent bins share the top smoothed score (a plateau), this method picks the best raw-count bin within that plateau, resolving further ties by proximity to the plateau's own midpoint and then to the search-range midpoint. This strategy is preferred over pure triangular-smoothed search for most fitting use cases.
- Parameters:
h- source histogram (non-null)bin0- inclusive start bin (0-based; clamped)bin1- inclusive end bin (0-based; clamped)smoothRadius- smoothing half-width (>= 0);0degenerates to raw-peak-with-plateauignoreZeroBins- iftrue, zero-count bins are skipped as candidates- Returns:
- peak bin index, or
-1if there are no bins - Throws:
IllegalArgumentException- ifsmoothRadius < 0
-
findPeakBinBest
Convenience overload offindPeakBinBest(edu.cnu.mdi.splot.pdata.HistoData, int, int, int, boolean)over the full histogram range.- Parameters:
h- source histogram (non-null)smoothRadius- smoothing half-width (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped- Returns:
- peak bin index, or
-1if there are no bins
-
prepareForFitAroundBestPeakGuarded
public static HistoFitPrep.FitWindowData prepareForFitAroundBestPeakGuarded(HistoData h, boolean includeZeroBins, int halfWindowBins, int smoothRadius, boolean ignoreZeroBins, boolean poissonWeights, int minPoints) Prepares fit vectors around the best peak with edge-safety and minimum-points guarantees.The strategy:
- Prefer peaks that can support the full requested window without clipping (search in the "inner" sub-range first).
- Auto-shrink the half-window if the peak is near an edge.
- If the result has fewer than
minPointsentries, expand to the maximum available window. - Last resort: if the caller excluded zero bins, retry with zeros included.
- If still insufficient, return an empty
HistoFitPrep.FitWindowDatawith metadata preserved.
- Parameters:
h- source histogram (non-null)includeZeroBins- iftrue, include zero-count bins normallyhalfWindowBins- requested half-window in bins (>= 0)smoothRadius- smoothing radius for peak detection (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped as peak candidatespoissonWeights- iftrue, include Poisson weightsminPoints- minimum number of points required (>= 1)- Returns:
HistoFitPrep.FitWindowDataincluding metadata about the selected window- Throws:
IllegalArgumentException- ifhalfWindowBins < 0orminPoints < 1
-
prepareForFitAroundBestPeakGuarded
public static HistoFitPrep.FitWindowData prepareForFitAroundBestPeakGuarded(HistoData h, boolean includeZeroBins, int searchBin0, int searchBin1, int halfWindowBins, int smoothRadius, boolean ignoreZeroBins, boolean poissonWeights, int minPoints) Prepares fit vectors around the best peak within a restricted search range, with edge-safety and minimum-points guarantees.- Parameters:
h- source histogram (non-null)includeZeroBins- iftrue, include zero-count bins normallysearchBin0- inclusive start bin to search for a peak (0-based; clamped)searchBin1- inclusive end bin to search for a peak (0-based; clamped)halfWindowBins- requested half-window in bins (>= 0)smoothRadius- smoothing radius for peak detection (>= 0)ignoreZeroBins- iftrue, zero-count bins are skipped as peak candidatespoissonWeights- iftrue, include Poisson weightsminPoints- minimum number of points required (>= 1)- Returns:
HistoFitPrep.FitWindowDataincluding metadata about the selected window- Throws:
IllegalArgumentException- ifhalfWindowBins < 0orminPoints < 1
-