00001 package SctData;
00002
00003 import hep.aida.ref.histogram.VariableAxis;
00004 import hep.aida.IAxis;
00005 import java.util.*;
00006 import Sct.*;
00007
00014 public class ScanPoints implements Streamable {
00016 public ScanPoints(float[] points, int[] nEvents, int[] nErrorEvents) throws IllegalArgumentException{
00017 init(points, nEvents, nErrorEvents);
00018 }
00019
00020 public ScanPoints(ScanPoints pts) {
00021 for (int i=0; i<pts.points.size(); ++i) {
00022
00023 }
00024 }
00025
00026 public int getNPoints() {
00027 return points.size();
00028 }
00029
00030 public double getPoint(int i) {
00031 return ((ScanPoint)points.get(i)).point;
00032 }
00033
00034 public int getNEvents(int i) {
00035 return ((ScanPoint)points.get(i)).nEvents;
00036 }
00037
00038 public int getNErrorEvents(int i) {
00039 return ((ScanPoint)points.get(i)).nErrorEvents;
00040 }
00041
00042 public void addPoint(double point, int nEvents, int nErrorEvents) {
00043 ScanPoint p = new ScanPoint(point, nEvents, nErrorEvents);
00044 points.add(p);
00045 }
00046
00047 public boolean ascending(){
00048 if (getNPoints()<2) return true;
00049 boolean up=false, down=false;
00050 double lastPoint=getPoint(0);
00051 for (int ipoint=1; ipoint<getNPoints(); ++ipoint){
00052 double point=getPoint(ipoint);
00053 if (point<lastPoint) down=true;
00054 if (point>lastPoint) up=true;
00055 if (point==lastPoint || (up && down)) throw new IllegalStateException("ScanPoints not in ascending or descending order, or has repeated points");
00056 lastPoint=point;
00057 }
00058 return up;
00059 }
00060
00062 public IAxis getAxis() {
00063 if (getNPoints() < 2) throw new IllegalStateException("Must have at least 2 points");
00064 double[] edges = new double[getNPoints() + 1];
00065
00066 if ( ascending() ){
00067 edges[0] = 3./2. * getPoint(0) - 0.5 * getPoint(1);
00068 for (int i=1; i<getNPoints(); ++i) {
00069 edges[i] = 0.5*(getPoint(i-1) + getPoint(i));
00070 }
00071 edges[getNPoints()] = 3./2. * getPoint(getNPoints()-1) - 0.5*getPoint(getNPoints()-2);
00072 } else{
00073 edges[0] = 3./2. * getPoint(getNPoints()-1) - 0.5 * getPoint(getNPoints()-2);
00074 for (int i=1; i<getNPoints(); ++i) {
00075 edges[i] = 0.5*(getPoint( getNPoints()-i-1 ) + getPoint( getNPoints()-i));
00076 }
00077 edges[getNPoints()] = 3./2. * getPoint(0) - 0.5*getPoint(1);
00078 }
00079
00080 return new VariableAxis(edges);
00081 }
00082
00083 public String getClassName() {
00084 return "SctData.ScanPoints";
00085 }
00086
00087 public static ScanPoints read(IStream s, ObjectManager o) throws java.io.IOException {
00088 short nPoints = s.readShort("NPoints");
00089 float[] pts = s.readFloatArray("Points");
00090 int[] nEvents = s.readIntArray("nEvents");
00091 int[] nErrorEvents = s.readIntArray("nErrorEvents");
00092
00093 if (nPoints != pts.length) throw new java.io.StreamCorruptedException("Arrays read in were not expected length\nExpected: " + nPoints + " Got: " + pts.length);
00094 try {
00095 return new ScanPoints(pts, nEvents, nErrorEvents);
00096 } catch (IllegalArgumentException e) {
00097 throw new java.io.StreamCorruptedException("Arrays read in were different lengths");
00098 }
00099 }
00100
00101 public void write(OStream s, ObjectManager o) throws java.io.IOException {
00102 short nPoints = (short)getNPoints();
00103 s.writeShort("NPoints", nPoints, false);
00104
00105
00106 float[] pts = new float[nPoints];
00107 int[] nEvents = new int[nPoints];
00108 int[] nErrorEvents = new int[nPoints];
00109 for (int i=0; i<nPoints; ++i) {
00110 pts[i] = (float)getPoint(i);
00111 nEvents[i] = getNEvents(i);
00112 nErrorEvents[i] = getNErrorEvents(i);
00113 }
00114
00115 s.writeFloatArray("Points", pts);
00116 s.writeIntArray("nEvents", nEvents, false);
00117 s.writeIntArray("nErrorEvents", nErrorEvents, false);
00118 }
00119
00120
00121 private void init(float[] points, int[] nEvents, int[] nErrorEvents) throws IllegalArgumentException{
00122 if (points.length != nEvents.length || points.length != nErrorEvents.length)
00123 throw new IllegalArgumentException("Arrays must be the same length!!");
00124
00125 for (int i=0; i<points.length; ++i) {
00126 addPoint(points[i], nEvents[i], nErrorEvents[i]);
00127 }
00128 }
00129
00130
00131 private ArrayList points = new ArrayList();
00132
00133 private class ScanPoint {
00134 public ScanPoint(double point, int nEv, int nErr) {
00135 this.point = point;
00136 this.nEvents = nEv;
00137 this.nErrorEvents = nErr;
00138 }
00139
00140 public double point;
00141 public int nEvents;
00142 public int nErrorEvents;
00143 }
00144 }