LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
KHitMulti.cxx
Go to the documentation of this file.
1 
12 #include "cetlib_except/exception.h"
13 
14 namespace trkf {
15 
18  fMeasDim(0),
19  fChisq(0.)
20  {}
21 
28  KHitMulti::KHitMulti(const std::shared_ptr<const Surface>& psurf) :
29  KHitBase(psurf),
30  fMeasDim(0),
31  fChisq(0.)
32  {}
33 
36  {}
37 
47  void KHitMulti::addMeas(const std::shared_ptr<const KHitBase>& pmeas)
48  {
49  // It is an error to pass in a null pointer.
50 
51  if(pmeas.get() == 0)
52  throw cet::exception("KHitMulti") << "Attempt to add null measurement pointer.\n";
53 
54  // Do the dynamic cast.
55 
56  std::shared_ptr<const KHit<1> > pmeas1 =
57  std::dynamic_pointer_cast<const KHit<1>, const KHitBase>(pmeas);
58 
59  // Throw an exception if dynamic cast failed.
60 
61  if(pmeas1.get() == 0)
62  throw cet::exception("KHitMulti") << "Dynamic cast for KHitBase pointer failed.\n";
63  addMeas(pmeas1);
64  }
65 
72  void KHitMulti::addMeas(const std::shared_ptr<const KHit<1> >& pmeas)
73  {
74  // It is an error to pass in a null pointer.
75 
76  if(pmeas.get() == 0)
77  throw cet::exception("KHitMulti") << "Attempt to add null measurement pointer.\n";
78 
79  // Add the measurement.
80 
81  ++fMeasDim;
82  fMeasVec.push_back(pmeas);
83  }
84 
98  bool KHitMulti::predict(const KETrack& tre, const Propagator* prop, const KTrack* ref) const
99  {
100  // Resize and clear all linear algebra objects.
101 
102  fMvec.resize(fMeasDim, false);
103  fMvec.clear();
104 
105  fMerr.resize(fMeasDim, false);
106  fMerr.clear();
107 
108  fPvec.resize(fMeasDim, false);
109  fPvec.clear();
110 
111  fPerr.resize(fMeasDim, false);
112  fPerr.clear();
113 
114  fRvec.resize(fMeasDim, false);
115  fRvec.clear();
116 
117  fRerr.resize(fMeasDim, false);
118  fRerr.clear();
119 
120  fRinv.resize(fMeasDim, false);
121  fRinv.clear();
122 
123  fH.resize(fMeasDim, tre.getVector().size());
124  fH.clear();
125 
126  // Update the prediction surface to be the track surface.
127 
128  fPredSurf = tre.getSurface();
129  fPredDist = 0.;
130 
131  // Result.
132 
133  bool ok = true;
134 
135  // Loop over one-dimensional measurements.
136 
137  for(unsigned int im = 0; ok && im < fMeasVec.size(); ++im) {
138  const KHit<1>& meas = *(fMeasVec[im]);
139 
140  // Update prediction for this measurement.
141 
142  ok = meas.predict(tre, prop, ref);
143  if(!ok)
144  break;
145 
146  //
147 
148  // Update objects that are concatenations of underlying measurements.
149 
150  fMvec(im) = meas.getMeasVector()(0); // Measurement vector.
151  fMerr(im, im) = meas.getMeasError()(0, 0); // Measurement error matrix.
152  fPvec(im) = meas.getPredVector()(0); // Prediction vector.
153 
154  // H-matrix.
155 
156  for(unsigned int j = 0; j < meas.getH().size2(); ++j)
157  fH(im, j) = meas.getH()(0, j);
158  }
159  if(ok) {
160 
161  // Calculate prediction error matrix.
162  // T = H C H^T.
163 
164  ublas::matrix<double> temp(fH.size2(), fMeasDim);
165  ublas::matrix<double> temp2(fMeasDim, fMeasDim);
166  temp = prod(tre.getError(), trans(fH));
167  temp2 = prod(fH, temp);
168  fPerr = ublas::symmetric_adaptor<ublas::matrix<double> >(temp2);
169 
170  // Update residual
171 
172  fRvec = fMvec - fPvec;
173  fRerr = fMerr + fPerr;
174  fRinv = fRerr;
175  ok = syminvert(fRinv);
176  if(ok) {
177 
178  // Calculate incremental chisquare.
179 
180  ublas::vector<double> rtemp = prod(fRinv, fRvec);
181  fChisq = inner_prod(fRvec, rtemp);
182  }
183  }
184 
185  // If a problem occured at any step, clear the prediction surface pointer.
186 
187  if(!ok) {
188  fPredSurf.reset();
189  fPredDist = 0.;
190  }
191 
192  // Done.
193 
194  return ok;
195  }
196 
205  void KHitMulti::update(KETrack& tre) const
206  {
207  // Make sure that the track surface and the prediction surface are the same.
208  // Throw an exception if they are not.
209 
210  if(!getPredSurface()->isEqual(*tre.getSurface()))
211  throw cet::exception("KHitMulti") << "Track surface not the same as prediction surface.\n";
212 
213  const TrackVector& tvec = tre.getVector();
214  const TrackError& terr = tre.getError();
215  TrackVector::size_type size = tvec.size();
216 
217  // Calculate gain matrix.
218 
219  ublas::matrix<double> temp(size, fMeasDim);
220  ublas::matrix<double> gain(size, fMeasDim);
221  temp = prod(trans(fH), fRinv);
222  gain = prod(terr, temp);
223 
224  // Calculate updated track state.
225 
226  TrackVector newvec = tre.getVector() + prod(gain, fRvec);
227 
228  // Calculate updated error matrix.
229 
230  TrackMatrix fact = ublas::identity_matrix<TrackVector::value_type>(size);
231  fact -= prod(gain, fH);
232  TrackMatrix errtemp1 = prod(terr, trans(fact));
233  TrackMatrix errtemp2 = prod(fact, errtemp1);
234  TrackError errtemp2s = ublas::symmetric_adaptor<TrackMatrix>(errtemp2);
235  ublas::matrix<double> errtemp3 = prod(fMerr, trans(gain));
236  TrackMatrix errtemp4 = prod(gain, errtemp3);
237  TrackError errtemp4s = ublas::symmetric_adaptor<TrackMatrix>(errtemp4);
238  TrackError newerr = errtemp2s + errtemp4s;
239 
240  // Update track.
241 
242  tre.setVector(newvec);
243  tre.setError(newerr);
244  }
245 
247  std::ostream& KHitMulti::Print(std::ostream& out, bool doTitle) const
248  {
249  if(doTitle)
250  out << "KHitMulti:\n";
251  return out;
252  }
253 
254 } // end namespace trkf
const TrackError & getError() const
Track error matrix.
Definition: KETrack.h:54
ublas::vector< double > fPvec
Prediction vector.
Definition: KHitMulti.h:129
bool predict(const KETrack &tre, const Propagator *prop=0, const KTrack *ref=0) const
Prediction method (return false if fail).
Definition: KHitMulti.cxx:98
const std::shared_ptr< const Surface > & getSurface() const
Surface.
Definition: KTrack.h:55
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
Compound Kalman Filter measurement.
const KHMatrix< N >::type & getH() const
Kalman H-matrix.
Definition: KHit.h:123
std::vector< std::shared_ptr< const KHit< 1 > > > fMeasVec
Underlying measurements.
Definition: KHitMulti.h:126
void setError(const TrackError &err)
Set error matrix.
Definition: KETrack.h:60
const KVector< N >::type & getPredVector() const
Prediction vector.
Definition: KHit.h:108
std::shared_ptr< const Surface > fPredSurf
Prediction surface.
Definition: KHitBase.h:116
bool syminvert(ublas::symmetric_matrix< T, TRI, L, A > &m)
void addMeas(const std::shared_ptr< const KHitBase > &pmeas)
Add a measurement of unknown type.
Definition: KHitMulti.cxx:47
ublas::symmetric_matrix< double > fRinv
Residual inverse error matrix.
Definition: KHitMulti.h:133
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KHitMulti.cxx:247
bool predict(const KETrack &tre, const Propagator *prop=0, const KTrack *ref=0) const
Prediction method (return false if fail).
Definition: KHit.h:215
KHitMulti()
Default constructor.
Definition: KHitMulti.cxx:17
ublas::symmetric_matrix< double > fPerr
Prediction error matrix.
Definition: KHitMulti.h:130
double fPredDist
Prediction distance.
Definition: KHitBase.h:117
ublas::vector< double > fRvec
Residual vector.
Definition: KHitMulti.h:131
const TrackVector & getVector() const
Track state vector.
Definition: KTrack.h:56
const KVector< N >::type & getMeasVector() const
Measurement vector.
Definition: KHit.h:102
ublas::matrix< double > fH
Kalman H-matrix.
Definition: KHitMulti.h:134
KMatrix< 5, 5 >::type TrackMatrix
General 5x5 matrix.
ublas::symmetric_matrix< double > fRerr
Residual error matrix.
Definition: KHitMulti.h:132
ublas::symmetric_matrix< double > fMerr
Measurement error matrix.
Definition: KHitMulti.h:128
void update(KETrack &tre) const
Update track method.
Definition: KHitMulti.cxx:205
double fChisq
Incremental chisquare.
Definition: KHitMulti.h:135
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
ublas::vector< double > fMvec
Measurement vector.
Definition: KHitMulti.h:127
const std::shared_ptr< const Surface > & getPredSurface() const
Predition surface.
Definition: KHitBase.h:75
virtual ~KHitMulti()
Destructor.
Definition: KHitMulti.cxx:35
int fMeasDim
Measurement space dimension.
Definition: KHitMulti.h:125