LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
KETrack.cxx
Go to the documentation of this file.
1 
11 #include <cmath>
13 #include "cetlib_except/exception.h"
14 
15 namespace trkf {
16 
19  {}
20 
27  KETrack::KETrack(const std::shared_ptr<const Surface>& psurf) :
28  KTrack(psurf)
29  {}
30 
41  KETrack::KETrack(const std::shared_ptr<const Surface>& psurf,
42  const TrackVector& vec,
43  const TrackError& err,
45  int pdg) :
46  KTrack(psurf, vec, dir, pdg),
47  fErr(err)
48  {}
49 
57  KETrack::KETrack(const KTrack& trk, const TrackError& err) :
58  KTrack(trk),
59  fErr(err)
60  {}
61 
64  {}
65 
74  double KETrack::PointingError() const
75  {
76  if(!isValid())
77  throw cet::exception("KETrack") << "Pointing error requested for invalid track.\n";
78  return getSurface()->PointingError(getVector(), fErr);
79  }
80 
95  boost::optional<double> KETrack::combineTrack(const KETrack& tre)
96  {
97  // Make sure that the two track surfaces are the same.
98  // Throw an exception if they are not.
99 
100  if(!getSurface()->isEqual(*tre.getSurface()))
101  throw cet::exception("KETrack") << "Track combination surfaces are not the same.\n";
102 
103  // Default result is failure.
104 
105  boost::optional<double> result(false, 0.);
106 
107  // We will use asymmetric versions of the updating formulas, such
108  // that the result is calculated as a perturbation on the
109  // better-measured track. We define the better measured track as
110  // the one with the smaller error matrix trace.
111 
112  // Extract the two state vectors and error matrices as pointers.
113 
114  const TrackVector* vec1 = &getVector();
115  const TrackError* err1 = &getError();
116  const TrackVector* vec2 = &tre.getVector();
117  const TrackError* err2 = &tre.getError();
118 
119  // Calculate the traces of the error matrices.
120 
121  double tr1 = 0;
122  for(unsigned int i=0; i<err1->size1(); ++i)
123  tr1 += (*err1)(i,i);
124 
125  double tr2 = 0;
126  for(unsigned int i=0; i<err2->size1(); ++i)
127  tr2 += (*err2)(i,i);
128 
129  // Define vec1, err1 as belong to the better measured track.
130  // Swap if necessary.
131 
132  if(tr1 > tr2) {
133  const TrackVector* tvec = vec1;
134  vec1 = vec2;
135  vec2 = tvec;
136  const TrackError* terr = err1;
137  err1 = err2;
138  err2 = terr;
139  }
140 
141  // Calculate the difference vector and difference error matrix.
142 
143  TrackVector dvec = *vec1 - *vec2;
144  TrackError derr = *err1 + *err2;
145 
146  // Invert the difference error matrix.
147  // This is the only place where a detectable failure can occur.
148 
149  bool ok = syminvert(derr);
150  if(ok) {
151 
152  // Calculate updated state vector.
153  // vec1 = vec1 - err1 * derr * dvec
154 
155  TrackVector tvec1 = prod(derr, dvec);
156  TrackVector tvec2 = prod(*err1, tvec1);
157  TrackVector tvec3 = *vec1 - tvec2;
158  setVector(tvec3);
159 
160  // Calculate updated error matrix.
161  // err1 = err1 - err1 * derr * err1
162 
163  TrackMatrix terr1 = prod(derr, *err1);
164  TrackMatrix terr2 = prod(*err1, terr1);
165  TrackError terr2s = ublas::symmetric_adaptor<TrackMatrix>(terr2);
166  TrackError terr3 = *err1 - terr2s;
167  setError(terr3);
168 
169  // Calculate chisquare.
170  // chisq = dvec^T * derr * dvec
171 
172  TrackVector dvec1 = prod(derr, dvec);
173  double chisq = inner_prod(dvec, dvec1);
174  result = boost::optional<double>(true, chisq);
175  }
176 
177  // Final validity check.
178 
179  if(!isValid())
180  result = boost::optional<double>(false, 0.);
181 
182  // Done.
183 
184  return result;
185  }
186 
188  std::ostream& KETrack::Print(std::ostream& out, bool doTitle) const
189  {
190  if(doTitle)
191  out << "KETrack:\n";
192 
193  // Print base class.
194 
195  KTrack::Print(out, false);
196 
197  // Print diagonal errors.
198 
199  out << " Diagonal errors:\n"
200  << " [";
201  for(unsigned int i = 0; i < fErr.size1(); ++i) {
202  if(i != 0)
203  out << ", ";
204  double err = fErr(i,i);
205  err = (err >= 0. ? std::sqrt(err) : -std::sqrt(-err));
206  out << err;
207  }
208  out << "]\n";
209 
210  // Print correlations.
211 
212  out << " Correlation matrix:";
213  for(unsigned int i = 0; i < fErr.size1(); ++i) {
214  if(i == 0)
215  out << "\n [";
216  else
217  out << "\n ";
218  for(unsigned int j = 0; j <= i; ++j) {
219  if(j != 0)
220  out << ", ";
221  if(i == j)
222  out << 1.;
223  else {
224  double eiijj = fErr(i,i) * fErr(j,j);
225  double eij = fErr(i,j);
226  if(eiijj != 0.)
227  eij /= std::sqrt(std::abs(eiijj));
228  else
229  eij = 0.;
230  out << eij;
231  }
232  }
233  }
234  out << "]\n";
235  out << " Pointing error = " << PointingError() << "\n";
236  return out;
237  }
238 
239 } // end namespace trkf
const TrackError & getError() const
Track error matrix.
Definition: KETrack.h:54
TrackDirection
Track direction enum.
Definition: Surface.h:56
const std::shared_ptr< const Surface > & getSurface() const
Surface.
Definition: KTrack.h:55
double PointingError() const
Pointing error (radians).
Definition: KETrack.cxx:74
KSymMatrix< 5 >::type TrackError
Track error matrix, dimension 5x5.
void setVector(const TrackVector &vec)
Set state vector.
Definition: KTrack.h:67
KETrack()
Default constructor.
Definition: KETrack.cxx:18
boost::optional< double > combineTrack(const KETrack &tre)
Combine two tracks.
Definition: KETrack.cxx:95
void setError(const TrackError &err)
Set error matrix.
Definition: KETrack.h:60
virtual ~KETrack()
Destructor.
Definition: KETrack.cxx:63
bool syminvert(ublas::symmetric_matrix< T, TRI, L, A > &m)
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KTrack.cxx:226
const TrackVector & getVector() const
Track state vector.
Definition: KTrack.h:56
TDirectory * dir
Definition: macro.C:5
KMatrix< 5, 5 >::type TrackMatrix
General 5x5 matrix.
TrackError fErr
Track error matrix.
Definition: KETrack.h:72
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KETrack.cxx:188
Basic Kalman filter track class, with error.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
bool isValid() const
Test if track is valid.
Definition: KTrack.cxx:90