Logo ROOT  
Reference Guide
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
ROOT::R::TRInterface & r
Definition: Object.C:4
#define b(i)
Definition: RSha256.hxx:100
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:53
void Error(const char *location, const char *msgfmt,...)
float * q
Definition: THbookFile.cxx:87
REveVector2T A two-vector template without TObject inheritance and virtual functions.
Definition: REveVector.hxx:305
REveVector2T & operator-=(const REveVector2T &v)
Definition: REveVector.hxx:323
REveVector2T & Mult(const REveVector2T &a, TT af)
Definition: REveVector.hxx:409
void Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: REveVector.cxx:128
void Set(const Float_t *v)
Definition: REveVector.hxx:331
void Set(const REveVector2T< OO > &v)
Definition: REveVector.hxx:336
REveVector2T(const Double_t *v)
Definition: REveVector.hxx:313
TT SquareDistance(const REveVector2T &v) const
Definition: REveVector.hxx:378
TT operator[](Int_t idx) const
Definition: REveVector.hxx:326
REveVector2T & operator+=(const REveVector2T &v)
Definition: REveVector.hxx:322
TT Distance(const REveVector2T &v) const
Definition: REveVector.hxx:370
TT Dot(const REveVector2T &a) const
Definition: REveVector.hxx:386
REveVector2T & operator*=(TT s)
Definition: REveVector.hxx:321
REveVector2T(const REveVector2T< OO > &v)
Definition: REveVector.hxx:311
void Dump() const
Dump to stdout as "(x, y)\n".
Definition: REveVector.cxx:141
REveVector2T & Sub(const REveVector2T &p, const REveVector2T &q)
Definition: REveVector.hxx:400
TT Cross(const REveVector2T &a) const
Definition: REveVector.hxx:393
void Set(const Double_t *v)
Definition: REveVector.hxx:332
REveVector4T A four-vector template without TObject inheritance and virtual functions.
Definition: REveVector.hxx:239
REveVector4T & operator-=(const REveVector4T &v)
Definition: REveVector.hxx:260
REveVector4T(const REveVector4T< OO > &v)
Definition: REveVector.hxx:251
REveVector4T(const REveVectorT< OO > &v)
Definition: REveVector.hxx:247
REveVector4T(const Double_t *v)
Definition: REveVector.hxx:253
void Dump() const
Dump to stdout as "(x, y, z; t)\n".
Definition: REveVector.cxx:109
REveVector4T(const REveVectorT< OO > &v, Float_t t)
Definition: REveVector.hxx:249
REveVector4T(TT x, TT y, TT z, TT t=0)
Definition: REveVector.hxx:254
REveVector4T & operator+=(const REveVector4T &v)
Definition: REveVector.hxx:259
REveVector4T & operator*=(TT s)
Definition: REveVector.hxx:258
REveVectorT A three-vector template without TObject inheritance and virtual functions.
Definition: REveVector.hxx:29
void Set(TT x, TT y, TT z)
Definition: REveVector.hxx:82
TT Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: REveVector.cxx:56
REveVectorT(const REveVectorT< OO > &v)
Definition: REveVector.hxx:35
REveVectorT & operator*=(TT s)
Definition: REveVector.hxx:76
REveVectorT & operator-=(const REveVectorT &v)
Definition: REveVector.hxx:78
REveVectorT & Mult(const REveVectorT &a, TT af)
Definition: REveVector.hxx:192
REveVectorT(TT x, TT y, TT z)
Definition: REveVector.hxx:38
REveVectorT Cross(const REveVectorT &a) const
Definition: REveVector.hxx:171
TT Distance(const REveVectorT &v) const
Definition: REveVector.hxx:146
void Set(const REveVectorT< OO > &v)
Definition: REveVector.hxx:86
void Set(const Double_t *v)
Definition: REveVector.hxx:81
TT operator[](Int_t idx) const
Definition: REveVector.hxx:73
REveVectorT(const Float_t *v)
Definition: REveVector.hxx:36
REveVectorT & Sub(const REveVectorT &a, const REveVectorT &b)
Definition: REveVector.hxx:182
REveVectorT Orthogonal() const
Returns an orthogonal vector (not normalized).
Definition: REveVector.cxx:70
TT SquareDistance(const REveVectorT &v) const
Definition: REveVector.hxx:155
void Set(const Float_t *v)
Definition: REveVector.hxx:80
void OrthoNormBase(REveVectorT &a, REveVectorT &b) const
Set vectors a and b to be normal to this and among themselves, both of length 1.
Definition: REveVector.cxx:86
TT Eta() const
Calculate eta of the point, pretending it's a momentum vector.
Definition: REveVector.cxx:44
REveVectorT(const Double_t *v)
Definition: REveVector.hxx:37
TT Dot(const REveVectorT &a) const
Definition: REveVector.hxx:164
REveVectorT & operator+=(const REveVectorT &v)
Definition: REveVector.hxx:77
void Dump() const
Dump to stdout as "(x, y, z)\n".
Definition: REveVector.cxx:28
TVector3 is a general three vector class, which can be used for the description of different vectors ...
Definition: TVector3.h:22
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:252
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
REveVector4T< Double_t > REveVector4D
Definition: REveVector.hxx:268
REveVector2T< Double_t > REveVector2D
Definition: REveVector.hxx:359
REveVectorT< Float_t > REveVectorF
Definition: REveVector.hxx:120
REveVector2T< Float_t > REveVector2F
Definition: REveVector.hxx:358
REveVector4T< Float_t > REveVector4F
Definition: REveVector.hxx:267
REveVector2T< Float_t > REveVector2
Definition: REveVector.hxx:357
REveVectorT< TT > operator-(const REveVectorT< TT > &a, const REveVectorT< TT > &b)
Definition: REveVector.hxx:210
REveVectorT< TT > operator*(const REveVectorT< TT > &a, TT b)
Definition: REveVector.hxx:218
REveVector4T< Float_t > REveVector4
Definition: REveVector.hxx:266
REveVectorT< Float_t > REveVector
Definition: REveVector.hxx:119
REveVectorT< Double_t > REveVectorD
Definition: REveVector.hxx:121
REveException operator+(const REveException &s1, const std::string &s2)
Definition: REveTypes.cxx:21
double Perp(const Vector1 &v, const Vector2 &u)
Find the magnitude of the vector component of v perpendicular to the given direction of u.
Definition: VectorUtil.h:196
VSD Structures.
Definition: StringConv.hxx:21
static constexpr double s
Double_t ATan2(Double_t y, Double_t x)
Definition: TMath.h:669
Double_t Sqrt(Double_t x)
Definition: TMath.h:681
auto * a
Definition: textangle.C:12