LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
KHit.h
Go to the documentation of this file.
1 
64 #ifndef KHIT_H
65 #define KHIT_H
66 
68 #include "cetlib_except/exception.h"
69 
70 namespace trkf {
71 
72  template <int N>
73  class KHit : public KHitBase
74  {
75  public:
76 
78  KHit();
79 
81  KHit(const std::shared_ptr<const Surface>& psurf);
82 
84  KHit(const std::shared_ptr<const Surface>& psurf,
85  const typename KVector<N>::type& mvec,
86  const typename KSymMatrix<N>::type& merr);
87 
89  virtual ~KHit();
90 
91  // Modifiers.
92 
94  void setMeasVector(const typename KVector<N>::type& mvec) {fMvec = mvec;}
95 
97  void setMeasError(const typename KSymMatrix<N>::type& merr) {fMerr = merr;}
98 
99  // Accessors.
100 
102  const typename KVector<N>::type& getMeasVector() const {return fMvec;}
103 
105  const typename KSymMatrix<N>::type& getMeasError() const {return fMerr;}
106 
108  const typename KVector<N>::type& getPredVector() const {return fPvec;}
109 
111  const typename KSymMatrix<N>::type& getPredError() const {return fPerr;}
112 
114  const typename KVector<N>::type& getResVector() const {return fRvec;}
115 
117  const typename KSymMatrix<N>::type& getResError() const {return fRerr;}
118 
120  const typename KSymMatrix<N>::type& getResInvError() const {return fRinv;}
121 
123  const typename KHMatrix<N>::type& getH() const {return fH;}
124 
126  double getChisq() const {return fChisq;}
127 
128  // Overrides.
129  // Implementation of overrides is found at the bottom of this header.
130 
132  bool predict(const KETrack& tre, const Propagator* prop = 0, const KTrack* ref = 0) const;
133 
135  void update(KETrack& tre) const;
136 
137  // Pure virtual methods.
138 
140  virtual bool subpredict(const KETrack& tre,
141  typename KVector<N>::type& pvec,
142  typename KSymMatrix<N>::type& perr,
143  typename KHMatrix<N>::type& hmatrix) const = 0;
144 
146  virtual std::ostream& Print(std::ostream& out, bool doTitle = true) const;
147 
148  private:
149 
150  // Attributes.
151 
154  mutable typename KVector<N>::type fPvec;
155  mutable typename KSymMatrix<N>::type fPerr;
156  mutable typename KVector<N>::type fRvec;
157  mutable typename KSymMatrix<N>::type fRerr;
158  mutable typename KSymMatrix<N>::type fRinv;
159  mutable typename KHMatrix<N>::type fH;
160  mutable double fChisq;
161  };
162 
163  // Method implementations.
164 
166  template <int N>
168  fChisq(0.)
169  {}
170 
177  template<int N>
178  KHit<N>::KHit(const std::shared_ptr<const Surface>& psurf) :
179  KHitBase(psurf),
180  fChisq(0.)
181  {}
182 
191  template<int N>
192  KHit<N>::KHit(const std::shared_ptr<const Surface>& psurf,
193  const typename KVector<N>::type& mvec,
194  const typename KSymMatrix<N>::type& merr) :
195  KHitBase(psurf),
196  fMvec(mvec),
197  fMerr(merr),
198  fChisq(0.)
199  {}
200 
202  template <int N>
204  {}
205 
214  template <int N>
215  bool KHit<N>::predict(const KETrack& tre, const Propagator* prop, const KTrack* ref) const
216  {
217  // Update the prediction surface to be the track surface.
218 
219  fPredSurf = tre.getSurface();
220  fPredDist = 0.;
221 
222  // Default result.
223 
224  bool ok = false;
225 
226  // Update prediction vector, error matrox, and H-matrix.
227 
228  // First test whether the prediction surface matches the
229  // measurement surface.
230 
231  if(getMeasSurface()->isEqual(*fPredSurf)) {
232 
233  // Prediction and measurement surfaces agree.
234  //Just call subpredict method (don't do propagation).
235 
236  ok = subpredict(tre, fPvec, fPerr, fH);
237  }
238  else {
239 
240  // Track surface does not match the prediction surface, so an
241  // internal propagation will be required. Throw an exception if
242  // no propagator was provided.
243 
244  if(prop == 0)
245  throw cet::exception("KHit") << "Track surface does not match measurement surface and no propagator.\n";
246 
247  // First make a copy of the original KETrack.
248 
249  KETrack treprop(tre);
250 
251  // Also make a copy of the reference track (if specified).
252 
253  KTrack refprop;
254  KTrack* prefprop = 0;
255  if(ref != 0) {
256  refprop = *ref;
257  prefprop = &refprop;
258  }
259 
260 
261  // Make a no-noise, no-dE/dx propagation to the measurement
262  // surface. But do calculate the propagation matrix, which we
263  // will use to update the H-matrix calculated in the derived
264  // class.
265 
266  TrackMatrix prop_matrix;
267  boost::optional<double> dist = prop->err_prop(treprop, getMeasSurface(),
268  Propagator::UNKNOWN, false,
269  prefprop, &prop_matrix);
270  ok = !!dist;
271  if(ok) {
272 
273  // Update prediction distance.
274 
275  fPredDist = *dist;
276 
277  // Now we are ready to calculate the prediction on the
278  // measurement surface.
279 
280  typename KHMatrix<N>::type hmatrix;
281  ok = subpredict(treprop, fPvec, fPerr, hmatrix);
282  if(ok) {
283 
284  // Use the propagation matrix to transform the H-matrix back
285  // to the prediction surface.
286 
287  fH = prod(hmatrix, prop_matrix);
288  }
289  }
290  }
291  if(ok) {
292 
293  // Update residual
294 
295  fRvec = fMvec - fPvec;
296  fRerr = fMerr + fPerr;
297  fRinv = fRerr;
298  ok = syminvert(fRinv);
299  if(ok) {
300 
301  // Calculate incremental chisquare.
302 
303  // (workaround: if we use the copy constructor, gcc emits a spurious warning)
304 // typename KVector<N>::type rtemp = prod(fRinv, fRvec);
305  fChisq = inner_prod(fRvec, prod(fRinv, fRvec));
306  }
307  }
308 
309  // If a problem occured at any step, clear the prediction surface pointer.
310 
311  if(!ok) {
312  fPredSurf.reset();
313  fPredDist = 0.;
314  }
315 
316  // Done.
317 
318  return ok;
319  }
320 
327  template <int N>
328  void KHit<N>::update(KETrack& tre) const
329  {
330  // Make sure that the track surface and the prediction surface are the same.
331  // Throw an exception if they are not.
332 
333  if(!getPredSurface()->isEqual(*tre.getSurface()))
334  throw cet::exception("KHit") << "Track surface not the same as prediction surface.\n";
335 
336  const TrackVector& tvec = tre.getVector();
337  const TrackError& terr = tre.getError();
338  TrackVector::size_type size = tvec.size();
339 
340  // Calculate gain matrix.
341 
342  typename KGMatrix<N>::type temp(size, N);
343  typename KGMatrix<N>::type gain(size, N);
344  temp = prod(trans(fH), fRinv);
345  gain = prod(terr, temp);
346 
347  // Calculate updated track state.
348 
349  TrackVector newvec = tre.getVector() + prod(gain, fRvec);
350 
351  // Calculate updated error matrix.
352 
353  TrackMatrix fact = ublas::identity_matrix<TrackVector::value_type>(size);
354  fact -= prod(gain, fH);
355  TrackMatrix errtemp1 = prod(terr, trans(fact));
356  TrackMatrix errtemp2 = prod(fact, errtemp1);
357  TrackError errtemp2s = ublas::symmetric_adaptor<TrackMatrix>(errtemp2);
358  typename KHMatrix<N>::type errtemp3 = prod(fMerr, trans(gain));
359  TrackMatrix errtemp4 = prod(gain, errtemp3);
360  TrackError errtemp4s = ublas::symmetric_adaptor<TrackMatrix>(errtemp4);
361  TrackError newerr = errtemp2s + errtemp4s;
362 
363  // Update track.
364 
365  tre.setVector(newvec);
366  tre.setError(newerr);
367  }
368 
370  template <int N>
371  std::ostream& KHit<N>::Print(std::ostream& out, bool doTitle) const
372  {
373  if(doTitle)
374  out << "KHit<" << N << ">:\n";
375 
376  // Print base class.
377 
378  KHitBase::Print(out, false);
379 
380  // Print measurement vector.
381 
382  out << " Measurement vector:\n"
383  << " [";
384  for(unsigned int i = 0; i < fMvec.size(); ++i) {
385  if(i != 0)
386  out << ", ";
387  out << fMvec(i);
388  }
389  out << "]\n";
390 
391  // Print diagonal measurement errors.
392 
393  out << " Diagonal measurement errors:\n"
394  << " [";
395  for(unsigned int i = 0; i < fMerr.size1(); ++i) {
396  if(i != 0)
397  out << ", ";
398  double err = fMerr(i,i);
399  err = (err >= 0. ? std::sqrt(err) : -std::sqrt(-err));
400  out << err;
401  }
402  out << "]\n";
403 
404  // Print measurement correlations.
405 
406  if(fMerr.size1() > 1) {
407  out << " Measurement correlation matrix:";
408  for(unsigned int i = 0; i < fMerr.size1(); ++i) {
409  if(i == 0)
410  out << "\n [";
411  else
412  out << "\n ";
413  for(unsigned int j = 0; j <= i; ++j) {
414  if(j != 0)
415  out << ", ";
416  if(i == j)
417  out << 1.;
418  else {
419  double eiijj = fMerr(i,i) * fMerr(j,j);
420  double eij = fMerr(i,j);
421  if(eiijj != 0.)
422  eij /= std::sqrt(std::abs(eiijj));
423  else
424  eij = 0.;
425  out << eij;
426  }
427  }
428  }
429  out << "]\n";
430  }
431 
432  // Print prediction vector.
433 
434  out << " Prediction vector:\n"
435  << " [";
436  for(unsigned int i = 0; i < fPvec.size(); ++i) {
437  if(i != 0)
438  out << ", ";
439  out << fPvec(i);
440  }
441  out << "]\n";
442 
443  // Print diagonal prediction errors.
444 
445  out << " Diagonal prediction errors:\n"
446  << " [";
447  for(unsigned int i = 0; i < fPerr.size1(); ++i) {
448  if(i != 0)
449  out << ", ";
450  double err = fPerr(i,i);
451  err = (err >= 0. ? std::sqrt(err) : -std::sqrt(-err));
452  out << err;
453  }
454  out << "]\n";
455 
456  // Print prediction correlations.
457 
458  if(fPerr.size1() > 1) {
459  out << " Prediction correlation matrix:";
460  for(unsigned int i = 0; i < fPerr.size1(); ++i) {
461  if(i == 0)
462  out << "\n [";
463  else
464  out << "\n ";
465  for(unsigned int j = 0; j <= i; ++j) {
466  if(j != 0)
467  out << ", ";
468  if(i == j)
469  out << 1.;
470  else {
471  double eiijj = fPerr(i,i) * fPerr(j,j);
472  double eij = fPerr(i,j);
473  if(eiijj != 0.)
474  eij /= std::sqrt(std::abs(eiijj));
475  else
476  eij = 0.;
477  out << eij;
478  }
479  }
480  }
481  out << "]\n";
482  }
483 
484  // Print residual vector.
485 
486  out << " Residual vector:\n"
487  << " [";
488  for(unsigned int i = 0; i < fRvec.size(); ++i) {
489  if(i != 0)
490  out << ", ";
491  out << fRvec(i);
492  }
493  out << "]\n";
494 
495  // Print diagonal residual errors.
496 
497  out << " Diagonal residual errors:\n"
498  << " [";
499  for(unsigned int i = 0; i < fRerr.size1(); ++i) {
500  if(i != 0)
501  out << ", ";
502  double err = fRerr(i,i);
503  err = (err >= 0. ? std::sqrt(err) : -std::sqrt(-err));
504  out << err;
505  }
506  out << "]\n";
507 
508  // Print residual correlations.
509 
510  if(fRerr.size1() > 1) {
511  out << " Residual correlation matrix:";
512  for(unsigned int i = 0; i < fRerr.size1(); ++i) {
513  if(i == 0)
514  out << "\n [";
515  else
516  out << "\n ";
517  for(unsigned int j = 0; j <= i; ++j) {
518  if(j != 0)
519  out << ", ";
520  if(i == j)
521  out << 1.;
522  else {
523  double eiijj = fRerr(i,i) * fRerr(j,j);
524  double eij = fRerr(i,j);
525  if(eiijj != 0.)
526  eij /= std::sqrt(std::abs(eiijj));
527  else
528  eij = 0.;
529  out << eij;
530  }
531  }
532  }
533  out << "]\n";
534  }
535 
536  // Print incremental chisquare.
537 
538  out << " Incremental chisquare = " << fChisq << "\n";
539 
540  // Done.
541 
542  return out;
543  }
544 }
545 
546 #endif
ublas::symmetric_matrix< double, ublas::lower, ublas::row_major, ublas::bounded_array< double, N *(N+1)/2 > > type
const TrackError & getError() const
Track error matrix.
Definition: KETrack.h:54
virtual bool subpredict(const KETrack &tre, typename KVector< N >::type &pvec, typename KSymMatrix< N >::type &perr, typename KHMatrix< N >::type &hmatrix) const =0
Calculate prediction function (return via arguments).
const std::shared_ptr< const Surface > & getSurface() const
Surface.
Definition: KTrack.h:55
void update(KETrack &tre) const
Update track method.
Definition: KHit.h:328
KSymMatrix< 5 >::type TrackError
Track error matrix, dimension 5x5.
void setVector(const TrackVector &vec)
Set state vector.
Definition: KTrack.h:67
const KSymMatrix< N >::type & getMeasError() const
Measurement error matrix.
Definition: KHit.h:105
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KHit.h:371
const KHMatrix< N >::type & getH() const
Kalman H-matrix.
Definition: KHit.h:123
const KVector< N >::type & getResVector() const
Residual vector.
Definition: KHit.h:114
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KHitBase.cxx:40
void setError(const TrackError &err)
Set error matrix.
Definition: KETrack.h:60
const KVector< N >::type & getPredVector() const
Prediction vector.
Definition: KHit.h:108
const KSymMatrix< N >::type & getPredError() const
Prediction matrix.
Definition: KHit.h:111
boost::optional< double > err_prop(KETrack &tre, const std::shared_ptr< const Surface > &psurf, PropDirection dir, bool doDedx, KTrack *ref=0, TrackMatrix *prop_matrix=0) const
Propagate with error, but without noise.
Definition: Propagator.cxx:354
KVector< N >::type fPvec
Prediction vector.
Definition: KHit.h:154
KHit()
Default constructor.
Definition: KHit.h:167
KMatrix< N, 5 >::type type
KVector< N >::type fMvec
Measurement vector.
Definition: KHit.h:152
KVector< N >::type fRvec
Residual vector.
Definition: KHit.h:156
void setMeasVector(const typename KVector< N >::type &mvec)
Set measurement vector.
Definition: KHit.h:94
std::shared_ptr< const Surface > fPredSurf
Prediction surface.
Definition: KHitBase.h:116
const KSymMatrix< N >::type & getResError() const
Residual error matrix.
Definition: KHit.h:117
ublas::vector< double, ublas::bounded_array< double, N > > type
bool syminvert(ublas::symmetric_matrix< T, TRI, L, A > &m)
KHMatrix< N >::type fH
Kalman H-matrix.
Definition: KHit.h:159
const KSymMatrix< N >::type & getResInvError() const
Residual inv. error matrix.
Definition: KHit.h:120
double getChisq() const
Incremental chisquare.
Definition: KHit.h:126
KSymMatrix< N >::type fRinv
Residual inverse error matrix.
Definition: KHit.h:158
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
void setMeasError(const typename KSymMatrix< N >::type &merr)
Set measurement error.
Definition: KHit.h:97
bool predict(const KETrack &tre, const Propagator *prop=0, const KTrack *ref=0) const
Prediction method (return false if fail).
Definition: KHit.h:215
double fPredDist
Prediction distance.
Definition: KHitBase.h:117
const TrackVector & getVector() const
Track state vector.
Definition: KTrack.h:56
KSymMatrix< N >::type fRerr
Residual error matrix.
Definition: KHit.h:157
const KVector< N >::type & getMeasVector() const
Measurement vector.
Definition: KHit.h:102
KMatrix< 5, 5 >::type TrackMatrix
General 5x5 matrix.
KSymMatrix< N >::type fPerr
Prediction error matrix.
Definition: KHit.h:155
KMatrix< 5, N >::type type
virtual ~KHit()
Destructor.
Definition: KHit.h:203
KSymMatrix< N >::type fMerr
Measurement error matrix.
Definition: KHit.h:153
Base class for Kalman filter measurement.
const std::shared_ptr< const Surface > & getMeasSurface() const
Measurement surface.
Definition: KHitBase.h:81
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
double fChisq
Incremental chisquare.
Definition: KHit.h:160
const std::shared_ptr< const Surface > & getPredSurface() const
Predition surface.
Definition: KHitBase.h:75