View Javadoc

1   package org.lcsim.recon.tracking.trfbase;
2   
3   import org.lcsim.recon.tracking.trfutil.TRFMath;
4   import Jama.Matrix;
5   
6   /**
7    * Class TrackVector is a 5-vector containing the parameters
8    * describing a track in a magnetic field.
9    *<p>
10   * TrackVector tv should support the following operations:
11   * <pre>
12   * construct: tv(), te(), td() (should fill with zeros)
13   * copy: tv2(tv1), te2(te1), td2(td1)
14   * assignment: tv2 = tv2, te2 = te1, td2 = td1
15   * indexing: tv(i), te(i,j), td(i,j)
16   * minimum element: tv.min(), te.min(), td.min()
17   * maximum element: tv.max(), te.max(), td.max()
18   * absolute minimum element: tv.amin(), te.amin(), td.amin()
19   * absolute maximum element: tv.amax(), te.amax(), td.amax()
20   * addition: tv2 += tv1, tv3 = tv1 + tv2,
21   *           te2 += te1, te3 = tv1 + te2,
22   *           td2 += td1, td3 = td1 + td2,
23   * subtraction: tv2 -= tv1, tv3 = tv1 - tv2,
24   *              te2 -= te1, te3 = tv1 - te2,
25   *              td2 -= td1, td3 = td1 - td2,
26   * inversion: int stat = invert( TrackError& te1 ) (0 for success)
27   * output stream: cout << tv << te << td
28   * equality: tv1==tv2, te1==te2, td1==td2
29   * inequality: tv!=tv2, te1!=te2, td1!=td2
30   * chisquare difference: chisq_diff(tv,te) = tv_T * te * tv
31   * (tv is the difference between two vectors and te is the inverse
32   * of the error matrix.)
33   * transpose: td.transpose()
34   * transform: te.xform(td)
35   * </pre>
36   *
37   * @author Norman A. Graf
38   * @version 1.0
39   */
40  public class TrackVector
41  {
42      private Matrix _vec;
43      private int _length;
44      //
45      
46      /**
47       * Default constructor.
48       * Builds vector of 5 zeros.
49       *
50       */
51      public TrackVector( )
52      {
53          _length = 5;
54          _vec = new Matrix(_length,1);
55      }
56      
57      //
58      
59      /**
60       * Constructor from array.
61       *
62       * @param   vec  5 dimensional vector of doubles.
63       */
64      public TrackVector( double[] vec)
65      {
66          _length = vec.length;
67          if(_length!=5.) throw new IllegalArgumentException();
68          _vec = new Matrix(vec, _length);
69      }
70      
71      //
72      
73      /**
74       * Constructor from Jama Matrix.
75       *
76       * @param   mat 5 dimensional Jama Column Matrix of doubles.
77       */
78      public TrackVector( Matrix mat)
79      {
80          if(mat.getRowDimension()!=5 && mat.getColumnDimension() != 1)
81          {
82              throw new IllegalArgumentException("TrackVector Matrix must be 5x1.");
83          }
84          _length = mat.getRowDimension();
85          _vec = new Matrix(mat.getArrayCopy());
86      }
87      
88      //
89      
90      /**
91       * Copy Constructor
92       *
93       * @param   tv  TrackVector to copy.
94       */
95      public TrackVector( TrackVector tv)
96      {
97          _length = tv.vector().length;
98          _vec  = new Matrix(tv.vector(),_length);
99      }
100     
101     //
102     
103     /**
104      * Return the underlying vector.
105      *
106      * @return  a 5 dimensional array of doubles.
107      */
108     public double[] vector()
109     {
110         return _vec.getColumnPackedCopy();
111     }
112     
113     
114     /**
115      * Math funtion minus.
116      *
117      * @param   tv TrackVector to be subtracted.
118      * @return  <em>this</em> minus TrackVector.
119      */
120     public TrackVector minus( TrackVector tv)
121     {
122         double[] tmp = new double[5];
123         for(int i = 0; i<5; ++i)
124         {
125             tmp[i] = _vec.get(i,0)-tv.get(i);
126         }
127         return new TrackVector(tmp);
128     }
129     
130     
131     /**
132      * Math funtion plus.
133      *
134      * @param   tv  TrackVector to be subtracted.
135      * @return   <em>this</em> pluus TrackVector.
136      */
137     public TrackVector plus( TrackVector tv)
138     {
139         double[] tmp = new double[5];
140         for(int i = 0; i<5; ++i)
141         {
142             tmp[i] = _vec.get(i,0)+tv.get(i);
143         }
144         return new TrackVector(tmp);
145     }
146     
147     
148     /**
149      * String representation of this TrackVector.
150      *
151      * @return     String representation of this TrackVector.
152      */
153     public String toString()
154     {
155         String className = getClass().getName();
156         int lastDot = className.lastIndexOf('.');
157         if(lastDot!=-1)className = className.substring(lastDot+1);
158         
159         StringBuffer sb = new StringBuffer(className+" ");
160         for(int i=0; i<_length; ++i)
161         {
162             sb.append(_vec.get(i,0)).append(" ");
163         }
164         return sb.append("\n").toString();
165     }
166     
167     /**
168      * Return the underlying track vector as a Jama Matrix.
169      *
170      * @return     The track vector as a 5 dimensional Jama Column Matrix.
171      */
172     public Matrix getMatrix()
173     {
174         return _vec.copy();
175     }
176     
177     /**
178      * Return the underlying track vector as a Jama Matrix.
179      *
180      * @return The track vector as a 5 dimensional Jama Column Matrix.
181      */
182     public Matrix matrix()
183     {
184         return _vec.copy();
185     }
186     
187     /**
188      * Set a track vector element.
189      *
190      * @param   i  The track element to set.
191      * @param   val  The value to be assigned to track element i.
192      */
193     public void set(int i, double val)
194     {
195         _vec.set(i, 0, val);
196     }
197     
198     /**
199      * Get a track vector element.
200      *
201      * @param   i  The track element to get.
202      * @return   The value of track element i.
203      */
204     public double get(int i)
205     {
206         return _vec.get(i,0);
207     }
208     
209     
210     /**
211      * Equality operator.
212      *
213      * @param   tv TrackVector to check against.
214      * @return  true if <em> this</em> equals TrackVector tv.
215      */
216     public boolean equals( TrackVector tv)
217     {
218         for(int i = 0; i<5; ++i)
219         {
220             if( get(i)!=tv.get(i) ) return false;
221         }
222         return true;
223     }
224     
225     
226     /**
227      * Inequality operator. Convenience method.
228      *
229      * @param   tv  TrackVector to check against.
230      * @return   true if <em> this</em> <b> does not </b> equal TrackVector tv.
231      */
232     public boolean notEquals(TrackVector tv)
233     {
234         return !equals(tv);
235     }
236     
237     
238     /**
239      * Equality within tolerance.
240      *
241      * @param   tv  TrackVector to check against.
242      * @return  true if <em> this</em> equals TrackVector tv within tolerance.
243      */
244     public boolean isEqual( TrackVector tv)
245     {
246         for(int i = 0; i<5; ++i)
247         {
248             if( !TRFMath.isEqual(get(i),tv.get(i)) ) return false;
249         }
250         return true;
251     }
252     
253     
254     /**
255      * Absolute Maximum TrackVector element.
256      *
257      * @return absolute value of absolute maximum TrackVector element.
258      */
259     public double amax()
260     {
261         double amax = Math.abs(get(0));
262         for (int i = 1; i<5; ++i)
263         {
264             if(Math.abs(get(i))>amax) amax = Math.abs(get(i));
265         }
266         return amax;
267     }
268     
269     
270     /**
271      * Maximum TrackVector element.
272      *
273      * @return  value of maximum TrackVector element.
274      */
275     public double max()
276     {
277         double max = get(0);
278         for (int i = 1; i<5; ++i)
279         {
280             if(get(i)>max) max = get(i);
281         }
282         return max;
283     }
284     
285     
286     /**
287      *Absolute Minimum TrackVector element.
288      *
289      * @return absolute value of absolute minimum TrackVector element.
290      */
291     public double amin()
292     {
293         double amin = Math.abs(get(0));
294         for (int i = 1; i<5; ++i)
295         {
296             if(Math.abs(get(i))<amin) amin = Math.abs(get(i));
297         }
298         return amin;
299     }
300     
301     
302     /**
303      * Mimimum TrackVector element.
304      *
305      * @return  value of minimum TrackVector element.
306      */
307     public double min()
308     {
309         double min = get(0);
310         for (int i = 1; i<5; ++i)
311         {
312             if(get(i)<min) min = get(i);
313         }
314         return min;
315     }
316     
317     
318     /**
319      * Chi-squared difference between TrackVectors.
320      *
321      * @param   tv  TrackVector to compare to.
322      * @param   te  TrackError of uncertainties.
323      * @return     double chi-squared.
324      */
325     public static double chisqDiff(TrackVector tv, TrackError te)
326     {
327         // cng needs to be checked...
328         return (tv._vec.transpose()).times(te.getMatrix()).times(tv._vec).get(0,0);
329     }
330     
331 }