LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
SurfYZLine.cxx
Go to the documentation of this file.
1 
12 #include "TMath.h"
13 #include "TVector2.h"
14 #include "cetlib_except/exception.h"
15 #include <cmath>
16 
17 namespace trkf {
18 
19  // Static attributes.
20 
21  double SurfYZLine::fPhiTolerance = 1.e-10;
22  double SurfYZLine::fSepTolerance = 1.e-6;
23 
25  SurfYZLine::SurfYZLine() : fX0(0.), fY0(0.), fZ0(0.), fPhi(0.) {}
26 
34  SurfYZLine::SurfYZLine(double x0, double y0, double z0, double phi)
35  : fX0(x0), fY0(y0), fZ0(z0), fPhi(phi)
36  {}
37 
40 
43  {
44  return new SurfYZLine(*this);
45  }
46 
48  bool SurfYZLine::isTrackValid(const TrackVector& vec) const
49  {
50  // Limit allowed range of eta parameter.
51 
52  return std::abs(vec(3)) < 10.;
53  }
54 
62  void SurfYZLine::toLocal(const double xyz[3], double uvw[3]) const
63  {
64  double sinphi = std::sin(fPhi);
65  double cosphi = std::cos(fPhi);
66 
67  // u = x-x0
68  uvw[0] = xyz[0] - fX0;
69 
70  // v = (y-y0)*cos(phi) + (z-z0)*sin(phi)
71  uvw[1] = (xyz[1] - fY0) * cosphi + (xyz[2] - fZ0) * sinphi;
72 
73  // w = -(y-y0)*sin(phi) + (z-z0)*cos(phi)
74  uvw[2] = -(xyz[1] - fY0) * sinphi + (xyz[2] - fZ0) * cosphi;
75  }
76 
84  void SurfYZLine::toGlobal(const double uvw[3], double xyz[3]) const
85  {
86  double sinphi = std::sin(fPhi);
87  double cosphi = std::cos(fPhi);
88 
89  // x = x0 + u
90  xyz[0] = fX0 + uvw[0];
91 
92  // y = y0 + v*cos(phi) - w*sin(phi)
93  xyz[1] = fY0 + uvw[1] * cosphi - uvw[2] * sinphi;
94 
95  // z = z0 + v*sin(phi) + w*cos(phi)
96  xyz[2] = fZ0 + uvw[1] * sinphi + uvw[2] * cosphi;
97  }
98 
108  TrackVector SurfYZLine::getDiff(const TrackVector& vec1, const TrackVector& vec2) const
109  {
110  TrackVector result = vec1 - vec2;
111  while (result(2) <= -TMath::Pi())
112  result(2) += TMath::TwoPi();
113  while (result(2) > TMath::Pi())
114  result(2) -= TMath::TwoPi();
115  return result;
116  }
117 
125  void SurfYZLine::getPosition(const TrackVector& vec, double xyz[3]) const
126  {
127  // Get position in local coordinate system.
128 
129  double phi = vec(2);
130  double sinphi = std::sin(phi);
131  double cosphi = std::cos(phi);
132 
133  double uvw[3];
134  uvw[0] = -vec(0) * sinphi;
135  uvw[1] = vec(1);
136  uvw[2] = vec(0) * cosphi;
137 
138  // Transform to global coordinate system.
139 
140  toGlobal(uvw, xyz);
141  return;
142  }
143 
152  void SurfYZLine::getMomentum(const TrackVector& vec, double mom[3], TrackDirection dir) const
153  {
154 
155  // Get momentum.
156 
157  double invp = std::abs(vec(4));
158  double p = 1. / std::max(invp, 1.e-3); // Capped at 1000. GeV/c.
159 
160  // Get track direction parameters.
161 
162  double phi = vec(2);
163  double eta = vec(3);
164 
165  double sinphi = std::sin(phi);
166  double cosphi = std::cos(phi);
167  double sh = 1. / std::cosh(eta); // sech(eta)
168  double th = std::tanh(eta);
169 
170  // Calculate momentum vector in local coordinate system.
171 
172  double pu = p * cosphi * sh;
173  double pv = p * th;
174  double pw = p * sinphi * sh;
175 
176  // Rotate momentum to global coordinte system.
177 
178  double sinfphi = std::sin(fPhi);
179  double cosfphi = std::cos(fPhi);
180 
181  mom[0] = pu;
182  mom[1] = pv * cosfphi - pw * sinfphi;
183  mom[2] = pv * sinfphi + pw * cosfphi;
184 
185  return;
186  }
187 
198  bool SurfYZLine::isParallel(const Surface& surf) const
199  {
200  bool result = false;
201 
202  // Test if the other surface is a SurfYZLine.
203 
204  const SurfYZLine* psurf = dynamic_cast<const SurfYZLine*>(&surf);
205  if (psurf != 0) {
206 
207  // Test whether surface angle parameters are the same
208  // within tolerance.
209 
210  double delta_phi = TVector2::Phi_mpi_pi(fPhi - psurf->phi());
211  if (std::abs(delta_phi) <= fPhiTolerance) result = true;
212  }
213  return result;
214  }
215 
226  double SurfYZLine::distanceTo(const Surface& surf) const
227  {
228  // Check if the other surface is parallel to this one.
229 
230  bool parallel = isParallel(surf);
231  if (!parallel)
232  throw cet::exception("SurfYZLine") << "Attempt to find distance to non-parallel surface.\n";
233 
234  // Find the origin of the other surface in global coordinates,
235  // then convert to our local coordinates.
236 
237  double otheruvw[3] = {0., 0., 0.};
238  double xyz[3];
239  double myuvw[3];
240  surf.toGlobal(otheruvw, xyz);
241  toLocal(xyz, myuvw);
242 
243  // Distance of v-axis to other surface origin.
244 
245  return std::hypot(myuvw[0], myuvw[2]);
246  }
247 
259  bool SurfYZLine::isEqual(const Surface& surf) const
260  {
261  bool result = false;
262 
263  // Test if the other surface is a SurfYZLine.
264 
265  const SurfYZLine* psurf = dynamic_cast<const SurfYZLine*>(&surf);
266  if (psurf != 0) {
267 
268  // Test whether surface parameters are the same within tolerance.
269 
270  double delta_phi = TVector2::Phi_mpi_pi(fPhi - psurf->phi());
271  double dx = fX0 - psurf->x0();
272  double dy = fY0 - psurf->y0();
273  double dz = fZ0 - psurf->z0();
274  if (std::abs(delta_phi) <= fPhiTolerance && std::abs(dx) <= fSepTolerance &&
275  std::abs(dy) <= fSepTolerance && std::abs(dz) <= fSepTolerance)
276  result = true;
277  }
278  return result;
279  }
280 
282  std::ostream& SurfYZLine::Print(std::ostream& out) const
283  {
284  out << "SurfYZLine{ x0=" << fX0 << ", y0=" << fY0 << ", z0=" << fZ0 << ", phi=" << fPhi << "}";
285  return out;
286  }
287 
288 } // end namespace trkf
TrackDirection
Track direction enum.
Definition: Surface.h:54
constexpr auto abs(T v)
Returns the absolute value of the argument.
virtual void toGlobal(const double uvw[3], double xyz[3]) const
Transform local to global coordinates.
Definition: SurfYZLine.cxx:84
virtual bool isEqual(const Surface &surf) const
Test two surfaces for equality, within tolerance.
Definition: SurfYZLine.cxx:259
virtual void toLocal(const double xyz[3], double uvw[3]) const
Transform global to local coordinates.
Definition: SurfYZLine.cxx:62
virtual bool isTrackValid(const TrackVector &vec) const
Surface-specific tests of validity of track parameters.
Definition: SurfYZLine.cxx:48
virtual void getMomentum(const TrackVector &vec, double mom[3], TrackDirection dir=UNKNOWN) const
Get momentum vector of track.
Definition: SurfYZLine.cxx:152
virtual bool isParallel(const Surface &surf) const
Test whether two surfaces are parallel, within tolerance.
Definition: SurfYZLine.cxx:198
double fX0
X origin.
Definition: SurfYZLine.h:138
virtual double distanceTo(const Surface &surf) const
Find perpendicular distance to a parallel surface.
Definition: SurfYZLine.cxx:226
virtual ~SurfYZLine()
Destructor.
Definition: SurfYZLine.cxx:39
static double fPhiTolerance
Phi tolerance for parallel.
Definition: SurfYZLine.h:133
virtual std::ostream & Print(std::ostream &out) const
Printout.
Definition: SurfYZLine.cxx:282
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
double z0() const
Z origin.
Definition: SurfYZLine.h:92
static double fSepTolerance
Separation tolerance for equal.
Definition: SurfYZLine.h:134
double x0() const
X origin.
Definition: SurfYZLine.h:90
TDirectory * dir
Definition: macro.C:5
double fZ0
Z origin.
Definition: SurfYZLine.h:140
double fY0
Y origin.
Definition: SurfYZLine.h:139
virtual TrackVector getDiff(const TrackVector &vec1, const TrackVector &vec2) const
Calculate difference of two track parameter vectors.
Definition: SurfYZLine.cxx:108
double phi() const
Rotation angle about x-axis.
Definition: SurfYZLine.h:93
double y0() const
Y origin.
Definition: SurfYZLine.h:91
Line surface perpendicular to x-axis.
double fPhi
Rotation angle about x-axis.
Definition: SurfYZLine.h:141
virtual Surface * clone() const
Clone method.
Definition: SurfYZLine.cxx:42
virtual void getPosition(const TrackVector &vec, double xyz[3]) const
Get position of track.
Definition: SurfYZLine.cxx:125
virtual void toGlobal(const double uvw[3], double xyz[3]) const =0
Transform local to global coordinates.
Float_t e
Definition: plot.C:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
SurfYZLine()
Default constructor.
Definition: SurfYZLine.cxx:25