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

testLorentzVector.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 // $Id: testLorentzVector.cc,v 1.2 2003/08/13 20:00:14 garren Exp $
3 // ---------------------------------------------------------------------------
4 //
5 // This file is a part of what might become CLHEP -
6 // a Class Library for High Energy Physics.
7 //
8 // This is a small program for testing the HepLorentzVector class
9 // and the interaction with the HepLorentzRotation class.
10 //
11 
12 #include "CLHEP/Units/GlobalSystemOfUnits.h" // to see shadowing problems
13 #include "CLHEP/Vector/defs.h"
14 #include "CLHEP/Vector/LorentzVector.h"
15 #include "CLHEP/Vector/LorentzRotation.h"
16 #include "CLHEP/Vector/Sqr.h"
17 #include <iostream>
18 #include <cmath>
19 #include <stdlib.h>
20 #include <assert.h>
21 
22 using namespace CLHEP;
23 
24 #define DEPS 1.0e-14
25 #define FEPS 1.0e-6
26 
27 bool approx(double a, double b, double eps) {
28  return bool( std::abs(a-b) < eps );
29 }
30 
31 bool
32 test(const HepLorentzVector & p, double x, double y, double z, double e,
33  double eps) {
34  bool t = bool( approx(p.x(), x, eps) && approx(p.y(), y, eps) &&
35  approx(p.z(), z, eps) && approx(p.t(), e, eps));
36  if ( !t ) std::cerr << p << std::endl
37  << x << '\t' << y << '\t' << z << '\t' << e
38  << std::endl;
39  return t;
40 }
41 
43  v3 = Hep3Vector(3.,2.,1.);
44  assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
45 }
46 
47 void conversion_test(const Hep3Vector & v3, const HepLorentzVector & v4) {
48  assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
49 }
50 
51 bool
52 test(const HepLorentzVector & p, const HepLorentzVector & q, double eps) {
53  bool t = bool( approx(p.x(), q.x(), eps) &&
54  approx(p.y(), q.y(), eps) &&
55  approx(p.z(), q.z(), eps) &&
56  approx(p.t(), q.t(), eps));
57  if ( !t ) std::cerr << p << std::endl
58  << q << std::endl;
59  return t;
60 }
61 
62 int main () {
63  HepLorentzVector v4(1.,2.,3.,4.);
64  const HepLorentzVector v4const(1.,2.,3.,4);
65  conversion_test(v4,v4);
66  conversion_test(v4const, v4const);
67 
68  Hep3Vector f3x(1.0), f3y(0.0, 1.0), f3z(0.0, 0.0, 1.0);
69  Hep3Vector d30, d3x(1.0), d3y(0.0, 1.0), d3z(0.0, 0.0, 1.0);
70 
71 // test constructors:
72 
74  if ( !test(d0, 0.0, 0.0, 0.0, 0.0, DEPS) ) exit(1);
75  HepLorentzVector d1(d3x, 1.0);
76  if ( !test(d1, 1.0, 0.0, 0.0, 1.0, DEPS) ) exit(1);
77  HepLorentzVector d2(d3x + d3y, std::sqrt(2.0));
78  if ( !test(d2, 1.0, 1.0, 0.0, std::sqrt(2.0), DEPS) ) exit(1);
79  HepLorentzVector d3(d3z + d2, std::sqrt(3.0));
80  if ( !test(d3, 1.0, 1.0, 1.0, std::sqrt(3.0), DEPS) ) exit(1);
81  HepLorentzVector d4(0.0, 0.0, 0.0, 1.0);
82  if ( !test(d4,0.0, 0.0, 0.0, 1.0, DEPS) ) exit(1);
83  HepLorentzVector d5(f3x, f3x.mag()); if ( !test(d5, d1, FEPS) ) exit(1);
84  HepLorentzVector d6(d3x+f3y, (d3x+f3y).mag());
85  if ( !test(d6, d2, FEPS) ) exit(1);
86  HepLorentzVector d7(f3x+f3y+f3z, (f3x+f3y+f3z).mag());
87  if ( !test(d7, d3, FEPS) ) exit(1);
88 
89  HepLorentzVector f0; if ( !test(f0, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(1);
90  HepLorentzVector f1(f3x, 1.0);
91  if ( !test(f1, 1.0, 0.0, 0.0, 1.0, FEPS) ) exit(1);
92  HepLorentzVector f2(f3x + f3y, std::sqrt(2.0));
93  if ( !test(f2, 1.0, 1.0, 0.0, std::sqrt(2.0), FEPS) ) exit(1);
94  HepLorentzVector f3(f3z + f2, std::sqrt(3.0));
95  if ( !test(f3, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(1);
96  HepLorentzVector f4(0.0, 0.0, 0.0, 1.0);
97  if ( !test(f4,0.0, 0.0, 0.0, 1.0, FEPS) ) exit(1);
98  HepLorentzVector f5(d3x, d3x.mag()); if ( !test(f5, f1, FEPS) ) exit(1);
99  HepLorentzVector f6(f3x+d3y, (f3x+d3y).mag());
100  if ( !test(f6, f2, FEPS) ) exit(1);
101  HepLorentzVector f7(d3x+d3y+d3z, (d3x+d3y+d3z).mag());
102  if ( !test(f7, f3, FEPS) ) exit(1);
103 
104  HepLorentzVector d8(f7); if ( !test(d8, d7, FEPS) ) exit(1);
105  HepLorentzVector d9(d7); if ( !test(d9, d7, DEPS) ) exit(1);
106  HepLorentzVector f8(f7); if ( !test(f8, d7, FEPS) ) exit(1);
107  HepLorentzVector f9(d7); if ( !test(f9, d7, FEPS) ) exit(1);
108 
109  HepLorentzVector d10(1.0, 1.0, 1.0, std::sqrt(3.0));
110  if ( !test(d10, d7, FEPS) ) exit(1);
111  HepLorentzVector f10(1.0, 1.0, 1.0, std::sqrt(3.0));
112  if ( !test(f10, f7, FEPS) ) exit(1);
113 
114  HepLorentzVector d11(d3x+d3y+d3z, 1.0);
115  if ( !test(d11, 1.0, 1.0, 1.0, 1.0, DEPS) ) exit(1);
116  HepLorentzVector f11(d3x+d3y+d3z, 1.0);
117  if ( !test(f11, 1.0, 1.0, 1.0, 1.0, FEPS) ) exit(1);
118 
119 // test input/output from a stream
120 
121  std::cin >> d0; if ( !test(d0, 1.1, 2.2, 3.3, 4.4, DEPS) ) exit(1);
122  std::cin >> f0; if ( !test(f0, 4.0, 3.0, 2.0, 1.0, FEPS) ) exit(1);
123  std::cout << d0 << std::endl;
124  std::cout << f0 << std::endl;
125 
126 // testing assignment
127 
128  d6 = d7; if ( !test(d6, d7, DEPS) ) exit(2);
129  d6 = f7; if ( !test(d6, d7, FEPS) ) exit(2);
130  f6 = d7; if ( !test(f6, f7, FEPS) ) exit(2);
131  f6 = f7; if ( !test(f6, f7, FEPS) ) exit(2);
132 
133  //testing addition and subtraction:
134 
135  d11 = d3 + d7 + f3;
136  if ( !test(d11, 3.0, 3.0, 3.0, std::sqrt(27.0), FEPS) ) exit(4);
137  f11 = d3 + d7 + f3;
138  if ( !test(f11, 3.0, 3.0, 3.0, std::sqrt(27.0), FEPS) ) exit(4);
139  d11 += d3;
140  if ( !test(d11, 4.0, 4.0, 4.0, std::sqrt(48.0), FEPS) ) exit(4);
141  f11 += f3;
142  if ( !test(f11, 4.0, 4.0, 4.0, std::sqrt(48.0), FEPS) ) exit(4);
143  d11 = d3 + d7 - f3;
144  if ( !test(d11, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(4);
145  if ( !test(-d11, -1.0, -1.0, -1.0, -std::sqrt(3.0), FEPS) ) exit(4);
146  f11 = d3 + f7 - d3;
147  if ( !test(f11, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(4);
148  if ( !test(-f11, -1.0, -1.0, -1.0, -std::sqrt(3.0), FEPS) ) exit(4);
149  d11 -= d3;
150  if ( !test(d11, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(4);
151  f11 -= f3;
152  if ( !test(f11, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(4);
153 
154  d11 = HepLorentzVector(1.0, 2.0, 3.0, 4.0);
155  d11 *= 2.;
156  if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
157  d11 = 2.*HepLorentzVector(1.0, 2.0, 3.0, 4.0);
158  if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
159  d11 = HepLorentzVector(1.0, 2.0, 3.0, 4.0)*2.;
160  if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
161 
162 // testing scalar products:
163 
164  if ( !approx(d1 * d2, std::sqrt(2.0)-1.0, DEPS) ) exit(5);
165  if ( !approx(d3.dot(d7), 0.0, FEPS) ) exit(5);
166  if ( !approx(d2 * f1, std::sqrt(2.0)-1.0, FEPS) ) exit(5);
167  if ( !approx(f3.dot(d7), 0.0, FEPS) ) exit(5);
168 
169 // testing components:
170 
171  d11 = HepLorentzVector(1.0, 1.0, 1.0, std::sqrt(7.0));
172  if ( !approx(d11.mag2(), 4.0, DEPS) ) exit(6);
173  if ( !approx(d11.mag(), 2.0, DEPS) ) exit(6);
174  if ( !approx(Hep3Vector(d11).mag2(), 3.0, DEPS) ) exit(6);
175  if ( !approx(Hep3Vector(d11).mag(), std::sqrt(3.0), DEPS) ) exit(6);
176  if ( !approx(d11.perp2(), 2.0, DEPS) ) exit(6);
177  if ( !approx(d11.perp(), std::sqrt(2.0), DEPS) ) exit(6);
178  f11 = HepLorentzVector(1.0, 1.0, 1.0, std::sqrt(7.0));
179  if ( !approx(f11.mag2(), 4.0, FEPS) ) exit(6);
180  if ( !approx(f11.mag(), 2.0, FEPS) ) exit(6);
181  if ( !approx(f11.vect().mag2(), 3.0, FEPS) ) exit(6);
182  if ( !approx(f11.vect().mag(), std::sqrt(3.0), FEPS) ) exit(6);
183  if ( !approx(f11.perp2(), 2.0, FEPS) ) exit(6);
184  if ( !approx(f11.perp(), std::sqrt(2.0), FEPS) ) exit(6);
185 
186 // testing boosts:
187 
188  d5 = d3 = d1 = HepLorentzVector(1.0, 2.0, -1.0, 3.0);
189  d6 = d4 = d2 = HepLorentzVector(-1.0, 1.0, 2.0, 4.0);
190  double M = (d1 + d2).mag();
191  double dm1 = d1.mag();
192  double dm2 = d2.mag();
193  double p2 = (sqr(M)-sqr(dm1+dm2))*(sqr(M)-sqr(dm1-dm2))/(4.0*sqr(M));
194  d30 = -(d1 + d2).boostVector();
195  d1.boost(d30);
196  double phi = d1.phi();
197  double theta = d1.theta();
198  d1.rotateZ(-phi);
199  d1.rotateY(-theta);
200  HepRotation r;
201  r.rotateZ(-phi);
202  HepLorentzRotation r1(d30), r2(r), r3, r4, r5;
203  r3.rotateY(-theta);
204  r4 = r3 * r2 * r1;
205  d2 *= r4;
206  if ( !test(d1, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
207  if ( !test(d2, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
208  d1.transform(r4.inverse());
209  if ( !test(d1, d3, DEPS) ) exit(7);
210  r5 *= r3;
211  r5 *= r;
212  r5 *= r1;
213  r5.invert();
214  d2 *= r5;
215  if ( !test(d2, d4, DEPS) ) exit(7);
216  r4 = r1;
217  r4.rotateZ(-phi);
218  r4.rotateY(-theta);
219  d3 *= r4;
220  d4 = r4 * d6;
221  if ( !test(d3, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
222  if ( !test(d4, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
223  r5 = r1.inverse();
224  r5 *= r.inverse();
225  r5 *= r3.inverse();
226  d4.transform(r5);
227  d3.transform(r5);
228 
229  if ( !test(d4, d6, DEPS) ) exit(7);
230  if ( !test(d3, d5, DEPS) ) exit(7);
231 
232  r5 = r1;
233  r5.transform(r);
234  r5.transform(r3);
235  d4.transform(r5);
236  d3.transform(r5);
237  if ( !test(d3, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
238  if ( !test(d4, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
239 
240  return 0;
241 }
f3
int(* f3)(int, bool)
Definition: testCategories.cc:153
test
bool test(const HepLorentzVector &p, double x, double y, double z, double e, double eps)
Definition: testLorentzVector.cc:32
CLHEP::HepLorentzVector::phi
double phi() const
CLHEP::HepLorentzVector::rotateY
HepLorentzVector & rotateY(double)
Definition: LorentzVector.cc:74
CLHEP::HepLorentzVector::mag
double mag() const
a
@ a
Definition: testCategories.cc:125
CLHEP::HepLorentzRotation::rotateZ
HepLorentzRotation & rotateZ(double delta)
Definition: LorentzRotation.cc:175
CLHEP::HepLorentzVector::transform
HepLorentzVector & transform(const HepRotation &)
sqr
T sqr(const T &x)
Definition: Geometry/CLHEP/Vector/Sqr.h:21
CLHEP::HepLorentzVector::y
double y() const
CLHEP::HepLorentzVector::rotateZ
HepLorentzVector & rotateZ(double)
Definition: LorentzVector.cc:78
CLHEP::HepLorentzVector::z
double z() const
b
@ b
Definition: testCategories.cc:125
CLHEP::HepLorentzVector::x
double x() const
CLHEP::HepRotation::inverse
HepRotation inverse() const
CLHEP::HepLorentzVector::vect
Hep3Vector vect() const
CLHEP::HepRotation::rotateZ
HepRotation & rotateZ(double delta)
Definition: Rotation.cc:92
CLHEP::HepRotation
Definition: Geometry/CLHEP/Vector/Rotation.h:48
CLHEP::Hep3Vector::x
double x() const
CLHEP::HepLorentzRotation
Definition: Geometry/CLHEP/Vector/LorentzRotation.h:54
CLHEP::HepLorentzVector::t
double t() const
CLHEP::HepLorentzVector::perp
double perp() const
CLHEP::HepLorentzRotation::inverse
HepLorentzRotation inverse() const
CLHEP::Hep3Vector::z
double z() const
f1
void(* f1)()
Definition: testCategories.cc:151
CLHEP
Definition: ClhepVersion.h:13
CLHEP::Hep3Vector::mag
double mag() const
Hep3Vector
Issues Concerning the PhysicsVectors CLHEP Vector Merge The merge of ZOOM PhysicsVdectors and the CLHEP Vector package is completed The purpose of this document is to list the major issues that affected the merge of these and where relevant describe the resolutions More detailed documents describe more minor issues General Approach As agreed at the June CLHEP the approach is to combine the features of each ZOOM class with the corresponding CLHEP class expanding the interface to create a single lingua franca of what a Hep3Vector(for example) means. We are not forming SpaceVector as an class derived from Hep3Vector and enhancing it in that way. Another rule imposed by the agreement is to avoid using the Exceptions package(even though that will later go into CLHEP for other uses). A desirable goal is to avoid cluttering the interface and enlarging the code linked in when ordinary CLHEP Vector functionallity is used. To this end
CLHEP::HepLorentzVector::mag2
double mag2() const
CLHEP::Hep3Vector::mag2
double mag2() const
CLHEP::HepLorentzRotation::invert
HepLorentzRotation & invert()
CLHEP::Hep3Vector
Definition: Geometry/CLHEP/Vector/ThreeVector.h:41
main
int main()
Definition: testLorentzVector.cc:62
mag2
the default is TimePositive CLHEP always does TimePositive The cost of the flexibility is and mag2 and no other routines are altered Because of I am keeping metric flexibility in the package Pure CLHEP users need never see this mag2()
phi
we want to make it possible for the user to use the so we provide a few new for double double phi
Definition: minorMergeIssues.doc:20
approx
bool approx(double a, double b, double eps)
Definition: testLorentzVector.cc:27
CLHEP::HepLorentzRotation::rotateY
HepLorentzRotation & rotateY(double delta)
Definition: LorentzRotation.cc:163
CLHEP::HepLorentzVector::boost
HepLorentzVector & boost(double, double, double)
Definition: LorentzVector.cc:57
FEPS
#define FEPS
Definition: testLorentzVector.cc:25
CLHEP::Hep3Vector::y
double y() const
DEPS
#define DEPS
Definition: testLorentzVector.cc:24
CLHEP::HepLorentzVector
Definition: Geometry/CLHEP/Vector/LorentzVector.h:72
f2
int(* f2)(int)
Definition: testCategories.cc:152
exit
it has advantages For I leave the ZMthrows but substitute I replaced ZMthrow with ZMthrowA in this package when will issue its message and exit(-1). Also
x
any side effects of that construction would occur twice The semantics of throw x
Definition: whyZMthrowRethrows.txt:37
CLHEP::HepLorentzVector::perp2
double perp2() const
CLHEP::HepLorentzVector::theta
double theta() const
conversion_test
void conversion_test(Hep3Vector &v3, HepLorentzVector &v4)
Definition: testLorentzVector.cc:42
theta
we want to make it possible for the user to use the so we provide a few new for double theta
Definition: minorMergeIssues.doc:20
CLHEP::HepLorentzRotation::transform
HepLorentzRotation & transform(const HepBoost &b)