LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
KTrack.cxx
Go to the documentation of this file.
1 
12 #include "cetlib_except/exception.h"
13 #include <cmath>
14 #include <cstdlib>
15 #include <ostream>
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 
29  KTrack::KTrack() : fDir(Surface::UNKNOWN), fPdgCode(0) {}
30 
37  KTrack::KTrack(const std::shared_ptr<const Surface>& psurf)
38  : fSurf(psurf), fDir(Surface::UNKNOWN), fPdgCode(0)
39  {}
40 
50  KTrack::KTrack(std::shared_ptr<const Surface> psurf,
51  const TrackVector& vec,
53  int pdg)
54  : fSurf(psurf), fVec(vec), fDir(dir), fPdgCode(pdg)
55  {}
56 
59 
65  {
67  if (fSurf.get() != 0) result = fSurf->getDirection(fVec, fDir);
68  return result;
69  }
70 
81  bool KTrack::isValid() const
82  {
83  bool result = true;
84 
85  // Check for valid direction.
86 
87  if (getDirection() == Surface::UNKNOWN) result = false;
88 
89  // Check for non-null surface pointer (for safety, should be redundant
90  // with previous check.
91 
92  if (result && fSurf.get() == 0) result = false;
93 
94  // Check for track parameters containing invalid floating point
95  // values.
96 
97  if (result) {
98  for (unsigned int i = 0; i < fVec.size(); ++i) {
99  if (!std::isfinite(fVec(i))) {
100  result = false;
101  break;
102  }
103  }
104  }
105 
106  // Surface-dependent check on track validity.
107 
108  if (result && !fSurf->isTrackValid(fVec)) result = false;
109 
110  // Done.
111 
112  return result;
113  }
114 
116  double KTrack::Mass() const
117  {
118  double mass = 0.;
119  int apdg = std::abs(fPdgCode);
120 
121  // Muon
122 
123  if (apdg == 13) mass = mumass;
124 
125  // Charged pion
126 
127  else if (apdg == 211)
128  mass = pimass;
129 
130  // Charged kaon
131 
132  else if (apdg == 321)
133  mass = kmass;
134 
135  // (Anti)proton
136 
137  else if (apdg == 2212)
138  mass = pmass;
139 
140  // Anything else throw exception
141 
142  else
143  throw cet::exception("KTrack") << "Mass requested for invalid pdg id = " << fPdgCode << "\n";
144 
145  // Done.
146 
147  return mass;
148  }
149 
157  void KTrack::getPosition(double xyz[3]) const
158  {
159  if (!isValid()) throw cet::exception("KTrack") << "Position requested for invalid track.\n";
160  fSurf->getPosition(fVec, xyz);
161  }
162 
169  double KTrack::XLatitude() const
170  {
171  double mom[3];
172  getMomentum(mom);
173  double ptx = std::sqrt(mom[1] * mom[1] + mom[2] * mom[2]);
174  double result = 0.;
175  if (ptx > 0. || mom[0] > 0.) result = atan2(mom[0], ptx);
176  return result;
177  }
178 
185  double KTrack::XLongitude() const
186  {
187  double mom[3];
188  getMomentum(mom);
189  double result = 0.;
190  if (mom[1] != 0. || mom[2] != 0.) result = atan2(mom[1], mom[2]);
191  return result;
192  }
193 
201  void KTrack::getMomentum(double mom[3]) const
202  {
203  if (!isValid())
204  throw cet::exception("KTrack") << "Momentum vector requested for invalid track.\n";
206  fSurf->getMomentum(fVec, mom, dir);
207  }
208 
210  std::ostream& KTrack::Print(std::ostream& out, bool doTitle) const
211  {
212  if (doTitle) out << "KTrack:\n";
213  double xyz[3];
214  double dir[3];
215  getPosition(xyz);
216  getMomentum(dir);
217  double p = std::sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
218  if (p != 0.) {
219  dir[0] /= p;
220  dir[1] /= p;
221  dir[2] /= p;
222  }
223  out << " Surface direction = "
224  << (fDir == Surface::FORWARD ? "FORWARD" :
225  (fDir == Surface::BACKWARD ? "BACKWARD" : "UNKNOWN"))
226  << "\n"
227  << " Pdg = " << fPdgCode << "\n"
228  << " Surface: " << *fSurf << "\n"
229  << " Track parameters:\n"
230  << " [";
231  for (unsigned int i = 0; i < fVec.size(); ++i) {
232  if (i != 0) out << ", ";
233  out << fVec(i);
234  }
235  out << "]\n";
236  out << " Position: [" << xyz[0] << ", " << xyz[1] << ", " << xyz[2] << "]\n";
237  out << " Direction: [" << dir[0] << ", " << dir[1] << ", " << dir[2] << "]\n";
238  out << " X-Latitude = " << XLatitude() << "\n";
239  out << " X-Longitude = " << XLongitude() << "\n";
240  return out;
241  }
242 
244  std::ostream& operator<<(std::ostream& out, const KTrack& trk)
245  {
246  return trk.Print(out);
247  }
248 
249 } // end namespace trkf
TrackVector fVec
Track state vector.
Definition: KTrack.h:91
TrackDirection
Track direction enum.
Definition: Surface.h:54
std::shared_ptr< const Surface > fSurf
Track surface.
Definition: KTrack.h:90
double Mass() const
Based on pdg code.
Definition: KTrack.cxx:116
double XLongitude() const
Get x-longitude.
Definition: KTrack.cxx:185
constexpr auto abs(T v)
Returns the absolute value of the argument.
int fPdgCode
Pdg id. hypothesis.
Definition: KTrack.h:93
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:300
void getPosition(double xyz[3]) const
Get position of track.
Definition: KTrack.cxx:157
KTrack()
Enum.
Definition: KTrack.cxx:29
virtual ~KTrack()
Destructor.
Definition: KTrack.cxx:58
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
virtual std::ostream & Print(std::ostream &out, bool doTitle=true) const
Printout.
Definition: KTrack.cxx:210
Surface::TrackDirection fDir
Track direction.
Definition: KTrack.h:92
TDirectory * dir
Definition: macro.C:5
void getMomentum(double mom[3]) const
Get momentum vector of track.
Definition: KTrack.cxx:201
Surface::TrackDirection getDirection() const
Track direction.
Definition: KTrack.cxx:64
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:81
double XLatitude() const
Get x-latitude.
Definition: KTrack.cxx:169