LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
KTrack.cxx
Go to the documentation of this file.
1 
11 #include <cmath>
12 #include <cstdlib>
14 #include "cetlib_except/exception.h"
15 
16 // Define some particle masses here.
17 
18 namespace {
19  const double mumass = 0.105658367; // Muon
20  const double pimass = 0.13957; // Charged pion
21  const double kmass = 0.493677; // Charged kaon
22  const double pmass = 0.938272; // Proton
23 }
24 
25 namespace trkf {
26 
29  fDir(Surface::UNKNOWN),
30  fPdgCode(0)
31  {}
32 
39  KTrack::KTrack(const std::shared_ptr<const Surface>& psurf) :
40  fSurf(psurf),
42  fPdgCode(0)
43  {}
44 
54  KTrack::KTrack(std::shared_ptr<const Surface> psurf,
55  const TrackVector& vec,
57  int pdg) :
58  fSurf(psurf),
59  fVec(vec),
60  fDir(dir),
61  fPdgCode(pdg)
62  {}
63 
66  {}
67 
73  {
75  if(fSurf.get() != 0)
76  result = fSurf->getDirection(fVec, fDir);
77  return result;
78  }
79 
90  bool KTrack::isValid() const
91  {
92  bool result = true;
93 
94  // Check for valid direction.
95 
97  result = false;
98 
99  // Check for non-null surface pointer (for safety, should be redundant
100  // with previous check.
101 
102  if(result && fSurf.get() == 0)
103  result = false;
104 
105  // Check for track parameters containing invalid floating point
106  // values.
107 
108  if(result) {
109  for(unsigned int i=0; i<fVec.size(); ++i) {
110  if(!std::isfinite(fVec(i))) {
111  result = false;
112  break;
113  }
114  }
115  }
116 
117  // Surface-dependent check on track validity.
118 
119  if(result && !fSurf->isTrackValid(fVec))
120  result = false;
121 
122  // Done.
123 
124  return result;
125  }
126 
128  double KTrack::Mass() const
129  {
130  double mass = 0.;
131  int apdg = std::abs(fPdgCode);
132 
133  // Muon
134 
135  if(apdg == 13)
136  mass = mumass;
137 
138  // Charged pion
139 
140  else if(apdg == 211)
141  mass = pimass;
142 
143  // Charged kaon
144 
145  else if(apdg == 321)
146  mass = kmass;
147 
148  // (Anti)proton
149 
150  else if(apdg == 2212)
151  mass = pmass;
152 
153  // Anything else throw exception
154 
155  else
156  throw cet::exception("KTrack") << "Mass requested for invalid pdg id = " << fPdgCode << "\n";
157 
158  // Done.
159 
160  return mass;
161  }
162 
170  void KTrack::getPosition(double xyz[3]) const
171  {
172  if(!isValid())
173  throw cet::exception("KTrack") << "Position requested for invalid track.\n";
174  fSurf->getPosition(fVec, xyz);
175  }
176 
183  double KTrack::XLatitude() const
184  {
185  double mom[3];
186  getMomentum(mom);
187  double ptx = std::sqrt(mom[1]*mom[1] + mom[2]*mom[2]);
188  double result = 0.;
189  if(ptx > 0. || mom[0] > 0.)
190  result = atan2(mom[0], ptx);
191  return result;
192  }
193 
200  double KTrack::XLongitude() const
201  {
202  double mom[3];
203  getMomentum(mom);
204  double result = 0.;
205  if(mom[1] != 0. || mom[2] != 0.)
206  result = atan2(mom[1], mom[2]);
207  return result;
208  }
209 
217  void KTrack::getMomentum(double mom[3]) const
218  {
219  if(!isValid())
220  throw cet::exception("KTrack") << "Momentum vector requested for invalid track.\n";
222  fSurf->getMomentum(fVec, mom, dir);
223  }
224 
226  std::ostream& KTrack::Print(std::ostream& out, bool doTitle) const
227  {
228  if(doTitle)
229  out << "KTrack:\n";
230  double xyz[3];
231  double dir[3];
232  getPosition(xyz);
233  getMomentum(dir);
234  double p = std::sqrt(dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2]);
235  if(p != 0.) {
236  dir[0] /= p;
237  dir[1] /= p;
238  dir[2] /= p;
239  }
240  out << " Surface direction = " << (fDir == Surface::FORWARD ?
241  "FORWARD" :
242  ( fDir == Surface::BACKWARD ?
243  "BACKWARD" : "UNKNOWN" )) << "\n"
244  << " Pdg = " << fPdgCode << "\n"
245  << " Surface: " << *fSurf << "\n"
246  << " Track parameters:\n"
247  << " [";
248  for(unsigned int i = 0; i < fVec.size(); ++i) {
249  if(i != 0)
250  out << ", ";
251  out << fVec(i);
252  }
253  out << "]\n";
254  out << " Position: [" << xyz[0] << ", " << xyz[1] << ", " << xyz[2] << "]\n";
255  out << " Direction: [" << dir[0] << ", " << dir[1] << ", " << dir[2] << "]\n";
256  out << " X-Latitude = " << XLatitude() << "\n";
257  out << " X-Longitude = " << XLongitude() << "\n";
258  return out;
259  }
260 
262  std::ostream& operator<<(std::ostream& out, const KTrack& trk)
263  {
264  return trk.Print(out);
265  }
266 
267 } // 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:128
double XLongitude() const
Get x-longitude.
Definition: KTrack.cxx:200
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:306
void getPosition(double xyz[3]) const
Get position of track.
Definition: KTrack.cxx:170
KTrack()
Enum.
Definition: KTrack.cxx:28
virtual ~KTrack()
Destructor.
Definition: KTrack.cxx:65
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
Surface::TrackDirection fDir
Track direction.
Definition: KTrack.h:95
TDirectory * dir
Definition: macro.C:5
void getMomentum(double mom[3]) const
Get momentum vector of track.
Definition: KTrack.cxx:217
Surface::TrackDirection getDirection() const
Track direction.
Definition: KTrack.cxx:72
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:90
double XLatitude() const
Get x-latitude.
Definition: KTrack.cxx:183