StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StLorentzVector.hh
1 /***************************************************************************
2  *
3  * $Id: StLorentzVector.hh,v 1.14 2012/06/11 15:29:26 fisyak Exp $
4  *
5  * Author: Brian Lasiuk, Thomas Ullrich, April 1998
6  ***************************************************************************
7  *
8  * Description:
9  *
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.
14  *
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
18  * are up-to-date. tu
19  *
20  ***************************************************************************
21  *
22  * $Log: StLorentzVector.hh,v $
23  * Revision 1.14 2012/06/11 15:29:26 fisyak
24  * std namespace
25  *
26  * Revision 1.13 2009/09/22 16:42:38 fine
27  * Add the extra ctor to complete the neet the template specilication signatures #1612
28  *
29  * Revision 1.12 2006/01/09 23:47:27 fisyak
30  * Add missing methods (found by Zhangbu) to Cint dictionary
31  *
32  * Revision 1.11 2005/09/22 20:09:20 fisyak
33  * Make StLorentzVector persistent
34  *
35  * Revision 1.10 2005/07/06 18:49:56 fisyak
36  * Replace StHelixD, StLorentzVectorD,StLorentzVectorF,StMatrixD,StMatrixF,StPhysicalHelixD,StThreeVectorD,StThreeVectorF by templated version
37  *
38  * Revision 1.9 2005/03/28 06:02:45 perev
39  * Defence FPE added
40  *
41  * Revision 1.8 2003/09/02 17:59:35 perev
42  * gcc 3.2 updates + WarnOff
43  *
44  * Revision 1.7 2003/05/01 19:24:31 ullrich
45  * Corrected problem in boost().
46  *
47  * Revision 1.6 1999/10/15 15:56:36 ullrich
48  * Changed output format in operator<<, added operator>>
49  *
50  * Revision 1.5 1999/06/04 18:01:36 ullrich
51  * New operators operator() and operator[] which can be used
52  * as lvalues.
53  *
54  * Revision 1.4 1999/04/14 23:12:07 fisyak
55  * Add __CINT__ to handle references
56  *
57  * Revision 1.3 1999/02/17 11:38:36 ullrich
58  * Removed specialization for 'long double'.
59  *
60  * Revision 1.2 1999/02/14 23:11:42 fisyak
61  * Fixes for Rootcint
62  *
63  * Revision 1.1 1999/01/30 03:59:02 fisyak
64  * Root Version of StarClassLibrary
65  *
66  * Revision 1.1 1999/01/23 00:27:52 ullrich
67  * Initial Revision
68  *
69  **************************************************************************/
70 #ifndef ST_LORENTZ_VECTOR_HH
71 #define ST_LORENTZ_VECTOR_HH
72 
73 #include "StThreeVector.hh"
74 template<class T> class StLorentzVector {
75 public:
76  StLorentzVector(T, T, T, T);
78  virtual ~StLorentzVector();
79 
80  template<class X> StLorentzVector(const StThreeVector<X>&, T);
81  template<class X, class Y> StLorentzVector(const StThreeVector<X>&, Y);
82  template<class X, class Y> StLorentzVector(Y, const StThreeVector<X>&);
83  template<class X> StLorentzVector(T, const StThreeVector<X>&);
84 
85  template<class X> StLorentzVector(const StLorentzVector<X>&);
86  template<class X> StLorentzVector<T>& operator=(const StLorentzVector<X>&);
87  // StLorentzVector(const StLorentzVector<T>&); use default
88  // StLorentzVector<T>& operator=(const StLorentzVector<T>&); use default
89  T x() const;
90  T y() const;
91  T z() const;
92  T t() const;
93  T px() const;
94  T py() const;
95  T pz() const;
96  T e() const;
97  T operator() (size_t) const;
98  T operator[] (size_t) const;
99 
100  T& operator() (size_t);
101  T& operator[] (size_t);
102 
103  const StThreeVector<T>& vect() const;
104 
105  void setX(T);
106  void setY(T);
107  void setZ(T);
108  void setPx(T);
109  void setPy(T);
110  void setPz(T);
111  void setE(T);
112  void setT(T);
113 
114 #if !defined(ST_NO_MEMBER_TEMPLATES) && !defined(__CINT__)
115  template <class X> void setVect(const StThreeVector<X>&);
116 #else
117  void setVect(const StThreeVector<float>&);
118  void setVect(const StThreeVector<double>&);
119 #endif
120 
121  T perp() const;
122  T perp2() const;
123  T pseudoRapidity() const;
124  T phi() const;
125  T theta() const;
126  T cosTheta() const;
127 
128  T plus() const;
129  T minus() const;
130 
131  T m() const;
132  T m2() const;
133  T mt() const;
134  T mt2() const;
135  T rapidity() const;
136 
137 #if !defined(ST_NO_MEMBER_TEMPLATES) && !defined(__CINT__)
138  template<class X> StLorentzVector<T> boost(const StLorentzVector<X>&) const;
139 #else
140  StLorentzVector<T> boost(const StLorentzVector<float>&) const;
141  StLorentzVector<T> boost(const StLorentzVector<double>&) const;
142 #endif
143 
144  StLorentzVector<T> operator- ();
145  StLorentzVector<T> operator+ ();
146  StLorentzVector<T>& operator*= (double);
147  StLorentzVector<T>& operator/= (double);
148 
149 #if !defined(ST_NO_MEMBER_TEMPLATES) && !defined(__CINT__)
150  template<class X> bool operator == (const StLorentzVector<X>&) const;
151  template<class X> bool operator != (const StLorentzVector<X>&) const;
152  template<class X> StLorentzVector<T>& operator+= (const StLorentzVector<X>&);
153  template<class X> StLorentzVector<T>& operator-= (const StLorentzVector<X>&);
154 #else
155  bool operator == (const StLorentzVector<float>&) const;
156  bool operator != (const StLorentzVector<float>&) const;
157  bool operator == (const StLorentzVector<double>&) const;
158  bool operator != (const StLorentzVector<double>&) const;
159 
160  StLorentzVector<T>& operator+= (const StLorentzVector<float>&);
161  StLorentzVector<T>& operator-= (const StLorentzVector<float>&);
162  StLorentzVector<T>& operator+= (const StLorentzVector<double>&);
163  StLorentzVector<T>& operator-= (const StLorentzVector<double>&);
164 #endif
165 
166 protected:
167  StThreeVector<T> mThreeVector;
168  T mX4;
169 #ifdef __ROOT__
170  ClassDef(StLorentzVector,3)
171 #endif
172 };
173 #ifndef __CINT__
174 //
175 // Implementation of member functions
176 //
177 template<class T>
179  : mThreeVector(0, 0, 0), mX4(0) { /* nop */ }
180 
181 template<class T>
182 StLorentzVector<T>::StLorentzVector(T X, T Y, T Z, T Time)
183  : mThreeVector(X, Y, Z), mX4(Time) { /* nop */ }
184 
185 template<class T>
186 StLorentzVector<T>::~StLorentzVector() { /* nopt */ }
187 
188 template<class T>
190 {
191  return mThreeVector;
192 }
193 
194 template<class T>
195 T StLorentzVector<T>::m2() const
196 {
197  return (mX4*mX4 - mThreeVector*mThreeVector);
198 }
199 
200 template<class T>
201 T StLorentzVector<T>::plus() const { return (e() + pz()); }
202 
203 template<class T>
204 T StLorentzVector<T>::minus() const { return (e() - pz()); }
205 
206 template<class T>
207 T StLorentzVector<T>::m() const
208 {
209  T mass2 = m2();
210  if (mass2 < 0)
211  return -::sqrt(-mass2);
212  else
213  return ::sqrt(mass2);
214 }
215 
216 template<class T>
217 T StLorentzVector<T>::mt2() const
218 {
219  return this->perp2() + m2();
220 }
221 
222 template<class T>
223 T StLorentzVector<T>::mt() const
224 {
225  //
226  // change to more optimal code ?
227  // return e()*e() - pz()*pz();
228  T massPerp2 = mt2();
229  if (massPerp2 < 0)
230  return -::sqrt(-massPerp2);
231  else
232  return ::sqrt(massPerp2);
233 }
234 
235 template<class T>
236 void StLorentzVector<T>::setPx(T X) {mThreeVector.setX(X);}
237 
238 template<class T>
239 void StLorentzVector<T>::setPy(T Y) {mThreeVector.setY(Y);}
240 
241 template<class T>
242 void StLorentzVector<T>::setPz(T Z) {mThreeVector.setZ(Z);}
243 
244 template<class T>
245 void StLorentzVector<T>::setX(T X) {mThreeVector.setX(X);}
246 
247 template<class T>
248 void StLorentzVector<T>::setY(T Y) {mThreeVector.setY(Y);}
249 
250 template<class T>
251 void StLorentzVector<T>::setZ(T Z) {mThreeVector.setZ(Z);}
252 
253 template<class T>
254 void StLorentzVector<T>::setT(T Time) {mX4 = Time;}
255 
256 template<class T>
257 void StLorentzVector<T>::setE(T E) {mX4 = E;}
258 
259 template<class T>
260 T StLorentzVector<T>::x() const {return mThreeVector.x();}
261 
262 template<class T>
263 T StLorentzVector<T>::y() const {return mThreeVector.y();}
264 
265 template<class T>
266 T StLorentzVector<T>::z() const {return mThreeVector.z();}
267 
268 template<class T>
269 T StLorentzVector<T>::px() const {return mThreeVector.x();}
270 
271 template<class T>
272 T StLorentzVector<T>::py() const {return mThreeVector.y();}
273 
274 template<class T>
275 T StLorentzVector<T>::pz() const {return mThreeVector.z();}
276 
277 template<class T>
278 T StLorentzVector<T>::e() const {return mX4;}
279 
280 template<class T>
281 T StLorentzVector<T>::t() const {return mX4;}
282 
283 template<class T>
284 T StLorentzVector<T>::perp() const {return mThreeVector.perp();}
285 
286 template<class T>
287 T StLorentzVector<T>::perp2() const {return mThreeVector.perp2();}
288 
289 template<class T>
290 T StLorentzVector<T>::pseudoRapidity() const {return mThreeVector.pseudoRapidity();}
291 
292 template<class T>
293 T StLorentzVector<T>::phi() const {return mThreeVector.phi();}
294 
295 template<class T>
296 T StLorentzVector<T>::theta() const {return mThreeVector.theta();}
297 
298 template<class T>
299 T StLorentzVector<T>::cosTheta() const {return mThreeVector.cosTheta();}
300 
301 template<class T>
302 T StLorentzVector<T>::operator() (size_t i) const
303 {
304  if (i < 3)
305  return mThreeVector(i);
306  else if (i == 3)
307  return mX4;
308  else {
309 #ifndef ST_NO_EXCEPTIONS
310  throw out_of_range("StLorentzVector<T>::operator(): bad index");
311 #else
312  cerr << "StLorentzVector<T>::operator(): bad index." << endl;
313 #endif
314  return 0;
315  }
316 }
317 
318 template<class T>
320 {
321  if (i < 3)
322  return mThreeVector(i);
323  else if (i == 3)
324  return mX4;
325  else {
326 #ifndef ST_NO_EXCEPTIONS
327  throw out_of_range("StLorentzVector<T>::operator(): bad index");
328 #else
329  cerr << "StLorentzVector<T>::operator(): bad index." << endl;
330 #endif
331  return mX4;
332  }
333 }
334 
335 template<class T>
336 T StLorentzVector<T>::operator[] (size_t i) const
337 {
338  if (i < 3)
339  return mThreeVector[i];
340  else if (i == 3)
341  return mX4;
342  else {
343 #ifndef ST_NO_EXCEPTIONS
344  throw out_of_range("StLorentzVector<T>::operator[]: bad index");
345 #else
346  cerr << "StLorentzVector<T>::operator[]: bad index." << endl;
347 #endif
348  return 0;
349  }
350 }
351 
352 template<class T>
354 {
355  if (i < 3)
356  return mThreeVector[i];
357  else if (i == 3)
358  return mX4;
359  else {
360 #ifndef ST_NO_EXCEPTIONS
361  throw out_of_range("StLorentzVector<T>::operator[]: bad index");
362 #else
363  cerr << "StLorentzVector<T>::operator[]: bad index." << endl;
364 #endif
365  return mX4;
366  }
367 }
368 
369 template<class T>
371 {
372  return 0.5*::log((mX4+mThreeVector.z())/(mX4-mThreeVector.z())+1e-20);
373 }
374 
375 template<class T>
377 {
378  return StLorentzVector<T>(-mX4,-mThreeVector);
379 }
380 
381 template<class T>
383 {
384  return *this;
385 }
386 
387 template<class T>
389 {
390  mThreeVector *= c;
391  mX4 *= c;
392  return *this;
393 }
394 
395 template<class T>
397 {
398  mThreeVector /= c;
399  mX4 /= c;
400  return *this;
401 }
402 
403 #if !defined(ST_NO_MEMBER_TEMPLATES) && !defined(__CINT__)
404 
405 template<class T>
406 template<class X>
408  : mThreeVector(vec), mX4(Time) { /* nop */ }
409 
410 template<class T>
411 template<class X>
413  : mThreeVector(vec), mX4(Time) { /* nop */ }
414 
415 template<class T>
416 template<class X>
418  : mThreeVector(vec.vect()), mX4(vec.t()) { /* nop */ }
419 
420 template<class T>
421 template<class X>
424 {
425  T mass = abs(pframe);
426  StThreeVector<T> eta = (-1./mass)*pframe.vect(); // gamma*beta
427  T gamma = fabs(pframe.e())/mass;
428  StThreeVector<T> pl = ((this->vect()*eta)/(eta*eta))*eta; // longitudinal momentum
429  return StLorentzVector<T>(gamma*this->e() - this->vect()*eta,
430  this->vect() + (gamma-1.)*pl - this->e()*eta);
431 }
432 
433 template<class T>
434 template<class X>
436 {
437  mThreeVector = v;
438 }
439 
440 template<class T>
441 template<class X>
444 {
445  mThreeVector = vec.vect();
446  mX4 = vec.t();
447  return *this;
448 }
449 
450 template<class T>
451 template<class X>
452 bool
454 {
455  return (mThreeVector == v.vect()) && (mX4 == v.t());
456 }
457 
458 template<class T>
459 template<class X>
460 bool
462 {
463  return !(*this == v);
464 }
465 
466 template<class T>
467 template<class X>
470 {
471  mThreeVector += v.vect();
472  mX4 += v.t();
473  return *this;
474 }
475 
476 template<class T>
477 template<class X>
480 {
481  mThreeVector -= v.vect();
482  mX4 -= v.t();
483  return *this;
484 }
485 
486 #else
487 
488 template<class T>
490  : mThreeVector(vec), mX4(t) { /* nop */ }
491 
492 template<class T>
494  : mThreeVector(vec), mX4(t) { /* nop */ }
495 
496 template<class T>
498  : mThreeVector(vec), mX4(t) { /* nop */ }
499 
500 template<class T>
502  : mThreeVector(vec), mX4(t) { /* nop */ }
503 
504 template<class T>
506  : mThreeVector(vec.vect()), mX4(vec.t()) { /* nop */ }
507 
508 template<class T>
510  : mThreeVector(vec.vect()), mX4(vec.t()) { /* nop */ }
511 
512 template<class T>
515 {
516  T mass = abs(pframe);
517  StThreeVector<T> eta = (-1./mass)*pframe.vect(); // gamma*beta
518  T gamma = fabs(pframe.e())/mass;
519  StThreeVector<T> pl = ((this->vect()*eta)/(eta*eta))*eta; // longitudinal momentum
520  return StLorentzVector<T>(gamma*this->e() - this->vect()*eta,
521  this->vect() + (gamma-1.)*pl - this->e()*eta);
522 }
523 
524 template<class T>
527 {
528  T mass = abs(pframe);
529  StThreeVector<T> eta = (-1./mass)*pframe.vect(); // gamma*beta
530  T gamma = fabs(pframe.e())/mass;
531  StThreeVector<T> pl = ((this->vect()*eta)/(eta*eta))*eta; // longitudinal momentum
532  return StLorentzVector<T>(gamma*this->e() - this->vect()*eta,
533  this->vect() + (gamma-1.)*pl - this->e()*eta);
534 }
535 
536 template<class T>
538 {
539  mThreeVector = v;
540 }
541 
542 template<class T>
544 {
545  mThreeVector = v;
546 }
547 
548 template<class T>
551 {
552  mThreeVector = vec.vect();
553  mX4 = vec.t();
554  return *this;
555 }
556 
557 template<class T>
560 {
561  mThreeVector = vec.vect();
562  mX4 = vec.t();
563  return *this;
564 }
565 
566 template<class T>
567 bool
569 {
570  return (this->vect() == v.vect()) && (mX4 == v.t());
571 }
572 
573 template<class T>
574 bool
576 {
577  return (mThreeVector == v.vect()) && (mX4 == v.t());
578 }
579 
580 template<class T>
581 bool
583 {
584  return !(*this == v);
585 }
586 
587 template<class T>
588 bool
590 {
591  return !(*this == v);
592 }
593 
594 template<class T>
597 {
598  mThreeVector += v.vect();
599  mX4 += v.t();
600  return *this;
601 }
602 
603 template<class T>
606 {
607  mThreeVector += v.vect();
608  mX4 += v.t();
609  return *this;
610 }
611 
612 template<class T>
615 {
616  mThreeVector -= v.vect();
617  mX4 -= v.t();
618  return *this;
619 }
620 
621 template<class T>
624 {
625  mThreeVector -= v.vect();
626  mX4 -= v.t();
627  return *this;
628 }
629 
630 #endif // ST_NO_MEMBER_TEMPLATES
631 #endif /* ! __CINT__ */
632 
633 //
634 // Non-member operators
635 //
636 template<class T, class X>
638 operator+ (const StLorentzVector<T>& v1, const StLorentzVector<X>& v2)
639 {
640  return StLorentzVector<T>(v1) += v2;
641 }
642 
643 template<class T, class X>
645 operator- (const StLorentzVector<T>& v1, const StLorentzVector<X>& v2)
646 {
647  return StLorentzVector<T>(v1) -= v2;
648 }
649 
650 template<class T, class X>
651 T operator* (const StLorentzVector<T>& v1, const StLorentzVector<X>& v2)
652 {
653  return v1.t()*v2.t() - v1.vect()*v2.vect();
654 }
655 
656 template<class T>
658 operator* (const StLorentzVector<T>& v, double c)
659 {
660  return StLorentzVector<T>(v) *= c;
661 }
662 
663 template<class T>
664 StLorentzVector<T> operator* (double c, const StLorentzVector<T>& v)
665 {
666  return StLorentzVector<T>(v) *= c;
667 }
668 
669 template<class T, class X>
670 StLorentzVector<T> operator/ (const StLorentzVector<T>& v, X c)
671 {
672  return StLorentzVector<T>(v) /= c;
673 }
674 
675 template<class T>
676 ostream& operator<< (ostream& os, const StLorentzVector<T>& v)
677 {
678  return os << v.vect() << "\t\t" << v.t();
679 }
680 
681 template<class T>
682 istream& operator>>(istream& is, StLorentzVector<T>& v)
683 {
684  T x, y, z, t;
685  is >> x >> y >> z >> t;
686  v.setX(x);
687  v.setY(y);
688  v.setZ(z);
689  v.setT(t);
690  return is;
691 }
692 
693 //
694 // Non-member functions
695 //
696 template<class T>
697 T abs(const StLorentzVector<T>& v) {return v.m();}
698 #endif