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 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 REveVectorT<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 /// Set each component to the smaller of its own and that of `v`.
94 {
95 fX = std::min(fX, v.fX);
96 fY = std::min(fY, v.fY);
97 fZ = std::min(fZ, v.fZ);
98 return *this;
99 }
100 /// Set each component to the larger of its own and that of `v`.
102 {
103 fX = std::max(fX, v.fX);
104 fY = std::max(fY, v.fY);
105 fZ = std::max(fZ, v.fZ);
106 return *this;
107 }
108
109 TT Phi() const;
110 TT Theta() const;
111 TT CosTheta() const;
112 TT Eta() const;
113
114 TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
115 TT Mag() const { return TMath::Sqrt(Mag2()); }
116
117 TT Perp2() const { return fX*fX + fY*fY; }
118 TT Perp() const { return TMath::Sqrt(Perp2()); }
119 TT R() const { return Perp(); }
120
121 TT Distance(const REveVectorT& v) const;
122 TT SquareDistance(const REveVectorT& v) const;
123
124 TT Dot(const REveVectorT& a) const;
125
127
129 REveVectorT& Mult(const REveVectorT& a, TT af);
130
131 REveVectorT Orthogonal() const;
132 void OrthoNormBase(REveVectorT& a, REveVectorT& b) const;
133
134 Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
135};
136
140
141//______________________________________________________________________________
142template<typename TT>
143inline TT REveVectorT<TT>::Phi() const
144{
145 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
146}
147
148//______________________________________________________________________________
149template<typename TT>
150inline TT REveVectorT<TT>::Theta() const
151{
152 return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
153}
154
155//______________________________________________________________________________
156template<typename TT>
158{
159 Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
160}
161
162//______________________________________________________________________________
163template<typename TT>
165{
166 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
167 (fY - b.fY)*(fY - b.fY) +
168 (fZ - b.fZ)*(fZ - b.fZ));
169}
170
171//______________________________________________________________________________
172template<typename TT>
174{
175 return ((fX - b.fX) * (fX - b.fX) +
176 (fY - b.fY) * (fY - b.fY) +
177 (fZ - b.fZ) * (fZ - b.fZ));
178}
179
180//______________________________________________________________________________
181template<typename TT>
182inline TT REveVectorT<TT>::Dot(const REveVectorT& a) const
183{
184 return a.fX*fX + a.fY*fY + a.fZ*fZ;
185}
186
187//______________________________________________________________________________
188template<typename TT>
190{
192 r.fX = fY * a.fZ - fZ * a.fY;
193 r.fY = fZ * a.fX - fX * a.fZ;
194 r.fZ = fX * a.fY - fY * a.fX;
195 return r;
196}
197
198//______________________________________________________________________________
199template<typename TT>
201{
202 fX = a.fX - b.fX;
203 fY = a.fY - b.fY;
204 fZ = a.fZ - b.fZ;
205 return *this;
206}
207
208//______________________________________________________________________________
209template<typename TT>
211{
212 fX = a.fX * af;
213 fY = a.fY * af;
214 fZ = a.fZ * af;
215 return *this;
216}
217
218//______________________________________________________________________________
219template<typename TT>
221{
223 return r += b;
224}
225
226//______________________________________________________________________________
227template<typename TT>
229{
231 return r -= b;
232}
233
234//______________________________________________________________________________
235template<typename TT>
237{
239 return r *= b;
240}
241
242//______________________________________________________________________________
243template<typename TT>
245{
247 return r *= b;
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// REveVector4T
252/// A four-vector template without TObject inheritance and virtual functions.
253////////////////////////////////////////////////////////////////////////////////
254
255template <typename TT>
256class REveVector4T : public REveVectorT<TT>
257{
259
260public:
261 TT fT;
262
263 REveVector4T() : TP(), fT(0) {}
264 template <typename OO>
265 REveVector4T(const REveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
266 template <typename OO>
267 REveVector4T(const REveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
268 template <typename OO>
270 REveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
271 REveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
272 REveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
273
274 void Dump() const;
275
276 REveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
277 REveVector4T& operator+=(const REveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
278 REveVector4T& operator-=(const REveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
279
280 using TP::operator+=;
281 using TP::operator-=;
282};
283
287
288//______________________________________________________________________________
289template<typename TT>
291{
292 return REveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
293}
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, a.fY*b, a.fZ*b, a.fT*b);
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////////////////////////////////////////////////////////////////////////////////
317/// REveVector2T
318/// A two-vector template without TObject inheritance and virtual functions.
319////////////////////////////////////////////////////////////////////////////////
320
321template <typename TT>
323{
324public:
325 TT fX, fY; // Components of the point.
326
327 REveVector2T() : fX(0), fY(0) {}
328 template <typename OO>
330 REveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
331 REveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
332 REveVector2T(TT x, TT y) : fX(x), fY(y) {}
333
334 void Dump() const;
335
336 operator const TT*() const { return &fX; }
337 operator TT*() { return &fX; }
338
339 REveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
340 REveVector2T& operator+=(const REveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
341 REveVector2T& operator-=(const REveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
342
343 TT& operator[](Int_t idx) { return (&fX)[idx]; }
344 TT operator[](Int_t idx) const { return (&fX)[idx]; }
345
346 const TT* Arr() const { return &fX; }
347 TT* Arr() { return &fX; }
348
349 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
350 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
351 void Set(TT x, TT y) { fX = x; fY = y; }
352
353 template <typename OO>
354 void Set(const REveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
355
356 void NegateXY() { fX = - fX; fY = -fY; }
357 void Normalize(TT length=1);
358
359 TT Phi() const;
360
361 TT Mag2() const { return fX*fX + fY*fY;}
362 TT Mag() const { return TMath::Sqrt(Mag2());}
363
364 TT Distance(const REveVector2T& v) const;
365 TT SquareDistance(const REveVector2T& v) const;
366
367 TT Dot(const REveVector2T& a) const;
368 TT Cross(const REveVector2T& a) const;
369
370 REveVector2T& Sub(const REveVector2T& p, const REveVector2T& q);
371
372 REveVector2T& Mult(const REveVector2T& a, TT af);
373};
374
378
379//______________________________________________________________________________
380template<typename TT>
381inline TT REveVector2T<TT>::Phi() const
382{
383 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
384}
385
386//______________________________________________________________________________
387template<typename TT>
389{
390 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
391 (fY - b.fY)*(fY - b.fY));
392}
393
394//______________________________________________________________________________
395template<typename TT>
397{
398 return ((fX - b.fX) * (fX - b.fX) +
399 (fY - b.fY) * (fY - b.fY));
400}
401
402//______________________________________________________________________________
403template<typename TT>
405{
406 return a.fX*fX + a.fY*fY;
407}
408
409//______________________________________________________________________________
410template<typename TT>
412{
413 return fX * a.fY - fY * a.fX;
414}
415
416//______________________________________________________________________________
417template<typename TT>
419{
420 fX = p.fX - q.fX;
421 fY = p.fY - q.fY;
422 return *this;
423}
424
425//______________________________________________________________________________
426template<typename TT>
428{
429 fX = a.fX * af;
430 fY = a.fY * af;
431 return *this;
432}
433
434//______________________________________________________________________________
435template<typename TT>
437{
439 return r += b;
440}
441
442//______________________________________________________________________________
443template<typename TT>
445{
447 return r -= b;
448}
449
450//______________________________________________________________________________
451template<typename TT>
453{
455 return r *= b;
456}
457
458//______________________________________________________________________________
459template<typename TT>
461{
463 return r *= b;
464}
465
466}}
467
468#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)
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.
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.
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
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