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 namespace kinem {
19  const double PI = 2.0 * acos(0.);
20  const double TWOPI = 2.0 * PI;
21  const float ETA_LIMIT = 15.0;
22  const float EPSILON = 1.E-10;
23 
24  // calculate phi from x, y
25  inline double phi(double x, double y);
26  // calculate phi for a line defined by xy1 and xy2 (xy2-xy1)
27  inline double phi(double xy1[2], double xy2[2]);
28  inline double phi(float xy1[2], float xy2[2]);
29 
30  // calculate theta from x, y, z
31  inline double theta(double x, double y, double z);
32  // calculate theta for a line defined by xyz1 and xyz2 (xyz2-xyz1)
33  inline double theta(double xyz1[3], double xyz2[3]);
34  inline double theta(float xyz1[3], float xyz2[3]);
35  // calculate theta from eta
36  inline double theta(double etap);
37 
38  // calculate eta from x, y, z (return also theta)
39  inline double eta(double x, double y, double z);
40  // calculate eta for a line defined by xyz1 and xyz2 (xyz2-xyz1)
41  inline double eta(double xyz1[3], double xyz2[3]);
42  inline double eta(float xyz1[3], float xyz2[3]);
43  // calculate eta from theta
44  inline double eta(double th);
45 
46  // calculate rapidity from E, pz
47  inline double y(double E, double pz);
48 
49  // calculate phi1-phi2 keeping value between 0 and pi
50  inline double delta_phi(double ph11, double phi2);
51  // calculate phi1-phi2 keeping value between -pi and pi
52  inline double signed_delta_phi(double ph11, double phi2);
53  // calculate eta1 - eta2
54  inline double delta_eta(double eta1, double eta2);
55 
56  // calculate deltaR
57  inline double delta_R(double eta1, double phi1, double eta2, double phi2);
58 
59  // calculate unit vectors given two points
60  inline void uvectors(double u[3], double xyz1[3], double xyz2[3]);
61  inline void uvectors(float u[3], float xyz1[3], float xyz2[3]);
62 
63  inline double tanl_from_theta(double theta);
64  inline double theta_from_tanl(double tanl);
65 } // namespace kinem
66 
67 inline double kinem::phi(double x, double y) {
68  double PHI = atan2(y, x);
69  return (PHI >= 0) ? PHI : kinem::TWOPI + PHI;
70 }
71 
72 inline double kinem::phi(float xy1[2], float xy2[2]) {
73  double dxy1[2] = {xy1[0], xy1[1]};
74  double dxy2[2] = {xy2[0], xy2[1]};
75  return phi(dxy1, dxy2);
76 }
77 
78 inline double kinem::phi(double xy1[2], double xy2[2]) {
79  double x = xy2[0] - xy1[0];
80  double y = xy2[1] - xy1[1];
81  return phi(x, y);
82 }
83 
84 inline double kinem::delta_phi(double phi1, double phi2) {
85  double PHI = fabs(phi1 - phi2);
86  return (PHI <= PI) ? PHI : kinem::TWOPI - PHI;
87 }
88 
89 inline double kinem::delta_eta(double eta1, double eta2) { return eta1 - eta2; }
90 
91 inline double kinem::signed_delta_phi(double phi1, double phi2) {
92  double phia = phi1;
93  if (phi1 > PI)
94  phia = phi1 - kinem::TWOPI;
95  double phib = phi2;
96  if (phi2 > PI)
97  phib = phi2 - kinem::TWOPI;
98  double dphi = phia - phib;
99  if (dphi > PI)
100  dphi -= kinem::TWOPI;
101  if (dphi < -PI)
102  dphi += kinem::TWOPI;
103  return dphi;
104 }
105 
106 inline double kinem::delta_R(double eta1, double phi1, double eta2, double phi2) {
107  double deta = eta1 - eta2;
108  double dphi = kinem::delta_phi(phi1, phi2);
109  return sqrt(deta * deta + dphi * dphi);
110 }
111 
112 inline double kinem::theta(double xyz1[3], double xyz2[3]) {
113  double x = xyz2[0] - xyz1[0];
114  double y = xyz2[1] - xyz1[1];
115  double z = xyz2[2] - xyz1[2];
116  return theta(x, y, z);
117 }
118 
119 inline double kinem::theta(float xyz1[3], float xyz2[3]) {
120  double dxyz1[3] = {xyz1[0], xyz1[1], xyz1[2]};
121  double dxyz2[3] = {xyz2[0], xyz2[1], xyz2[2]};
122  return theta(dxyz1, dxyz2);
123 }
124 
125 inline double kinem::theta(double x, double y, double z) { return atan2(sqrt(x * x + y * y), z); }
126 
127 inline double kinem::theta(double etap) { return 2.0 * atan(exp(-etap)); }
128 
129 inline double kinem::eta(double xyz1[3], double xyz2[3]) {
130  double x = xyz2[0] - xyz1[0];
131  double y = xyz2[1] - xyz1[1];
132  double z = xyz2[2] - xyz1[2];
133  return eta(x, y, z);
134 }
135 
136 inline double kinem::eta(float xyz1[3], float xyz2[3]) {
137  double dxyz1[3] = {xyz1[0], xyz1[1], xyz1[2]};
138  double dxyz2[3] = {xyz2[0], xyz2[1], xyz2[2]};
139  return eta(dxyz1, dxyz2);
140 }
141 
142 inline double kinem::eta(double x, double y, double z) {
143  return 0.5 * log((sqrt(x * x + y * y + z * z) + z + EPSILON) / (sqrt(x * x + y * y + z * z) - z + EPSILON));
144 }
145 
146 inline double kinem::eta(double th) {
147  if (th == 0)
148  return ETA_LIMIT;
149  if (th >= PI - 0.0001)
150  return -ETA_LIMIT;
151  return -log(tan(th / 2.0));
152 }
153 
154 inline double kinem::y(double E, double pz) { return 0.5 * log((E + pz + EPSILON) / (E - pz + EPSILON)); }
155 
156 inline void kinem::uvectors(double u[3], double xyz1[3], double xyz2[3]) {
157  double xdiff = xyz2[0] - xyz1[0];
158  double ydiff = xyz2[1] - xyz1[1];
159  double zdiff = xyz2[2] - xyz1[2];
160  double s = sqrt(xdiff * xdiff + ydiff * ydiff + zdiff * zdiff);
161  if (s > 0) {
162  u[0] = xdiff / s;
163  u[1] = ydiff / s;
164  u[2] = zdiff / s;
165  } else {
166  u[0] = 0;
167  u[1] = 0;
168  u[2] = 0;
169  }
170 }
171 
172 inline void kinem::uvectors(float u[3], float xyz1[3], float xyz2[3]) {
173  double du[3];
174  double dxyz1[3] = {xyz1[0], xyz1[1], xyz1[2]};
175  double dxyz2[3] = {xyz2[0], xyz2[1], xyz2[2]};
176  uvectors(du, dxyz1, dxyz2);
177  u[0] = du[0];
178  u[1] = du[1];
179  u[2] = du[2];
180 }
181 
182 inline double kinem::tanl_from_theta(double theta) { return tan(PI / 2.0 - theta); }
183 
184 inline double kinem::theta_from_tanl(double tanl) { return PI / 2.0 - atan(tanl); }
185 
186 #endif // INC_ANGLESUTIL
double theta(double x, double y, double z)
Definition: AnglesUtil.h:125
double delta_eta(double eta1, double eta2)
Definition: AnglesUtil.h:89
double tanl_from_theta(double theta)
Definition: AnglesUtil.h:182
double theta(double etap)
Definition: AnglesUtil.h:127
const double PI
Definition: AnglesUtil.h:19
float float float z
double theta_from_tanl(double tanl)
Definition: AnglesUtil.h:184
double signed_delta_phi(double ph11, double phi2)
Definition: AnglesUtil.h:91
double phi(float xy1[2], float xy2[2])
Definition: AnglesUtil.h:72
T sqrt(T t)
Definition: SSEVec.h:23
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:106
const double TWOPI
Definition: AnglesUtil.h:20
double delta_phi(double ph11, double phi2)
Definition: AnglesUtil.h:84
const float ETA_LIMIT
Definition: AnglesUtil.h:21
double eta(double th)
Definition: AnglesUtil.h:146
const float EPSILON
Definition: AnglesUtil.h:22
void uvectors(float u[3], float xyz1[3], float xyz2[3])
Definition: AnglesUtil.h:172
#define PHI
double phi(double x, double y)
Definition: AnglesUtil.h:67
double y(double E, double pz)
Definition: AnglesUtil.h:154
double eta(double x, double y, double z)
Definition: AnglesUtil.h:142
float x
void uvectors(double u[3], double xyz1[3], double xyz2[3])
Definition: AnglesUtil.h:156