File indexing completed on 2025-01-18 10:10:41
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT7_REveVector
0013 #define ROOT7_REveVector
0014
0015 #include "TMath.h"
0016 #include <cstddef>
0017
0018 class TVector3;
0019
0020 namespace ROOT {
0021 namespace Experimental {
0022
0023
0024
0025
0026
0027
0028 template <typename TT>
0029 class REveVectorT {
0030 public:
0031 TT fX{0}, fY{0}, fZ{0};
0032
0033 REveVectorT() = default;
0034 template <typename OO>
0035 REveVectorT(const REveVectorT<OO>& v) : fX(v.fX), fY(v.fY), fZ(v.fZ) {}
0036 REveVectorT(const Float_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
0037 REveVectorT(const Double_t* v) : fX(v[0]), fY(v[1]), fZ(v[2]) {}
0038 REveVectorT(TT x, TT y, TT z) : fX(x), fY(y), fZ(z) {}
0039
0040 void Dump() const;
0041
0042 #ifdef R__WIN32
0043 const TT *Arr() const
0044 {
0045 if (offsetof(REveVectorT, fZ) != offsetof(REveVectorT, fX) + 2 * sizeof(TT))
0046 Error("REveVectorT", "Subsequent members cannot be accessed as array!");
0047 return &fX;
0048 }
0049 TT *Arr()
0050 {
0051 if (offsetof(REveVectorT, fZ) != offsetof(REveVectorT, fX) + 2 * sizeof(TT))
0052 Error("REveVectorT", "Subsequent members cannot be accessed as array!");
0053 return &fX;
0054 }
0055 #else
0056 const TT *Arr() const
0057 {
0058 static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT),
0059 "Subsequent members cannot be accessed as array!");
0060 return &fX;
0061 }
0062 TT *Arr()
0063 {
0064 static_assert(offsetof(REveVectorT, fZ) == offsetof(REveVectorT, fX) + 2 * sizeof(TT),
0065 "Subsequent members cannot be accessed as array!");
0066 return &fX;
0067 }
0068 #endif
0069
0070 operator const TT*() const { return Arr(); }
0071 operator TT*() { return Arr(); }
0072
0073 TT operator [] (Int_t idx) const { return Arr()[idx]; }
0074 TT& operator [] (Int_t idx) { return Arr()[idx]; }
0075
0076 REveVectorT& operator*=(TT s) { fX *= s; fY *= s; fZ *= s; return *this; }
0077 REveVectorT& operator+=(const REveVectorT& v) { fX += v.fX; fY += v.fY; fZ += v.fZ; return *this; }
0078 REveVectorT& operator-=(const REveVectorT& v) { fX -= v.fX; fY -= v.fY; fZ -= v.fZ; return *this; }
0079
0080 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
0081 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; fZ = v[2]; }
0082 void Set(TT x, TT y, TT z) { fX = x; fY = y; fZ = z; }
0083 void Set(const TVector3& v);
0084
0085 template <typename OO>
0086 void Set(const REveVectorT<OO>& v) { fX = v.fX; fY = v.fY; fZ = v.fZ; }
0087
0088 void NegateXYZ() { fX = - fX; fY = -fY; fZ = -fZ; }
0089 TT Normalize(TT length=1);
0090
0091 TT Phi() const;
0092 TT Theta() const;
0093 TT CosTheta() const;
0094 TT Eta() const;
0095
0096 TT Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
0097 TT Mag() const { return TMath::Sqrt(Mag2()); }
0098
0099 TT Perp2() const { return fX*fX + fY*fY; }
0100 TT Perp() const { return TMath::Sqrt(Perp2()); }
0101 TT R() const { return Perp(); }
0102
0103 TT Distance(const REveVectorT& v) const;
0104 TT SquareDistance(const REveVectorT& v) const;
0105
0106 TT Dot(const REveVectorT& a) const;
0107
0108 REveVectorT Cross(const REveVectorT& a) const;
0109
0110 REveVectorT& Sub(const REveVectorT& a, const REveVectorT& b);
0111 REveVectorT& Mult(const REveVectorT& a, TT af);
0112
0113 REveVectorT Orthogonal() const;
0114 void OrthoNormBase(REveVectorT& a, REveVectorT& b) const;
0115
0116 Bool_t IsZero() const { return fX == 0 && fY == 0 && fZ == 0; }
0117 };
0118
0119 typedef REveVectorT<Float_t> REveVector;
0120 typedef REveVectorT<Float_t> REveVectorF;
0121 typedef REveVectorT<Double_t> REveVectorD;
0122
0123
0124 template<typename TT>
0125 inline TT REveVectorT<TT>::Phi() const
0126 {
0127 return fX == 0 && fY == 0 ? 0 : TMath::ATan2(fY, fX);
0128 }
0129
0130
0131 template<typename TT>
0132 inline TT REveVectorT<TT>::Theta() const
0133 {
0134 return fX == 0 && fY == 0 && fZ == 0 ? 0 : TMath::ATan2(Perp(), fZ);
0135 }
0136
0137
0138 template<typename TT>
0139 inline TT REveVectorT<TT>::CosTheta() const
0140 {
0141 Float_t ptot = Mag(); return ptot == 0 ? 1 : fZ/ptot;
0142 }
0143
0144
0145 template<typename TT>
0146 inline TT REveVectorT<TT>::Distance(const REveVectorT& b) const
0147 {
0148 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
0149 (fY - b.fY)*(fY - b.fY) +
0150 (fZ - b.fZ)*(fZ - b.fZ));
0151 }
0152
0153
0154 template<typename TT>
0155 inline TT REveVectorT<TT>::SquareDistance(const REveVectorT& b) const
0156 {
0157 return ((fX - b.fX) * (fX - b.fX) +
0158 (fY - b.fY) * (fY - b.fY) +
0159 (fZ - b.fZ) * (fZ - b.fZ));
0160 }
0161
0162
0163 template<typename TT>
0164 inline TT REveVectorT<TT>::Dot(const REveVectorT& a) const
0165 {
0166 return a.fX*fX + a.fY*fY + a.fZ*fZ;
0167 }
0168
0169
0170 template<typename TT>
0171 inline REveVectorT<TT> REveVectorT<TT>::Cross(const REveVectorT<TT>& a) const
0172 {
0173 REveVectorT<TT> r;
0174 r.fX = fY * a.fZ - fZ * a.fY;
0175 r.fY = fZ * a.fX - fX * a.fZ;
0176 r.fZ = fX * a.fY - fY * a.fX;
0177 return r;
0178 }
0179
0180
0181 template<typename TT>
0182 inline REveVectorT<TT>& REveVectorT<TT>::Sub(const REveVectorT<TT>& a, const REveVectorT<TT>& b)
0183 {
0184 fX = a.fX - b.fX;
0185 fY = a.fY - b.fY;
0186 fZ = a.fZ - b.fZ;
0187 return *this;
0188 }
0189
0190
0191 template<typename TT>
0192 inline REveVectorT<TT>& REveVectorT<TT>::Mult(const REveVectorT<TT>& a, TT af)
0193 {
0194 fX = a.fX * af;
0195 fY = a.fY * af;
0196 fZ = a.fZ * af;
0197 return *this;
0198 }
0199
0200
0201 template<typename TT>
0202 inline REveVectorT<TT> operator+(const REveVectorT<TT>& a, const REveVectorT<TT>& b)
0203 {
0204 REveVectorT<TT> r(a);
0205 return r += b;
0206 }
0207
0208
0209 template<typename TT>
0210 inline REveVectorT<TT> operator-(const REveVectorT<TT>& a, const REveVectorT<TT>& b)
0211 {
0212 REveVectorT<TT> r(a);
0213 return r -= b;
0214 }
0215
0216
0217 template<typename TT>
0218 inline REveVectorT<TT> operator*(const REveVectorT<TT>& a, TT b)
0219 {
0220 REveVectorT<TT> r(a);
0221 return r *= b;
0222 }
0223
0224
0225 template<typename TT>
0226 inline REveVectorT<TT> operator*(TT b, const REveVectorT<TT>& a)
0227 {
0228 REveVectorT<TT> r(a);
0229 return r *= b;
0230 }
0231
0232
0233
0234
0235
0236
0237 template <typename TT>
0238 class REveVector4T : public REveVectorT<TT>
0239 {
0240 typedef REveVectorT<TT> TP;
0241
0242 public:
0243 TT fT;
0244
0245 REveVector4T() : TP(), fT(0) {}
0246 template <typename OO>
0247 REveVector4T(const REveVectorT<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(0) {}
0248 template <typename OO>
0249 REveVector4T(const REveVectorT<OO>& v, Float_t t) : TP(v.fX, v.fY, v.fZ), fT(t) {}
0250 template <typename OO>
0251 REveVector4T(const REveVector4T<OO>& v) : TP(v.fX, v.fY, v.fZ), fT(v.fT) {}
0252 REveVector4T(const Float_t* v) : TP(v), fT(v[3]) {}
0253 REveVector4T(const Double_t* v) : TP(v), fT(v[3]) {}
0254 REveVector4T(TT x, TT y, TT z, TT t=0) : TP(x, y, z), fT(t) {}
0255
0256 void Dump() const;
0257
0258 REveVector4T& operator*=(TT s) { TP::operator*=(s); fT *= s; return *this; }
0259 REveVector4T& operator+=(const REveVector4T& v) { TP::operator+=(v); fT += v.fT; return *this; }
0260 REveVector4T& operator-=(const REveVector4T& v) { TP::operator-=(v); fT -= v.fT; return *this; }
0261
0262 using TP::operator+=;
0263 using TP::operator-=;
0264 };
0265
0266 typedef REveVector4T<Float_t> REveVector4;
0267 typedef REveVector4T<Float_t> REveVector4F;
0268 typedef REveVector4T<Double_t> REveVector4D;
0269
0270
0271 template<typename TT>
0272 inline REveVector4T<TT> operator+(const REveVector4T<TT>& a, const REveVector4T<TT>& b)
0273 {
0274 return REveVector4T<TT>(a.fX + b.fX, a.fY + b.fY, a.fZ + b.fZ, a.fT + b.fT);
0275 }
0276
0277
0278 template<typename TT>
0279 inline REveVector4T<TT> operator-(const REveVector4T<TT>& a, const REveVector4T<TT>& b)
0280 {
0281 return REveVector4T<TT>(a.fX - b.fX, a.fY - b.fY, a.fZ - b.fZ, a.fT - b.fT);
0282 }
0283
0284
0285 template<typename TT>
0286 inline REveVector4T<TT> operator*(const REveVector4T<TT>& a, TT b)
0287 {
0288 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
0289 }
0290
0291
0292 template<typename TT>
0293 inline REveVector4T<TT> operator*(TT b, const REveVector4T<TT>& a)
0294 {
0295 return REveVector4T<TT>(a.fX*b, a.fY*b, a.fZ*b, a.fT*b);
0296 }
0297
0298
0299
0300
0301
0302
0303 template <typename TT>
0304 class REveVector2T
0305 {
0306 public:
0307 TT fX, fY;
0308
0309 REveVector2T() : fX(0), fY(0) {}
0310 template <typename OO>
0311 REveVector2T(const REveVector2T<OO>& v) : fX(v.fX), fY(v.fY) {}
0312 REveVector2T(const Float_t* v) : fX(v[0]), fY(v[1]) {}
0313 REveVector2T(const Double_t* v) : fX(v[0]), fY(v[1]) {}
0314 REveVector2T(TT x, TT y) : fX(x), fY(y) {}
0315
0316 void Dump() const;
0317
0318 operator const TT*() const { return &fX; }
0319 operator TT*() { return &fX; }
0320
0321 REveVector2T& operator*=(TT s) { fX *= s; fY *= s; return *this; }
0322 REveVector2T& operator+=(const REveVector2T& v) { fX += v.fX; fY += v.fY; return *this; }
0323 REveVector2T& operator-=(const REveVector2T& v) { fX -= v.fX; fY -= v.fY; return *this; }
0324
0325 TT& operator[](Int_t idx) { return (&fX)[idx]; }
0326 TT operator[](Int_t idx) const { return (&fX)[idx]; }
0327
0328 const TT* Arr() const { return &fX; }
0329 TT* Arr() { return &fX; }
0330
0331 void Set(const Float_t* v) { fX = v[0]; fY = v[1]; }
0332 void Set(const Double_t* v) { fX = v[0]; fY = v[1]; }
0333 void Set(TT x, TT y) { fX = x; fY = y; }
0334
0335 template <typename OO>
0336 void Set(const REveVector2T<OO>& v) { fX = v.fX; fY = v.fY; }
0337
0338 void NegateXY() { fX = - fX; fY = -fY; }
0339 void Normalize(TT length=1);
0340
0341 TT Phi() const;
0342
0343 TT Mag2() const { return fX*fX + fY*fY;}
0344 TT Mag() const { return TMath::Sqrt(Mag2());}
0345
0346 TT Distance(const REveVector2T& v) const;
0347 TT SquareDistance(const REveVector2T& v) const;
0348
0349 TT Dot(const REveVector2T& a) const;
0350 TT Cross(const REveVector2T& a) const;
0351
0352 REveVector2T& Sub(const REveVector2T& p, const REveVector2T& q);
0353
0354 REveVector2T& Mult(const REveVector2T& a, TT af);
0355 };
0356
0357 typedef REveVector2T<Float_t> REveVector2;
0358 typedef REveVector2T<Float_t> REveVector2F;
0359 typedef REveVector2T<Double_t> REveVector2D;
0360
0361
0362 template<typename TT>
0363 inline TT REveVector2T<TT>::Phi() const
0364 {
0365 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY, fX);
0366 }
0367
0368
0369 template<typename TT>
0370 inline TT REveVector2T<TT>::Distance( const REveVector2T<TT>& b) const
0371 {
0372 return TMath::Sqrt((fX - b.fX)*(fX - b.fX) +
0373 (fY - b.fY)*(fY - b.fY));
0374 }
0375
0376
0377 template<typename TT>
0378 inline TT REveVector2T<TT>::SquareDistance(const REveVector2T<TT>& b) const
0379 {
0380 return ((fX - b.fX) * (fX - b.fX) +
0381 (fY - b.fY) * (fY - b.fY));
0382 }
0383
0384
0385 template<typename TT>
0386 inline TT REveVector2T<TT>::Dot(const REveVector2T<TT>& a) const
0387 {
0388 return a.fX*fX + a.fY*fY;
0389 }
0390
0391
0392 template<typename TT>
0393 inline TT REveVector2T<TT>::Cross(const REveVector2T<TT>& a) const
0394 {
0395 return fX * a.fY - fY * a.fX;
0396 }
0397
0398
0399 template<typename TT>
0400 inline REveVector2T<TT>& REveVector2T<TT>::Sub(const REveVector2T<TT>& p, const REveVector2T<TT>& q)
0401 {
0402 fX = p.fX - q.fX;
0403 fY = p.fY - q.fY;
0404 return *this;
0405 }
0406
0407
0408 template<typename TT>
0409 inline REveVector2T<TT>& REveVector2T<TT>::Mult(const REveVector2T<TT>& a, TT af)
0410 {
0411 fX = a.fX * af;
0412 fY = a.fY * af;
0413 return *this;
0414 }
0415
0416
0417 template<typename TT>
0418 inline REveVector2T<TT> operator+(const REveVector2T<TT>& a, const REveVector2T<TT>& b)
0419 {
0420 REveVector2T<TT> r(a);
0421 return r += b;
0422 }
0423
0424
0425 template<typename TT>
0426 inline REveVector2T<TT> operator-(const REveVector2T<TT>& a, const REveVector2T<TT>& b)
0427 {
0428 REveVector2T<TT> r(a);
0429 return r -= b;
0430 }
0431
0432
0433 template<typename TT>
0434 inline REveVector2T<TT> operator*(const REveVector2T<TT>& a, TT b)
0435 {
0436 REveVector2T<TT> r(a);
0437 return r *= b;
0438 }
0439
0440
0441 template<typename TT>
0442 inline REveVector2T<TT> operator*(TT b, const REveVector2T<TT>& a)
0443 {
0444 REveVector2T<TT> r(a);
0445 return r *= b;
0446 }
0447
0448 }}
0449
0450 #endif