KTrack.cxx
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////
2 ///
3 /// \file KTrack.cxx
4 ///
5 /// \brief Basic Kalman filter track class, without error.
6 ///
7 /// \author H. Greenlee
8 ///
9 ////////////////////////////////////////////////////////////////////////
10 
11 #include <cmath>
12 #include <cstdlib>
13 #include <ostream>
15 #include "cetlib_except/exception.h"
16 
17 // Define some particle masses here.
18 
19 namespace {
20  const double mumass = 0.105658367; // Muon
21  const double pimass = 0.13957; // Charged pion
22  const double kmass = 0.493677; // Charged kaon
23  const double pmass = 0.938272; // Proton
24 }
25 
26 namespace trkf {
27 
28  /// Default constructor.
30  fDir(Surface::UNKNOWN),
31  fPdgCode(0)
32  {}
33 
34  /// Constructor - specify surface only.
35  ///
36  /// Arguments:
37  ///
38  /// psurf - Surface pointer.
39  ///
40  KTrack::KTrack(const std::shared_ptr<const Surface>& psurf) :
41  fSurf(psurf),
43  fPdgCode(0)
44  {}
45 
46  /// Constructor - surface + track parameters.
47  ///
48  /// Arguments:
49  ///
50  /// psurf - Surface pointer.
51  /// vec - Track state vector.
52  /// dir - Track direction.
53  /// pdg - Pdg code.
54  ///
55  KTrack::KTrack(std::shared_ptr<const Surface> psurf,
56  const TrackVector& vec,
58  int pdg) :
59  fSurf(psurf),
60  fVec(vec),
61  fDir(dir),
62  fPdgCode(pdg)
63  {}
64 
65  /// Destructor.
67  {}
68 
69  /// Track direction accessor.
70  /// Track direction implied by track parameters has precedence
71  /// over track direction attribute.
72  /// If the surface pointer is null, return UNKNOWN.
74  {
76  if(fSurf.get() != 0)
77  result = fSurf->getDirection(fVec, fDir);
78  return result;
79  }
80 
81  /// Test if track is valid.
82  ///
83  /// A default-constructed or partially-constructed track, is
84  /// invalid by virtue of having an unknown propagation direction
85  /// or a null surface pointer.
86  ///
87  /// Tracks can become invaliddynamically for other reasons. This
88  /// method also does the following checks:
89  /// a) Check for invalid floating point values (inf and nan).
90  /// b) Surface-dependent checks via virtual method Surface::isTrackValid.
91  bool KTrack::isValid() const
92  {
93  bool result = true;
94 
95  // Check for valid direction.
96 
98  result = false;
99 
100  // Check for non-null surface pointer (for safety, should be redundant
101  // with previous check.
102 
103  if(result && fSurf.get() == 0)
104  result = false;
105 
106  // Check for track parameters containing invalid floating point
107  // values.
108 
109  if(result) {
110  for(unsigned int i=0; i<fVec.size(); ++i) {
111  if(!std::isfinite(fVec(i))) {
112  result = false;
113  break;
114  }
115  }
116  }
117 
118  // Surface-dependent check on track validity.
119 
120  if(result && !fSurf->isTrackValid(fVec))
121  result = false;
122 
123  // Done.
124 
125  return result;
126  }
127 
128  /// Particle mass based on pdg code.
129  double KTrack::Mass() const
130  {
131  double mass = 0.;
132  int apdg = std::abs(fPdgCode);
133 
134  // Muon
135 
136  if(apdg == 13)
137  mass = mumass;
138 
139  // Charged pion
140 
141  else if(apdg == 211)
142  mass = pimass;
143 
144  // Charged kaon
145 
146  else if(apdg == 321)
147  mass = kmass;
148 
149  // (Anti)proton
150 
151  else if(apdg == 2212)
152  mass = pmass;
153 
154  // Anything else throw exception
155 
156  else
157  throw cet::exception("KTrack") << "Mass requested for invalid pdg id = " << fPdgCode << "\n";
158 
159  // Done.
160 
161  return mass;
162  }
163 
164  /// Get position of track.
165  /// Throw an exception if track is not valid.
166  ///
167  /// Arguments:
168  ///
169  /// xyz - Position vector.
170  ///
171  void KTrack::getPosition(double xyz[3]) const
172  {
173  if(!isValid())
174  throw cet::exception("KTrack") << "Position requested for invalid track.\n";
175  fSurf->getPosition(fVec, xyz);
176  }
177 
178  /// Get x-latitude.
179  ///
180  /// The x-latitude is the latitude defined with respect to the
181  /// x-axis. The x-latitude is zero of the track is traveling
182  /// parallel to the wire planes.
183  ///
184  double KTrack::XLatitude() const
185  {
186  double mom[3];
187  getMomentum(mom);
188  double ptx = std::sqrt(mom[1]*mom[1] + mom[2]*mom[2]);
189  double result = 0.;
190  if(ptx > 0. || mom[0] > 0.)
191  result = atan2(mom[0], ptx);
192  return result;
193  }
194 
195  /// Get x-longitude.
196  ///
197  /// The x-longitude is the longitude defined with respect to the y-
198  /// and z-axes. The x-longitude is zero of the track is parallel to
199  /// the z-axis in the yz-plane.
200  ///
201  double KTrack::XLongitude() const
202  {
203  double mom[3];
204  getMomentum(mom);
205  double result = 0.;
206  if(mom[1] != 0. || mom[2] != 0.)
207  result = atan2(mom[1], mom[2]);
208  return result;
209  }
210 
211  /// Get momentum vector of track.
212  /// Throw an exception if track is not valid.
213  ///
214  /// Arguments:
215  ///
216  /// mom - Momentum vector of track.
217  ///
218  void KTrack::getMomentum(double mom[3]) const
219  {
220  if(!isValid())
221  throw cet::exception("KTrack") << "Momentum vector requested for invalid track.\n";
223  fSurf->getMomentum(fVec, mom, dir);
224  }
225 
226  /// Printout
227  std::ostream& KTrack::Print(std::ostream& out, bool doTitle) const
228  {
229  if(doTitle)
230  out << "KTrack:\n";
231  double xyz[3];
232  double dir[3];
233  getPosition(xyz);
234  getMomentum(dir);
235  double p = std::sqrt(dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2]);
236  if(p != 0.) {
237  dir[0] /= p;
238  dir[1] /= p;
239  dir[2] /= p;
240  }
241  out << " Surface direction = " << (fDir == Surface::FORWARD ?
242  "FORWARD" :
243  ( fDir == Surface::BACKWARD ?
244  "BACKWARD" : "UNKNOWN" )) << "\n"
245  << " Pdg = " << fPdgCode << "\n"
246  << " Surface: " << *fSurf << "\n"
247  << " Track parameters:\n"
248  << " [";
249  for(unsigned int i = 0; i < fVec.size(); ++i) {
250  if(i != 0)
251  out << ", ";
252  out << fVec(i);
253  }
254  out << "]\n";
255  out << " Position: [" << xyz[0] << ", " << xyz[1] << ", " << xyz[2] << "]\n";
256  out << " Direction: [" << dir[0] << ", " << dir[1] << ", " << dir[2] << "]\n";
257  out << " X-Latitude = " << XLatitude() << "\n";
258  out << " X-Longitude = " << XLongitude() << "\n";
259  return out;
260  }
261 
262  /// Output operator.
263  std::ostream& operator<<(std::ostream& out, const KTrack& trk)
264  {
265  return trk.Print(out);
266  }
267 
268 } // end namespace trkf
TrackVector fVec
Track state vector.
Definition: KTrack.h:94
TrackDirection
Track direction enum.
Definition: Surface.h:56
std::shared_ptr< const Surface > fSurf
Track surface.
Definition: KTrack.h:93
double Mass() const
Based on pdg code.
Definition: KTrack.cxx:129
static QCString result
double XLongitude() const
Get x-longitude.
Definition: KTrack.cxx:201
string dir
T abs(T value)
int fPdgCode
Pdg id. hypothesis.
Definition: KTrack.h:96
bool isfinite(Vector const &v)
Returns whether all components of the vector are finite.
std::ostream & operator<<(std::ostream &out, const KGTrack &trg)
Output operator.
Definition: KGTrack.cxx:309
void getPosition(double xyz[3]) const
Get position of track.
Definition: KTrack.cxx:171
KTrack()
Enum.
Definition: KTrack.cxx:29
p
Definition: test.py:223
virtual ~KTrack()
Destructor.
Definition: KTrack.cxx:66
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KTrack.cxx:227
Surface::TrackDirection fDir
Track direction.
Definition: KTrack.h:95
void getMomentum(double mom[3]) const
Get momentum vector of track.
Definition: KTrack.cxx:218
Surface::TrackDirection getDirection() const
Track direction.
Definition: KTrack.cxx:73
Basic Kalman filter track class, without error.
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
bool isValid() const
Test if track is valid.
Definition: KTrack.cxx:91
double XLatitude() const
Get x-latitude.
Definition: KTrack.cxx:184