[4] | 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 |
|
---|
| 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 x) { dx = x; }
|
---|
| 33 | inline void Hep3Vector::setY(double y) { dy = y; }
|
---|
| 34 | inline void Hep3Vector::setZ(double z) { dz = z; }
|
---|
| 35 |
|
---|
| 36 | inline 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 |
|
---|
| 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 r, double theta, double phi ) {
|
---|
| 72 | setSpherical (r, theta, phi);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | inline void Hep3Vector::setREtaPhi
|
---|
| 76 | ( double r, double eta, double phi ) {
|
---|
| 77 | setSpherical (r, 2*std::atan(std::exp(-eta)), phi);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | inline void Hep3Vector::setRhoPhiZ
|
---|
| 81 | ( double rho, double phi, double z) {
|
---|
| 82 | setCylindrical (rho, phi, z);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // ------------
|
---|
| 86 | // Constructors
|
---|
| 87 | // ------------
|
---|
| 88 |
|
---|
| 89 | inline Hep3Vector::Hep3Vector(double x, double y, double z)
|
---|
| 90 | : dx(x), dy(y), dz(z) {}
|
---|
| 91 |
|
---|
| 92 | inline Hep3Vector::Hep3Vector(const Hep3Vector & p)
|
---|
| 93 | : dx(p.dx), dy(p.dy), dz(p.dz) {}
|
---|
| 94 |
|
---|
| 95 | inline Hep3Vector::~Hep3Vector() {}
|
---|
| 96 |
|
---|
| 97 | inline 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 |
|
---|
| 110 | inline double Hep3Vector::mag2() const { return dx*dx + dy*dy + dz*dz; }
|
---|
| 111 | inline double Hep3Vector::mag() const { return std::sqrt(mag2()); }
|
---|
| 112 | inline double Hep3Vector::r() const { return mag(); }
|
---|
| 113 |
|
---|
| 114 | inline double Hep3Vector::theta() const {
|
---|
| 115 | return dx == 0.0 && dy == 0.0 && dz == 0.0 ? 0.0 : std::atan2(perp(),dz);
|
---|
| 116 | }
|
---|
| 117 | inline double Hep3Vector::phi() const {
|
---|
| 118 | return dx == 0.0 && dy == 0.0 ? 0.0 : std::atan2(dy,dx);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | inline double Hep3Vector::getR() const { return mag(); }
|
---|
| 122 | inline double Hep3Vector::getTheta() const { return theta(); }
|
---|
| 123 | inline double Hep3Vector::getPhi() const { return phi(); }
|
---|
| 124 | inline double Hep3Vector::angle() const { return theta(); }
|
---|
| 125 |
|
---|
| 126 | inline double Hep3Vector::cosTheta() const {
|
---|
| 127 | double ptot = mag();
|
---|
| 128 | return ptot == 0.0 ? 1.0 : dz/ptot;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | inline double Hep3Vector::cos2Theta() const {
|
---|
| 132 | double ptot2 = mag2();
|
---|
| 133 | return ptot2 == 0.0 ? 1.0 : dz*dz/ptot2;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | inline void Hep3Vector::setR(double r) { setMag(r); }
|
---|
| 137 |
|
---|
| 138 | inline 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 |
|
---|
| 146 | inline 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 |
|
---|
| 154 | inline double Hep3Vector::perp2() const { return dx*dx + dy*dy; }
|
---|
| 155 | inline double Hep3Vector::perp() const { return std::sqrt(perp2()); }
|
---|
| 156 | inline double Hep3Vector::rho() const { return perp(); }
|
---|
| 157 | inline double Hep3Vector::eta() const { return pseudoRapidity();}
|
---|
| 158 |
|
---|
| 159 | inline double Hep3Vector::getRho() const { return perp(); }
|
---|
| 160 | inline double Hep3Vector::getEta() const { return pseudoRapidity();}
|
---|
| 161 |
|
---|
| 162 | inline void Hep3Vector::setPerp(double r) {
|
---|
| 163 | double p = perp();
|
---|
| 164 | if (p != 0.0) {
|
---|
| 165 | dx *= r/p;
|
---|
| 166 | dy *= r/p;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | inline void Hep3Vector::setRho(double rho) { setPerp (rho); }
|
---|
| 170 |
|
---|
| 171 | // ----------
|
---|
| 172 | // Comparison
|
---|
| 173 | // ----------
|
---|
| 174 |
|
---|
| 175 | inline bool Hep3Vector::operator == (const Hep3Vector& v) const {
|
---|
| 176 | return (v.x()==x() && v.y()==y() && v.z()==z()) ? true : false;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | inline bool Hep3Vector::operator != (const Hep3Vector& v) const {
|
---|
| 180 | return (v.x()!=x() || v.y()!=y() || v.z()!=z()) ? true : false;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | inline double Hep3Vector::getTolerance () {
|
---|
| 184 | return tolerance;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | // ----------
|
---|
| 188 | // Arithmetic
|
---|
| 189 | // ----------
|
---|
| 190 |
|
---|
| 191 | inline Hep3Vector& Hep3Vector::operator += (const Hep3Vector & p) {
|
---|
| 192 | dx += p.x();
|
---|
| 193 | dy += p.y();
|
---|
| 194 | dz += p.z();
|
---|
| 195 | return *this;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | inline Hep3Vector& Hep3Vector::operator -= (const Hep3Vector & p) {
|
---|
| 199 | dx -= p.x();
|
---|
| 200 | dy -= p.y();
|
---|
| 201 | dz -= p.z();
|
---|
| 202 | return *this;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | inline Hep3Vector Hep3Vector::operator - () const {
|
---|
| 206 | return Hep3Vector(-dx, -dy, -dz);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | inline 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 |
|
---|
| 220 | inline double Hep3Vector::diff2(const Hep3Vector & p) const {
|
---|
| 221 | return (*this-p).mag2();
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | inline double Hep3Vector::dot(const Hep3Vector & p) const {
|
---|
| 225 | return dx*p.x() + dy*p.y() + dz*p.z();
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | inline 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 |
|
---|
| 232 | inline 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 |
|
---|
| 238 | inline double Hep3Vector::perp(const Hep3Vector & p) const {
|
---|
| 239 | return std::sqrt(perp2(p));
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | inline Hep3Vector Hep3Vector::perpPart () const {
|
---|
| 243 | return Hep3Vector (dx, dy, 0);
|
---|
| 244 | }
|
---|
| 245 | inline Hep3Vector Hep3Vector::project () const {
|
---|
| 246 | return Hep3Vector (0, 0, dz);
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | inline Hep3Vector Hep3Vector::perpPart (const Hep3Vector & v2) const {
|
---|
| 250 | return ( *this - project(v2) );
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | inline double Hep3Vector::angle(const Hep3Vector & q) const {
|
---|
| 254 | return std::acos(cosTheta(q));
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | inline double Hep3Vector::theta(const Hep3Vector & q) const {
|
---|
| 258 | return angle(q);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | inline double Hep3Vector::azimAngle(const Hep3Vector & v2) const {
|
---|
| 262 | return deltaPhi(v2);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | // ----------
|
---|
| 266 | // Properties
|
---|
| 267 | // ----------
|
---|
| 268 |
|
---|
| 269 | inline 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 |
|
---|
| 275 | inline 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
|
---|