Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TVector2.cxx
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/** \class TVector2
13 \ingroup Physics
14
15\attention \parblock
16TVector2 is a legacy class. It is slower and worse for serialization than the recommended superior alternative ROOT::Math::XYVector.
17
18More details can be found in the documentation of the @ref GenVector package.
19\endparblock
20
21TVector2 is a general two vector class, which can be used for
22the description of different vectors in 2D.
23*/
24
25#include "TROOT.h"
26#include "TVector2.h"
27#include "TMath.h"
28#include "TBuffer.h"
29
30
32Double_t const kTWOPI = 2.*kPI;
33
35
36////////////////////////////////////////////////////////////////////////////////
37/// Constructor
38
40{
41 fX = 0.;
42 fY = 0.;
43}
44
45////////////////////////////////////////////////////////////////////////////////
46/// Constructor
47
49{
50 fX = v[0];
51 fY = v[1];
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor
56
58{
59 fX = x0;
60 fY = y0;
61}
62
63////////////////////////////////////////////////////////////////////////////////
64
66{
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Return modulo of this vector
71
73{
74 return TMath::Sqrt(fX*fX+fY*fY);
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Return module normalized to 1
79
81{
82 return (Mod2()) ? *this/Mod() : TVector2();
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Return vector phi
87
89{
90 return TMath::Pi()+TMath::ATan2(-fY,-fX);
91}
92
93////////////////////////////////////////////////////////////////////////////////
94/// Returns phi angle in the interval [0,2*PI)
95
97 if(TMath::IsNaN(x)){
98 gROOT->Error("TVector2::Phi_0_2pi","function called with NaN");
99 return x;
100 }
101 while (x >= kTWOPI) x -= kTWOPI;
102 while (x < 0.) x += kTWOPI;
103 return x;
104}
105
106////////////////////////////////////////////////////////////////////////////////
107/// Returns phi angle in the interval [-PI,PI)
108
110 if(TMath::IsNaN(x)){
111 gROOT->Error("TVector2::Phi_mpi_pi","function called with NaN");
112 return x;
113 }
114 while (x >= kPI) x -= kTWOPI;
115 while (x < -kPI) x += kTWOPI;
116 return x;
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// Rotation by phi
121
123{
124 return TVector2( fX*TMath::Cos(phi)-fY*TMath::Sin(phi), fX*TMath::Sin(phi)+fY*TMath::Cos(phi) );
125}
126
127////////////////////////////////////////////////////////////////////////////////
128/// Set vector using mag and phi
129
131{
132 Double_t amag = TMath::Abs(mag);
133 fX = amag * TMath::Cos(phi);
134 fY = amag * TMath::Sin(phi);
135}
136////////////////////////////////////////////////////////////////////////////////
137/// Stream an object of class TVector2.
138
140{
141 if (R__b.IsReading()) {
142 UInt_t R__s, R__c;
143 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
144 if (R__v > 2) {
145 R__b.ReadClassBuffer(TVector2::Class(), this, R__v, R__s, R__c);
146 return;
147 }
148 //====process old versions before automatic schema evolution
149 if (R__v < 2) TObject::Streamer(R__b);
150 R__b >> fX;
151 R__b >> fY;
152 R__b.CheckByteCount(R__s, R__c, TVector2::IsA());
153 //====end of old versions
154
155 } else {
157 }
158}
159
161{
162 //print vector parameters
163 Printf("%s %s (x,y)=(%f,%f) (rho,phi)=(%f,%f)",GetName(),GetTitle(),X(),Y(),
164 Mod(),Phi()*TMath::RadToDeg());
165}
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:382
constexpr Double_t kPI
Definition TEllipse.cxx:24
#define gROOT
Definition TROOT.h:406
void Printf(const char *fmt,...)
Formats a string in a circular formatting buffer and prints the string.
Definition TString.cxx:2503
Double_t const kPI
Definition TVector2.cxx:31
Double_t const kTWOPI
Definition TVector2.cxx:32
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
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:456
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:906
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:500
void Print(Option_t *option="") const override
This method must be overridden when a class wants to print itself.
Definition TVector2.cxx:160
Double_t Phi() const
Return vector phi.
Definition TVector2.cxx:88
void SetMagPhi(Double_t mag, Double_t phi)
Set vector using mag and phi.
Definition TVector2.cxx:130
Double_t Y() const
Definition TVector2.h:74
static Double_t Phi_0_2pi(Double_t x)
Returns phi angle in the interval [0,2*PI)
Definition TVector2.cxx:96
~TVector2() override
Definition TVector2.cxx:65
Double_t Mod2() const
Definition TVector2.h:68
TVector2()
Constructor.
Definition TVector2.cxx:39
TClass * IsA() const override
Definition TVector2.h:108
Double_t fX
Definition TVector2.h:24
void Streamer(TBuffer &) override
Stream an object of class TVector2.
Definition TVector2.cxx:139
Double_t X() const
Definition TVector2.h:73
Double_t fY
Definition TVector2.h:25
static TClass * Class()
static Double_t Phi_mpi_pi(Double_t x)
Returns phi angle in the interval [-PI,PI)
Definition TVector2.cxx:109
TVector2 Rotate(Double_t phi) const
Rotation by phi.
Definition TVector2.cxx:122
Double_t Mod() const
Return modulo of this vector.
Definition TVector2.cxx:72
TVector2 Unit() const
Return module normalized to 1.
Definition TVector2.cxx:80
Double_t x[n]
Definition legend1.C:17
Bool_t IsNaN(Double_t x)
Definition TMath.h:896
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:650
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:598
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:592
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