Geant4 Cross Reference |
1 // -*- C++ -*- 1 // -*- C++ -*- 2 // ------------------------------------------- 2 // --------------------------------------------------------------------------- 3 // 3 // 4 // This file is a part of the CLHEP - a Class 4 // This file is a part of the CLHEP - a Class Library for High Energy Physics. 5 // 5 // 6 // This is the implementation of the Hep3Vecto 6 // This is the implementation of the Hep3Vector class. 7 // 7 // 8 // See also ThreeVectorR.cc for implementation 8 // See also ThreeVectorR.cc for implementation of Hep3Vector methods which 9 // would couple in all the HepRotation methods 9 // would couple in all the HepRotation methods. 10 // 10 // 11 11 >> 12 #ifdef GNUPRAGMA >> 13 #pragma implementation >> 14 #endif >> 15 12 #include "CLHEP/Vector/ThreeVector.h" 16 #include "CLHEP/Vector/ThreeVector.h" 13 #include "CLHEP/Units/PhysicalConstants.h" 17 #include "CLHEP/Units/PhysicalConstants.h" 14 18 15 #include <cmath> 19 #include <cmath> 16 #include <iostream> 20 #include <iostream> 17 21 18 namespace CLHEP { 22 namespace CLHEP { 19 23 20 void Hep3Vector::setMag(double ma) { 24 void Hep3Vector::setMag(double ma) { 21 double factor = mag(); 25 double factor = mag(); 22 if (factor == 0) { 26 if (factor == 0) { 23 std::cerr << "Hep3Vector::setMag() - " 27 std::cerr << "Hep3Vector::setMag() - " 24 << "zero vector can't be stretch 28 << "zero vector can't be stretched" << std::endl; 25 }else{ 29 }else{ 26 factor = ma/factor; 30 factor = ma/factor; 27 setX(x()*factor); 31 setX(x()*factor); 28 setY(y()*factor); 32 setY(y()*factor); 29 setZ(z()*factor); 33 setZ(z()*factor); 30 } 34 } 31 } 35 } 32 36 33 Hep3Vector & Hep3Vector::rotateUz(const Hep3Ve 37 Hep3Vector & Hep3Vector::rotateUz(const Hep3Vector& NewUzVector) { 34 // NewUzVector must be normalized ! 38 // NewUzVector must be normalized ! 35 39 36 double u1 = NewUzVector.x(); 40 double u1 = NewUzVector.x(); 37 double u2 = NewUzVector.y(); 41 double u2 = NewUzVector.y(); 38 double u3 = NewUzVector.z(); 42 double u3 = NewUzVector.z(); 39 double up = u1*u1 + u2*u2; 43 double up = u1*u1 + u2*u2; 40 44 41 if (up > 0) { << 45 if (up>0) { 42 up = std::sqrt(up); << 46 up = std::sqrt(up); 43 double px = (u1 * u3 * x() - u2 * y()) / u << 47 double px = dx, py = dy, pz = dz; 44 double py = (u2 * u3 * x() + u1 * y()) / u << 48 dx = (u1*u3*px - u2*py)/up + u1*pz; 45 double pz = -up * x() + u3 * z(); << 49 dy = (u2*u3*px + u1*py)/up + u2*pz; 46 set(px, py, pz); << 50 dz = -up*px + u3*pz; 47 } else if (u3 < 0.) { << 51 } 48 setX(-x()); << 52 else if (u3 < 0.) { dx = -dx; dz = -dz; } // phi=0 teta=pi 49 setZ(-z()); << 53 else {}; 50 } // phi=0 teta=pi << 51 << 52 return *this; 54 return *this; 53 } 55 } 54 56 55 double Hep3Vector::pseudoRapidity() const { 57 double Hep3Vector::pseudoRapidity() const { 56 double m1 = mag(); 58 double m1 = mag(); 57 if ( m1== 0 ) return 0.0; 59 if ( m1== 0 ) return 0.0; 58 if ( m1== z() ) return 1.0E72; 60 if ( m1== z() ) return 1.0E72; 59 if ( m1== -z() ) return -1.0E72; 61 if ( m1== -z() ) return -1.0E72; 60 return 0.5*std::log( (m1+z())/(m1-z()) ); 62 return 0.5*std::log( (m1+z())/(m1-z()) ); 61 } 63 } 62 64 63 std::ostream & operator<< (std::ostream & os, 65 std::ostream & operator<< (std::ostream & os, const Hep3Vector & v) { 64 return os << "(" << v.x() << "," << v.y() << 66 return os << "(" << v.x() << "," << v.y() << "," << v.z() << ")"; 65 } 67 } 66 68 67 void ZMinput3doubles ( std::istream & is, cons 69 void ZMinput3doubles ( std::istream & is, const char * type, 68 double & x, double & y, 70 double & x, double & y, double & z ); 69 71 70 std::istream & operator>>(std::istream & is, H 72 std::istream & operator>>(std::istream & is, Hep3Vector & v) { 71 double x, y, z; 73 double x, y, z; 72 ZMinput3doubles ( is, "Hep3Vector", x, y, z 74 ZMinput3doubles ( is, "Hep3Vector", x, y, z ); 73 v.set(x, y, z); 75 v.set(x, y, z); 74 return is; 76 return is; 75 } // operator>>() 77 } // operator>>() 76 78 77 const Hep3Vector HepXHat(1.0, 0.0, 0.0); 79 const Hep3Vector HepXHat(1.0, 0.0, 0.0); 78 const Hep3Vector HepYHat(0.0, 1.0, 0.0); 80 const Hep3Vector HepYHat(0.0, 1.0, 0.0); 79 const Hep3Vector HepZHat(0.0, 0.0, 1.0); 81 const Hep3Vector HepZHat(0.0, 0.0, 1.0); 80 82 81 //------------------- 83 //------------------- 82 // 84 // 83 // New methods introduced when ZOOM PhysicsVec 85 // New methods introduced when ZOOM PhysicsVectors was merged in: 84 // 86 // 85 //------------------- 87 //------------------- 86 88 87 Hep3Vector & Hep3Vector::rotateX (double phi1) 89 Hep3Vector & Hep3Vector::rotateX (double phi1) { 88 double sinphi = std::sin(phi1); 90 double sinphi = std::sin(phi1); 89 double cosphi = std::cos(phi1); 91 double cosphi = std::cos(phi1); 90 double ty = y() * cosphi - z() * sinphi; << 92 double ty; 91 double tz = z() * cosphi + y() * sinphi; << 93 ty = dy * cosphi - dz * sinphi; 92 setY(ty); << 94 dz = dz * cosphi + dy * sinphi; 93 setZ(tz); << 95 dy = ty; 94 return *this; 96 return *this; 95 } /* rotateX */ 97 } /* rotateX */ 96 98 97 Hep3Vector & Hep3Vector::rotateY (double phi1) 99 Hep3Vector & Hep3Vector::rotateY (double phi1) { 98 double sinphi = std::sin(phi1); 100 double sinphi = std::sin(phi1); 99 double cosphi = std::cos(phi1); 101 double cosphi = std::cos(phi1); 100 double tx = x() * cosphi + z() * sinphi; << 102 double tz; 101 double tz = z() * cosphi - x() * sinphi; << 103 tz = dz * cosphi - dx * sinphi; 102 setX(tx); << 104 dx = dx * cosphi + dz * sinphi; 103 setZ(tz); << 105 dz = tz; 104 return *this; 106 return *this; 105 } /* rotateY */ 107 } /* rotateY */ 106 108 107 Hep3Vector & Hep3Vector::rotateZ (double phi1) 109 Hep3Vector & Hep3Vector::rotateZ (double phi1) { 108 double sinphi = std::sin(phi1); 110 double sinphi = std::sin(phi1); 109 double cosphi = std::cos(phi1); 111 double cosphi = std::cos(phi1); 110 double tx = x() * cosphi - y() * sinphi; << 112 double tx; 111 double ty = y() * cosphi + x() * sinphi; << 113 tx = dx * cosphi - dy * sinphi; 112 setX(tx); << 114 dy = dy * cosphi + dx * sinphi; 113 setY(ty); << 115 dx = tx; 114 return *this; 116 return *this; 115 } /* rotateZ */ 117 } /* rotateZ */ 116 118 117 bool Hep3Vector::isNear(const Hep3Vector & v, 119 bool Hep3Vector::isNear(const Hep3Vector & v, double epsilon) const { 118 double limit = dot(v)*epsilon*epsilon; 120 double limit = dot(v)*epsilon*epsilon; 119 return ( (*this - v).mag2() <= limit ); 121 return ( (*this - v).mag2() <= limit ); 120 } /* isNear() */ 122 } /* isNear() */ 121 123 122 double Hep3Vector::howNear(const Hep3Vector & 124 double Hep3Vector::howNear(const Hep3Vector & v ) const { 123 // | V1 - V2 | **2 / V1 dot V2, up to 1 125 // | V1 - V2 | **2 / V1 dot V2, up to 1 124 double d = (*this - v).mag2(); 126 double d = (*this - v).mag2(); 125 double vdv = dot(v); 127 double vdv = dot(v); 126 if ( (vdv > 0) && (d < vdv) ) { 128 if ( (vdv > 0) && (d < vdv) ) { 127 return std::sqrt (d/vdv); 129 return std::sqrt (d/vdv); 128 } else if ( (vdv == 0) && (d == 0) ) { 130 } else if ( (vdv == 0) && (d == 0) ) { 129 return 0; 131 return 0; 130 } else { 132 } else { 131 return 1; 133 return 1; 132 } 134 } 133 } /* howNear */ 135 } /* howNear */ 134 136 135 double Hep3Vector::deltaPhi (const Hep3Vector 137 double Hep3Vector::deltaPhi (const Hep3Vector & v2) const { 136 double dphi = v2.getPhi() - getPhi(); 138 double dphi = v2.getPhi() - getPhi(); 137 if ( dphi > CLHEP::pi ) { 139 if ( dphi > CLHEP::pi ) { 138 dphi -= CLHEP::twopi; 140 dphi -= CLHEP::twopi; 139 } else if ( dphi <= -CLHEP::pi ) { 141 } else if ( dphi <= -CLHEP::pi ) { 140 dphi += CLHEP::twopi; 142 dphi += CLHEP::twopi; 141 } 143 } 142 return dphi; 144 return dphi; 143 } /* deltaPhi */ 145 } /* deltaPhi */ 144 146 145 double Hep3Vector::deltaR ( const Hep3Vector & 147 double Hep3Vector::deltaR ( const Hep3Vector & v ) const { 146 double a = eta() - v.eta(); 148 double a = eta() - v.eta(); 147 double b = deltaPhi(v); 149 double b = deltaPhi(v); 148 return std::sqrt ( a*a + b*b ); 150 return std::sqrt ( a*a + b*b ); 149 } /* deltaR */ 151 } /* deltaR */ 150 152 151 double Hep3Vector::cosTheta(const Hep3Vector & 153 double Hep3Vector::cosTheta(const Hep3Vector & q) const { 152 double arg; 154 double arg; 153 double ptot2 = mag2()*q.mag2(); 155 double ptot2 = mag2()*q.mag2(); 154 if(ptot2 <= 0) { 156 if(ptot2 <= 0) { 155 arg = 0.0; 157 arg = 0.0; 156 }else{ 158 }else{ 157 arg = dot(q)/std::sqrt(ptot2); 159 arg = dot(q)/std::sqrt(ptot2); 158 if(arg > 1.0) arg = 1.0; 160 if(arg > 1.0) arg = 1.0; 159 if(arg < -1.0) arg = -1.0; 161 if(arg < -1.0) arg = -1.0; 160 } 162 } 161 return arg; 163 return arg; 162 } 164 } 163 165 164 double Hep3Vector::cos2Theta(const Hep3Vector 166 double Hep3Vector::cos2Theta(const Hep3Vector & q) const { 165 double arg; 167 double arg; 166 double ptot2 = mag2(); 168 double ptot2 = mag2(); 167 double qtot2 = q.mag2(); 169 double qtot2 = q.mag2(); 168 if ( ptot2 == 0 || qtot2 == 0 ) { 170 if ( ptot2 == 0 || qtot2 == 0 ) { 169 arg = 1.0; 171 arg = 1.0; 170 }else{ 172 }else{ 171 double pdq = dot(q); 173 double pdq = dot(q); 172 arg = (pdq/ptot2) * (pdq/qtot2); 174 arg = (pdq/ptot2) * (pdq/qtot2); 173 // More naive methods overflow on vect 175 // More naive methods overflow on vectors which can be squared 174 // but can't be raised to the 4th powe 176 // but can't be raised to the 4th power. 175 if(arg > 1.0) arg = 1.0; 177 if(arg > 1.0) arg = 1.0; 176 } 178 } 177 return arg; 179 return arg; 178 } 180 } 179 181 180 void Hep3Vector::setEta (double eta1) { 182 void Hep3Vector::setEta (double eta1) { 181 double phi1 = 0; 183 double phi1 = 0; 182 double r1; 184 double r1; 183 if ( (x() == 0) && (y() == 0) ) { << 185 if ( (dx == 0) && (dy == 0) ) { 184 if (z() == 0) { << 186 if (dz == 0) { 185 std::cerr << "Hep3Vector::setEta() - " 187 std::cerr << "Hep3Vector::setEta() - " 186 << "Attempt to set eta of zero 188 << "Attempt to set eta of zero vector -- vector is unchanged" 187 << std::endl; 189 << std::endl; 188 return; 190 return; 189 } 191 } 190 std::cerr << "Hep3Vector::setEta() - " 192 std::cerr << "Hep3Vector::setEta() - " 191 << "Attempt to set eta of vector a 193 << "Attempt to set eta of vector along Z axis -- will use phi = 0" 192 << std::endl; 194 << std::endl; 193 r1 = std::fabs(z()); << 195 r1 = std::fabs(dz); 194 } else { 196 } else { 195 r1 = getR(); 197 r1 = getR(); 196 phi1 = getPhi(); 198 phi1 = getPhi(); 197 } 199 } 198 double tanHalfTheta = std::exp ( -eta1 ); 200 double tanHalfTheta = std::exp ( -eta1 ); 199 double cosTheta1 = 201 double cosTheta1 = 200 (1 - tanHalfTheta*tanHalfTheta) / (1 + 202 (1 - tanHalfTheta*tanHalfTheta) / (1 + tanHalfTheta*tanHalfTheta); >> 203 dz = r1 * cosTheta1; 201 double rho1 = r1*std::sqrt(1 - cosTheta1*cos 204 double rho1 = r1*std::sqrt(1 - cosTheta1*cosTheta1); 202 setZ(r1 * cosTheta1); << 205 dy = rho1 * std::sin (phi1); 203 setY(rho1 * std::sin (phi1)); << 206 dx = rho1 * std::cos (phi1); 204 setX(rho1 * std::cos (phi1)); << 205 return; 207 return; 206 } 208 } 207 209 208 void Hep3Vector::setCylTheta (double theta1) { 210 void Hep3Vector::setCylTheta (double theta1) { 209 211 210 // In cylindrical coords, set theta while ke 212 // In cylindrical coords, set theta while keeping rho and phi fixed 211 213 212 if ( (x() == 0) && (y() == 0) ) { << 214 if ( (dx == 0) && (dy == 0) ) { 213 if (z() == 0) { << 215 if (dz == 0) { 214 std::cerr << "Hep3Vector::setCylTheta() 216 std::cerr << "Hep3Vector::setCylTheta() - " 215 << "Attempt to set cylTheta of 217 << "Attempt to set cylTheta of zero vector -- vector is unchanged" 216 << std::endl; 218 << std::endl; 217 return; 219 return; 218 } 220 } 219 if (theta1 == 0) { 221 if (theta1 == 0) { 220 setZ(std::fabs(z())); << 222 dz = std::fabs(dz); 221 return; 223 return; 222 } 224 } 223 if (theta1 == CLHEP::pi) { 225 if (theta1 == CLHEP::pi) { 224 setZ(-std::fabs(z())); << 226 dz = -std::fabs(dz); 225 return; 227 return; 226 } 228 } 227 std::cerr << "Hep3Vector::setCylTheta() - 229 std::cerr << "Hep3Vector::setCylTheta() - " 228 << "Attempt set cylindrical theta of vec 230 << "Attempt set cylindrical theta of vector along Z axis " 229 << "to a non-trivial value, while keepin 231 << "to a non-trivial value, while keeping rho fixed -- " 230 << "will return zero vector" << std::end 232 << "will return zero vector" << std::endl; 231 setZ(0.0); << 233 dz = 0; 232 return; 234 return; 233 } 235 } 234 if ( (theta1 < 0) || (theta1 > CLHEP::pi) ) 236 if ( (theta1 < 0) || (theta1 > CLHEP::pi) ) { 235 std::cerr << "Hep3Vector::setCylTheta() - 237 std::cerr << "Hep3Vector::setCylTheta() - " 236 << "Setting Cyl theta of a vector based 238 << "Setting Cyl theta of a vector based on a value not in [0, PI]" 237 << std::endl; 239 << std::endl; 238 // No special return needed if warning 240 // No special return needed if warning is ignored. 239 } 241 } 240 double phi1 (getPhi()); 242 double phi1 (getPhi()); 241 double rho1 = getRho(); 243 double rho1 = getRho(); 242 if ( (theta1 == 0) || (theta1 == CLHEP::pi) 244 if ( (theta1 == 0) || (theta1 == CLHEP::pi) ) { 243 std::cerr << "Hep3Vector::setCylTheta() - 245 std::cerr << "Hep3Vector::setCylTheta() - " 244 << "Attempt to set cylindrical theta to 246 << "Attempt to set cylindrical theta to 0 or PI " 245 << "while keeping rho fixed -- infinite 247 << "while keeping rho fixed -- infinite Z will be computed" 246 << std::endl; 248 << std::endl; 247 setZ((theta1==0) ? 1.0E72 : -1.0E72); << 249 dz = (theta1==0) ? 1.0E72 : -1.0E72; 248 return; 250 return; 249 } 251 } 250 setZ(rho1 / std::tan (theta1)); << 252 dz = rho1 / std::tan (theta1); 251 setY(rho1 * std::sin (phi1)); << 253 dy = rho1 * std::sin (phi1); 252 setX(rho1 * std::cos (phi1)); << 254 dx = rho1 * std::cos (phi1); 253 255 254 } /* setCylTheta */ 256 } /* setCylTheta */ 255 257 256 void Hep3Vector::setCylEta (double eta1) { 258 void Hep3Vector::setCylEta (double eta1) { 257 259 258 // In cylindrical coords, set eta while keep 260 // In cylindrical coords, set eta while keeping rho and phi fixed 259 261 260 double theta1 = 2 * std::atan ( std::exp (-e 262 double theta1 = 2 * std::atan ( std::exp (-eta1) ); 261 263 262 //-| The remaining code is similar to 264 //-| The remaining code is similar to setCylTheta, The reason for 263 //-| using a copy is so as to be able 265 //-| using a copy is so as to be able to change the messages in the 264 //-| ZMthrows to say eta rather than t 266 //-| ZMthrows to say eta rather than theta. Besides, we assumedly 265 //-| need not check for theta of 0 or 267 //-| need not check for theta of 0 or PI. 266 268 267 if ( (x() == 0) && (y() == 0) ) { << 269 if ( (dx == 0) && (dy == 0) ) { 268 if (z() == 0) { << 270 if (dz == 0) { 269 std::cerr << "Hep3Vector::setCylEta() - 271 std::cerr << "Hep3Vector::setCylEta() - " 270 << "Attempt to set cylEta of zero vect 272 << "Attempt to set cylEta of zero vector -- vector is unchanged" 271 << std::endl; 273 << std::endl; 272 return; 274 return; 273 } 275 } 274 if (theta1 == 0) { 276 if (theta1 == 0) { 275 setZ(std::fabs(z())); << 277 dz = std::fabs(dz); 276 return; 278 return; 277 } 279 } 278 if (theta1 == CLHEP::pi) { 280 if (theta1 == CLHEP::pi) { 279 setZ(-std::fabs(z())); << 281 dz = -std::fabs(dz); 280 return; 282 return; 281 } 283 } 282 std::cerr << "Hep3Vector::setCylEta() - " 284 std::cerr << "Hep3Vector::setCylEta() - " 283 << "Attempt set cylindrical eta of vecto 285 << "Attempt set cylindrical eta of vector along Z axis " 284 << "to a non-trivial value, while keepin 286 << "to a non-trivial value, while keeping rho fixed -- " 285 << "will return zero vector" << std::end 287 << "will return zero vector" << std::endl; 286 setZ(0.0); << 288 dz = 0; 287 return; 289 return; 288 } 290 } 289 double phi1 (getPhi()); 291 double phi1 (getPhi()); 290 double rho1 = getRho(); 292 double rho1 = getRho(); 291 setZ(rho1 / std::tan (theta1)); << 293 dz = rho1 / std::tan (theta1); 292 setY(rho1 * std::sin (phi1)); << 294 dy = rho1 * std::sin (phi1); 293 setX(rho1 * std::cos (phi1)); << 295 dx = rho1 * std::cos (phi1); 294 296 295 } /* setCylEta */ 297 } /* setCylEta */ 296 298 297 299 298 Hep3Vector operator/ ( const Hep3Vector & v1, << 300 Hep3Vector operator/ ( const Hep3Vector & v1, double c ) { 299 // if (c == 0) { 301 // if (c == 0) { 300 // std::cerr << "Hep3Vector::operator/ () - 302 // std::cerr << "Hep3Vector::operator/ () - " 301 // << "Attempt to divide vector by 0 -- " 303 // << "Attempt to divide vector by 0 -- " 302 // << "will produce infinities and/or NAN 304 // << "will produce infinities and/or NANs" << std::endl; 303 // } 305 // } 304 return v1 * (1.0/c); << 306 double oneOverC = 1.0/c; >> 307 return Hep3Vector ( v1.x() * oneOverC, >> 308 v1.y() * oneOverC, >> 309 v1.z() * oneOverC ); 305 } /* v / c */ 310 } /* v / c */ 306 311 307 Hep3Vector & Hep3Vector::operator/= (double c) 312 Hep3Vector & Hep3Vector::operator/= (double c) { 308 // if (c == 0) { 313 // if (c == 0) { 309 // std::cerr << "Hep3Vector::operator/ () - 314 // std::cerr << "Hep3Vector::operator/ () - " 310 // << "Attempt to do vector /= 0 -- " 315 // << "Attempt to do vector /= 0 -- " 311 // << "division by zero would produce inf 316 // << "division by zero would produce infinite or NAN components" 312 // << std::endl; 317 // << std::endl; 313 // } 318 // } 314 *this *= 1.0/c; << 319 double oneOverC = 1.0/c; >> 320 dx *= oneOverC; >> 321 dy *= oneOverC; >> 322 dz *= oneOverC; 315 return *this; 323 return *this; 316 } 324 } 317 325 318 double Hep3Vector::tolerance = Hep3Vector::Tol 326 double Hep3Vector::tolerance = Hep3Vector::ToleranceTicks * 2.22045e-16; 319 327 320 } // namespace CLHEP 328 } // namespace CLHEP 321 329