CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testLorentzVector.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // $Id: testLorentzVector.cc,v 1.2 2003/08/13 20:00:14 garren Exp $
00003 // ---------------------------------------------------------------------------
00004 //
00005 // This file is a part of what might become CLHEP -
00006 // a Class Library for High Energy Physics.
00007 //
00008 // This is a small program for testing the HepLorentzVector class
00009 // and the interaction with the HepLorentzRotation class.
00010 //
00011 
00012 #include "CLHEP/Units/GlobalSystemOfUnits.h"    // to see shadowing problems
00013 #include "CLHEP/Vector/defs.h"
00014 #include "CLHEP/Vector/LorentzVector.h"
00015 #include "CLHEP/Vector/LorentzRotation.h"
00016 #include "CLHEP/Vector/Sqr.h"
00017 #include <iostream>
00018 #include <cmath>
00019 #include <stdlib.h>
00020 #include <assert.h>
00021 
00022 using namespace CLHEP;
00023 
00024 #define DEPS 1.0e-14
00025 #define FEPS 1.0e-6
00026 
00027 bool approx(double a, double b, double eps) {
00028   return bool( std::abs(a-b) < eps );
00029 }
00030 
00031 bool
00032 test(const HepLorentzVector & p, double x, double y, double z, double e,
00033      double eps) {
00034   bool t = bool( approx(p.x(), x, eps) && approx(p.y(), y, eps) &&
00035                              approx(p.z(), z, eps) && approx(p.t(), e, eps));
00036   if ( !t ) std::cerr << p << std::endl
00037                       << x << '\t' << y << '\t' << z << '\t' << e
00038                       << std::endl; 
00039   return t;
00040 }
00041 
00042 void conversion_test(Hep3Vector & v3, HepLorentzVector & v4) {
00043   v3 = Hep3Vector(3.,2.,1.);
00044   assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
00045 }
00046 
00047 void conversion_test(const Hep3Vector & v3, const HepLorentzVector & v4) {
00048   assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
00049 }
00050 
00051 bool
00052 test(const HepLorentzVector & p, const HepLorentzVector & q, double eps) {
00053   bool t = bool( approx(p.x(), q.x(), eps) &&
00054                              approx(p.y(), q.y(), eps) &&
00055                              approx(p.z(), q.z(), eps) &&
00056                              approx(p.t(), q.t(), eps));
00057   if ( !t ) std::cerr << p << std::endl
00058                          << q << std::endl; 
00059   return t;
00060 }
00061 
00062 int main () {
00063   HepLorentzVector v4(1.,2.,3.,4.);
00064   const HepLorentzVector v4const(1.,2.,3.,4);
00065   conversion_test(v4,v4);
00066   conversion_test(v4const, v4const);
00067 
00068   Hep3Vector f3x(1.0), f3y(0.0, 1.0), f3z(0.0, 0.0, 1.0);
00069   Hep3Vector d30, d3x(1.0), d3y(0.0, 1.0), d3z(0.0, 0.0, 1.0);
00070 
00071 // test constructors:
00072 
00073   HepLorentzVector d0;
00074   if ( !test(d0, 0.0, 0.0, 0.0, 0.0, DEPS) ) exit(1);
00075   HepLorentzVector d1(d3x, 1.0);
00076   if ( !test(d1, 1.0, 0.0, 0.0, 1.0, DEPS) ) exit(1);
00077   HepLorentzVector d2(d3x + d3y, std::sqrt(2.0));
00078   if ( !test(d2, 1.0, 1.0, 0.0, std::sqrt(2.0), DEPS) ) exit(1);
00079   HepLorentzVector d3(d3z + d2, std::sqrt(3.0));
00080   if ( !test(d3, 1.0, 1.0, 1.0, std::sqrt(3.0), DEPS) ) exit(1);
00081   HepLorentzVector d4(0.0, 0.0, 0.0, 1.0);
00082   if ( !test(d4,0.0, 0.0, 0.0, 1.0, DEPS) ) exit(1);
00083   HepLorentzVector d5(f3x, f3x.mag()); if ( !test(d5, d1, FEPS) ) exit(1);
00084   HepLorentzVector d6(d3x+f3y, (d3x+f3y).mag());
00085   if ( !test(d6, d2, FEPS) ) exit(1);
00086   HepLorentzVector d7(f3x+f3y+f3z, (f3x+f3y+f3z).mag());
00087   if ( !test(d7, d3, FEPS) ) exit(1);
00088 
00089   HepLorentzVector f0; if ( !test(f0, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(1);
00090   HepLorentzVector f1(f3x, 1.0);
00091   if ( !test(f1, 1.0, 0.0, 0.0, 1.0, FEPS) ) exit(1);
00092   HepLorentzVector f2(f3x + f3y, std::sqrt(2.0));
00093   if ( !test(f2, 1.0, 1.0, 0.0, std::sqrt(2.0), FEPS) ) exit(1);
00094   HepLorentzVector f3(f3z + f2, std::sqrt(3.0));
00095   if ( !test(f3, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(1);
00096   HepLorentzVector f4(0.0, 0.0, 0.0, 1.0);
00097   if ( !test(f4,0.0, 0.0, 0.0, 1.0, FEPS) ) exit(1);
00098   HepLorentzVector f5(d3x, d3x.mag()); if ( !test(f5, f1, FEPS) ) exit(1);
00099   HepLorentzVector f6(f3x+d3y, (f3x+d3y).mag());
00100   if ( !test(f6, f2, FEPS) ) exit(1);
00101   HepLorentzVector f7(d3x+d3y+d3z, (d3x+d3y+d3z).mag());
00102   if ( !test(f7, f3, FEPS) ) exit(1);
00103 
00104   HepLorentzVector d8(f7); if ( !test(d8, d7, FEPS) ) exit(1);
00105   HepLorentzVector d9(d7); if ( !test(d9, d7, DEPS) ) exit(1);
00106   HepLorentzVector f8(f7); if ( !test(f8, d7, FEPS) ) exit(1);
00107   HepLorentzVector f9(d7); if ( !test(f9, d7, FEPS) ) exit(1);
00108 
00109   HepLorentzVector d10(1.0, 1.0, 1.0, std::sqrt(3.0));
00110   if ( !test(d10, d7, FEPS) ) exit(1);
00111   HepLorentzVector f10(1.0, 1.0, 1.0, std::sqrt(3.0));
00112   if ( !test(f10, f7, FEPS) ) exit(1);
00113 
00114   HepLorentzVector d11(d3x+d3y+d3z, 1.0);
00115   if ( !test(d11, 1.0, 1.0, 1.0, 1.0, DEPS) ) exit(1);
00116   HepLorentzVector f11(d3x+d3y+d3z, 1.0);
00117   if ( !test(f11, 1.0, 1.0, 1.0, 1.0, FEPS) ) exit(1);
00118 
00119 // test input/output from a stream
00120 
00121   std::cin >> d0; if ( !test(d0, 1.1, 2.2, 3.3, 4.4, DEPS) ) exit(1); 
00122   std::cin >> f0; if ( !test(f0, 4.0, 3.0, 2.0, 1.0, FEPS) ) exit(1); 
00123   std::cout << d0 << std::endl;
00124   std::cout << f0 << std::endl;
00125 
00126 // testing assignment
00127 
00128   d6 = d7; if ( !test(d6, d7, DEPS) ) exit(2);
00129   d6 = f7; if ( !test(d6, d7, FEPS) ) exit(2);
00130   f6 = d7; if ( !test(f6, f7, FEPS) ) exit(2);
00131   f6 = f7; if ( !test(f6, f7, FEPS) ) exit(2);
00132 
00133   //testing addition and subtraction:
00134 
00135   d11 = d3 + d7 + f3;
00136   if ( !test(d11, 3.0, 3.0, 3.0, std::sqrt(27.0), FEPS) ) exit(4);
00137   f11 = d3 + d7 + f3;
00138   if ( !test(f11, 3.0, 3.0, 3.0, std::sqrt(27.0), FEPS) ) exit(4);
00139   d11 += d3;
00140   if ( !test(d11, 4.0, 4.0, 4.0, std::sqrt(48.0), FEPS) ) exit(4);
00141   f11 += f3;
00142   if ( !test(f11, 4.0, 4.0, 4.0, std::sqrt(48.0), FEPS) ) exit(4);
00143   d11 = d3 + d7 - f3;
00144   if ( !test(d11, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(4);
00145   if ( !test(-d11, -1.0, -1.0, -1.0, -std::sqrt(3.0), FEPS) ) exit(4);
00146   f11 = d3 + f7 - d3;
00147   if ( !test(f11, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(4);
00148   if ( !test(-f11, -1.0, -1.0, -1.0, -std::sqrt(3.0), FEPS) ) exit(4);
00149   d11 -= d3;
00150   if ( !test(d11, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(4);
00151   f11 -= f3;
00152   if ( !test(f11, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(4);
00153 
00154   d11 = HepLorentzVector(1.0, 2.0, 3.0, 4.0);
00155   d11 *= 2.;
00156   if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
00157   d11 = 2.*HepLorentzVector(1.0, 2.0, 3.0, 4.0);
00158   if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
00159   d11 = HepLorentzVector(1.0, 2.0, 3.0, 4.0)*2.;
00160   if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
00161 
00162 // testing scalar products:
00163 
00164   if ( !approx(d1 * d2, std::sqrt(2.0)-1.0, DEPS) ) exit(5);
00165   if ( !approx(d3.dot(d7), 0.0, FEPS) ) exit(5);
00166   if ( !approx(d2 * f1, std::sqrt(2.0)-1.0, FEPS) ) exit(5);
00167   if ( !approx(f3.dot(d7), 0.0, FEPS) ) exit(5);
00168 
00169 // testing components:
00170 
00171   d11 = HepLorentzVector(1.0, 1.0, 1.0, std::sqrt(7.0));
00172   if ( !approx(d11.mag2(), 4.0, DEPS) ) exit(6);
00173   if ( !approx(d11.mag(), 2.0, DEPS) ) exit(6);
00174   if ( !approx(Hep3Vector(d11).mag2(), 3.0, DEPS) ) exit(6);
00175   if ( !approx(Hep3Vector(d11).mag(), std::sqrt(3.0), DEPS) ) exit(6);
00176   if ( !approx(d11.perp2(), 2.0, DEPS) ) exit(6);
00177   if ( !approx(d11.perp(), std::sqrt(2.0), DEPS) ) exit(6);
00178   f11 = HepLorentzVector(1.0, 1.0, 1.0, std::sqrt(7.0));
00179   if ( !approx(f11.mag2(), 4.0, FEPS) ) exit(6);
00180   if ( !approx(f11.mag(), 2.0, FEPS) ) exit(6);
00181   if ( !approx(f11.vect().mag2(), 3.0, FEPS) ) exit(6);
00182   if ( !approx(f11.vect().mag(), std::sqrt(3.0), FEPS) ) exit(6);
00183   if ( !approx(f11.perp2(), 2.0, FEPS) ) exit(6);
00184   if ( !approx(f11.perp(), std::sqrt(2.0), FEPS) ) exit(6);
00185   
00186 // testing boosts:
00187 
00188   d5 = d3 = d1 = HepLorentzVector(1.0, 2.0, -1.0, 3.0);
00189   d6 = d4 = d2 = HepLorentzVector(-1.0, 1.0, 2.0, 4.0);
00190   double M = (d1 + d2).mag();
00191   double dm1 = d1.mag();
00192   double dm2 = d2.mag();
00193   double p2 = (sqr(M)-sqr(dm1+dm2))*(sqr(M)-sqr(dm1-dm2))/(4.0*sqr(M));
00194   d30 = -(d1 + d2).boostVector();
00195   d1.boost(d30);
00196   double phi = d1.phi();
00197   double theta = d1.theta();
00198   d1.rotateZ(-phi);
00199   d1.rotateY(-theta);
00200   HepRotation r;
00201   r.rotateZ(-phi);
00202   HepLorentzRotation r1(d30), r2(r), r3, r4, r5;
00203   r3.rotateY(-theta);
00204   r4 = r3  * r2 * r1;
00205   d2 *= r4;
00206   if ( !test(d1, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
00207   if ( !test(d2, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
00208   d1.transform(r4.inverse());
00209   if ( !test(d1, d3, DEPS) ) exit(7);
00210   r5 *= r3;
00211   r5 *= r;
00212   r5 *= r1;
00213   r5.invert();
00214   d2 *= r5;
00215   if ( !test(d2, d4, DEPS) ) exit(7);
00216   r4 = r1;
00217   r4.rotateZ(-phi);
00218   r4.rotateY(-theta);
00219   d3 *= r4;
00220   d4 = r4 * d6;
00221   if ( !test(d3, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
00222   if ( !test(d4, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
00223   r5 = r1.inverse();
00224   r5 *= r.inverse();
00225   r5 *= r3.inverse();
00226   d4.transform(r5);
00227   d3.transform(r5);
00228   
00229   if ( !test(d4, d6, DEPS) ) exit(7);
00230   if ( !test(d3, d5, DEPS) ) exit(7);
00231 
00232   r5 = r1;
00233   r5.transform(r);
00234   r5.transform(r3);
00235   d4.transform(r5);
00236   d3.transform(r5);
00237   if ( !test(d3, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
00238   if ( !test(d4, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
00239 
00240   return 0;
00241 }

Generated on 15 Nov 2012 for CLHEP by  doxygen 1.4.7