1 /***************************************************************************
5 * Author: Brian Lasiuk, Thomas Ullrich, April 1998
6 ***************************************************************************
10 * Remarks: Since not all compilers support member templates
11 * we have to specialize the templated member on these
12 * platforms. If member templates are not supported the
13 * ST_NO_MEMBER_TEMPLATES flag has to be set. tu.
15 * In the near future when all compilers can handle member
16 * templates this class should be cleaned up. A lot of
17 * redundant code can be removed as soon as the compilers
20 ***************************************************************************
23 * Revision 1.1.1.1 2007/04/25 15:38:41 panos
24 * Importing the HBT code dir
26 * Revision 1.1.1.1 2007/03/07 10:14:49 mchojnacki
27 * First version on CVS
29 * Revision 1.11 2005/09/22 20:09:20 fisyak
30 * Make AliFmLorentzVector persistent
32 * Revision 1.10 2005/07/06 18:49:56 fisyak
33 * Replace AliFmHelixD, AliFmLorentzVectorD,AliFmLorentzVectorF,AliFmMatrixD,AliFmMatrixF,AliFmPhysicalHelixD,AliFmThreeVectorD,AliFmThreeVectorF by templated version
35 * Revision 1.9 2005/03/28 06:02:45 perev
38 * Revision 1.8 2003/09/02 17:59:35 perev
39 * gcc 3.2 updates + WarnOff
41 * Revision 1.7 2003/05/01 19:24:31 ullrich
42 * Corrected problem in boost().
44 * Revision 1.6 1999/10/15 15:56:36 ullrich
45 * Changed output format in operator<<, added operator>>
47 * Revision 1.5 1999/06/04 18:01:36 ullrich
48 * New operators operator() and operator[] which can be used
51 * Revision 1.4 1999/04/14 23:12:07 fisyak
52 * Add __CINT__ to handle references
54 * Revision 1.3 1999/02/17 11:38:36 ullrich
55 * Removed specialization for 'long double'.
57 * Revision 1.2 1999/02/14 23:11:42 fisyak
60 * Revision 1.1 1999/01/30 03:59:02 fisyak
61 * Root Version of AliFmarClassLibrary
63 * Revision 1.1 1999/01/23 00:27:52 ullrich
66 **************************************************************************/
67 #ifndef ST_LORENTZ_VECTOR_HH
68 #define ST_LORENTZ_VECTOR_HH
70 #include "AliFmThreeVector.h"
71 template<class T> class AliFmLorentzVector {
73 AliFmLorentzVector(T = 0, T = 0, T = 0, T = 0);
74 virtual ~AliFmLorentzVector();
76 #ifndef ST_NO_MEMBER_TEMPLATES
77 template<class X> AliFmLorentzVector(const AliFmThreeVector<X>&, T);
78 template<class X> AliFmLorentzVector(T, const AliFmThreeVector<X>&);
80 template<class X> AliFmLorentzVector(const AliFmLorentzVector<X>&);
81 template<class X> AliFmLorentzVector<T>& operator=(const AliFmLorentzVector<X>&);
82 // AliFmLorentzVector(const AliFmLorentzVector<T>&); use default
83 // AliFmLorentzVector<T>& operator=(const AliFmLorentzVector<T>&); use default
85 AliFmLorentzVector(const AliFmThreeVector<float>&, T);
86 AliFmLorentzVector(T, const AliFmThreeVector<float>&);
87 AliFmLorentzVector(const AliFmLorentzVector<float>&);
89 AliFmLorentzVector(const AliFmThreeVector<double>&, T);
90 AliFmLorentzVector(T, const AliFmThreeVector<double>&);
91 AliFmLorentzVector(const AliFmLorentzVector<double>&);
93 AliFmLorentzVector<T>& operator=(const AliFmLorentzVector<float>&);
94 AliFmLorentzVector<T>& operator=(const AliFmLorentzVector<double>&);
105 T operator() (size_t) const;
106 T operator[] (size_t) const;
108 T& operator() (size_t);
109 T& operator[] (size_t);
111 const AliFmThreeVector<T>& vect() const;
122 #ifndef ST_NO_MEMBER_TEMPLATES
123 template <class X> void setVect(const AliFmThreeVector<X>&);
125 void setVect(const AliFmThreeVector<float>&);
126 void setVect(const AliFmThreeVector<double>&);
131 T pseudoRapidity() const;
145 #ifndef ST_NO_MEMBER_TEMPLATES
146 template<class X> AliFmLorentzVector<T> boost(const AliFmLorentzVector<X>&) const;
148 AliFmLorentzVector<T> boost(const AliFmLorentzVector<float>&) const;
149 AliFmLorentzVector<T> boost(const AliFmLorentzVector<double>&) const;
152 AliFmLorentzVector<T> operator- ();
153 AliFmLorentzVector<T> operator+ ();
154 AliFmLorentzVector<T>& operator*= (double);
155 AliFmLorentzVector<T>& operator/= (double);
157 #ifndef ST_NO_MEMBER_TEMPLATES
158 template<class X> bool operator == (const AliFmLorentzVector<X>&) const;
159 template<class X> bool operator != (const AliFmLorentzVector<X>&) const;
160 template<class X> AliFmLorentzVector<T>& operator+= (const AliFmLorentzVector<X>&);
161 template<class X> AliFmLorentzVector<T>& operator-= (const AliFmLorentzVector<X>&);
163 bool operator == (const AliFmLorentzVector<float>&) const;
164 bool operator != (const AliFmLorentzVector<float>&) const;
165 bool operator == (const AliFmLorentzVector<double>&) const;
166 bool operator != (const AliFmLorentzVector<double>&) const;
168 AliFmLorentzVector<T>& operator+= (const AliFmLorentzVector<float>&);
169 AliFmLorentzVector<T>& operator-= (const AliFmLorentzVector<float>&);
170 AliFmLorentzVector<T>& operator+= (const AliFmLorentzVector<double>&);
171 AliFmLorentzVector<T>& operator-= (const AliFmLorentzVector<double>&);
175 AliFmThreeVector<T> mThreeVector;
178 ClassDef(AliFmLorentzVector,3)
183 // Implementation of member functions
186 AliFmLorentzVector<T>::AliFmLorentzVector(T x, T y, T z, T t)
187 : mThreeVector(x, y, z), mX4(t) { /* nop */ }
190 AliFmLorentzVector<T>::~AliFmLorentzVector() { /* nopt */ }
193 const AliFmThreeVector<T>& AliFmLorentzVector<T>::vect() const
199 T AliFmLorentzVector<T>::m2() const
201 return (mX4*mX4 - mThreeVector*mThreeVector);
205 T AliFmLorentzVector<T>::plus() const { return (e() + pz()); }
208 T AliFmLorentzVector<T>::minus() const { return (e() - pz()); }
211 T AliFmLorentzVector<T>::m() const
215 return -::sqrt(-mass2);
217 return ::sqrt(mass2);
221 T AliFmLorentzVector<T>::mt2() const
223 return this->perp2() + m2();
227 T AliFmLorentzVector<T>::mt() const
230 // change to more optimal code ?
231 // return e()*e() - pz()*pz();
234 return -::sqrt(-massPerp2);
236 return ::sqrt(massPerp2);
240 void AliFmLorentzVector<T>::setPx(T x) {mThreeVector.setX(x);}
243 void AliFmLorentzVector<T>::setPy(T y) {mThreeVector.setY(y);}
246 void AliFmLorentzVector<T>::setPz(T z) {mThreeVector.setZ(z);}
249 void AliFmLorentzVector<T>::setX(T x) {mThreeVector.setX(x);}
252 void AliFmLorentzVector<T>::setY(T y) {mThreeVector.setY(y);}
255 void AliFmLorentzVector<T>::setZ(T z) {mThreeVector.setZ(z);}
258 void AliFmLorentzVector<T>::setT(T t) {mX4 = t;}
261 void AliFmLorentzVector<T>::setE(T e) {mX4 = e;}
264 T AliFmLorentzVector<T>::x() const {return mThreeVector.x();}
267 T AliFmLorentzVector<T>::y() const {return mThreeVector.y();}
270 T AliFmLorentzVector<T>::z() const {return mThreeVector.z();}
273 T AliFmLorentzVector<T>::px() const {return mThreeVector.x();}
276 T AliFmLorentzVector<T>::py() const {return mThreeVector.y();}
279 T AliFmLorentzVector<T>::pz() const {return mThreeVector.z();}
282 T AliFmLorentzVector<T>::e() const {return mX4;}
285 T AliFmLorentzVector<T>::t() const {return mX4;}
288 T AliFmLorentzVector<T>::perp() const {return mThreeVector.perp();}
291 T AliFmLorentzVector<T>::perp2() const {return mThreeVector.perp2();}
294 T AliFmLorentzVector<T>::pseudoRapidity() const {return mThreeVector.pseudoRapidity();}
297 T AliFmLorentzVector<T>::phi() const {return mThreeVector.phi();}
300 T AliFmLorentzVector<T>::theta() const {return mThreeVector.theta();}
303 T AliFmLorentzVector<T>::cosTheta() const {return mThreeVector.cosTheta();}
306 T AliFmLorentzVector<T>::operator() (size_t i) const
309 return mThreeVector(i);
313 #ifndef ST_NO_EXCEPTIONS
314 throw out_of_range("AliFmLorentzVector<T>::operator(): bad index");
316 cerr << "AliFmLorentzVector<T>::operator(): bad index." << endl;
323 T& AliFmLorentzVector<T>::operator() (size_t i)
326 return mThreeVector(i);
330 #ifndef ST_NO_EXCEPTIONS
331 throw out_of_range("AliFmLorentzVector<T>::operator(): bad index");
333 cerr << "AliFmLorentzVector<T>::operator(): bad index." << endl;
340 T AliFmLorentzVector<T>::operator[] (size_t i) const
343 return mThreeVector[i];
347 #ifndef ST_NO_EXCEPTIONS
348 throw out_of_range("AliFmLorentzVector<T>::operator[]: bad index");
350 cerr << "AliFmLorentzVector<T>::operator[]: bad index." << endl;
357 T& AliFmLorentzVector<T>::operator[] (size_t i)
360 return mThreeVector[i];
364 #ifndef ST_NO_EXCEPTIONS
365 throw out_of_range("AliFmLorentzVector<T>::operator[]: bad index");
367 cerr << "AliFmLorentzVector<T>::operator[]: bad index." << endl;
374 T AliFmLorentzVector<T>::rapidity() const
376 return 0.5*::log((mX4+mThreeVector.z())/(mX4-mThreeVector.z())+1e-20);
380 AliFmLorentzVector<T> AliFmLorentzVector<T>::operator- ()
382 return AliFmLorentzVector<T>(-mX4,-mThreeVector);
386 AliFmLorentzVector<T> AliFmLorentzVector<T>::operator+ ()
392 AliFmLorentzVector<T>& AliFmLorentzVector<T>::operator*= (double c)
400 AliFmLorentzVector<T>& AliFmLorentzVector<T>::operator/= (double c)
407 #ifndef ST_NO_MEMBER_TEMPLATES
412 AliFmLorentzVector<T>::AliFmLorentzVector(const AliFmThreeVector<X> &vec, T t)
413 : mThreeVector(vec), mX4(t) { /* nop */ }
417 AliFmLorentzVector<T>::AliFmLorentzVector(T t, const AliFmThreeVector<X> &vec)
418 : mThreeVector(vec), mX4(t) { /* nop */ }
422 AliFmLorentzVector<T>::AliFmLorentzVector(const AliFmLorentzVector<X> &vec)
423 : mThreeVector(vec.vect()), mX4(vec.t()) { /* nop */ }
427 AliFmLorentzVector<T>
428 AliFmLorentzVector<T>::boost(const AliFmLorentzVector<X>& pframe) const
430 T mass = abs(pframe);
431 AliFmThreeVector<T> eta = (-1./mass)*pframe.vect(); // gamma*beta
432 T gamma = fabs(pframe.e())/mass;
433 AliFmThreeVector<T> pl = ((this->vect()*eta)/(eta*eta))*eta; // longitudinal momentum
434 return AliFmLorentzVector<T>(gamma*this->e() - this->vect()*eta,
435 this->vect() + (gamma-1.)*pl - this->e()*eta);
440 void AliFmLorentzVector<T>::setVect(const AliFmThreeVector<X>& v)
447 AliFmLorentzVector<T>&
448 AliFmLorentzVector<T>::operator=(const AliFmLorentzVector<X>& vec)
450 mThreeVector = vec.vect();
458 AliFmLorentzVector<T>::operator== (const AliFmLorentzVector<X>& v) const
460 return (mThreeVector == v.vect()) && (mX4 == v.t());
466 AliFmLorentzVector<T>::operator!= (const AliFmLorentzVector<X>& v) const
468 return !(*this == v);
473 AliFmLorentzVector<T>&
474 AliFmLorentzVector<T>::operator+= (const AliFmLorentzVector<X>& v)
476 mThreeVector += v.vect();
483 AliFmLorentzVector<T>&
484 AliFmLorentzVector<T>::operator-= (const AliFmLorentzVector<X>& v)
486 mThreeVector -= v.vect();
495 AliFmLorentzVector<T>::AliFmLorentzVector(const AliFmThreeVector<float> &vec, T t)
496 : mThreeVector(vec), mX4(t) { /* nop */ }
499 AliFmLorentzVector<T>::AliFmLorentzVector(const AliFmThreeVector<double> &vec, T t)
500 : mThreeVector(vec), mX4(t) { /* nop */ }
503 AliFmLorentzVector<T>::AliFmLorentzVector(T t, const AliFmThreeVector<float> &vec)
504 : mThreeVector(vec), mX4(t) { /* nop */ }
507 AliFmLorentzVector<T>::AliFmLorentzVector(T t, const AliFmThreeVector<double> &vec)
508 : mThreeVector(vec), mX4(t) { /* nop */ }
511 AliFmLorentzVector<T>::AliFmLorentzVector(const AliFmLorentzVector<float> &vec)
512 : mThreeVector(vec.vect()), mX4(vec.t()) { /* nop */ }
515 AliFmLorentzVector<T>::AliFmLorentzVector(const AliFmLorentzVector<double> &vec)
516 : mThreeVector(vec.vect()), mX4(vec.t()) { /* nop */ }
519 AliFmLorentzVector<T>
520 AliFmLorentzVector<T>::boost(const AliFmLorentzVector<float>& pframe) const
522 T mass = abs(pframe);
523 AliFmThreeVector<T> eta = (-1./mass)*pframe.vect(); // gamma*beta
524 T gamma = fabs(pframe.e())/mass;
525 AliFmThreeVector<T> pl = ((this->vect()*eta)/(eta*eta))*eta; // longitudinal momentum
526 return AliFmLorentzVector<T>(gamma*this->e() - this->vect()*eta,
527 this->vect() + (gamma-1.)*pl - this->e()*eta);
531 AliFmLorentzVector<T>
532 AliFmLorentzVector<T>::boost(const AliFmLorentzVector<double>& pframe) const
534 T mass = abs(pframe);
535 AliFmThreeVector<T> eta = (-1./mass)*pframe.vect(); // gamma*beta
536 T gamma = fabs(pframe.e())/mass;
537 AliFmThreeVector<T> pl = ((this->vect()*eta)/(eta*eta))*eta; // longitudinal momentum
538 return AliFmLorentzVector<T>(gamma*this->e() - this->vect()*eta,
539 this->vect() + (gamma-1.)*pl - this->e()*eta);
543 void AliFmLorentzVector<T>::setVect(const AliFmThreeVector<float>& v)
549 void AliFmLorentzVector<T>::setVect(const AliFmThreeVector<double>& v)
555 AliFmLorentzVector<T>&
556 AliFmLorentzVector<T>::operator=(const AliFmLorentzVector<float>& vec)
558 mThreeVector = vec.vect();
564 AliFmLorentzVector<T>&
565 AliFmLorentzVector<T>::operator=(const AliFmLorentzVector<double>& vec)
567 mThreeVector = vec.vect();
574 AliFmLorentzVector<T>::operator== (const AliFmLorentzVector<float>& v) const
576 return (this->vect() == v.vect()) && (mX4 == v.t());
581 AliFmLorentzVector<T>::operator== (const AliFmLorentzVector<double>& v) const
583 return (mThreeVector == v.vect()) && (mX4 == v.t());
588 AliFmLorentzVector<T>::operator!= (const AliFmLorentzVector<float>& v) const
590 return !(*this == v);
595 AliFmLorentzVector<T>::operator!= (const AliFmLorentzVector<double>& v) const
597 return !(*this == v);
601 AliFmLorentzVector<T>&
602 AliFmLorentzVector<T>::operator+= (const AliFmLorentzVector<float>& v)
604 mThreeVector += v.vect();
610 AliFmLorentzVector<T>&
611 AliFmLorentzVector<T>::operator+= (const AliFmLorentzVector<double>& v)
613 mThreeVector += v.vect();
619 AliFmLorentzVector<T>&
620 AliFmLorentzVector<T>::operator-= (const AliFmLorentzVector<float>& v)
622 mThreeVector -= v.vect();
628 AliFmLorentzVector<T>&
629 AliFmLorentzVector<T>::operator-= (const AliFmLorentzVector<double>& v)
631 mThreeVector -= v.vect();
636 #endif // ST_NO_MEMBER_TEMPLATES
637 #endif /* ! __CINT__ */
639 template<> AliFmLorentzVector<double> operator+ (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<double>& v2);
640 template<> AliFmLorentzVector<double> operator+ (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<float>& v2);
641 template<> AliFmLorentzVector<double> operator+ (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<double>& v2);
642 template<> AliFmLorentzVector<float> operator+ (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<float>& v2);
643 template<> AliFmLorentzVector<double> operator- (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<double>& v2);
644 template<> AliFmLorentzVector<double> operator- (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<float>& v2);
645 template<> AliFmLorentzVector<double> operator- (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<double>& v2);
646 template<> AliFmLorentzVector<float> operator- (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<float>& v2);
647 template<> AliFmLorentzVector<double> operator* (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<double>& v2);
648 template<> AliFmLorentzVector<double> operator* (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<float>& v2);
649 template<> AliFmLorentzVector<double> operator* (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<double>& v2);
650 template<> AliFmLorentzVector<float> operator* (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<float>& v2);
651 template<> AliFmLorentzVector<double> operator* (const double v1, const AliFmLorentzVector<double>& v2);
652 template<> AliFmLorentzVector<double> operator* (const double v1, const AliFmLorentzVector<float>& v2);
653 template<> AliFmLorentzVector<double> operator* (const AliFmLorentzVector<double>& v1, const double v2);
654 template<> AliFmLorentzVector<double> operator* (const AliFmLorentzVector<float>& v1, const double v2);
655 template<> AliFmLorentzVector<double> operator/ (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<double>& v2);
656 template<> AliFmLorentzVector<double> operator/ (const AliFmLorentzVector<double>& v1, const AliFmLorentzVector<float>& v2);
657 template<> AliFmLorentzVector<double> operator/ (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<double>& v2);
658 template<> AliFmLorentzVector<float> operator/ (const AliFmLorentzVector<float>& v1, const AliFmLorentzVector<float>& v2);
659 template<> AliFmLorentzVector<double> operator/ (const double v1, const AliFmLorentzVector<double>& v2);
660 template<> AliFmLorentzVector<double> operator/ (const double v1, const AliFmLorentzVector<float>& v2);
661 template<> AliFmLorentzVector<double> operator/ (const AliFmLorentzVector<double>& v1, const double v2);
662 template<> AliFmLorentzVector<double> operator/ (const AliFmLorentzVector<float>& v1, const double v2);
663 template<> istream& operator>> (istream& is, const AliFmLorentzVector<double>& v);
664 template<> ostream& operator<< (ostream& os, const AliFmLorentzVector<double>& v);
665 template<> istream& operator>> (istream& is, const AliFmLorentzVector<float>& v);
666 template<> ostream& operator<< (ostream& os, const AliFmLorentzVector<float>& v);
667 template<> double abs(const AliFmLorentzVector<double>& v);
668 template<> float abs(const AliFmLorentzVector<float>& v);
671 // Non-member operators
673 template<class T, class X>
674 AliFmLorentzVector<T>
675 operator+ (const AliFmLorentzVector<T>& v1, const AliFmLorentzVector<X>& v2)
677 return AliFmLorentzVector<T>(v1) += v2;
680 template<class T, class X>
681 AliFmLorentzVector<T>
682 operator- (const AliFmLorentzVector<T>& v1, const AliFmLorentzVector<X>& v2)
684 return AliFmLorentzVector<T>(v1) -= v2;
687 template<class T, class X>
689 operator* (const AliFmLorentzVector<T>& v1, const AliFmLorentzVector<X>& v2)
691 return v1.t()*v2.t() - v1.vect()*v2.vect();
695 AliFmLorentzVector<T>
696 operator* (const AliFmLorentzVector<T>& v, double c)
698 return AliFmLorentzVector<T>(v) *= c;
702 AliFmLorentzVector<T> operator* (double c, const AliFmLorentzVector<T>& v)
704 return AliFmLorentzVector<T>(v) *= c;
707 template<class T, class X>
708 AliFmLorentzVector<T> operator/ (const AliFmLorentzVector<T>& v, X c)
710 return AliFmLorentzVector<T>(v) /= c;
714 ostream& operator<< (ostream& os, const AliFmLorentzVector<T>& v)
716 return os << v.vect() << "\t\t" << v.t();
720 istream& operator>>(istream& is, AliFmLorentzVector<T>& v)
723 is >> x >> y >> z >> t;
732 // Non-member functions
735 T abs(const AliFmLorentzVector<T>& v) {return v.m();}
737 #endif /* __CINT__ */