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 {
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
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/// NewUzVector must be normalized !
304
305void 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
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 {
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
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}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
short Version_t
Definition RtypesCore.h:65
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
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:2481
TVector3 operator-(const TVector3 &a, const TVector3 &b)
Operator -.
Definition TVector3.cxx:431
TVector3 operator+(const TVector3 &a, const TVector3 &b)
Operator +.
Definition TVector3.cxx:424
TVector3 operator*(const TVector3 &p, Double_t a)
Operator *.
Definition TVector3.cxx:438
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=nullptr)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
TMatrixT.
Definition TMatrixT.h:39
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:439
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:888
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:483
The TRotation class describes a rotation of objects of the TVector3 class.
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:388
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:368
Double_t fZ
Definition TVector3.h:185
void Streamer(TBuffer &) override
Stream an object of class TVector3.
Definition TVector3.cxx:399
Double_t fX
Definition TVector3.h:185
void Print(Option_t *option="") const override
Print vector parameters.
Definition TVector3.cxx:466
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:326
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:338
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:346
void RotateUz(const TVector3 &)
NewUzVector must be normalized !
Definition TVector3.cxx:305
Double_t DeltaR(const TVector3 &) const
Return deltaR with respect to v.
Definition TVector3.cxx:378
TClass * IsA() const override
Definition TVector3.h:188
void SetTheta(Double_t)
Set theta keeping mag and phi constant (BaBar).
Definition TVector3.cxx:356
TPaveText * pt
Double_t ACos(Double_t)
Returns the principal value of the arc cosine of x, expressed in radians.
Definition TMath.h:632
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:709
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:640
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:646
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:756
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:594
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:588
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:600
constexpr Double_t RadToDeg()
Conversion from radian to degree: .
Definition TMath.h:72
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
#define Dot(u, v)
Definition normal.c:49
TMarker m
Definition textangle.C:8