ThreeVector.icc 7.79 KB
Newer Older
1
// -*- C++ -*-
Lynn Garren's avatar
Lynn Garren committed
2
// $Id: ThreeVector.icc,v 1.2 2010/06/16 17:15:57 garren Exp $
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// ---------------------------------------------------------------------------
//
// This file is a part of the CLHEP - a Class Library for High Energy Physics.
// 
// This is the definitions of the inline member functions of the
// Hep3Vector class.
//

#include <cmath>

namespace CLHEP {

// ------------------
// Access to elements
// ------------------

// x, y, z

inline double & Hep3Vector::operator[] (int i)       { return operator()(i); }
inline double   Hep3Vector::operator[] (int i) const { return operator()(i); }

inline double Hep3Vector::x() const { return dx; }
inline double Hep3Vector::y() const { return dy; }
inline double Hep3Vector::z() const { return dz; }

inline double Hep3Vector::getX() const { return dx; }
inline double Hep3Vector::getY() const { return dy; }
inline double Hep3Vector::getZ() const { return dz; }

32
33
34
inline void Hep3Vector::setX(double x1) { dx = x1; }
inline void Hep3Vector::setY(double y1) { dy = y1; }
inline void Hep3Vector::setZ(double z1) { dz = z1; }
35

36
37
38
39
inline void Hep3Vector::set(double x1, double y1, double z1) { 
  dx = x1; 
  dy = y1; 
  dz = z1; 
40
41
}

42
43
44
45
46
47
48
49
inline double Hep3Vector::operator () (int i) const {
  switch(i) {
  case X:
    return x();
  case Y:
    return y();
  case Z:
    return z();
50
51
  default:
    return 0.;
52
53
54
55
56
57
58
59
60
61
62
63
  }
}

inline double & Hep3Vector::operator () (int i) {
  static double dummy;
  switch(i) {
  case X:
    return dx;
  case Y:
    return dy;
  case Z:
    return dz;
64
65
  default:
    return dummy;
66
67
68
  }
}

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// --------------
// Global methods
// --------------

inline Hep3Vector operator + (const Hep3Vector & a, const Hep3Vector & b) {
  return Hep3Vector(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
}

inline Hep3Vector operator - (const Hep3Vector & a, const Hep3Vector & b) {
  return Hep3Vector(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
}

inline Hep3Vector operator * (const Hep3Vector & p, double a) {
  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
}

inline Hep3Vector operator * (double a, const Hep3Vector & p) {
  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
}

inline double operator * (const Hep3Vector & a, const Hep3Vector & b) {
  return a.dot(b);
}

// --------------------------
// Set in various coordinates
// --------------------------

inline void Hep3Vector::setRThetaPhi
98
99
		  ( double r1, double theta1, double phi1 ) {
  setSpherical (r1, theta1, phi1); 
100
101
102
}

inline void Hep3Vector::setREtaPhi
103
104
		  ( double r1, double eta1,  double phi1 ) {
  setSpherical (r1, 2*std::atan(std::exp(-eta1)), phi1); 
105
106
107
}

inline void Hep3Vector::setRhoPhiZ
108
109
		  ( double rho1, double phi1, double z1) {
  setCylindrical (rho1, phi1, z1); 
110
111
112
113
114
115
}

// ------------
// Constructors
// ------------

Lynn Garren's avatar
Lynn Garren committed
116
117
inline Hep3Vector::Hep3Vector()
  : dx(0.), dy(0.), dz(0.) {}
118
119
120
121
122
123
inline Hep3Vector::Hep3Vector(double x1)
  : dx(x1), dy(0.), dz(0.) {}
inline Hep3Vector::Hep3Vector(double x1, double y1)
  : dx(x1), dy(y1), dz(0.) {}
inline Hep3Vector::Hep3Vector(double x1, double y1, double z1)
  : dx(x1), dy(y1), dz(z1) {}
124
125
126
127
128
129
130

inline Hep3Vector::Hep3Vector(const Hep3Vector & p)
: dx(p.dx), dy(p.dy), dz(p.dz) {}

inline Hep3Vector::~Hep3Vector() {}

inline Hep3Vector & Hep3Vector::operator = (const Hep3Vector & p) {
131
  set(p.x(), p.y(), p.z());
132
133
134
135
136
137
138
139
140
  return *this;
}

// ------------------
// Access to elements
// ------------------

// r, theta, phi

141
inline double Hep3Vector::mag2() const { return x()*x() + y()*y() + z()*z(); }
142
143
144
145
inline double Hep3Vector::mag()  const { return std::sqrt(mag2()); }
inline double Hep3Vector::r()    const { return mag(); }

inline double Hep3Vector::theta() 	const {
146
  return x() == 0.0 && y() == 0.0 && z() == 0.0 ? 0.0 : std::atan2(perp(),z());
147
148
}
inline double Hep3Vector::phi() const {
149
  return x() == 0.0 && y() == 0.0 ? 0.0 : std::atan2(y(),x());
150
151
152
153
154
155
156
157
158
}

inline double Hep3Vector::getR()     const { return mag();   }
inline double Hep3Vector::getTheta() const { return theta(); }
inline double Hep3Vector::getPhi()   const { return phi();   }
inline double Hep3Vector::angle()    const { return theta(); }

inline double Hep3Vector::cosTheta() const {
  double ptot = mag();
159
  return ptot == 0.0 ? 1.0 : z()/ptot;
160
161
162
163
}

inline double Hep3Vector::cos2Theta() const {
  double ptot2 = mag2();
164
  return ptot2 == 0.0 ? 1.0 : z()*z()/ptot2;
165
166
}

167
inline void Hep3Vector::setR(double r1) { setMag(r1); }
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

inline void Hep3Vector::setTheta(double th) {
  double ma   = mag();
  double ph   = phi();
  setX(ma*std::sin(th)*std::cos(ph));
  setY(ma*std::sin(th)*std::sin(ph));
  setZ(ma*std::cos(th));
}

inline void Hep3Vector::setPhi(double ph) {
  double xy   = perp();
  setX(xy*std::cos(ph));
  setY(xy*std::sin(ph));
}

// perp, eta, 

185
inline double Hep3Vector::perp2()  const { return x()*x() + y()*y(); }
186
187
188
189
190
191
192
inline double Hep3Vector::perp()   const { return std::sqrt(perp2()); }
inline double Hep3Vector::rho()    const { return perp();  }
inline double Hep3Vector::eta()    const { return pseudoRapidity();}

inline double Hep3Vector::getRho() const { return perp();  }
inline double Hep3Vector::getEta() const { return pseudoRapidity();}

193
inline void Hep3Vector::setPerp(double r1) {
194
195
  double p = perp();
  if (p != 0.0) {
196
197
    dx *= r1/p;
    dy *= r1/p;
198
199
  }
}
200
inline void Hep3Vector::setRho(double rho1) { setPerp (rho1); }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

// ----------
// Comparison
// ----------

inline bool Hep3Vector::operator == (const Hep3Vector& v) const {
  return (v.x()==x() && v.y()==y() && v.z()==z()) ? true : false;
}

inline bool Hep3Vector::operator != (const Hep3Vector& v) const {
  return (v.x()!=x() || v.y()!=y() || v.z()!=z()) ? true : false;
}

inline double Hep3Vector::getTolerance () {
  return tolerance;
}

// ----------
// Arithmetic
// ----------

inline Hep3Vector& Hep3Vector::operator += (const Hep3Vector & p) {
  dx += p.x();
  dy += p.y();
  dz += p.z();
  return *this;
}

inline Hep3Vector& Hep3Vector::operator -= (const Hep3Vector & p) {
  dx -= p.x();
  dy -= p.y();
  dz -= p.z();
  return *this;
}

inline Hep3Vector Hep3Vector::operator - () const {
237
  return Hep3Vector(-x(), -y(), -z());
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
}

inline Hep3Vector& Hep3Vector::operator *= (double a) {
  dx *= a;
  dy *= a;
  dz *= a;
  return *this;
}

// -------------------
// Combine two Vectors
// -------------------

inline double Hep3Vector::diff2(const Hep3Vector & p) const {
  return (*this-p).mag2();
}

inline double Hep3Vector::dot(const Hep3Vector & p) const {
256
  return x()*p.x() + y()*p.y() + z()*p.z();
257
258
259
}

inline Hep3Vector Hep3Vector::cross(const Hep3Vector & p) const {
260
  return Hep3Vector(y()*p.z()-p.y()*z(), z()*p.x()-p.z()*x(), x()*p.y()-p.x()*y());
261
262
263
264
265
266
267
268
269
270
271
272
273
}

inline double Hep3Vector::perp2(const Hep3Vector & p)  const {
  double tot = p.mag2();
  double ss  = dot(p);
  return tot > 0.0 ? mag2()-ss*ss/tot : mag2();
}

inline double Hep3Vector::perp(const Hep3Vector & p) const {
  return std::sqrt(perp2(p));
}

inline Hep3Vector Hep3Vector::perpPart () const {
274
  return Hep3Vector (x(), y(), 0);
275
276
}
inline Hep3Vector Hep3Vector::project () const {
277
  return Hep3Vector (0, 0, z());
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
}

inline Hep3Vector Hep3Vector::perpPart (const Hep3Vector & v2) const {
  return ( *this - project(v2) );
}

inline double Hep3Vector::angle(const Hep3Vector & q) const {
  return std::acos(cosTheta(q));
}

inline double Hep3Vector::theta(const Hep3Vector & q) const { 
  return angle(q); 
}

inline double Hep3Vector::azimAngle(const Hep3Vector & v2) const { 
  return deltaPhi(v2); 
}

// ----------
// Properties
// ----------

inline Hep3Vector Hep3Vector::unit() const {
  double  tot = mag2();
  Hep3Vector p(x(),y(),z());
  return tot > 0.0 ? p *= (1.0/std::sqrt(tot)) : p;
}

inline Hep3Vector Hep3Vector::orthogonal() const {
307
308
309
  double xx = x() < 0.0 ? -x() : x();
  double yy = y() < 0.0 ? -y() : y();
  double zz = z() < 0.0 ? -z() : z();
310
  if (xx < yy) {
311
    return xx < zz ? Hep3Vector(0,z(),-y()) : Hep3Vector(y(),-x(),0);
312
  }else{
313
    return yy < zz ? Hep3Vector(-z(),0,x()) : Hep3Vector(y(),-x(),0);
314
315
316
317
  }
}

}  // namespace CLHEP