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