LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
GeoVector.cxx
Go to the documentation of this file.
1 #ifndef BASICTOOL_GEOVECTOR_CXX
2 #define BASICTOOL_GEOVECTOR_CXX
3 
4 #include "GeoVector.h"
5 #include <iostream>
6 #include <iomanip>
7 #include <vector>
8 #include <sstream>
9 #include <cmath>
10 #include <limits>
11 
12 namespace geoalgo {
13 
14  Vector::Vector(const double x, const double y) : Vector(2)
15  { (*this)[0] = x; (*this)[1] = y; }
16 
17  Vector::Vector(const double x, const double y, const double z) : Vector(3)
18  { (*this)[0] = x; (*this)[1] = y; (*this)[2] = z; }
19 
20  Vector::Vector(const TVector3 &pt) : Vector(3)
21  { (*this)[0] = pt[0]; (*this)[1] = pt[1]; (*this)[2] = pt[2]; }
22 
23  Vector::Vector(const TLorentzVector &pt) : Vector(3)
24  { (*this)[0] = pt[0]; (*this)[1] = pt[1]; (*this)[2] = pt[2]; }
25 
26  bool Vector::IsValid() const {
27 
28  for (auto const &v : (*this)){
29  // if any point is different from kINVALID_DOUBLE
30  // then the point is valid
31  if (v != kINVALID_DOUBLE)
32  return true;
33  }
34 
35  return false;
36  }
37 
38  double Vector::SqLength() const {
39  double res=0;
40  for(auto const &v : (*this)) res += v*v;
41  return res;
42  }
43 
44  double Vector::Length() const
45  { return sqrt(SqLength()); }
46 
47  double Vector::SqDist(const Vector &obj) const {
48  compat(obj);
49  return _SqDist_(obj);
50  }
51 
52  double Vector::Dist(const Vector& obj) const
53  { return sqrt(SqDist(obj)); }
54 
55  double Vector::Dot(const Vector &obj) const {
56  compat(obj);
57  return _Dot_(obj);
58  }
59 
60  Vector Vector::Cross(const Vector &obj) const {
61 
62  if(size()!=3 || obj.size()!=3)
63 
64  throw GeoAlgoException("<<Cross>> only possible for 3-dimensional vectors!");
65 
66  return _Cross_(obj);
67  }
68 
69  double Vector::Phi() const {
70  return (*this)[0] == 0.0 && (*this)[1] == 0.0 ? 0.0 : atan2((*this)[1],(*this)[0]);
71  }
72 
73  double Vector::Theta() const {
74  if ( size() != 3 )
75  throw GeoAlgoException("<<Theta>> Only possible for 3-dimensional vectors!");
76 
77  return (*this).Length() == 0.0 ? 0.0 : acos( (*this)[2] / (*this).Length() );
78  }
79 
80  double Vector::Angle(const Vector &obj) const {
81  compat(obj);
82  if(size()!=2 && size()!=3)
83  throw GeoAlgoException("<<Angle>> only possible for 2 or 3-dimensional vectors!");
84  return _Angle_(obj);
85  }
86 
87  TLorentzVector Vector::ToTLorentzVector() const {
88  if(size()!=3)
89  throw GeoAlgoException("<<ToTLorentsVector>> only possible for 3-dimensional vectors!");
90  return TLorentzVector((*this)[0],(*this)[1],(*this)[2],0.);
91  }
92 
93  void Vector::Normalize() { (*this) /= this->Length(); }
94 
95  Vector Vector::Dir() const {
96  Vector res(*this);
97  res /= res.Length();
98  return res;
99  }
100 
101  void Vector::compat(const Vector& obj) const {
102  if(size() != obj.size()) {
103  std::ostringstream msg;
104  msg << "<<" << __FUNCTION__ << ">>"
105  << " size mismatch: "
106  << size() << " != " << obj.size()
107  << std::endl;
108  throw GeoAlgoException(msg.str());
109  }
110  }
111 
112  double Vector::_SqDist_(const Vector& obj) const
113  {
114  double dist = 0;
115  for(size_t i=0; i<size(); ++i) dist += ((*this)[i] - obj[i]) * ((*this)[i] - obj[i]);
116  return dist;
117  }
118 
119  double Vector::_Dist_(const Vector& obj) const
120  { return sqrt(_SqDist_(obj)); }
121 
122  double Vector::_Dot_(const Vector& obj) const
123  { return (*this) * obj; }
124 
125  Vector Vector::_Cross_(const Vector& obj) const
126  {
127  Vector res(3);
128  res[0] = (*this)[1] * obj[2] - obj[1] * (*this)[2];
129  res[1] = (*this)[2] * obj[0] - obj[2] * (*this)[0];
130  res[2] = (*this)[0] * obj[1] - obj[0] * (*this)[1];
131  return res;
132  }
133 
134  double Vector::_Angle_(const Vector& obj) const
135  { return acos( _Dot_(obj) / Length() / obj.Length() ); }
136 
137 
138  void Vector::RotateX(const double& theta)
139  {
140 
141  double c = cos(theta);
142  double s = sin(theta);
143 
144  double ynew = (*this)[1] * c - (*this)[2] * s;
145  double znew = (*this)[1] * s + (*this)[2] * c;
146 
147  (*this)[1] = ynew;
148  (*this)[2] = znew;
149 
150  return;
151  }
152 
153 
154  void Vector::RotateY(const double& theta)
155  {
156 
157  double c = cos(theta);
158  double s = sin(theta);
159 
160  double xnew = (*this)[0] * c + (*this)[2] * s;
161  double znew = - (*this)[0] * s + (*this)[2] * c;
162 
163  (*this)[0] = xnew;
164  (*this)[2] = znew;
165 
166  return;
167  }
168 
169 
170  void Vector::RotateZ(const double& theta)
171  {
172 
173  double c = cos(theta);
174  double s = sin(theta);
175 
176  double xnew = (*this)[0] * c - (*this)[1] * s;
177  double ynew = (*this)[0] * s + (*this)[1] * c;
178 
179  (*this)[0] = xnew;
180  (*this)[1] = ynew;
181 
182  return;
183  }
184 
185 
186 }
187 
188 #endif
189 
190 
Float_t x
Definition: compare.C:6
Float_t s
Definition: plot.C:23
double _Angle_(const Vector &obj) const
Compute the angle in degrees between 2 vectors w/o dimension check.
Definition: GeoVector.cxx:134
Float_t y
Definition: compare.C:6
double _Dot_(const Vector &obj) const
Compute a dot product w/o dimention check.
Definition: GeoVector.cxx:122
Vector()
Default ctor.
Definition: GeoVector.h:43
Double_t z
Definition: plot.C:279
double Phi() const
Compute the angle Phi.
Definition: GeoVector.cxx:69
void compat(const Vector &obj) const
Dimensional check for a compatibility.
Definition: GeoVector.cxx:101
double _SqDist_(const Vector &obj) const
Compute the squared-distance to another vector w/o dimension check.
Definition: GeoVector.cxx:112
double Dist(const Vector &obj) const
Compute the distance to another vector.
Definition: GeoVector.cxx:52
void Normalize()
Normalize itself.
Definition: GeoVector.cxx:93
double SqLength() const
Compute the squared length of the vector.
Definition: GeoVector.cxx:38
Vector _Cross_(const Vector &obj) const
Compute a cross product w/o dimension check.
Definition: GeoVector.cxx:125
double Length() const
Compute the length of the vector.
Definition: GeoVector.cxx:44
double Dot(const Vector &obj) const
Definition: GeoVector.cxx:55
Class def header for a class Point and Vector.
bool IsValid() const
Check if point is valid.
Definition: GeoVector.cxx:26
TMarker * pt
Definition: egs.C:25
Vector Cross(const Vector &obj) const
Compute a dot product of two vectors.
Definition: GeoVector.cxx:60
void RotateY(const double &theta)
Definition: GeoVector.cxx:154
double SqDist(const Vector &obj) const
Compute the squared distance to another vector.
Definition: GeoVector.cxx:47
TLorentzVector ToTLorentzVector() const
Compute an opening angle w.r.t. the given vector.
Definition: GeoVector.cxx:87
void RotateX(const double &theta)
rotation operations
Definition: GeoVector.cxx:138
Vector Dir() const
Return a direction unit vector.
Definition: GeoVector.cxx:95
double Theta() const
Compute the angle theta.
Definition: GeoVector.cxx:73
double _Dist_(const Vector &obj) const
Compute the distance to another vector w/o dimension check.
Definition: GeoVector.cxx:119
static const double kINVALID_DOUBLE
void RotateZ(const double &theta)
Definition: GeoVector.cxx:170
double Angle(const Vector &obj) const
Compute a cross product of two vectors.
Definition: GeoVector.cxx:80