1 package hep.aida.ref;
2 
3 import hep.aida.IAxis;
4 import hep.aida.IHistogram;
5 /**
6 Fixed-width axis; A reference implementation of hep.aida.IAxis.
7 
8 @author Wolfgang Hoschek, Tony Johnson, and others.
9 @version 1.0, 23/03/2000
10 */
11 public class FixedAxis implements IAxis
12 {
13 	private int bins;
14 	private double min;
15 	private double binWidth;
16 	// Package private for ease of use in Histogram1D and Histogram2D
17 	private int xunder, xover;
18 	/**
19 	 * Create an Axis
20 	 * @param bins Number of bins
21 	 * @param min Minimum for axis
22 	 * @param max Maximum for axis
23 	 */
FixedAxis(int bins, double min, double max)24 	public FixedAxis(int bins, double min, double max)
25 	{
26 		if (bins < 1) throw new IllegalArgumentException("bins="+bins);
27 		if (max <= min) throw new IllegalArgumentException("max <= min");
28 
29 		// Note, for internal consistency we save only min and binWidth
30 		// and always use these quantities for all calculations. Due to
31 		// rounding errors the return value from upperEdge is not necessarily
32 		// exactly equal to max
33 
34 		this.bins = bins;
35 		this.min = min;
36 		this.binWidth =  (max - min)/bins;
37 
38 		// our internal definition of overflow/underflow differs from
39 		// that of the outside world
40 		//this.under = 0;
41 		//this.over = bins+1;
42 
43 	}
binCentre(int index)44 	public double binCentre(int index)
45 	{
46 		return min + binWidth*index + binWidth/2;
47 	}
binLowerEdge(int index)48 	public double binLowerEdge(int index)
49 	{
50 		if (index == IHistogram.UNDERFLOW) return Double.NEGATIVE_INFINITY;
51 		if (index == IHistogram.OVERFLOW) return upperEdge();
52 		return min + binWidth*index;
53 	}
bins()54 	public int bins()
55 	{
56 		return bins;
57 	}
binUpperEdge(int index)58 	public double binUpperEdge(int index)
59 	{
60 		if (index == IHistogram.UNDERFLOW) return min;
61 		if (index == IHistogram.OVERFLOW) return Double.POSITIVE_INFINITY;
62 		return min + binWidth*(index+1);
63 	}
binWidth(int index)64 	public double binWidth(int index)
65 	{
66 		return binWidth;
67 	}
coordToIndex(double coord)68 	public int coordToIndex(double coord)
69 	{
70 		if (coord < min) return IHistogram.UNDERFLOW;
71 		int index = (int) Math.floor((coord - min)/binWidth);
72 		if (index >= bins) return IHistogram.OVERFLOW;
73 
74 		return index;
75 	}
lowerEdge()76 	public double lowerEdge()
77 	{
78 		return min;
79 	}
upperEdge()80 	public double upperEdge()
81 	{
82 		return min + binWidth*bins;
83 	}
84 	/**
85 	 * This package private method is similar to coordToIndex except
86 	 * that it returns our internal definition for overflow/underflow
87 	 */
xgetBin(double coord)88 	int xgetBin(double coord)
89 	{
90 		if (coord < min) return xunder;
91 		int index = (int) Math.floor((coord - min)/binWidth);
92 		if (index > bins) return xover;
93 		return index+1;
94 	}
95 	/**
96 	 * Package private method to map from the external representation of bin
97 	 * number to our internal representation of bin number
98 	 */
xmap(int index)99 	int xmap(int index)
100 	{
101 		if (index >= bins) throw new IllegalArgumentException("bin="+index);
102 		if (index >= 0) return index+1;
103 		if (index == IHistogram.UNDERFLOW) return xunder;
104 		if (index == IHistogram.OVERFLOW) return xover;
105 		throw new IllegalArgumentException("bin="+index);
106 	}
107 }
108