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