Logo ROOT   6.08/07
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 
18 class TVector2 : public TObject {
19 //------------------------------------------------------------------------------
20 // data members
21 //------------------------------------------------------------------------------
22 protected:
23 
24  Double_t fX; // components of the vector
26 //------------------------------------------------------------------------------
27 // function members
28 //------------------------------------------------------------------------------
29 public:
30 
31  typedef Double_t Scalar; // to be able to use it with the ROOT::Math::VectorUtil functions
32 
33  TVector2 ();
34  TVector2 (Double_t *s);
35  TVector2 (Double_t x0, Double_t y0);
36  virtual ~TVector2();
37  // ****** unary operators
38 
39  TVector2& operator = (TVector2 const & v);
40  TVector2& operator += (TVector2 const & v);
41  TVector2& operator -= (TVector2 const & v);
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)
99  static Double_t Phi_0_2pi(Double_t x);
100 
101  // returns phi angle in the interval [-PI,PI)
102  static Double_t Phi_mpi_pi(Double_t x);
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 
113 inline TVector2& TVector2::operator = (TVector2 const& v) {fX = v.fX; fY = v.fY; return *this;}
114 inline TVector2& TVector2::operator += (TVector2 const& v) {fX += v.fX; fY += v.fY; return *this;}
115 inline TVector2& TVector2::operator -= (TVector2 const& v) {fX -= v.fX; fY -= v.fY; return *this;}
116 
117  // scalar product of 2 2-vectors
118 
119 inline Double_t TVector2::operator *= (const TVector2& v) { return(fX*v.fX+fY*v.fY); }
120 
121 inline TVector2& TVector2::operator *= (Double_t s) { fX *=s; fY *=s; return *this; }
122 inline TVector2& TVector2::operator /= (Double_t s) { fX /=s; fY /=s; return *this; }
123 
124  // ****** binary operators
125 
126 inline TVector2 operator + (const TVector2& v1, const TVector2& v2) {
127  return TVector2(v1.fX+v2.fX,v1.fY+v2.fY);
128 }
129 
130 inline TVector2 operator + (const TVector2& v1, Double_t bias) {
131  return TVector2 (v1.fX+bias,v1.fY+bias);
132 }
133 
134 inline TVector2 operator + (Double_t bias, const TVector2& v1) {
135  return TVector2 (v1.fX+bias,v1.fY+bias);
136 }
137 
138 inline TVector2 operator - (const TVector2& v1, const TVector2& v2) {
139  return TVector2(v1.fX-v2.fX,v1.fY-v2.fY);
140 }
141 
142 inline 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 
154 inline 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 
162 inline Double_t operator ^ (const TVector2& v1, const TVector2& v2) {
163  return v1.fX*v2.fY-v1.fY*v2.fX;
164 }
165 
166 inline Double_t TVector2::DeltaPhi(const TVector2& v) const { return Phi_mpi_pi(Phi()-v.Phi()); }
167 
168 inline TVector2 TVector2::Ort () const { return Unit(); }
169 
170 inline TVector2 TVector2::Proj(const TVector2& v) const { return v*(((*this)*v)/v.Mod2()); }
171 
172 inline TVector2 TVector2::Norm(const TVector2& v) const {return *this-Proj(v); }
173 
174  // ****** setters
175 
176 inline void TVector2::Set(const TVector2& v ) { fX = v.fX; fY = v.fY; }
177 inline void TVector2::Set(Double_t x0, Double_t y0) { fX = x0 ; fY = y0 ; }
178 inline void TVector2::Set(float x0, float y0) { fX = x0 ; fY = y0 ; }
179 inline void TVector2::SetX(Double_t x0) { fX = x0 ; }
180 inline void TVector2::SetY(Double_t y0) { fY = y0 ; }
181 
182 
183 #endif
friend TVector2 operator/(const TVector2 &, Double_t)
Definition: TVector2.h:158
static Double_t Phi_0_2pi(Double_t x)
Returns phi angle in the interval [0,2*PI)
Definition: TVector2.cxx:89
Double_t Py() const
Definition: TVector2.h:71
Double_t fY
Definition: TVector2.h:25
void SetY(Double_t y0)
Definition: TVector2.h:180
const char Option_t
Definition: RtypesCore.h:62
friend TVector2 operator+(const TVector2 &, const TVector2 &)
Definition: TVector2.h:126
Double_t fX
Definition: TVector2.h:24
TVector2 Ort() const
Definition: TVector2.h:168
TVector2 Rotate(Double_t phi) const
Rotation by phi.
Definition: TVector2.cxx:115
TVector2 is a general two vector class, which can be used for the description of different vectors in...
Definition: TVector2.h:18
TVector2 Proj(const TVector2 &v) const
Definition: TVector2.h:170
void Set(const TVector2 &v)
Definition: TVector2.h:176
Double_t x[n]
Definition: legend1.C:17
#define ClassDef(name, id)
Definition: Rtypes.h:254
static Double_t Phi_mpi_pi(Double_t x)
Returns phi angle in the interval [-PI,PI)
Definition: TVector2.cxx:102
friend Double_t operator^(const TVector2 &, const TVector2 &)
Definition: TVector2.h:162
TVector2 Norm(const TVector2 &v) const
Definition: TVector2.h:172
virtual ~TVector2()
Definition: TVector2.cxx:58
TVector2 & operator/=(Double_t s)
Definition: TVector2.h:122
void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TVector2.cxx:153
Double_t operator*=(TVector2 const &v)
Definition: TVector2.h:119
friend Double_t operator*(const TVector2 &, const TVector2 &)
Definition: TVector2.h:154
SVector< double, 2 > v
Definition: Dict.h:5
TVector2()
Constructor.
Definition: TVector2.cxx:32
Double_t X() const
Definition: TVector2.h:72
friend TVector2 operator-(const TVector2 &, const TVector2 &)
Definition: TVector2.h:138
Double_t Phi() const
Return vector phi.
Definition: TVector2.cxx:81
double Double_t
Definition: RtypesCore.h:55
TVector2 & operator-=(TVector2 const &v)
Definition: TVector2.h:115
Double_t Px() const
Definition: TVector2.h:70
Double_t Scalar
Definition: TVector2.h:31
TVector2 & operator=(TVector2 const &v)
Definition: TVector2.h:113
void SetX(Double_t x0)
Definition: TVector2.h:179
Mother of all ROOT objects.
Definition: TObject.h:37
Double_t DeltaPhi(const TVector2 &v) const
Definition: TVector2.h:166
Double_t Y() const
Definition: TVector2.h:73
TVector2 & operator+=(TVector2 const &v)
Definition: TVector2.h:114
TVector2 Unit() const
Return module normalized to 1.
Definition: TVector2.cxx:73
void SetMagPhi(Double_t mag, Double_t phi)
Set vector using mag and phi.
Definition: TVector2.cxx:123
Double_t Mod2() const
Definition: TVector2.h:67
Double_t Mod() const
Return modulo of this vector.
Definition: TVector2.cxx:65