source: trunk/CLHEP/Vector/ThreeVector.icc@ 10

Last change on this file since 10 was 4, checked in by Pavel Demin, 16 years ago

first commit

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