Logo ROOT  
Reference Guide
TVector3.cxx
Go to the documentation of this file.
1 // @(#)root/physics:$Id$
2 // Author: Pasha Murat, Peter Malzacher 12/02/99
3 // Aug 11 1999: added Pt == 0 guard to Eta()
4 // Oct 8 1999: changed Warning to Error and
5 // return fX in Double_t & operator()
6 // Oct 20 1999: Bug fix: sign in PseudoRapidity
7 // Warning-> Error in Double_t operator()
8 
9 /** \class TVector3
10  \ingroup Physics
11 
12 \attention \parblock
13 TVector3 is a legacy class. It is slower and worse for serialization than the recommended superior alternative ROOT::Math::XYZVector.
14 
15 More details can be found in the documentation of the @ref GenVector package.
16 \endparblock
17 
18 TVector3 is a general three vector class, which can be used for
19 the description of different vectors in 3D.
20 
21 ### Declaration / Access to the components
22 
23 TVector3 has been implemented as a vector of three Double_t
24 variables, representing the cartesian coordinates. By default all components
25 are initialized to zero:
26 
27 ~~~
28  TVector3 v1; // v1 = (0,0,0)
29  TVector3 v3(1,2,3); // v3 = (1,2,3)
30  TVector3 v4(v2); // v4 = v2
31 ~~~
32 
33 It is also possible (but not recommended) to initialize a TVector3
34 with a Double_t or Float_t C array.
35 
36 You can get the basic components either by name or by index using operator():
37 
38 ~~~
39  xx = v1.X(); or xx = v1(0);
40  yy = v1.Y(); yy = v1(1);
41  zz = v1.Z(); zz = v1(2);
42 ~~~
43 
44 The member functions SetX(), SetY(), SetZ() and SetXYZ() allow to set the components:
45 
46 ~~~
47  v1.SetX(1.); v1.SetY(2.); v1.SetZ(3.);
48  v1.SetXYZ(1.,2.,3.);
49 ~~~
50 
51 ### Non-cartesian coordinates
52 
53 To get information on the TVector3 in spherical (rho,phi,theta)
54 or cylindrical (z,r,theta) coordinates, the
55 
56 the member functions Mag() (=magnitude=rho in spherical coordinates),
57 Mag2(), Theta(), CosTheta(), Phi(), Perp() (the transverse component=r in
58 cylindrical coordinates), Perp2() can be used:
59 
60 
61 ~~~
62  Double_t m = v.Mag(); // get magnitude (=rho=Sqrt(x*x+y*y+z*z)))
63  Double_t m2 = v.Mag2(); // get magnitude squared
64  Double_t t = v.Theta(); // get polar angle
65  Double_t ct = v.CosTheta(); // get cos of theta
66  Double_t p = v.Phi(); // get azimuth angle
67  Double_t pp = v.Perp(); // get transverse component
68  Double_t pp2= v.Perp2(); // get transvers component squared
69 ~~~
70 
71 It is also possible to get the transverse component with respect to
72 another vector:
73 
74 ~~~
75  Double_t ppv1 = v.Perp(v1);
76  Double_t pp2v1 = v.Perp2(v1);
77 ~~~
78 
79 The pseudo-rapidity ( eta=-ln (tan (theta/2)) ) can be obtained by Eta()
80 or PseudoRapidity():
81 
82 ~~~
83  Double_t eta = v.PseudoRapidity();
84 ~~~
85 
86 There are set functions to change one of the non-cartesian coordinates:
87 
88 ~~~
89  v.SetTheta(.5); // keeping rho and phi
90  v.SetPhi(.8); // keeping rho and theta
91  v.SetMag(10.); // keeping theta and phi
92  v.SetPerp(3.); // keeping z and phi
93 ~~~
94 
95 ### Arithmetic / Comparison
96 
97 The TVector3 class provides the operators to add, subtract, scale and compare
98 vectors:
99 
100 ~~~
101  v3 = -v1;
102  v1 = v2+v3;
103  v1 += v3;
104  v1 = v1 - v3
105  v1 -= v3;
106  v1 *= 10;
107  v1 = 5*v2;
108  if (v1==v2) {...}
109  if (v1!=v2) {...}
110 ~~~
111 
112 ### Related Vectors
113 
114 ~~~
115  v2 = v1.Unit(); // get unit vector parallel to v1
116  v2 = v1.Orthogonal(); // get vector orthogonal to v1
117 ~~~
118 
119 ### Scalar and vector products
120 
121 ~~~
122  s = v1.Dot(v2); // scalar product
123  s = v1 * v2; // scalar product
124  v = v1.Cross(v2); // vector product
125 ~~~
126 
127 ### Angle between two vectors
128 
129 ~~~
130  Double_t a = v1.Angle(v2);
131 ~~~
132 
133 ### Rotations
134 
135 #### Rotation around axes
136 
137 ~~~
138  v.RotateX(.5);
139  v.RotateY(TMath::Pi());
140  v.RotateZ(angle);
141 ~~~
142 
143 #### Rotation around a vector
144 
145 ~~~
146  v1.Rotate(TMath::Pi()/4, v2); // rotation around v2
147 ~~~
148 
149 #### Rotation by TRotation
150 TVector3 objects can be rotated by objects of the TRotation
151 class using the Transform() member functions,
152 
153 the operator *= or the operator * of the TRotation class:
154 
155 ~~~
156  TRotation m;
157  ...
158  v1.transform(m);
159  v1 = m*v1;
160  v1 *= m; // Attention v1 = m*v1
161 ~~~
162 
163 #### Transformation from rotated frame
164 
165 ~~~
166  TVector3 direction = v.Unit()
167  v1.RotateUz(direction); // direction must be TVector3 of unit length
168 ~~~
169 
170 transforms v1 from the rotated frame (z' parallel to direction, x' in
171 the theta plane and y' in the xy plane as well as perpendicular to the
172 theta plane) to the (x,y,z) frame.
173 */
174 
175 
176 #include "TMatrix.h"
177 #include "TVector3.h"
178 
179 #include "TBuffer.h"
180 #include "TRotation.h"
181 #include "TMath.h"
182 
184 
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Multiplication operator
188 
190  return *this = m * (*this);
191 }
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 /// Transform this vector with a TRotation
195 
197  return *this = m * (*this);
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Return the angle w.r.t. another 3-vector.
202 
204 {
205  Double_t ptot2 = Mag2()*q.Mag2();
206  if(ptot2 <= 0) {
207  return 0.0;
208  } else {
209  Double_t arg = Dot(q)/TMath::Sqrt(ptot2);
210  if(arg > 1.0) arg = 1.0;
211  if(arg < -1.0) arg = -1.0;
212  return TMath::ACos(arg);
213  }
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// Return the transverse component (R in cylindrical coordinate system)
218 
220 {
221  return TMath::Sqrt(Perp2());
222 }
223 
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Return the transverse component (R in cylindrical coordinate system)
227 
229 {
230  return TMath::Sqrt(Perp2(p));
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// Return the azimuth angle. Returns phi from -pi to pi.
235 
237 {
238  return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY,fX);
239 }
240 
241 ////////////////////////////////////////////////////////////////////////////////
242 /// Return the polar angle
243 
245 {
246  return fX == 0.0 && fY == 0.0 && fZ == 0.0 ? 0.0 : TMath::ATan2(Perp(),fZ);
247 }
248 
249 ////////////////////////////////////////////////////////////////////////////////
250 /// Return unit vector parallel to this.
251 
253 {
254  Double_t tot2 = Mag2();
255  Double_t tot = (tot2 > 0) ? 1.0/TMath::Sqrt(tot2) : 1.0;
256  TVector3 p(fX*tot,fY*tot,fZ*tot);
257  return p;
258 }
259 
260 ////////////////////////////////////////////////////////////////////////////////
261 /// Rotate vector around X.
262 
264  Double_t s = TMath::Sin(angle);
265  Double_t c = TMath::Cos(angle);
266  Double_t yy = fY;
267  fY = c*yy - s*fZ;
268  fZ = s*yy + c*fZ;
269 }
270 
271 ////////////////////////////////////////////////////////////////////////////////
272 /// Rotate vector around Y.
273 
275  Double_t s = TMath::Sin(angle);
276  Double_t c = TMath::Cos(angle);
277  Double_t zz = fZ;
278  fZ = c*zz - s*fX;
279  fX = s*zz + c*fX;
280 }
281 
282 ////////////////////////////////////////////////////////////////////////////////
283 /// Rotate vector around Z.
284 
286  Double_t s = TMath::Sin(angle);
287  Double_t c = TMath::Cos(angle);
288  Double_t xx = fX;
289  fX = c*xx - s*fY;
290  fY = s*xx + c*fY;
291 }
292 
293 ////////////////////////////////////////////////////////////////////////////////
294 /// Rotate vector.
295 
296 void TVector3::Rotate(Double_t angle, const TVector3 & axis){
297  TRotation trans;
298  trans.Rotate(angle, axis);
299  operator*=(trans);
300 }
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// NewUzVector must be normalized !
304 
305 void TVector3::RotateUz(const TVector3& NewUzVector) {
306  Double_t u1 = NewUzVector.fX;
307  Double_t u2 = NewUzVector.fY;
308  Double_t u3 = NewUzVector.fZ;
309  Double_t up = u1*u1 + u2*u2;
310 
311  if (up) {
312  up = TMath::Sqrt(up);
313  Double_t px = fX, py = fY, pz = fZ;
314  fX = (u1*u3*px - u2*py + u1*up*pz)/up;
315  fY = (u2*u3*px + u1*py + u2*up*pz)/up;
316  fZ = (u3*u3*px - px + u3*up*pz)/up;
317  } else if (u3 < 0.) { fX = -fX; fZ = -fZ; } // phi=0 teta=pi
318  else {};
319 }
320 
321 ////////////////////////////////////////////////////////////////////////////////
322 /// Double_t m = Mag();
323 /// return 0.5*log( (m+fZ)/(m-fZ) );
324 /// guard against Pt=0
325 
327  double cosTheta = CosTheta();
328  if (cosTheta*cosTheta < 1) return -0.5* TMath::Log( (1.0-cosTheta)/(1.0+cosTheta) );
329  if (fZ == 0) return 0;
330  //Warning("PseudoRapidity","transvers momentum = 0! return +/- 10e10");
331  if (fZ > 0) return 10e10;
332  else return -10e10;
333 }
334 
335 ////////////////////////////////////////////////////////////////////////////////
336 /// Set Pt, Eta and Phi
337 
339  Double_t apt = TMath::Abs(pt);
340  SetXYZ(apt*TMath::Cos(phi), apt*TMath::Sin(phi), apt/TMath::Tan(2.0*TMath::ATan(TMath::Exp(-eta))) );
341 }
342 
343 ////////////////////////////////////////////////////////////////////////////////
344 /// Set Pt, Theta and Phi
345 
347  fX = pt * TMath::Cos(phi);
348  fY = pt * TMath::Sin(phi);
349  Double_t tanTheta = TMath::Tan(theta);
350  fZ = tanTheta ? pt / tanTheta : 0;
351 }
352 
353 ////////////////////////////////////////////////////////////////////////////////
354 /// Set theta keeping mag and phi constant (BaBar).
355 
357 {
358  Double_t ma = Mag();
359  Double_t ph = Phi();
360  SetX(ma*TMath::Sin(th)*TMath::Cos(ph));
361  SetY(ma*TMath::Sin(th)*TMath::Sin(ph));
362  SetZ(ma*TMath::Cos(th));
363 }
364 
365 ////////////////////////////////////////////////////////////////////////////////
366 /// Set phi keeping mag and theta constant (BaBar).
367 
369 {
370  Double_t xy = Perp();
371  SetX(xy*TMath::Cos(ph));
372  SetY(xy*TMath::Sin(ph));
373 }
374 
375 ////////////////////////////////////////////////////////////////////////////////
376 /// Return deltaR with respect to v.
377 
379 {
380  Double_t deta = Eta()-v.Eta();
381  Double_t dphi = TVector2::Phi_mpi_pi(Phi()-v.Phi());
382  return TMath::Sqrt( deta*deta+dphi*dphi );
383 }
384 
385 ////////////////////////////////////////////////////////////////////////////////
386 /// Setter with mag, theta, phi.
387 
389 {
390  Double_t amag = TMath::Abs(mag);
391  fX = amag * TMath::Sin(theta) * TMath::Cos(phi);
392  fY = amag * TMath::Sin(theta) * TMath::Sin(phi);
393  fZ = amag * TMath::Cos(theta);
394 }
395 
396 ////////////////////////////////////////////////////////////////////////////////
397 /// Stream an object of class TVector3.
398 
399 void TVector3::Streamer(TBuffer &R__b)
400 {
401  if (R__b.IsReading()) {
402  UInt_t R__s, R__c;
403  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
404  if (R__v > 2) {
405  R__b.ReadClassBuffer(TVector3::Class(), this, R__v, R__s, R__c);
406  return;
407  }
408  //====process old versions before automatic schema evolution
409  if (R__v < 2) TObject::Streamer(R__b);
410  R__b >> fX;
411  R__b >> fY;
412  R__b >> fZ;
413  R__b.CheckByteCount(R__s, R__c, TVector3::IsA());
414  //====end of old versions
415 
416  } else {
417  R__b.WriteClassBuffer(TVector3::Class(),this);
418  }
419 }
420 
421 ////////////////////////////////////////////////////////////////////////////////
422 /// Operator +
423 
425  return TVector3(a.X() + b.X(), a.Y() + b.Y(), a.Z() + b.Z());
426 }
427 
428 ////////////////////////////////////////////////////////////////////////////////
429 /// Operator -
430 
432  return TVector3(a.X() - b.X(), a.Y() - b.Y(), a.Z() - b.Z());
433 }
434 
435 ////////////////////////////////////////////////////////////////////////////////
436 /// Operator *
437 
439  return TVector3(a*p.X(), a*p.Y(), a*p.Z());
440 }
441 
442 ////////////////////////////////////////////////////////////////////////////////
443 /// Operator *
444 
446  return TVector3(a*p.X(), a*p.Y(), a*p.Z());
447 }
448 
449 ////////////////////////////////////////////////////////////////////////////////
451  return a.Dot(b);
452 }
453 
454 ////////////////////////////////////////////////////////////////////////////////
455 /// Operator *
456 
457 TVector3 operator * (const TMatrix & m, const TVector3 & v ) {
458  return TVector3( m(0,0)*v.X()+m(0,1)*v.Y()+m(0,2)*v.Z(),
459  m(1,0)*v.X()+m(1,1)*v.Y()+m(1,2)*v.Z(),
460  m(2,0)*v.X()+m(2,1)*v.Y()+m(2,2)*v.Z());
461 }
462 
463 ////////////////////////////////////////////////////////////////////////////////
464 /// Print vector parameters.
465 
467 {
468  Printf("%s %s (x,y,z)=(%f,%f,%f) (rho,theta,phi)=(%f,%f,%f)",GetName(),GetTitle(),X(),Y(),Z(),
470 }
c
#define c(i)
Definition: RSha256.hxx:101
TVector3::SetPhi
void SetPhi(Double_t)
Set phi keeping mag and theta constant (BaBar).
Definition: TVector3.cxx:368
m
auto * m
Definition: textangle.C:8
TVector3
Definition: TVector3.h:22
TVector3::CosTheta
Double_t CosTheta() const
Definition: TVector3.h:371
operator*
TVector3 operator*(const TVector3 &p, Double_t a)
Operator *.
Definition: TVector3.cxx:438
TVector3::Transform
TVector3 & Transform(const TRotation &)
Transform this vector with a TRotation.
Definition: TVector3.cxx:196
TMath::ATan2
Double_t ATan2(Double_t y, Double_t x)
Definition: TMath.h:679
Version_t
short Version_t
Definition: RtypesCore.h:65
TVector3::SetXYZ
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition: TVector3.h:227
TMath::RadToDeg
constexpr Double_t RadToDeg()
Conversion from radian to degree:
Definition: TMath.h:73
Option_t
const char Option_t
Definition: RtypesCore.h:66
TVector3::Y
Double_t Y() const
Definition: TVector3.h:217
TRotation::Rotate
TRotation & Rotate(Double_t, const TVector3 &)
Rotate along an axis.
Definition: TRotation.cxx:324
TMath::Cos
Double_t Cos(Double_t)
Definition: TMath.h:643
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TVector3::fY
Double_t fY
Definition: TVector3.h:185
TBuffer::ReadClassBuffer
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
TVector3::Eta
Double_t Eta() const
Definition: TVector3.h:400
TVector3::RotateZ
void RotateZ(Double_t)
Rotate vector around Z.
Definition: TVector3.cxx:285
TMath::Log
Double_t Log(Double_t x)
Definition: TMath.h:760
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
TMath::Tan
Double_t Tan(Double_t)
Definition: TMath.h:647
TMath::Exp
Double_t Exp(Double_t x)
Definition: TMath.h:727
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
TVector3::RotateUz
void RotateUz(const TVector3 &)
NewUzVector must be normalized !
Definition: TVector3.cxx:305
TVector3::RotateX
void RotateX(Double_t)
Rotate vector around X.
Definition: TVector3.cxx:263
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TBuffer
Buffer base class used for serializing objects.
Definition: TBuffer.h:43
TVector3::SetZ
void SetZ(Double_t)
Definition: TVector3.h:225
TObject::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:403
TVector3::PseudoRapidity
Double_t PseudoRapidity() const
Double_t m = Mag(); return 0.5*log( (m+fZ)/(m-fZ) ); guard against Pt=0.
Definition: TVector3.cxx:326
TBuffer::CheckByteCount
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
TVector3::Mag2
Double_t Mag2() const
Definition: TVector3.h:339
TMatrixT
TMatrixT.
Definition: TMatrixT.h:39
v
@ v
Definition: rootcling_impl.cxx:3664
b
#define b(i)
Definition: RSha256.hxx:100
TVector3::Rotate
void Rotate(Double_t, const TVector3 &)
Rotate vector.
Definition: TVector3.cxx:296
TMatrix.h
TVector3.h
q
float * q
Definition: THbookFile.cxx:89
xy
XPoint xy[kMAXMK]
Definition: TGX11.cxx:123
TVector3::Mag
Double_t Mag() const
Definition: TVector3.h:86
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TVector3::fX
Double_t fX
Definition: TVector3.h:185
operator-
TVector3 operator-(const TVector3 &a, const TVector3 &b)
Operator -.
Definition: TVector3.cxx:431
TVector3::Angle
Double_t Angle(const TVector3 &) const
Return the angle w.r.t. another 3-vector.
Definition: TVector3.cxx:203
TVector3::Theta
Double_t Theta() const
Return the polar angle.
Definition: TVector3.cxx:244
TBuffer.h
TVector3::SetPtThetaPhi
void SetPtThetaPhi(Double_t pt, Double_t theta, Double_t phi)
Set Pt, Theta and Phi.
Definition: TVector3.cxx:346
a
auto * a
Definition: textangle.C:12
TVector3::DeltaR
Double_t DeltaR(const TVector3 &) const
Return deltaR with respect to v.
Definition: TVector3.cxx:378
TBuffer::WriteClassBuffer
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
TVector3::SetX
void SetX(Double_t)
Definition: TVector3.h:223
TVector3::fZ
Double_t fZ
Definition: TVector3.h:185
TVector3::Print
void Print(Option_t *option="") const
Print vector parameters.
Definition: TVector3.cxx:466
TVector3::operator*=
TVector3 & operator*=(Double_t)
Definition: TVector3.h:324
TMath::Sin
Double_t Sin(Double_t)
Definition: TMath.h:639
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TBuffer::ReadVersion
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
TRotation
The TRotation class describes a rotation of objects of the TVector3 class.
Definition: TRotation.h:20
TVector3::Perp2
Double_t Perp2() const
Definition: TVector3.h:353
TVector3::Perp
Double_t Perp() const
Return the transverse component (R in cylindrical coordinate system)
Definition: TVector3.cxx:219
Printf
void Printf(const char *fmt,...)
TVector3::RotateY
void RotateY(Double_t)
Rotate vector around Y.
Definition: TVector3.cxx:274
TVector3::Phi
Double_t Phi() const
Return the azimuth angle. Returns phi from -pi to pi.
Definition: TVector3.cxx:236
TBuffer::IsReading
Bool_t IsReading() const
Definition: TBuffer.h:86
TMath::ACos
Double_t ACos(Double_t)
Definition: TMath.h:669
TMath::ATan
Double_t ATan(Double_t)
Definition: TMath.h:675
TVector3::SetPtEtaPhi
void SetPtEtaPhi(Double_t pt, Double_t eta, Double_t phi)
Set Pt, Eta and Phi.
Definition: TVector3.cxx:338
TVector3::Dot
Double_t Dot(const TVector3 &) const
Definition: TVector3.h:331
Double_t
double Double_t
Definition: RtypesCore.h:59
TVector3::Z
Double_t Z() const
Definition: TVector3.h:218
TRotation.h
TVector3::SetMagThetaPhi
void SetMagThetaPhi(Double_t mag, Double_t theta, Double_t phi)
Setter with mag, theta, phi.
Definition: TVector3.cxx:388
TVector3::X
Double_t X() const
Definition: TVector3.h:216
pt
TPaveText * pt
Definition: entrylist_figure1.C:7
operator+
TVector3 operator+(const TVector3 &a, const TVector3 &b)
Operator +.
Definition: TVector3.cxx:424
Class
void Class()
Definition: Class.C:29
TVector3::SetTheta
void SetTheta(Double_t)
Set theta keeping mag and phi constant (BaBar).
Definition: TVector3.cxx:356
TVector2::Phi_mpi_pi
static Double_t Phi_mpi_pi(Double_t x)
Returns phi angle in the interval [-PI,PI)
Definition: TVector2.cxx:103
TVector3::SetY
void SetY(Double_t)
Definition: TVector3.h:224
TMath.h
TVector3::Unit
TVector3 Unit() const
Return unit vector parallel to this.
Definition: TVector3.cxx:252