Logo ROOT   6.18/05
Reference Guide
TVector2.h
Go to the documentation of this file.
1// @(#)root/physics:$Id$
2// Author: Pasha Murat 12/02/99
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TVector2
13#define ROOT_TVector2
14
15#include "TObject.h"
16
17
18class TVector2 : public TObject {
19//------------------------------------------------------------------------------
20// data members
21//------------------------------------------------------------------------------
22protected:
23
24 Double_t fX; // components of the vector
26//------------------------------------------------------------------------------
27// function members
28//------------------------------------------------------------------------------
29public:
30
31 typedef Double_t Scalar; // to be able to use it with the ROOT::Math::VectorUtil functions
32
33 TVector2 ();
35 TVector2 (Double_t x0, Double_t y0);
36 virtual ~TVector2();
37 // ****** unary operators
38
45
46 // ****** binary operators
47
48 friend TVector2 operator + (const TVector2&, const TVector2&);
49 friend TVector2 operator + (const TVector2&, Double_t );
50 friend TVector2 operator + (Double_t , const TVector2&);
51 friend TVector2 operator - (const TVector2&, const TVector2&);
52 friend TVector2 operator - (const TVector2&, Double_t );
53 friend Double_t operator * (const TVector2&, const TVector2&);
54 friend TVector2 operator * (const TVector2&, Double_t );
55 friend TVector2 operator * (Double_t , const TVector2&);
56 friend TVector2 operator / (const TVector2&, Double_t );
57 friend Double_t operator ^ (const TVector2&, const TVector2&);
58
59 // ****** setters
60 void Set(const TVector2& v);
61 void Set(Double_t x0, Double_t y0);
62 void Set(float x0, float y0);
63 void SetX(Double_t x0);
64 void SetY(Double_t y0);
65 // ****** other member functions
66
67 Double_t Mod2() const { return fX*fX+fY*fY; };
68 Double_t Mod () const;
69
70 Double_t Px() const { return fX; };
71 Double_t Py() const { return fY; };
72 Double_t X () const { return fX; };
73 Double_t Y () const { return fY; };
74
75 // phi() is defined in [0,TWOPI]
76
77 Double_t Phi () const;
78 Double_t DeltaPhi(const TVector2& v) const;
79 void SetMagPhi(Double_t mag, Double_t phi);
80
81 // unit vector in the direction of *this
82
83 TVector2 Unit() const;
84 TVector2 Ort () const;
85
86 // projection of *this to the direction
87 // of TVector2 vector `v'
88
89 TVector2 Proj(const TVector2& v) const;
90
91 // component of *this normal to `v'
92
93 TVector2 Norm(const TVector2& v) const;
94
95 // rotates 2-vector by phi radians
96 TVector2 Rotate (Double_t phi) const;
97
98 // returns phi angle in the interval [0,2*PI)
100
101 // returns phi angle in the interval [-PI,PI)
103
104
105 void Print(Option_t* option="") const;
106
107 ClassDef(TVector2,3) // A 2D physics vector
108
109};
110
111 // ****** unary operators
112
113inline TVector2& TVector2::operator = (TVector2 const& v) {fX = v.fX; fY = v.fY; return *this;}
114inline TVector2& TVector2::operator += (TVector2 const& v) {fX += v.fX; fY += v.fY; return *this;}
115inline TVector2& TVector2::operator -= (TVector2 const& v) {fX -= v.fX; fY -= v.fY; return *this;}
116
117 // scalar product of 2 2-vectors
118
119inline Double_t TVector2::operator *= (const TVector2& v) { return(fX*v.fX+fY*v.fY); }
120
121inline TVector2& TVector2::operator *= (Double_t s) { fX *=s; fY *=s; return *this; }
122inline TVector2& TVector2::operator /= (Double_t s) { fX /=s; fY /=s; return *this; }
123
124 // ****** binary operators
125
126inline TVector2 operator + (const TVector2& v1, const TVector2& v2) {
127 return TVector2(v1.fX+v2.fX,v1.fY+v2.fY);
128}
129
130inline TVector2 operator + (const TVector2& v1, Double_t bias) {
131 return TVector2 (v1.fX+bias,v1.fY+bias);
132}
133
134inline TVector2 operator + (Double_t bias, const TVector2& v1) {
135 return TVector2 (v1.fX+bias,v1.fY+bias);
136}
137
138inline TVector2 operator - (const TVector2& v1, const TVector2& v2) {
139 return TVector2(v1.fX-v2.fX,v1.fY-v2.fY);
140}
141
142inline TVector2 operator - (const TVector2& v1, Double_t bias) {
143 return TVector2 (v1.fX-bias,v1.fY-bias);
144}
145
147 return TVector2 (v.fX*s,v.fY*s);
148}
149
151 return TVector2 (v.fX*s,v.fY*s);
152}
153
154inline Double_t operator * (const TVector2& v1, const TVector2& v2) {
155 return v1.fX*v2.fX+v1.fY*v2.fY;
156}
157
159 return TVector2 (v.fX/s,v.fY/s);
160}
161
162inline Double_t operator ^ (const TVector2& v1, const TVector2& v2) {
163 return v1.fX*v2.fY-v1.fY*v2.fX;
164}
165
166inline Double_t TVector2::DeltaPhi(const TVector2& v) const { return Phi_mpi_pi(Phi()-v.Phi()); }
167
168inline TVector2 TVector2::Ort () const { return Unit(); }
169
170inline TVector2 TVector2::Proj(const TVector2& v) const { return v*(((*this)*v)/v.Mod2()); }
171
172inline TVector2 TVector2::Norm(const TVector2& v) const {return *this-Proj(v); }
173
174 // ****** setters
175
176inline void TVector2::Set(const TVector2& v ) { fX = v.fX; fY = v.fY; }
177inline void TVector2::Set(Double_t x0, Double_t y0) { fX = x0 ; fY = y0 ; }
178inline void TVector2::Set(float x0, float y0) { fX = x0 ; fY = y0 ; }
179inline void TVector2::SetX(Double_t x0) { fX = x0 ; }
180inline void TVector2::SetY(Double_t y0) { fY = y0 ; }
181
182
183#endif
SVector< double, 2 > v
Definition: Dict.h:5
double Double_t
Definition: RtypesCore.h:55
const char Option_t
Definition: RtypesCore.h:62
#define ClassDef(name, id)
Definition: Rtypes.h:326
TVector2 operator*(const TVector2 &v, Double_t s)
Definition: TVector2.h:146
Double_t operator^(const TVector2 &v1, const TVector2 &v2)
Definition: TVector2.h:162
TVector2 operator/(const TVector2 &v, Double_t s)
Definition: TVector2.h:158
TVector2 operator-(const TVector2 &v1, const TVector2 &v2)
Definition: TVector2.h:138
TVector2 operator+(const TVector2 &v1, const TVector2 &v2)
Definition: TVector2.h:126
Mother of all ROOT objects.
Definition: TObject.h:37
TVector2 is a general two vector class, which can be used for the description of different vectors in...
Definition: TVector2.h:18
friend TVector2 operator+(const TVector2 &, const TVector2 &)
Definition: TVector2.h:126
Double_t Phi() const
Return vector phi.
Definition: TVector2.cxx:80
TVector2 & operator=(TVector2 const &v)
Definition: TVector2.h:113
void SetY(Double_t y0)
Definition: TVector2.h:180
Double_t Scalar
Definition: TVector2.h:31
void SetMagPhi(Double_t mag, Double_t phi)
Set vector using mag and phi.
Definition: TVector2.cxx:122
virtual ~TVector2()
Definition: TVector2.cxx:57
Double_t Py() const
Definition: TVector2.h:71
TVector2 & operator-=(TVector2 const &v)
Definition: TVector2.h:115
TVector2 & operator+=(TVector2 const &v)
Definition: TVector2.h:114
friend TVector2 operator/(const TVector2 &, Double_t)
Definition: TVector2.h:158
TVector2 Ort() const
Definition: TVector2.h:168
Double_t Y() const
Definition: TVector2.h:73
Double_t Px() const
Definition: TVector2.h:70
static Double_t Phi_0_2pi(Double_t x)
Returns phi angle in the interval [0,2*PI)
Definition: TVector2.cxx:88
void Set(const TVector2 &v)
Definition: TVector2.h:176
Double_t Mod2() const
Definition: TVector2.h:67
friend TVector2 operator-(const TVector2 &, const TVector2 &)
Definition: TVector2.h:138
TVector2()
Constructor.
Definition: TVector2.cxx:31
friend Double_t operator^(const TVector2 &, const TVector2 &)
Definition: TVector2.h:162
Double_t fX
Definition: TVector2.h:24
Double_t X() const
Definition: TVector2.h:72
Double_t fY
Definition: TVector2.h:25
friend Double_t operator*(const TVector2 &, const TVector2 &)
Definition: TVector2.h:154
TVector2 Proj(const TVector2 &v) const
Definition: TVector2.h:170
TVector2 Norm(const TVector2 &v) const
Definition: TVector2.h:172
static Double_t Phi_mpi_pi(Double_t x)
Returns phi angle in the interval [-PI,PI)
Definition: TVector2.cxx:101
TVector2 & operator/=(Double_t s)
Definition: TVector2.h:122
TVector2 Rotate(Double_t phi) const
Rotation by phi.
Definition: TVector2.cxx:114
Double_t operator*=(TVector2 const &v)
Definition: TVector2.h:119
Double_t DeltaPhi(const TVector2 &v) const
Definition: TVector2.h:166
void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TVector2.cxx:152
void SetX(Double_t x0)
Definition: TVector2.h:179
Double_t Mod() const
Return modulo of this vector.
Definition: TVector2.cxx:64
TVector2 Unit() const
Return module normalized to 1.
Definition: TVector2.cxx:72
Double_t x[n]
Definition: legend1.C:17
static constexpr double s