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