Geant4  10.00.p01
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 // --------------
43 // Global methods
44 // --------------
45 
46 inline Hep3Vector operator + (const Hep3Vector & a, const Hep3Vector & b) {
47  return Hep3Vector(a.x() + b.x(), a.y() + b.y(), a.z() + b.z());
48 }
49 
50 inline Hep3Vector operator - (const Hep3Vector & a, const Hep3Vector & b) {
51  return Hep3Vector(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());
52 }
53 
54 inline Hep3Vector operator * (const Hep3Vector & p, double a) {
55  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
56 }
57 
58 inline Hep3Vector operator * (double a, const Hep3Vector & p) {
59  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
60 }
61 
62 inline double operator * (const Hep3Vector & a, const Hep3Vector & b) {
63  return a.dot(b);
64 }
65 
66 // --------------------------
67 // Set in various coordinates
68 // --------------------------
69 
70 inline void Hep3Vector::setRThetaPhi
71  ( double r1, double theta1, double phi1 ) {
72  setSpherical (r1, theta1, phi1);
73 }
74 
75 inline void Hep3Vector::setREtaPhi
76  ( double r1, double eta1, double phi1 ) {
77  setSpherical (r1, 2*std::atan(std::exp(-eta1)), phi1);
78 }
79 
80 inline void Hep3Vector::setRhoPhiZ
81  ( double rho1, double phi1, double z1) {
82  setCylindrical (rho1, phi1, z1);
83 }
84 
85 // ------------
86 // Constructors
87 // ------------
88 
89 inline Hep3Vector::Hep3Vector()
90  : dx(0.), dy(0.), dz(0.) {}
91 inline Hep3Vector::Hep3Vector(double x1)
92  : dx(x1), dy(0.), dz(0.) {}
93 inline Hep3Vector::Hep3Vector(double x1, double y1)
94  : dx(x1), dy(y1), dz(0.) {}
95 inline Hep3Vector::Hep3Vector(double x1, double y1, double z1)
96  : dx(x1), dy(y1), dz(z1) {}
97 
98 inline Hep3Vector::Hep3Vector(const Hep3Vector & p)
99 : dx(p.dx), dy(p.dy), dz(p.dz) {}
100 
101 inline Hep3Vector::~Hep3Vector() {}
102 
103 inline Hep3Vector & Hep3Vector::operator = (const Hep3Vector & p) {
104  dx = p.dx;
105  dy = p.dy;
106  dz = p.dz;
107  return *this;
108 }
109 
110 // ------------------
111 // Access to elements
112 // ------------------
113 
114 // r, theta, phi
115 
116 inline double Hep3Vector::mag2() const { return dx*dx + dy*dy + dz*dz; }
117 inline double Hep3Vector::mag() const { return std::sqrt(mag2()); }
118 inline double Hep3Vector::r() const { return mag(); }
119 
120 inline double Hep3Vector::theta() const {
121  return dx == 0.0 && dy == 0.0 && dz == 0.0 ? 0.0 : std::atan2(perp(),dz);
122 }
123 inline double Hep3Vector::phi() const {
124  return dx == 0.0 && dy == 0.0 ? 0.0 : std::atan2(dy,dx);
125 }
126 
127 inline double Hep3Vector::getR() const { return mag(); }
128 inline double Hep3Vector::getTheta() const { return theta(); }
129 inline double Hep3Vector::getPhi() const { return phi(); }
130 inline double Hep3Vector::angle() const { return theta(); }
131 
132 inline double Hep3Vector::cosTheta() const {
133  double ptot = mag();
134  return ptot == 0.0 ? 1.0 : dz/ptot;
135 }
136 
137 inline double Hep3Vector::cos2Theta() const {
138  double ptot2 = mag2();
139  return ptot2 == 0.0 ? 1.0 : dz*dz/ptot2;
140 }
141 
142 inline void Hep3Vector::setR(double r1) { setMag(r1); }
143 
144 inline void Hep3Vector::setTheta(double th) {
145  double ma = mag();
146  double ph = phi();
147  setX(ma*std::sin(th)*std::cos(ph));
148  setY(ma*std::sin(th)*std::sin(ph));
149  setZ(ma*std::cos(th));
150 }
151 
152 inline void Hep3Vector::setPhi(double ph) {
153  double xy = perp();
154  setX(xy*std::cos(ph));
155  setY(xy*std::sin(ph));
156 }
157 
158 // perp, eta,
159 
160 inline double Hep3Vector::perp2() const { return dx*dx + dy*dy; }
161 inline double Hep3Vector::perp() const { return std::sqrt(perp2()); }
162 inline double Hep3Vector::rho() const { return perp(); }
163 inline double Hep3Vector::eta() const { return pseudoRapidity();}
164 
165 inline double Hep3Vector::getRho() const { return perp(); }
166 inline double Hep3Vector::getEta() const { return pseudoRapidity();}
167 
168 inline void Hep3Vector::setPerp(double r1) {
169  double p = perp();
170  if (p != 0.0) {
171  dx *= r1/p;
172  dy *= r1/p;
173  }
174 }
175 inline void Hep3Vector::setRho(double rho1) { setPerp (rho1); }
176 
177 // ----------
178 // Comparison
179 // ----------
180 
181 inline bool Hep3Vector::operator == (const Hep3Vector& v) const {
182  return (v.x()==x() && v.y()==y() && v.z()==z()) ? true : false;
183 }
184 
185 inline bool Hep3Vector::operator != (const Hep3Vector& v) const {
186  return (v.x()!=x() || v.y()!=y() || v.z()!=z()) ? true : false;
187 }
188 
189 inline double Hep3Vector::getTolerance () {
190  return tolerance;
191 }
192 
193 // ----------
194 // Arithmetic
195 // ----------
196 
197 inline Hep3Vector& Hep3Vector::operator += (const Hep3Vector & p) {
198  dx += p.x();
199  dy += p.y();
200  dz += p.z();
201  return *this;
202 }
203 
204 inline Hep3Vector& Hep3Vector::operator -= (const Hep3Vector & p) {
205  dx -= p.x();
206  dy -= p.y();
207  dz -= p.z();
208  return *this;
209 }
210 
211 inline Hep3Vector Hep3Vector::operator - () const {
212  return Hep3Vector(-dx, -dy, -dz);
213 }
214 
215 inline Hep3Vector& Hep3Vector::operator *= (double a) {
216  dx *= a;
217  dy *= a;
218  dz *= a;
219  return *this;
220 }
221 
222 // -------------------
223 // Combine two Vectors
224 // -------------------
225 
226 inline double Hep3Vector::diff2(const Hep3Vector & p) const {
227  return (*this-p).mag2();
228 }
229 
230 inline double Hep3Vector::dot(const Hep3Vector & p) const {
231  return dx*p.x() + dy*p.y() + dz*p.z();
232 }
233 
234 inline Hep3Vector Hep3Vector::cross(const Hep3Vector & p) const {
235  return Hep3Vector(dy*p.z()-p.y()*dz, dz*p.x()-p.z()*dx, dx*p.y()-p.x()*dy);
236 }
237 
238 inline double Hep3Vector::perp2(const Hep3Vector & p) const {
239  double tot = p.mag2();
240  double ss = dot(p);
241  return tot > 0.0 ? mag2()-ss*ss/tot : mag2();
242 }
243 
244 inline double Hep3Vector::perp(const Hep3Vector & p) const {
245  return std::sqrt(perp2(p));
246 }
247 
248 inline Hep3Vector Hep3Vector::perpPart () const {
249  return Hep3Vector (dx, dy, 0);
250 }
251 inline Hep3Vector Hep3Vector::project () const {
252  return Hep3Vector (0, 0, dz);
253 }
254 
255 inline Hep3Vector Hep3Vector::perpPart (const Hep3Vector & v2) const {
256  return ( *this - project(v2) );
257 }
258 
259 inline double Hep3Vector::angle(const Hep3Vector & q) const {
260  return std::acos(cosTheta(q));
261 }
262 
263 inline double Hep3Vector::theta(const Hep3Vector & q) const {
264  return angle(q);
265 }
266 
267 inline double Hep3Vector::azimAngle(const Hep3Vector & v2) const {
268  return deltaPhi(v2);
269 }
270 
271 // ----------
272 // Properties
273 // ----------
274 
275 inline Hep3Vector Hep3Vector::unit() const {
276  double tot = mag2();
277  Hep3Vector p(x(),y(),z());
278  return tot > 0.0 ? p *= (1.0/std::sqrt(tot)) : p;
279 }
280 
281 inline Hep3Vector Hep3Vector::orthogonal() const {
282  double xx = dx < 0.0 ? -dx : dx;
283  double yy = dy < 0.0 ? -dy : dy;
284  double zz = dz < 0.0 ? -dz : dz;
285  if (xx < yy) {
286  return xx < zz ? Hep3Vector(0,dz,-dy) : Hep3Vector(dy,-dx,0);
287  }else{
288  return yy < zz ? Hep3Vector(-dz,0,dx) : Hep3Vector(dy,-dx,0);
289  }
290 }
291 
292 } // namespace CLHEP