ROOT logo
// @(#)root/physics:$Id: TVector3.h 30815 2009-10-20 13:49:22Z rdm $
// Author: Pasha Murat, Peter Malzacher   12/02/99

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/
#ifndef ROOT_TVector3
#define ROOT_TVector3

#ifndef ROOT_TError
#include "TError.h"
#endif
#ifndef ROOT_TVector2
#include "TVector2.h"
#endif
#ifndef ROOT_TMatrix
#include "TMatrix.h"
#endif

class TRotation;


class TVector3 : public TObject {

public:


   TVector3(Double_t x = 0.0, Double_t y = 0.0, Double_t z = 0.0);
   // The constructor.

   TVector3(const Double_t *);
   TVector3(const Float_t *);
   // Constructors from an array

   TVector3(const TVector3 &);
   // The copy constructor.

   virtual ~TVector3();
   // Destructor

   Double_t operator () (int) const;
   inline Double_t operator [] (int) const;
   // Get components by index (Geant4).

   Double_t & operator () (int);
   inline Double_t & operator [] (int);
   // Set components by index.

   inline Double_t x()  const;
   inline Double_t y()  const;
   inline Double_t z()  const;
   inline Double_t X()  const;
   inline Double_t Y()  const;
   inline Double_t Z()  const;
   inline Double_t Px() const;
   inline Double_t Py() const;
   inline Double_t Pz() const;
   // The components in cartesian coordinate system.

   inline void SetX(Double_t);
   inline void SetY(Double_t);
   inline void SetZ(Double_t);
   inline void SetXYZ(Double_t x, Double_t y, Double_t z);
   void        SetPtEtaPhi(Double_t pt, Double_t eta, Double_t phi);
   void        SetPtThetaPhi(Double_t pt, Double_t theta, Double_t phi);

   inline void GetXYZ(Double_t *carray) const;
   inline void GetXYZ(Float_t *carray) const;
   // Get the components into an array
   // not checked!

   Double_t Phi() const;
   // The azimuth angle. returns phi from -pi to pi

   Double_t Theta() const;
   // The polar angle.

   inline Double_t CosTheta() const;
   // Cosine of the polar angle.

   inline Double_t Mag2() const;
   // The magnitude squared (rho^2 in spherical coordinate system).

   Double_t Mag() const;
   // The magnitude (rho in spherical coordinate system).

   void SetPhi(Double_t);
   // Set phi keeping mag and theta constant (BaBar).

   void SetTheta(Double_t);
   // Set theta keeping mag and phi constant (BaBar).

   inline void SetMag(Double_t);
   // Set magnitude keeping theta and phi constant (BaBar).

   inline Double_t Perp2() const;
   // The transverse component squared (R^2 in cylindrical coordinate system).

   inline Double_t Pt() const;
   Double_t Perp() const;
   // The transverse component (R in cylindrical coordinate system).

   inline void SetPerp(Double_t);
   // Set the transverse component keeping phi and z constant.

   inline Double_t Perp2(const TVector3 &) const;
   // The transverse component w.r.t. given axis squared.

   inline Double_t Pt(const TVector3 &) const;
   Double_t Perp(const TVector3 &) const;
   // The transverse component w.r.t. given axis.

   inline Double_t DeltaPhi(const TVector3 &) const;
   Double_t DeltaR(const TVector3 &) const;
   inline Double_t DrEtaPhi(const TVector3 &) const;
   inline TVector2 EtaPhiVector() const;
   void SetMagThetaPhi(Double_t mag, Double_t theta, Double_t phi);

   inline TVector3 & operator = (const TVector3 &);
   // Assignment.

   inline Bool_t operator == (const TVector3 &) const;
   inline Bool_t operator != (const TVector3 &) const;
   // Comparisons (Geant4).

   inline TVector3 & operator += (const TVector3 &);
   // Addition.

   inline TVector3 & operator -= (const TVector3 &);
   // Subtraction.

   inline TVector3 operator - () const;
   // Unary minus.

   inline TVector3 & operator *= (Double_t);
   // Scaling with real numbers.

   TVector3 Unit() const;
   // Unit vector parallel to this.

