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 <cstddef>
17
18class TVector3;
19
20namespace ROOT {
21namespace Experimental {
22
23////////////////////////////////////////////////////////////////////////////////
24/// REveVectorT
25/// A three-vector template without TObject inheritance and virtual functions.
26////////////////////////////////////////////////////////////////////////////////
27
28template <typename TT>
30public:
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
109
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
122
123//______________________________________________________________________________
124template<typename TT>
125inline TT REveVectorT<TT>::Phi() const
126{
127 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
128}
129
130//______________________________________________________________________________
131template<typename TT>
132inline TT REveVectorT<TT>::Theta() const
133{
134 return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
135}
136
137//______________________________________________________________________________
138template<typename TT>
140{
141 Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
142}
143
144//______________________________________________________________________________
145template<typename TT>
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//______________________________________________________________________________
154template<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//______________________________________________________________________________
163template<typename TT>
164inline TT REveVectorT<TT>::Dot(const REveVectorT& a) const
165{
166 return a.fX*fX + a.fY*fY + a.fZ*fZ;
167}
168
169//______________________________________________________________________________
170template<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//______________________________________________________________________________
181template<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//______________________________________________________________________________
191template<typename TT>
193{
194 fX = a.fX * af;
195 fY = a.fY * af;
196 fZ = a.fZ * af;
197 return *this;
198}
199
200//______________________________________________________________________________
201template<typename TT>
203{
205 return r += b;
206}
207
208//______________________________________________________________________________
209template<typename TT>
211{
213 return r -= b;
214}
215
216//______________________________________________________________________________
217template<typename TT>
219{
221 return r *= b;
222}
223
224//______________________________________________________________________________
225template<typename TT>
227{
229 return r *= b;
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// REveVector4T
234/// A four-vector template without TObject inheritance and virtual functions.
235////////////////////////////////////////////////////////////////////////////////
236
237template <typename TT>
238class REveVector4T : public REveVectorT<TT>
239{
241
242public:
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>
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
269
270//______________________________________________________________________________
271template<typename TT>
273{
274 return REveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
275}
276
277//______________________________________________________________________________
278template<typename TT>
280{
281 return REveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
282}
283
284//______________________________________________________________________________
285template<typename TT>
287{
288 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
289}
290
291//______________________________________________________________________________
292template<typename TT>
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
303template <typename TT>
305{
306public:
307 TT fX, fY; // Components of the point.
308
309 REveVector2T() : fX(0), fY(0) {}
310 template <typename OO>
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
360
361//______________________________________________________________________________
362template<typename TT>
363inline TT REveVector2T<TT>::Phi() const
364{
365 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
366}
367
368//______________________________________________________________________________
369template<typename TT>
371{
372 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
373 (fY - b.fY)*(fY - b.fY));
374}
375
376//______________________________________________________________________________
377template<typename TT>
379{
380 return ((fX - b.fX) * (fX - b.fX) +
381 (fY - b.fY) * (fY - b.fY));
382}
383
384//______________________________________________________________________________
385template<typename TT>
387{
388 return a.fX*fX + a.fY*fY;
389}
390
391//______________________________________________________________________________
392template<typename TT>
394{
395 return fX * a.fY - fY * a.fX;
396}
397
398//______________________________________________________________________________
399template<typename TT>
401{
402 fX = p.fX - q.fX;
403 fY = p.fY - q.fY;
404 return *this;
405}
406
407//______________________________________________________________________________
408template<typename TT>
410{
411 fX = a.fX * af;
412 fY = a.fY * af;
413 return *this;
414}
415
416//______________________________________________________________________________
417template<typename TT>
419{
421 return r += b;
422}
423
424//______________________________________________________________________________
425template<typename TT>
427{
429 return r -= b;
430}
431
432//______________________________________________________________________________
433template<typename TT>
435{
437 return r *= b;
438}
439
440//______________________________________________________________________________
441template<typename TT>
443{
445 return r *= b;
446}
447
448}}
449
450#endif
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
float Float_t
Definition RtypesCore.h:57
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
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)
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)
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".
REveVector4T(const REveVectorT< OO > &v, Float_t t)
REveVector4T(TT x, TT y, TT z, TT t=0)
REveVector4T & operator+=(const REveVector4T &v)
REveVector4T & operator*=(TT s)
REveVectorT A three-vector template without TObject inheritance and virtual functions.
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)
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
ELogLevel operator+(ELogLevel severity, int offset)
Definition RLogger.hxx:45
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
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
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:646
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
#define Dot(u, v)
Definition normal.c:49