CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
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
42// don't generate warnings about unused parameter inside assert
43#if defined __GNUC__
44 #if __GNUC__ > 3 && __GNUC_MINOR__ > 6
45 #pragma GCC diagnostic push
46 #pragma GCC diagnostic ignored "-Wunused-parameter"
47 #endif
48#endif
49#ifdef __clang__
50 #pragma clang diagnostic push
51 #pragma clang diagnostic ignored "-Wunused-parameter"
52#endif
54 v3 = Hep3Vector(3.,2.,1.);
55 assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
56}
57
58void conversion_test(const Hep3Vector & v3, const HepLorentzVector & v4) {
59 assert (v3.x() == v4.x() && v3.y() == v4.y() && v3.z() == v4.z());
60}
61#if defined __GNUC__
62 #if __GNUC__ > 3 && __GNUC_MINOR__ > 6
63 #pragma GCC diagnostic pop
64 #endif
65#endif
66#ifdef __clang__
67 #pragma clang diagnostic pop
68#endif
69
70bool
71test(const HepLorentzVector & p, const HepLorentzVector & q, double eps) {
72 bool t = bool( approx(p.x(), q.x(), eps) &&
73 approx(p.y(), q.y(), eps) &&
74 approx(p.z(), q.z(), eps) &&
75 approx(p.t(), q.t(), eps));
76 if ( !t ) std::cerr << p << std::endl
77 << q << std::endl;
78 return t;
79}
80
81int main () {
82 HepLorentzVector v4(1.,2.,3.,4.);
83 const HepLorentzVector v4const(1.,2.,3.,4);
84 conversion_test(v4,v4);
85 conversion_test(v4const, v4const);
86
87 Hep3Vector f3x(1.0), f3y(0.0, 1.0), f3z(0.0, 0.0, 1.0);
88 Hep3Vector d30, d3x(1.0), d3y(0.0, 1.0), d3z(0.0, 0.0, 1.0);
89
90// test constructors:
91
93 if ( !test(d0, 0.0, 0.0, 0.0, 0.0, DEPS) ) exit(1);
94 HepLorentzVector d1(d3x, 1.0);
95 if ( !test(d1, 1.0, 0.0, 0.0, 1.0, DEPS) ) exit(1);
96 HepLorentzVector d2(d3x + d3y, std::sqrt(2.0));
97 if ( !test(d2, 1.0, 1.0, 0.0, std::sqrt(2.0), DEPS) ) exit(1);
98 HepLorentzVector d3(d3z + d2, std::sqrt(3.0));
99 if ( !test(d3, 1.0, 1.0, 1.0, std::sqrt(3.0), DEPS) ) exit(1);
100 HepLorentzVector d4(0.0, 0.0, 0.0, 1.0);
101 if ( !test(d4,0.0, 0.0, 0.0, 1.0, DEPS) ) exit(1);
102 HepLorentzVector d5(f3x, f3x.mag()); if ( !test(d5, d1, FEPS) ) exit(1);
103 HepLorentzVector d6(d3x+f3y, (d3x+f3y).mag());
104 if ( !test(d6, d2, FEPS) ) exit(1);
105 HepLorentzVector d7(f3x+f3y+f3z, (f3x+f3y+f3z).mag());
106 if ( !test(d7, d3, FEPS) ) exit(1);
107
108 HepLorentzVector f0; if ( !test(f0, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(1);
109 HepLorentzVector f1(f3x, 1.0);
110 if ( !test(f1, 1.0, 0.0, 0.0, 1.0, FEPS) ) exit(1);
111 HepLorentzVector f2(f3x + f3y, std::sqrt(2.0));
112 if ( !test(f2, 1.0, 1.0, 0.0, std::sqrt(2.0), FEPS) ) exit(1);
113 HepLorentzVector f3(f3z + f2, std::sqrt(3.0));
114 if ( !test(f3, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(1);
115 HepLorentzVector f4(0.0, 0.0, 0.0, 1.0);
116 if ( !test(f4,0.0, 0.0, 0.0, 1.0, FEPS) ) exit(1);
117 HepLorentzVector f5(d3x, d3x.mag()); if ( !test(f5, f1, FEPS) ) exit(1);
118 HepLorentzVector f6(f3x+d3y, (f3x+d3y).mag());
119 if ( !test(f6, f2, FEPS) ) exit(1);
120 HepLorentzVector f7(d3x+d3y+d3z, (d3x+d3y+d3z).mag());
121 if ( !test(f7, f3, FEPS) ) exit(1);
122
123 HepLorentzVector d8(f7); if ( !test(d8, d7, FEPS) ) exit(1);
124 HepLorentzVector d9(d7); if ( !test(d9, d7, DEPS) ) exit(1);
125 HepLorentzVector f8(f7); if ( !test(f8, d7, FEPS) ) exit(1);
126 HepLorentzVector f9(d7); if ( !test(f9, d7, FEPS) ) exit(1);
127
128 HepLorentzVector d10(1.0, 1.0, 1.0, std::sqrt(3.0));
129 if ( !test(d10, d7, FEPS) ) exit(1);
130 HepLorentzVector f10(1.0, 1.0, 1.0, std::sqrt(3.0));
131 if ( !test(f10, f7, FEPS) ) exit(1);
132
133 HepLorentzVector d11(d3x+d3y+d3z, 1.0);
134 if ( !test(d11, 1.0, 1.0, 1.0, 1.0, DEPS) ) exit(1);
135 HepLorentzVector f11(d3x+d3y+d3z, 1.0);
136 if ( !test(f11, 1.0, 1.0, 1.0, 1.0, FEPS) ) exit(1);
137
138// test input/output from a stream
139
140 std::cin >> d0; if ( !test(d0, 1.1, 2.2, 3.3, 4.4, DEPS) ) exit(1);
141 std::cin >> f0; if ( !test(f0, 4.0, 3.0, 2.0, 1.0, FEPS) ) exit(1);
142 std::cout << d0 << std::endl;
143 std::cout << f0 << std::endl;
144
145// testing assignment
146
147 d6 = d7; if ( !test(d6, d7, DEPS) ) exit(2);
148 d6 = f7; if ( !test(d6, d7, FEPS) ) exit(2);
149 f6 = d7; if ( !test(f6, f7, FEPS) ) exit(2);
150 f6 = f7; if ( !test(f6, f7, FEPS) ) exit(2);
151
152 //testing addition and subtraction:
153
154 d11 = d3 + d7 + f3;
155 if ( !test(d11, 3.0, 3.0, 3.0, std::sqrt(27.0), FEPS) ) exit(4);
156 f11 = d3 + d7 + f3;
157 if ( !test(f11, 3.0, 3.0, 3.0, std::sqrt(27.0), FEPS) ) exit(4);
158 d11 += d3;
159 if ( !test(d11, 4.0, 4.0, 4.0, std::sqrt(48.0), FEPS) ) exit(4);
160 f11 += f3;
161 if ( !test(f11, 4.0, 4.0, 4.0, std::sqrt(48.0), FEPS) ) exit(4);
162 d11 = d3 + d7 - f3;
163 if ( !test(d11, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(4);
164 if ( !test(-d11, -1.0, -1.0, -1.0, -std::sqrt(3.0), FEPS) ) exit(4);
165 f11 = d3 + f7 - d3;
166 if ( !test(f11, 1.0, 1.0, 1.0, std::sqrt(3.0), FEPS) ) exit(4);
167 if ( !test(-f11, -1.0, -1.0, -1.0, -std::sqrt(3.0), FEPS) ) exit(4);
168 d11 -= d3;
169 if ( !test(d11, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(4);
170 f11 -= f3;
171 if ( !test(f11, 0.0, 0.0, 0.0, 0.0, FEPS) ) exit(4);
172
173 d11 = HepLorentzVector(1.0, 2.0, 3.0, 4.0);
174 d11 *= 2.;
175 if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
176 d11 = 2.*HepLorentzVector(1.0, 2.0, 3.0, 4.0);
177 if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
178 d11 = HepLorentzVector(1.0, 2.0, 3.0, 4.0)*2.;
179 if ( !test(d11, 2.0, 4.0, 6.0, 8.0, DEPS) ) exit(4);
180
181// testing scalar products:
182
183 if ( !approx(d1 * d2, std::sqrt(2.0)-1.0, DEPS) ) exit(5);
184 if ( !approx(d3.dot(d7), 0.0, FEPS) ) exit(5);
185 if ( !approx(d2 * f1, std::sqrt(2.0)-1.0, FEPS) ) exit(5);
186 if ( !approx(f3.dot(d7), 0.0, FEPS) ) exit(5);
187
188// testing components:
189
190 d11 = HepLorentzVector(1.0, 1.0, 1.0, std::sqrt(7.0));
191 if ( !approx(d11.mag2(), 4.0, DEPS) ) exit(6);
192 if ( !approx(d11.mag(), 2.0, DEPS) ) exit(6);
193 if ( !approx(Hep3Vector(d11).mag2(), 3.0, DEPS) ) exit(6);
194 if ( !approx(Hep3Vector(d11).mag(), std::sqrt(3.0), DEPS) ) exit(6);
195 if ( !approx(d11.perp2(), 2.0, DEPS) ) exit(6);
196 if ( !approx(d11.perp(), std::sqrt(2.0), DEPS) ) exit(6);
197 f11 = HepLorentzVector(1.0, 1.0, 1.0, std::sqrt(7.0));
198 if ( !approx(f11.mag2(), 4.0, FEPS) ) exit(6);
199 if ( !approx(f11.mag(), 2.0, FEPS) ) exit(6);
200 if ( !approx(f11.vect().mag2(), 3.0, FEPS) ) exit(6);
201 if ( !approx(f11.vect().mag(), std::sqrt(3.0), FEPS) ) exit(6);
202 if ( !approx(f11.perp2(), 2.0, FEPS) ) exit(6);
203 if ( !approx(f11.perp(), std::sqrt(2.0), FEPS) ) exit(6);
204
205// testing boosts:
206
207 d5 = d3 = d1 = HepLorentzVector(1.0, 2.0, -1.0, 3.0);
208 d6 = d4 = d2 = HepLorentzVector(-1.0, 1.0, 2.0, 4.0);
209 double M = (d1 + d2).mag();
210 double dm1 = d1.mag();
211 double dm2 = d2.mag();
212 double p2 = (sqr(M)-sqr(dm1+dm2))*(sqr(M)-sqr(dm1-dm2))/(4.0*sqr(M));
213 d30 = -(d1 + d2).boostVector();
214 d1.boost(d30);
215 double phi = d1.phi();
216 double theta = d1.theta();
217 d1.rotateZ(-phi);
218 d1.rotateY(-theta);
219 HepRotation r;
220 r.rotateZ(-phi);
221 HepLorentzRotation r1(d30), r2(r), r3, r4, r5;
222 r3.rotateY(-theta);
223 r4 = r3 * r2 * r1;
224 d2 *= r4;
225 if ( !test(d1, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
226 if ( !test(d2, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
227 d1.transform(r4.inverse());
228 if ( !test(d1, d3, DEPS) ) exit(7);
229 r5 *= r3;
230 r5 *= r;
231 r5 *= r1;
232 r5.invert();
233 d2 *= r5;
234 if ( !test(d2, d4, DEPS) ) exit(7);
235 r4 = r1;
236 r4.rotateZ(-phi);
237 r4.rotateY(-theta);
238 d3 *= r4;
239 d4 = r4 * d6;
240 if ( !test(d3, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
241 if ( !test(d4, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
242 r5 = r1.inverse();
243 r5 *= r.inverse();
244 r5 *= r3.inverse();
245 d4.transform(r5);
246 d3.transform(r5);
247
248 if ( !test(d4, d6, DEPS) ) exit(7);
249 if ( !test(d3, d5, DEPS) ) exit(7);
250
251 r5 = r1;
252 r5.transform(r);
253 r5.transform(r3);
254 d4.transform(r5);
255 d3.transform(r5);
256 if ( !test(d3, 0.0, 0.0, std::sqrt(p2), std::sqrt(p2 + sqr(dm1)), DEPS) ) exit(7);
257 if ( !test(d4, 0.0, 0.0, -std::sqrt(p2), std::sqrt(p2 + sqr(dm2)), DEPS) ) exit(7);
258
259 return 0;
260}
T sqr(const T &x)
Definition: Sqr.h:21
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:88
#define exit(x)
#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)