Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <algorithm>
17#include <cstddef>
18
19class TVector3;
20
21namespace ROOT {
22namespace Experimental {
23
24////////////////////////////////////////////////////////////////////////////////
25/// REveVectorT
26/// A three-vector template without TObject inheritance and virtual functions.
27////////////////////////////////////////////////////////////////////////////////
28
29template <typename TT>
31public:
32 TT fX{0}, fY{0}, fZ{0}; // Components of the vector.
33
34 REveVectorT() = default;
35 template <typename OO>
36 REveVectorT(const REveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
37 REveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
38 REveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
39 REveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
40
41 void Dump() const;
42
43#ifdef R__WIN32
44 const TT *Arr() const
45 {
46 if (offsetof(REveVectorT, fZ) != offsetof(REveVectorT, fX) + 2 * sizeof(TT))
47 Error("REveVectorT", "Subsequent members cannot be accessed as array!");
48 return &fX;
49 }
50 TT *Arr()
51 {
52 if (offsetof(REveVectorT, fZ) != offsetof(REveVectorT, fX) + 2 * sizeof(TT))
53 Error("REveVectorT", "Subsequent members cannot be accessed as array!");
54 return &fX;
55 }
56#else
57 const TT *Arr() const
58 {
59 static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT),
60 "Subsequent members cannot be accessed as array!");
61 return &fX;
62 }
63 TT *Arr()
64 {
65 static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT),
66 "Subsequent members 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 REveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
78 REveVectorT& operator+=(const REveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
79 REveVectorT& operator-=(const REveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
80
81 bool operator==(const REveVectorT& v) const { return fX == v.fX && fY == v.fY && fZ == v.fZ; }
82 bool operator!=(const REveVectorT& v) const { return !(*this == v); }
83
84 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
85 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
86 void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
87 void Set(const TVector3& v);
88
89 template <typename OO>
90 void Set(const REveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
91
92 void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
93 TT Normalize(TT length=1);
94
95 /// Set each component to the smaller of its own and that of `v`.
97 {
98 fX = std::min(fX, v.fX);
99 fY = std::min(fY, v.fY);
100 fZ = std::min(fZ, v.fZ);
101 return *this;
102 }
103 /// Set each component to the larger of its own and that of `v`.
105 {
106 fX = std::max(fX, v.fX);
107 fY = std::max(fY, v.fY);
108 fZ = std::max(fZ, v.fZ);
109 return *this;
110 }
111
112 TT Phi() const;
113 TT Theta() const;
114 TT CosTheta() const;
115 TT Eta() const;
116
117 TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
118 TT Mag() const { return TMath::Sqrt(Mag2()); }
119
120 TT Perp2() const { return fX*fX + fY*fY; }
121 TT Perp() const { return TMath::Sqrt(Perp2()); }
122 // Transverse radius, not the 3D R() of ROOT::Math vectors (that is Mag()).
123 TT R() const { return Perp(); }
124
125 TT Distance(const REveVectorT& v) const;
126 TT SquareDistance(const REveVectorT& v) const;
127
128 TT Dot(const REveVectorT& a) const;
129
131
133 REveVectorT& Mult(const REveVectorT& a, TT af);
134
135 REveVectorT Orthogonal() const;
136 void OrthoNormBase(REveVectorT& a, REveVectorT& b) const;
137
138 Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
139};
140
144
145//______________________________________________________________________________
146template<typename TT>
147inline TT REveVectorT<TT>::Phi() const
148{
149 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
150}
151
152//______________________________________________________________________________
153template<typename TT>
154inline TT REveVectorT<TT>::Theta() const
155{
156 return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
157}
158
159//______________________________________________________________________________
160template<typename TT>
162{
163 TT ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
164}
165
166//______________________________________________________________________________
167template<typename TT>
169{
170 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
171 (fY - b.fY)*(fY - b.fY) +
172 (fZ - b.fZ)*(fZ - b.fZ));
173}
174
175//______________________________________________________________________________
176template<typename TT>
178{
179 return ((fX - b.fX) * (fX - b.fX) +
180 (fY - b.fY) * (fY - b.fY) +
181 (fZ - b.fZ) * (fZ - b.fZ));
182}
183
184//______________________________________________________________________________
185template<typename TT>
186inline TT REveVectorT<TT>::Dot(const REveVectorT& a) const
187{
188 return a.fX*fX + a.fY*fY + a.fZ*fZ;
189}
190
191//______________________________________________________________________________
192template<typename TT>
194{
196 r.fX = fY * a.fZ - fZ * a.fY;
197 r.fY = fZ * a.fX - fX * a.fZ;
198 r.fZ = fX * a.fY - fY * a.fX;
199 return r;
200}
201
202//______________________________________________________________________________
203template<typename TT>
205{
206 fX = a.fX - b.fX;
207 fY = a.fY - b.fY;
208 fZ = a.fZ - b.fZ;
209 return *this;
210}
211
212//______________________________________________________________________________
213template<typename TT>
215{
216 fX = a.fX * af;
217 fY = a.fY * af;
218 fZ = a.fZ * af;
219 return *this;
220}
221
222//______________________________________________________________________________
223template<typename TT>
225{
227 return r += b;
228}
229
230//______________________________________________________________________________
231template<typename TT>
233{
235 return r -= b;
236}
237
238//______________________________________________________________________________
239template<typename TT>
241{
243 return r *= b;
244}
245
246//______________________________________________________________________________
247template<typename TT>
249{
251 return r *= b;
252}
253
254////////////////////////////////////////////////////////////////////////////////
255/// REveVector4T
256/// A four-vector template without TObject inheritance and virtual functions.
257////////////////////////////////////////////////////////////////////////////////
258
259template <typename TT>
260class REveVector4T : public REveVectorT<TT>
261{
263
264public:
265 TT fT;
266
267 REveVector4T() : TP(), fT(0) {}
268 template <typename OO>
269 REveVector4T(const REveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
270 template <typename OO>
271 REveVector4T(const REveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
272 template <typename OO>
274 REveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
275 REveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
276 REveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
277
278 void Dump() const;
279
280 REveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
281 REveVector4T& operator+=(const REveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
282 REveVector4T& operator-=(const REveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
283
284 bool operator==(const REveVector4T& v) const { return TP::operator==(v) && fT == v.fT; }
285 bool operator!=(const REveVector4T& v) const { return !(*this == v); }
286
287 using TP::operator+=;
288 using TP::operator-=;
289};
290
294
295//______________________________________________________________________________
296template<typename TT>
298{
299 return REveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
300}
301
302//______________________________________________________________________________
303template<typename TT>
305{
306 return REveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
307}
308
309//______________________________________________________________________________
310template<typename TT>
312{
313 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
314}
315
316//______________________________________________________________________________
317template<typename TT>
319{
320 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
321}
322
323////////////////////////////////////////////////////////////////////////////////
324/// REveVector2T
325/// A two-vector template without TObject inheritance and virtual functions.
326////////////////////////////////////////////////////////////////////////////////
327
328template <typename TT>
330{
331public:
332 TT fX, fY; // Components of the point.
333
334 REveVector2T() : fX(0), fY(0) {}
335 template <typename OO>
337 REveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
338 REveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
339 REveVector2T(TT x, TT y) : fX(x), fY(y) {}
340
341 void Dump() const;
342
343 operator const TT*() const { return &fX; }
344 operator TT*() { return &fX; }
345
346 REveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
347 REveVector2T& operator+=(const REveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
348 REveVector2T& operator-=(const REveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
349
350 bool operator==(const REveVector2T& v) const { return fX == v.fX && fY == v.fY; }
351 bool operator!=(const REveVector2T& v) const { return !(*this == v); }
352
353 TT& operator[](Int_t idx) { return (&fX)[idx]; }
354 TT operator[](Int_t idx) const { return (&fX)[idx]; }
355
356 const TT* Arr() const { return &fX; }
357 TT* Arr() { return &fX; }
358
359 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
360 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
361 void Set(TT x, TT y) { fX = x; fY = y; }
362
363 template <typename OO>
364 void Set(const REveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
365
366 void NegateXY() { fX = - fX; fY = -fY; }
367 void Normalize(TT length=1);
368
369 TT Phi() const;
370
371 TT Mag2() const { return fX*fX + fY*fY;}
372 TT Mag() const { return TMath::Sqrt(Mag2());}
373
374 TT Distance(const REveVector2T& v) const;
375 TT SquareDistance(const REveVector2T& v) const;
376
377 TT Dot(const REveVector2T& a) const;
378 TT Cross(const REveVector2T& a) const;
379
380 REveVector2T& Sub(const REveVector2T& p, const REveVector2T& q);
381
382 REveVector2T& Mult(const REveVector2T& a, TT af);
383};
384
388
389//______________________________________________________________________________
390template<typename TT>
391inline TT REveVector2T<TT>::Phi() const
392{
393 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
394}
395
396//______________________________________________________________________________
397template<typename TT>
399{
400 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
401 (fY - b.fY)*(fY - b.fY));
402}
403
404//______________________________________________________________________________
405template<typename TT>
407{
408 return ((fX - b.fX) * (fX - b.fX) +
409 (fY - b.fY) * (fY - b.fY));
410}
411
412//______________________________________________________________________________
413template<typename TT>
415{
416 return a.fX*fX + a.fY*fY;
417}
418
419//______________________________________________________________________________
420template<typename TT>
422{
423 return fX * a.fY - fY * a.fX;
424}
425
426//______________________________________________________________________________
427template<typename TT>
429{
430 fX = p.fX - q.fX;
431 fY = p.fY - q.fY;
432 return *this;
433}
434
435//______________________________________________________________________________
436template<typename TT>
438{
439 fX = a.fX * af;
440 fY = a.fY * af;
441 return *this;
442}
443
444//______________________________________________________________________________
445template<typename TT>
447{
449 return r += b;
450}
451
452//______________________________________________________________________________
453template<typename TT>
455{
457 return r -= b;
458}
459
460//______________________________________________________________________________
461template<typename TT>
463{
465 return r *= b;
466}
467
468//______________________________________________________________________________
469template<typename TT>
471{
473 return r *= b;
474}
475
476}}
477
478#endif
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
float * q
REveVector2T A two-vector template without TObject inheritance and virtual functions.
REveVector2T & operator-=(const REveVector2T &v)
bool operator!=(const REveVector2T &v) const
REveVector2T & Mult(const REveVector2T &a, TT af)
void Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
void Set(const Float_t *v)
void Set(const REveVector2T< OO > &v)
bool operator==(const REveVector2T &v) const
TT SquareDistance(const REveVector2T &v) const
REveVector2T & operator+=(const REveVector2T &v)
TT Distance(const REveVector2T &v) const
TT Dot(const REveVector2T &a) const
REveVector2T & operator*=(TT s)
REveVector2T(const REveVector2T< OO > &v)
void Dump() const
Dump to stdout as "(x, y)\n".
REveVector2T & Sub(const REveVector2T &p, const REveVector2T &q)
TT Cross(const REveVector2T &a) const
void Set(const Double_t *v)
REveVector4T A four-vector template without TObject inheritance and virtual functions.
REveVector4T & operator-=(const REveVector4T &v)
REveVector4T(const REveVector4T< OO > &v)
REveVector4T(const REveVectorT< OO > &v)
void Dump() const
Dump to stdout as "(x, y, z; t)\n".
bool operator==(const REveVector4T &v) const
REveVector4T(const REveVectorT< OO > &v, Float_t t)
REveVector4T(TT x, TT y, TT z, TT t=0)
REveVector4T & operator+=(const REveVector4T &v)
bool operator!=(const REveVector4T &v) const
REveVector4T & operator*=(TT s)
REveVectorT A three-vector template without TObject inheritance and virtual functions.
REveVectorT & SetMin(const REveVectorT &v)
Set each component to the smaller of its own and that of v.
REveVectorT & SetMax(const REveVectorT &v)
Set each component to the larger of its own and that of v.
bool operator==(const REveVectorT &v) const
void Set(TT x, TT y, TT z)
TT Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
REveVectorT(const REveVectorT< OO > &v)
REveVectorT & operator*=(TT s)
bool operator!=(const REveVectorT &v) const
REveVectorT & operator-=(const REveVectorT &v)
REveVectorT & Mult(const REveVectorT &a, TT af)
REveVectorT Cross(const REveVectorT &a) const
TT Distance(const REveVectorT &v) const
void Set(const REveVectorT< OO > &v)
void Set(const Double_t *v)
TT operator[](Int_t idx) const
REveVectorT & Sub(const REveVectorT &a, const REveVectorT &b)
REveVectorT Orthogonal() const
Returns an orthogonal vector (not normalized).
TT SquareDistance(const REveVectorT &v) const
void Set(const Float_t *v)
void OrthoNormBase(REveVectorT &a, REveVectorT &b) const
Set vectors a and b to be normal to this and among themselves, both of length 1.
TT Eta() const
Calculate eta of the point, pretending it's a momentum vector.
REveVectorT(const Double_t *v)
TT Dot(const REveVectorT &a) const
REveVectorT & operator+=(const REveVectorT &v)
void Dump() const
Dump to stdout as "(x, y, z)\n".
SCoord_t fY
Definition TPoint.h:36
SCoord_t fX
Definition TPoint.h:35
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
REveVector4T< Double_t > REveVector4D
REveVector2T< Double_t > REveVector2D
REveVectorT< Float_t > REveVectorF
REveVector2T< Float_t > REveVector2F
REveVector4T< Float_t > REveVector4F
REveVector2T< Float_t > REveVector2
REveVectorT< TT > operator-(const REveVectorT< TT > &a, const REveVectorT< TT > &b)
REveVectorT< TT > operator*(const REveVectorT< TT > &a, TT b)
REveVector4T< Float_t > REveVector4
REveVectorT< Float_t > REveVector
REveVectorT< Double_t > REveVectorD
REveException operator+(const REveException &s1, const std::string &s2)
Definition REveTypes.cxx:40
Double_t ATan2(Double_t y, Double_t x)
Returns the principal value of the arc tangent of y/x, expressed in radians.
Definition TMath.h:659
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675