54 double delta1(10*prec);
55 double delta2(10*prec);
57 static const unsigned maxiter(20);
58 while(niter < maxiter &&
59 ( delta1 > prec || delta2 > prec ) ){
63 if(niter == 0) pos1b = pos1;
71 double m1, m2, q1, q2;
72 double r1, r2, xc1,xc2,yc1,yc2,sr1,sr2;
80 q1 = pos1.y()-pos1.x()*m1;
83 r1 = (1- dir1[2]*dir1[2])/curv1;
85 if(dir1[0]*dd1[1] - dir1[1]*dd1[0] < 0) sr1 = -r1;
86 double cosphi1 = dir1[0]/sqrt(dir1[0]*dir1[0]+dir1[1]*dir1[1]);
87 double sinphi1 = cosphi1 * dir1[1]/dir1[0];
88 xc1 = pos1.x() - sr1 * sinphi1;
89 yc1 = pos1.y() + sr1 * cosphi1;
97 q2 = pos2.y()-pos2.x()*m2;
99 r2 = (1-dir2[2]*dir2[2])/curv2;
101 if(dir2[0]*dd2[1] - dir2[1]*dd2[0] < 0) sr2 = -r2;
102 double cosphi2 = dir2[0]/sqrt(dir2[0]*dir2[0]+dir2[1]*dir2[1]);
103 double sinphi2 = cosphi2 * dir2[1]/dir2[0];
104 xc2 = pos2.x() - sr2 * sinphi2;
105 yc2 = pos2.y() + sr2 * cosphi2;
107 double xint, yint, xint1, xint2, yint1, yint2, absdoca;
113 if(curv1==0 && curv2==0){
115 interTwoLines(m1, q1, m2, q2, xint, yint);
120 if(curv1 !=0 && curv2 !=0){
122 double cdist = sqrt((xc1-xc2)*(xc1-xc2)+(yc1-yc2)*(yc1-yc2));
124 if (fabs(cdist) < 1.e-12 ) {
127 "TrkPocaXY:: the two circles have the same center...");
131 if ( (fabs(r1-r2) < cdist) && (cdist < r1+r2 ) ) {
132 interTwoCircles(xc1,yc1,r1,xc2,yc2,r2,xint1,yint1,xint2,yint2);
133 double dist1 = (xint1-pos1b.x())*(xint1-pos1b.x()) +
134 (yint1-pos1b.y())*(yint1-pos1b.y());
135 double dist2 = (xint2-pos1b.x())*(xint2-pos1b.x()) +
136 (yint2-pos1b.y())*(yint2-pos1b.y());
150 if ( (fabs(r1-r2) > cdist) ||
151 ( cdist > (r1+r2) )) {
154 double m = (yc1-yc2)/(xc1-xc2);
155 double q = yc1 - m*xc1;
159 double xint1, yint1, xint2, yint2, zOfDCrossT1;
161 interLineCircle(m,
q, xc1, yc1, r1, xint1, yint1, xint2, yint2);
163 double xint3, yint3, xint4, yint4 ;
164 interLineCircle(m,
q, xc2, yc2, r2, xint3, yint3, xint4, yint4);
165 if (fabs(r1-r2) > cdist) {
166 absdoca = fabs(r1-r2)-cdist;
167#ifdef MDCPATREC_DEBUG
168 cout <<
"ErrMsg(debugging) doing nested circles in TrkPocaXY " << endl;
171 double dist1_3 = pow((xint1-xint3),2.) + pow((yint1-yint3),2.);
172 double dist2_4 = pow((xint2-xint4),2.) + pow((yint2-yint4),2.);
174 if(dist1_3<dist2_4) {
175 xint = 0.5*(xint1+xint3);
176 yint = 0.5*(yint1+yint3);
177 zOfDCrossT1 = (xint3-xint1)*dir1[1]-(yint3-yint1)*dir1[0];
179 xint = 0.5*(xint2+xint4);
180 yint = 0.5*(yint2+yint4);
181 zOfDCrossT1 = (xint4-xint2)*dir1[1]-(yint4-yint2)*dir1[0];
184 if( zOfDCrossT1 > 0) _docaxy = -absdoca;
186 if( cdist > (r1+r2) ) {
187 absdoca = cdist - (r1+r2);
188#ifdef MDCPATREC_DEBUG
189 cout <<
"ErrMsg(debugging) doing separated circles in TrkPocaXY"<< endl;
192 double dist2_3 = pow((xint2-xint3),2.) + pow((yint2-yint3),2.);
193 double dist1_4 = pow((xint1-xint4),2.) + pow((yint1-yint4),2.);
194 if (dist2_3<dist1_4){
195 xint = 0.5*(xint2+xint3);
196 yint = 0.5*(yint2+yint3);
197 zOfDCrossT1 = (xint3-xint1)*dir1[1]-(yint3-yint1)*dir1[0];
199 xint = 0.5*(xint1+xint4);
200 yint = 0.5*(yint1+yint4);
201 zOfDCrossT1 = (xint4-xint2)*dir1[1]-(yint4-yint2)*dir1[0];
204 if( zOfDCrossT1 > 0) _docaxy = -absdoca;
211 if((curv1 == 0 && curv2 !=0) || (curv1 != 0 && curv2 == 0)) {
214 double m,
q,r,xc,yc, zOfDCrossT1;
215 if(curv1==0) {m=m1;
q=q1; r=r2; xc=xc2; yc=yc2; dirT=dir2;}
216 else {m=m2;
q=q2; r=r1; xc=xc1; yc=yc1; dirT=dir1;}
218 double dist = fabs((m*xc-yc+
q)/sqrt(1+m*m));
223 interLineCircle(m,
q,xc,yc,r, xint1, yint1, xint2, yint2);
224 double dist1 = (xint1-pos1b.x())*(xint1-pos1b.x()) +
225 (yint1-pos1b.y())*(yint1-pos1b.y());
226 double dist2 = (xint2-pos1b.x())*(xint2-pos1b.x()) +
227 (yint2-pos1b.y())*(yint2-pos1b.y());
237#ifdef MDCPATREC_DEBUG
238 cout <<
"ErrMsg(debugging) doing separated line-circle in TrkPocaXY"<<endl;
243 double mperp = -1./m;
244 double qperp = yc - mperp*xc;
248 interLineCircle(mperp, qperp, xc, yc, r, xint1,yint1,xint2,yint2);
251 interTwoLines(m,
q, mperp, qperp, xint3, yint3);
253 double dist1_3 = pow((xint1-xint3),2.) + pow((yint1-yint3),2.);
254 double dist2_3 = pow((xint2-xint3),2.) + pow((yint2-yint3),2.);
255 if (dist1_3<dist2_3) {
256 xint = 0.5*(xint3 + xint1);
257 yint = 0.5*(yint3 + yint1);
258 absdoca = sqrt((xint3-xint1)*(xint3-xint1)+
259 (yint3-yint1)*(yint3-yint1));
260 zOfDCrossT1 = (xint3-xint1)*dirT[1]-(yint3-yint1)*dirT[0];
263 xint = 0.5*(xint3 + xint2);
264 yint = 0.5*(yint3 + yint2);
265 absdoca = sqrt((xint3-xint2)*(xint3-xint2)+
266 (yint3-yint2)*(yint3-yint2));
267 zOfDCrossT1 = (xint3-xint2)*dirT[1]-(yint3-yint2)*dirT[0];
270 if( zOfDCrossT1 > 0) _docaxy = -absdoca;
286#ifdef MDCPATREC_WARNING
287 cout <<
"ErrMsg(warning)" <<
" poca fialure " << endl;
294#ifdef MDCPATREC_DEBUG
295 cout <<
"ErrMsg(debugging)" <<
"TrkPocaXY iterations = " << niter-1
296 <<
" flight lengths " <<
_flt1 <<
" " <<
_flt2 << endl
297 <<
" deltas " << delta1 <<
" " << delta2 << endl;
299 if(niter == maxiter){
300#ifdef MDCPATREC_ROUTINE
301 cout <<
"ErrMsg(routine)" <<
"TrkPocaXY:: did not converge" << endl;