   inline TVector3 Orthogonal() const;
   // Vector orthogonal to this (Geant4).

   inline Double_t Dot(const TVector3 &) const;
   // Scalar product.

   inline TVector3 Cross(const TVector3 &) const;
   // Cross product.

   Double_t Angle(const TVector3 &) const;
   // The angle w.r.t. another 3-vector.

   Double_t PseudoRapidity() const;
   // Returns the pseudo-rapidity, i.e. -ln(tan(theta/2))

   inline Double_t Eta() const;

   void RotateX(Double_t);
   // Rotates the Hep3Vector around the x-axis.

   void RotateY(Double_t);
   // Rotates the Hep3Vector around the y-axis.

   void RotateZ(Double_t);
   // Rotates the Hep3Vector around the z-axis.

   void RotateUz(const TVector3&);
   // Rotates reference frame from Uz to newUz (unit vector) (Geant4).

   void Rotate(Double_t, const TVector3 &);
   // Rotates around the axis specified by another Hep3Vector.

   TVector3 & operator *= (const TRotation &);
   TVector3 & Transform(const TRotation &);
   // Transformation with a Rotation matrix.

   inline TVector2 XYvector() const;

   void Print(Option_t* option="") const;

private:

   Double_t fX, fY, fZ;
   // The components.

   ClassDef(TVector3,3) // A 3D physics vector

};


TVector3 operator + (const TVector3 &, const TVector3 &);
// Addition of 3-vectors.

TVector3 operator - (const TVector3 &, const TVector3 &);
// Subtraction of 3-vectors.

Double_t operator * (const TVector3 &, const TVector3 &);
// Scalar product of 3-vectors.

TVector3 operator * (const TVector3 &, Double_t a);
TVector3 operator * (Double_t a, const TVector3 &);
// Scaling of 3-vectors with a real number

TVector3 operator * (const TMatrix &, const TVector3 &);


Double_t & TVector3::operator[] (int i)       { return operator()(i); }
Double_t   TVector3::operator[] (int i) const { return operator()(i); }

inline Double_t TVector3::x()  const { return fX; }
inline Double_t TVector3::y()  const { return fY; }
inline Double_t TVector3::z()  const { return fZ; }
inline Double_t TVector3::X()  const { return fX; }
inline Double_t TVector3::Y()  const { return fY; }
inline Double_t TVector3::Z()  const { return fZ; }
inline Double_t TVector3::Px() const { return fX; }
inline Double_t TVector3::Py() const { return fY; }
inline Double_t TVector3::Pz() const { return fZ; }

inline void TVector3::SetX(Double_t xx) { fX = xx; }
inline void TVector3::SetY(Double_t yy) { fY = yy; }
inline void TVector3::SetZ(Double_t zz) { fZ = zz; }

inline void TVector3::SetXYZ(Double_t xx, Double_t yy, Double_t zz) {
   fX = xx;
   fY = yy;
   fZ = zz;
}

inline void TVector3::GetXYZ(Double_t *carray) const {
   carray[0] = fX;
   carray[1] = fY;
   carray[2] = fZ;
}

inline void TVector3::GetXYZ(Float_t *carray) const {
   carray[0] = fX;
   carray[1] = fY;
   carray[2] = fZ;
}


inline TVector3 & TVector3::operator = (const TVector3 & p) {
   fX = p.fX;
   fY = p.fY;
   fZ = p.fZ;
   return *this;
}

inline Bool_t TVector3::operator == (const TVector3& v) const {
   return (v.fX==fX && v.fY==fY && v.fZ==fZ) ? kTRUE : kFALSE;
}

inline Bool_t TVector3::operator != (const TVector3& v) const {
   return (v.fX!=fX || v.fY!=fY || v.fZ!=fZ) ? kTRUE : kFALSE;
}

inline TVector3& TVector3::operator += (const TVector3 & p) {
   fX += p.fX;
   fY += p.fY;
   fZ += p.fZ;
   return *this;
}

inline TVector3& TVector3::operator -= (const TVector3 & p) {
   fX -= p.fX;
   fY -= p.fY;
   fZ -= p.fZ;
   return *this;
}

