CLHEP 2.0.4.7 Reference Documentation
CLHEP Home Page CLHEP Documentation CLHEP Bug Reports |
00001 // -*- C++ -*- 00002 // $Id: testThreeVector.cc,v 1.3 2003/08/08 13:47:09 garren Exp $ 00003 // --------------------------------------------------------------------------- 00004 // 00005 // This file is a part of the CLHEP - a Class Library for High Energy Physics. 00006 // 00007 // This is a small program for testing the Hep3Vector class 00008 // and the interaction with the HepRotation class. 00009 // 00010 00011 #include "CLHEP/Vector/ThreeVector.h" 00012 #include "CLHEP/Vector/Rotation.h" 00013 #include "CLHEP/Units/PhysicalConstants.h" 00014 00015 #include <cmath> 00016 #include <iostream> 00017 #include <stdlib.h> // for exit 00018 00019 using namespace std; 00020 using namespace CLHEP; 00021 00022 00023 #define DEPS 1.0e-14 00024 #define FEPS 1.0e-6 00025 00026 bool approx(double a, double b, double eps) { 00027 return bool( abs(a-b) < eps ); 00028 } 00029 00030 bool 00031 test(const Hep3Vector & p, double x, double y, double z, 00032 double eps) { 00033 return bool( approx(p.x(), x, eps) && approx(p.y(), y, eps) && 00034 approx(p.z(), z, eps) ); 00035 } 00036 00037 int main () { 00038 00039 // test constructors: 00040 00041 Hep3Vector d0; if ( !test(d0, 0.0, 0.0, 0.0, DEPS) ) exit(1); 00042 Hep3Vector f0; if ( !test(f0, 0.0, 0.0, 0.0, FEPS) ) exit(1); 00043 Hep3Vector d1(1.0); if ( !test(d1, 1.0, 0.0, 0.0, DEPS) ) exit(1); 00044 Hep3Vector f1(1.0); if ( !test(f1, 1.0, 0.0, 0.0, FEPS) ) exit(1); 00045 Hep3Vector d2(1.0, 1.0); if ( !test(d2, 1.0, 1.0, 0.0, DEPS) ) exit(1); 00046 Hep3Vector f2(1.0, 1.0); if ( !test(f2, 1.0, 1.0, 0.0, FEPS) ) exit(1); 00047 Hep3Vector d3(1.0, 1.0, 1.0); if ( !test(d3, 1.0, 1.0, 1.0, DEPS) ) exit(1); 00048 Hep3Vector f3(1.0, 1.0, 1.0); if ( !test(f3, 1.0, 1.0, 1.0, FEPS) ) exit(1); 00049 Hep3Vector d4(f3); if ( !test(d4, 1.0, 1.0, 1.0, DEPS) ) exit(1); 00050 Hep3Vector f4(d3); if ( !test(f4, 1.0, 1.0, 1.0, FEPS) ) exit(1); 00051 00052 // test input/output from a stream 00053 00054 cin >> d0; if ( !test(d0, 1.1, 2.2, 3.3, DEPS) ) exit(1); 00055 cin >> f0; if ( !test(f0, 3.0, 2.0, 1.0, FEPS) ) exit(1); 00056 cout << d0 << endl; 00057 cout << f0 << endl; 00058 00059 // test assignment: 00060 00061 d4 = d1; if ( !test(d4, 1.0, 0.0, 0.0, DEPS) ) exit(2); 00062 f4 = f1; if ( !test(f4, 1.0, 0.0, 0.0, FEPS) ) exit(2); 00063 d4 = f1; if ( !test(d4, 1.0, 0.0, 0.0, FEPS) ) exit(2); 00064 f4 = d1; if ( !test(f4, 1.0, 0.0, 0.0, FEPS) ) exit(2); 00065 00066 // test addition: 00067 00068 d4 = d1 + d2; if ( !test(d4, 2.0, 1.0, 0.0, DEPS) ) exit(3); 00069 d4 = f1 + d2; if ( !test(d4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00070 d4 = d1 + f2; if ( !test(d4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00071 d4 = f1 + f2; if ( !test(d4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00072 d4 += d3; if ( !test(d4, 3.0, 2.0, 1.0, FEPS) ) exit(3); 00073 d4 += f3; if ( !test(d4, 4.0, 3.0, 2.0, FEPS) ) exit(3); 00074 f4 = d1 + d2; if ( !test(f4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00075 f4 = f1 + d2; if ( !test(f4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00076 f4 = d1 + f2; if ( !test(f4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00077 f4 = f1 + f2; if ( !test(f4, 2.0, 1.0, 0.0, FEPS) ) exit(3); 00078 f4 += d3; if ( !test(f4, 3.0, 2.0, 1.0, FEPS) ) exit(3); 00079 f4 += f3; if ( !test(f4, 4.0, 3.0, 2.0, FEPS) ) exit(3); 00080 00081 // test subtraction 00082 00083 d4 -= d3; if ( !test(d4, 3.0, 2.0, 1.0, FEPS) ) exit(4); 00084 d4 -= f3; if ( !test(d4, 2.0, 1.0, 0.0, FEPS) ) exit(4); 00085 f4 -= d3; if ( !test(f4, 3.0, 2.0, 1.0, FEPS) ) exit(4); 00086 f4 -= f3; if ( !test(f4, 2.0, 1.0, 0.0, FEPS) ) exit(4); 00087 d4 = d1 - d2; if ( !test(d4, 0.0, -1.0, 0.0, DEPS) ) exit(4); 00088 d4 = f1 - d2; if ( !test(d4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00089 d4 = d1 - f2; if ( !test(d4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00090 d4 = f1 - f2; if ( !test(d4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00091 f4 = d1 - d2; if ( !test(f4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00092 f4 = f1 - d2; if ( !test(f4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00093 f4 = d1 - f2; if ( !test(f4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00094 f4 = f1 - f2; if ( !test(f4, 0.0, -1.0, 0.0, FEPS) ) exit(4); 00095 00096 // test unary minus: 00097 00098 if ( !test(-d3, -1.0, -1.0, -1.0, DEPS) ) exit(5); 00099 if ( !test(-f3, -1.0, -1.0, -1.0, FEPS) ) exit(5); 00100 if ( !test(-d1, -1.0, 0.0, 0.0, DEPS) ) exit(5); 00101 if ( !test(-f1, -1.0, 0.0, 0.0, FEPS) ) exit(5); 00102 00103 // test scaling: 00104 00105 if ( !test(d3*2.0, 2.0, 2.0, 2.0, DEPS) ) exit(6); 00106 if ( !test(2.0*d3, 2.0, 2.0, 2.0, DEPS) ) exit(6); 00107 if ( !test(d1*2.0, 2.0, 0.0, 0.0, DEPS) ) exit(6); 00108 if ( !test(2.0*d1, 2.0, 0.0, 0.0, DEPS) ) exit(6); 00109 if ( !test(f3*2.0f, 2.0, 2.0, 2.0, FEPS) ) exit(6); 00110 if ( !test(2.0f*f3, 2.0, 2.0, 2.0, FEPS) ) exit(6); 00111 if ( !test(f1*2.0f, 2.0, 0.0, 0.0, FEPS) ) exit(6); 00112 if ( !test(2.0f*f1, 2.0, 0.0, 0.0, FEPS) ) exit(6); 00113 if ( !test(d4*=2.0, 0.0, -2.0, 0.0, FEPS) ) exit(6); 00114 if ( !test(f4*=2.0, 0.0, -2.0, 0.0, FEPS) ) exit(6); 00115 00116 // testing scalar and vector product: 00117 00118 if ( !approx(d4*d1, 0.0, DEPS) ) exit(7); 00119 if ( !approx(d4*f1, 0.0, FEPS) ) exit(7); 00120 if ( !approx(f4*d1, 0.0, FEPS) ) exit(7); 00121 if ( !approx(f4*f1, 0.0, FEPS) ) exit(7); 00122 if ( !approx(d4.dot(d1), 0.0, DEPS) ) exit(7); 00123 if ( !approx(d4.dot(f1), 0.0, FEPS) ) exit(7); 00124 if ( !approx(f4.dot(d1), 0.0, FEPS) ) exit(7); 00125 if ( !approx(f4.dot(f1), 0.0, FEPS) ) exit(7); 00126 if ( !approx(d4*d2, -2.0, DEPS) ) exit(7); 00127 if ( !approx(d4*f2, -2.0, FEPS) ) exit(7); 00128 if ( !approx(f4*d2, -2.0, FEPS) ) exit(7); 00129 if ( !approx(f4*f2, -2.0, FEPS) ) exit(7); 00130 if ( !approx(d4.dot(d2), -2.0, DEPS) ) exit(7); 00131 if ( !approx(d4.dot(f2), -2.0, FEPS) ) exit(7); 00132 if ( !approx(f4.dot(d2), -2.0, FEPS) ) exit(7); 00133 if ( !approx(f4.dot(f2), -2.0, FEPS) ) exit(7); 00134 d4 = d1.cross(d2); if ( !test(d4, 0.0, 0.0, 1.0, DEPS) ) exit(7); 00135 d4 = d2.cross(d1); if ( !test(d4, 0.0, 0.0, -1.0, DEPS) ) exit(7); 00136 f4 = f1.cross(d2); if ( !test(f4, 0.0, 0.0, 1.0, FEPS) ) exit(7); 00137 f4 = d2.cross(f1); if ( !test(f4, 0.0, 0.0, -1.0, FEPS) ) exit(7); 00138 00139 // testing ptot and pt: 00140 00141 d4 = d1 + f2 + d3; 00142 f4 = d1 + f2 + d3; 00143 if ( !approx(d4.mag2(), 14.0, FEPS) ) exit(8); 00144 if ( !approx(d4.mag(), sqrt(14.0), FEPS) ) exit(8); 00145 if ( !approx(d4.perp2(), 13.0, FEPS) ) exit(8); 00146 if ( !approx(d4.perp(), sqrt(13.0), FEPS) ) exit(8); 00147 if ( !approx(f4.mag2(), 14.0, FEPS) ) exit(8); 00148 if ( !approx(f4.mag(), sqrt(14.0), FEPS) ) exit(8); 00149 if ( !approx(f4.perp2(), 13.0, FEPS) ) exit(8); 00150 if ( !approx(f4.perp(), sqrt(13.0), FEPS) ) exit(8); 00151 00152 // testing angles: 00153 00154 d4 = d2 - 2.0 * d1; 00155 f4 = d2 - 2.0f * f1; 00156 if ( !approx(d1.phi(), 0.0, DEPS) ) exit(9); 00157 if ( !approx(d1.theta(), CLHEP::halfpi, DEPS) ) exit(9); 00158 if ( !approx(d1.cosTheta(), 0.0, DEPS) ) exit(9); 00159 if ( !approx(d2.phi(), CLHEP::halfpi*0.5, DEPS) ) exit(9); 00160 if ( !approx(d2.theta(), CLHEP::halfpi, DEPS) ) exit(9); 00161 if ( !approx(d2.cosTheta(), 0.0, DEPS) ) exit(9); 00162 if ( !approx((-d2).phi(), -3.0*CLHEP::halfpi*0.5, DEPS) ) exit(9); 00163 if ( !approx(d4.phi(), 3.0*CLHEP::halfpi*0.5, DEPS) ) exit(9); 00164 00165 if ( !approx(f1.phi(), 0.0, FEPS) ) exit(9); 00166 if ( !approx(f1.theta(), CLHEP::halfpi, FEPS) ) exit(9); 00167 if ( !approx(f1.cosTheta(), 0.0, FEPS) ) exit(9); 00168 if ( !approx(f2.phi(), CLHEP::halfpi*0.5, FEPS) ) exit(9); 00169 if ( !approx(f2.theta(), CLHEP::halfpi, FEPS) ) exit(9); 00170 if ( !approx(f2.cosTheta(), 0.0, FEPS) ) exit(9); 00171 if ( !approx((-f2).phi(), -3.0*CLHEP::halfpi*0.5, FEPS) ) exit(9); 00172 if ( !approx(f4.phi(), 3.0*CLHEP::halfpi*0.5, FEPS) ) exit(9); 00173 00174 d4 = d3 - d1; if ( !approx(d4.theta(), CLHEP::halfpi*0.5, DEPS) ) exit(9); 00175 if ( !approx((-d4).theta(), 3.0*CLHEP::halfpi*0.5, DEPS) ) exit(9); 00176 if ( !approx((-d4).cosTheta(), -sqrt(0.5), DEPS) ) exit(9); 00177 d4 = d3 - d2; if ( !approx(d4.theta(), 0.0, DEPS) ) exit(9); 00178 if ( !approx(d4.cosTheta(), 1.0, DEPS) ) exit(9); 00179 if ( !approx((-d4).theta(), CLHEP::pi, DEPS) ) exit(9); 00180 if ( !approx((-d4).cosTheta(), -1.0, DEPS) ) exit(9); 00181 f4 = d3 - d1; if ( !approx(f4.theta(), CLHEP::halfpi*0.5, FEPS) ) exit(9); 00182 if ( !approx((-f4).theta(), 3.0*CLHEP::halfpi*0.5, FEPS) ) exit(9); 00183 if ( !approx((-f4).cosTheta(), -sqrt(0.5), FEPS) ) exit(9); 00184 f4 = d3 - d2; if ( !approx(f4.theta(), 0.0, FEPS) ) exit(9); 00185 if ( !approx(f4.cosTheta(), 1.0, FEPS) ) exit(9); 00186 if ( !approx((-f4).theta(), CLHEP::pi, FEPS) ) exit(9); 00187 if ( !approx((-f4).cosTheta(), -1.0, FEPS) ) exit(9); 00188 00189 d4 = d2 - 2.0*d1; if ( !approx(d4.angle(d2), CLHEP::halfpi, DEPS) ) exit(9); 00190 f4 = d2 - 2.0*d1; if ( !approx(f4.angle(f2), CLHEP::halfpi, FEPS) ) exit(9); 00191 00192 // testing rotations 00193 00194 d4 = d1; 00195 d4.rotateZ(CLHEP::halfpi); if ( !test(d4, 0.0, 1.0, 0.0, DEPS) ) exit(10); 00196 d4.rotateY(25.3); if ( !test(d4, 0.0, 1.0, 0.0, DEPS) ) exit(10); 00197 d4.rotateZ(CLHEP::halfpi); if ( !test(d4, -1.0, 0.0, 0.0, DEPS) ) exit(10); 00198 d4.rotateY(CLHEP::halfpi); if ( !test(d4, 0.0, 0.0, 1.0, DEPS) ) exit(10); 00199 d4.rotateZ(2.6); if ( !test(d4, 0.0, 0.0, 1.0, DEPS) ) exit(10); 00200 d4.rotateY(CLHEP::pi*0.25); 00201 if ( !test(d4, sqrt(0.5), 0.0, sqrt(0.5), DEPS) ) exit(10); 00202 f4 = f1; 00203 f4.rotateZ(CLHEP::halfpi); if ( !test(f4, 0.0, 1.0, 0.0, FEPS) ) exit(10); 00204 f4.rotateY(25.3); if ( !test(f4, 0.0, 1.0, 0.0, FEPS) ) exit(10); 00205 f4.rotateZ(CLHEP::halfpi); if ( !test(f4, -1.0, 0.0, 0.0, FEPS) ) exit(10); 00206 f4.rotateY(CLHEP::halfpi); if ( !test(f4, 0.0, 0.0, 1.0, FEPS) ) exit(10); 00207 f4.rotateZ(2.6); if ( !test(f4, 0.0, 0.0, 1.0, FEPS) ) exit(10); 00208 f4.rotateY(CLHEP::pi*0.25); 00209 if ( !test(f4, sqrt(0.5), 0.0, sqrt(0.5), FEPS) ) exit(10); 00210 00211 d4 = d1; 00212 d4.rotate(d4.angle(d3), d4.cross(d3)); 00213 d4 *= d3.mag(); 00214 if ( !test(d4, 1.0, 1.0, 1.0, DEPS) ) exit(10); 00215 d4 = d1; 00216 d4.rotate(0.23, d4.cross(d3)); 00217 if ( !approx(d4.angle(d1), 0.23, DEPS) ) exit(10); 00218 f4 = d1; 00219 f4.rotate(f4.angle(d3), f4.cross(d3)); 00220 f4 *= f3.mag(); 00221 if ( !test(f4, 1.0, 1.0, 1.0, FEPS) ) exit(10); 00222 f4 = f1; 00223 f4.rotate(0.23, f4.cross(d3)); 00224 if ( !approx(f4.angle(f1), 0.23, FEPS) ) exit(10); 00225 if ( !approx(f4.angle(d3), f1.angle(d3) - 0.23, FEPS) ) exit(10); 00226 00227 // test rotation maticies: 00228 00229 d4 = d1; 00230 00231 HepRotation r0, r1, r2, r3, r4, r5; 00232 r1.rotateZ(CLHEP::halfpi); 00233 r2.rotateY(CLHEP::halfpi); 00234 r4.rotate(d4.angle(d3), d4.cross(d3)); 00235 r5.rotate(0.23, d4.cross(d3)); 00236 d4 = r4.inverse() * d3; 00237 if ( !test(d4, d3.mag(), 0.0, 0.0, DEPS) ) exit(11); 00238 d4 = r5 * d3; 00239 if ( !approx(d1.angle(d4), d1.angle(d3)+0.23, DEPS) ) exit(11); 00240 f4 = r4.inverse() * f3; 00241 if ( !test(f4, f3.mag(), 0.0, 0.0, FEPS) ) exit(11); 00242 f4 = r5 * d3; 00243 if ( !approx(d1.angle(f4), f1.angle(f3)+0.23, FEPS) ) exit(11); 00244 r5 = r2 * r1 * r3.inverse() * r0 * r0.inverse(); 00245 d4 = d3; 00246 d4 *= r3.inverse(); 00247 d4 *= r1; 00248 d4 *= r2; 00249 if ( !test(d4, 1.0, 1.0, 1.0, DEPS) ) exit(11); 00250 r5.invert(); 00251 d4 = r5 * d4; 00252 if ( !test(d4, 1.0, 1.0, 1.0, DEPS) ) exit(11); 00253 d1 = d2 = Hep3Vector(1.0, -0.5, 2.1); 00254 d3 = Hep3Vector(-0.3, 1.1, 1.5); 00255 d4 = d3.unit(); 00256 d4 *= d3.mag(); 00257 if ( !test(d4, d3.x(), d3.y(), d3.z(), DEPS) ) exit(11); 00258 r0.rotate(0.10, d1.cross(d3)); 00259 d1 *= r0; 00260 if ( !approx(d1.angle(d3), d2.angle(d3)-0.1, DEPS) ) exit(12); 00261 if ( !approx(d1.angle(d2), 0.1, DEPS) ) exit(12); 00262 00263 return 0; 00264 00265 }