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