CLHEP  2.4.7.2
C++ Class Library for High Energy Physics
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 
12 namespace CLHEP {
13 
14 // ------------------
15 // Access to elements
16 // ------------------
17 
18 // x, y, z
19 
20 inline double & Hep3Vector::operator[] (int i) { return data[i]; }
21 inline double Hep3Vector::operator[] (int i) const { return data[i]; }
22 
23 inline double Hep3Vector::x() const { return (*this)[X]; }
24 inline double Hep3Vector::y() const { return (*this)[Y]; }
25 inline double Hep3Vector::z() const { return (*this)[Z]; }
26 
27 inline double Hep3Vector::getX() const { return (*this)[X]; }
28 inline double Hep3Vector::getY() const { return (*this)[Y]; }
29 inline double Hep3Vector::getZ() const { return (*this)[Z]; }
30 
31 inline void Hep3Vector::setX(double x) { (*this)[X] = x; }
32 inline void Hep3Vector::setY(double y) { (*this)[Y] = y; }
33 inline void Hep3Vector::setZ(double z) { (*this)[Z] = z; }
34 
35 inline void Hep3Vector::set(double x, double y, double z) {
36  (*this)[X] = x;
37  (*this)[Y] = y;
38  (*this)[Z] = z;
39 }
40 
41 inline double Hep3Vector::operator () (int i) const {
42  return data[i];
43 }
44 
45 inline double & Hep3Vector::operator () (int i) {
46  return data[i];
47 }
48 
49 // --------------
50 // Global methods
51 // --------------
52 
53 inline 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 
57 inline 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 
61 inline Hep3Vector operator * (const Hep3Vector & p, double a) {
62  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
63 }
64 
65 inline Hep3Vector operator * (double a, const Hep3Vector & p) {
66  return Hep3Vector(a*p.x(), a*p.y(), a*p.z());
67 }
68 
69 inline double operator * (const Hep3Vector & a, const Hep3Vector & b) {
70  return a.dot(b);
71 }
72 
73 // --------------------------
74 // Set in various coordinates
75 // --------------------------
76 
77 inline void Hep3Vector::setRThetaPhi
78  ( double r1, double theta1, double phi1 ) {
79  setSpherical (r1, theta1, phi1);
80 }
81 
82 inline void Hep3Vector::setREtaPhi
83  ( double r1, double eta1, double phi1 ) {
84  setSpherical (r1, 2*std::atan(std::exp(-eta1)), phi1);
85 }
86 
87 inline void Hep3Vector::setRhoPhiZ
88  ( double rho1, double phi1, double z1) {
89  setCylindrical (rho1, phi1, z1);
90 }
91 
92 // ------------
93 // Constructors
94 // ------------
95 
96 inline Hep3Vector::Hep3Vector()
97  : data{0.0, 0.0, 0.0} {}
98 inline Hep3Vector::Hep3Vector(double x)
99  : data{ x , 0.0, 0.0} {}
100 inline Hep3Vector::Hep3Vector(double x, double y)
101  : data{ x , y , 0.0} {}
102 inline Hep3Vector::Hep3Vector(double x, double y, double z)
103  : data{ x , y , z } {}
104 
105 inline Hep3Vector::Hep3Vector(const Hep3Vector & p)
106  : data{p.x(), p.y(), p.z()} {}
107 
108 inline Hep3Vector::~Hep3Vector() {}
109 
110 inline 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 
121 inline double Hep3Vector::mag2() const { return x()*x() + y()*y() + z()*z(); }
122 inline double Hep3Vector::mag() const { return std::sqrt(mag2()); }
123 inline double Hep3Vector::r() const { return mag(); }
124 
125 inline double Hep3Vector::theta() const {
126  return x() == 0.0 && y() == 0.0 && z() == 0.0 ? 0.0 : std::atan2(perp(),z());
127 }
128 inline double Hep3Vector::phi() const {
129  return x() == 0.0 && y() == 0.0 ? 0.0 : std::atan2(y(),x());
130 }
131 
132 inline double Hep3Vector::getR() const { return mag(); }
133 inline double Hep3Vector::getTheta() const { return theta(); }
134 inline double Hep3Vector::getPhi() const { return phi(); }
135 inline double Hep3Vector::angle() const { return theta(); }
136 
137 inline double Hep3Vector::cosTheta() const {
138  double ptot = mag();
139  return ptot == 0.0 ? 1.0 : z()/ptot;
140 }
141 
142 inline double Hep3Vector::cos2Theta() const {
143  double ptot2 = mag2();
144  return ptot2 == 0.0 ? 1.0 : z()*z()/ptot2;
145 }
146 
147 inline void Hep3Vector::setR(double r1) { setMag(r1); }
148 
149 inline 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 
157 inline 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 
165 inline double Hep3Vector::perp2() const { return x()*x() + y()*y(); }
166 inline double Hep3Vector::perp() const { return std::sqrt(perp2()); }
167 inline double Hep3Vector::rho() const { return perp(); }
168 inline double Hep3Vector::eta() const { return pseudoRapidity();}
169 
170 inline double Hep3Vector::getRho() const { return perp(); }
171 inline double Hep3Vector::getEta() const { return pseudoRapidity();}
172 
173 inline 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 }
180 inline void Hep3Vector::setRho(double rho1) { setPerp (rho1); }
181 
182 // ----------
183 // Comparison
184 // ----------
185 
186 inline bool Hep3Vector::operator == (const Hep3Vector& v) const {
187  return (v.x()==x() && v.y()==y() && v.z()==z()) ? true : false;
188 }
189 
190 inline bool Hep3Vector::operator != (const Hep3Vector& v) const {
191  return (v.x()!=x() || v.y()!=y() || v.z()!=z()) ? true : false;
192 }
193 
194 inline double Hep3Vector::getTolerance () {
195  return tolerance;
196 }
197 
198 // ----------
199 // Arithmetic
200 // ----------
201 
202 inline 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 
209 inline 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 
216 inline Hep3Vector Hep3Vector::operator - () const {
217  return Hep3Vector(-x(), -y(), -z());
218 }
219 
220 inline 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 
231 inline double Hep3Vector::diff2(const Hep3Vector & p) const {
232  return (*this-p).mag2();
233 }
234 
235 inline double Hep3Vector::dot(const Hep3Vector & p) const {
236  return x()*p.x() + y()*p.y() + z()*p.z();
237 }
238 
239 inline 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 
243 inline 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 
249 inline double Hep3Vector::perp(const Hep3Vector & p) const {
250  return std::sqrt(perp2(p));
251 }
252 
253 inline Hep3Vector Hep3Vector::perpPart () const {
254  return Hep3Vector (x(), y(), 0);
255 }
256 inline Hep3Vector Hep3Vector::project () const {
257  return Hep3Vector (0, 0, z());
258 }
259 
260 inline Hep3Vector Hep3Vector::perpPart (const Hep3Vector & v2) const {
261  return ( *this - project(v2) );
262 }
263 
264 inline double Hep3Vector::angle(const Hep3Vector & q) const {
265  return std::acos(cosTheta(q));
266 }
267 
268 inline double Hep3Vector::theta(const Hep3Vector & q) const {
269  return angle(q);
270 }
271 
272 inline double Hep3Vector::azimAngle(const Hep3Vector & v2) const {
273  return deltaPhi(v2);
274 }
275 
276 // ----------
277 // Properties
278 // ----------
279 
280 inline 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 
286 inline 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
HepLorentzVector operator*(const HepLorentzVector &p, double a)
Hep3Vector operator-(const Hep3Vector &a, const Hep3Vector &b)
Definition: ThreeVector.icc:57
std::complex< double > dot(const SphericalHarmonicCoefficientSet &a, const SphericalHarmonicCoefficientSet &b)
Hep3Vector operator+(const Hep3Vector &a, const Hep3Vector &b)
Definition: ThreeVector.icc:53