View Javadoc

1   package org.lcsim.recon.tracking.trfbase;
2   import Jama.Matrix;
3   /** Class HitError contains the corresponding (symmetric) error matrix.
4    *
5    *@author Norman A. Graf
6    *@version 1.0
7    */
8   
9   
10  public class HitError
11  {
12      private Matrix _mat;
13      private int _size;
14      
15      // constructor from implementation
16      private HitError(  Matrix err )
17      {
18          if(err.getRowDimension() != err.getColumnDimension() )
19              throw new IllegalArgumentException("Error Matrix must be symmetric!");
20          _size = err.getRowDimension();
21          _mat = (Matrix) err.clone();
22      }
23      
24      
25      // default constructor
26      public HitError()
27      {
28          _size = 0;
29          _mat = new Matrix(_size, _size);
30      }
31      // constructor
32      /**
33       * @param size
34       */
35      public HitError( int size )
36      {
37          _size = size;
38          _mat = new Matrix(_size, _size);
39      }
40      
41      // constructor from an array
42      // order is lower triangle (00, 10, 11, 20, ...)
43      // This is a symmetric matrix, so always set both terms
44      /**
45       * @param size
46       * @param arr
47       */
48      public HitError( int size, double[] arr )
49      {
50          _size = size;
51          _mat = new Matrix(_size, _size);
52          int i=0;
53          int j=0;
54          int k=0;
55  //        System.out.println(_size+" "+arr.length);
56          while(k<(_size*_size+_size)/2)
57          {
58              _mat.set(i,j,arr[k]);
59              _mat.set(j,i,arr[k]);
60              k++;
61              for (j = 1; j<=i; ++j)
62              {
63                  _mat.set(i,j,arr[k]);
64                  _mat.set(j,i,arr[k]);
65                  k++;
66              }
67              i++;
68              j=0;
69          }
70      }
71      
72      // constructor for 1D from values
73      /**
74       * @param e11
75       */
76      public HitError( double e11 )
77      {
78          _size = 1;
79          _mat = new Matrix(_size,_size);
80          _mat.set(0,0,e11);
81      }
82      
83      // constructor for 2D from values
84      /**
85       * @param e11
86       * @param e12
87       * @param e22
88       */
89      public HitError( double e11, double e12, double e22 )
90      {
91          _size = 2;
92          _mat = new Matrix(_size,_size);
93          _mat.set(0,0,e11);
94          _mat.set(1,0,e12);
95          _mat.set(0,1,e12);
96          _mat.set(1,1,e22);
97      }
98      
99      // constructor for 3D from values
100     /**
101      * @param e11
102      * @param e21
103      * @param e22
104      * @param e31
105      * @param e32
106      * @param e33
107      */
108     public HitError( double e11, double e21, double e22,
109             double e31, double e32, double e33 )
110     {
111         _size = 3;
112         _mat = new Matrix(_size,_size);
113         _mat.set(0,0,e11);
114         _mat.set(1,0,e21);
115         _mat.set(1,1,e22);
116         _mat.set(2,0,e31);
117         _mat.set(2,1,e32);
118         _mat.set(2,2,e33);
119         // now symmetric terms...
120         _mat.set(0,1,e21);
121         _mat.set(0,2,e31);
122         _mat.set(1,2,e32);
123     }
124     
125     // constructor from a track error and a track derivative:
126     // E_hit = dhit_dtrack * E_track * dhit_dtrack_transpose
127     
128     public HitError( HitDerivative dhit_dtrack,
129             TrackError trkerr )
130     {
131         _size = dhit_dtrack.size();
132         _mat = dhit_dtrack.matrix().times(trkerr.getMatrix().times(dhit_dtrack.matrix().transpose()));
133     }
134     
135     // copy constructor
136     public HitError( HitError herr )
137     {
138         _size = herr.size();
139         _mat = herr.matrix();
140     }
141     
142     // assignment
143     // left side must be unassigned or have the same length
144     //  HitError& operator=( const HitError& herr ) {
145     //    assert( size() == 0 || size() == herr.size() );
146     //    _err = herr._err;
147     //    return *this;
148     //  }
149     
150     // Return the underlying matrix.
151     public  Matrix matrix()
152     {
153         return _mat.copy();
154     }
155     
156     // return the dimension of the matrix
157     public int size()
158     {
159         return _size;
160     }
161     
162     // accessor
163     public double get( int i, int j )
164     {
165         return _mat.get(i,j);
166     }
167     
168     // set
169     // This is a symmetric matrix, so always set the
170     // other element as well.
171     public void set(int i, int j, double val)
172     {
173         _mat.set(i, j, val);
174         _mat.set(j, i, val);
175     }
176     
177     // minimum
178     public double min( )
179     { return _mat.min(); }
180     
181     // maximum
182     public double max( )
183     { return _mat.max(); }
184     
185     // absolute minimum
186     public double amin( )
187     { return _mat.amin(); }
188     
189     // absolute maximum
190     public double amax( )
191     { return _mat.amax(); }
192     
193     // invert -- return 0 for success
194     public int invert()
195     {
196         // Count on Matrix class to throw an exception for no inverse
197         // need to check
198         _mat.inverse();
199         return 0;
200     }
201     
202     // +=
203     public HitError plusEquals( HitError herr)
204     {
205         if(_size != herr.size()) throw new IllegalArgumentException("HitVectors have different dimensions!");
206         
207         return new HitError( _mat.plusEquals(herr.matrix()) );
208     }
209     
210     
211     // -=
212     public HitError minusEquals( HitError herr)
213     {
214         if(_size != herr.size()) throw new IllegalArgumentException("HitVectors have different dimensions!");
215         
216         return new HitError( _mat.minusEquals(herr.matrix()) );
217     }
218     
219     // error + error
220     public HitError
221             plus( HitError he)
222     {
223         return new HitError( _mat.plus(he.matrix()) );
224     }
225     
226     // error - error
227     public HitError
228             minus( HitError he)
229     {
230         return new HitError( _mat.minus(he.matrix())  );
231     }
232     
233     // equality
234     // must have the same dimension and the same values
235     public boolean equals( HitError he)
236     {
237         if ( _size != he.size() ) return false;
238         Matrix tmp = he.matrix();
239         for(int i =0; i<_size; ++i)
240         {
241             for (int j=0; j<_size; ++j)
242             {
243                 if(_mat.get(i,j)!=tmp.get(i,j)) return false;
244             }
245         }
246         return true;
247     }
248     
249     // inequality
250     public boolean notEquals( HitError he)
251     {
252         return ! equals(he);
253     }
254     
255     //  // equality with tolerance
256     //  // must have the same dimension and the same values
257     //  friend bool is_equal( HitError& lhs,  HitError& rhs) {
258     //    if ( lhs.size() != rhs.size() ) return false;
259     //    return is_equal( lhs._err, rhs._err );
260     //  }
261     
262     // output stream
263     public String toString()
264     {
265         String className = getClass().getName();
266         int lastDot = className.lastIndexOf('.');
267         if(lastDot!=-1)className = className.substring(lastDot+1);
268         
269         return className+"\n"+_mat;
270     }
271 }