inline TVector3 TVector3::operator - () const {
   return TVector3(-fX, -fY, -fZ);
}

inline TVector3& TVector3::operator *= (Double_t a) {
   fX *= a;
   fY *= a;
   fZ *= a;
   return *this;
}

inline Double_t TVector3::Dot(const TVector3 & p) const {
   return fX*p.fX + fY*p.fY + fZ*p.fZ;
}

inline TVector3 TVector3::Cross(const TVector3 & p) const {
   return TVector3(fY*p.fZ-p.fY*fZ, fZ*p.fX-p.fZ*fX, fX*p.fY-p.fX*fY);
}

inline Double_t TVector3::Mag2() const { return fX*fX + fY*fY + fZ*fZ; }


inline TVector3 TVector3::Orthogonal() const {
   Double_t xx = fX < 0.0 ? -fX : fX;
   Double_t yy = fY < 0.0 ? -fY : fY;
   Double_t zz = fZ < 0.0 ? -fZ : fZ;
   if (xx < yy) {
      return xx < zz ? TVector3(0,fZ,-fY) : TVector3(fY,-fX,0);
   } else {
      return yy < zz ? TVector3(-fZ,0,fX) : TVector3(fY,-fX,0);
   }
}

inline Double_t TVector3::Perp2() const { return fX*fX + fY*fY; }


inline Double_t TVector3::Pt() const { return Perp(); }

inline Double_t TVector3::Perp2(const TVector3 & p)  const {
   Double_t tot = p.Mag2();
   Double_t ss  = Dot(p);
   Double_t per = Mag2();
   if (tot > 0.0) per -= ss*ss/tot;
   if (per < 0)   per = 0;
   return per;
}

inline Double_t TVector3::Pt(const TVector3 & p) const {
   return Perp(p);
}

inline Double_t TVector3::CosTheta() const {
   Double_t ptot = Mag();
   return ptot == 0.0 ? 1.0 : fZ/ptot;
}

inline void TVector3::SetMag(Double_t ma) {
   Double_t factor = Mag();
   if (factor == 0) {
      Warning("SetMag","zero vector can't be stretched");
   } else {
      factor = ma/factor;
      SetX(fX*factor);
      SetY(fY*factor);
      SetZ(fZ*factor);
   }
}

inline void TVector3::SetPerp(Double_t r) {
   Double_t p = Perp();
   if (p != 0.0) {
      fX *= r/p;
      fY *= r/p;
   }
}

inline Double_t TVector3::DeltaPhi(const TVector3 & v) const {
   return TVector2::Phi_mpi_pi(Phi()-v.Phi());
}

inline Double_t TVector3::Eta() const {
   return PseudoRapidity();
}

inline Double_t TVector3::DrEtaPhi(const TVector3 & v) const{
   return DeltaR(v);
}


inline TVector2 TVector3::EtaPhiVector() const {
   return TVector2 (Eta(),Phi());
}

inline TVector2 TVector3::XYvector() const {
   return TVector2(fX,fY);
}

