TestVectors.cxx

Go to the documentation of this file.
00001 // @(#)root/test:$Id: TestVectors.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Peter Malzacher   19/06/99
00003 
00004 #ifndef __CINT__
00005 #include <Riostream.h>
00006 #include <TMath.h>
00007 #include <TVector3.h>
00008 #include <TLorentzVector.h>
00009 #include <TRotation.h>
00010 #include <TLorentzRotation.h>
00011 #include <assert.h>
00012 #endif
00013 
00014 Double_t DEPS=1.0e-14;
00015 Double_t FEPS=1.0e-6;
00016 
00017 Bool_t approx(Double_t a, Double_t b, Double_t eps) {
00018   Double_t diff = TMath::Abs(a-b);
00019   Bool_t OK = Bool_t(diff < eps);
00020   if (OK) return OK;
00021   printf(" a = %.18g, b= %.18g, diff = %.18g\n",a,b,diff);
00022   return kFALSE;
00023 }
00024 
00025 Bool_t test(const TVector3 &p, Double_t x, Double_t y, Double_t z, Double_t eps) {
00026   Bool_t OK = Bool_t( approx(p.X(), x, eps) &&
00027                       approx(p.Y(), y, eps) &&
00028                       approx(p.Z(), z, eps) );
00029   if (OK) return OK;
00030   //p.Dump();
00031   printf("px = %.18g, py= %.18g, pz = %.18g, eps = %.18g\n",p.X(),p.Y(),p.Z(),eps);
00032   printf("x = %.18g, y= %.18g, z = %.18g, eps = %.18g\n",x,y,z,eps);
00033   return kFALSE;
00034 
00035 }
00036 
00037 Bool_t test(const TLorentzVector & p, Double_t x, Double_t y, Double_t z, Double_t e, Double_t eps) {
00038   Bool_t OK = Bool_t( approx(p.X(), x, eps) &&
00039                       approx(p.Y(), y, eps) &&
00040                       approx(p.Z(), z, eps) &&
00041                       approx(p.T(), e, eps));
00042   if (OK) return OK;
00043   //p.Dump();
00044   printf("px = %.18g, py= %.18g, pz = %.18g, pe = %.18g, eps = %.18g\n",p.X(),p.Y(),p.Z(),p.E(),eps);
00045   printf("x = %.18g, y= %.18g, z = %.18g, e = %.18g, eps = %.18g\n",x,y,z,e,eps);
00046   return kFALSE;
00047 }
00048 
00049 Bool_t test(const TLorentzVector & p, const TLorentzVector & q, Double_t eps) {
00050   Bool_t OK =Bool_t( approx(p.X(), q.X(), eps) &&
00051                      approx(p.Y(), q.Y(), eps) &&
00052                      approx(p.Z(), q.Z(), eps) &&
00053                      approx(p.T(), q.T(), eps));
00054   if (OK) return OK;
00055   //p.Dump();
00056   //q.Dump();
00057   printf("px = %.18g, py= %.18g, pz = %.18g, pe = %.18g, eps = %.18g\n",p.X(),p.Y(),p.Z(),p.E(),eps);
00058   printf("qx = %.18g, qy= %.18g, qz = %.18g, qe = %.18g, eps = %.18g\n",q.X(),q.Y(),q.Z(),q.E(),eps);
00059   printf("eps = %.18g\n",eps);
00060   return kFALSE;
00061 }
00062 
00063 Double_t SQR(Double_t x) {return x*x;}
00064 
00065 int TestVector3();
00066 int TestLorentzVector();
00067 int TestRotation();
00068 
00069 int TestVectors()
00070 {
00071    int t1 = TestVector3();
00072    int t2 = TestLorentzVector();
00073    int t3 = TestRotation();
00074    return t1+t2+t3;
00075 }
00076 
00077 
00078 int TestVector3() {
00079 
00080 // test constructors:
00081 
00082   TVector3 d0; assert( test(d0, 0.0, 0.0, 0.0, DEPS) );
00083   TVector3 f0; assert( test(f0, 0.0, 0.0, 0.0, FEPS) );
00084 
00085   TVector3 d1(1.0); assert( test(d1, 1.0, 0.0, 0.0, DEPS) ) ;
00086   TVector3 f1(1.0); assert( test(f1, 1.0, 0.0, 0.0, FEPS) ) ;
00087 
00088   TVector3 d2(1.0, 1.0); assert( test(d2, 1.0, 1.0, 0.0, DEPS) ) ;
00089   TVector3 f2(1.0, 1.0); assert( test(f2, 1.0, 1.0, 0.0, FEPS) ) ;
00090   TVector3 d3(1.0, 1.0, 1.0); assert( test(d3, 1.0, 1.0, 1.0, DEPS) ) ;
00091   TVector3 f3(1.0, 1.0, 1.0); assert( test(f3, 1.0, 1.0, 1.0, FEPS) ) ;
00092 
00093 
00094   TVector3 d4(f3); assert( test(d4, 1.0, 1.0, 1.0, DEPS) ) ;
00095   TVector3 f4(d3); assert( test(f4, 1.0, 1.0, 1.0, FEPS) ) ;
00096 
00097 
00098 // test assignment:
00099 
00100   d4 = d1;  assert( test(d4, 1.0, 0.0, 0.0, DEPS) ) ;
00101   f4 = f1;  assert( test(f4, 1.0, 0.0, 0.0, FEPS) ) ;
00102   d4 = f1;  assert( test(d4, 1.0, 0.0, 0.0, DEPS) ) ;
00103   f4 = d1;  assert( test(f4, 1.0, 0.0, 0.0, FEPS) ) ;
00104 
00105 
00106 // test addition:
00107 
00108   d4 = d1 + d2; assert( test(d4, 2.0, 1.0, 0.0, DEPS) ) ;
00109   d4 = f1 + d2; assert( test(d4, 2.0, 1.0, 0.0, FEPS) ) ;
00110   d4 = d1 + f2; assert( test(d4, 2.0, 1.0, 0.0, FEPS) ) ;
00111   d4 = f1 + f2; assert( test(d4, 2.0, 1.0, 0.0, FEPS) ) ;
00112   d4 += d3; assert( test(d4, 3.0, 2.0, 1.0, FEPS) ) ;
00113   d4 += f3; assert( test(d4, 4.0, 3.0, 2.0, FEPS) ) ;
00114   f4 = d1 + d2; assert( test(f4, 2.0, 1.0, 0.0, FEPS) ) ;
00115   f4 = f1 + d2; assert( test(f4, 2.0, 1.0, 0.0, FEPS) ) ;
00116   f4 = d1 + f2; assert( test(f4, 2.0, 1.0, 0.0, FEPS) ) ;
00117   f4 = f1 + f2; assert( test(f4, 2.0, 1.0, 0.0, FEPS) ) ;
00118   f4 += d3; assert( test(f4, 3.0, 2.0, 1.0, FEPS) ) ;
00119   f4 += f3; assert( test(f4, 4.0, 3.0, 2.0, FEPS) ) ;
00120 
00121 // test subtraction
00122 
00123   d4 -= d3; assert( test(d4, 3.0, 2.0, 1.0, FEPS) ) ;
00124   d4 -= f3; assert( test(d4, 2.0, 1.0, 0.0, FEPS) ) ;
00125   f4 -= d3; assert( test(f4, 3.0, 2.0, 1.0, FEPS) ) ;
00126   f4 -= f3; assert( test(f4, 2.0, 1.0, 0.0, FEPS) ) ;
00127   d4 = d1 - d2; assert( test(d4, 0.0, -1.0, 0.0, DEPS) ) ;
00128   d4 = f1 - d2; assert( test(d4, 0.0, -1.0, 0.0, FEPS) ) ;
00129   d4 = d1 - f2; assert( test(d4, 0.0, -1.0, 0.0, FEPS) ) ;
00130   d4 = f1 - f2; assert( test(d4, 0.0, -1.0, 0.0, FEPS) ) ;
00131   f4 = d1 - d2; assert( test(f4, 0.0, -1.0, 0.0, FEPS) ) ;
00132   f4 = f1 - d2; assert( test(f4, 0.0, -1.0, 0.0, FEPS) ) ;
00133   f4 = d1 - f2; assert( test(f4, 0.0, -1.0, 0.0, FEPS) ) ;
00134   f4 = f1 - f2; assert( test(f4, 0.0, -1.0, 0.0, FEPS) ) ;
00135 
00136 // test unary minus:
00137 
00138   assert( test(-d3, -1.0, -1.0, -1.0, DEPS) ) ;
00139   assert( test(-f3, -1.0, -1.0, -1.0, FEPS) ) ;
00140   assert( test(-d1, -1.0, 0.0, 0.0, DEPS) ) ;
00141   assert( test(-f1, -1.0, 0.0, 0.0, FEPS) ) ;
00142 
00143 // test scaling:
00144 
00145   assert( test(d3*2.0, 2.0, 2.0, 2.0, DEPS) ) ;
00146   assert( test(2.0*d3, 2.0, 2.0, 2.0, DEPS) ) ;
00147   assert( test(d1*2.0, 2.0, 0.0, 0.0, DEPS) ) ;
00148   assert( test(2.0*d1, 2.0, 0.0, 0.0, DEPS) ) ;
00149   assert( test(f3*2.0f, 2.0, 2.0, 2.0, FEPS) ) ;
00150   assert( test(2.0f*f3, 2.0, 2.0, 2.0, FEPS) ) ;
00151   assert( test(f1*2.0f, 2.0, 0.0, 0.0, FEPS) ) ;
00152   assert( test(2.0f*f1, 2.0, 0.0, 0.0, FEPS) ) ;
00153   assert( test(d4*=2.0, 0.0, -2.0, 0.0, FEPS) ) ;
00154   assert( test(f4*=2.0, 0.0, -2.0, 0.0, FEPS) ) ;
00155 
00156 // testing scalar and vector product:
00157 
00158   assert( approx(d4*d1, 0.0, DEPS) ) ;
00159   assert( approx(d4*f1, 0.0, FEPS) ) ;
00160   assert( approx(f4*d1, 0.0, FEPS) ) ;
00161   assert( approx(f4*f1, 0.0, FEPS) ) ;
00162   assert( approx(d4.Dot(d1), 0.0, DEPS) ) ;
00163   assert( approx(d4.Dot(f1), 0.0, FEPS) ) ;
00164   assert( approx(f4.Dot(d1), 0.0, FEPS) ) ;
00165   assert( approx(f4.Dot(f1), 0.0, FEPS) ) ;
00166   assert( approx(d4*d2, -2.0, DEPS) ) ;
00167   assert( approx(d4*f2, -2.0, FEPS) ) ;
00168   assert( approx(f4*d2, -2.0, FEPS) ) ;
00169   assert( approx(f4*f2, -2.0, FEPS) ) ;
00170   assert( approx(d4.Dot(d2), -2.0, DEPS) ) ;
00171   assert( approx(d4.Dot(f2), -2.0, FEPS) ) ;
00172   assert( approx(f4.Dot(d2), -2.0, FEPS) ) ;
00173   assert( approx(f4.Dot(f2), -2.0, FEPS) ) ;
00174   d4 = d1.Cross(d2); assert( test(d4, 0.0, 0.0, 1.0, DEPS) ) ;
00175   d4 = d2.Cross(d1); assert( test(d4, 0.0, 0.0, -1.0, DEPS) ) ;
00176   f4 = f1.Cross(d2); assert( test(f4, 0.0, 0.0, 1.0, FEPS) ) ;
00177   f4 = d2.Cross(f1); assert( test(f4, 0.0, 0.0, -1.0, FEPS) ) ;
00178 
00179 // testing ptot and pt:
00180 
00181   d4 = d1 + f2 + d3;
00182   f4 = d1 + f2 + d3;
00183   assert( approx(d4.Mag2(), 14.0, FEPS) ) ;
00184   assert( approx(d4.Mag(), TMath::Sqrt(14.0), FEPS) ) ;
00185   assert( approx(d4.Perp2(), 13.0, FEPS) ) ;
00186   assert( approx(d4.Perp(), TMath::Sqrt(13.0), FEPS) ) ;
00187   assert( approx(f4.Mag2(), 14.0, FEPS) ) ;
00188   assert( approx(f4.Mag(), TMath::Sqrt(14.0), FEPS) ) ;
00189   assert( approx(f4.Perp2(), 13.0, FEPS) ) ;
00190   assert( approx(f4.Perp(), TMath::Sqrt(13.0), FEPS) ) ;
00191 
00192 // testing angles:
00193 
00194   d4 = d2 - 2.0 * d1;
00195   f4 = d2 - 2.0f * f1;
00196   assert( approx(d1.Phi(), 0.0, DEPS) ) ;
00197   assert( approx(d1.Theta(), TMath::Pi()/2., DEPS) ) ;
00198   assert( approx(d1.CosTheta(), 0.0, DEPS) ) ;
00199   assert( approx(d2.Phi(), TMath::Pi()/2.*0.5, DEPS) ) ;
00200   assert( approx(d2.Theta(), TMath::Pi()/2., DEPS) ) ;
00201   assert( approx(d2.CosTheta(), 0.0, DEPS) ) ;
00202   assert( approx(((-d2)).Phi(), -3.0*TMath::Pi()/2.*0.5, DEPS) ) ;
00203   assert( approx(d4.Phi(), 3.0*TMath::Pi()/2.*0.5, DEPS) ) ;
00204 
00205   assert( approx(f1.Phi(), 0.0, FEPS) ) ;
00206   assert( approx(f1.Theta(), TMath::Pi()/2., FEPS) ) ;
00207   assert( approx(f1.CosTheta(), 0.0, FEPS) ) ;
00208   assert( approx(f2.Phi(), TMath::Pi()/2.*0.5, FEPS) ) ;
00209   assert( approx(f2.Theta(), TMath::Pi()/2., FEPS) ) ;
00210   assert( approx(f2.CosTheta(), 0.0, FEPS) ) ;
00211   assert( approx(((-f2)).Phi(), -3.0*TMath::Pi()/2.*0.5, FEPS) ) ;
00212   assert( approx(f4.Phi(), 3.0*TMath::Pi()/2.*0.5, FEPS) ) ;
00213 
00214   d4 = d3 - d1; assert( approx(d4.Theta(), TMath::Pi()/2.*0.5, DEPS) ) ;
00215   assert( approx(((-d4)).Theta(), 3.0*TMath::Pi()/2.*0.5, DEPS) ) ;
00216   assert( approx(((-d4)).CosTheta(), -TMath::Sqrt(0.5), DEPS) ) ;
00217   d4 = d3 - d2; assert( approx(d4.Theta(), 0.0, DEPS) ) ;
00218   assert( approx(d4.CosTheta(), 1.0, DEPS) ) ;
00219   assert( approx(((-d4)).Theta(), TMath::Pi(), DEPS) ) ;
00220   assert( approx(((-d4)).CosTheta(), -1.0, DEPS) ) ;
00221   f4 = d3 - d1; assert( approx(f4.Theta(), TMath::Pi()/2.*0.5, FEPS) ) ;
00222   assert( approx(((-f4)).Theta(), 3.0*TMath::Pi()/2.*0.5, FEPS) ) ;
00223   assert( approx(((-f4)).CosTheta(), -TMath::Sqrt(0.5), FEPS) ) ;
00224   f4 = d3 - d2; assert( approx(f4.Theta(), 0.0, FEPS) ) ;
00225   assert( approx(f4.CosTheta(), 1.0, FEPS) ) ;
00226   assert( approx(((-f4)).Theta(), TMath::Pi(), FEPS) ) ;
00227   assert( approx(((-f4)).CosTheta(), -1.0, FEPS) ) ;
00228 
00229   d4 = d2 - 2.0*d1; assert( approx(d4.Angle(d2), TMath::Pi()/2., DEPS) ) ;
00230   f4 = d2 - 2.0*d1; assert( approx(f4.Angle(f2), TMath::Pi()/2., FEPS) ) ;
00231 
00232 // testing rotations
00233 
00234   d4 = d1;
00235   d4.RotateZ(TMath::Pi()/2.); assert( test(d4, 0.0, 1.0, 0.0, DEPS) ) ;
00236   d4.RotateY(25.3); assert( test(d4, 0.0, 1.0, 0.0, DEPS) ) ;
00237   d4.RotateZ(TMath::Pi()/2.); assert( test(d4, -1.0, 0.0, 0.0, DEPS) ) ;
00238   d4.RotateY(TMath::Pi()/2.); assert( test(d4, 0.0, 0.0, 1.0, DEPS) ) ;
00239   d4.RotateZ(2.6); assert( test(d4, 0.0, 0.0, 1.0, DEPS) ) ;
00240   d4.RotateY(TMath::Pi()*0.25);
00241   assert( test(d4, TMath::Sqrt(0.5), 0.0, TMath::Sqrt(0.5), DEPS) ) ;
00242   f4 = f1;
00243   f4.RotateZ(TMath::Pi()/2.); assert( test(f4, 0.0, 1.0, 0.0, FEPS) ) ;
00244   f4.RotateY(25.3); assert( test(f4, 0.0, 1.0, 0.0, FEPS) ) ;
00245   f4.RotateZ(TMath::Pi()/2.); assert( test(f4, -1.0, 0.0, 0.0, FEPS) ) ;
00246   f4.RotateY(TMath::Pi()/2.); assert( test(f4, 0.0, 0.0, 1.0, FEPS) ) ;
00247   f4.RotateZ(2.6); assert( test(f4, 0.0, 0.0, 1.0, FEPS) ) ;
00248   f4.RotateY(TMath::Pi()*0.25);
00249   assert( test(f4, TMath::Sqrt(0.5), 0.0, TMath::Sqrt(0.5), FEPS) ) ;
00250 
00251   d4 = d1;
00252 
00253   d4.Rotate(d4.Angle(d3), d4.Cross(d3));
00254 
00255   d4 *= d3.Mag();
00256 
00257   assert( test(d4, 1.0, 1.0, 1.0, DEPS) ) ;
00258   d4 = d1;
00259   d4.Rotate(0.23, d4.Cross(d3));
00260   assert( approx(d4.Angle(d1), 0.23, DEPS) ) ;
00261   f4 = d1;
00262   f4.Rotate(f4.Angle(d3), f4.Cross(d3));
00263   f4 *= f3.Mag();
00264   assert( test(f4, 1.0, 1.0, 1.0, FEPS) ) ;
00265   f4 = f1;
00266   f4.Rotate(0.23, f4.Cross(d3));
00267   assert( approx(f4.Angle(f1), 0.23, FEPS) ) ;
00268   assert( approx(f4.Angle(d3), f1.Angle(d3) - 0.23, FEPS) ) ;
00269 
00270 // test rotation maticies:
00271 
00272   d4 = d1;
00273 
00274   TRotation r0, r1, r2, r3, r4, r5;
00275   r1.RotateZ(TMath::Pi()/2.);
00276   r2.RotateY(TMath::Pi()/2.);
00277   r4.Rotate(d4.Angle(d3), d4.Cross(d3));
00278   r5.Rotate(0.23, d4.Cross(d3));
00279   d4 = r4.Inverse() * d3;
00280   assert( test(d4, d3.Mag(), 0.0, 0.0, DEPS) ) ;
00281   d4 = r5 * d3;
00282   assert( approx(d1.Angle(d4), d1.Angle(d3)+0.23, DEPS) ) ;
00283   f4 = r4.Inverse() * f3;
00284   assert( test(f4, f3.Mag(), 0.0, 0.0, FEPS) ) ;
00285   f4 = r5 * d3;
00286   assert( approx(d1.Angle(f4), f1.Angle(f3)+0.23, FEPS) ) ;
00287   r5 = r2 * r1 * r3.Inverse() * r0 * r0.Inverse();
00288   d4 = d3;
00289   d4 *= r3.Inverse();
00290   d4 *= r1;
00291   d4 *= r2;
00292   assert( test(d4, 1.0, 1.0, 1.0, DEPS) ) ;
00293   r5.Invert();
00294   d4 = r5 * d4;
00295   assert( test(d4, 1.0, 1.0, 1.0, DEPS) ) ;
00296   d1 = d2 = TVector3(1.0, -0.5, 2.1);
00297   d3 = TVector3(-0.3, 1.1, 1.5);
00298   d4 = d3.Unit();
00299   d4 *= d3.Mag();
00300 
00301   assert( test(d4, d3.X(), d3.Y(), d3.Z(), DEPS) ) ;
00302   r0.Rotate(0.10, d1.Cross(d3));
00303   d1 *= r0;
00304   assert( approx(d1.Angle(d3), d2.Angle(d3)-0.1, DEPS) ) ;
00305   assert( approx(d1.Angle(d2), 0.1, DEPS) ) ;
00306 
00307   return 0;
00308 
00309 }
00310 
00311 
00312 int TestLorentzVector() {
00313 
00314   TVector3 f3x(1.0), f3y(0.0, 1.0), f3z(0.0, 0.0, 1.0);
00315   TVector3 d30, d3x(1.0), d3y(0.0, 1.0), d3z(0.0, 0.0, 1.0);
00316 
00317 // test constructors:
00318 
00319   TLorentzVector d0;
00320   assert( test(d0, 0.0, 0.0, 0.0, 0.0, DEPS) ) ;
00321   TLorentzVector d1(d3x, 1.0);
00322   assert( test(d1, 1.0, 0.0, 0.0, 1.0, DEPS) ) ;
00323   TLorentzVector d2(d3x + d3y, TMath::Sqrt(2.0));
00324   assert( test(d2, 1.0, 1.0, 0.0, TMath::Sqrt(2.0), DEPS) ) ;
00325   TLorentzVector d3(d3z + d2.Vect(), TMath::Sqrt(3.0));
00326   assert( test(d3, 1.0, 1.0, 1.0, TMath::Sqrt(Double_t(3.0)), DEPS) ) ;
00327   TLorentzVector d4(0.0, 0.0, 0.0, 1.0);
00328   assert( test(d4,0.0, 0.0, 0.0, 1.0, DEPS) ) ;
00329   TLorentzVector d5(f3x, f3x.Mag()); assert( test(d5, d1, FEPS) ) ;
00330   TLorentzVector d6(d3x+f3y, ((d3x+f3y)).Mag());
00331   assert( test(d6, d2, FEPS) ) ;
00332   TLorentzVector d7(f3x+f3y+f3z, ((f3x+f3y+f3z)).Mag());
00333   assert( test(d7, d3, FEPS) ) ;
00334 
00335   TLorentzVector f0; assert( test(f0, 0.0, 0.0, 0.0, 0.0, FEPS) ) ;
00336   TLorentzVector f1(f3x, 1.0);
00337   assert( test(f1, 1.0, 0.0, 0.0, 1.0, FEPS) ) ;
00338   TLorentzVector f2(f3x + f3y, TMath::Sqrt(2.0));
00339   assert( test(f2, 1.0, 1.0, 0.0, TMath::Sqrt(2.0), FEPS) ) ;
00340   TLorentzVector f3(f3z + f2.Vect(), TMath::Sqrt(3.0));
00341   assert( test(f3, 1.0, 1.0, 1.0, TMath::Sqrt(3.0), FEPS) ) ;
00342   TLorentzVector f4(0.0, 0.0, 0.0, 1.0);
00343   assert( test(f4,0.0, 0.0, 0.0, 1.0, FEPS) ) ;
00344   TLorentzVector f5(d3x, d3x.Mag()); assert( test(f5, f1, FEPS) ) ;
00345   TLorentzVector f6(f3x+d3y, ((f3x+d3y)).Mag());
00346   assert( test(f6, f2, FEPS) ) ;
00347   TLorentzVector f7(d3x+d3y+d3z, ((d3x+d3y+d3z)).Mag());
00348   assert( test(f7, f3, FEPS) ) ;
00349 
00350   TLorentzVector d8(f7); assert( test(d8, d7, FEPS) ) ;
00351   TLorentzVector d9(d7); assert( test(d9, d7, DEPS) ) ;
00352   TLorentzVector f8(f7); assert( test(f8, d7, FEPS) ) ;
00353   TLorentzVector f9(d7); assert( test(f9, d7, FEPS) ) ;
00354 
00355   TLorentzVector d10(1.0, 1.0, 1.0, TMath::Sqrt(3.0));
00356   assert( test(d10, d7, FEPS) ) ;
00357   TLorentzVector f10(1.0, 1.0, 1.0, TMath::Sqrt(3.0));
00358   assert( test(f10, f7, FEPS) ) ;
00359 
00360   TLorentzVector d11(d3x+d3y+d3z, 1.0);
00361   assert( test(d11, 1.0, 1.0, 1.0, 1.0, DEPS) ) ;
00362   TLorentzVector f11(d3x+d3y+d3z, 1.0);
00363   assert( test(f11, 1.0, 1.0, 1.0, 1.0, FEPS) ) ;
00364 
00365 // testing assignment
00366 
00367   d6 = d7; assert( test(d6, d7, DEPS) ) ;
00368   d6 = f7; assert( test(d6, d7, FEPS) ) ;
00369   f6 = d7; assert( test(f6, f7, FEPS) ) ;
00370   f6 = f7; assert( test(f6, f7, FEPS) ) ;
00371 
00372   //testing addition and subtraction:
00373 
00374   d11 = d3 + d7 + f3;
00375   assert( test(d11, 3.0, 3.0, 3.0, TMath::Sqrt(27.0), FEPS) ) ;
00376   f11 = d3 + d7 + f3;
00377   assert( test(f11, 3.0, 3.0, 3.0, TMath::Sqrt(27.0), FEPS) ) ;
00378   d11 += d3;
00379   assert( test(d11, 4.0, 4.0, 4.0, TMath::Sqrt(48.0), FEPS) ) ;
00380   f11 += f3;
00381   assert( test(f11, 4.0, 4.0, 4.0, TMath::Sqrt(48.0), FEPS) ) ;
00382   d11 = d3 + d7 - f3;
00383   assert( test(d11, 1.0, 1.0, 1.0, TMath::Sqrt(3.0), FEPS) ) ;
00384   assert( test(-d11, -1.0, -1.0, -1.0, -TMath::Sqrt(3.0), FEPS) ) ;
00385   f11 = d3 + f7 - d3;
00386   assert( test(f11, 1.0, 1.0, 1.0, TMath::Sqrt(3.0), FEPS) ) ;
00387   assert( test(-f11, -1.0, -1.0, -1.0, -TMath::Sqrt(3.0), FEPS) ) ;
00388   d11 -= d3;
00389   assert( test(d11, 0.0, 0.0, 0.0, 0.0, FEPS) ) ;
00390   f11 -= f3;
00391   assert( test(f11, 0.0, 0.0, 0.0, 0.0, FEPS) ) ;
00392 
00393   d11 = TLorentzVector(1.0, 2.0, 3.0, 4.0);
00394   d11 *= 2.;
00395   assert( test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) ;
00396   d11 = 2.*TLorentzVector(1.0, 2.0, 3.0, 4.0);
00397   assert( test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) ;
00398   d11 = TLorentzVector(1.0, 2.0, 3.0, 4.0)*2.;
00399   assert( test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) ;
00400 
00401 // testing scalar products:
00402 
00403   assert( approx(d1 * d2, TMath::Sqrt(2.0)-1.0, DEPS) ) ;
00404   assert( approx(d3.Dot(d7), 0.0, FEPS) ) ;
00405   assert( approx(d2 * f1, TMath::Sqrt(2.0)-1.0, FEPS) ) ;
00406   assert( approx(f3.Dot(d7), 0.0, FEPS) ) ;
00407 
00408 // testing components:
00409 
00410   d11 = TLorentzVector(1.0, 1.0, 1.0, TMath::Sqrt(7.0));
00411   assert( approx(d11.Mag2(), 4.0, DEPS) ) ;
00412   assert( approx(d11.Mag(), 2.0, DEPS) ) ;
00413   assert( approx(TVector3(d11.Vect()).Mag2(), 3.0, DEPS) ) ;
00414   assert( approx(TVector3(d11.Vect()).Mag(), TMath::Sqrt(3.0), DEPS) ) ;
00415   assert( approx(d11.Perp2(), 2.0, DEPS) ) ;
00416   assert( approx(d11.Perp(), TMath::Sqrt(2.0), DEPS) ) ;
00417   f11 = TLorentzVector(1.0, 1.0, 1.0, TMath::Sqrt(7.0));
00418   assert( approx(f11.Mag2(), 4.0, FEPS) ) ;
00419   assert( approx(f11.Mag(), 2.0, FEPS) ) ;
00420   assert( approx(f11.Vect().Mag2(), 3.0, FEPS) ) ;
00421   assert( approx(f11.Vect().Mag(), TMath::Sqrt(3.0), FEPS) ) ;
00422   assert( approx(f11.Perp2(), 2.0, FEPS) ) ;
00423   assert( approx(f11.Perp(), TMath::Sqrt(2.0), FEPS) ) ;
00424 
00425 // testing boosts:
00426 
00427   d5 = d3 = d1 = TLorentzVector(1.0, 2.0, -1.0, 3.0);
00428   d6 = d4 = d2 = TLorentzVector(-1.0, 1.0, 2.0, 4.0);
00429   Double_t M = ((d1 + d2)).Mag();
00430   Double_t m1 = d1.Mag();
00431   Double_t m2 = d2.Mag();
00432   Double_t p2 = (SQR(M)-SQR(m1+m2))*(SQR(M)-SQR(m1-m2))/(4.0*SQR(M));
00433   d30 = -((d1 + d2)).BoostVector();
00434   d1.Boost(d30);
00435   Double_t phi = d1.Phi();
00436   Double_t theta = d1.Theta();
00437   d1.RotateZ(-phi);
00438   d1.RotateY(-theta);
00439   TRotation r;
00440   r.RotateZ(-phi);
00441   TLorentzRotation r1(d30), r2(r), r3, r4, r5;
00442   r3.RotateY(-theta);
00443   r4 = r3  * r2 * r1;
00444   d2 *= r4;
00445   assert( test(d1, 0.0, 0.0, TMath::Sqrt(p2), TMath::Sqrt(p2 + SQR(m1)), DEPS) ) ;
00446   assert( test(d2, 0.0, 0.0, -TMath::Sqrt(p2), TMath::Sqrt(p2 + SQR(m2)), DEPS) ) ;
00447   d1.Transform(r4.Inverse());
00448   assert( test(d1, d3, DEPS) ) ;
00449   r5 *= r3;
00450   r5 *= r;
00451   r5 *= r1;
00452   r5.Invert();
00453   d2 *= r5;
00454   assert( test(d2, d4, DEPS) ) ;
00455   r4 = r1;
00456   r4.RotateZ(-phi);
00457   r4.RotateY(-theta);
00458   d3 *= r4;
00459   d4 = r4 * d6;
00460   assert( test(d3, 0.0, 0.0, TMath::Sqrt(p2), TMath::Sqrt(p2 + SQR(m1)), DEPS) ) ;
00461   assert( test(d4, 0.0, 0.0, -TMath::Sqrt(p2), TMath::Sqrt(p2 + SQR(m2)), DEPS) ) ;
00462   r5 = r1.Inverse();
00463   r5 *= r.Inverse();
00464   r5 *= r3.Inverse();
00465   d4.Transform(r5);
00466   d3.Transform(r5);
00467 
00468   assert( test(d4, d6, DEPS) ) ;
00469   assert( test(d3, d5, DEPS) ) ;
00470 
00471   r5 = r1;
00472   r5.Transform(r);
00473   r5.Transform(r3);
00474   d4.Transform(r5);
00475   d3.Transform(r5);
00476   assert( test(d3, 0.0, 0.0, TMath::Sqrt(p2), TMath::Sqrt(p2 + SQR(m1)), DEPS) ) ;
00477   assert( test(d4, 0.0, 0.0, -TMath::Sqrt(p2), TMath::Sqrt(p2 + SQR(m2)), DEPS) ) ;
00478 
00479   // beta and gamma
00480 
00481   assert( approx(d3.BoostVector().Mag(), d3.Beta(), DEPS) );
00482   assert( approx(d4.BoostVector().Mag(), d4.Beta(), DEPS) );
00483 
00484   assert( approx(d3.Gamma(), 1./TMath::Sqrt(1-d3.Beta()*d3.Beta()), DEPS) );
00485   assert( approx(d4.Gamma(), 1./TMath::Sqrt(1-d4.Beta()*d4.Beta()), DEPS) );
00486 
00487   return 0;
00488 }
00489 
00490 //typedef TRotation Rotation;
00491 //typedef TVector3  Vector;
00492 
00493 int TestRotation() {
00494 
00495   int i,k;
00496   double angA=TMath::Pi()/3, angB=TMath::Pi()/4, angC=TMath::Pi()/6;
00497   double cosA=TMath::Cos(angA), sinA=TMath::Sin(angA);
00498   double cosB=TMath::Cos(angB), sinB=TMath::Sin(angB);
00499   double cosC=TMath::Cos(angC), sinC=TMath::Sin(angC);
00500 
00501   TRotation R;                   // default constructor
00502   assert ( R.XX() == 1 );
00503   assert ( R.XY() == 0 );
00504   assert ( R.XZ() == 0 );
00505   assert ( R.YX() == 0 );
00506   assert ( R.YY() == 1 );
00507   assert ( R.YZ() == 0 );
00508   assert ( R.ZX() == 0 );
00509   assert ( R.ZY() == 0 );
00510   assert ( R.ZZ() == 1 );
00511 
00512   assert( R.IsIdentity() );     // isIdentity()
00513 
00514   R = TRotation();               // rotateX()
00515   R.RotateX(angA);
00516   assert ( approx(R.XX(), 1,    DEPS) );
00517   assert ( approx(R.XY(), 0,    DEPS) );
00518   assert ( approx(R.XZ(), 0,    DEPS) );
00519   assert ( approx(R.YX(), 0,    DEPS) );
00520   assert ( approx(R.YY(), cosA, DEPS) );
00521   assert ( approx(R.YZ(),-sinA, DEPS) );
00522   assert ( approx(R.ZX(), 0,    DEPS) );
00523   assert ( approx(R.ZY(), sinA, DEPS) );
00524   assert ( approx(R.ZZ(), cosA, DEPS) );
00525 
00526   R = TRotation();               // rotateY()
00527   R.RotateY(angB);
00528   assert ( approx(R.XX(), cosB, DEPS) );
00529   assert ( approx(R.XY(), 0,    DEPS) );
00530   assert ( approx(R.XZ(), sinB, DEPS) );
00531   assert ( approx(R.YX(), 0,    DEPS) );
00532   assert ( approx(R.YY(), 1,    DEPS) );
00533   assert ( approx(R.YZ(), 0,    DEPS) );
00534   assert ( approx(R.ZX(),-sinB, DEPS) );
00535   assert ( approx(R.ZY(), 0,    DEPS) );
00536   assert ( approx(R.ZZ(), cosB, DEPS) );
00537 
00538   R = TRotation();               // rotateZ()
00539   R.RotateZ(angC);
00540   assert ( approx(R.XX(), cosC, DEPS) );
00541   assert ( approx(R.XY(),-sinC, DEPS) );
00542   assert ( approx(R.XZ(), 0,    DEPS) );
00543   assert ( approx(R.YX(), sinC, DEPS) );
00544   assert ( approx(R.YY(), cosC, DEPS) );
00545   assert ( approx(R.YZ(), 0,    DEPS) );
00546   assert ( approx(R.ZX(), 0,    DEPS) );
00547   assert ( approx(R.ZY(), 0,    DEPS) );
00548   assert ( approx(R.ZZ(), 1,    DEPS) );
00549 
00550   R = TRotation();               // copy constructor
00551   R.RotateZ(angC);
00552   R.RotateY(angB);
00553   R.RotateZ(angA);
00554   TRotation RR(R);
00555 
00556   assert ( TMath::Abs(RR.XX() - cosA*cosB*cosC + sinA*sinC) < DEPS );
00557   assert ( TMath::Abs(RR.XY() + cosA*cosB*sinC + sinA*cosC) < DEPS );
00558   assert ( TMath::Abs(RR.XZ() - cosA*sinB)                  < DEPS );
00559   assert ( TMath::Abs(RR.YX() - sinA*cosB*cosC - cosA*sinC) < DEPS );
00560   assert ( TMath::Abs(RR.YY() + sinA*cosB*sinC - cosA*cosC) < DEPS );
00561   assert ( TMath::Abs(RR.YZ() - sinA*sinB)                  < DEPS );
00562   assert ( TMath::Abs(RR.ZX() + sinB*cosC)                  < DEPS );
00563   assert ( TMath::Abs(RR.ZY() - sinB*sinC)                  < DEPS );
00564   assert ( TMath::Abs(RR.ZZ() - cosB)                       < DEPS );
00565 
00566   RR = TRotation();              // operator=, operator!=, operator==
00567   assert ( RR != R );
00568   RR = R;
00569   assert ( RR == R );
00570 
00571   assert ( R(0,0) == R.XX() );  // operator(i,j)
00572   assert ( R(0,1) == R.XY() );
00573   assert ( R(0,2) == R.XZ() );
00574   assert ( R(1,0) == R.YX() );
00575   assert ( R(1,1) == R.YY() );
00576   assert ( R(1,2) == R.YZ() );
00577   assert ( R(2,0) == R.ZX() );
00578   assert ( R(2,1) == R.ZY() );
00579   assert ( R(2,2) == R.ZZ() );
00580 
00581   for(i=0; i<3; i++) {
00582     for(k=0; k<3; k++) {
00583       assert ( RR(i,k) == R(i,k) );
00584     }
00585   }
00586 
00587   TRotation A, B ,C;                                // operator*=
00588   A.RotateZ(angA);
00589   B.RotateY(angB);
00590   C.RotateZ(angC);
00591   R  = A; R *= B; R *= C;
00592 
00593   TVector3 V(1,2,3);                                 // operator* (Vector)
00594   V = R * V;
00595   assert ( TMath::Abs(V.X()-R.XX()-2.*R.XY()-3.*R.XZ()) < DEPS );
00596   assert ( TMath::Abs(V.Y()-R.YX()-2.*R.YY()-3.*R.YZ()) < DEPS );
00597   assert ( TMath::Abs(V.Z()-R.ZX()-2.*R.ZY()-3.*R.ZZ()) < DEPS );
00598 
00599   R = A * B * C;                                  // operator*(Matrix)
00600   assert ( TMath::Abs(RR.XX() - R.XX()) < DEPS );
00601   assert ( TMath::Abs(RR.XY() - R.XY()) < DEPS );
00602   assert ( TMath::Abs(RR.XZ() - R.XZ()) < DEPS );
00603   assert ( TMath::Abs(RR.YX() - R.YX()) < DEPS );
00604   assert ( TMath::Abs(RR.YY() - R.YY()) < DEPS );
00605   assert ( TMath::Abs(RR.YZ() - R.YZ()) < DEPS );
00606   assert ( TMath::Abs(RR.ZX() - R.ZX()) < DEPS );
00607   assert ( TMath::Abs(RR.ZY() - R.ZY()) < DEPS );
00608   assert ( TMath::Abs(RR.ZZ() - R.ZZ()) < DEPS );
00609 
00610   R = C;                                           // transform()
00611   R.Transform(B);
00612   R.Transform(A);
00613   assert ( TMath::Abs(RR.XX() - R.XX()) < DEPS );
00614   assert ( TMath::Abs(RR.XY() - R.XY()) < DEPS );
00615   assert ( TMath::Abs(RR.XZ() - R.XZ()) < DEPS );
00616   assert ( TMath::Abs(RR.YX() - R.YX()) < DEPS );
00617   assert ( TMath::Abs(RR.YY() - R.YY()) < DEPS );
00618   assert ( TMath::Abs(RR.YZ() - R.YZ()) < DEPS );
00619   assert ( TMath::Abs(RR.ZX() - R.ZX()) < DEPS );
00620   assert ( TMath::Abs(RR.ZY() - R.ZY()) < DEPS );
00621   assert ( TMath::Abs(RR.ZZ() - R.ZZ()) < DEPS );
00622 
00623   R = RR.Inverse();                                // inverse()
00624   for(i=0; i<3; i++) {
00625     for(k=0; k<3; k++) {
00626       assert ( RR(i,k) == R(k,i) );
00627     }
00628   }
00629 
00630   R.Invert();                                      // invert()
00631   assert ( RR == R );
00632 
00633   R = TRotation();                                  // rotateAxes()
00634   R.RotateAxes( TVector3(RR.XX(), RR.YX(), RR.ZX()),
00635                 TVector3(RR.XY(), RR.YY(), RR.ZY()),
00636                 TVector3(RR.XZ(), RR.YZ(), RR.ZZ()) );
00637   assert ( RR == R );
00638 
00639   double ang=2.*TMath::Pi()/9.;                           // rotate()
00640   R = TRotation();
00641   R.Rotate(ang, V);
00642 
00643   RR = TRotation();
00644   RR.RotateZ(-(V.Phi()));
00645   RR.RotateY(-(V.Theta()));
00646   RR.RotateZ(ang);
00647   RR.RotateY(V.Theta());
00648   RR.RotateZ(V.Phi());
00649 
00650   assert ( TMath::Abs(RR.XX() - R.XX()) < DEPS );
00651   assert ( TMath::Abs(RR.XY() - R.XY()) < DEPS );
00652   assert ( TMath::Abs(RR.XZ() - R.XZ()) < DEPS );
00653   assert ( TMath::Abs(RR.YX() - R.YX()) < DEPS );
00654   assert ( TMath::Abs(RR.YY() - R.YY()) < DEPS );
00655   assert ( TMath::Abs(RR.YZ() - R.YZ()) < DEPS );
00656   assert ( TMath::Abs(RR.ZX() - R.ZX()) < DEPS );
00657   assert ( TMath::Abs(RR.ZY() - R.ZY()) < DEPS );
00658   assert ( TMath::Abs(RR.ZZ() - R.ZZ()) < DEPS );
00659 
00660   TVector3 Vu = V.Unit();                           // getAngleAxis
00661   R.AngleAxis(ang, V);
00662   assert ( TMath::Abs(ang   - 2.*TMath::Pi()/9.) < DEPS );
00663   assert ( TMath::Abs(V.X() - Vu.X())     < DEPS );
00664   assert ( TMath::Abs(V.Y() - Vu.Y())     < DEPS );
00665   assert ( TMath::Abs(V.Z() - Vu.Z())     < DEPS );
00666 
00667   assert ( TMath::Abs(RR.PhiX()-TMath::ATan2(RR.YX(),RR.XX())) < DEPS ); // phiX()
00668   assert ( TMath::Abs(RR.PhiY()-TMath::ATan2(RR.YY(),RR.XY())) < DEPS ); // phiY()
00669   assert ( TMath::Abs(RR.PhiZ()-TMath::ATan2(RR.YZ(),RR.XZ())) < DEPS ); // phiZ()
00670 
00671   assert ( TMath::Abs(RR.ThetaX()-TMath::ACos(RR.ZX())) < DEPS );        // thetaX()
00672   assert ( TMath::Abs(RR.ThetaY()-TMath::ACos(RR.ZY())) < DEPS );        // thetaY()
00673   assert ( TMath::Abs(RR.ThetaZ()-TMath::ACos(RR.ZZ())) < DEPS );        // thetaZ()
00674 
00675   return 0;
00676 }

Generated on Tue Jul 5 15:16:19 2011 for ROOT_528-00b_version by  doxygen 1.5.1