View Javadoc

1   package org.lcsim.recon.tracking.trfbase;
2   import Jama.Matrix;
3   
4   /** Class HitVector contains the parameters describing a hit.
5    *
6    * @author Norman A. Graf
7    * @version 1.0
8    */
9   
10  public class HitVector
11  {
12      
13      private Matrix _vec;
14      private int _length;
15      
16      //
17      
18      /**
19       *constructor from Matrix implementation
20       *
21       * @param   Matrix vec containing the hit values
22       */
23      private HitVector(  Matrix vec )
24      {
25          _length = vec.getRowDimension();
26          _vec = vec.copy();
27      }
28      
29      
30      
31      //
32      
33      /**
34       *default constructor
35       * leaves HitVector in invalid state
36       */
37      public HitVector()
38      {
39          _length = 0;
40          _vec = new Matrix(_length, _length);
41      }
42      
43      //
44      
45      /**
46       *constructor initializing size of hit
47       *
48       * @param   size  dimensionality of the hit
49       */
50      public HitVector( int size )
51      {
52          _length = size;
53          _vec = new Matrix(_length, 1);
54      }
55      
56      //
57      
58      /**
59       *constructor from an array
60       *
61       * @param   size of the array of values
62       * @param   vec  double[] containing the values
63       */
64      public HitVector( int size, double[] vec )
65      {
66          _length = size;
67          //needed until I fix JAMA
68          double[] tmp = new double[_length];
69          System.arraycopy(vec, 0, tmp, 0, _length);
70          _vec = new Matrix(tmp, _length);
71      }
72      
73      //
74      
75      /**
76       *constructor from one value
77       *
78       * @param   x1  single HitVector value
79       */
80      public HitVector( double x1 )
81      {
82          _length = 1;
83          _vec = new Matrix(_length,1);
84          _vec.set(0,0,x1);
85      }
86      
87      //
88      
89      /**
90       *constructor from two values
91       *
92       * @param   x1  first value
93       * @param   x2  second value
94       */
95      public HitVector( double x1, double x2 )
96      {
97          _length = 2;
98          _vec = new Matrix(_length,1);
99          _vec.set(0,0,x1);
100         _vec.set(1,0,x2);
101     }
102     
103     //
104     
105     /**
106      *constructor from three values
107      *
108      * @param   x1  first value
109      * @param   x2  second value
110      * @param   x3  third value
111      */
112     public HitVector( double x1, double x2, double x3 )
113     {
114         _length = 3;
115         _vec = new Matrix(_length,1);
116         _vec.set(0,0,x1);
117         _vec.set(1,0,x2);
118         _vec.set(2,0,x3);
119     }
120     
121     //
122     
123     /**
124      *copy constructor
125      *
126      * @param  hvec HitVector to replicate
127      */
128     public HitVector( HitVector hvec )
129     {
130         _length = hvec.vector().length;
131         _vec = new Matrix( hvec.vector(), _length);
132     }
133     
134     // doesn't work in Java
135     // assignment
136     // left side must be unassigned or have the same length
137     //  HitVector& operator=( const HitVector& hvec ) {
138     //    assert( size() == 0 || size() == hvec.size() );
139     //    _vec = hvec._vec;
140     //    return *this;
141     //  }
142     
143     //
144     
145     /**
146      *access the hit vector
147      *
148      * @return the underlying hit vector.
149      */
150     public  double[] vector()
151     {
152         return _vec.getColumnPackedCopy();
153     }
154     
155     //
156     
157     /**
158      * access the hit vector
159      *
160      * @return  the underlying hit vector as a Matrix.
161      */
162     public  Matrix matrix()
163     {
164         return _vec.copy();
165     }
166     
167     
168     //
169     
170     /**
171      * hit size
172      *
173      * @return the dimension of the hit
174      */
175     public int size()
176     {
177         return _length;
178     }
179     
180     //
181     
182     /**
183      * accessor
184      *
185      * @param   i  element to access
186      * @return  value of vector element i
187      */
188     public double get( int i )
189     {
190         return _vec.get(i,0);
191     }
192     
193     //
194     
195     /**
196      *  set vector element i to value val
197      *
198      * @param   i  element to set
199      * @param   val  value of element i
200      */
201     public void set(int i, double val)
202     {
203         _vec.set(i, 0, val);
204     }
205     
206     
207     //
208     
209     /**
210      *minimum
211      *
212      * @return minimum value of vector elements
213      */
214     public double min( )
215     { return _vec.min(); }
216     
217     //
218     
219     /**
220      *maximum
221      *
222      * @return maximum value of vector elements
223      */
224     public double max( )
225     { return _vec.max(); }
226     
227     //
228     
229     /**
230      * absolute minimum
231      *
232      * @return absolute minimum value of vector elements
233      */
234     public double amin( )
235     { return _vec.amin(); }
236     
237     
238     
239     /**
240      *absolute maximum
241      *
242      * @return     absolute maximum value of vector elements
243      */
244     public double amax( )
245     { return _vec.amax(); }
246     
247     // +=
248     
249     /** vector addition in place
250      * changes value of this to this + hv
251      * @param hv HitVector to add
252      */
253     public void plusEquals( HitVector hv)
254     {
255         if(_length != hv.size()) throw new IllegalArgumentException("HitVectors have different dimensions!");
256         
257         _vec.plusEquals(hv.matrix());
258     }
259     
260     
261     /** vector subtraction in place
262      * changes value of this to this - hv
263      * @param hv HitVector to subtract
264      */
265     
266     public void minusEquals( HitVector hv)
267     {
268         if(_length != hv.size()) throw new IllegalArgumentException("HitVectors have different dimensions!");
269         
270         _vec.minusEquals(hv.matrix());
271     }
272     
273     
274     /** vector addition
275      *
276      * @return new HitVector equal to this + hv
277      * @param hv HitVector to add
278      */
279     public  HitVector plus(HitVector hv)
280     {
281         double[] tmp = new double[_length];
282         for(int i = 0; i<_length; ++i)
283         {
284             tmp[i] = _vec.get(i,0) + hv.get(i);
285         }
286         return new HitVector(_length, tmp);
287     }
288     
289     
290     
291     /** vector subtraction
292      *
293      * @return return new HitVector equal to this - hv
294      * @param hv HitVector to subtract
295      */
296     public  HitVector minus(HitVector hv)
297     {
298         double[] tmp = new double[_length];
299         for(int i = 0; i<_length; ++i)
300         {
301             tmp[i] = _vec.get(i,0) - hv.get(i);
302         }
303         return new HitVector(_length, tmp);
304     }
305     
306     
307     /** equality
308      * must have the same length and the same values
309      *
310      * @return true if HitVectors this and hv are <bf>not</bf> equal
311      * @param hv HitVector to compare
312      */
313     public  boolean equals( HitVector hv)
314     {
315         if(_length != hv.size()) return false;
316         for(int i = 0; i<_length; ++i)
317         {
318             if (_vec.get(i,0)!=hv.get(i)) return false;
319         }
320         return true;
321     }
322     
323     
324     
325     /** inequality
326      *
327      * @return true if HitVectors this and hv are <bf>not</bf> equal
328      * @param hv HitVector to compare
329      */
330     public  boolean notEquals( HitVector hv)
331     {
332         return ! equals(hv);
333     }
334     
335     
336     // equality with tolerance
337     // must have the same length and the same values
338     //  public  boolean is_equal(HitVector hv)
339     //  {
340     //    // for now just fake it.
341     //    return equals(hv);
342     //  }
343     
344     
345     /**
346      *output stream
347      *
348      * @return String representation of HitVector
349      */
350     public String toString()
351     {
352         String className = getClass().getName();
353         int lastDot = className.lastIndexOf('.');
354         if(lastDot!=-1)className = className.substring(lastDot+1);
355         
356         return className+" \n"+_vec;
357     }
358 }
359 
360 
361 
362