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
22using namespace CLHEP;
23
24#define DEPS 1.0e-14
25#define FEPS 1.0e-6
26
27bool approx(double a, double b, double eps) {
28 return bool( std::abs(a-b) < eps );
29}
30
31bool
32test(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
47void conversion_test(const Hep3Vector & v3, const HepLorentzVector & v4) {
48 assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
49}
50
51bool
52test(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
62int 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}
T sqr(const T &x)
double z() const
double x() const
double mag2() const
double y() const
double mag() const
HepLorentzRotation & rotateY(double delta)
HepLorentzRotation & rotateZ(double delta)
HepLorentzRotation inverse() const
HepLorentzRotation & transform(const HepBoost &b)
HepLorentzRotation & invert()
double theta() const
HepLorentzVector & boost(double, double, double)
double dot(const HepLorentzVector &) const
Hep3Vector vect() const
HepLorentzVector & rotateZ(double)
double perp2() const
HepLorentzVector & rotateY(double)
HepLorentzVector & transform(const HepRotation &)
HepRotation inverse() const
HepRotation & rotateZ(double delta)
Definition: Rotation.cc:92
#define exit(x)
int(* f2)(int)
int(* f3)(int, bool)
void(* f1)()
@ b
@ a
#define FEPS
#define DEPS
bool test(const HepLorentzVector &p, double x, double y, double z, double e, double eps)
void conversion_test(Hep3Vector &v3, HepLorentzVector &v4)
int main()
bool approx(double a, double b, double eps)