3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ASV
9 #include "AliL3Logging.h"
10 #include "AliL3ConfMapFit.h"
11 #include "AliL3Vertex.h"
12 #include "AliL3ConfMapTrack.h"
13 #include "AliL3ConfMapPoint.h"
14 #include "AliL3Transform.h"
16 //_____________________________________________________________
19 // Fit class for conformal mapping tracking
21 ClassImp(AliL3ConfMapFit)
23 Double_t AliL3ConfMapFit::pi=3.14159265358979323846;
25 AliL3ConfMapFit::AliL3ConfMapFit(AliL3ConfMapTrack *track,AliL3Vertex *vertex)
34 Int_t AliL3ConfMapFit::FitHelix()
38 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitHelix","TrackFit")<<AliL3Log::kDec<<
39 "Problems during circle fit"<<ENDLOG;
44 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitHelix","TrackFit")<<AliL3Log::kDec<<
45 "Problems during line fit"<<ENDLOG;
51 Int_t AliL3ConfMapFit::FitCircle()
53 //-----------------------------------------------------------------
54 //Fits circle parameters using algorithm
55 //described by ChErnov and Oskov in Computer Physics
58 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
59 //Moved to C by Pablo Yepes
60 //Moved to AliROOT by ASV.
61 //------------------------------------------------------------------
67 Int_t num_of_hits = fTrack->GetNumberOfPoints();
69 // Loop over hits calculating average
72 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
75 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
76 cHit->SetXYWeight( 1./ (Double_t)(cHit->GetXerr()*cHit->GetXerr() + cHit->GetYerr()*cHit->GetYerr()) );
77 wsum += cHit->GetXYWeight() ;
78 xav += cHit->GetXYWeight() * cHit->GetX() ;
79 yav += cHit->GetXYWeight() * cHit->GetY() ;
82 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitCircle","TrackFit")<<AliL3Log::kDec<<
83 "Mismatch of hits. Counter: "<<co<<" nHits: "<<num_of_hits<<ENDLOG;
84 if (fTrack->ComesFromMainVertex() == true)
86 wsum += fVertex->GetXYWeight() ;
87 xav += fVertex->GetX() ;
88 yav += fVertex->GetY() ;
94 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
101 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
103 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint *)hits->At(hit_counter);
104 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
105 xi = cHit->GetX() - xav ;
106 yi = cHit->GetY() - yav ;
107 xxav += xi * xi * cHit->GetXYWeight() ;
108 xyav += xi * yi * cHit->GetXYWeight() ;
109 yyav += yi * yi * cHit->GetXYWeight() ;
112 if (fTrack->ComesFromMainVertex() == true)
114 xi = fVertex->GetX() - xav ;
115 yi = fVertex->GetY() - yav ;
116 xxav += xi * xi * fVertex->GetXYWeight() ;
117 xyav += xi * yi * fVertex->GetXYWeight() ;
118 yyav += yi * yi * fVertex->GetXYWeight() ;
124 //--> ROTATE COORDINATES SO THAT <XY> = 0
126 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
127 //--> & > ==> NEW : (XXAV-YYAV) > 0
128 //--> SIGN(S) = SIGN(XYAV) >
130 Double_t a = fabs( xxav - yyav ) ;
131 Double_t b = 4.0 * xyav * xyav ;
133 Double_t asqpb = a * a + b ;
134 Double_t rasqpb = sqrt ( asqpb) ;
136 Double_t splus = 1.0 + a / rasqpb ;
137 Double_t sminus = b / (asqpb * splus) ;
139 splus = sqrt (0.5 * splus ) ;
140 sminus = sqrt (0.5 * sminus) ;
142 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
144 Double_t sinrot, cosrot ;
145 if ( xxav <= yyav ) {
154 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
156 if ( xyav < 0.0 ) sinrot = - sinrot ;
158 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
159 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
160 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
161 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
163 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
165 if ( cosrot*xav+sinrot*yav < 0.0 ) {
170 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
172 Double_t rrav = xxav + yyav ;
173 Double_t rscale = sqrt(rrav) ;
178 Double_t xrrav = 0.0 ;
179 Double_t yrrav = 0.0 ;
180 Double_t rrrrav = 0.0 ;
182 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
184 //for (hit_counter=0; hit_counter<num_of_hits; hit_counter++)
185 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
187 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
188 AliL3ConfMapPoint* cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
190 xold = cHit->GetX() - xav ;
191 yold = cHit->GetY() - yav ;
193 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
195 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
196 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
201 wiriri = cHit->GetXYWeight() * riri ;
203 xyav += cHit->GetXYWeight() * xi * yi ;
204 xxav += cHit->GetXYWeight() * xixi ;
205 yyav += cHit->GetXYWeight() * yiyi ;
207 xrrav += wiriri * xi ;
208 yrrav += wiriri * yi ;
209 rrrrav += wiriri * riri ;
212 // Include vertex if required
214 if (fTrack->ComesFromMainVertex() == true)
216 xold = fVertex->GetX() - xav ;
217 yold = fVertex->GetY() - yav ;
219 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
221 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
222 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
227 wiriri = fVertex->GetXYWeight() * riri ;
229 xyav += fVertex->GetXYWeight() * xi * yi ;
230 xxav += fVertex->GetXYWeight() * xixi ;
231 yyav += fVertex->GetXYWeight() * yiyi ;
233 xrrav += wiriri * xi ;
234 yrrav += wiriri * yi ;
235 rrrrav += wiriri * riri ;
240 //--> DIVIDE BY WSUM TO MAKE AVERAGES
244 xrrav = xrrav / wsum ;
245 yrrav = yrrav / wsum ;
246 rrrrav = rrrrav / wsum ;
249 Int_t const ntry = 5 ;
251 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
252 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
254 Double_t xrrxrr = xrrav * xrrav ;
255 Double_t yrryrr = yrrav * yrrav ;
256 Double_t rrrrm1 = rrrrav - 1.0 ;
257 Double_t xxyy = xxav * yyav ;
259 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
260 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
261 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
262 Double_t c4 = - 4.0 ;
264 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
266 Double_t c2d = 2.0 * c2 ;
267 Double_t c4d = 4.0 * c4 ;
269 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
271 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
272 Double_t lamda = 0.0 ;
273 Double_t dlamda = 0.0 ;
275 Double_t chiscl = wsum * rscale * rscale ;
276 Double_t dlamax = 0.001 / chiscl ;
279 for ( int itry = 1 ; itry <= ntry ; itry++ ) {
280 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
281 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
283 lamda = lamda + dlamda ;
284 if (fabs(dlamda)< dlamax) break ;
287 Double_t chi2 = (Double_t)(chiscl * lamda) ;
289 fTrack->SetChiSq1(chi2);
290 // Double_t dchisq = chiscl * dlamda ;
292 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
294 Double_t h11 = xxav - lamda ;
295 Double_t h14 = xrrav ;
296 Double_t h22 = yyav - lamda ;
297 Double_t h24 = yrrav ;
298 Double_t h34 = 1.0 + 2.0*lamda ;
299 if ( h11 == 0.0 || h22 == 0.0 ){
300 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitCircle","TrackFit")<<AliL3Log::kDec<<
301 "Problems fitting circle"<<ENDLOG;
304 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
306 Double_t ratio, kappa, beta ;
307 if ( fabs(h22) > fabs(h24) ) {
309 rootsq = ratio * ratio + rootsq ;
310 kappa = 1.0 / sqrt(rootsq) ;
311 beta = - ratio * kappa ;
315 rootsq = 1.0 + ratio * ratio * rootsq ;
316 beta = 1.0 / sqrt(rootsq) ;
317 if ( h24 > 0 ) beta = - beta ;
318 kappa = -ratio * beta ;
320 Double_t alpha = - (h14/h11) * kappa ;
322 //--> transform these into the lab coordinate system
323 //--> first get kappa and back to real dimensions
325 Double_t kappa1 = kappa / rscale ;
326 Double_t dbro = 0.5 / kappa1 ;
328 //--> next rotate alpha and beta and scale
330 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
331 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
333 //--> then translate by (xav,yav)
335 Double_t acent = (double)(xav - alphar) ;
336 Double_t bcent = (double)(yav - betar ) ;
337 Double_t radius = (double)dbro ;
341 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
343 fTrack->SetCharge(q);
346 // Get other track parameters
348 Double_t x0, y0,phi0,r0,psi,pt ;
349 if ( fTrack->ComesFromMainVertex() == true )
351 //flag = 1 ; // primary track flag
352 x0 = fVertex->GetX() ;
353 y0 = fVertex->GetY() ;
354 phi0 = fVertex->GetPhi() ;
355 r0 = fVertex->GetR() ;
356 fTrack->SetPhi0(phi0);
361 //AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)hits->Last();
362 AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)fTrack->lastHit;
363 //flag = 0 ; // primary track flag
366 phi0 = atan2(lHit->GetY(),lHit->GetX());
367 if ( phi0 < 0 ) phi0 += 2*pi;
368 r0 = sqrt ( lHit->GetX() * lHit->GetX() + lHit->GetY() * lHit->GetY() ) ;
369 fTrack->SetPhi0(phi0);
373 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
374 psi = psi + q * 0.5F * pi ;
375 if ( psi < 0 ) psi = psi + 2*pi;
377 pt = (Double_t)(BFACT * AliL3Transform::GetBField() * radius ) ;
382 // Get errors from fast fit
384 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
390 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
391 // Fit Line in s-z plane
392 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
393 Int_t AliL3ConfMapFit::FitLine ( )
404 //find sum , sums ,sumz, sumss
407 Double_t radius = (Double_t)(fTrack->GetPt() / ( BFACT * AliL3Transform::GetBField() ) ) ;
409 //TObjArray *hits = fTrack->GetHits();
410 //Int_t num_of_hits = fTrack->GetNumberOfPoints();
412 if ( fTrack->ComesFromMainVertex() == true )
414 dx = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetX() - fVertex->GetX();
415 dy = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetY() - fVertex->GetY() ;
419 dx = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetX() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetX() ;
420 dy = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetY() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetY() ;
421 //dx = ((AliL3ConfMapPoint *)hits->First())->GetX() - ((AliL3ConfMapPoint *)hits->Last())->GetX() ;
422 //dy = ((AliL3ConfMapPoint *)hits->First())->GetY() - ((AliL3ConfMapPoint *)hits->Last())->GetY() ;
425 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
428 if ( fabs(localPsi) < 1. )
430 total_s = 2.0 * radius * asin ( localPsi ) ;
434 total_s = 2.0 * radius * pi ;
437 AliL3ConfMapPoint *previousHit = NULL;
439 // FtfBaseHit *previousHit = 0 ;
441 //for ( startLoop() ; done() ; nextHit() ) {
444 // for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
445 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
447 // AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
448 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
449 // if ( currentHit != firstHit )
450 if(cHit != fTrack->firstHit)// hits->First())
452 dx = cHit->GetX() - previousHit->GetX() ;
453 dy = cHit->GetY() - previousHit->GetY() ;
454 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
455 fTrack->SetPsierr(dpsi);
456 s = previousHit->GetS() - 2.0 * radius * (Double_t)asin ( dpsi ) ;
461 // cHit->s = total_s ;
463 sum += cHit->GetZWeight() ;
464 ss += cHit->GetZWeight() * cHit->GetS() ;
465 sz += cHit->GetZWeight() * cHit->GetZ() ;
466 sss += cHit->GetZWeight() * cHit->GetS() * cHit->GetS() ;
467 ssz += cHit->GetZWeight() * cHit->GetS() * cHit->GetZ() ;
471 Double_t chi2,det = sum * sss - ss * ss;
472 if ( fabs(det) < 1e-20)
475 fTrack->SetChiSq2(chi2);
479 //Compute the best fitted parameters A,B
480 Double_t tanl,z0,dtanl,dz0;
482 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
483 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
485 fTrack->SetTgl(tanl);
488 // calculate chi-square
493 //for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
494 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
496 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
497 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
498 r1 = cHit->GetZ() - tanl * cHit->GetS() - z0 ;
499 chi2 += (Double_t) ( (Double_t)cHit->GetZWeight() * (r1 * r1) );
501 fTrack->SetChiSq2(chi2);
503 // calculate estimated variance
504 // varsq=chi/(double(n)-2.)
505 // calculate covariance matrix
506 // siga=sqrt(varsq*sxx/det)
507 // sigb=sqrt(varsq*sum/det)
509 dtanl = (Double_t) ( sum / det );
510 dz0 = (Double_t) ( sss / det );
512 fTrack->SetTglerr(dtanl);
513 fTrack->SetZ0err(dz0);