EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairGeoVector.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairGeoVector.h
1 #ifndef FAIRGEOVECTOR_H
2 #define FAIRGEOVECTOR_H
3 
4 #include "TObject.h"
5 #include "TMath.h"
6 
7 #include <iostream>
8 
9 
10 class FairGeoVector : public TObject
11 {
12  protected:
13  Double_t x;
14  Double_t y;
15  Double_t z;
16  inline void round(Double_t d,Int_t n);
17  public:
18  FairGeoVector(Double_t dx=0,Double_t dy=0,Double_t dz=0)
19  : TObject(),x(dx),y(dy),z(dz) {}
21  : TObject(v),x(v.getX()), y(v.getY()), z(v.getZ()) {}
23  Double_t& X() {return x;}
24  Double_t& Y() {return y;}
25  Double_t& Z() {return z;}
26  Double_t getX() const {return x;}
27  Double_t getY() const {return y;}
28  Double_t getZ() const {return z;}
29 
30  Double_t getValues(Int_t i ) {
31  if ( i<0 || i>2 ) {
32  std::cout << " -E- Vector index is 0 1 2 only ! " << std::endl;
33  }
34  if ( i == 0 ) { return x; }
35  if ( i == 1 ) { return y; }
36  if ( i == 2 ) { return z; }
37  return -1;
38  }
39 
40  void setXYZ(const Double_t xx,const Double_t yy,const Double_t zz) {
41  x=xx;
42  y=yy;
43  z=zz;
44  }
45  void setX(const Double_t a) {x=a;}
46  void setY(const Double_t a) {y=a;}
47  void setZ(const Double_t a) {z=a;}
48  inline void setVector(const Double_t* a);
49  inline void setVector(const Float_t* a);
50  inline Double_t operator() (const Int_t i) const;
51  inline FairGeoVector operator - () const;
52  inline FairGeoVector& operator = (const FairGeoVector& v);
53  inline Bool_t operator == (const FairGeoVector& v) const;
54  inline Bool_t operator != (const FairGeoVector& v) const;
55  inline Bool_t operator < (const Double_t a);
56  inline Bool_t operator <= (const Double_t a);
57  inline Bool_t operator > (const Double_t a);
58  inline Bool_t operator >= (const Double_t a);
59  inline FairGeoVector& operator += (const Double_t a);
60  inline FairGeoVector& operator -= (const Double_t a);
61  inline FairGeoVector& operator *= (const Double_t a);
62  inline FairGeoVector& operator /= (const Double_t a);
63  inline FairGeoVector& operator += (const FairGeoVector& v);
64  inline FairGeoVector& operator -= (const FairGeoVector& v);
65  inline FairGeoVector operator + (const FairGeoVector& v) const;
66  inline FairGeoVector operator - (const FairGeoVector& v) const;
67  inline FairGeoVector& abs();
68  inline Double_t scalarProduct(const FairGeoVector& v) const;
69  inline FairGeoVector vectorProduct(const FairGeoVector& v) const;
70  Double_t length() const {return sqrt(x*x+y*y+z*z);}
71  void clear() {x=y=z=0.;}
72  void print() const {printf("%10.3f%10.3f%10.3f\n",x,y,z);}
73  inline void round(Int_t n);
74  inline friend std::ostream& operator << (std::ostream& put,const FairGeoVector& v);
75  inline friend std::istream& operator >> (std::istream& get,FairGeoVector& v);
76  ClassDef(FairGeoVector,1) // vector with 3 components
77 };
78 
79 // -------------------- inlines ---------------------------
80 
81 inline void FairGeoVector::setVector(const Double_t* a)
82 {
83  x=a[0];
84  y=a[1];
85  z=a[2];
86 }
87 
88 inline void FairGeoVector::setVector(const Float_t* a)
89 {
90  x=a[0];
91  y=a[1];
92  z=a[2];
93 }
94 
95 inline Double_t FairGeoVector::operator() (const Int_t i) const
96 {
97  switch (i) {
98  case 0:
99  return x;
100  case 1:
101  return y;
102  case 2:
103  return z;
104  default:
105  Error("operator()","bad index");
106  }
107  return 0;
108 }
109 
111 {
112  FairGeoVector p(-x,-y,-z);
113  return p;
114 }
115 
117 {
118  x=v.getX();
119  y=v.getY();
120  z=v.getZ();
121  return *this;
122 }
123 
124 inline Bool_t FairGeoVector::operator == (const FairGeoVector& v) const
125 {
126  return ((v.getX()!=x || v.getY()!=y || v.getZ()!=z) ? kFALSE : kTRUE);
127 }
128 
129 inline Bool_t FairGeoVector::operator != (const FairGeoVector& v) const
130 {
131  return (v.getX()!=x || v.getY()!=y || v.getZ()!=z) ? kTRUE : kFALSE;
132 }
134 inline Bool_t FairGeoVector::operator < (const Double_t a)
135 {
136  return (x>=a || y>=a || z>=a) ? kFALSE : kTRUE;
137 }
138 
139 inline Bool_t FairGeoVector::operator <= (const Double_t a)
140 {
141  return (x>a || y>a || z>a) ? kFALSE : kTRUE;
142 }
143 
144 inline Bool_t FairGeoVector::operator > (const Double_t a)
145 {
146  return (x<=a || y<=a || z<=a) ? kFALSE : kTRUE;
147 }
148 
149 inline Bool_t FairGeoVector::operator >= (const Double_t a)
150 {
151  return (x<a || y<a || z<a) ? kFALSE : kTRUE;
152 }
153 
155 {
156  x+=a;
157  y+=a;
158  z+=a;
159  return *this;
160 }
161 
163 {
164  x-=a;
165  y-=a;
166  z-=a;
167  return *this;
168 }
169 
171 {
172  x*=a;
173  y*=a;
174  z*=a;
175  return *this;
176 }
177 
179 {
180  x/=a;
181  y/=a;
182  z/=a;
183  return *this;
184 }
185 
187 {
188  x+=v.getX();
189  y+=v.getY();
190  z+=v.getZ();
191  return *this;
192 }
193 
195 {
196  x-=v.getX();
197  y-=v.getY();
198  z-=v.getZ();
199  return *this;
200 }
201 
203 {
204  FairGeoVector p(*this);
205  return p+=v;
206 }
207 
209 {
210  FairGeoVector p(*this);
211  return p-=v;
212 }
213 
215 {
216  x=TMath::Abs(x);
217  y=TMath::Abs(y);
218  z=TMath::Abs(z);
219  return *this;
220 }
221 
222 inline Double_t FairGeoVector::scalarProduct(const FairGeoVector& v) const
223 {
224  return (x*v.getX()+y*v.getY()+z*v.getZ());
225 }
226 
228 {
229  FairGeoVector p(y*v.getZ()-z*v.getY(),z*v.getX()-x*v.getZ(),
230  x*v.getY()-y*v.getX());
231  return p;
232 }
233 
234 inline void FairGeoVector::round(Double_t d,Int_t n)
235 {
236  // rounds d to a precision with n digits
237  if(d>0) { d=floor(d*pow(10.,n)+0.5)/pow(10.,n); }
238  else { d=-floor((-d)*pow(10.,n)+0.5)/pow(10.,n); }
239 }
240 
241 inline void FairGeoVector::round(Int_t n)
242 {
243  // rounds every component to a precision with n digits
244  round(x,n);
245  round(y,n);
246  round(z,n);
247 }
248 
249 inline std::ostream& operator << (std::ostream& put,const FairGeoVector& v)
250 {
251  return put<<v(0)<<" "<<v(1)<<" "<<v(2)<<'\n';
252 }
253 
254 inline std::istream& operator >> (std::istream& get,FairGeoVector& v)
255 {
256  Double_t x[3];
257  get>>x[0]>>x[1]>>x[2];
258  v.setVector(x);
259  return get;
260 }
261 
262 #endif /* !FAIRGEOVECTOR_H */
263 
264 
265 
266 
267 
268 
269 
270 
271 
272 
273 
274 
275