Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
13TVector3 is a legacy class. It is slower and worse for serialization than the recommended superior alternative ROOT::Math::XYZVector.
14
15More details can be found in the documentation of the @ref GenVector package.
16\endparblock
17
18TVector3 is a general three vector class, which can be used for
19the description of different vectors in 3D.
20
21### Declaration / Access to the components
22
23TVector3 has been implemented as a vector of three Double_t
24variables, representing the cartesian coordinates. By default all components
25are 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
33It is also possible (but not recommended) to initialize a TVector3
34with a Double_t or Float_t C array.
35
36You 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
44The 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
53To get information on the TVector3 in spherical (rho,phi,theta)
54or cylindrical (z,r,theta) coordinates, the
55
56the member functions Mag() (=magnitude=rho in spherical coordinates),
57Mag2(), Theta(), CosTheta(), Phi(), Perp() (the transverse component=r in
58cylindrical 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
71It is also possible to get the transverse component with respect to
72another vector:
73
74~~~
75 Double_t ppv1 = v.Perp(v1);
76 Double_t pp2v1 = v.Perp2(v1);
77~~~
78
79The pseudo-rapidity ( eta=-ln (tan (theta/2)) ) can be obtained by Eta()
80or PseudoRapidity():
81
82~~~
83 Double_t eta = v.PseudoRapidity();
84~~~
85
86There 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
97The TVector3 class provides the operators to add, subtract, scale and compare
98vectors:
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
150TVector3 objects can be rotated by objects of the TRotation
151class using the Transform() member functions,
152
153the 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
170transforms v1 from the rotated frame (z' parallel to direction, x' in
171the theta plane and y' in the xy plane as well as perpendicular to the
172theta 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 {
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;
257 return p;
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Rotate vector around X.
262
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
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
288 Double_t xx = fX;
289 fX = c*xx - s*fY;
290 fY = s*xx + c*fY;
291}
292
293////////////////////////////////////////////////////////////////////////////////
294/// Rotate vector.
295
297 TRotation trans;
298 trans.Rotate(angle, axis);
299 operator*=(trans);
300}
301
302////////////////////////////////////////////////////////////////////////////////
303/// \brief Express the coordinates of this vector in a new reference frame S'
304/// whose z' axis will lie in the direction of NewUzVector (expressed in frame S).
305///
306/// \note NewUzVector must be normalized !
307///
308/// \f[ R = \left( \begin{array}{ccc} u1*u3/up & -u2/up & u1 \\ u2*u3/up & u1/up & u2 \\ -up & 0 & u3 \end{array} \right) \f]
309/// The columns of the applied rotation matrix represent the coordinates of
310/// unit vectors of the new axes in the original coordinate system.
311/// The z' axis i.e. third column is `NewUzVector=(u1,u2,u3)`.
312/// The y' axis i.e. second column is the cross product of the old and new Z axes: `(0,0,1)x(u1,u2,u3) = (-u2,u1,0)`,
313/// which after normalisation becomes `(-u2/up,u1/up,0)` with `up=sqrt(u1*u1+u2*u2)`.
314/// The x' axis i.e. first column is the cross product of new y' and z' axes: `(-u2/up,u1/up,0)x(u1,u2,u3) = (u1*u3/up,u2*u3/up,-up)`.
315/// In the special case that z' is parallel to z, the vector is left untouched;
316/// if it's antiparallel, the sign of the x and z vector coordinates is inverted.
317///
318/// This function originates from CLHEP / Geant4.
319/// \see https://proj-clhep.web.cern.ch/proj-clhep/doc/CLHEP_1_7/UserGuide/VectorDefs/node49.html#eq:rotUz
320///
321/// \note If you want to transform your frame using the "shortest" rotation path and avoid Gimbal-lock artefacts,
322/// use instead TVector3::Rotate(Double_t angle,const TVector3& axis), where `axis=(u2/up, -u1/up, 0) and `angle=-arccos(u3)`, being `axis`
323/// the normalized cross-product of `(u1,u2,u3)` and `(0,0,1)`.
324
329 Double_t up = u1*u1 + u2*u2;
330
331 if (up) {
332 up = TMath::Sqrt(up);
333 Double_t px = fX, py = fY, pz = fZ;
334 fX = (u1*u3*px - u2*py + u1*up*pz)/up;
335 fY = (u2*u3*px + u1*py + u2*up*pz)/up;
336 fZ = (u3*u3*px - px + u3*up*pz)/up;
337 } else if (u3 < 0.) { fX = -fX; fZ = -fZ; }
338 else {};
339}
340
341////////////////////////////////////////////////////////////////////////////////
342/// Double_t m = Mag();
343/// return 0.5*log( (m+fZ)/(m-fZ) );
344/// guard against Pt=0
345
347 double cosTheta = CosTheta();
348 if (cosTheta*cosTheta < 1) return -0.5* TMath::Log( (1.0-cosTheta)/(1.0+cosTheta) );
349 if (fZ == 0) return 0;
350 //Warning("PseudoRapidity","transvers momentum = 0! return +/- 10e10");
351 if (fZ > 0) return 10e10;
352 else return -10e10;
353}
354
355////////////////////////////////////////////////////////////////////////////////
356/// Set Pt, Eta and Phi
357
362
363////////////////////////////////////////////////////////////////////////////////
364/// Set Pt, Theta and Phi
365
367 fX = pt * TMath::Cos(phi);
368 fY = pt * TMath::Sin(phi);
370 fZ = tanTheta ? pt / tanTheta : 0;
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// Set theta keeping mag and phi constant (BaBar).
375
377{
378 Double_t ma = Mag();
379 Double_t ph = Phi();
382 SetZ(ma*TMath::Cos(th));
383}
384
385////////////////////////////////////////////////////////////////////////////////
386/// Set phi keeping mag and theta constant (BaBar).
387
394
395////////////////////////////////////////////////////////////////////////////////
396/// Return deltaR with respect to v.
397
399{
400 Double_t deta = Eta()-v.Eta();
402 return TMath::Sqrt( deta*deta+dphi*dphi );
403}
404
405////////////////////////////////////////////////////////////////////////////////
406/// Setter with mag, theta, phi.
407
409{
410 Double_t amag = TMath::Abs(mag);
411 fX = amag * TMath::Sin(theta) * TMath::Cos(phi);
412 fY = amag * TMath::Sin(theta) * TMath::Sin(phi);
413 fZ = amag * TMath::Cos(theta);
414}
415
416////////////////////////////////////////////////////////////////////////////////
417/// Stream an object of class TVector3.
418
420{
421 if (R__b.IsReading()) {
423 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
424 if (R__v > 2) {
425 R__b.ReadClassBuffer(TVector3::Class(), this, R__v, R__s, R__c);
426 return;
427 }
428 //====process old versions before automatic schema evolution
429 if (R__v < 2) TObject::Streamer(R__b);
430 R__b >> fX;
431 R__b >> fY;
432 R__b >> fZ;
433 R__b.CheckByteCount(R__s, R__c, TVector3::IsA());
434 //====end of old versions
435
436 } else {
437 R__b.WriteClassBuffer(TVector3::Class(),this);
438 }
439}
440
441////////////////////////////////////////////////////////////////////////////////
442/// Operator +
443
445 return TVector3(a.X() + b.X(), a.Y() + b.Y(), a.Z() + b.Z());
446}
447
448////////////////////////////////////////////////////////////////////////////////
449/// Operator -
450
452 return TVector3(a.X() - b.X(), a.Y() - b.Y(), a.Z() - b.Z());
453}
454
455////////////////////////////////////////////////////////////////////////////////
456/// Operator *
457
459 return TVector3(a*p.X(), a*p.Y(), a*p.Z());
460}
461
462////////////////////////////////////////////////////////////////////////////////
463/// Operator *
464
466 return TVector3(a*p.X(), a*p.Y(), a*p.Z());
467}
468
469////////////////////////////////////////////////////////////////////////////////
471 return a.Dot(b);
472}
473
474////////////////////////////////////////////////////////////////////////////////
475/// Operator *
476
478 return TVector3( m(0,0)*v.X()+m(0,1)*v.Y()+m(0,2)*v.Z(),
479 m(1,0)*v.X()+m(1,1)*v.Y()+m(1,2)*v.Z(),
480 m(2,0)*v.X()+m(2,1)*v.Y()+m(2,2)*v.Z());
481}
482
483////////////////////////////////////////////////////////////////////////////////
484/// Print vector parameters.
485
487{
488 Printf("%s %s (x,y,z)=(%f,%f,%f) (rho,theta,phi)=(%f,%f,%f)",GetName(),GetTitle(),X(),Y(),Z(),
490}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
#define ClassImp(name)
Definition Rtypes.h:376
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint angle
Option_t Option_t TPoint xy
float * q
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2510
TVector3 operator-(const TVector3 &a, const TVector3 &b)
Operator -.
Definition TVector3.cxx:451
TVector3 operator+(const TVector3 &a, const TVector3 &b)
Operator +.
Definition TVector3.cxx:444
TVector3 operator*(const TVector3 &p, Double_t a)
Operator *.
Definition TVector3.cxx:458
Buffer base class used for serializing objects.
Definition TBuffer.h:43
TMatrixT.
Definition TMatrixT.h:40
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:458
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:973
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:502
<div class="legacybox"><h2>Legacy Code</h2> TRotation is a legacy interface: there will be no bug fix...
Definition TRotation.h:20
TRotation & Rotate(Double_t, const TVector3 &)
Rotate along an axis.
static Double_t Phi_mpi_pi(Double_t x)
Returns phi angle in the interval [-PI,PI)
Definition TVector2.cxx:104
Double_t Z() const
Definition TVector3.h:218
void SetMagThetaPhi(Double_t mag, Double_t theta, Double_t phi)
Setter with mag, theta, phi.
Definition TVector3.cxx:408
void SetY(Double_t)
Definition TVector3.h:224
void SetXYZ(Double_t x, Double_t y, Double_t z)
Definition TVector3.h:227
Double_t Eta() const
Definition TVector3.h:400
void SetPhi(Double_t)
Set phi keeping mag and theta constant (BaBar).
Definition TVector3.cxx:388
Double_t fZ
Definition TVector3.h:185
void Streamer(TBuffer &) override
Stream an object of class TVector3.
Definition TVector3.cxx:419
Double_t fX
Definition TVector3.h:185
void Print(Option_t *option="") const override
Print vector parameters.
Definition TVector3.cxx:486
Double_t Phi() const
Return the azimuth angle. Returns phi from -pi to pi.
Definition TVector3.cxx:236
Double_t Y() const
Definition TVector3.h:217
Double_t Dot(const TVector3 &) const
Definition TVector3.h:331
void RotateZ(Double_t)
Rotate vector around Z.
Definition TVector3.cxx:285
Double_t Mag2() const
Definition TVector3.h:339
Double_t X() const
Definition TVector3.h:216
TVector3 Unit() const
Return unit vector parallel to this.
Definition TVector3.cxx:252
void RotateX(Double_t)
Rotate vector around X.
Definition TVector3.cxx:263
Double_t Angle(const TVector3 &) const
Return the angle w.r.t. another 3-vector.
Definition TVector3.cxx:203
Double_t PseudoRapidity() const
Double_t m = Mag(); return 0.5*log( (m+fZ)/(m-fZ) ); guard against Pt=0.
Definition TVector3.cxx:346
void Rotate(Double_t, const TVector3 &)
Rotate vector.
Definition TVector3.cxx:296
void SetPtEtaPhi(Double_t pt, Double_t eta, Double_t phi)
Set Pt, Eta and Phi.
Definition TVector3.cxx:358
TVector3 & operator*=(Double_t)
Definition TVector3.h:324
Double_t CosTheta() const
Definition TVector3.h:371
static TClass * Class()
Double_t Mag() const
Definition TVector3.h:86
Double_t Theta() const
Return the polar angle.
Definition TVector3.cxx:244
Double_t fY
Definition TVector3.h:185
Double_t Perp() const
Return the transverse component (R in cylindrical coordinate system)
Definition TVector3.cxx:219
TVector3 & Transform(const TRotation &)
Transform this vector with a TRotation.
Definition TVector3.cxx:196
void SetX(Double_t)
Definition TVector3.h:223
Double_t Perp2() const
Definition TVector3.h:353
void RotateY(Double_t)
Rotate vector around Y.
Definition TVector3.cxx:274
void SetZ(Double_t)
Definition TVector3.h:225
void SetPtThetaPhi(Double_t pt, Double_t theta, Double_t phi)
Set Pt, Theta and Phi.
Definition TVector3.cxx:366
void RotateUz(const TVector3 &)
Express the coordinates of this vector in a new reference frame S' whose z' axis will lie in the dire...
Definition TVector3.cxx:325
Double_t DeltaR(const TVector3 &) const
Return deltaR with respect to v.
Definition TVector3.cxx:398
TClass * IsA() const override
Definition TVector3.h:188
void SetTheta(Double_t)
Set theta keeping mag and phi constant (BaBar).
Definition TVector3.cxx:376
TPaveText * pt
Double_t ACos(Double_t)
Returns the principal value of the arc cosine of x, expressed in radians.
Definition TMath.h:638
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Definition TMath.h:715
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:646
Double_t ATan2(Double_t y, Double_t x)
Returns the principal value of the arc tangent of y/x, expressed in radians.
Definition TMath.h:652
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:762
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:668
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:600
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:594
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:606
constexpr Double_t RadToDeg()
Conversion from radian to degree: .
Definition TMath.h:73
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:124
TMarker m
Definition textangle.C:8