Logo ROOT   6.10/09
Reference Guide
TEveVector.h
Go to the documentation of this file.
1 // @(#)root/eve:$Id$
2 // Author: Matevz Tadel 2007
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TEveVector
13 #define ROOT_TEveVector
14 
15 #include "TMath.h"
16 #include <cstddef>
17 
18 class TVector3;
19 
20 
21 //==============================================================================
22 // TEveVectorT
23 //==============================================================================
24 
25 template <typename TT>
27 {
28 public:
29  TT fX, fY, fZ; // Components of the vector.
30 
31  TEveVectorT() : fX(0), fY(0), fZ(0) {}
32  template <typename OO>
33  TEveVectorT(const TEveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
34  TEveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
35  TEveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
36  TEveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
37 
38  void Dump() const;
39 
40  const TT* Arr() const {
41  static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2*sizeof(TT),
42  "Subsequent nembers cannot be accessed as array!");
43  return &fX; }
44  TT* Arr() {
45  static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2*sizeof(TT),
46  "Subsequent nembers cannot be accessed as array!");
47  return &fX; }
48 
49  operator const TT*() const { return Arr(); }
50  operator TT*() { return Arr(); }
51 
52  TT operator [] (Int_t idx) const { return Arr()[idx]; }
53  TT& operator [] (Int_t idx) { return Arr()[idx]; }
54 
55  TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
56  TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
57  TEveVectorT& operator-=(const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
58 
59  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
60  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
61  void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
62  void Set(const TVector3& v);
63 
64  template <typename OO>
65  void Set(const TEveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
66 
67  void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
68  TT Normalize(TT length=1);
69 
70  TT Phi() const;
71  TT Theta() const;
72  TT CosTheta() const;
73  TT Eta() const;
74 
75  TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
76  TT Mag() const { return TMath::Sqrt(Mag2()); }
77 
78  TT Perp2() const { return fX*fX + fY*fY; }
79  TT Perp() const { return TMath::Sqrt(Perp2()); }
80  TT R() const { return Perp(); }
81 
82  TT Distance(const TEveVectorT& v) const;
83  TT SquareDistance(const TEveVectorT& v) const;
84 
85  TT Dot(const TEveVectorT& a) const;
86 
87  TEveVectorT Cross(const TEveVectorT& a) const;
88 
89  TEveVectorT& Sub(const TEveVectorT& a, const TEveVectorT& b);
90  TEveVectorT& Mult(const TEveVectorT& a, TT af);
91 
92  TEveVectorT Orthogonal() const;
93  void OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const;
94 
95  Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
96 
97  ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions.
98 };
99 
103 
104 //______________________________________________________________________________
105 template<typename TT>
106 inline TT TEveVectorT<TT>::Phi() const
107 {
108  return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
109 }
110 
111 //______________________________________________________________________________
112 template<typename TT>
113 inline TT TEveVectorT<TT>::Theta() const
114 {
115  return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
116 }
117 
118 //______________________________________________________________________________
119 template<typename TT>
120 inline TT TEveVectorT<TT>::CosTheta() const
121 {
122  Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
123 }
124 
125 //______________________________________________________________________________
126 template<typename TT>
127 inline TT TEveVectorT<TT>::Distance(const TEveVectorT& b) const
128 {
129  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
130  (fY - b.fY)*(fY - b.fY) +
131  (fZ - b.fZ)*(fZ - b.fZ));
132 }
133 
134 //______________________________________________________________________________
135 template<typename TT>
137 {
138  return ((fX - b.fX) * (fX - b.fX) +
139  (fY - b.fY) * (fY - b.fY) +
140  (fZ - b.fZ) * (fZ - b.fZ));
141 }
142 
143 //______________________________________________________________________________
144 template<typename TT>
145 inline TT TEveVectorT<TT>::Dot(const TEveVectorT& a) const
146 {
147  return a.fX*fX + a.fY*fY + a.fZ*fZ;
148 }
149 
150 //______________________________________________________________________________
151 template<typename TT>
153 {
155  r.fX = fY * a.fZ - fZ * a.fY;
156  r.fY = fZ * a.fX - fX * a.fZ;
157  r.fZ = fX * a.fY - fY * a.fX;
158  return r;
159 }
160 
161 //______________________________________________________________________________
162 template<typename TT>
164 {
165  fX = a.fX - b.fX;
166  fY = a.fY - b.fY;
167  fZ = a.fZ - b.fZ;
168  return *this;
169 }
170 
171 //______________________________________________________________________________
172 template<typename TT>
174 {
175  fX = a.fX * af;
176  fY = a.fY * af;
177  fZ = a.fZ * af;
178  return *this;
179 }
180 
181 //______________________________________________________________________________
182 template<typename TT>
184 {
185  TEveVectorT<TT> r(a);
186  return r += b;
187 }
188 
189 //______________________________________________________________________________
190 template<typename TT>
192 {
193  TEveVectorT<TT> r(a);
194  return r -= b;
195 }
196 
197 //______________________________________________________________________________
198 template<typename TT>
200 {
201  TEveVectorT<TT> r(a);
202  return r *= b;
203 }
204 
205 //______________________________________________________________________________
206 template<typename TT>
208 {
209  TEveVectorT<TT> r(a);
210  return r *= b;
211 }
212 
213 
214 //==============================================================================
215 // TEveVector4T
216 //==============================================================================
217 
218 template <typename TT>
219 class TEveVector4T : public TEveVectorT<TT>
220 {
222 
223 public:
224  TT fT;
225 
226  TEveVector4T() : TP(), fT(0) {}
227  template <typename OO>
228  TEveVector4T(const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
229  template <typename OO>
230  TEveVector4T(const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
231  template <typename OO>
232  TEveVector4T(const TEveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
233  TEveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
234  TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
235  TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
236 
237  void Dump() const;
238 
239  TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
240  TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
241  TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
242 
243  using TP::operator+=;
244  using TP::operator-=;
245 
246  ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions.
247 };
248 
252 
253 //______________________________________________________________________________
254 template<typename TT>
256 {
257  return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
258 }
259 
260 //______________________________________________________________________________
261 template<typename TT>
263 {
264  return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
265 }
266 
267 //______________________________________________________________________________
268 template<typename TT>
270 {
271  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
272 }
273 
274 //______________________________________________________________________________
275 template<typename TT>
277 {
278  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
279 }
280 
281 
282 //==============================================================================
283 // TEveVector2T
284 //==============================================================================
285 
286 template <typename TT>
288 {
289 public:
290  TT fX, fY; // Components of the point.
291 
292  TEveVector2T() : fX(0), fY(0) {}
293  template <typename OO>
294  TEveVector2T(const TEveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
295  TEveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
296  TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
297  TEveVector2T(TT x, TT y) : fX(x), fY(y) {}
298 
299  void Dump() const;
300 
301  operator const TT*() const { return &fX; }
302  operator TT*() { return &fX; }
303 
304  TEveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
305  TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
306  TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
307 
308  TT& operator[](Int_t idx) { return (&fX)[idx]; }
309  TT operator[](Int_t idx) const { return (&fX)[idx]; }
310 
311  const TT* Arr() const { return &fX; }
312  TT* Arr() { return &fX; }
313 
314  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
315  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
316  void Set(TT x, TT y) { fX = x; fY = y; }
317 
318  template <typename OO>
319  void Set(const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
320 
321  void NegateXY() { fX = - fX; fY = -fY; }
322  void Normalize(TT length=1);
323 
324  TT Phi() const;
325 
326  TT Mag2() const { return fX*fX + fY*fY;}
327  TT Mag() const { return TMath::Sqrt(Mag2());}
328 
329  TT Distance(const TEveVector2T& v) const;
330  TT SquareDistance(const TEveVector2T& v) const;
331 
332  TT Dot(const TEveVector2T& a) const;
333  TT Cross(const TEveVector2T& a) const;
334 
335  TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q);
336 
337  TEveVector2T& Mult(const TEveVector2T& a, TT af);
338 
339  ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions.
340 };
341 
345 
346 //______________________________________________________________________________
347 template<typename TT>
348 inline TT TEveVector2T<TT>::Phi() const
349 {
350  return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
351 }
352 
353 //______________________________________________________________________________
354 template<typename TT>
356 {
357  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
358  (fY - b.fY)*(fY - b.fY));
359 }
360 
361 //______________________________________________________________________________
362 template<typename TT>
364 {
365  return ((fX - b.fX) * (fX - b.fX) +
366  (fY - b.fY) * (fY - b.fY));
367 }
368 
369 //______________________________________________________________________________
370 template<typename TT>
371 inline TT TEveVector2T<TT>::Dot(const TEveVector2T<TT>& a) const
372 {
373  return a.fX*fX + a.fY*fY;
374 }
375 
376 //______________________________________________________________________________
377 template<typename TT>
379 {
380  return fX * a.fY - fY * a.fX;
381 }
382 
383 //______________________________________________________________________________
384 template<typename TT>
386 {
387  fX = p.fX - q.fX;
388  fY = p.fY - q.fY;
389  return *this;
390 }
391 
392 //______________________________________________________________________________
393 template<typename TT>
395 {
396  fX = a.fX * af;
397  fY = a.fY * af;
398  return *this;
399 }
400 
401 //______________________________________________________________________________
402 template<typename TT>
404 {
405  TEveVector2T<TT> r(a);
406  return r += b;
407 }
408 
409 //______________________________________________________________________________
410 template<typename TT>
412 {
413  TEveVector2T<TT> r(a);
414  return r -= b;
415 }
416 
417 //______________________________________________________________________________
418 template<typename TT>
420 {
421  TEveVector2T<TT> r(a);
422  return r *= b;
423 }
424 
425 //______________________________________________________________________________
426 template<typename TT>
428 {
429  TEveVector2T<TT> r(a);
430  return r *= b;
431 }
432 
433 #endif
TT Cross(const TEveVector2T &a) const
Definition: TEveVector.h:378
void Set(const TEveVector2T< OO > &v)
Definition: TEveVector.h:319
TEveVectorT & operator*=(TT s)
Definition: TEveVector.h:55
TEveVector4T< Float_t > TEveVector4
Definition: TEveVector.h:249
TT Theta() const
Definition: TEveVector.h:113
TEveVectorT & operator-=(const TEveVectorT &v)
Definition: TEveVector.h:57
void Set(const Float_t *v)
Definition: TEveVector.h:59
float Float_t
Definition: RtypesCore.h:53
TEveVectorT< TT > TP
Definition: TEveVector.h:221
TT operator[](Int_t idx) const
Definition: TEveVector.h:309
void Set(TT x, TT y)
Definition: TEveVector.h:316
TT SquareDistance(const TEveVectorT &v) const
Definition: TEveVector.h:136
TEveVector4T< Double_t > TEveVector4D
Definition: TEveVector.h:251
TT * Arr()
Definition: TEveVector.h:312
TT Phi() const
Definition: TEveVector.h:348
TT Dot(const TEveVectorT &a) const
Definition: TEveVector.h:145
TEveVectorT(TT x, TT y, TT z)
Definition: TEveVector.h:36
void Dump() const
Dump to stdout as "(x, y, z)\n".
Definition: TEveVector.cxx:28
TT & operator[](Int_t idx)
Definition: TEveVector.h:308
TEveVector4T(TT x, TT y, TT z, TT t=0)
Definition: TEveVector.h:235
TT Perp() const
Definition: TEveVector.h:79
TEveVectorT Cross(const TEveVectorT &a) const
Definition: TEveVector.h:152
void Set(const Double_t *v)
Definition: TEveVector.h:60
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
TEveVector2T< Float_t > TEveVector2
Definition: TEveVector.h:342
Bool_t IsZero() const
Definition: TEveVector.h:95
TEveVector2T(TT x, TT y)
Definition: TEveVector.h:297
TEveVector4T & operator+=(const TEveVector4T &v)
Definition: TEveVector.h:240
void Set(const Double_t *v)
Definition: TEveVector.h:315
void Set(const TEveVectorT< OO > &v)
Definition: TEveVector.h:65
TEveVector4T(const Double_t *v)
Definition: TEveVector.h:234
TT Phi() const
Definition: TEveVector.h:106
Minimal, templated four-vector.
Definition: TEveVector.h:219
Minimal, templated three-vector.
Definition: TEveVector.h:26
ClassDefNV(TEveVectorT, 2)
Double_t x[n]
Definition: legend1.C:17
void NegateXY()
Definition: TEveVector.h:321
TEveVectorT< Double_t > TEveVectorD
Definition: TEveVector.h:102
void Set(TT x, TT y, TT z)
Definition: TEveVector.h:61
TT Dot(const TEveVector2T &a) const
Definition: TEveVector.h:371
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:581
TT Mag() const
Definition: TEveVector.h:327
TT Distance(const TEveVector2T &v) const
Definition: TEveVector.h:355
TEveVector2T & operator*=(TT s)
Definition: TEveVector.h:304
TT Mag2() const
Definition: TEveVector.h:326
TEveVector4T(const TEveVectorT< OO > &v, Float_t t)
Definition: TEveVector.h:230
const TT * Arr() const
Definition: TEveVector.h:40
TVector3 is a general three vector class, which can be used for the description of different vectors ...
Definition: TVector3.h:22
TEveVectorT< TT > operator*(const TEveVectorT< TT > &a, TT b)
Definition: TEveVector.h:199
void NegateXYZ()
Definition: TEveVector.h:67
TEveVector4T & operator*=(TT s)
Definition: TEveVector.h:239
TEveVectorT< TT > operator-(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:191
const TT * Arr() const
Definition: TEveVector.h:311
TRandom2 r(17)
TEveVectorT(const Double_t *v)
Definition: TEveVector.h:35
SVector< double, 2 > v
Definition: Dict.h:5
TEveVector2T & Mult(const TEveVector2T &a, TT af)
Definition: TEveVector.h:394
TT SquareDistance(const TEveVector2T &v) const
Definition: TEveVector.h:363
void OrthoNormBase(TEveVectorT &a, TEveVectorT &b) const
Set vectors a and b to be normal to this and among themselves, both of length 1.
Definition: TEveVector.cxx:86
TEveVector4T(const Float_t *v)
Definition: TEveVector.h:233
Minimal, templated two-vector.
Definition: TEveVector.h:287
TEveVectorT & Mult(const TEveVectorT &a, TT af)
Definition: TEveVector.h:173
TEveVector2T(const TEveVector2T< OO > &v)
Definition: TEveVector.h:294
TEveVector2T< Double_t > TEveVector2D
Definition: TEveVector.h:344
TEveVector2T(const Double_t *v)
Definition: TEveVector.h:296
void Set(const Float_t *v)
Definition: TEveVector.h:314
TEveVector2T(const Float_t *v)
Definition: TEveVector.h:295
TEveVectorT< TT > operator+(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:183
TT Distance(const TEveVectorT &v) const
Definition: TEveVector.h:127
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
TT R() const
Definition: TEveVector.h:80
TT * Arr()
Definition: TEveVector.h:44
TEveVectorT(const Float_t *v)
Definition: TEveVector.h:34
TEveVectorT & operator+=(const TEveVectorT &v)
Definition: TEveVector.h:56
TEveVector4T(const TEveVector4T< OO > &v)
Definition: TEveVector.h:232
TEveVectorT< Float_t > TEveVector
Definition: TEveVector.h:100
you should not use this method at all Int_t Int_t z
Definition: TRolke.cxx:630
TEveVector4T & operator-=(const TEveVector4T &v)
Definition: TEveVector.h:241
TT Perp2() const
Definition: TEveVector.h:78
TT Eta() const
Calculate eta of the point, pretending it&#39;s a momentum vector.
Definition: TEveVector.cxx:44
TEveVectorT(const TEveVectorT< OO > &v)
Definition: TEveVector.h:33
TEveVectorT< Float_t > TEveVectorF
Definition: TEveVector.h:101
TEveVector2T & Sub(const TEveVector2T &p, const TEveVector2T &q)
Definition: TEveVector.h:385
TEveVector4T< Float_t > TEveVector4F
Definition: TEveVector.h:250
TT Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: TEveVector.cxx:56
TEveVector2T & operator+=(const TEveVector2T &v)
Definition: TEveVector.h:305
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
TT Mag() const
Definition: TEveVector.h:76
TEveVector2T & operator-=(const TEveVector2T &v)
Definition: TEveVector.h:306
TEveVectorT & Sub(const TEveVectorT &a, const TEveVectorT &b)
Definition: TEveVector.h:163
TEveVector2T< Float_t > TEveVector2F
Definition: TEveVector.h:343
TT CosTheta() const
Definition: TEveVector.h:120
TEveVectorT Orthogonal() const
Returns an orthogonal vector (not normalized).
Definition: TEveVector.cxx:70
TEveVector4T(const TEveVectorT< OO > &v)
Definition: TEveVector.h:228
Double_t Sqrt(Double_t x)
Definition: TMath.h:591
float * q
Definition: THbookFile.cxx:87
TT Mag2() const
Definition: TEveVector.h:75
TT operator[](Int_t idx) const
Definition: TEveVector.h:52
std::string & operator+=(std::string &left, const TString &right)
Definition: TString.h:456