ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
17 class TVector3;
18 
19 
20 //==============================================================================
21 // TEveVectorT
22 //==============================================================================
23 
24 template <typename TT>
26 {
27 public:
28  TT fX, fY, fZ; // Components of the vector.
29 
30  TEveVectorT() : fX(0), fY(0), fZ(0) {}
31  template <typename OO>
32  TEveVectorT(const TEveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
33  TEveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
34  TEveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
35  TEveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
36 
37  void Dump() const;
38 
39  operator const TT*() const { return &fX; }
40  operator TT*() { return &fX; }
41 
42  TT operator [] (Int_t idx) const { return (&fX)[idx]; }
43  TT& operator [] (Int_t idx) { return (&fX)[idx]; }
44 
45  const TT* Arr() const { return &fX; }
46  TT* Arr() { return &fX; }
47 
48  TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
49  TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
50  TEveVectorT& operator-=(const TEveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
51 
52  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
53  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
54  void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
55  void Set(const TVector3& v);
56 
57  template <typename OO>
58  void Set(const TEveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
59 
60  void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
61  TT Normalize(TT length=1);
62 
63  TT Phi() const;
64  TT Theta() const;
65  TT CosTheta() const;
66  TT Eta() const;
67 
68  TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
69  TT Mag() const { return TMath::Sqrt(Mag2()); }
70 
71  TT Perp2() const { return fX*fX + fY*fY; }
72  TT Perp() const { return TMath::Sqrt(Perp2()); }
73  TT R() const { return Perp(); }
74 
75  TT Distance(const TEveVectorT& v) const;
76  TT SquareDistance(const TEveVectorT& v) const;
77 
78  TT Dot(const TEveVectorT& a) const;
79 
80  TEveVectorT Cross(const TEveVectorT& a) const;
81 
82  TEveVectorT& Sub(const TEveVectorT& a, const TEveVectorT& b);
83  TEveVectorT& Mult(const TEveVectorT& a, TT af);
84 
85  TEveVectorT Orthogonal() const;
86  void OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const;
87 
88  Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
89 
90  ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions.
91 };
92 
96 
97 //______________________________________________________________________________
98 template<typename TT>
99 inline TT TEveVectorT<TT>::Phi() const
100 {
101  return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
102 }
103 
104 //______________________________________________________________________________
105 template<typename TT>
106 inline TT TEveVectorT<TT>::Theta() const
107 {
108  return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
109 }
110 
111 //______________________________________________________________________________
112 template<typename TT>
113 inline TT TEveVectorT<TT>::CosTheta() const
114 {
115  Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
116 }
117 
118 //______________________________________________________________________________
119 template<typename TT>
120 inline TT TEveVectorT<TT>::Distance(const TEveVectorT& b) const
121 {
122  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
123  (fY - b.fY)*(fY - b.fY) +
124  (fZ - b.fZ)*(fZ - b.fZ));
125 }
126 
127 //______________________________________________________________________________
128 template<typename TT>
130 {
131  return ((fX - b.fX) * (fX - b.fX) +
132  (fY - b.fY) * (fY - b.fY) +
133  (fZ - b.fZ) * (fZ - b.fZ));
134 }
135 
136 //______________________________________________________________________________
137 template<typename TT>
138 inline TT TEveVectorT<TT>::Dot(const TEveVectorT& a) const
139 {
140  return a.fX*fX + a.fY*fY + a.fZ*fZ;
141 }
142 
143 //______________________________________________________________________________
144 template<typename TT>
146 {
148  r.fX = fY * a.fZ - fZ * a.fY;
149  r.fY = fZ * a.fX - fX * a.fZ;
150  r.fZ = fX * a.fY - fY * a.fX;
151  return r;
152 }
153 
154 //______________________________________________________________________________
155 template<typename TT>
157 {
158  fX = a.fX - b.fX;
159  fY = a.fY - b.fY;
160  fZ = a.fZ - b.fZ;
161  return *this;
162 }
163 
164 //______________________________________________________________________________
165 template<typename TT>
167 {
168  fX = a.fX * af;
169  fY = a.fY * af;
170  fZ = a.fZ * af;
171  return *this;
172 }
173 
174 //______________________________________________________________________________
175 template<typename TT>
177 {
178  TEveVectorT<TT> r(a);
179  return r += b;
180 }
181 
182 //______________________________________________________________________________
183 template<typename TT>
185 {
186  TEveVectorT<TT> r(a);
187  return r -= b;
188 }
189 
190 //______________________________________________________________________________
191 template<typename TT>
193 {
194  TEveVectorT<TT> r(a);
195  return r *= b;
196 }
197 
198 //______________________________________________________________________________
199 template<typename TT>
201 {
202  TEveVectorT<TT> r(a);
203  return r *= b;
204 }
205 
206 
207 //==============================================================================
208 // TEveVector4T
209 //==============================================================================
210 
211 template <typename TT>
212 class TEveVector4T : public TEveVectorT<TT>
213 {
215 
216 public:
217  TT fT;
218 
219  TEveVector4T() : TP(), fT(0) {}
220  template <typename OO>
221  TEveVector4T(const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
222  template <typename OO>
223  TEveVector4T(const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
224  template <typename OO>
225  TEveVector4T(const TEveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
226  TEveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
227  TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
228  TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
229 
230  void Dump() const;
231 
232  TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
233  TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
234  TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
235 
236  using TP::operator+=;
237  using TP::operator-=;
238 
239  ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions.
240 };
241 
245 
246 //______________________________________________________________________________
247 template<typename TT>
249 {
250  return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
251 }
252 
253 //______________________________________________________________________________
254 template<typename TT>
256 {
257  return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
258 }
259 
260 //______________________________________________________________________________
261 template<typename TT>
263 {
264  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
265 }
266 
267 //______________________________________________________________________________
268 template<typename TT>
270 {
271  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
272 }
273 
274 
275 //==============================================================================
276 // TEveVector2T
277 //==============================================================================
278 
279 template <typename TT>
281 {
282 public:
283  TT fX, fY; // Components of the point.
284 
285  TEveVector2T() : fX(0), fY(0) {}
286  template <typename OO>
287  TEveVector2T(const TEveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
288  TEveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
289  TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
290  TEveVector2T(TT x, TT y) : fX(x), fY(y) {}
291 
292  void Dump() const;
293 
294  operator const TT*() const { return &fX; }
295  operator TT*() { return &fX; }
296 
297  TEveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
298  TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
299  TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
300 
301  TT& operator[](Int_t idx) { return (&fX)[idx]; }
302  TT operator[](Int_t idx) const { return (&fX)[idx]; }
303 
304  const TT* Arr() const { return &fX; }
305  TT* Arr() { return &fX; }
306 
307  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
308  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
309  void Set(TT x, TT y) { fX = x; fY = y; }
310 
311  template <typename OO>
312  void Set(const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
313 
314  void NegateXY() { fX = - fX; fY = -fY; }
315  void Normalize(TT length=1);
316 
317  TT Phi() const;
318 
319  TT Mag2() const { return fX*fX + fY*fY;}
320  TT Mag() const { return TMath::Sqrt(Mag2());}
321 
322  TT Distance(const TEveVector2T& v) const;
323  TT SquareDistance(const TEveVector2T& v) const;
324 
325  TT Dot(const TEveVector2T& a) const;
326  TT Cross(const TEveVector2T& a) const;
327 
328  TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q);
329 
330  TEveVector2T& Mult(const TEveVector2T& a, TT af);
331 
332  ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions.
333 };
334 
338 
339 //______________________________________________________________________________
340 template<typename TT>
341 inline TT TEveVector2T<TT>::Phi() const
342 {
343  return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
344 }
345 
346 //______________________________________________________________________________
347 template<typename TT>
349 {
350  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
351  (fY - b.fY)*(fY - b.fY));
352 }
353 
354 //______________________________________________________________________________
355 template<typename TT>
357 {
358  return ((fX - b.fX) * (fX - b.fX) +
359  (fY - b.fY) * (fY - b.fY));
360 }
361 
362 //______________________________________________________________________________
363 template<typename TT>
364 inline TT TEveVector2T<TT>::Dot(const TEveVector2T<TT>& a) const
365 {
366  return a.fX*fX + a.fY*fY;
367 }
368 
369 //______________________________________________________________________________
370 template<typename TT>
372 {
373  return fX * a.fY - fY * a.fX;
374 }
375 
376 //______________________________________________________________________________
377 template<typename TT>
379 {
380  fX = p.fX - q.fX;
381  fY = p.fY - q.fY;
382  return *this;
383 }
384 
385 //______________________________________________________________________________
386 template<typename TT>
388 {
389  fX = a.fX * af;
390  fY = a.fY * af;
391  return *this;
392 }
393 
394 //______________________________________________________________________________
395 template<typename TT>
397 {
398  TEveVector2T<TT> r(a);
399  return r += b;
400 }
401 
402 //______________________________________________________________________________
403 template<typename TT>
405 {
406  TEveVector2T<TT> r(a);
407  return r -= b;
408 }
409 
410 //______________________________________________________________________________
411 template<typename TT>
413 {
414  TEveVector2T<TT> r(a);
415  return r *= b;
416 }
417 
418 //______________________________________________________________________________
419 template<typename TT>
421 {
422  TEveVector2T<TT> r(a);
423  return r *= b;
424 }
425 
426 #endif
void Set(const TEveVector2T< OO > &v)
Definition: TEveVector.h:312
TT Dot(const TEveVector2T &a) const
Definition: TEveVector.h:364
TEveVectorT & operator*=(TT s)
Definition: TEveVector.h:48
TEveVector4T< Float_t > TEveVector4
Definition: TEveVector.h:242
TT Phi() const
Definition: TEveVector.h:341
Bool_t IsZero() const
Definition: TEveVector.h:88
TEveVectorT & operator-=(const TEveVectorT &v)
Definition: TEveVector.h:50
void Set(const Float_t *v)
Definition: TEveVector.h:52
float Float_t
Definition: RtypesCore.h:53
TEveVectorT< TT > TP
Definition: TEveVector.h:214
void Set(TT x, TT y)
Definition: TEveVector.h:309
TEveVector4T< Double_t > TEveVector4D
Definition: TEveVector.h:244
TT * Arr()
Definition: TEveVector.h:305
TEveVectorT(TT x, TT y, TT z)
Definition: TEveVector.h:35
TT CosTheta() const
Definition: TEveVector.h:113
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:254
TT & operator[](Int_t idx)
Definition: TEveVector.h:301
TT Distance(const TEveVector2T &v) const
Definition: TEveVector.h:348
TEveVector4T(TT x, TT y, TT z, TT t=0)
Definition: TEveVector.h:228
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
TT Dot(const TEveVectorT &a) const
Definition: TEveVector.h:138
ClassDefNV(TEveVector4T, 1)
void Set(const Double_t *v)
Definition: TEveVector.h:53
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TArc * a
Definition: textangle.C:12
TEveVector2T< Float_t > TEveVector2
Definition: TEveVector.h:335
TEveVector2T(TT x, TT y)
Definition: TEveVector.h:290
TEveVector4T & operator+=(const TEveVector4T &v)
Definition: TEveVector.h:233
void Set(const Double_t *v)
Definition: TEveVector.h:308
void Set(const TEveVectorT< OO > &v)
Definition: TEveVector.h:58
TEveVector4T(const Double_t *v)
Definition: TEveVector.h:227
TT Mag2() const
Definition: TEveVector.h:68
TT Distance(const TEveVectorT &v) const
Definition: TEveVector.h:120
Minimal, templated four-vector.
Definition: TEveVector.h:212
Minimal, templated three-vector.
Definition: TEveVector.h:25
ClassDefNV(TEveVectorT, 2)
void Dump() const
Dump to stdout as "(x, y, z)\n".
Definition: TEveVector.cxx:28
Double_t x[n]
Definition: legend1.C:17
void NegateXY()
Definition: TEveVector.h:314
TEveVectorT< Double_t > TEveVectorD
Definition: TEveVector.h:95
void Dump() const
Dump to stdout as "(x, y, z; t)\n".
Definition: TEveVector.cxx:110
void Set(TT x, TT y, TT z)
Definition: TEveVector.h:54
TEveVectorT Orthogonal() const
Returns an orthogonal vector (not normalized).
Definition: TEveVector.cxx:70
Double_t ATan2(Double_t, Double_t)
Definition: TMath.h:454
Float_t z[5]
Definition: Ifit.C:16
TEveVectorT Cross(const TEveVectorT &a) const
Definition: TEveVector.h:145
TT Perp() const
Definition: TEveVector.h:72
ClassDefNV(TEveVector2T, 1)
TEveVector2T & operator*=(TT s)
Definition: TEveVector.h:297
TT Mag() const
Definition: TEveVector.h:320
TEveVector4T(const TEveVectorT< OO > &v, Float_t t)
Definition: TEveVector.h:223
TT Eta() const
Calculate eta of the point, pretending it's a momentum vector.
Definition: TEveVector.cxx:44
TEveVectorT< TT > operator*(const TEveVectorT< TT > &a, TT b)
Definition: TEveVector.h:192
TThread * t[5]
Definition: threadsh1.C:13
void NegateXYZ()
Definition: TEveVector.h:60
TT R() const
Definition: TEveVector.h:73
TEveVector4T & operator*=(TT s)
Definition: TEveVector.h:232
TEveVectorT< TT > operator-(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:184
ROOT::R::TRInterface & r
Definition: Object.C:4
TEveVectorT(const Double_t *v)
Definition: TEveVector.h:34
Double_t length(const TVector2 &v)
Definition: CsgOps.cxx:347
SVector< double, 2 > v
Definition: Dict.h:5
void Dump() const
Dump to stdout as "(x, y)\n".
Definition: TEveVector.cxx:144
TT Theta() const
Definition: TEveVector.h:106
TEveVector2T & Mult(const TEveVector2T &a, TT af)
Definition: TEveVector.h:387
TEveVector4T(const Float_t *v)
Definition: TEveVector.h:226
Minimal, templated two-vector.
Definition: TEveVector.h:280
const TT * Arr() const
Definition: TEveVector.h:45
TT Phi() const
Definition: TEveVector.h:99
TEveVectorT & Mult(const TEveVectorT &a, TT af)
Definition: TEveVector.h:166
void Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: TEveVector.cxx:131
TT Mag() const
Definition: TEveVector.h:69
TEveVector2T(const TEveVector2T< OO > &v)
Definition: TEveVector.h:287
TEveVector2T< Double_t > TEveVector2D
Definition: TEveVector.h:337
TEveVector2T(const Double_t *v)
Definition: TEveVector.h:289
void Set(const Float_t *v)
Definition: TEveVector.h:307
TEveVector2T(const Float_t *v)
Definition: TEveVector.h:288
TEveVectorT< TT > operator+(const TEveVectorT< TT > &a, const TEveVectorT< TT > &b)
Definition: TEveVector.h:176
double Double_t
Definition: RtypesCore.h:55
const TT * Arr() const
Definition: TEveVector.h:304
Double_t y[n]
Definition: legend1.C:17
TT SquareDistance(const TEveVector2T &v) const
Definition: TEveVector.h:356
TT * Arr()
Definition: TEveVector.h:46
TEveVectorT(const Float_t *v)
Definition: TEveVector.h:33
TEveVectorT & operator+=(const TEveVectorT &v)
Definition: TEveVector.h:49
TEveVector4T(const TEveVector4T< OO > &v)
Definition: TEveVector.h:225
TEveVectorT< Float_t > TEveVector
Definition: TEveVector.h:93
TEveVector4T & operator-=(const TEveVector4T &v)
Definition: TEveVector.h:234
TEveVectorT(const TEveVectorT< OO > &v)
Definition: TEveVector.h:32
TEveVectorT< Float_t > TEveVectorF
Definition: TEveVector.h:94
TEveVector2T & Sub(const TEveVector2T &p, const TEveVector2T &q)
Definition: TEveVector.h:378
TT operator[](Int_t idx) const
Definition: TEveVector.h:302
TEveVector4T< Float_t > TEveVector4F
Definition: TEveVector.h:243
TT Normalize(TT length=1)
Normalize the vector to length if current length is non-zero.
Definition: TEveVector.cxx:56
TEveVector2T & operator+=(const TEveVector2T &v)
Definition: TEveVector.h:298
TT Mag2() const
Definition: TEveVector.h:319
TEveVector2T & operator-=(const TEveVector2T &v)
Definition: TEveVector.h:299
TEveVectorT & Sub(const TEveVectorT &a, const TEveVectorT &b)
Definition: TEveVector.h:156
TEveVector2T< Float_t > TEveVector2F
Definition: TEveVector.h:336
TT Perp2() const
Definition: TEveVector.h:71
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:198
TEveVector4T(const TEveVectorT< OO > &v)
Definition: TEveVector.h:221
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
TT operator[](Int_t idx) const
Definition: TEveVector.h:42
float * q
Definition: THbookFile.cxx:87
TT Cross(const TEveVector2T &a) const
Definition: TEveVector.h:371
TT SquareDistance(const TEveVectorT &v) const
Definition: TEveVector.h:129