Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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 #include <cstddef>
17 
18 class TVector3;
19 
20 
21 //==============================================================================
22 // TEveVectorT
23 //==============================================================================
24 
25 template <typename TT>
26 class TEveVectorT
27 {
28 public:
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  // This fixes the following rootcling error when generating the dictionary:
42  // error G34C21FBE: static_assert expression is not an integral constant expression
43  // FIXME: check if the error is fixed when upgrading llvm/clang
44  const TT *Arr() const
45  {
46  if (offsetof(TEveVectorT, fZ) != offsetof(TEveVectorT, fX) + 2 * sizeof(TT))
47  Error("TEveVectorT", "Subsequent nembers cannot be accessed as array!");
48  return &fX;
49  }
50  TT *Arr()
51  {
52  if (offsetof(TEveVectorT, fZ) != offsetof(TEveVectorT, fX) + 2 * sizeof(TT))
53  Error("TEveVectorT", "Subsequent nembers cannot be accessed as array!");
54  return &fX;
55  }
56 #else
57  const TT *Arr() const
58  {
59  static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT),
60  "Subsequent nembers cannot be accessed as array!");
61  return &fX;
62  }
63  TT *Arr()
64  {
65  static_assert(offsetof(TEveVectorT, fZ) == offsetof(TEveVectorT, fX) + 2 * sizeof(TT),
66  "Subsequent nembers 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  TEveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
78  TEveVectorT& operator+=(const TEveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
79  TEveVectorT& operator-=(const TEveVectorT& 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 TEveVectorT<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  TT Phi() const;
93  TT Theta() const;
94  TT CosTheta() const;
95  TT Eta() const;
96 
97  TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
98  TT Mag() const { return TMath::Sqrt(Mag2()); }
99 
100  TT Perp2() const { return fX*fX + fY*fY; }
101  TT Perp() const { return TMath::Sqrt(Perp2()); }
102  TT R() const { return Perp(); }
103 
104  TT Distance(const TEveVectorT& v) const;
105  TT SquareDistance(const TEveVectorT& v) const;
106 
107  TT Dot(const TEveVectorT& a) const;
108 
109  TEveVectorT Cross(const TEveVectorT& a) const;
110 
111  TEveVectorT& Sub(const TEveVectorT& a, const TEveVectorT& b);
112  TEveVectorT& Mult(const TEveVectorT& a, TT af);
113 
114  TEveVectorT Orthogonal() const;
115  void OrthoNormBase(TEveVectorT& a, TEveVectorT& b) const;
116 
117  Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
118 
119  ClassDefNV(TEveVectorT, 2); // A three-vector template without TObject inheritance and virtual functions.
120 };
121 
122 typedef TEveVectorT<Float_t> TEveVector;
123 typedef TEveVectorT<Float_t> TEveVectorF;
124 typedef TEveVectorT<Double_t> TEveVectorD;
125 
126 //______________________________________________________________________________
127 template<typename TT>
128 inline TT TEveVectorT<TT>::Phi() const
129 {
130  return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
131 }
132 
133 //______________________________________________________________________________
134 template<typename TT>
135 inline TT TEveVectorT<TT>::Theta() const
136 {
137  return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
138 }
139 
140 //______________________________________________________________________________
141 template<typename TT>
142 inline TT TEveVectorT<TT>::CosTheta() const
143 {
144  Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
145 }
146 
147 //______________________________________________________________________________
148 template<typename TT>
149 inline TT TEveVectorT<TT>::Distance(const TEveVectorT& b) const
150 {
151  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
152  (fY - b.fY)*(fY - b.fY) +
153  (fZ - b.fZ)*(fZ - b.fZ));
154 }
155 
156 //______________________________________________________________________________
157 template<typename TT>
158 inline TT TEveVectorT<TT>::SquareDistance(const TEveVectorT& b) const
159 {
160  return ((fX - b.fX) * (fX - b.fX) +
161  (fY - b.fY) * (fY - b.fY) +
162  (fZ - b.fZ) * (fZ - b.fZ));
163 }
164 
165 //______________________________________________________________________________
166 template<typename TT>
167 inline TT TEveVectorT<TT>::Dot(const TEveVectorT& a) const
168 {
169  return a.fX*fX + a.fY*fY + a.fZ*fZ;
170 }
171 
172 //______________________________________________________________________________
173 template<typename TT>
174 inline TEveVectorT<TT> TEveVectorT<TT>::Cross(const TEveVectorT<TT>& a) const
175 {
176  TEveVectorT<TT> r;
177  r.fX = fY * a.fZ - fZ * a.fY;
178  r.fY = fZ * a.fX - fX * a.fZ;
179  r.fZ = fX * a.fY - fY * a.fX;
180  return r;
181 }
182 
183 //______________________________________________________________________________
184 template<typename TT>
185 inline TEveVectorT<TT>& TEveVectorT<TT>::Sub(const TEveVectorT<TT>& a, const TEveVectorT<TT>& b)
186 {
187  fX = a.fX - b.fX;
188  fY = a.fY - b.fY;
189  fZ = a.fZ - b.fZ;
190  return *this;
191 }
192 
193 //______________________________________________________________________________
194 template<typename TT>
195 inline TEveVectorT<TT>& TEveVectorT<TT>::Mult(const TEveVectorT<TT>& a, TT af)
196 {
197  fX = a.fX * af;
198  fY = a.fY * af;
199  fZ = a.fZ * af;
200  return *this;
201 }
202 
203 //______________________________________________________________________________
204 template<typename TT>
205 inline TEveVectorT<TT> operator+(const TEveVectorT<TT>& a, const TEveVectorT<TT>& b)
206 {
207  TEveVectorT<TT> r(a);
208  return r += b;
209 }
210 
211 //______________________________________________________________________________
212 template<typename TT>
213 inline TEveVectorT<TT> operator-(const TEveVectorT<TT>& a, const TEveVectorT<TT>& b)
214 {
215  TEveVectorT<TT> r(a);
216  return r -= b;
217 }
218 
219 //______________________________________________________________________________
220 template<typename TT>
221 inline TEveVectorT<TT> operator*(const TEveVectorT<TT>& a, TT b)
222 {
223  TEveVectorT<TT> r(a);
224  return r *= b;
225 }
226 
227 //______________________________________________________________________________
228 template<typename TT>
229 inline TEveVectorT<TT> operator*(TT b, const TEveVectorT<TT>& a)
230 {
231  TEveVectorT<TT> r(a);
232  return r *= b;
233 }
234 
235 
236 //==============================================================================
237 // TEveVector4T
238 //==============================================================================
239 
240 template <typename TT>
241 class TEveVector4T : public TEveVectorT<TT>
242 {
243  typedef TEveVectorT<TT> TP;
244 
245 public:
246  TT fT;
247 
248  TEveVector4T() : TP(), fT(0) {}
249  template <typename OO>
250  TEveVector4T(const TEveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
251  template <typename OO>
252  TEveVector4T(const TEveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
253  template <typename OO>
254  TEveVector4T(const TEveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
255  TEveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
256  TEveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
257  TEveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
258 
259  void Dump() const;
260 
261  TEveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
262  TEveVector4T& operator+=(const TEveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
263  TEveVector4T& operator-=(const TEveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
264 
265  using TP::operator+=;
266  using TP::operator-=;
267 
268  ClassDefNV(TEveVector4T, 1); // A four-vector template without TObject inheritance and virtual functions.
269 };
270 
271 typedef TEveVector4T<Float_t> TEveVector4;
272 typedef TEveVector4T<Float_t> TEveVector4F;
273 typedef TEveVector4T<Double_t> TEveVector4D;
274 
275 //______________________________________________________________________________
276 template<typename TT>
277 inline TEveVector4T<TT> operator+(const TEveVector4T<TT>& a, const TEveVector4T<TT>& b)
278 {
279  return TEveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
280 }
281 
282 //______________________________________________________________________________
283 template<typename TT>
284 inline TEveVector4T<TT> operator-(const TEveVector4T<TT>& a, const TEveVector4T<TT>& b)
285 {
286  return TEveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
287 }
288 
289 //______________________________________________________________________________
290 template<typename TT>
291 inline TEveVector4T<TT> operator*(const TEveVector4T<TT>& a, TT b)
292 {
293  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
294 }
295 
296 //______________________________________________________________________________
297 template<typename TT>
298 inline TEveVector4T<TT> operator*(TT b, const TEveVector4T<TT>& a)
299 {
300  return TEveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
301 }
302 
303 
304 //==============================================================================
305 // TEveVector2T
306 //==============================================================================
307 
308 template <typename TT>
309 class TEveVector2T
310 {
311 public:
312  TT fX, fY; // Components of the point.
313 
314  TEveVector2T() : fX(0), fY(0) {}
315  template <typename OO>
316  TEveVector2T(const TEveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
317  TEveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
318  TEveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
319  TEveVector2T(TT x, TT y) : fX(x), fY(y) {}
320 
321  void Dump() const;
322 
323  operator const TT*() const { return &fX; }
324  operator TT*() { return &fX; }
325 
326  TEveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
327  TEveVector2T& operator+=(const TEveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
328  TEveVector2T& operator-=(const TEveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
329 
330  TT& operator[](Int_t idx) { return (&fX)[idx]; }
331  TT operator[](Int_t idx) const { return (&fX)[idx]; }
332 
333  const TT* Arr() const { return &fX; }
334  TT* Arr() { return &fX; }
335 
336  void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
337  void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
338  void Set(TT x, TT y) { fX = x; fY = y; }
339 
340  template <typename OO>
341  void Set(const TEveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
342 
343  void NegateXY() { fX = - fX; fY = -fY; }
344  void Normalize(TT length=1);
345 
346  TT Phi() const;
347 
348  TT Mag2() const { return fX*fX + fY*fY;}
349  TT Mag() const { return TMath::Sqrt(Mag2());}
350 
351  TT Distance(const TEveVector2T& v) const;
352  TT SquareDistance(const TEveVector2T& v) const;
353 
354  TT Dot(const TEveVector2T& a) const;
355  TT Cross(const TEveVector2T& a) const;
356 
357  TEveVector2T& Sub(const TEveVector2T& p, const TEveVector2T& q);
358 
359  TEveVector2T& Mult(const TEveVector2T& a, TT af);
360 
361  ClassDefNV(TEveVector2T, 1); // // A two-vector template without TObject inheritance and virtual functions.
362 };
363 
364 typedef TEveVector2T<Float_t> TEveVector2;
365 typedef TEveVector2T<Float_t> TEveVector2F;
366 typedef TEveVector2T<Double_t> TEveVector2D;
367 
368 //______________________________________________________________________________
369 template<typename TT>
370 inline TT TEveVector2T<TT>::Phi() const
371 {
372  return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
373 }
374 
375 //______________________________________________________________________________
376 template<typename TT>
377 inline TT TEveVector2T<TT>::Distance( const TEveVector2T<TT>& b) const
378 {
379  return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
380  (fY - b.fY)*(fY - b.fY));
381 }
382 
383 //______________________________________________________________________________
384 template<typename TT>
385 inline TT TEveVector2T<TT>::SquareDistance(const TEveVector2T<TT>& b) const
386 {
387  return ((fX - b.fX) * (fX - b.fX) +
388  (fY - b.fY) * (fY - b.fY));
389 }
390 
391 //______________________________________________________________________________
392 template<typename TT>
393 inline TT TEveVector2T<TT>::Dot(const TEveVector2T<TT>& a) const
394 {
395  return a.fX*fX + a.fY*fY;
396 }
397 
398 //______________________________________________________________________________
399 template<typename TT>
400 inline TT TEveVector2T<TT>::Cross(const TEveVector2T<TT>& a) const
401 {
402  return fX * a.fY - fY * a.fX;
403 }
404 
405 //______________________________________________________________________________
406 template<typename TT>
407 inline TEveVector2T<TT>& TEveVector2T<TT>::Sub(const TEveVector2T<TT>& p, const TEveVector2T<TT>& q)
408 {
409  fX = p.fX - q.fX;
410  fY = p.fY - q.fY;
411  return *this;
412 }
413 
414 //______________________________________________________________________________
415 template<typename TT>
416 inline TEveVector2T<TT>& TEveVector2T<TT>::Mult(const TEveVector2T<TT>& a, TT af)
417 {
418  fX = a.fX * af;
419  fY = a.fY * af;
420  return *this;
421 }
422 
423 //______________________________________________________________________________
424 template<typename TT>
425 inline TEveVector2T<TT> operator+(const TEveVector2T<TT>& a, const TEveVector2T<TT>& b)
426 {
427  TEveVector2T<TT> r(a);
428  return r += b;
429 }
430 
431 //______________________________________________________________________________
432 template<typename TT>
433 inline TEveVector2T<TT> operator-(const TEveVector2T<TT>& a, const TEveVector2T<TT>& b)
434 {
435  TEveVector2T<TT> r(a);
436  return r -= b;
437 }
438 
439 //______________________________________________________________________________
440 template<typename TT>
441 inline TEveVector2T<TT> operator*(const TEveVector2T<TT>& a, TT b)
442 {
443  TEveVector2T<TT> r(a);
444  return r *= b;
445 }
446 
447 //______________________________________________________________________________
448 template<typename TT>
449 inline TEveVector2T<TT> operator*(TT b, const TEveVector2T<TT>& a)
450 {
451  TEveVector2T<TT> r(a);
452  return r *= b;
453 }
454 
455 #endif