Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
REveVector.hxx
Go to the documentation of this file.
1 // @(#)root/eve7:$Id$
2 // Author: Matevz Tadel 2007, 2018
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2019, 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 ROOT7_REveVector
13 #define ROOT7_REveVector
14 
15 #include "TMath.h"
16 #include <cstddef>
17 
18 class TVector3;
19 
20 namespace ROOT {
21 namespace Experimental {
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// REveVectorT
25 /// A three-vector template without TObject inheritance and virtual functions.
26 ////////////////////////////////////////////////////////////////////////////////
27 
28 template <typename TT>
29 class REveVectorT {
30 public:
31  TT fX{0}, fY{0}, fZ{0}; // Components of the vector.
32 
33  REveVectorT() = default;
34  template <typename OO>
35  REveVectorT(const REveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
36  REveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
37  REveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
38  REveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
39 
40  void Dump() const;
41 
42 #ifdef R__WIN32
43  const TT *Arr() const
44  {
45  if (offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT))
46  Error("REveVectorT", "Subsequent members cannot be accessed as array!");
47  return &fX;
48  }
49  TT *Arr()
50  {
51  if (offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT))
52  Error("REveVectorT", "Subsequent members cannot be accessed as array!");
53  return &fX;
54  }
55 #else
56  const TT *Arr() const
57  {
58  static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT),
59  "Subsequent members cannot be accessed as array!");
60  return &fX;
61  }
62  TT *Arr()
63  {
64  static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT),
65  "Subsequent members cannot be accessed as array!");
66  return &fX;
67  }
68 #endif
69 
70  operator const TT*() const { return Arr(); }
71  operator TT*() { return Arr(); }
72 
73  TT operator [] (Int_t idx) const { return Arr()[idx]; }
74  TT& operator [] (Int_t idx) { return Arr()[idx]; }
75 
76  REveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
77  REveVectorT& operator+=(const REveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
78  REveVectorT& operator-=(const REveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
79 
80  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
81  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
82  void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
83  void Set(const TVector3& v);
84 
85  template <typename OO>
86  void Set(const REveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
87 
88  void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
89  TT Normalize(TT length=1);
90 
91  TT Phi() const;
92  TT Theta() const;
93  TT CosTheta() const;
94  TT Eta() const;
95 
96  TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
97  TT Mag() const { return TMath::Sqrt(Mag2()); }
98 
99  TT Perp2() const { return fX*fX + fY*fY; }
100  TT Perp() const { return TMath::Sqrt(Perp2()); }
101  TT R() const { return Perp(); }
102 
103  TT Distance(const REveVectorT& v) const;
104  TT SquareDistance(const REveVectorT& v) const;
105 
106  TT Dot(const REveVectorT& a) const;
107 
108  REveVectorT Cross(const REveVectorT& a) const;
109 
110  REveVectorT& Sub(const REveVectorT& a, const REveVectorT& b);
111  REveVectorT& Mult(const REveVectorT& a, TT af);
112 
113  REveVectorT Orthogonal() const;
114  void OrthoNormBase(REveVectorT& a, REveVectorT& b) const;
115 
116  Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
117 };
118 
119 typedef REveVectorT<Float_t> REveVector;
120 typedef REveVectorT<Float_t> REveVectorF;
121 typedef REveVectorT<Double_t> REveVectorD;
122 
123 //______________________________________________________________________________
124 template<typename TT>
125 inline TT REveVectorT<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 REveVectorT<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 REveVectorT<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 REveVectorT<TT>::Distance(const REveVectorT& 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>
155 inline TT REveVectorT<TT>::SquareDistance(const REveVectorT& b) const
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 REveVectorT<TT>::Dot(const REveVectorT& a) const
165 {
166  return a.fX*fX + a.fY*fY + a.fZ*fZ;
167 }
168 
169 //______________________________________________________________________________
170 template<typename TT>
171 inline REveVectorT<TT> REveVectorT<TT>::Cross(const REveVectorT<TT>& a) const
172 {
173  REveVectorT<TT> r;
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>
182 inline REveVectorT<TT>& REveVectorT<TT>::Sub(const REveVectorT<TT>& a, const REveVectorT<TT>& b)
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>
192 inline REveVectorT<TT>& REveVectorT<TT>::Mult(const REveVectorT<TT>& a, TT af)
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>
202 inline REveVectorT<TT> operator+(const REveVectorT<TT>& a, const REveVectorT<TT>& b)
203 {
204  REveVectorT<TT> r(a);
205  return r += b;
206 }
207 
208 //______________________________________________________________________________
209 template<typename TT>
210 inline REveVectorT<TT> operator-(const REveVectorT<TT>& a, const REveVectorT<TT>& b)
211 {
212  REveVectorT<TT> r(a);
213  return r -= b;
214 }
215 
216 //______________________________________________________________________________
217 template<typename TT>
218 inline REveVectorT<TT> operator*(const REveVectorT<TT>& a, TT b)
219 {
220  REveVectorT<TT> r(a);
221  return r *= b;
222 }
223 
224 //______________________________________________________________________________
225 template<typename TT>
226 inline REveVectorT<TT> operator*(TT b, const REveVectorT<TT>& a)
227 {
228  REveVectorT<TT> r(a);
229  return r *= b;
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 /// REveVector4T
234 /// A four-vector template without TObject inheritance and virtual functions.
235 ////////////////////////////////////////////////////////////////////////////////
236 
237 template <typename TT>
238 class REveVector4T : public REveVectorT<TT>
239 {
240  typedef REveVectorT<TT> TP;
241 
242 public:
243  TT fT;
244 
245  REveVector4T() : TP(), fT(0) {}
246  template <typename OO>
247  REveVector4T(const REveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
248  template <typename OO>
249  REveVector4T(const REveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
250  template <typename OO>
251  REveVector4T(const REveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
252  REveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
253  REveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
254  REveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
255 
256  void Dump() const;
257 
258  REveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
259  REveVector4T& operator+=(const REveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
260  REveVector4T& operator-=(const REveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
261 
262  using TP::operator+=;
263  using TP::operator-=;
264 };
265 
266 typedef REveVector4T<Float_t> REveVector4;
267 typedef REveVector4T<Float_t> REveVector4F;
268 typedef REveVector4T<Double_t> REveVector4D;
269 
270 //______________________________________________________________________________
271 template<typename TT>
272 inline REveVector4T<TT> operator+(const REveVector4T<TT>& a, const REveVector4T<TT>& b)
273 {
274  return REveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
275 }
276 
277 //______________________________________________________________________________
278 template<typename TT>
279 inline REveVector4T<TT> operator-(const REveVector4T<TT>& a, const REveVector4T<TT>& b)
280 {
281  return REveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
282 }
283 
284 //______________________________________________________________________________
285 template<typename TT>
286 inline REveVector4T<TT> operator*(const REveVector4T<TT>& a, TT b)
287 {
288  return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
289 }
290 
291 //______________________________________________________________________________
292 template<typename TT>
293 inline REveVector4T<TT> operator*(TT b, const REveVector4T<TT>& a)
294 {
295  return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
296 }
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 /// REveVector2T
300 /// A two-vector template without TObject inheritance and virtual functions.
301 ////////////////////////////////////////////////////////////////////////////////
302 
303 template <typename TT>
304 class REveVector2T
305 {
306 public:
307  TT fX, fY; // Components of the point.
308 
309  REveVector2T() : fX(0), fY(0) {}
310  template <typename OO>
311  REveVector2T(const REveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
312  REveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
313  REveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
314  REveVector2T(TT x, TT y) : fX(x), fY(y) {}
315 
316  void Dump() const;
317 
318  operator const TT*() const { return &fX; }
319  operator TT*() { return &fX; }
320 
321  REveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
322  REveVector2T& operator+=(const REveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
323  REveVector2T& operator-=(const REveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
324 
325  TT& operator[](Int_t idx) { return (&fX)[idx]; }
326  TT operator[](Int_t idx) const { return (&fX)[idx]; }
327 
328  const TT* Arr() const { return &fX; }
329  TT* Arr() { return &fX; }
330 
331  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
332  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
333  void Set(TT x, TT y) { fX = x; fY = y; }
334 
335  template <typename OO>
336  void Set(const REveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
337 
338  void NegateXY() { fX = - fX; fY = -fY; }
339  void Normalize(TT length=1);
340 
341  TT Phi() const;
342 
343  TT Mag2() const { return fX*fX + fY*fY;}
344  TT Mag() const { return TMath::Sqrt(Mag2());}
345 
346  TT Distance(const REveVector2T& v) const;
347  TT SquareDistance(const REveVector2T& v) const;
348 
349  TT Dot(const REveVector2T& a) const;
350  TT Cross(const REveVector2T& a) const;
351 
352  REveVector2T& Sub(const REveVector2T& p, const REveVector2T& q);
353 
354  REveVector2T& Mult(const REveVector2T& a, TT af);
355 };
356 
357 typedef REveVector2T<Float_t> REveVector2;
358 typedef REveVector2T<Float_t> REveVector2F;
359 typedef REveVector2T<Double_t> REveVector2D;
360 
361 //______________________________________________________________________________
362 template<typename TT>
363 inline TT REveVector2T<TT>::Phi() const
364 {
365  return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
366 }
367 
368 //______________________________________________________________________________
369 template<typename TT>
370 inline TT REveVector2T<TT>::Distance( const REveVector2T<TT>& b) const
371 {
372  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
373  (fY - b.fY)*(fY - b.fY));
374 }
375 
376 //______________________________________________________________________________
377 template<typename TT>
378 inline TT REveVector2T<TT>::SquareDistance(const REveVector2T<TT>& b) const
379 {
380  return ((fX - b.fX) * (fX - b.fX) +
381  (fY - b.fY) * (fY - b.fY));
382 }
383 
384 //______________________________________________________________________________
385 template<typename TT>
386 inline TT REveVector2T<TT>::Dot(const REveVector2T<TT>& a) const
387 {
388  return a.fX*fX + a.fY*fY;
389 }
390 
391 //______________________________________________________________________________
392 template<typename TT>
393 inline TT REveVector2T<TT>::Cross(const REveVector2T<TT>& a) const
394 {
395  return fX * a.fY - fY * a.fX;
396 }
397 
398 //______________________________________________________________________________
399 template<typename TT>
400 inline REveVector2T<TT>& REveVector2T<TT>::Sub(const REveVector2T<TT>& p, const REveVector2T<TT>& q)
401 {
402  fX = p.fX - q.fX;
403  fY = p.fY - q.fY;
404  return *this;
405 }
406 
407 //______________________________________________________________________________
408 template<typename TT>
409 inline REveVector2T<TT>& REveVector2T<TT>::Mult(const REveVector2T<TT>& a, TT af)
410 {
411  fX = a.fX * af;
412  fY = a.fY * af;
413  return *this;
414 }
415 
416 //______________________________________________________________________________
417 template<typename TT>
418 inline REveVector2T<TT> operator+(const REveVector2T<TT>& a, const REveVector2T<TT>& b)
419 {
420  REveVector2T<TT> r(a);
421  return r += b;
422 }
423 
424 //______________________________________________________________________________
425 template<typename TT>
426 inline REveVector2T<TT> operator-(const REveVector2T<TT>& a, const REveVector2T<TT>& b)
427 {
428  REveVector2T<TT> r(a);
429  return r -= b;
430 }
431 
432 //______________________________________________________________________________
433 template<typename TT>
434 inline REveVector2T<TT> operator*(const REveVector2T<TT>& a, TT b)
435 {
436  REveVector2T<TT> r(a);
437  return r *= b;
438 }
439 
440 //______________________________________________________________________________
441 template<typename TT>
442 inline REveVector2T<TT> operator*(TT b, const REveVector2T<TT>& a)
443 {
444  REveVector2T<TT> r(a);
445  return r *= b;
446 }
447 
448 }}
449 
450 #endif