CMS 3D CMS Logo

AnglesUtil.h
Go to the documentation of this file.
1 #ifndef INC_ANGLESUTIL
2 #define INC_ANGLESUTIL
3 // File: AnglesUtil.hpp
5 //
6 // Purpose: Provide useful functions for calculating angles and eta
7 //
8 // Created: 4-NOV-1998 Serban Protopopescu
9 // History: replaced old KinemUtil
10 // Modified: 23-July-2000 Add rapidity calculation
11 // 14-Aug-2003 converted all functions to double precision
13 // Dependencies (#includes)
14 
15 #include <cmath>
16 #include <cstdlib>
17 
18 
19 namespace kinem{
20  const double PI=2.0*acos(0.);
21  const double TWOPI=2.0*PI;
22  const float ETA_LIMIT=15.0;
23  const float EPSILON=1.E-10;
24 
25  // calculate phi from x, y
26  inline double phi(double x, double y);
27  // calculate phi for a line defined by xy1 and xy2 (xy2-xy1)
28  inline double phi(double xy1[2], double xy2[2]);
29  inline double phi(float xy1[2], float xy2[2]);
30 
31  // calculate theta from x, y, z
32  inline double theta(double x, double y, double z);
33  // calculate theta for a line defined by xyz1 and xyz2 (xyz2-xyz1)
34  inline double theta(double xyz1[3], double xyz2[3]);
35  inline double theta(float xyz1[3], float xyz2[3]);
36  // calculate theta from eta
37  inline double theta(double etap);
38 
39  // calculate eta from x, y, z (return also theta)
40  inline double eta(double x, double y, double z);
41  // calculate eta for a line defined by xyz1 and xyz2 (xyz2-xyz1)
42  inline double eta(double xyz1[3], double xyz2[3]);
43  inline double eta(float xyz1[3], float xyz2[3]);
44  // calculate eta from theta
45  inline double eta(double th);
46 
47  // calculate rapidity from E, pz
48  inline double y(double E, double pz);
49 
50  // calculate phi1-phi2 keeping value between 0 and pi
51  inline double delta_phi(double ph11, double phi2);
52  // calculate phi1-phi2 keeping value between -pi and pi
53  inline double signed_delta_phi(double ph11, double phi2);
54  // calculate eta1 - eta2
55  inline double delta_eta(double eta1, double eta2);
56 
57  // calculate deltaR
58  inline double delta_R(double eta1, double phi1, double eta2, double phi2);
59 
60  // calculate unit vectors given two points
61  inline void uvectors(double u[3], double xyz1[3], double xyz2[3]);
62  inline void uvectors(float u[3], float xyz1[3], float xyz2[3]);
63 
64  inline double tanl_from_theta(double theta);
65  inline double theta_from_tanl(double tanl);
66 }
67 
68 inline
69 double kinem::phi(double x, double y)
70 {
71  double PHI=atan2(y, x);
72  return (PHI>=0)? PHI : kinem::TWOPI+PHI;
73 }
74 
75 inline
76 double kinem::phi(float xy1[2], float xy2[2]){
77  double dxy1[2]={xy1[0],xy1[1]};
78  double dxy2[2]={xy2[0],xy2[1]};
79  return phi(dxy1,dxy2);
80  }
81 
82 inline
83 double kinem::phi(double xy1[2], double xy2[2])
84 {
85  double x=xy2[0]-xy1[0];
86  double y=xy2[1]-xy1[1];
87  return phi(x, y);
88 }
89 
90 inline
91 double kinem::delta_phi(double phi1, double phi2)
92 {
93  double PHI=fabs(phi1-phi2);
94  return (PHI<=PI)? PHI : kinem::TWOPI-PHI;
95 }
96 
97 inline
98 double kinem::delta_eta(double eta1, double eta2)
99 {
100  return eta1 - eta2;
101 }
102 
103 inline
104 double kinem::signed_delta_phi(double phi1, double phi2)
105 {
106  double phia=phi1;
107  if(phi1>PI) phia=phi1-kinem::TWOPI;
108  double phib=phi2;
109  if(phi2>PI) phib=phi2-kinem::TWOPI;
110  double dphi=phia-phib;
111  if(dphi>PI) dphi-=kinem::TWOPI;
112  if(dphi<-PI) dphi+=kinem::TWOPI;
113  return dphi;
114 }
115 
116 inline double kinem::delta_R(double eta1, double phi1, double eta2, double phi2)
117 {
118  double deta = eta1-eta2;
119  double dphi = kinem::delta_phi(phi1,phi2);
120  return sqrt(deta*deta + dphi*dphi);
121 }
122 
123 inline
124 double kinem::theta(double xyz1[3], double xyz2[3])
125 {
126  double x=xyz2[0]-xyz1[0];
127  double y=xyz2[1]-xyz1[1];
128  double z=xyz2[2]-xyz1[2];
129  return theta(x, y, z);
130 }
131 
132 inline
133 double kinem::theta(float xyz1[3], float xyz2[3]){
134  double dxyz1[3]={xyz1[0],xyz1[1],xyz1[2]};
135  double dxyz2[3]={xyz2[0],xyz2[1],xyz2[2]};
136  return theta(dxyz1,dxyz2);
137 }
138 
139 inline
140 double kinem::theta(double x, double y, double z)
141 {
142  return atan2(sqrt(x*x + y*y), z);
143 }
144 
145 inline
146 double kinem::theta(double etap)
147 {
148  return 2.0*atan(exp(-etap));
149 }
150 
151 inline
152 double kinem::eta(double xyz1[3], double xyz2[3])
153 {
154  double x=xyz2[0]-xyz1[0];
155  double y=xyz2[1]-xyz1[1];
156  double z=xyz2[2]-xyz1[2];
157  return eta(x, y, z);
158 }
159 
160 inline
161 double kinem::eta(float xyz1[3], float xyz2[3]){
162  double dxyz1[3]={xyz1[0],xyz1[1],xyz1[2]};
163  double dxyz2[3]={xyz2[0],xyz2[1],xyz2[2]};
164  return eta(dxyz1,dxyz2);
165 }
166 
167 inline
168 double kinem::eta(double x, double y, double z)
169 {
170  return 0.5*log((sqrt(x*x + y*y + z*z) + z + EPSILON) /
171  (sqrt(x*x + y*y + z*z) - z + EPSILON));
172 }
173 
174 inline
175 double kinem::eta(double th)
176 {
177  if(th == 0) return ETA_LIMIT;
178  if(th >= PI-0.0001) return -ETA_LIMIT;
179  return -log(tan(th/2.0));
180 }
181 
182 inline
183 double kinem::y(double E, double pz)
184 {
185  return 0.5 * log ((E+pz+EPSILON)/(E-pz+EPSILON));
186 }
187 
188 inline
189 void kinem::uvectors(double u[3], double xyz1[3], double xyz2[3]){
190  double xdiff=xyz2[0]-xyz1[0];
191  double ydiff=xyz2[1]-xyz1[1];
192  double zdiff=xyz2[2]-xyz1[2];
193  double s=sqrt(xdiff*xdiff+ydiff*ydiff+zdiff*zdiff);
194  if(s > 0) {
195  u[0]=xdiff/s;
196  u[1]=ydiff/s;
197  u[2]=zdiff/s;
198  }else{
199  u[0]=0;
200  u[1]=0;
201  u[2]=0;
202  }
203 }
204 
205 inline
206 void kinem::uvectors(float u[3], float xyz1[3], float xyz2[3]){
207  double du[3];
208  double dxyz1[3]={xyz1[0],xyz1[1],xyz1[2]};
209  double dxyz2[3]={xyz2[0],xyz2[1],xyz2[2]};
210  uvectors(du,dxyz1,dxyz2);
211  u[0]=du[0];
212  u[1]=du[1];
213  u[2]=du[2];
214 }
215 
216 inline
218  return tan(PI/2.0 - theta);
219 }
220 
221 inline
222 double kinem::theta_from_tanl(double tanl){
223  return PI/2.0 - atan(tanl);
224 }
225 
226 #endif // INC_ANGLESUTIL
double theta(double x, double y, double z)
Definition: AnglesUtil.h:140
double delta_eta(double eta1, double eta2)
Definition: AnglesUtil.h:98
double tanl_from_theta(double theta)
Definition: AnglesUtil.h:217
const double PI
Definition: AnglesUtil.h:20
T x() const
Cartesian x coordinate.
double theta_from_tanl(double tanl)
Definition: AnglesUtil.h:222
double signed_delta_phi(double ph11, double phi2)
Definition: AnglesUtil.h:104
T sqrt(T t)
Definition: SSEVec.h:18
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
double delta_R(double eta1, double phi1, double eta2, double phi2)
Definition: AnglesUtil.h:116
const double TWOPI
Definition: AnglesUtil.h:21
double delta_phi(double ph11, double phi2)
Definition: AnglesUtil.h:91
const float ETA_LIMIT
Definition: AnglesUtil.h:22
const float EPSILON
Definition: AnglesUtil.h:23
#define PHI
double phi(double x, double y)
Definition: AnglesUtil.h:69
double y(double E, double pz)
Definition: AnglesUtil.h:183
double eta(double x, double y, double z)
Definition: AnglesUtil.h:168
void uvectors(double u[3], double xyz1[3], double xyz2[3])
Definition: AnglesUtil.h:189