Logo ROOT   master
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 #ifdef R__WIN32
41  // This fixes the following rootcling error when generating the dictionary:
42  // error G34C21FBE: static_assert expression is not an integral constant expression
43  // FIXME: check if the error is fixed when upgrading llvm/clang
44  const TT *Arr() const
45  {
46  if (offsetof(TEveVectorT, fZ) != offsetof(TEveVectorT, fX) + 2 * sizeof(TT))
47  Error("TEveVectorT", "Subsequent nembers cannot be accessed as array!");
48  return &fX;
49  }
50  TT *Arr()
51  {
52  if (offsetof(TEveVectorT, fZ) != offsetof(TEveVectorT, fX) + 2 * sizeof(TT))
53  Error("TEveVectorT", "Subsequent nembers cannot be accessed as array!");
54  return &fX;
55  }
56 #else
57  const TT *Arr() const
58  {
59  static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT),
60  "Subsequent nembers cannot be accessed as array!");
61  return &fX;
62  }
63  TT *Arr()
64  {
65  static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT),
66  "Subsequent nembers cannot be accessed as array!");
67  return &fX;
68  }
69 #endif
70 
71  operator const TT*() const { return Arr(); }
72  operator TT*() { return Arr(); }
73 
74  TT operator [] (Int_t idx) const { return Arr()[idx]; }
75  TT& operator [] (Int_t idx) { return Arr()[idx]; }
76 
77  TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
78  TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
79  TEveVectorT& operator-=(const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
80 
81  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
82  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
83  void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
84  void Set(const TVector3& v);
85 
86  template <typename OO>
87  void Set(const TEveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
88 
89  void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
90  TT Normalize(TT length=1);
91 
92  TT Phi() const;
93  TT Theta() const;
94  TT CosTheta() const;
95  TT Eta() const;
96 
97  TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
98  TT Mag() const { return TMath::Sqrt(Mag2()); }
99 
100  TT Perp2() const { return fX*fX + fY*fY; }
101  TT Perp() const { return TMath::Sqrt(Perp2()); }
102  TT R() const { return Perp(); }
103 
104  TT Distance(const TEveVectorT& v) const;
105  TT SquareDistance(const TEveVectorT& v) const;
106 
107  TT Dot(const TEveVectorT& a) const;
108 
109  TEveVectorT Cross(const TEveVectorT& a) const;
110 
111  TEveVectorT& Sub(const TEveVectorT& a, const TEveVectorT& b);
112  TEveVectorT& Mult(const TEveVectorT& a, TT af);
113 
114  TEveVectorT Orthogonal() const;
115  void OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const;
116 
117  Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
118 
119  ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions.
120 };
121 
125 
126 //______________________________________________________________________________
127 template<typename TT>
128 inline TT TEveVectorT<TT>::Phi() const
129 {
130  return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
131 }
132 
133 //______________________________________________________________________________
134 template<typename TT>
135 inline TT TEveVectorT<TT>::Theta() const
136 {
137  return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
138 }
139 
140 //______________________________________________________________________________
141 template<typename TT>
142 inline TT TEveVectorT<TT>::CosTheta() const
143 {
144  Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
145 }
146 
147 //______________________________________________________________________________
148 template<typename TT>
149 inline TT TEveVectorT<TT>::Distance(const TEveVectorT& b) const
150 {
151  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
152  (fY - b.fY)*(fY - b.fY) +
153  (fZ - b.fZ)*(fZ - b.fZ));
154 }
155 
156 //______________________________________________________________________________
157 template<typename TT>
159 {
160  return ((fX - b.fX) * (fX - b.fX) +
161  (fY - b.fY) * (fY - b.fY) +
162  (fZ - b.fZ) * (fZ - b.fZ));
163 }
164 
165 //______________________________________________________________________________
166 template<typename TT>
167 inline TT TEveVectorT<TT>::Dot(const TEveVectorT& a) const
168 {
169  return a.fX*fX + a.fY*fY + a.fZ*fZ;
170 }
171 
172 //______________________________________________________________________________
173 template<typename TT>
175 {
177  r.fX = fY * a.fZ - fZ * a.fY;
178  r.fY = fZ * a.fX - fX * a.fZ;
179  r.fZ = fX * a.fY - fY * a.fX;
180  return r;
181 }
182 
183 //______________________________________________________________________________
184 template<typename TT>
186 {
187  fX = a.fX - b.fX;
188  fY = a.fY - b.fY;
189  fZ = a.fZ - b.fZ;
190  return *this;
191 }
192 
193 //______________________________________________________________________________
194 template<typename TT>
196 {
197  fX = a.fX * af;
198  fY = a.fY * af;
199  fZ = a.fZ * af;
200  return *this;
201 }
202 
203 //______________________________________________________________________________
204 template<typename TT>
206 {
208  return r += b;
209 }
210 
211 //______________________________________________________________________________
212 template<typename TT>
214 {
216  return r -= b;
217 }
218 
219 //______________________________________________________________________________
220 template<typename TT>
222 {
224  return r *= b;
225 }
226 
227 //______________________________________________________________________________
228 template<typename TT>
230 {
232  return r *= b;
233 }
234 
235 
236 //==============================================================================
237 // TEveVector4T
238 //==============================================================================
239 
240 template <typename TT>
241 class TEveVector4T : public TEveVectorT<TT>
242 {
244 
245 public:
246  TT fT;
247 
248  TEveVector4T() : TP(), fT(0) {}
249  template <typename OO>
250  TEveVector4T(const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
251  template <typename OO>
252  TEveVector4T(const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
253  template <typename OO>
254  TEveVector4T(const TEveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
255  TEveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
256  TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
257  TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
258 
259  void Dump() const;
260 
261  TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
262  TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
263  TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
264 
265  using TP::operator+=;
266  using TP::operator-=;
267 
268  ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions.
269 };
270 
274 
275 //______________________________________________________________________________
276 template<typename TT>
278 {
279  return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
280 }
281 
282 //______________________________________________________________________________
283 template<typename TT>
285 {
286  return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
287 }
288 
289 //______________________________________________________________________________
290 template<typename TT>
292 {
293  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
294 }
295 
296 //______________________________________________________________________________
297 template<typename TT>
299 {
300  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
301 }
302 
303 
304 //==============================================================================
305 // TEveVector2T
306 //==============================================================================
307 
308 template <typename TT>
310 {
311 public:
312  TT fX, fY; // Components of the point.
313 
314  TEveVector2T() : fX(0), fY(0) {}
315  template <typename OO>
317  TEveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
318  TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
319  TEveVector2T(TT x, TT y) : fX(x), fY(y) {}
320 
321  void Dump() const;
322 
323  operator const TT*() const { return &fX; }
324  operator TT*() { return &fX; }
325 
326  TEveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
327  TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
328  TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
329 
330  TT& operator[](Int_t idx) { return (&fX)[idx]; }
331  TT operator[](Int_t idx) const { return (&fX)[idx]; }
332 
333  const TT* Arr() const { return &fX; }
334  TT* Arr() { return &fX; }
335 
336  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
337  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
338  void Set(TT x, TT y) { fX = x; fY = y; }
339 
340  template <typename OO>
341  void Set(const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
342 
343  void NegateXY() { fX = - fX; fY = -fY; }
344  void Normalize(TT length=1);
345 
346  TT Phi() const;
347 
348  TT Mag2() const { return fX*fX + fY*fY;}
349  TT Mag() const { return TMath::Sqrt(Mag2());}
350 
351  TT Distance(const TEveVector2T& v) const;
352  TT SquareDistance(const TEveVector2T& v) const;
353 
354  TT Dot(const TEveVector2T& a) const;
355  TT Cross(const TEveVector2T& a) const;
356 
357  TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q);
358 
359  TEveVector2T& Mult(const TEveVector2T& a, TT af);
360 
361  ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions.
362 };
363 
367 
368 //______________________________________________________________________________
369 template<typename TT>
370 inline TT TEveVector2T<TT>::Phi() const
371 {
372  return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
373 }
374 
375 //______________________________________________________________________________
376 template<typename TT>
378 {
379  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
380  (fY - b.fY)*(fY - b.fY));
381 }
382 
383 //______________________________________________________________________________
384 template<typename TT>
386 {
387  return ((fX - b.fX) * (fX - b.fX) +
388  (fY - b.fY) * (fY - b.fY));
389 }
390 
391 //______________________________________________________________________________
392 template<typename TT>
393 inline TT TEveVector2T<TT>::Dot(const TEveVector2T<TT>& a) const
394 {
395  return a.fX*fX + a.fY*fY;
396 }
397 
398 //______________________________________________________________________________
399 template<typename TT>
401 {
402  return fX * a.fY - fY * a.fX;
403 }
404 
405 //______________________________________________________________________________
406 template<typename TT>
408 {
409  fX = p.fX - q.fX;
410  fY = p.fY - q.fY;
411  return *this;
412 }
413 
414 //______________________________________________________________________________
415 template<typename TT>
417 {
418  fX = a.fX * af;
419  fY = a.fY * af;
420  return *this;
421 }
422 
423 //______________________________________________________________________________
424 template<typename TT>
426 {
428  return r += b;
429 }
430 
431 //______________________________________________________________________________
432 template<typename TT>
434 {
436  return r -= b;
437 }
438 
439 //______________________________________________________________________________
440 template<typename TT>
442 {
444  return r *= b;
445 }
446 
447 //______________________________________________________________________________
448 template<typename TT>
450 {
452  return r *= b;
453 }
454 
455 #endif
TT Cross(const TEveVector2T &a) const
Definition: TEveVector.h:400
void Set(const TEveVector2T< OO > &v)
Definition: TEveVector.h:341
TEveVectorT & operator*=(TT s)
Definition: TEveVector.h:77
TEveVector4T< Float_t > TEveVector4
Definition: TEveVector.h:271
TT Theta() const
Definition: TEveVector.h:135
TEveVectorT & operator-=(const TEveVectorT &v)
Definition: TEveVector.h:79
void Set(const Float_t *v)
Definition: TEveVector.h:81
float Float_t
Definition: RtypesCore.h:53
void Dump() const
Dump to stdout as "(x, y, z; t)\n".
Definition: TEveVector.cxx:112
TEveVectorT< TT > TP
Definition: TEveVector.h:243
TT operator[](Int_t idx) const
Definition: TEveVector.h:331
void Set(TT x, TT y)
Definition: TEveVector.h:338
TT SquareDistance(const TEveVectorT &v) const
Definition: TEveVector.h:158
TEveVector4T< Double_t > TEveVector4D
Definition: TEveVector.h:273
TT * Arr()
Definition: TEveVector.h:334
TT Phi() const
Definition: TEveVector.h:370
TT Dot(const TEveVectorT &a) const
Definition: TEveVector.h:167
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
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:252
TT & operator[](Int_t idx)
Definition: TEveVector.h:330
TEveVector4T(TT x, TT y, TT z, TT t=0)
Definition: TEveVector.h:257
TT Perp() const
Definition: TEveVector.h:101
TEveVectorT Cross(const TEveVectorT &a) const
Definition: TEveVector.h:174
ClassDefNV(TEveVector4T, 1)
void Set(const Double_t *v)
Definition: TEveVector.h:82
TEveVector2T< Float_t > TEveVector2
Definition: TEveVector.h:364
Bool_t IsZero() const
Definition: TEveVector.h:117
TEveVector2T(TT x, TT y)
Definition: TEveVector.h:319
TEveVector4T & operator+=(const TEveVector4T &v)
Definition: TEveVector.h:262
void Set(const Double_t *v)
Definition: TEveVector.h:337
void Set(const TEveVectorT< OO > &v)
Definition: TEveVector.h:87
TEveVector4T(const Double_t *v)
Definition: TEveVector.h:256
TT Phi() const
Definition: TEveVector.h:128
Minimal, templated four-vector.
Definition: TEveVector.h:241
Minimal, templated three-vector.
Definition: TEveVector.h:26
ClassDefNV(TEveVectorT, 2)
Double_t x[n]
Definition: legend1.C:17
void NegateXY()
Definition: TEveVector.h:343
TEveVectorT< Double_t > TEveVectorD
Definition: TEveVector.h:124
Double_t ATan2(Double_t y, Double_t x)
Definition: TMath.h:669
void Set(TT x, TT y, TT z)
Definition: TEveVector.h:83
TT Dot(const TEveVector2T &a) const
Definition: TEveVector.h:393
static constexpr double s
void Error(const char *location, const char *msgfmt,...)
TT Mag() const
Definition: TEveVector.h:349
ClassDefNV(TEveVector2T, 1)
TT Distance(const TEveVector2T &v) const
Definition: TEveVector.h:377
TEveVector2T & operator*=(TT s)
Definition: TEveVector.h:326
TT Mag2() const
Definition: TEveVector.h:348
TEveVector4T(const TEveVectorT< OO > &v, Float_t t)
Definition: TEveVector.h:252
const TT * Arr() const
Definition: TEveVector.h:57
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:221
void NegateXYZ()
Definition: TEveVector.h:89
TEveVector4T & operator*=(TT s)
Definition: TEveVector.h:261
TEveVectorT< TT > operator-(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:213
ROOT::R::TRInterface & r
Definition: Object.C:4
const TT * Arr() const
Definition: TEveVector.h:333
TEveVectorT(const Double_t *v)
Definition: TEveVector.h:35
auto * a
Definition: textangle.C:12
TEveVector2T & Mult(const TEveVector2T &a, TT af)
Definition: TEveVector.h:416
TT SquareDistance(const TEveVector2T &v) const
Definition: TEveVector.h:385
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:255
Minimal, templated two-vector.
Definition: TEveVector.h:309
TEveVectorT & Mult(const TEveVectorT &a, TT af)
Definition: TEveVector.h:195
void Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: TEveVector.cxx:133
TEveVector2T(const TEveVector2T< OO > &v)
Definition: TEveVector.h:316
TEveVector2T< Double_t > TEveVector2D
Definition: TEveVector.h:366
TEveVector2T(const Double_t *v)
Definition: TEveVector.h:318
void Set(const Float_t *v)
Definition: TEveVector.h:336
TEveVector2T(const Float_t *v)
Definition: TEveVector.h:317
TEveVectorT< TT > operator+(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:205
TT Distance(const TEveVectorT &v) const
Definition: TEveVector.h:149
Double_t y[n]
Definition: legend1.C:17
TT R() const
Definition: TEveVector.h:102
TT * Arr()
Definition: TEveVector.h:63
TEveVectorT(const Float_t *v)
Definition: TEveVector.h:34
TEveVectorT & operator+=(const TEveVectorT &v)
Definition: TEveVector.h:78
TEveVector4T(const TEveVector4T< OO > &v)
Definition: TEveVector.h:254
TEveVectorT< Float_t > TEveVector
Definition: TEveVector.h:122
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:263
TT Perp2() const
Definition: TEveVector.h:100
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:123
TEveVector2T & Sub(const TEveVector2T &p, const TEveVector2T &q)
Definition: TEveVector.h:407
TEveVector4T< Float_t > TEveVector4F
Definition: TEveVector.h:272
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:327
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:98
TEveVector2T & operator-=(const TEveVector2T &v)
Definition: TEveVector.h:328
TEveVectorT & Sub(const TEveVectorT &a, const TEveVectorT &b)
Definition: TEveVector.h:185
TEveVector2T< Float_t > TEveVector2F
Definition: TEveVector.h:365
double Perp(const Vector1 &v, const Vector2 &u)
Find the magnitude of the vector component of v perpendicular to the given direction of u ...
Definition: VectorUtil.h:196
TT CosTheta() const
Definition: TEveVector.h:142
TEveVectorT Orthogonal() const
Returns an orthogonal vector (not normalized).
Definition: TEveVector.cxx:70
TEveVector4T(const TEveVectorT< OO > &v)
Definition: TEveVector.h:250
Double_t Sqrt(Double_t x)
Definition: TMath.h:681
void Dump() const
Dump to stdout as "(x, y)\n".
Definition: TEveVector.cxx:146
float * q
Definition: THbookFile.cxx:87
TT Mag2() const
Definition: TEveVector.h:97
TT operator[](Int_t idx) const
Definition: TEveVector.h:74
std::string & operator+=(std::string &left, const TString &right)
Definition: TString.h:473