00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ROOT_TVector3
00012 #define ROOT_TVector3
00013
00014 #include "TMath.h"
00015 #include "TError.h"
00016 #include "TVector2.h"
00017 #include "TMatrix.h"
00018
00019 class TRotation;
00020
00021
00022 class TVector3 : public TObject {
00023
00024 public:
00025
00026
00027 TVector3(Double_t x = 0.0, Double_t y = 0.0, Double_t z = 0.0);
00028
00029
00030 TVector3(const Double_t *);
00031 TVector3(const Float_t *);
00032
00033
00034 TVector3(const TVector3 &);
00035
00036
00037 virtual ~TVector3();
00038
00039
00040 Double_t operator () (int) const;
00041 inline Double_t operator [] (int) const;
00042
00043
00044 Double_t & operator () (int);
00045 inline Double_t & operator [] (int);
00046
00047
00048 inline Double_t x() const;
00049 inline Double_t y() const;
00050 inline Double_t z() const;
00051 inline Double_t X() const;
00052 inline Double_t Y() const;
00053 inline Double_t Z() const;
00054 inline Double_t Px() const;
00055 inline Double_t Py() const;
00056 inline Double_t Pz() const;
00057
00058
00059 inline void SetX(Double_t);
00060 inline void SetY(Double_t);
00061 inline void SetZ(Double_t);
00062 inline void SetXYZ(Double_t x, Double_t y, Double_t z);
00063 void SetPtEtaPhi(Double_t pt, Double_t eta, Double_t phi);
00064 void SetPtThetaPhi(Double_t pt, Double_t theta, Double_t phi);
00065
00066 inline void GetXYZ(Double_t *carray) const;
00067 inline void GetXYZ(Float_t *carray) const;
00068
00069
00070
00071 inline Double_t Phi() const;
00072
00073
00074 inline Double_t Theta() const;
00075
00076
00077 inline Double_t CosTheta() const;
00078
00079
00080 inline Double_t Mag2() const;
00081
00082
00083 inline Double_t Mag() const;
00084
00085
00086 inline void SetPhi(Double_t);
00087
00088
00089 inline void SetTheta(Double_t);
00090
00091
00092 inline void SetMag(Double_t);
00093
00094
00095 inline Double_t Perp2() const;
00096
00097
00098 inline Double_t Pt() const;
00099 inline Double_t Perp() const;
00100
00101
00102 inline void SetPerp(Double_t);
00103
00104
00105 inline Double_t Perp2(const TVector3 &) const;
00106
00107
00108 inline Double_t Pt(const TVector3 &) const;
00109 inline Double_t Perp(const TVector3 &) const;
00110
00111
00112 inline Double_t DeltaPhi(const TVector3 &) const;
00113 inline Double_t DeltaR(const TVector3 &) const;
00114 inline Double_t DrEtaPhi(const TVector3 &) const;
00115 inline TVector2 EtaPhiVector() const;
00116 inline void SetMagThetaPhi(Double_t mag, Double_t theta, Double_t phi);
00117
00118 inline TVector3 & operator = (const TVector3 &);
00119
00120
00121 inline Bool_t operator == (const TVector3 &) const;
00122 inline Bool_t operator != (const TVector3 &) const;
00123
00124
00125 inline TVector3 & operator += (const TVector3 &);
00126
00127
00128 inline TVector3 & operator -= (const TVector3 &);
00129
00130
00131 inline TVector3 operator - () const;
00132
00133
00134 inline TVector3 & operator *= (Double_t);
00135
00136
00137 inline TVector3 Unit() const;
00138
00139
00140 inline TVector3 Orthogonal() const;
00141
00142
00143 inline Double_t Dot(const TVector3 &) const;
00144
00145
00146 inline TVector3 Cross(const TVector3 &) const;
00147
00148
00149 inline Double_t Angle(const TVector3 &) const;
00150
00151
00152 Double_t PseudoRapidity() const;
00153
00154
00155 inline Double_t Eta() const;
00156
00157 void RotateX(Double_t);
00158
00159
00160 void RotateY(Double_t);
00161
00162
00163 void RotateZ(Double_t);
00164
00165
00166 void RotateUz(const TVector3&);
00167
00168
00169 void Rotate(Double_t, const TVector3 &);
00170
00171
00172 TVector3 & operator *= (const TRotation &);
00173 TVector3 & Transform(const TRotation &);
00174
00175
00176 inline TVector2 XYvector() const;
00177
00178 void Print(Option_t* option="") const;
00179
00180 private:
00181
00182 Double_t fX, fY, fZ;
00183
00184
00185 ClassDef(TVector3,3)
00186
00187 };
00188
00189
00190 TVector3 operator + (const TVector3 &, const TVector3 &);
00191
00192
00193 TVector3 operator - (const TVector3 &, const TVector3 &);
00194
00195
00196 Double_t operator * (const TVector3 &, const TVector3 &);
00197
00198
00199 TVector3 operator * (const TVector3 &, Double_t a);
00200 TVector3 operator * (Double_t a, const TVector3 &);
00201
00202
00203 TVector3 operator * (const TMatrix &, const TVector3 &);
00204
00205
00206 Double_t & TVector3::operator[] (int i) { return operator()(i); }
00207 Double_t TVector3::operator[] (int i) const { return operator()(i); }
00208
00209 inline Double_t TVector3::x() const { return fX; }
00210 inline Double_t TVector3::y() const { return fY; }
00211 inline Double_t TVector3::z() const { return fZ; }
00212 inline Double_t TVector3::X() const { return fX; }
00213 inline Double_t TVector3::Y() const { return fY; }
00214 inline Double_t TVector3::Z() const { return fZ; }
00215 inline Double_t TVector3::Px() const { return fX; }
00216 inline Double_t TVector3::Py() const { return fY; }
00217 inline Double_t TVector3::Pz() const { return fZ; }
00218
00219 inline void TVector3::SetX(Double_t x) { fX = x; }
00220 inline void TVector3::SetY(Double_t y) { fY = y; }
00221 inline void TVector3::SetZ(Double_t z) { fZ = z; }
00222
00223 inline void TVector3::SetXYZ(Double_t x, Double_t y, Double_t z) {
00224 fX = x;
00225 fY = y;
00226 fZ = z;
00227 }
00228
00229 inline void TVector3::GetXYZ(Double_t *carray) const {
00230 carray[0] = fX;
00231 carray[1] = fY;
00232 carray[2] = fZ;
00233 }
00234
00235 inline void TVector3::GetXYZ(Float_t *carray) const {
00236 carray[0] = fX;
00237 carray[1] = fY;
00238 carray[2] = fZ;
00239 }
00240
00241
00242 inline TVector3 & TVector3::operator = (const TVector3 & p) {
00243 fX = p.fX;
00244 fY = p.fY;
00245 fZ = p.fZ;
00246 return *this;
00247 }
00248
00249 inline Bool_t TVector3::operator == (const TVector3& v) const {
00250 return (v.fX==fX && v.fY==fY && v.fZ==fZ) ? kTRUE : kFALSE;
00251 }
00252
00253 inline Bool_t TVector3::operator != (const TVector3& v) const {
00254 return (v.fX!=fX || v.fY!=fY || v.fZ!=fZ) ? kTRUE : kFALSE;
00255 }
00256
00257 inline TVector3& TVector3::operator += (const TVector3 & p) {
00258 fX += p.fX;
00259 fY += p.fY;
00260 fZ += p.fZ;
00261 return *this;
00262 }
00263
00264 inline TVector3& TVector3::operator -= (const TVector3 & p) {
00265 fX -= p.fX;
00266 fY -= p.fY;
00267 fZ -= p.fZ;
00268 return *this;
00269 }
00270
00271 inline TVector3 TVector3::operator - () const {
00272 return TVector3(-fX, -fY, -fZ);
00273 }
00274
00275 inline TVector3& TVector3::operator *= (Double_t a) {
00276 fX *= a;
00277 fY *= a;
00278 fZ *= a;
00279 return *this;
00280 }
00281
00282 inline Double_t TVector3::Dot(const TVector3 & p) const {
00283 return fX*p.fX + fY*p.fY + fZ*p.fZ;
00284 }
00285
00286 inline TVector3 TVector3::Cross(const TVector3 & p) const {
00287 return TVector3(fY*p.fZ-p.fY*fZ, fZ*p.fX-p.fZ*fX, fX*p.fY-p.fX*fY);
00288 }
00289
00290 inline Double_t TVector3::Mag2() const { return fX*fX + fY*fY + fZ*fZ; }
00291
00292 inline Double_t TVector3::Mag() const { return TMath::Sqrt(Mag2()); }
00293
00294 inline TVector3 TVector3::Unit() const {
00295 Double_t tot = Mag2();
00296 TVector3 p(fX,fY,fZ);
00297 return tot > 0.0 ? p *= (1.0/TMath::Sqrt(tot)) : p;
00298 }
00299
00300 inline TVector3 TVector3::Orthogonal() const {
00301 Double_t x = fX < 0.0 ? -fX : fX;
00302 Double_t y = fY < 0.0 ? -fY : fY;
00303 Double_t z = fZ < 0.0 ? -fZ : fZ;
00304 if (x < y) {
00305 return x < z ? TVector3(0,fZ,-fY) : TVector3(fY,-fX,0);
00306 } else {
00307 return y < z ? TVector3(-fZ,0,fX) : TVector3(fY,-fX,0);
00308 }
00309 }
00310
00311 inline Double_t TVector3::Perp2() const { return fX*fX + fY*fY; }
00312
00313 inline Double_t TVector3::Perp() const { return TMath::Sqrt(Perp2()); }
00314
00315 inline Double_t TVector3::Pt() const { return Perp(); }
00316
00317 inline Double_t TVector3::Perp2(const TVector3 & p) const {
00318 Double_t tot = p.Mag2();
00319 Double_t ss = Dot(p);
00320 Double_t per = Mag2();
00321 if (tot > 0.0) per -= ss*ss/tot;
00322 if (per < 0) per = 0;
00323 return per;
00324 }
00325
00326
00327 inline Double_t TVector3::Perp(const TVector3 & p) const {
00328 return TMath::Sqrt(Perp2(p));
00329 }
00330
00331 inline Double_t TVector3::Pt(const TVector3 & p) const {
00332 return Perp(p);
00333 }
00334
00335
00336 inline Double_t TVector3::Phi() const {
00337 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY,fX);
00338 }
00339
00340 inline Double_t TVector3::Theta() const {
00341 return fX == 0.0 && fY == 0.0 && fZ == 0.0 ? 0.0 : TMath::ATan2(Perp(),fZ);
00342 }
00343
00344 inline Double_t TVector3::CosTheta() const {
00345 Double_t ptot = Mag();
00346 return ptot == 0.0 ? 1.0 : fZ/ptot;
00347 }
00348
00349 inline Double_t TVector3::Angle(const TVector3 & q) const {
00350 Double_t ptot2 = Mag2()*q.Mag2();
00351 if(ptot2 <= 0) {
00352 return 0.0;
00353 } else {
00354 Double_t arg = Dot(q)/TMath::Sqrt(ptot2);
00355 if(arg > 1.0) arg = 1.0;
00356 if(arg < -1.0) arg = -1.0;
00357 return TMath::ACos(arg);
00358 }
00359 }
00360
00361 inline void TVector3::SetMag(Double_t ma) {
00362 Double_t factor = Mag();
00363 if (factor == 0) {
00364 Warning("SetMag","zero vector can't be stretched");
00365 } else {
00366 factor = ma/factor;
00367 SetX(fX*factor);
00368 SetY(fY*factor);
00369 SetZ(fZ*factor);
00370 }
00371 }
00372
00373 inline void TVector3::SetTheta(Double_t th) {
00374 Double_t ma = Mag();
00375 Double_t ph = Phi();
00376 SetX(ma*TMath::Sin(th)*TMath::Cos(ph));
00377 SetY(ma*TMath::Sin(th)*TMath::Sin(ph));
00378 SetZ(ma*TMath::Cos(th));
00379 }
00380
00381 inline void TVector3::SetPhi(Double_t ph) {
00382 Double_t xy = Perp();
00383 SetX(xy*TMath::Cos(ph));
00384 SetY(xy*TMath::Sin(ph));
00385 }
00386
00387 inline void TVector3::SetPerp(Double_t r) {
00388 Double_t p = Perp();
00389 if (p != 0.0) {
00390 fX *= r/p;
00391 fY *= r/p;
00392 }
00393 }
00394
00395 inline Double_t TVector3::DeltaPhi(const TVector3 & v) const {
00396 return TVector2::Phi_mpi_pi(Phi()-v.Phi());
00397 }
00398
00399 inline Double_t TVector3::Eta() const {
00400 return PseudoRapidity();
00401 }
00402
00403 inline Double_t TVector3::DrEtaPhi(const TVector3 & v) const{
00404 return DeltaR(v);
00405 }
00406
00407 inline Double_t TVector3::DeltaR(const TVector3 & v) const {
00408 Double_t deta = Eta()-v.Eta();
00409 Double_t dphi = TVector2::Phi_mpi_pi(Phi()-v.Phi());
00410 return TMath::Sqrt( deta*deta+dphi*dphi );
00411 }
00412
00413 inline void TVector3::SetMagThetaPhi(Double_t mag, Double_t theta, Double_t phi) {
00414 Double_t amag = TMath::Abs(mag);
00415 fX = amag * TMath::Sin(theta) * TMath::Cos(phi);
00416 fY = amag * TMath::Sin(theta) * TMath::Sin(phi);
00417 fZ = amag * TMath::Cos(theta);
00418 }
00419
00420
00421 inline TVector2 TVector3::EtaPhiVector() const {
00422 return TVector2 (Eta(),Phi());
00423 }
00424
00425 inline TVector2 TVector3::XYvector() const {
00426 return TVector2(fX,fY);
00427 }
00428
00429 #endif