SurfYZPlane.cxx
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////
2 ///
3 /// \file SurfYZPlane.cxx
4 ///
5 /// \brief Planar surface parallel to x-axis.
6 ///
7 /// \author H. Greenlee
8 ///
9 ////////////////////////////////////////////////////////////////////////
10 
11 #include <cmath>
13 #include "cetlib_except/exception.h"
14 #include "TVector2.h"
15 
16 namespace trkf {
17 
18  // Static attributes.
19 
20  double SurfYZPlane::fPhiTolerance = 1.e-10;
21  double SurfYZPlane::fSepTolerance = 1.e-6;
22 
23  /// Default constructor.
25  fX0(0.),
26  fY0(0.),
27  fZ0(0.),
28  fPhi(0.)
29  {}
30 
31  /// Initializing constructor.
32  ///
33  /// Arguments:
34  ///
35  /// x0, y0, z0 - Global coordinates of local origin.
36  /// phi - Rotation angle about x-axis.
37  ///
38  SurfYZPlane::SurfYZPlane(double x0, double y0, double z0, double phi) :
39  fX0(x0),
40  fY0(y0),
41  fZ0(z0),
42  fPhi(phi)
43  {}
44 
45  /// Destructor.
47  {}
48 
49  /// Clone method.
51  {
52  return new SurfYZPlane(*this);
53  }
54 
55  /// Surface-specific tests of validity of track parameters.
56  bool SurfYZPlane::isTrackValid(const TrackVector& vec) const
57  {
58  return true;
59  }
60 
61  /// Transform global to local coordinates.
62  ///
63  /// Arguments:
64  ///
65  /// xyz - Cartesian coordinates in global coordinate system.
66  /// uvw - Cartesian coordinates in local coordinate system.
67  ///
68  void SurfYZPlane::toLocal(const double xyz[3], double uvw[3]) const
69  {
70  double sinphi = std::sin(fPhi);
71  double cosphi = std::cos(fPhi);
72 
73  // u = x-x0
74  uvw[0] = xyz[0] - fX0;
75 
76  // v = (y-y0)*cos(phi) + (z-z0)*sin(phi)
77  uvw[1] = (xyz[1] - fY0) * cosphi + (xyz[2] - fZ0) * sinphi;
78 
79  // w = -(y-y0)*sin(phi) + (z-z0)*cos(phi)
80  uvw[2] = -(xyz[1] - fY0) * sinphi + (xyz[2] - fZ0) * cosphi;
81  }
82 
83  /// Transform local to global coordinates.
84  ///
85  /// Arguments:
86  ///
87  /// uvw - Cartesian coordinates in local coordinate system.
88  /// xyz - Cartesian coordinates in global coordinate system.
89  ///
90  void SurfYZPlane::toGlobal(const double uvw[3], double xyz[3]) const
91  {
92  double sinphi = std::sin(fPhi);
93  double cosphi = std::cos(fPhi);
94 
95  // x = x0 + u
96  xyz[0] = fX0 + uvw[0];
97 
98  // y = y0 + v*cos(phi) - w*sin(phi)
99  xyz[1] = fY0 + uvw[1] * cosphi - uvw[2] * sinphi;
100 
101  // z = z0 + v*sin(phi) + w*cos(phi)
102  xyz[2] = fZ0 + uvw[1] * sinphi + uvw[2] * cosphi;
103  }
104 
105  /// Get position of track.
106  ///
107  /// Arguments:
108  ///
109  /// vec - Track state vector.
110  /// xyz - Position in global coordinate system.
111  ///
112  void SurfYZPlane::getPosition(const TrackVector& vec, double xyz[3]) const
113  {
114  // Get position in local coordinate system.
115 
116  double uvw[3];
117  uvw[0] = vec(0);
118  uvw[1] = vec(1);
119  uvw[2] = 0.;
120 
121  // Transform to global coordinate system.
122 
123  toGlobal(uvw, xyz);
124  return;
125  }
126 
127  /// Get momentum vector of track.
128  ///
129  /// Arguments:
130  ///
131  /// vec - Track state vector.
132  /// mom - Momentum vector in global coordinate system.
133  /// dir - Track direction.
134  ///
135  void SurfYZPlane::getMomentum(const TrackVector& vec, double mom[3],
136  TrackDirection dir) const
137  {
138 
139  // Get momentum.
140 
141  double invp = std::abs(vec(4));
142  double p = 1. / std::max(invp, 1.e-3); // Capped at 1000. GeV/c.
143 
144  // Get track slope parameters.
145 
146  double dudw = vec(2);
147  double dvdw = vec(3);
148 
149  // Calculate dw/ds.
150 
151  double dwds = 1. / std::sqrt(1. + dudw*dudw + dvdw*dvdw);
152  TrackDirection realdir = getDirection(vec, dir); // Should be same as original direction.
153  if(realdir == BACKWARD)
154  dwds = -dwds;
155  else if(realdir != FORWARD)
156  throw cet::exception("SurfYZPlane") << "Track direction not specified.\n";
157 
158  // Calculate momentum vector in local coordinate system.
159 
160  double pu = p * dudw * dwds;
161  double pv = p * dvdw * dwds;
162  double pw = p * dwds;
163 
164  // Rotate momentum to global coordinte system.
165 
166  double sinphi = std::sin(fPhi);
167  double cosphi = std::cos(fPhi);
168 
169  mom[0] = pu;
170  mom[1] = pv * cosphi - pw * sinphi;
171  mom[2] = pv * sinphi + pw * cosphi;
172 
173  return;
174  }
175 
176  /// Test whether two surfaces are parallel, within tolerance.
177  /// This method will only return true if the other surface
178  /// is a SurfYZPlane.
179  ///
180  /// Arguments:
181  ///
182  /// surf - Other surface.
183  ///
184  /// Returned value: true if parallel.
185  ///
186  bool SurfYZPlane::isParallel(const Surface& surf) const
187  {
188  bool result = false;
189 
190  // Test if the other surface is a SurfYZPlane.
191 
192  const SurfYZPlane* psurf = dynamic_cast<const SurfYZPlane*>(&surf);
193  if(psurf != 0) {
194 
195  // Test whether surface angle parameters are the same
196  // within tolerance.
197 
198  double delta_phi = TVector2::Phi_mpi_pi(fPhi - psurf->phi());
199  if(std::abs(delta_phi) <= fPhiTolerance)
200  result = true;
201  }
202  return result;
203  }
204 
205  /// Find perpendicular forward distance to a parallel surface.
206  ///
207  /// Throw an exception if the other surface is not parallel.
208  ///
209  /// Assuming the other surface is parallel, the distance is
210  /// simply the w-coordinate of the other surface, and is signed.
211  ///
212  /// Arguments:
213  ///
214  /// surf - Other surface.
215  ///
216  /// Returned value: Distance.
217  ///
218  double SurfYZPlane::distanceTo(const Surface& surf) const
219  {
220  // Check if the other surface is parallel to this one.
221 
222  bool parallel = isParallel(surf);
223  if(!parallel)
224  throw cet::exception("SurfYZPlane") << "Attempt to find distance to non-parallel surface.\n";
225 
226  // Find the origin of the other surface in global coordinates,
227  // then convert to our local coordinates.
228 
229  double otheruvw[3] = {0., 0., 0.};
230  double xyz[3];
231  double myuvw[3];
232  surf.toGlobal(otheruvw, xyz);
233  toLocal(xyz, myuvw);
234 
235  // Distance is local w-coordinate of other surface origin.
236 
237  return myuvw[2];
238  }
239 
240  /// Test two surfaces for equality, within tolerance.
241  /// Here equal is defined as having all surface parameters the same,
242  /// not just having the surfaces coincide spatially, so that the
243  /// local coordinate systems are the same between the two surfaces.
244  ///
245  /// Arguments:
246  ///
247  /// surf - Other surface.
248  ///
249  /// Returned values: true if equal.
250  ///
251  bool SurfYZPlane::isEqual(const Surface& surf) const
252  {
253  bool result = false;
254 
255  // Test if the other surface is a SurfYZPlane.
256 
257  const SurfYZPlane* psurf = dynamic_cast<const SurfYZPlane*>(&surf);
258  if(psurf != 0) {
259 
260  // Test whether surface parameters are the same within tolerance.
261 
262  double delta_phi = TVector2::Phi_mpi_pi(fPhi - psurf->phi());
263  double dx = fX0 - psurf->x0();
264  double dy = fY0 - psurf->y0();
265  double dz = fZ0 - psurf->z0();
266  if(std::abs(delta_phi) <= fPhiTolerance &&
267  std::abs(dx) <= fSepTolerance &&
268  std::abs(dy) <= fSepTolerance &&
269  std::abs(dz) <= fSepTolerance)
270  result = true;
271  }
272  return result;
273  }
274 
275  /// Printout
276  std::ostream& SurfYZPlane::Print(std::ostream& out) const
277  {
278  out << "SurfYZPlane{ x0=" << fX0 << ", y0=" << fY0 << ", z0=" << fZ0 << ", phi=" << fPhi << "}";
279  return out;
280  }
281 
282 } // end namespace trkf
static double fSepTolerance
Separation tolerance for equal.
Definition: SurfYZPlane.h:101
TrackDirection
Track direction enum.
Definition: Surface.h:56
static QCString result
double fPhi
Rotation angle about x-axis.
Definition: SurfYZPlane.h:108
double z0() const
Z origin.
Definition: SurfYZPlane.h:62
double x0() const
X origin.
Definition: SurfYZPlane.h:60
Planar surface parallel to x-axis.
static double fPhiTolerance
Phi tolerance for parallel.
Definition: SurfYZPlane.h:100
virtual ~SurfYZPlane()
Destructor.
Definition: SurfYZPlane.cxx:46
virtual bool isParallel(const Surface &surf) const
Test whether two surfaces are parallel, within tolerance.
virtual double distanceTo(const Surface &surf) const
Find perpendicular forward distance to a parallel surface.
string dir
T abs(T value)
const double e
virtual void getMomentum(const TrackVector &vec, double mom[3], TrackDirection dir=UNKNOWN) const
Get momentum vector of track.
double y0() const
Y origin.
Definition: SurfYZPlane.h:61
virtual bool isTrackValid(const TrackVector &vec) const
Surface-specific tests of validity of track parameters.
Definition: SurfYZPlane.cxx:56
p
Definition: test.py:223
KVector< 5 >::type TrackVector
Track state vector, dimension 5.
static int max(int a, int b)
virtual void toLocal(const double xyz[3], double uvw[3]) const
Transform global to local coordinates.
Definition: SurfYZPlane.cxx:68
virtual std::ostream & Print(std::ostream &out) const
Printout.
virtual void toGlobal(const double uvw[3], double xyz[3]) const
Transform local to global coordinates.
Definition: SurfYZPlane.cxx:90
SurfYZPlane()
Default constructor.
Definition: SurfYZPlane.cxx:24
virtual bool isEqual(const Surface &surf) const
Test two surfaces for equality, within tolerance.
double phi() const
Rotation angle about x-axis.
Definition: SurfYZPlane.h:63
double fZ0
Z origin.
Definition: SurfYZPlane.h:107
virtual TrackDirection getDirection(const TrackVector &, TrackDirection dir=UNKNOWN) const
Get direction of track (default UNKNOWN).
Definition: Surface.h:85
virtual void getPosition(const TrackVector &vec, double xyz[3]) const
Get position of track.
virtual Surface * clone() const
Clone method.
Definition: SurfYZPlane.cxx:50
virtual void toGlobal(const double uvw[3], double xyz[3]) const =0
Transform local to global coordinates.
double fX0
X origin.
Definition: SurfYZPlane.h:105
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
double fY0
Y origin.
Definition: SurfYZPlane.h:106