#endif
 TVector3.h:1
 TVector3.h:2
 TVector3.h:3
 TVector3.h:4
 TVector3.h:5
 TVector3.h:6
 TVector3.h:7
 TVector3.h:8
 TVector3.h:9
 TVector3.h:10
 TVector3.h:11
 TVector3.h:12
 TVector3.h:13
 TVector3.h:14
 TVector3.h:15
 TVector3.h:16
 TVector3.h:17
 TVector3.h:18
 TVector3.h:19
 TVector3.h:20
 TVector3.h:21
 TVector3.h:22
 TVector3.h:23
 TVector3.h:24
 TVector3.h:25
 TVector3.h:26
 TVector3.h:27
 TVector3.h:28
 TVector3.h:29
 TVector3.h:30
 TVector3.h:31
 TVector3.h:32
 TVector3.h:33
 TVector3.h:34
 TVector3.h:35
 TVector3.h:36
 TVector3.h:37
 TVector3.h:38
 TVector3.h:39
 TVector3.h:40
 TVector3.h:41
 TVector3.h:42
 TVector3.h:43
 TVector3.h:44
 TVector3.h:45
 TVector3.h:46
 TVector3.h:47
 TVector3.h:48
 TVector3.h:49
 TVector3.h:50
 TVector3.h:51
 TVector3.h:52
 TVector3.h:53
 TVector3.h:54
 TVector3.h:55
 TVector3.h:56
 TVector3.h:57
 TVector3.h:58
 TVector3.h:59
 TVector3.h:60
 TVector3.h:61
 TVector3.h:62
 TVector3.h:63
 TVector3.h:64
 TVector3.h:65
 TVector3.h:66
 TVector3.h:67
 TVector3.h:68
 TVector3.h:69
 TVector3.h:70
 TVector3.h:71
 TVector3.h:72
 TVector3.h:73
 TVector3.h:74
 TVector3.h:75
 TVector3.h:76
 TVector3.h:77
 TVector3.h:78
 TVector3.h:79
 TVector3.h:80
 TVector3.h:81
 TVector3.h:82
 TVector3.h:83
 TVector3.h:84
 TVector3.h:85
 TVector3.h:86
 TVector3.h:87
 TVector3.h:88
 TVector3.h:89
 TVector3.h:90
 TVector3.h:91
 TVector3.h:92
 TVector3.h:93
 TVector3.h:94
 TVector3.h:95
 TVector3.h:96
 TVector3.h:97
 TVector3.h:98
 TVector3.h:99
 TVector3.h:100
 TVector3.h:101
 TVector3.h:102
 TVector3.h:103
 TVector3.h:104
 TVector3.h:105
 TVector3.h:106
 TVector3.h:107
 TVector3.h:108
 TVector3.h:109
 TVector3.h:110
 TVector3.h:111
 TVector3.h:112
 TVector3.h:113
 TVector3.h:114
 TVector3.h:115
 TVector3.h:116
 TVector3.h:117
 TVector3.h:118
 TVector3.h:119
 TVector3.h:120
 TVector3.h:121
 TVector3.h:122
 TVector3.h:123
 TVector3.h:124
 TVector3.h:125
 TVector3.h:126
 TVector3.h:127
 TVector3.h:128
 TVector3.h:129
 TVector3.h:130
 TVector3.h:131
 TVector3.h:132
 TVector3.h:133
 TVector3.h:134
 TVector3.h:135
 TVector3.h:136
 TVector3.h:137
 TVector3.h:138
 TVector3.h:139
 TVector3.h:140
 TVector3.h:141
 TVector3.h:142
 TVector3.h:143
 TVector3.h:144
 TVector3.h:145
 TVector3.h:146
 TVector3.h:147
 TVector3.h:148
 TVector3.h:149
 TVector3.h:150
 TVector3.h:151
 TVector3.h:152
 TVector3.h:153
 TVector3.h:154
 TVector3.h:155
 TVector3.h:156
 TVector3.h:157
 TVector3.h:158
 TVector3.h:159
 TVector3.h:160
 TVector3.h:161
 TVector3.h:162
 TVector3.h:163
 TVector3.h:164
 TVector3.h:165
 TVector3.h:166
 TVector3.h:167
 TVector3.h:168
 TVector3.h:169
 TVector3.h:170
 TVector3.h:171
 TVector3.h:172
 TVector3.h:173
 TVector3.h:174
 TVector3.h:175
 TVector3.h:176
 TVector3.h:177
 TVector3.h:178
 TVector3.h:179
 TVector3.h:180
 TVector3.h:181
 TVector3.h:182
 TVector3.h:183
 TVector3.h:184
 TVector3.h:185
 TVector3.h:186
 TVector3.h:187
 TVector3.h:188
 TVector3.h:189
 TVector3.h:190
 TVector3.h:191
 TVector3.h:192
 TVector3.h:193
 TVector3.h:194
 TVector3.h:195
 TVector3.h:196
 TVector3.h:197
 TVector3.h:198
 TVector3.h:199
 TVector3.h:200
 TVector3.h:201
 TVector3.h:202
 TVector3.h:203
 TVector3.h:204
 TVector3.h:205
 TVector3.h:206
 TVector3.h:207
 TVector3.h:208
 TVector3.h:209
 TVector3.h:210
 TVector3.h:211
 TVector3.h:212
 TVector3.h:213
 TVector3.h:214
 TVector3.h:215
 TVector3.h:216
 TVector3.h:217
 TVector3.h:218
 TVector3.h:219
 TVector3.h:220
 TVector3.h:221
 TVector3.h:222
 TVector3.h:223
 TVector3.h:224
 TVector3.h:225
 TVector3.h:226
 TVector3.h:227
 TVector3.h:228
 TVector3.h:229
 TVector3.h:230
 TVector3.h:231
 TVector3.h:232
 TVector3.h:233
 TVector3.h:234
 TVector3.h:235
 TVector3.h:236
 TVector3.h:237
 TVector3.h:238
 TVector3.h:239
 TVector3.h:240
 TVector3.h:241
 TVector3.h:242
 TVector3.h:243
 TVector3.h:244
 TVector3.h:245
 TVector3.h:246
 TVector3.h:247
 TVector3.h:248
 TVector3.h:249
 TVector3.h:250
 TVector3.h:251
 TVector3.h:252
 TVector3.h:253
 TVector3.h:254
 TVector3.h:255
 TVector3.h:256
 TVector3.h:257
 TVector3.h:258
 TVector3.h:259
 TVector3.h:260
 TVector3.h:261
 TVector3.h:262
 TVector3.h:263
 TVector3.h:264
 TVector3.h:265
 TVector3.h:266
 TVector3.h:267
 TVector3.h:268
 TVector3.h:269
 TVector3.h:270
 TVector3.h:271
 TVector3.h:272
 TVector3.h:273
 TVector3.h:274
 TVector3.h:275
 TVector3.h:276
 TVector3.h:277
 TVector3.h:278
 TVector3.h:279
 TVector3.h:280
 TVector3.h:281
 TVector3.h:282
 TVector3.h:283
 TVector3.h:284
 TVector3.h:285
 TVector3.h:286
 TVector3.h:287
 TVector3.h:288
 TVector3.h:289
 TVector3.h:290
 TVector3.h:291
 TVector3.h:292
 TVector3.h:293
 TVector3.h:294
 TVector3.h:295
 TVector3.h:296
 TVector3.h:297
 TVector3.h:298
 TVector3.h:299
 TVector3.h:300
 TVector3.h:301
 TVector3.h:302
 TVector3.h:303
 TVector3.h:304
 TVector3.h:305
 TVector3.h:306
 TVector3.h:307
 TVector3.h:308
 TVector3.h:309
 TVector3.h:310
 TVector3.h:311
 TVector3.h:312
 TVector3.h:313
 TVector3.h:314
 TVector3.h:315
 TVector3.h:316
 TVector3.h:317
 TVector3.h:318
 TVector3.h:319
 TVector3.h:320
 TVector3.h:321
 TVector3.h:322
 TVector3.h:323
 TVector3.h:324
 TVector3.h:325
 TVector3.h:326
 TVector3.h:327
 TVector3.h:328
 TVector3.h:329
 TVector3.h:330
 TVector3.h:331
 TVector3.h:332
 TVector3.h:333
 TVector3.h:334
 TVector3.h:335
 TVector3.h:336
 TVector3.h:337
 TVector3.h:338
 TVector3.h:339
 TVector3.h:340
 TVector3.h:341
 TVector3.h:342
 TVector3.h:343
 TVector3.h:344
 TVector3.h:345
 TVector3.h:346
 TVector3.h:347
 TVector3.h:348
 TVector3.h:349
 TVector3.h:350
 TVector3.h:351
 TVector3.h:352
 TVector3.h:353
 TVector3.h:354
 TVector3.h:355
 TVector3.h:356
 TVector3.h:357
 TVector3.h:358
 TVector3.h:359
 TVector3.h:360
 TVector3.h:361
 TVector3.h:362
 TVector3.h:363
 TVector3.h:364
 TVector3.h:365
 TVector3.h:366
 TVector3.h:367
 TVector3.h:368
 TVector3.h:369
 TVector3.h:370
 TVector3.h:371
 TVector3.h:372
 TVector3.h:373