Belle II Software  release-08-01-10
ThreeVector.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * Forked from https://github.com/iLCSoft/MarlinKinfit *
6  * *
7  * Further information about the fit engine and the user interface *
8  * provided in MarlinKinfit can be found at *
9  * https://www.desy.de/~blist/kinfit/doc/html/ *
10  * and in the LCNotes LC-TOOL-2009-001 and LC-TOOL-2009-004 available *
11  * from http://www-flc.desy.de/lcnotes/ *
12  * *
13  * See git log for contributors and copyright holders. *
14  * This file is licensed under LGPL-3.0, see LICENSE.md. *
15  **************************************************************************/
16 
17 #ifndef __THREEVECTOR_H
18 #define __THREEVECTOR_H
19 
20 #include <iostream>
21 #include <cmath>
22 
23 namespace Belle2 {
28  namespace OrcaKinFit {
29 
30 
31  class ThreeVector {
32  public:
33  inline ThreeVector();
34  inline ThreeVector(double px_, double py_, double pz_);
35  // automatically generated copy constructor and assignment is fine
36 
37  inline double getPx() const;
38  inline double getPy() const;
39  inline double getPz() const;
40  inline double getX() const;
41  inline double getY() const;
42  inline double getZ() const;
43 
44  inline double getP2() const;
45  inline double getP() const;
46  inline double getMag() const;
47  inline double getPt2() const;
48  inline double getPt() const;
49  inline double getR() const;
50 
51  inline double getPhi() const;
52  inline double getTheta() const;
53  inline double getEta() const;
54 
55  inline double getComponent(int i) const;
56 
57  inline ThreeVector& setValues(double px_, double py_, double pz_);
58 
59  inline ThreeVector& operator+= (const ThreeVector& rhs);
60  inline ThreeVector& operator-= (const ThreeVector& rhs);
61  inline ThreeVector& operator*= (double rhs);
62 
63  private:
64  double px, py, pz;
65  };
66 
67  ThreeVector::ThreeVector()
68  : px(0), py(0), pz(0)
69  {}
70 
71  ThreeVector::ThreeVector(double px_, double py_, double pz_)
72  : px(px_), py(py_), pz(pz_)
73  {}
74 
75  double ThreeVector::getPx() const { return px; }
76  double ThreeVector::getPy() const { return py; }
77  double ThreeVector::getPz() const { return pz; }
78  double ThreeVector::getX() const { return px; }
79  double ThreeVector::getY() const { return py; }
80  double ThreeVector::getZ() const { return pz; }
81 
82  double ThreeVector::getPt2() const { return px * px + py * py; }
83  double ThreeVector::getPt() const { return std::sqrt(getPt2()); }
84  double ThreeVector::getR() const { return std::sqrt(getPt2()); }
85 
86  double ThreeVector::getP2() const { return px * px + py * py + pz * pz; }
87  double ThreeVector::getP() const { return std::sqrt(getP2()); }
88  double ThreeVector::getMag()const { return std::sqrt(getP2()); }
89 
90  double ThreeVector::getPhi() const { return std::atan2(py, px); }
91  double ThreeVector::getTheta() const { return std::atan2(getPt(), pz); }
92  double ThreeVector::getEta() const { return -std::log(std::tan(0.5 * getTheta())); }
93 
94  double ThreeVector::getComponent(int i) const
95  {
96  switch (i) {
97  case 0: return getPx();
98  case 1: return getPy();
99  case 2: return getPz();
100  }
101  return NAN; // not-a-number, defined in cmath
102  }
103 
104  ThreeVector& ThreeVector::setValues(double px_, double py_, double pz_)
105  {
106  px = px_;
107  py = py_;
108  pz = pz_;
109  return *this;
110  }
111 
112 
113  ThreeVector& ThreeVector::operator+= (const ThreeVector& rhs)
114  {
115  px += rhs.px;
116  py += rhs.py;
117  pz += rhs.pz;
118  return *this;
119  }
120 
121  ThreeVector& ThreeVector::operator-= (const ThreeVector& rhs)
122  {
123  px -= rhs.px;
124  py -= rhs.py;
125  pz -= rhs.pz;
126  return *this;
127  }
128 
129  ThreeVector& ThreeVector::operator*= (double rhs)
130  {
131  px *= rhs;
132  py *= rhs;
133  pz *= rhs;
134  return *this;
135  }
136 
137  inline ThreeVector operator+ (const ThreeVector& lhs, const ThreeVector& rhs)
138  {
139  return ThreeVector(lhs.getPx() + rhs.getPx(), lhs.getPy() + rhs.getPy(), lhs.getPz() + rhs.getPz());
140  }
141 
142  inline ThreeVector operator- (const ThreeVector& lhs, const ThreeVector& rhs)
143  {
144  return ThreeVector(lhs.getPx() - rhs.getPx(), lhs.getPy() - rhs.getPy(), lhs.getPz() - rhs.getPz());
145  }
146 
147  inline ThreeVector operator- (const ThreeVector& rhs)
148  {
149  return ThreeVector(-rhs.getPx(), -rhs.getPy(), -rhs.getPz());
150  }
151 
152  inline double operator* (const ThreeVector& lhs, const ThreeVector& rhs)
153  {
154  return lhs.getPx() * rhs.getPx() + lhs.getPy() * rhs.getPy() + lhs.getPz() * rhs.getPz();
155  }
156 
157  inline ThreeVector operator* (double lhs, const ThreeVector& rhs)
158  {
159  return ThreeVector(lhs * rhs.getPx(), lhs * rhs.getPy(), lhs * rhs.getPz());
160  }
161 
162  inline std::ostream& operator<< (std::ostream& out, const ThreeVector& v)
163  {
164  out << "(" << v.getPx() << ", " << v.getPy() << ", " << v.getPz() << ")";
165  return out;
166  }
167 
168 
169  }// end OrcaKinFit namespace
171 } // end Belle2 namespace
172 
173 
174 #endif // __THREEVECTOR_H
175 
B2Vector3< DataType > operator*(DataType a, const B2Vector3< DataType > &p)
non-memberfunction Scaling of 3-vectors with a real number
Definition: B2Vector3.h:537
std::ostream & operator<<(std::ostream &output, const IntervalOfValidity &iov)
B2Vector3< DataType > operator-(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for substracting a TVector3 from a B2Vector3
Definition: B2Vector3.h:551
B2Vector3< DataType > operator+(const TVector3 &a, const B2Vector3< DataType > &b)
non-memberfunction for adding a TVector3 to a B2Vector3
Definition: B2Vector3.h:544
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
double tan(double a)
tan for double
Definition: beamHelpers.h:31
Abstract base class for different kinds of events.