Geant4  10.01.p02
ThreeVector.icc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id:$
3 // ---------------------------------------------------------------------------
4 //
5 // This file is a part of the CLHEP - a Class Library for High Energy Physics.
6 //
7 // This is the definitions of the inline member functions of the
8 // Hep3Vector class.
9 //
10 
11 #include <cmath>
12 
13 namespace CLHEP {
14 
15 // ------------------
16 // Access to elements
17 // ------------------
18 
19 // x, y, z
20 
21 inline double & Hep3Vector::operator[] (int i) { return operator()(i); }
22 inline double Hep3Vector::operator[] (int i) const { return operator()(i); }
23 
24 inline double Hep3Vector::x() const { return dx; }
25 inline double Hep3Vector::y() const { return dy; }
26 inline double Hep3Vector::z() const { return dz; }
27 
28 inline double Hep3Vector::getX() const { return dx; }
29 inline double Hep3Vector::getY() const { return dy; }
30 inline double Hep3Vector::getZ() const { return dz; }
31 
32 inline void Hep3Vector::setX(double x1) { dx = x1; }
33 inline void Hep3Vector::setY(double y1) { dy = y1; }
34 inline void Hep3Vector::setZ(double z1) { dz = z1; }
35 
36 inline void Hep3Vector::set(double x1, double y1, double z1) {
37  dx = x1;
38  dy = y1;
39  dz = z1;
40 }
41 
42 inline double Hep3Vector::operator () (int i) const {
43  switch(i) {
44  case X:
45  return x();
46  case Y:
47  return y();
48  case Z:
49  return z();
50  }
51  return 0.;
52 }
53 
54 inline double & Hep3Vector::operator () (int i) {
55  static double dummy;
56  switch(i) {
57  case X:
58  return dx;
59  case Y:
60  return dy;
61  case Z:
62  return dz;
63  }
64  return dummy;
65 }
66 
67 // --------------
68 // Global methods
69 // --------------
70 
71 inline Hep3Vector operator + (const Hep3Vector & a, const Hep3Vector & b) {
72  return Hep3Vector(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
73 }
74 
75 inline Hep3Vector operator - (const Hep3Vector & a, const Hep3Vector & b) {
76  return Hep3Vector(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
77 }
78 
79 inline Hep3Vector operator * (const Hep3Vector & p, double a) {
80  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
81 }
82 
83 inline Hep3Vector operator * (double a, const Hep3Vector & p) {
84  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
85 }
86 
87 inline double operator * (const Hep3Vector & a, const Hep3Vector & b) {
88  return a.dot(b);
89 }
90 
91 // --------------------------
92 // Set in various coordinates
93 // --------------------------
94 
95 inline void Hep3Vector::setRThetaPhi
96  ( double r1, double theta1, double phi1 ) {
97  setSpherical (r1, theta1, phi1);
98 }
99 
100 inline void Hep3Vector::setREtaPhi
101  ( double r1, double eta1, double phi1 ) {
102  setSpherical (r1, 2*std::atan(std::exp(-eta1)), phi1);
103 }
104 
105 inline void Hep3Vector::setRhoPhiZ
106  ( double rho1, double phi1, double z1) {
107  setCylindrical (rho1, phi1, z1);
108 }
109 
110 // ------------
111 // Constructors
112 // ------------
113 
114 inline Hep3Vector::Hep3Vector()
115  : dx(0.), dy(0.), dz(0.) {}
116 inline Hep3Vector::Hep3Vector(double x1)
117  : dx(x1), dy(0.), dz(0.) {}
118 inline Hep3Vector::Hep3Vector(double x1, double y1)
119  : dx(x1), dy(y1), dz(0.) {}
120 inline Hep3Vector::Hep3Vector(double x1, double y1, double z1)
121  : dx(x1), dy(y1), dz(z1) {}
122 
123 inline Hep3Vector::Hep3Vector(const Hep3Vector & p)
124 : dx(p.dx), dy(p.dy), dz(p.dz) {}
125 
126 inline Hep3Vector::~Hep3Vector() {}
127 
128 inline Hep3Vector & Hep3Vector::operator = (const Hep3Vector & p) {
129  dx = p.dx;
130  dy = p.dy;
131  dz = p.dz;
132  return *this;
133 }
134 
135 // ------------------
136 // Access to elements
137 // ------------------
138 
139 // r, theta, phi
140 
141 inline double Hep3Vector::mag2() const { return dx*dx + dy*dy + dz*dz; }
142 inline double Hep3Vector::mag() const { return std::sqrt(mag2()); }
143 inline double Hep3Vector::r() const { return mag(); }
144 
145 inline double Hep3Vector::theta() const {
146  return dx == 0.0 && dy == 0.0 && dz == 0.0 ? 0.0 : std::atan2(perp(),dz);
147 }
148 inline double Hep3Vector::phi() const {
149  return dx == 0.0 && dy == 0.0 ? 0.0 : std::atan2(dy,dx);
150 }
151 
152 inline double Hep3Vector::getR() const { return mag(); }
153 inline double Hep3Vector::getTheta() const { return theta(); }
154 inline double Hep3Vector::getPhi() const { return phi(); }
155 inline double Hep3Vector::angle() const { return theta(); }
156 
157 inline double Hep3Vector::cosTheta() const {
158  double ptot = mag();
159  return ptot == 0.0 ? 1.0 : dz/ptot;
160 }
161 
162 inline double Hep3Vector::cos2Theta() const {
163  double ptot2 = mag2();
164  return ptot2 == 0.0 ? 1.0 : dz*dz/ptot2;
165 }
166 
167 inline void Hep3Vector::setR(double r1) { setMag(r1); }
168 
169 inline void Hep3Vector::setTheta(double th) {
170  double ma = mag();
171  double ph = phi();
172  setX(ma*std::sin(th)*std::cos(ph));
173  setY(ma*std::sin(th)*std::sin(ph));
174  setZ(ma*std::cos(th));
175 }
176 
177 inline void Hep3Vector::setPhi(double ph) {
178  double xy = perp();
179  setX(xy*std::cos(ph));
180  setY(xy*std::sin(ph));
181 }
182 
183 // perp, eta,
184 
185 inline double Hep3Vector::perp2() const { return dx*dx + dy*dy; }
186 inline double Hep3Vector::perp() const { return std::sqrt(perp2()); }
187 inline double Hep3Vector::rho() const { return perp(); }
188 inline double Hep3Vector::eta() const { return pseudoRapidity();}
189 
190 inline double Hep3Vector::getRho() const { return perp(); }
191 inline double Hep3Vector::getEta() const { return pseudoRapidity();}
192 
193 inline void Hep3Vector::setPerp(double r1) {
194  double p = perp();
195  if (p != 0.0) {
196  dx *= r1/p;
197  dy *= r1/p;
198  }
199 }
200 inline void Hep3Vector::setRho(double rho1) { setPerp (rho1); }
201 
202 // ----------
203 // Comparison
204 // ----------
205 
206 inline bool Hep3Vector::operator == (const Hep3Vector& v) const {
207  return (v.x()==x() && v.y()==y() && v.z()==z()) ? true : false;
208 }
209 
210 inline bool Hep3Vector::operator != (const Hep3Vector& v) const {
211  return (v.x()!=x() || v.y()!=y() || v.z()!=z()) ? true : false;
212 }
213 
214 inline double Hep3Vector::getTolerance () {
215  return tolerance;
216 }
217 
218 // ----------
219 // Arithmetic
220 // ----------
221 
222 inline Hep3Vector& Hep3Vector::operator += (const Hep3Vector & p) {
223  dx += p.x();
224  dy += p.y();
225  dz += p.z();
226  return *this;
227 }
228 
229 inline Hep3Vector& Hep3Vector::operator -= (const Hep3Vector & p) {
230  dx -= p.x();
231  dy -= p.y();
232  dz -= p.z();
233  return *this;
234 }
235 
236 inline Hep3Vector Hep3Vector::operator - () const {
237  return Hep3Vector(-dx, -dy, -dz);
238 }
239 
240 inline Hep3Vector& Hep3Vector::operator *= (double a) {
241  dx *= a;
242  dy *= a;
243  dz *= a;
244  return *this;
245 }
246 
247 // -------------------
248 // Combine two Vectors
249 // -------------------
250 
251 inline double Hep3Vector::diff2(const Hep3Vector & p) const {
252  return (*this-p).mag2();
253 }
254 
255 inline double Hep3Vector::dot(const Hep3Vector & p) const {
256  return dx*p.x() + dy*p.y() + dz*p.z();
257 }
258 
259 inline Hep3Vector Hep3Vector::cross(const Hep3Vector & p) const {
260  return Hep3Vector(dy*p.z()-p.y()*dz, dz*p.x()-p.z()*dx, dx*p.y()-p.x()*dy);
261 }
262 
263 inline double Hep3Vector::perp2(const Hep3Vector & p) const {
264  double tot = p.mag2();
265  double ss = dot(p);
266  return tot > 0.0 ? mag2()-ss*ss/tot : mag2();
267 }
268 
269 inline double Hep3Vector::perp(const Hep3Vector & p) const {
270  return std::sqrt(perp2(p));
271 }
272 
273 inline Hep3Vector Hep3Vector::perpPart () const {
274  return Hep3Vector (dx, dy, 0);
275 }
276 inline Hep3Vector Hep3Vector::project () const {
277  return Hep3Vector (0, 0, dz);
278 }
279 
280 inline Hep3Vector Hep3Vector::perpPart (const Hep3Vector & v2) const {
281  return ( *this - project(v2) );
282 }
283 
284 inline double Hep3Vector::angle(const Hep3Vector & q) const {
285  return std::acos(cosTheta(q));
286 }
287 
288 inline double Hep3Vector::theta(const Hep3Vector & q) const {
289  return angle(q);
290 }
291 
292 inline double Hep3Vector::azimAngle(const Hep3Vector & v2) const {
293  return deltaPhi(v2);
294 }
295 
296 // ----------
297 // Properties
298 // ----------
299 
300 inline Hep3Vector Hep3Vector::unit() const {
301  double tot = mag2();
302  Hep3Vector p(x(),y(),z());
303  return tot > 0.0 ? p *= (1.0/std::sqrt(tot)) : p;
304 }
305 
306 inline Hep3Vector Hep3Vector::orthogonal() const {
307  double xx = dx < 0.0 ? -dx : dx;
308  double yy = dy < 0.0 ? -dy : dy;
309  double zz = dz < 0.0 ? -dz : dz;
310  if (xx < yy) {
311  return xx < zz ? Hep3Vector(0,dz,-dy) : Hep3Vector(dy,-dx,0);
312  }else{
313  return yy < zz ? Hep3Vector(-dz,0,dx) : Hep3Vector(dy,-dx,0);
314  }
315 }
316 
317 } // namespace CLHEP