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