Belle II Software development
Lpar.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <iostream>
10#include <cmath>
11#include "CLHEP/Vector/Sqr.h"
12#include "trg/cdc/Lpar.h"
13
14namespace Belle2 {
20//
21// constants, enums and typedefs
22//
23
24//
25// static data member definitions
26//
27
28 const double TRGCDCLpar::BELLE_ALPHA(222.37606);
29
30//
31// constructors and destructor
32//
33// TRGCDCLpar::TRGCDCLpar(double x1, double y1, double x2, double y2, double x3, double y3) {
34// circle(x1, y1, x2, y2, x3, y3);
35// }
37 {
38 m_cu = l.kappa();
39 if (l.alpha() != 0 && l.beta() != 0)
40 m_fi = atan2(l.alpha(), -l.beta());
41 else m_fi = 0;
42 if (m_fi < 0) m_fi += 2 * M_PI;
43 m_da = 2 * l.gamma() / (1 + sqrt(1 + 4 * l.kappa() * l.gamma()));
44 m_cfi = cos(m_fi);
45 m_sfi = sin(m_fi);
46 }
47
48// TRGCDCLpar::TRGCDCLpar( const TRGCDCLpar& )
49// {
50// }
51
53 {
54 }
55
56//
57// assignment operators
58//
59// const TRGCDCLpar& TRGCDCLpar::operator=( const TRGCDCLpar& )
60// {
61// }
62
63//
64// comparison operators
65//
66// bool TRGCDCLpar::operator==( const TRGCDCLpar& ) const
67// {
68// }
69
70// bool TRGCDCLpar::operator!=( const TRGCDCLpar& ) const
71// {
72// }
73
74//
75// member functions
76//
77 void TRGCDCLpar::circle(double x1, double y1, double x2, double y2,
78 double x3, double y3)
79 {
80 double delta = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
81 if (delta == 0) {
82 //
83 // three points are on a line.
84 //
85 m_kappa = 0;
86 double r12sq = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
87 if (r12sq > 0) {
88 double r12 = sqrt(r12sq);
89 m_beta = -(x1 - x2) / r12;
90 m_alpha = (y1 - y2) / r12;
91 m_gamma = - (m_alpha * x1 + m_beta * y1);
92 } else {
93 double r13sq = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
94 if (r13sq > 0) {
95 double r13 = sqrt(r13sq);
96 m_beta = -(x1 - x3) / r13;
97 m_alpha = (y1 - y3) / r13;
98 m_gamma = - (m_alpha * x3 + m_beta * y3);
99 } else {
100 double r23sq = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
101 if (r23sq > 0) {
102 double r23 = sqrt(r23sq);
103 m_beta = -(x2 - x3) / r23;
104 m_alpha = (y2 - y3) / r23;
105 m_gamma = - (m_alpha * x3 + m_beta * y3);
106 } else {
107 m_alpha = 1;
108 m_beta = 0;
109 m_gamma = 0;
110 }
111 }
112 }
113 } else {
114 double r1sq = x1 * x1 + y1 * y1;
115 double r2sq = x2 * x2 + y2 * y2;
116 double r3sq = x3 * x3 + y3 * y3;
117 double a = 0.5 * ((y1 - y3) * (r1sq - r2sq) - (y1 - y2) * (r1sq - r3sq)) / delta;
118 double b = 0.5 * (- (x1 - x3) * (r1sq - r2sq) + (x1 - x2) * (r1sq - r3sq)) / delta;
119 double csq = (x1 - a) * (x1 - a) + (y1 - b) * (y1 - b);
120 double c = sqrt(csq);
121//cnv double csq2 = (x2-a)*(x2-a) + (y2-b)*(y2-b);
122//cnv double csq3 = (x3-a)*(x3-a) + (y3-b)*(y3-b);
123 m_kappa = 1 / (2 * c);
124 m_alpha = - 2 * a * m_kappa;
125 m_beta = - 2 * b * m_kappa;
126 m_gamma = (a * a + b * b - c * c) * m_kappa;
127 }
128 }
129
130 CLHEP::HepMatrix TRGCDCLpar::dldc() const
131#ifdef BELLE_OPTIMIZED_RETURN
132 return vret(3, 4);
133 {
134#else
135 {
136 CLHEP::HepMatrix vret(3, 4);
137#endif
138 Cpar cp(*this);
139 double xi = cp.xi();
140 double s = cp.sfi();
141 double c = cp.cfi();
142 vret(1, 1) = 2 * cp.da() * s;
143 vret(1, 2) = -2 * cp.da() * c;
144 vret(1, 3) = cp.da() * cp.da();
145 vret(1, 4) = 1;
146 vret(2, 1) = xi * c;
147 vret(2, 2) = xi * s;
148 vret(2, 3) = 0;
149 vret(2, 4) = 0;
150 vret(3, 1) = 2 * cp.cu() * s;
151 vret(3, 2) = -2 * cp.cu() * c;
152 vret(3, 3) = xi;
153 vret(3, 4) = 0;
154 return vret;
155 }
156
157 bool TRGCDCLpar::xy(double r, double& x, double& y, int dir) const
158 {
159 double t_kr2g = kr2g(r);
160 double t_xi2 = xi2();
161 double ro = r * r * t_xi2 - t_kr2g * t_kr2g;
162 if (ro < 0) return false;
163 double rs = sqrt(ro);
164 if (dir == 0) {
165 x = (- m_alpha * t_kr2g - m_beta * rs) / t_xi2;
166 y = (- m_beta * t_kr2g + m_alpha * rs) / t_xi2;
167 } else {
168 x = (- m_alpha * t_kr2g + m_beta * rs) / t_xi2;
169 y = (- m_beta * t_kr2g - m_alpha * rs) / t_xi2;
170 }
171 return true;
172 }
173
174 double TRGCDCLpar::x(double r) const
175 {
176 double t_x, t_y;
177 xy(r, t_x, t_y);
178 return t_x;
179 }
180
181 double TRGCDCLpar::y(double r) const
182 {
183 double t_x, t_y;
184 xy(r, t_x, t_y);
185 return t_y;
186 }
187
188 double TRGCDCLpar::phi(double r, int dir) const
189 {
190 double x, y;
191 if (!xy(r, x, y, dir)) return -1;
192 double p = atan2(y, x);
193 if (p < 0) p += (2 * M_PI);
194 return p;
195 }
196
197 void TRGCDCLpar::xhyh(double x, double y, double& xh, double& yh) const
198 {
199 double ddm = dr(x, y);
200 if (ddm == 0) {
201 xh = x;
202 yh = y;
203 return;
204 }
205 double kdp1 = 1 + 2 * kappa() * ddm;
206 xh = x - ddm * (2 * kappa() * x + alpha()) / kdp1;
207 yh = y - ddm * (2 * kappa() * y + beta()) / kdp1;
208 }
209
210 double TRGCDCLpar::s(double x, double y) const
211 {
212 double xh, yh, xx, yy;
213 xhyh(x, y, xh, yh);
214 double fk = fabs(kappa());
215 if (fk == 0) return 0;
216 yy = 2 * fk * (alpha() * yh - beta() * xh);
217 xx = 2 * kappa() * (alpha() * xh + beta() * yh) + xi2();
218 double sp = atan2(yy, xx);
219 if (sp < 0) sp += (2 * M_PI);
220 return sp / 2 / fk;
221 }
222
223 double TRGCDCLpar::s(double r, int dir) const
224 {
225 double d0 = da();
226 if (fabs(r) < fabs(d0)) return -1;
227 double b = fabs(kappa()) * sqrt((r * r - d0 * d0) / (1 + 2 * kappa() * d0));
228 if (fabs(b) > 1) return -1;
229 if (dir == 0)return asin(b) / fabs(kappa());
230 return (M_PI - asin(b)) / fabs(kappa());
231 }
232
233 CLHEP::HepVector TRGCDCLpar::center() const
234#ifdef BELLE_OPTIMIZED_RETURN
235 return v(3);
236 {
237#else
238 {
239 CLHEP::HepVector v(3);
240#endif
241 v(1) = xc();
242 v(2) = yc();
243 v(3) = 0;
244 return (v);
245 }
246
248 // cppcheck-suppress constParameter
249 int intersect(const TRGCDCLpar& lp1, const TRGCDCLpar& lp2, CLHEP::HepVector& v1, CLHEP::HepVector& v2)
250 {
251 CLHEP::HepVector cen1(lp1.center());
252 CLHEP::HepVector cen2(lp2.center());
253 double dx = cen1(1) - cen2(1);
254 double dy = cen1(2) - cen2(2);
255 double dc = sqrt(dx * dx + dy * dy);
256 if (dc < fabs(0.5 / lp1.kappa()) + fabs(0.5 / lp2.kappa())) {
257 double a1 = sqr(lp1.alpha()) + sqr(lp1.beta());
258 double a2 = sqr(lp2.alpha()) + sqr(lp2.beta());
259 double a3 = lp1.alpha() * lp2.alpha() + lp1.beta() * lp2.beta();
260 double det = lp1.alpha() * lp2.beta() - lp1.beta() * lp2.alpha();
261 if (fabs(det) > 1e-12) {
262 double c1 = a2 * sqr(lp1.kappa()) + a1 * sqr(lp2.kappa()) -
263 2.0 * a3 * lp1.kappa() * lp2.kappa();
264 if (c1 != 0) {
265 double cinv = 1.0 / c1;
266 double c2 = sqr(a3) - 0.5 * (a1 + a2) - 2.0 * a3 *
267 (lp1.gamma() * lp2.kappa() + lp2.gamma() * lp1.kappa());
268 double c3 = a2 * sqr(lp1.gamma()) + a1 * sqr(lp2.gamma()) -
269 2.0 * a3 * lp1.gamma() * lp2.gamma();
270 double root = sqr(c2) - 4.0 * c1 * c3;
271 if (root >= 0) {
272 root = sqrt(root);
273 double rad2[2];
274 rad2[0] = 0.5 * cinv * (-c2 - root);
275 rad2[1] = 0.5 * cinv * (-c2 + root);
276 double ab1 = -(lp2.beta() * lp1.gamma() - lp1.beta() * lp2.gamma());
277 double ab2 = (lp2.alpha() * lp1.gamma() - lp1.alpha() * lp2.gamma());
278 double ac1 = -(lp2.beta() * lp1.kappa() - lp1.beta() * lp2.kappa());
279 double ac2 = (lp2.alpha() * lp1.kappa() - lp1.alpha() * lp2.kappa());
280 double dinv = 1.0 / det;
281 v1(1) = dinv * (ab1 + ac1 * rad2[0]);
282 v1(2) = dinv * (ab2 + ac2 * rad2[0]);
283 v1(3) = 0;
284 v2(1) = dinv * (ab1 + ac1 * rad2[1]);
285 v2(2) = dinv * (ab2 + ac2 * rad2[1]);
286 v2(3) = 0;
287//cnv double d1 = lp1.d(v1(1),v1(2));
288//cnv double d2 = lp2.d(v1(1),v1(2));
289//cnv double d3 = lp1.d(v2(1),v2(2));
290//cnv double d4 = lp2.d(v2(1),v2(2));
291//cnv double r = sqrt(rad2[0]);
292 TRGCDCLpar::Cpar cp1(lp1);
293 TRGCDCLpar::Cpar cp2(lp2);
294// for(int j=0;j<2;j++) {
295//jb double s1,s2;
296//jb if(j==0) {
297//jb s1 = lp1.s(v1(1),v1(2));
298//jb s2 = lp2.s(v1(1),v1(2));
299//jb } else {
300//jb s1 = lp1.s(v2(1),v2(2));
301//jb s2 = lp2.s(v2(1),v2(2));
302//jb }
303//cnv double phi1 = cp1.fi() + 2 * cp1.cu() * s1;
304//cnv double phi2 = cp2.fi() + 2 * cp2.cu() * s2;
305// double f = (1 + 2 * cp1.cu() * cp1.da()) *
306// (1 + 2 * cp2.cu() * cp2.da()) * cos(cp1.fi()-cp2.fi());
307// f -= 2 * (lp1.gamma() * lp2.kappa() + lp2.gamma() * lp1.kappa());
308//cnv double cosphi12 = f;
309// }
310 return 2;
311 }
312 }
313 }
314 }
315 return 0;
316 }
317
318//
319// const member functions
320//
321
322//
323// static member functions
324//
325
327 std::ostream& operator<<(std::ostream& o, const TRGCDCLpar& s)
328 {
329 return o << " al=" << s.m_alpha << " be=" << s.m_beta
330 << " ka=" << s.m_kappa << " ga=" << s.m_gamma;
331 }
332
334} // namespace Belle2
335
Private class cpar.
Definition: Lpar.h:94
double m_cfi
parameter of Cpar class
Definition: Lpar.h:120
double m_fi
parameter of Cpar class
Definition: Lpar.h:114
double m_sfi
parameter of Cpar class
Definition: Lpar.h:118
double m_cu
parameter of Cpar class
Definition: Lpar.h:112
double m_da
parameter of Cpar class
Definition: Lpar.h:116
TRGCDCLpar class.
Definition: Lpar.h:35
double m_beta
data members
Definition: Lpar.h:176
double m_kappa
data members
Definition: Lpar.h:180
double xc() const
private const member functions
Definition: Lpar.h:165
friend std::ostream & operator<<(std::ostream &o, const TRGCDCLpar &)
ostream operator
Definition: Lpar.cc:327
double m_gamma
data members
Definition: Lpar.h:178
double kappa() const
const member functions
Definition: Lpar.h:57
double gamma() const
private const member functions
Definition: Lpar.h:143
double da() const
private const member functions
Definition: Lpar.h:169
double kr2g(double r) const
private const member functions
Definition: Lpar.h:151
double beta() const
private const member functions
Definition: Lpar.h:141
friend int intersect(const TRGCDCLpar &, const TRGCDCLpar &, CLHEP::HepVector &, CLHEP::HepVector &)
intersection
Definition: Lpar.cc:249
double m_alpha
data members
Definition: Lpar.h:174
double yc() const
private const member functions
Definition: Lpar.h:167
double alpha() const
private const member functions
Definition: Lpar.h:139
double xi2() const
private const member functions
Definition: Lpar.h:159
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
double d0(double x, double y) const
private const member functions
Definition: Lpar.h:250
void circle(double x1, double y1, double x2, double y2, double x3, double y3)
circle
Definition: Lpar.cc:77
double s(double x, double y) const
const member functions
Definition: Lpar.cc:210
CLHEP::HepMatrix dldc() const
private const member functions
Definition: Lpar.cc:130
bool xy(double, double &, double &, int dir=0) const
private const member functions
Definition: Lpar.cc:157
double x(double r) const
private const member functions
Definition: Lpar.cc:174
Cpar(const TRGCDCLpar &)
constructor of Cpar class
Definition: Lpar.cc:36
void xhyh(double x, double y, double &xh, double &yh) const
private const member functions
Definition: Lpar.cc:197
static const double BELLE_ALPHA
belle alpha
Definition: Lpar.h:183
virtual ~TRGCDCLpar()
Destructor.
Definition: Lpar.cc:52
double y(double r) const
private const member functions
Definition: Lpar.cc:181
CLHEP::HepVector center() const
const member functions
Definition: Lpar.cc:233
double phi(double r, int dir=0) const
const member functions
Definition: Lpar.cc:188
double dr(double x, double y) const
const member functions
Definition: Lpar.h:263
Abstract base class for different kinds of events.