View Javadoc

1   package org.lcsim.recon.tracking.trfbase;
2   import Jama.Matrix;
3   /**  Class HitDerivative is the derivative with respect to a track
4    * vector, i.e. is an nx5 matrix where n = hit dimension.
5    *
6    *@author Norman A. Graf
7    *@version 1.0
8    */
9   
10  public class HitDerivative
11  {
12      private Matrix _mat;
13      private int _size;
14      
15      /** constructor from implementation
16       *
17       *@param mtx explicit Matrix representation of the HitDerivative
18       */
19      private HitDerivative( Matrix mtx )
20      {
21          _size = mtx.getRowDimension();
22          _mat = (Matrix) mtx.clone();
23      }
24      
25      /** Default constructor
26       *
27       */
28      public HitDerivative()
29      {
30          _size = 0;
31          _mat = new Matrix(_size, 5);
32      }
33      
34      //
35      /** Constructor
36       * @param size  Size of the Hit measurement
37       */
38      public HitDerivative( int size)
39      {
40          _size = size;
41          _mat = new Matrix(_size, 5);
42      }
43      
44      //
45      // order is by completed rows (00, 01, 02, ..., 10, 12, ...)
46      /** constructor from an array
47       * @param size Size of the Hit measurement
48       * @param arr array of hit derivatives, by completed rows (00, 01, 02, ..., 10, 12, ...)
49       */
50      public HitDerivative( int size, double[] arr )
51      {
52          
53          _size = size;
54          _mat = new Matrix(_size, 5);
55          int i=0;
56          int j=0;
57          int k=0;
58          while(k<_size*5)
59          {
60              for (j = 0; j<5; ++j)
61              {
62                  _mat.set(i,j,arr[k++]);
63              }
64              i++;
65              j=0;
66          }
67      }
68      
69      
70      //
71      /** copy constructor
72       * @param hder HitDerivative to replicate
73       */
74      public HitDerivative( HitDerivative hder )
75      {
76          _size = hder.size();
77          _mat = hder.matrix();
78      }
79      
80      // assignment
81      // left side must be unassigned or have the same length
82      //  HitDerivative& operator=( const HitDerivative& hder ) {
83      //    assert( size() == 0 || size() == hder.size() );
84      //    _mtx = hder._mtx;
85      //    return *this;
86      //  }
87      
88      //
89      /** Return the underlying matrix.
90       * @return Matrix representation of the derivative
91       */
92      public Matrix matrix()
93      {
94          return _mat.copy();
95      }
96      
97      //
98      /** return the # rows in the matrix
99       * @return the size of the hit measurement
100      */
101     public int size()
102     {
103         return _size;
104     }
105     
106     //
107     /** access a derivatie element
108      * @param i first index
109      * @param j second index
110      * @return the (i, j)th derivative
111      */
112     public double get( int i, int j )
113     {
114         return _mat.get(i,j);
115     }
116     
117     //
118     /** set a dervative element
119      * @param i first index
120      * @param j second index
121      * @param val value of (i,j)th element to set
122      */
123     public void set(int i, int j, double val)
124     {
125         _mat.set(i, j, val);
126     }
127     
128     
129     //
130     /** minimum derivative element
131      * @return the minimum derivative element
132      */
133     public double min( )
134     { return _mat.min(); };
135     
136     //
137     /** maximum  derivative element
138      * @return the maximum derivative element
139      */
140     public double max( )
141     { return _mat.max(); };
142     
143     //
144     /** absolute minimum
145      * @return the derivative absolute value minimum
146      */
147     public double amin( )
148     { return _mat.amin(); };
149     
150     //
151     /** absolute maximum
152      * @return the derivative absolute value maximum
153      */
154     public double amax( )
155     { return _mat.amax(); };
156     
157     // +=
158     /** Addition in place, shouldn't this be void?
159      * fix
160      * @param hder HitDerivative to add
161      * @return result of addition
162      */
163     public HitDerivative plusEquals( HitDerivative hder)
164     {
165         if(_size != hder.size()) throw new IllegalArgumentException("HitVectors have different dimensions!");
166         
167         return new HitDerivative( _mat.plusEquals(hder.matrix()) );
168     }
169     
170     // -=
171     /** subtraction
172      * @param hder HitDerivative to subtract
173      * @return result of subtraction
174      */
175     public HitDerivative minusEquals( HitDerivative hder)
176     {
177         if(_size != hder.size()) throw new IllegalArgumentException("HitVectors have different dimensions!");
178         
179         return new HitDerivative( _mat.minusEquals(hder.matrix()) );
180     }
181     
182     // derivative + derivative
183     /** Addition
184      * @param hd HitDerivative to add
185      * @return result of addition
186      */
187     public HitDerivative plus( HitDerivative hd)
188     {
189         return new HitDerivative( _mat.plus(hd.matrix()) );
190     }
191     
192     // derivative - derivative
193     /** Subtraction
194      * @param hd HitDerivative to subtract
195      * @return result of subtraction
196      */
197     public  HitDerivative minus(HitDerivative hd)
198     {
199         return new HitDerivative( _mat.minus(hd.matrix()) );
200     }
201     
202     //
203     // must have the same dimension and the same values
204     /** equality
205      * @param hd HitDerivative to compare
206      * @return true if HitDerivatives are of the same dimension and the same values
207      */
208     public boolean equals( HitDerivative hd )
209     {
210         if ( _size != hd.size() ) return false;
211         Matrix tmp = hd.matrix();
212         for(int i =0; i<_size; ++i)
213         {
214             for (int j=0; j<5; ++j)
215             {
216                 if(_mat.get(i,j)!=tmp.get(i,j)) return false;
217             }
218         }
219         return true;
220     }
221     //
222     /** inequality
223      * @param hd HitDerivative to compare
224      * @return rue if HitDerivatives are not equal
225      */
226     public boolean notEquals( HitDerivative hd)
227     {
228         return ! equals(hd);
229     }
230     
231     // equality with tolerance
232     //  friend bool
233     //  is_equal( HitDerivative& lhs,  HitDerivative& rhs) {
234     //    if ( lhs.size() != rhs.size() ) return false;
235     //    return is_equal( lhs._mat, rhs._mat );
236     //  }
237     
238     //
239     /** output stream
240      * @return String representation of this class
241      */
242     public String toString()
243     {
244         String className = getClass().getName();
245         int lastDot = className.lastIndexOf('.');
246         if(lastDot!=-1)className = className.substring(lastDot+1);
247         
248         return className+"\n"+_mat.toString();
249     }
250     
251 }