MCBooster  1.0.1
Tool to generate MC phase space samples in parallel.
Vector4R.h
Go to the documentation of this file.
1 /*
2  * Vector4R.h
3  *
4  * obs.: inspired on the corresponding EvtGen class.
5  *
6  * Created on : Feb 25, 2016
7  * Author: Antonio Augusto Alves Junior
8  */
9 
10 /*
11  * This file is part of MCBooster.
12  *
13  * MCBooster is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation, either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * MCBooster is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with MCBooster. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #ifndef VECTOR4R_H_
28 #define VECTOR4R_H_
29 
30 #include <mcbooster/Config.h>
31 #include <mcbooster/GTypes.h>
32 #include <math.h>
33 #include <iostream>
34 #include <cmath>
35 #include "mcbooster/Vector3R.h"
36 /*
37 #ifndef __host__
38 #define __host__
39 #endif // __host__
40 
41 #ifndef __device__
42 #define __device__
43 #endif // __device_
44 */
45 using std::ostream;
46 
48 
49 namespace MCBooster
50 {
51 
52 
53 
54 class Vector4R
55 {
56 
57  __host__ __device__ inline friend Vector4R operator*(GReal_t d,
58  const Vector4R& v2);
59  __host__ __device__ inline friend Vector4R operator*(const Vector4R& v2,
60  GReal_t d);
61  __host__ __device__ inline friend Vector4R operator/(const Vector4R& v2,
62  GReal_t d);
63  __host__ __device__ inline friend GReal_t operator*(const Vector4R& v1,
64  const Vector4R& v2);
65  __host__ __device__ inline friend Vector4R operator+(const Vector4R& v1,
66  const Vector4R& v2);
67  __host__ __device__ inline friend Vector4R operator-(const Vector4R& v1,
68  const Vector4R& v2);
69 
70 public:
71  __host__ __device__ inline Vector4R();
72  __host__ __device__ inline Vector4R(GReal_t e, GReal_t px, GReal_t py,
73  GReal_t pz);
74  __host__ __device__ inline Vector4R(const Vector4R& other);
75  __host__ __device__ inline void set(GInt_t i, GReal_t d);
76  __host__ __device__ inline void set(GReal_t e, GReal_t px, GReal_t py,
77  GReal_t pz);
78  __host__ __device__ inline Vector4R& operator*=(GReal_t c);
79  __host__ __device__ inline Vector4R& operator/=(GReal_t c);
80  __host__ __device__ inline Vector4R& operator=(const Vector4R& v2);
81  __host__ __device__ inline Vector4R& operator+=(const Vector4R& v2);
82  __host__ __device__ inline Vector4R& operator-=(const Vector4R& v2);
83  __host__ __device__ inline GReal_t get(GInt_t i) const;
84  __host__ __device__ inline GReal_t cont(const Vector4R& v4) const;
85  __host__ inline friend std::ostream& operator<<(std::ostream& s,
86  const Vector4R& v);
87  __host__ __device__ inline GReal_t mass2() const;
88  __host__ __device__ inline GReal_t mass() const;
89  __host__ __device__ inline void applyRotateEuler(GReal_t alpha,
90  GReal_t beta, GReal_t gamma);
91  __host__ __device__ inline void applyBoostTo(const Vector4R& p4,
92  bool inverse = false);
93  __host__ __device__ inline void applyBoostTo(const Vector3R& boost,
94  bool inverse = false);
95  __host__ __device__ inline void applyBoostTo(const GReal_t bx,
96  const GReal_t by, const GReal_t bz, bool inverse = false);
97  __host__ __device__ inline Vector4R cross(const Vector4R& v2);
98  __host__ __device__ inline GReal_t dot(const Vector4R& v2) const;
99  __host__ __device__ inline GReal_t d3mag() const;
100 
101  // Added by AJB - calculate scalars in the rest frame of the current object
102  __host__ __device__ inline GReal_t scalartripler3(const Vector4R& p1,
103  const Vector4R& p2, const Vector4R& p3) const;
104  __host__ __device__ inline GReal_t dotr3(const Vector4R& p1,
105  const Vector4R& p2) const;
106  __host__ __device__ inline GReal_t mag2r3(const Vector4R& p1) const;
107  __host__ __device__ inline GReal_t magr3(const Vector4R& p1) const;
108 
109 private:
110 
111  GReal_t v[4];
112 
113  __host__ __device__ inline GReal_t Square(GReal_t x) const
114  {
115  return x * x;
116  }
117 
118 };
119 
120 Vector4R rotateEuler(const Vector4R& rs, GReal_t alpha, GReal_t beta,
121  GReal_t gamma);
122 Vector4R boostTo(const Vector4R& rs, const Vector4R& p4, bool inverse = false);
123 Vector4R boostTo(const Vector4R& rs, const Vector3R& boost,
124  bool inverse = false);
125 
127 {
128 
129  v[0] = v2.v[0];
130  v[1] = v2.v[1];
131  v[2] = v2.v[2];
132  v[3] = v2.v[3];
133 
134  return *this;
135 }
136 
138 {
139 
140  v[0] += v2.v[0];
141  v[1] += v2.v[1];
142  v[2] += v2.v[2];
143  v[3] += v2.v[3];
144 
145  return *this;
146 }
147 
149 {
150 
151  v[0] -= v2.v[0];
152  v[1] -= v2.v[1];
153  v[2] -= v2.v[2];
154  v[3] -= v2.v[3];
155 
156  return *this;
157 }
158 
159 inline GReal_t Vector4R::mass2() const
160 {
161 
162  return v[0] * v[0] - v[1] * v[1] - v[2] * v[2] - v[3] * v[3];
163 }
164 
165 inline Vector4R operator*(GReal_t c, const Vector4R& v2)
166 {
167 
168  return Vector4R(v2) *= c;
169 }
170 
171 inline Vector4R operator*(const Vector4R& v2, GReal_t c)
172 {
173 
174  return Vector4R(v2) *= c;
175 }
176 
177 inline Vector4R operator/(const Vector4R& v2, GReal_t c)
178 {
179 
180  return Vector4R(v2) /= c;
181 }
182 
184 {
185 
186  v[0] *= c;
187  v[1] *= c;
188  v[2] *= c;
189  v[3] *= c;
190 
191  return *this;
192 }
193 
195 {
196 
197  GReal_t cinv = 1.0 / c;
198  v[0] *= cinv;
199  v[1] *= cinv;
200  v[2] *= cinv;
201  v[3] *= cinv;
202 
203  return *this;
204 }
205 
206 inline GReal_t operator*(const Vector4R& v1, const Vector4R& v2)
207 {
208 
209  return v1.v[0] * v2.v[0] - v1.v[1] * v2.v[1] - v1.v[2] * v2.v[2]
210  - v1.v[3] * v2.v[3];
211 }
212 
213 inline GReal_t Vector4R::cont(const Vector4R& v4) const
214 {
215 
216  return v[0] * v4.v[0] - v[1] * v4.v[1] - v[2] * v4.v[2] - v[3] * v4.v[3];
217 }
218 
219 inline Vector4R operator-(const Vector4R& v1, const Vector4R& v2)
220 {
221 
222  return Vector4R(v1) -= v2;
223 }
224 
225 inline Vector4R operator+(const Vector4R& v1, const Vector4R& v2)
226 {
227 
228  return Vector4R(v1) += v2;
229 }
230 
231 inline GReal_t Vector4R::get(GInt_t i) const
232 {
233  return v[i];
234 }
235 
236 inline void Vector4R::set(GInt_t i, GReal_t d)
237 {
238 
239  v[i] = d;
240 }
241 
242 __host__ __device__ inline void Vector4R::set(GReal_t e, GReal_t p1, GReal_t p2,
243  GReal_t p3)
244 {
245 
246  v[0] = e;
247  v[1] = p1;
248  v[2] = p2;
249  v[3] = p3;
250 }
251 
252 using std::ostream;
253 
255 {
256  v[0] = 0.0;
257  v[1] = 0.0;
258  v[2] = 0.0;
259  v[3] = 0.0;
260 }
261 
263 {
264 
265  v[0] = e;
266  v[1] = p1;
267  v[2] = p2;
268  v[3] = p3;
269 }
270 
271 inline Vector4R::Vector4R(const Vector4R& other)
272 {
273 
274  v[0] = other.get(0);
275  v[1] = other.get(1);
276  v[2] = other.get(2);
277  v[3] = other.get(3);
278 }
279 
280 inline GReal_t Vector4R::mass() const
281 {
282 
283  GReal_t m2 = v[0] * v[0] - v[1] * v[1] - v[2] * v[2] - v[3] * v[3];
284 
285  if (m2 > 0.0)
286  {
287  return sqrt(m2);
288  }
289  else
290  {
291  return 0.0;
292  }
293 }
294 
295 inline Vector4R rotateEuler(const Vector4R& rs, GReal_t alpha, GReal_t beta,
296  GReal_t gamma)
297 {
298 
299  Vector4R tmp(rs);
300  tmp.applyRotateEuler(alpha, beta, gamma);
301  return tmp;
302 
303 }
304 
305 inline Vector4R boostTo(const Vector4R& rs, const Vector4R& p4, bool inverse)
306 {
307 
308  Vector4R tmp(rs);
309  tmp.applyBoostTo(p4, inverse);
310  return tmp;
311 
312 }
313 
314 inline Vector4R boostTo(const Vector4R& rs, const Vector3R& boost, bool inverse)
315 {
316 
317  Vector4R tmp(rs);
318  tmp.applyBoostTo(boost, inverse);
319  return tmp;
320 
321 }
322 
323 inline void Vector4R::applyRotateEuler(GReal_t phi, GReal_t theta, GReal_t ksi)
324 {
325 
326  GReal_t sp = sin(phi);
327  GReal_t st = sin(theta);
328  GReal_t sk = sin(ksi);
329  GReal_t cp = cos(phi);
330  GReal_t ct = cos(theta);
331  GReal_t ck = cos(ksi);
332 
333  GReal_t x = (ck * ct * cp - sk * sp) * v[1]
334  + (-sk * ct * cp - ck * sp) * v[2] + st * cp * v[3];
335  GReal_t y = (ck * ct * sp + sk * cp) * v[1]
336  + (-sk * ct * sp + ck * cp) * v[2] + st * sp * v[3];
337  GReal_t z = -ck * st * v[1] + sk * st * v[2] + ct * v[3];
338 
339  v[1] = x;
340  v[2] = y;
341  v[3] = z;
342 
343 }
344 
345 inline ostream& operator<<(ostream& s, const Vector4R& v)
346 {
347 
348  s << "(" << v.v[0] << "," << v.v[1] << "," << v.v[2] << "," << v.v[3]
349  << ")";
350 
351  return s;
352 
353 }
354 
355 inline void Vector4R::applyBoostTo(const Vector4R& p4, bool inverse)
356 {
357 
358  GReal_t e = p4.get(0);
359 
360  Vector3R boost(p4.get(1) / e, p4.get(2) / e, p4.get(3) / e);
361 
362  applyBoostTo(boost, inverse);
363 
364  return;
365 
366 }
367 
368 inline void Vector4R::applyBoostTo(const Vector3R& boost, bool inverse)
369 {
370 
371  GReal_t bx, by, bz, gamma, b2;
372 
373  bx = boost.get(0);
374  by = boost.get(1);
375  bz = boost.get(2);
376 
377  GReal_t bxx = bx * bx;
378  GReal_t byy = by * by;
379  GReal_t bzz = bz * bz;
380 
381  b2 = bxx + byy + bzz;
382 
383  if (b2 > 0.0 && b2 < 1.0)
384  {
385 
386  gamma = 1.0 / sqrt(1.0 - b2);
387 
388  GReal_t gb2 = (gamma - 1.0) / b2;
389 
390  GReal_t gb2xy = gb2 * bx * by;
391  GReal_t gb2xz = gb2 * bx * bz;
392  GReal_t gb2yz = gb2 * by * bz;
393 
394  GReal_t gbx = gamma * bx;
395  GReal_t gby = gamma * by;
396  GReal_t gbz = gamma * bz;
397 
398  GReal_t e2 = v[0];
399  GReal_t px2 = v[1];
400  GReal_t py2 = v[2];
401  GReal_t pz2 = v[3];
402 
403  if (inverse)
404  {
405  v[0] = gamma * e2 - gbx * px2 - gby * py2 - gbz * pz2;
406 
407  v[1] = -gbx * e2 + gb2 * bxx * px2 + px2 + gb2xy * py2
408  + gb2xz * pz2;
409 
410  v[2] = -gby * e2 + gb2 * byy * py2 + py2 + gb2xy * px2
411  + gb2yz * pz2;
412 
413  v[3] = -gbz * e2 + gb2 * bzz * pz2 + pz2 + gb2yz * py2
414  + gb2xz * px2;
415  }
416  else
417  {
418  v[0] = gamma * e2 + gbx * px2 + gby * py2 + gbz * pz2;
419 
420  v[1] = gbx * e2 + gb2 * bxx * px2 + px2 + gb2xy * py2 + gb2xz * pz2;
421 
422  v[2] = gby * e2 + gb2 * byy * py2 + py2 + gb2xy * px2 + gb2yz * pz2;
423 
424  v[3] = gbz * e2 + gb2 * bzz * pz2 + pz2 + gb2yz * py2 + gb2xz * px2;
425  }
426  }
427 
428 }
429 
430 inline void Vector4R::applyBoostTo(const GReal_t bx, const GReal_t by,
431  const GReal_t bz, bool inverse)
432 {
433 
434  GReal_t gamma, b2;
435 
436  GReal_t bxx = bx * bx;
437  GReal_t byy = by * by;
438  GReal_t bzz = bz * bz;
439 
440  b2 = bxx + byy + bzz;
441 
442  if (b2 > 0.0 && b2 < 1.0)
443  {
444 
445  gamma = 1.0 / sqrt(1.0 - b2);
446 
447  GReal_t gb2 = (gamma - 1.0) / b2;
448 
449  GReal_t gb2xy = gb2 * bx * by;
450  GReal_t gb2xz = gb2 * bx * bz;
451  GReal_t gb2yz = gb2 * by * bz;
452 
453  GReal_t gbx = gamma * bx;
454  GReal_t gby = gamma * by;
455  GReal_t gbz = gamma * bz;
456 
457  GReal_t e2 = v[0];
458  GReal_t px2 = v[1];
459  GReal_t py2 = v[2];
460  GReal_t pz2 = v[3];
461 
462  if (inverse)
463  {
464  v[0] = gamma * e2 - gbx * px2 - gby * py2 - gbz * pz2;
465 
466  v[1] = -gbx * e2 + gb2 * bxx * px2 + px2 + gb2xy * py2
467  + gb2xz * pz2;
468 
469  v[2] = -gby * e2 + gb2 * byy * py2 + py2 + gb2xy * px2
470  + gb2yz * pz2;
471 
472  v[3] = -gbz * e2 + gb2 * bzz * pz2 + pz2 + gb2yz * py2
473  + gb2xz * px2;
474  }
475  else
476  {
477  v[0] = gamma * e2 + gbx * px2 + gby * py2 + gbz * pz2;
478 
479  v[1] = gbx * e2 + gb2 * bxx * px2 + px2 + gb2xy * py2 + gb2xz * pz2;
480 
481  v[2] = gby * e2 + gb2 * byy * py2 + py2 + gb2xy * px2 + gb2yz * pz2;
482 
483  v[3] = gbz * e2 + gb2 * bzz * pz2 + pz2 + gb2yz * py2 + gb2xz * px2;
484  }
485  }
486 
487 }
489 {
490 
491  //Calcs the cross product. Added by djl on July 27, 1995.
492  //Modified for real vectros by ryd Aug 28-96
493 
494  Vector4R temp;
495 
496  temp.v[0] = 0.0;
497  temp.v[1] = v[2] * p2.v[3] - v[3] * p2.v[2];
498  temp.v[2] = v[3] * p2.v[1] - v[1] * p2.v[3];
499  temp.v[3] = v[1] * p2.v[2] - v[2] * p2.v[1];
500 
501  return temp;
502 }
503 
504 inline GReal_t Vector4R::d3mag() const
505 
506 // returns the 3 momentum mag.
507 {
508  GReal_t temp;
509 
510  temp = v[1] * v[1] + v[2] * v[2] + v[3] * v[3];
511 
512  temp = sqrt(temp);
513 
514  return temp;
515 } // r3mag
516 
517 inline GReal_t Vector4R::dot(const Vector4R& p2) const
518 {
519 
520  //Returns the dot product of the 3 momentum. Added by
521  //djl on July 27, 1995. for real!!!
522 
523  GReal_t temp;
524 
525  temp = v[1] * p2.v[1];
526  temp += v[2] * p2.v[2];
527  temp += v[3] * p2.v[3];
528 
529  return temp;
530 
531 } //dot
532 
533 
534 // Calculate the 3-d dot product of 4-vectors p1 and p2 in the rest frame of
535 // 4-vector p0
536 inline GReal_t Vector4R::dotr3(const Vector4R& p1, const Vector4R& p2) const
537 {
538  return 1 / mass2() * ((*this) * p1) * ((*this) * p2) - p1 * p2;
539 }
540 
541 // Calculate the 3-d magnitude squared of 4-vector p1 in the rest frame of
542 // 4-vector p0
543 inline GReal_t Vector4R::mag2r3(const Vector4R& p1) const
544 {
545  return Square((*this) * p1) / mass2() - p1.mass2();
546 }
547 
548 // Calculate the 3-d magnitude 4-vector p1 in the rest frame of 4-vector p0.
549 inline GReal_t Vector4R::magr3(const Vector4R& p1) const
550 {
551  return sqrt(mag2r3(p1));
552 }
553 }
554 #endif /* VECTOR4R_H_ */
__host__ __device__ friend Vector4R operator/(const Vector4R &v2, GReal_t d)
Definition: Vector4R.h:177
__host__ __device__ friend Vector4R operator*(GReal_t d, const Vector4R &v2)
Definition: Vector4R.h:165
__host__ __device__ GReal_t get(GInt_t i) const
Definition: Vector4R.h:231
__host__ __device__ Vector4R cross(const Vector4R &v2)
Definition: Vector4R.h:488
__host__ __device__ Vector4R & operator/=(GReal_t c)
Definition: Vector4R.h:194
__host__ __device__ GReal_t mag2r3(const Vector4R &p1) const
Definition: Vector4R.h:543
__host__ __device__ GReal_t d3mag() const
Definition: Vector4R.h:504
__host__ __device__ Vector4R & operator*=(GReal_t c)
Definition: Vector4R.h:183
__host__ __device__ Vector4R & operator=(const Vector4R &v2)
Definition: Vector4R.h:126
Vector3R operator+(const Vector3R &v1, const Vector3R &v2)
Definition: Vector3R.h:146
__host__ __device__ GReal_t dotr3(const Vector4R &p1, const Vector4R &p2) const
Definition: Vector4R.h:536
Vector3R rotateEuler(const Vector3R &v, GReal_t alpha, GReal_t beta, GReal_t gamma)
Definition: Vector3R.h:200
__host__ __device__ GReal_t get(GInt_t i) const
Definition: Vector3R.h:159
__host__ __device__ GReal_t mass2() const
Definition: Vector4R.h:159
__host__ __device__ Vector4R & operator-=(const Vector4R &v2)
Definition: Vector4R.h:148
__host__ __device__ Vector4R & operator+=(const Vector4R &v2)
Definition: Vector4R.h:137
__host__ __device__ GReal_t scalartripler3(const Vector4R &p1, const Vector4R &p2, const Vector4R &p3) const
__host__ __device__ GReal_t dot(const Vector4R &v2) const
Definition: Vector4R.h:517
__host__ __device__ void set(GInt_t i, GReal_t d)
Definition: Vector4R.h:236
Vector4R boostTo(const Vector4R &rs, const Vector4R &p4, bool inverse=false)
Definition: Vector4R.h:305
Vector3R operator/(const Vector3R &v1, GReal_t c)
Definition: Vector3R.h:134
ostream & operator<<(ostream &s, const Vector3R &v)
Definition: Vector3R.h:234
double GReal_t
Double 8 bytes or float 4 bytes.
Definition: GTypes.h:52
__host__ __device__ void applyBoostTo(const Vector4R &p4, bool inverse=false)
Definition: Vector4R.h:355
__host__ __device__ GReal_t magr3(const Vector4R &p1) const
Definition: Vector4R.h:549
__host__ __device__ friend Vector4R operator-(const Vector4R &v1, const Vector4R &v2)
Definition: Vector4R.h:219
Vector3R operator-(const Vector3R &v1, const Vector3R &v2)
Definition: Vector3R.h:152
__host__ friend std::ostream & operator<<(std::ostream &s, const Vector4R &v)
Definition: Vector4R.h:345
Vector3R operator*(GReal_t c, const Vector3R &v2)
Definition: Vector3R.h:122
int GInt_t
Signed integer 4 bytes (int)
Definition: GTypes.h:37
__host__ __device__ Vector4R()
Definition: Vector4R.h:254
__host__ __device__ GReal_t cont(const Vector4R &v4) const
Definition: Vector4R.h:213
__host__ __device__ void applyRotateEuler(GReal_t alpha, GReal_t beta, GReal_t gamma)
Definition: Vector4R.h:323
__host__ __device__ friend Vector4R operator+(const Vector4R &v1, const Vector4R &v2)
Definition: Vector4R.h:225
__host__ __device__ GReal_t mass() const
Definition: Vector4R.h:280