HIPO  4.3.0
High Performance Output data format for experimental physics
fizika.h
Go to the documentation of this file.
1 /*****************************************************************************************
2 * ██╗ ██╗███████╗ ██████╗████████╗ ██████╗ ██████╗
3 * ██║ ██║██╔════╝██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗
4 * ██║ ██║█████╗ ██║ ██║ ██║ ██║██████╔╝
5 * ╚██╗ ██╔╝██╔══╝ ██║ ██║ ██║ ██║██╔══██╗
6 * ╚████╔╝ ███████╗╚██████╗ ██║ ╚██████╔╝██║ ██║
7 * ╚═══╝ ╚══════╝ ╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝
8 *
9 * ██╗ ██╗██████╗ ██████╗ █████╗ ██████╗ ██╗ ██╗
10 * ██║ ██║██╔══██╗██╔══██╗██╔══██╗██╔══██╗╚██╗ ██╔╝
11 * ██║ ██║██████╔╝██████╔╝███████║██████╔╝ ╚████╔╝
12 * ██║ ██║██╔══██╗██╔══██╗██╔══██║██╔══██╗ ╚██╔╝
13 * ███████╗██║██████╔╝██║ ██║██║ ██║██║ ██║ ██║
14 * ╚══════╝╚═╝╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
15 * DESCRIPTION: VECTOR LIBRARY for physics calcuations, includes a 3d vector class and
16 * a Lorentz vector class. The library comes as an include file only.
17 * thre is no need to compile, just include this file into your code.
18 * Author: G.Gavalian
19 * Date : Wed Nov 18 22:43:50 EST 2009
20 *********************************************************************************************/
21 
27 
28 #ifndef __VECTOR_LIB__
29 #define __VECTOR_LIB__
30 
31 
32 #include <iostream>
33 #include <string>
34 #include <vector>
35 #include <fstream>
36 #include <iomanip>
37 #include <cmath>
38 
39 namespace fizika {
40 
47 class vector3 {
48 private:
49  double cX;
50  double cY;
51  double cZ;
52 public:
53 
55 vector3 (){}
57 vector3 (double x, double y, double z){setXYZ(x,y,z);}
58 
60 
62  void setXYZ(double x, double y, double z){cX=x;cY=y;cZ=z;}
67 void setMagThetaPhi(double mag, double theta, double phi)
68 {
69  double amag = fabs(mag); cX = amag * sin(theta) * cos(phi);
70  cY = amag * sin(theta) * sin(phi);cZ = amag * cos(theta);
71 }
72 
73 
75 double theta() const
76 { return cX == 0.0 && cY == 0.0 && cZ == 0.0 ? 0.0 : atan2(perp(),cZ);}
77 
79 double phi() const
80 { return cX == 0.0 && cY == 0.0 ? 0.0 : atan2(cY,cX);}
81 
83 double perp() const
84 { return sqrt(cX*cX + cY*cY);}
85 
87 double perp2() const
88 { return (cX*cX + cY*cY);}
89 
91 double mag2() const
92 {return (cX*cX + cY*cY + cZ*cZ);}
93 
95 double mag() const
96 { return sqrt(mag2());}
97 
99  double x() const { return cX;};
101  double y() const { return cY;};
103  double z() const { return cZ;};
104 
106  void setX(double _x){cX=_x;};
108  void setY(double _y){cY=_y;};
110  void setZ(double _z){cZ=_z;};
111 
115  double angle(const vector3 &q){
116  double ptot2 = mag2()*q.mag2();
117  if(ptot2 <= 0) {
118  return 0.0;
119  } else {
120  double arg = dot(q)/sqrt(ptot2);
121  if(arg > 1.0) arg = 1.0;
122  if(arg < -1.0) arg = -1.0;
123  return acos(arg);
124  }
125  }
128 void rotateX(double angle)
129 {
130  double s = sin(angle); double c = cos(angle); double yy = cY;
131  cY = c*yy - s*cZ; cZ = s*yy + c*cZ;
132 }
135 void rotateY(double angle)
136 {
137  double s = sin(angle); double c = cos(angle); double zz = cZ;
138  cZ = c*zz - s*cX; cX = s*zz + c*cX;
139 }
142 void rotateZ(double angle)
143 {
144  double s = sin(angle); double c = cos(angle); double xx = cX;
145  cX = c*xx - s*cY; cY = s*xx + c*cY;
146 }
147 
149 vector3 unit() const;
150 
153 vector3 cross(const vector3 &vec) const
154 {
155  return vector3(cY*vec.z()-vec.y()*cZ, cZ*vec.x()-vec.z()*cX,
156  cX*vec.y()-vec.x()*cY);
157 }
160 double dot(const vector3 &vec) const
161 { return (cX*vec.x() + cY*vec.y() + cZ*vec.z());}
162 
163 
166  void print(const char *_line = "V3 ")
167  {printf("%s : %9.5f %9.5f %9.5f (Mag = %9.5f )\n",
168  _line,x(),y(),z(),mag());};
169 
171  const vector3 &operator=(const vector3 &vec){
172  this->setXYZ(vec.x(),vec.y(),vec.z());
173  return *this;
174  }
176  inline vector3 &operator += (const vector3 &);
178  inline vector3 &operator -= (const vector3 &);
179 
180 };
181 
182 
184 vector3 operator+(const vector3 &a, const vector3 &b)
185 { return vector3(a.x() + b.x(),a.y() + b.y(),a.z() + b.z());}
186 
188 vector3 operator-(const vector3 &a, const vector3 &b)
189 { return vector3(a.x() - b.x(), a.y() - b.y(), a.z() - b.z());}
190 
192 vector3 operator*(double a, const vector3 &b)
193 { return vector3(a*b.x(),a*b.y(),a*b.z());}
194 
196 vector3 operator*(const vector3 &b,double a)
197 {return vector3(a*b.x(),a*b.y(),a*b.z());}
198 
199 //-----------------------------------------------------------
201 { cX += p.cX; cY += p.cY; cZ += p.cZ; return *this; }
202 
204 { cX -= p.cX; cY -= p.cY; cZ -= p.cZ; return *this; }
205 
206 
213 class lorentz4 {
214 private:
215 
216  vector3 fVect;
217  double fE;
218 
219 public:
220 
224 lorentz4 (double _px, double _py, double _pz, double _e)
225 { fVect.setXYZ(_px,_py,_pz);fE = _e;}
227  lorentz4 (vector3 v, double energy) { fVect = v; fE = energy;};
229 
231 double px() const {return fVect.x();};
233  double py() const {return fVect.y();};
235  double pz() const {return fVect.z();};
237  double x() const {return fVect.x();};
239  double y() const {return fVect.y();};
241  double z() const {return fVect.z();};
243  double theta() const {return fVect.theta();};
245  double phi() const {return fVect.phi();};
246 
248  double m2() const {return (fE*fE-fVect.mag2());};
250  double p() const {return vect().mag();};
252  double e() const {return fE;};
254  double t() const {return fE;};
256  vector3 vect() const {return fVect;};
257 
258 
260  void setX(double _p){fVect.setX(_p);};
262  void setY(double _p){fVect.setY(_p);};
264  void setZ(double _p){fVect.setZ(_p);};
266  void setT(double _eg){fE=_eg;};
267 
269  void setXYZM(double _x, double _y, double _z, double _m)
270 { if(_m>0)
271  setXYZE(_x,_y,_z,sqrt(_x*_x+_y*_y+_z*_z+_m*_m));
272  else
273  setXYZE(_x,_y,_z,sqrt(_x*_x+_y*_y+_z*_z));
274 }
275 
277 void setXYZE(double _x, double _y, double _z, double _e)
278 {
279  fVect.setXYZ(_x,_y,_z);
280  fE = _e;
281 }
282 
284 void setVectM(vector3 &_v, double _mass)
285 {
286  setXYZM(_v.x(),_v.y(),_v.z(),_mass);
287 }
288 
291 void rotateX(double angle)
292  { fVect.rotateX(angle);};
295  void rotateY(double angle)
296  { fVect.rotateY(angle);};
299  void rotateZ(double angle)
300  { fVect.rotateZ(angle);};
301 
303 double m() const
304 {
305  double mm = m2();
306  return mm < 0.0 ? -sqrt(-mm) : sqrt(mm);
307 }
308 
311 {
312  // if(T()==0)
313  return vector3(px()/t(),py()/t(),pz()/t());
314 }
317 void boost(vector3 vb)
318 {
319  boost(vb.x(),vb.y(),vb.z());
320 }
321 
326 void boost(double bx, double by, double bz)
327 {
328  double b2 = bx*bx + by*by + bz*bz;
329  //register
330  double gamma = 1.0 / sqrt(1.0 - b2);
331  //register
332  double bp = bx*x() + by*y() + bz*z();
333  //register
334  double gamma2 = b2 > 0 ? (gamma - 1.0)/b2 : 0.0;
335 
336  setX(x() + gamma2*bp*bx + gamma*bx*t());
337  setY(y() + gamma2*bp*by + gamma*by*t());
338  setZ(z() + gamma2*bp*bz + gamma*bz*t());
339  setT(gamma*(t() + bp));
340 }
341 
344  void print(const char *_line = "LV ")
345  {printf("%s : %9.5f %9.5f %9.5f (Mag = %9.5f , M = %9.5f)\n",
346  _line,vect().x(),vect().y(),vect().z(),vect().mag(),m());};
347 
349  inline lorentz4 & operator = (const lorentz4 &);
351  inline lorentz4 operator + (const lorentz4 &) const;
353  inline lorentz4 operator - (const lorentz4 &) const;
355  inline lorentz4 & operator += (const lorentz4 &);
357  inline lorentz4 & operator -= (const lorentz4 &);
358 };
359 
361 {
362  fVect = q.vect();
363  fE = q.t();
364  return *this;
365 }
366 inline lorentz4 lorentz4::operator + (const lorentz4 & q) const {
367  return lorentz4(fVect+q.vect(), fE+q.t());
368 }
369 inline lorentz4 &lorentz4::operator += (const lorentz4 & q) { fVect += q.vect(); fE += q.t(); return *this;
370 }
371 inline lorentz4 lorentz4::operator - (const lorentz4 & q) const {
372 return lorentz4(fVect-q.vect(), fE-q.t());
373 }
375  fVect -= q.vect(); fE -= q.t(); return *this;
376 }
377 
378 }
380 #endif
Four-component Lorentz vector for relativistic kinematics.
Definition: fizika.h:213
lorentz4(double _px, double _py, double _pz, double _e)
Construct from px, py, pz, and energy.
Definition: fizika.h:224
void boost(double bx, double by, double bz)
Apply a Lorentz boost using beta components.
Definition: fizika.h:326
void setY(double _p)
Set the Y momentum component.
Definition: fizika.h:262
void rotateY(double angle)
Rotate the 3-momentum around the Y axis.
Definition: fizika.h:295
lorentz4()
Default constructor (uninitialized).
Definition: fizika.h:222
void setZ(double _p)
Set the Z momentum component.
Definition: fizika.h:264
lorentz4 & operator+=(const lorentz4 &)
Add another Lorentz vector to this one in place.
Definition: fizika.h:369
double y() const
Definition: fizika.h:239
double py() const
Definition: fizika.h:233
double p() const
Definition: fizika.h:250
lorentz4(vector3 v, double energy)
Construct from a 3-vector and energy.
Definition: fizika.h:227
void rotateX(double angle)
Rotate the 3-momentum around the X axis.
Definition: fizika.h:291
vector3 boostVector()
Definition: fizika.h:310
double m() const
Definition: fizika.h:303
double x() const
Definition: fizika.h:237
void setX(double _p)
Set the X momentum component.
Definition: fizika.h:260
double e() const
Definition: fizika.h:252
void setXYZE(double _x, double _y, double _z, double _e)
Set components from momentum and energy directly.
Definition: fizika.h:277
double t() const
Definition: fizika.h:254
double phi() const
Definition: fizika.h:245
double m2() const
Definition: fizika.h:248
void rotateZ(double angle)
Rotate the 3-momentum around the Z axis.
Definition: fizika.h:299
void setXYZM(double _x, double _y, double _z, double _m)
Set components from momentum and mass; energy is computed.
Definition: fizika.h:269
double pz() const
Definition: fizika.h:235
void boost(vector3 vb)
Apply a Lorentz boost using a boost vector.
Definition: fizika.h:317
void setT(double _eg)
Set the energy/time component.
Definition: fizika.h:266
void setVectM(vector3 &_v, double _mass)
Set from a 3-vector and mass; energy is computed.
Definition: fizika.h:284
lorentz4 & operator=(const lorentz4 &)
Copy-assignment operator.
Definition: fizika.h:360
lorentz4 & operator-=(const lorentz4 &)
Subtract another Lorentz vector from this one in place.
Definition: fizika.h:374
void print(const char *_line="LV ")
Print the Lorentz vector components, magnitude, and mass to stdout.
Definition: fizika.h:344
double z() const
Definition: fizika.h:241
vector3 vect() const
Definition: fizika.h:256
double theta() const
Definition: fizika.h:243
lorentz4 operator-(const lorentz4 &) const
Lorentz vector subtraction.
Definition: fizika.h:371
lorentz4 operator+(const lorentz4 &) const
Lorentz vector addition.
Definition: fizika.h:366
double px() const
Definition: fizika.h:231
Three-dimensional Cartesian vector.
Definition: fizika.h:47
vector3 & operator+=(const vector3 &)
Add another vector to this one in place.
Definition: fizika.h:200
double mag2() const
Definition: fizika.h:91
void rotateZ(double angle)
Rotate the vector around the Z axis.
Definition: fizika.h:142
void setXYZ(double x, double y, double z)
Set the Cartesian components.
Definition: fizika.h:62
double theta() const
Definition: fizika.h:75
double perp2() const
Definition: fizika.h:87
double z() const
Definition: fizika.h:103
vector3 & operator-=(const vector3 &)
Subtract another vector from this one in place.
Definition: fizika.h:203
double x() const
Definition: fizika.h:99
void setZ(double _z)
Set the Z component.
Definition: fizika.h:110
double mag() const
Definition: fizika.h:95
void print(const char *_line="V3 ")
Print the vector components and magnitude to stdout.
Definition: fizika.h:166
vector3 cross(const vector3 &vec) const
Compute the cross product with another vector.
Definition: fizika.h:153
void setX(double _x)
Set the X component.
Definition: fizika.h:106
void setMagThetaPhi(double mag, double theta, double phi)
Set the vector from spherical coordinates.
Definition: fizika.h:67
double y() const
Definition: fizika.h:101
double phi() const
Definition: fizika.h:79
double perp() const
Definition: fizika.h:83
vector3(double x, double y, double z)
Construct from Cartesian components.
Definition: fizika.h:57
double dot(const vector3 &vec) const
Compute the dot product with another vector.
Definition: fizika.h:160
double angle(const vector3 &q)
Compute the angle between this vector and another.
Definition: fizika.h:115
const vector3 & operator=(const vector3 &vec)
Copy-assignment operator.
Definition: fizika.h:171
void rotateY(double angle)
Rotate the vector around the Y axis.
Definition: fizika.h:135
void setY(double _y)
Set the Y component.
Definition: fizika.h:108
vector3 unit() const
vector3()
Default constructor (uninitialized components).
Definition: fizika.h:55
void rotateX(double angle)
Rotate the vector around the X axis.
Definition: fizika.h:128
Definition: fizika.h:39
vector3 operator*(double a, const vector3 &b)
Scalar-vector multiplication (scalar on the left).
Definition: fizika.h:192
vector3 operator+(const vector3 &a, const vector3 &b)
Vector addition.
Definition: fizika.h:184
vector3 operator-(const vector3 &a, const vector3 &b)
Vector subtraction.
Definition: fizika.h:188