3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.h"
8 #include "AliL3Logging.h"
9 #include "AliL3ConfMapFit.h"
10 #include "AliL3Vertex.h"
11 #include "AliL3ConfMapTrack.h"
12 #include "AliL3ConfMapPoint.h"
13 #include "AliL3Transform.h"
15 /** \class AliL3ConfMapFit
17 //_____________________________________________________________
20 // Fit class for conformal mapping tracking
24 ClassImp(AliL3ConfMapFit)
26 Double_t AliL3ConfMapFit::pi=3.14159265358979323846;
28 AliL3ConfMapFit::AliL3ConfMapFit(AliL3ConfMapTrack *track,AliL3Vertex *vertex)
37 Int_t AliL3ConfMapFit::FitHelix()
41 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitHelix","TrackFit")<<AliL3Log::kDec<<
42 "Problems during circle fit"<<ENDLOG;
47 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitHelix","TrackFit")<<AliL3Log::kDec<<
48 "Problems during line fit"<<ENDLOG;
54 Int_t AliL3ConfMapFit::FitCircle()
56 //-----------------------------------------------------------------
57 //Fits circle parameters using algorithm
58 //described by ChErnov and Oskov in Computer Physics
61 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
62 //Moved to C by Pablo Yepes
63 //Moved to AliROOT by ASV.
64 //------------------------------------------------------------------
70 Int_t num_of_hits = fTrack->GetNumberOfPoints();
72 // Loop over hits calculating average
75 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
78 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
79 cHit->SetXYWeight( 1./ (Double_t)(cHit->GetXerr()*cHit->GetXerr() + cHit->GetYerr()*cHit->GetYerr()) );
80 wsum += cHit->GetXYWeight() ;
81 xav += cHit->GetXYWeight() * cHit->GetX() ;
82 yav += cHit->GetXYWeight() * cHit->GetY() ;
85 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitCircle","TrackFit")<<AliL3Log::kDec<<
86 "Mismatch of hits. Counter: "<<co<<" nHits: "<<num_of_hits<<ENDLOG;
87 if (fTrack->ComesFromMainVertex() == true)
89 wsum += fVertex->GetXYWeight() ;
90 xav += fVertex->GetX() ;
91 yav += fVertex->GetY() ;
97 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
100 Double_t xyav = 0.0 ;
101 Double_t yyav = 0.0 ;
104 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
106 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint *)hits->At(hit_counter);
107 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
108 xi = cHit->GetX() - xav ;
109 yi = cHit->GetY() - yav ;
110 xxav += xi * xi * cHit->GetXYWeight() ;
111 xyav += xi * yi * cHit->GetXYWeight() ;
112 yyav += yi * yi * cHit->GetXYWeight() ;
115 if (fTrack->ComesFromMainVertex() == true)
117 xi = fVertex->GetX() - xav ;
118 yi = fVertex->GetY() - yav ;
119 xxav += xi * xi * fVertex->GetXYWeight() ;
120 xyav += xi * yi * fVertex->GetXYWeight() ;
121 yyav += yi * yi * fVertex->GetXYWeight() ;
127 //--> ROTATE COORDINATES SO THAT <XY> = 0
129 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
130 //--> & > ==> NEW : (XXAV-YYAV) > 0
131 //--> SIGN(S) = SIGN(XYAV) >
133 Double_t a = fabs( xxav - yyav ) ;
134 Double_t b = 4.0 * xyav * xyav ;
136 Double_t asqpb = a * a + b ;
137 Double_t rasqpb = sqrt ( asqpb) ;
139 Double_t splus = 1.0 + a / rasqpb ;
140 Double_t sminus = b / (asqpb * splus) ;
142 splus = sqrt (0.5 * splus ) ;
143 sminus = sqrt (0.5 * sminus) ;
145 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
147 Double_t sinrot, cosrot ;
148 if ( xxav <= yyav ) {
157 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
159 if ( xyav < 0.0 ) sinrot = - sinrot ;
161 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
162 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
163 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
164 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
166 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
168 if ( cosrot*xav+sinrot*yav < 0.0 ) {
173 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
175 Double_t rrav = xxav + yyav ;
176 Double_t rscale = sqrt(rrav) ;
181 Double_t xrrav = 0.0 ;
182 Double_t yrrav = 0.0 ;
183 Double_t rrrrav = 0.0 ;
185 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
187 //for (hit_counter=0; hit_counter<num_of_hits; hit_counter++)
188 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
190 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
191 AliL3ConfMapPoint* cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
193 xold = cHit->GetX() - xav ;
194 yold = cHit->GetY() - yav ;
196 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
198 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
199 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
204 wiriri = cHit->GetXYWeight() * riri ;
206 xyav += cHit->GetXYWeight() * xi * yi ;
207 xxav += cHit->GetXYWeight() * xixi ;
208 yyav += cHit->GetXYWeight() * yiyi ;
210 xrrav += wiriri * xi ;
211 yrrav += wiriri * yi ;
212 rrrrav += wiriri * riri ;
215 // Include vertex if required
217 if (fTrack->ComesFromMainVertex() == true)
219 xold = fVertex->GetX() - xav ;
220 yold = fVertex->GetY() - yav ;
222 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
224 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
225 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
230 wiriri = fVertex->GetXYWeight() * riri ;
232 xyav += fVertex->GetXYWeight() * xi * yi ;
233 xxav += fVertex->GetXYWeight() * xixi ;
234 yyav += fVertex->GetXYWeight() * yiyi ;
236 xrrav += wiriri * xi ;
237 yrrav += wiriri * yi ;
238 rrrrav += wiriri * riri ;
243 //--> DIVIDE BY WSUM TO MAKE AVERAGES
247 xrrav = xrrav / wsum ;
248 yrrav = yrrav / wsum ;
249 rrrrav = rrrrav / wsum ;
252 Int_t const ntry = 5 ;
254 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
255 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
257 Double_t xrrxrr = xrrav * xrrav ;
258 Double_t yrryrr = yrrav * yrrav ;
259 Double_t rrrrm1 = rrrrav - 1.0 ;
260 Double_t xxyy = xxav * yyav ;
262 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
263 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
264 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
265 Double_t c4 = - 4.0 ;
267 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
269 Double_t c2d = 2.0 * c2 ;
270 Double_t c4d = 4.0 * c4 ;
272 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
274 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
275 Double_t lamda = 0.0 ;
276 Double_t dlamda = 0.0 ;
278 Double_t chiscl = wsum * rscale * rscale ;
279 Double_t dlamax = 0.001 / chiscl ;
282 for ( int itry = 1 ; itry <= ntry ; itry++ ) {
283 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
284 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
286 lamda = lamda + dlamda ;
287 if (fabs(dlamda)< dlamax) break ;
290 Double_t chi2 = (Double_t)(chiscl * lamda) ;
292 fTrack->SetChiSq1(chi2);
293 // Double_t dchisq = chiscl * dlamda ;
295 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
297 Double_t h11 = xxav - lamda ;
298 Double_t h14 = xrrav ;
299 Double_t h22 = yyav - lamda ;
300 Double_t h24 = yrrav ;
301 Double_t h34 = 1.0 + 2.0*lamda ;
302 if ( h11 == 0.0 || h22 == 0.0 ){
303 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitCircle","TrackFit")<<AliL3Log::kDec<<
304 "Problems fitting circle"<<ENDLOG;
307 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
309 Double_t ratio, kappa, beta ;
310 if ( fabs(h22) > fabs(h24) ) {
312 rootsq = ratio * ratio + rootsq ;
313 kappa = 1.0 / sqrt(rootsq) ;
314 beta = - ratio * kappa ;
318 rootsq = 1.0 + ratio * ratio * rootsq ;
319 beta = 1.0 / sqrt(rootsq) ;
320 if ( h24 > 0 ) beta = - beta ;
321 kappa = -ratio * beta ;
323 Double_t alpha = - (h14/h11) * kappa ;
325 //--> transform these into the lab coordinate system
326 //--> first get kappa and back to real dimensions
328 Double_t kappa1 = kappa / rscale ;
329 Double_t dbro = 0.5 / kappa1 ;
331 //--> next rotate alpha and beta and scale
333 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
334 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
336 //--> then translate by (xav,yav)
338 Double_t acent = (double)(xav - alphar) ;
339 Double_t bcent = (double)(yav - betar ) ;
340 Double_t radius = (double)dbro ;
344 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
346 fTrack->SetCharge(q);
349 // Get other track parameters
351 Double_t x0, y0,phi0,r0,psi,pt ;
352 if ( fTrack->ComesFromMainVertex() == true )
354 //flag = 1 ; // primary track flag
355 x0 = fVertex->GetX() ;
356 y0 = fVertex->GetY() ;
357 phi0 = fVertex->GetPhi() ;
358 r0 = fVertex->GetR() ;
359 fTrack->SetPhi0(phi0);
364 //AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)hits->Last();
365 AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)fTrack->lastHit;
366 //flag = 0 ; // primary track flag
369 phi0 = atan2(lHit->GetY(),lHit->GetX());
370 if ( phi0 < 0 ) phi0 += 2*pi;
371 r0 = sqrt ( lHit->GetX() * lHit->GetX() + lHit->GetY() * lHit->GetY() ) ;
372 fTrack->SetPhi0(phi0);
376 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
377 psi = psi + q * 0.5F * pi ;
378 if ( psi < 0 ) psi = psi + 2*pi;
380 pt = (Double_t)(BFACT * AliL3Transform::GetBField() * radius ) ;
385 // Get errors from fast fit
387 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
393 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
394 // Fit Line in s-z plane
395 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
396 Int_t AliL3ConfMapFit::FitLine ( )
407 //find sum , sums ,sumz, sumss
410 Double_t radius = (Double_t)(fTrack->GetPt() / ( BFACT * AliL3Transform::GetBField() ) ) ;
412 //TObjArray *hits = fTrack->GetHits();
413 //Int_t num_of_hits = fTrack->GetNumberOfPoints();
415 if ( fTrack->ComesFromMainVertex() == true )
417 dx = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetX() - fVertex->GetX();
418 dy = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetY() - fVertex->GetY() ;
422 dx = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetX() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetX() ;
423 dy = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetY() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetY() ;
424 //dx = ((AliL3ConfMapPoint *)hits->First())->GetX() - ((AliL3ConfMapPoint *)hits->Last())->GetX() ;
425 //dy = ((AliL3ConfMapPoint *)hits->First())->GetY() - ((AliL3ConfMapPoint *)hits->Last())->GetY() ;
428 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
431 if ( fabs(localPsi) < 1. )
433 total_s = 2.0 * radius * asin ( localPsi ) ;
437 total_s = 2.0 * radius * pi ;
440 AliL3ConfMapPoint *previousHit = NULL;
442 // FtfBaseHit *previousHit = 0 ;
444 //for ( startLoop() ; done() ; nextHit() ) {
447 // for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
448 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
450 // AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
451 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
452 // if ( currentHit != firstHit )
453 if(cHit != fTrack->firstHit)// hits->First())
455 dx = cHit->GetX() - previousHit->GetX() ;
456 dy = cHit->GetY() - previousHit->GetY() ;
457 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
458 fTrack->SetPsierr(dpsi);
459 s = previousHit->GetS() - 2.0 * radius * (Double_t)asin ( dpsi ) ;
464 // cHit->s = total_s ;
466 sum += cHit->GetZWeight() ;
467 ss += cHit->GetZWeight() * cHit->GetS() ;
468 sz += cHit->GetZWeight() * cHit->GetZ() ;
469 sss += cHit->GetZWeight() * cHit->GetS() * cHit->GetS() ;
470 ssz += cHit->GetZWeight() * cHit->GetS() * cHit->GetZ() ;
474 Double_t chi2,det = sum * sss - ss * ss;
475 if ( fabs(det) < 1e-20)
478 fTrack->SetChiSq2(chi2);
482 //Compute the best fitted parameters A,B
483 Double_t tanl,z0,dtanl,dz0;
485 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
486 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
488 fTrack->SetTgl(tanl);
491 // calculate chi-square
496 //for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
497 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
499 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
500 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
501 r1 = cHit->GetZ() - tanl * cHit->GetS() - z0 ;
502 chi2 += (Double_t) ( (Double_t)cHit->GetZWeight() * (r1 * r1) );
504 fTrack->SetChiSq2(chi2);
506 // calculate estimated variance
507 // varsq=chi/(double(n)-2.)
508 // calculate covariance matrix
509 // siga=sqrt(varsq*sxx/det)
510 // sigb=sqrt(varsq*sum/det)
512 dtanl = (Double_t) ( sum / det );
513 dz0 = (Double_t) ( sss / det );
515 fTrack->SetTglerr(dtanl);
516 fTrack->SetZ0err(dz0);