Logo ROOT   6.16/01
Reference Guide
TEveVector.h
Go to the documentation of this file.
1// @(#)root/eve:$Id$
2// Author: Matevz Tadel 2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2007, 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 ROOT_TEveVector
13#define ROOT_TEveVector
14
15#include "TMath.h"
16#include <cstddef>
17
18class TVector3;
19
20
21//==============================================================================
22// TEveVectorT
23//==============================================================================
24
25template <typename TT>
27{
28public:
29 TT fX, fY, fZ; // Components of the vector.
30
31 TEveVectorT() : fX(0), fY(0), fZ(0) {}
32 template <typename OO>
33 TEveVectorT(const TEveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
34 TEveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
35 TEveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
36 TEveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
37
38 void Dump() const;
39
40#ifdef R__WIN32
41 const TT *Arr() const
42 {
43 if (offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT))
44 Error("TEveVectorT", "Subsequent nembers cannot be accessed as array!");
45 return &fX;
46 }
47 TT *Arr()
48 {
49 if (offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT))
50 Error("TEveVectorT", "Subsequent nembers cannot be accessed as array!");
51 return &fX;
52 }
53#else
54 const TT *Arr() const
55 {
56 static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT),
57 "Subsequent nembers cannot be accessed as array!");
58 return &fX;
59 }
60 TT *Arr()
61 {
62 static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT),
63 "Subsequent nembers cannot be accessed as array!");
64 return &fX;
65 }
66#endif
67
68 operator const TT*() const { return Arr(); }
69 operator TT*() { return Arr(); }
70
71 TT operator [] (Int_t idx) const { return Arr()[idx]; }
72 TT& operator [] (Int_t idx) { return Arr()[idx]; }
73
74 TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
75 TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
76 TEveVectorT& operator-=(const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
77
78 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
79 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
80 void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
81 void Set(const TVector3& v);
82
83 template <typename OO>
84 void Set(const TEveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
85
86 void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
87 TT Normalize(TT length=1);
88
89 TT Phi() const;
90 TT Theta() const;
91 TT CosTheta() const;
92 TT Eta() const;
93
94 TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
95 TT Mag() const { return TMath::Sqrt(Mag2()); }
96
97 TT Perp2() const { return fX*fX + fY*fY; }
98 TT Perp() const { return TMath::Sqrt(Perp2()); }
99 TT R() const { return Perp(); }
100
101 TT Distance(const TEveVectorT& v) const;
102 TT SquareDistance(const TEveVectorT& v) const;
103
104 TT Dot(const TEveVectorT& a) const;
105
107
109 TEveVectorT& Mult(const TEveVectorT& a, TT af);
110
111 TEveVectorT Orthogonal() const;
112 void OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const;
113
114 Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
115
116 ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions.
117};
118
122
123//______________________________________________________________________________
124template<typename TT>
125inline TT TEveVectorT<TT>::Phi() const
126{
127 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
128}
129
130//______________________________________________________________________________
131template<typename TT>
132inline TT TEveVectorT<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 TEveVectorT<TT>::Dot(const TEveVectorT& 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//==============================================================================
234// TEveVector4T
235//==============================================================================
236
237template <typename TT>
238class TEveVector4T : public TEveVectorT<TT>
239{
241
242public:
243 TT fT;
244
245 TEveVector4T() : TP(), fT(0) {}
246 template <typename OO>
247 TEveVector4T(const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
248 template <typename OO>
249 TEveVector4T(const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
250 template <typename OO>
252 TEveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
253 TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
254 TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
255
256 void Dump() const;
257
258 TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
259 TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
260 TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
261
262 using TP::operator+=;
263 using TP::operator-=;
264
265 ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions.
266};
267
271
272//______________________________________________________________________________
273template<typename TT>
275{
276 return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
277}
278
279//______________________________________________________________________________
280template<typename TT>
282{
283 return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
284}
285
286//______________________________________________________________________________
287template<typename TT>
289{
290 return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
291}
292
293//______________________________________________________________________________
294template<typename TT>
296{
297 return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
298}
299
300
301//==============================================================================
302// TEveVector2T
303//==============================================================================
304
305template <typename TT>
307{
308public:
309 TT fX, fY; // Components of the point.
310
311 TEveVector2T() : fX(0), fY(0) {}
312 template <typename OO>
314 TEveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
315 TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
316 TEveVector2T(TT x, TT y) : fX(x), fY(y) {}
317
318 void Dump() const;
319
320 operator const TT*() const { return &fX; }
321 operator TT*() { return &fX; }
322
323 TEveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
324 TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
325 TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
326
327 TT& operator[](Int_t idx) { return (&fX)[idx]; }
328 TT operator[](Int_t idx) const { return (&fX)[idx]; }
329
330 const TT* Arr() const { return &fX; }
331 TT* Arr() { return &fX; }
332
333 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
334 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
335 void Set(TT x, TT y) { fX = x; fY = y; }
336
337 template <typename OO>
338 void Set(const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
339
340 void NegateXY() { fX = - fX; fY = -fY; }
341 void Normalize(TT length=1);
342
343 TT Phi() const;
344
345 TT Mag2() const { return fX*fX + fY*fY;}
346 TT Mag() const { return TMath::Sqrt(Mag2());}
347
348 TT Distance(const TEveVector2T& v) const;
349 TT SquareDistance(const TEveVector2T& v) const;
350
351 TT Dot(const TEveVector2T& a) const;
352 TT Cross(const TEveVector2T& a) const;
353
354 TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q);
355
356 TEveVector2T& Mult(const TEveVector2T& a, TT af);
357
358 ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions.
359};
360
364
365//______________________________________________________________________________
366template<typename TT>
367inline TT TEveVector2T<TT>::Phi() const
368{
369 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
370}
371
372//______________________________________________________________________________
373template<typename TT>
375{
376 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
377 (fY - b.fY)*(fY - b.fY));
378}
379
380//______________________________________________________________________________
381template<typename TT>
383{
384 return ((fX - b.fX) * (fX - b.fX) +
385 (fY - b.fY) * (fY - b.fY));
386}
387
388//______________________________________________________________________________
389template<typename TT>
391{
392 return a.fX*fX + a.fY*fY;
393}
394
395//______________________________________________________________________________
396template<typename TT>
398{
399 return fX * a.fY - fY * a.fX;
400}
401
402//______________________________________________________________________________
403template<typename TT>
405{
406 fX = p.fX - q.fX;
407 fY = p.fY - q.fY;
408 return *this;
409}
410
411//______________________________________________________________________________
412template<typename TT>
414{
415 fX = a.fX * af;
416 fY = a.fY * af;
417 return *this;
418}
419
420//______________________________________________________________________________
421template<typename TT>
423{
425 return r += b;
426}
427
428//______________________________________________________________________________
429template<typename TT>
431{
433 return r -= b;
434}
435
436//______________________________________________________________________________
437template<typename TT>
439{
441 return r *= b;
442}
443
444//______________________________________________________________________________
445template<typename TT>
447{
449 return r *= b;
450}
451
452#endif
SVector< double, 2 > v
Definition: Dict.h:5
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,...)
TEveVector2T< Float_t > TEveVector2
Definition: TEveVector.h:361
TEveVectorT< Float_t > TEveVectorF
Definition: TEveVector.h:120
TEveVectorT< TT > operator+(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:202
TEveVectorT< Double_t > TEveVectorD
Definition: TEveVector.h:121
TEveVectorT< TT > operator*(const TEveVectorT< TT > &a, TT b)
Definition: TEveVector.h:218
TEveVectorT< Float_t > TEveVector
Definition: TEveVector.h:119
TEveVector4T< Float_t > TEveVector4F
Definition: TEveVector.h:269
TEveVector4T< Double_t > TEveVector4D
Definition: TEveVector.h:270
TEveVector2T< Double_t > TEveVector2D
Definition: TEveVector.h:363
TEveVector2T< Float_t > TEveVector2F
Definition: TEveVector.h:362
TEveVectorT< TT > operator-(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:210
TEveVector4T< Float_t > TEveVector4
Definition: TEveVector.h:268
float * q
Definition: THbookFile.cxx:87
Minimal, templated two-vector.
Definition: TEveVector.h:307
TEveVector2T(const TEveVector2T< OO > &v)
Definition: TEveVector.h:313
TEveVector2T(TT x, TT y)
Definition: TEveVector.h:316
void Set(const Double_t *v)
Definition: TEveVector.h:334
TT * Arr()
Definition: TEveVector.h:331
TT Cross(const TEveVector2T &a) const
Definition: TEveVector.h:397
void Dump() const
Dump to stdout as "(x, y)\n".
Definition: TEveVector.cxx:146
TT Distance(const TEveVector2T &v) const
Definition: TEveVector.h:374
void Set(TT x, TT y)
Definition: TEveVector.h:335
TT Dot(const TEveVector2T &a) const
Definition: TEveVector.h:390
TEveVector2T(const Double_t *v)
Definition: TEveVector.h:315
TEveVector2T & Mult(const TEveVector2T &a, TT af)
Definition: TEveVector.h:413
void Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: TEveVector.cxx:133
TT SquareDistance(const TEveVector2T &v) const
Definition: TEveVector.h:382
TT Mag2() const
Definition: TEveVector.h:345
TT operator[](Int_t idx) const
Definition: TEveVector.h:328
TEveVector2T(const Float_t *v)
Definition: TEveVector.h:314
TEveVector2T & operator-=(const TEveVector2T &v)
Definition: TEveVector.h:325
TT Mag() const
Definition: TEveVector.h:346
const TT * Arr() const
Definition: TEveVector.h:330
TT Phi() const
Definition: TEveVector.h:367
void Set(const Float_t *v)
Definition: TEveVector.h:333
TT & operator[](Int_t idx)
Definition: TEveVector.h:327
void Set(const TEveVector2T< OO > &v)
Definition: TEveVector.h:338
ClassDefNV(TEveVector2T, 1)
TEveVector2T & operator+=(const TEveVector2T &v)
Definition: TEveVector.h:324
void NegateXY()
Definition: TEveVector.h:340
TEveVector2T & operator*=(TT s)
Definition: TEveVector.h:323
TEveVector2T & Sub(const TEveVector2T &p, const TEveVector2T &q)
Definition: TEveVector.h:404
Minimal, templated four-vector.
Definition: TEveVector.h:239
TEveVector4T(const TEveVectorT< OO > &v)
Definition: TEveVector.h:247
ClassDefNV(TEveVector4T, 1)
TEveVector4T(const Float_t *v)
Definition: TEveVector.h:252
void Dump() const
Dump to stdout as "(x, y, z; t)\n".
Definition: TEveVector.cxx:112
TEveVectorT< TT > TP
Definition: TEveVector.h:240
TEveVector4T(TT x, TT y, TT z, TT t=0)
Definition: TEveVector.h:254
TEveVector4T & operator-=(const TEveVector4T &v)
Definition: TEveVector.h:260
TEveVector4T & operator*=(TT s)
Definition: TEveVector.h:258
TEveVector4T & operator+=(const TEveVector4T &v)
Definition: TEveVector.h:259
TEveVector4T(const TEveVectorT< OO > &v, Float_t t)
Definition: TEveVector.h:249
TEveVector4T(const TEveVector4T< OO > &v)
Definition: TEveVector.h:251
TEveVector4T(const Double_t *v)
Definition: TEveVector.h:253
Minimal, templated three-vector.
Definition: TEveVector.h:27
TEveVectorT Cross(const TEveVectorT &a) const
Definition: TEveVector.h:171
TT Perp2() const
Definition: TEveVector.h:97
const TT * Arr() const
Definition: TEveVector.h:54
void OrthoNormBase(TEveVectorT &a, TEveVectorT &b) const
Set vectors a and b to be normal to this and among themselves, both of length 1.
Definition: TEveVector.cxx:86
void Set(TT x, TT y, TT z)
Definition: TEveVector.h:80
void Set(const Double_t *v)
Definition: TEveVector.h:79
TT Distance(const TEveVectorT &v) const
Definition: TEveVector.h:146
TT Phi() const
Definition: TEveVector.h:125
TEveVectorT(TT x, TT y, TT z)
Definition: TEveVector.h:36
TEveVectorT & Mult(const TEveVectorT &a, TT af)
Definition: TEveVector.h:192
TT CosTheta() const
Definition: TEveVector.h:139
TT Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: TEveVector.cxx:56
TEveVectorT(const Float_t *v)
Definition: TEveVector.h:34
TEveVectorT & operator+=(const TEveVectorT &v)
Definition: TEveVector.h:75
TT Eta() const
Calculate eta of the point, pretending it's a momentum vector.
Definition: TEveVector.cxx:44
TEveVectorT & operator-=(const TEveVectorT &v)
Definition: TEveVector.h:76
TT Dot(const TEveVectorT &a) const
Definition: TEveVector.h:164
TT Perp() const
Definition: TEveVector.h:98
void Set(const TEveVectorT< OO > &v)
Definition: TEveVector.h:84
TEveVectorT & operator*=(TT s)
Definition: TEveVector.h:74
TEveVectorT(const Double_t *v)
Definition: TEveVector.h:35
ClassDefNV(TEveVectorT, 2)
TT operator[](Int_t idx) const
Definition: TEveVector.h:71
TEveVectorT Orthogonal() const
Returns an orthogonal vector (not normalized).
Definition: TEveVector.cxx:70
void Dump() const
Dump to stdout as "(x, y, z)\n".
Definition: TEveVector.cxx:28
TT Mag() const
Definition: TEveVector.h:95
TT * Arr()
Definition: TEveVector.h:60
TEveVectorT & Sub(const TEveVectorT &a, const TEveVectorT &b)
Definition: TEveVector.h:182
TT Mag2() const
Definition: TEveVector.h:94
Bool_t IsZero() const
Definition: TEveVector.h:114
void Set(const Float_t *v)
Definition: TEveVector.h:78
TT Theta() const
Definition: TEveVector.h:132
TEveVectorT(const TEveVectorT< OO > &v)
Definition: TEveVector.h:33
TT SquareDistance(const TEveVectorT &v) const
Definition: TEveVector.h:155
TT R() const
Definition: TEveVector.h:99
void NegateXYZ()
Definition: TEveVector.h:86
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
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
static constexpr double s
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:667
Double_t Sqrt(Double_t x)
Definition: TMath.h:679
auto * a
Definition: textangle.C:12