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
183
184
185////////////////////////////////////////////////////////////////////////////////
186/// Multiplication operator
187
189 return *this = m * (*this);
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// Transform this vector with a TRotation
194
196 return *this = m * (*this);
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// Return the angle w.r.t. another 3-vector.
201
203{
204 Double_t ptot2 = Mag2()*q.Mag2();
205 if(ptot2 <= 0) {
206 return 0.0;
207 } else {
209 if(arg > 1.0) arg = 1.0;
210 if(arg < -1.0) arg = -1.0;
211 return TMath::ACos(arg);
212 }
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Return the transverse component (R in cylindrical coordinate system)
217
219{
220 return TMath::Sqrt(Perp2());
221}
222
223
224////////////////////////////////////////////////////////////////////////////////
225/// Return the transverse component (R in cylindrical coordinate system)
226
228{
229 return TMath::Sqrt(Perp2(p));
230}
231
232////////////////////////////////////////////////////////////////////////////////
233/// Return the azimuth angle. Returns phi from -pi to pi.
234
236{
237 return fX == 0.0 && fY == 0.0 ? 0.0 : TMath::ATan2(fY,fX);
238}
239
240////////////////////////////////////////////////////////////////////////////////
241/// Return the polar angle
242
244{
245 return fX == 0.0 && fY == 0.0 && fZ == 0.0 ? 0.0 : TMath::ATan2(Perp(),fZ);
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Return unit vector parallel to this.
250
252{
253 Double_t tot2 = Mag2();
254 Double_t tot = (tot2 > 0) ? 1.0/TMath::Sqrt(tot2) : 1.0;
256 return p;
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Rotate vector around X.
261
265 Double_t yy = fY;
266 fY = c*yy - s*fZ;
267 fZ = s*yy + c*fZ;
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// Rotate vector around Y.
272
276 Double_t zz = fZ;
277 fZ = c*zz - s*fX;
278 fX = s*zz + c*fX;
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// Rotate vector around Z.
283
287 Double_t xx = fX;
288 fX = c*xx - s*fY;
289 fY = s*xx + c*fY;
290}
291
292////////////////////////////////////////////////////////////////////////////////
293/// Rotate vector.
294
296 TRotation trans;
297 trans.Rotate(angle, axis);
298 operator*=(trans);
299}
300
301////////////////////////////////////////////////////////////////////////////////
302/// \brief Express the coordinates of this vector in a new reference frame S'
303/// whose z' axis will lie in the direction of NewUzVector (expressed in frame S).
304///
305/// \note NewUzVector must be normalized !
306///
307/// \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]
308/// The columns of the applied rotation matrix represent the coordinates of
309/// unit vectors of the new axes in the original coordinate system.
310/// The z' axis i.e. third column is `NewUzVector=(u1,u2,u3)`.
311/// 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)`,
312/// which after normalisation becomes `(-u2/up,u1/up,0)` with `up=sqrt(u1*u1+u2*u2)`.
313/// 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)`.
314/// In the special case that z' is parallel to z, the vector is left untouched;
315/// if it's antiparallel, the sign of the x and z vector coordinates is inverted.
316///
317/// This function originates from CLHEP / Geant4.
318/// \see https://proj-clhep.web.cern.ch/proj-clhep/doc/CLHEP_1_7/UserGuide/VectorDefs/node49.html#eq:rotUz
319///
320/// \note If you want to transform your frame using the "shortest" rotation path and avoid Gimbal-lock artefacts,
321/// use instead TVector3::Rotate(Double_t angle,const TVector3& axis), where `axis=(u2/up, -u1/up, 0) and `angle=-arccos(u3)`, being `axis`
322/// the normalized cross-product of `(u1,u2,u3)` and `(0,0,1)`.
323
328 Double_t up = u1*u1 + u2*u2;
329
330 if (up) {
331 up = TMath::Sqrt(up);
332 Double_t px = fX, py = fY, pz = fZ;
333 fX = (u1*u3*px - u2*py + u1*up*pz)/up;
334 fY = (u2*u3*px + u1*py + u2*up*pz)/up;
335 fZ = (u3*u3*px - px + u3*up*pz)/up;
336 } else if (u3 < 0.) { fX = -fX; fZ = -fZ; }
337 else {};
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Double_t m = Mag();
342/// return 0.5*log( (m+fZ)/(m-fZ) );
343/// guard against Pt=0
344
346 double cosTheta = CosTheta();
347 if (cosTheta*cosTheta < 1) return -0.5* TMath::Log( (1.0-cosTheta)/(1.0+cosTheta) );
348 if (fZ == 0) return 0;
349 //Warning("PseudoRapidity","transvers momentum = 0! return +/- 10e10");
350 if (fZ > 0) return 10e10;
351 else return -10e10;
352}
353
354////////////////////////////////////////////////////////////////////////////////
355/// Set Pt, Eta and Phi
356
361
362////////////////////////////////////////////////////////////////////////////////
363/// Set Pt, Theta and Phi
364
366 fX = pt * TMath::Cos(phi);
367 fY = pt * TMath::Sin(phi);
369 fZ = tanTheta ? pt / tanTheta : 0;
370}
371
372////////////////////////////////////////////////////////////////////////////////
373/// Set theta keeping mag and phi constant (BaBar).
374
376{
377 Double_t ma = Mag();
378 Double_t ph = Phi();
381 SetZ(ma*TMath::Cos(th));
382}
383
384////////////////////////////////////////////////////////////////////////////////
385/// Set phi keeping mag and theta constant (BaBar).
386
393
394////////////////////////////////////////////////////////////////////////////////
395/// Return deltaR with respect to v.
396
398{
399 Double_t deta = Eta()-v.Eta();
401 return TMath::Sqrt( deta*deta+dphi*dphi );
402}
403
404////////////////////////////////////////////////////////////////////////////////
405/// Setter with mag, theta, phi.
406
408{
409 Double_t amag = TMath::Abs(mag);
410 fX = amag * TMath::Sin(theta) * TMath::Cos(phi);
411 fY = amag * TMath::Sin(theta) * TMath::Sin(phi);
412 fZ = amag * TMath::Cos(theta);
413}
414
415////////////////////////////////////////////////////////////////////////////////
416/// Stream an object of class TVector3.
417
419{
420 if (R__b.IsReading()) {
422 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
423 if (R__v > 2) {
424 R__b.ReadClassBuffer(TVector3::Class(), this, R__v, R__s, R__c);
425 return;
426 }
427 //====process old versions before automatic schema evolution
428 if (R__v < 2) TObject::Streamer(R__b);
429 R__b >> fX;
430 R__b >> fY;
431 R__b >> fZ;
432 R__b.CheckByteCount(R__s, R__c, TVector3::IsA());
433 //====end of old versions
434
435 } else {
436 R__b.WriteClassBuffer(TVector3::Class(),this);
437 }
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Operator +
442
444 return TVector3(a.X() + b.X(), a.Y() + b.Y(), a.Z() + b.Z());
445}
446
447////////////////////////////////////////////////////////////////////////////////
448/// Operator -
449
451 return TVector3(a.X() - b.X(), a.Y() - b.Y(), a.Z() - b.Z());
452}
453
454////////////////////////////////////////////////////////////////////////////////
455/// Operator *
456
458 return TVector3(a*p.X(), a*p.Y(), a*p.Z());
459}
460
461////////////////////////////////////////////////////////////////////////////////
462/// Operator *
463
465 return TVector3(a*p.X(), a*p.Y(), a*p.Z());
466}
467
468////////////////////////////////////////////////////////////////////////////////
470 return a.Dot(b);
471}
472
473////////////////////////////////////////////////////////////////////////////////
474/// Operator *
475
477 return TVector3( m(0,0)*v.X()+m(0,1)*v.Y()+m(0,2)*v.Z(),
478 m(1,0)*v.X()+m(1,1)*v.Y()+m(1,2)*v.Z(),
479 m(2,0)*v.X()+m(2,1)*v.Y()+m(2,2)*v.Z());
480}
481
482////////////////////////////////////////////////////////////////////////////////
483/// Print vector parameters.
484
486{
487 Printf("%s %s (x,y,z)=(%f,%f,%f) (rho,theta,phi)=(%f,%f,%f)",GetName(),GetTitle(),X(),Y(),Z(),
489}
#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
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:2509
TVector3 operator-(const TVector3 &a, const TVector3 &b)
Operator -.
Definition TVector3.cxx:450
TVector3 operator+(const TVector3 &a, const TVector3 &b)
Operator +.
Definition TVector3.cxx:443
TVector3 operator*(const TVector3 &p, Double_t a)
Operator *.
Definition TVector3.cxx:457
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:457
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:972
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:501
<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:103
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:407
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:387
Double_t fZ
Definition TVector3.h:185
void Streamer(TBuffer &) override
Stream an object of class TVector3.
Definition TVector3.cxx:418
Double_t fX
Definition TVector3.h:185
void Print(Option_t *option="") const override
Print vector parameters.
Definition TVector3.cxx:485
Double_t Phi() const
Return the azimuth angle. Returns phi from -pi to pi.
Definition TVector3.cxx:235
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:284
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:251
void RotateX(Double_t)
Rotate vector around X.
Definition TVector3.cxx:262
Double_t Angle(const TVector3 &) const
Return the angle w.r.t. another 3-vector.
Definition TVector3.cxx:202
Double_t PseudoRapidity() const
Double_t m = Mag(); return 0.5*log( (m+fZ)/(m-fZ) ); guard against Pt=0.
Definition TVector3.cxx:345
void Rotate(Double_t, const TVector3 &)
Rotate vector.
Definition TVector3.cxx:295
void SetPtEtaPhi(Double_t pt, Double_t eta, Double_t phi)
Set Pt, Eta and Phi.
Definition TVector3.cxx:357
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:243
Double_t fY
Definition TVector3.h:185
Double_t Perp() const
Return the transverse component (R in cylindrical coordinate system)
Definition TVector3.cxx:218
TVector3 & Transform(const TRotation &)
Transform this vector with a TRotation.
Definition TVector3.cxx:195
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:273
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:365
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:324
Double_t DeltaR(const TVector3 &) const
Return deltaR with respect to v.
Definition TVector3.cxx:397
TClass * IsA() const override
Definition TVector3.h:188
void SetTheta(Double_t)
Set theta keeping mag and phi constant (BaBar).
Definition TVector3.cxx:375
TPaveText * pt
Double_t ACos(Double_t)
Returns the principal value of the arc cosine of x, expressed in radians.
Definition TMath.h:643
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:720
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:651
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:657
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:767
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:673
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:605
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:599
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:611
constexpr Double_t RadToDeg()
Conversion from radian to degree: .
Definition TMath.h:75
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