3 #include "AliL3Logging.h"
4 #include "AliL3ConfMapFit.h"
5 #include "AliL3Vertex.h"
6 #include "AliL3ConfMapTrack.h"
7 #include "AliL3ConfMapPoint.h"
9 //_______________________________
12 // Fit class for conformal mapping tracking
14 ClassImp(AliL3ConfMapFit)
16 Double_t AliL3ConfMapFit::pi=3.14159265358979323846;
18 AliL3ConfMapFit::AliL3ConfMapFit(AliL3ConfMapTrack *track,AliL3Vertex *vertex)
28 Int_t AliL3ConfMapFit::FitHelix()
32 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitHelix","TrackFit")<<AliL3Log::kDec<<
33 "Problems during circle fit"<<ENDLOG;
38 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitHelix","TrackFit")<<AliL3Log::kDec<<
39 "Problems during line fit"<<ENDLOG;
45 Int_t AliL3ConfMapFit::FitCircle()
47 //-----------------------------------------------------------------
48 //Fits circle parameters using algorithm
49 //described by ChErnov and Oskov in Computer Physics
52 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
53 //Moved to C by Pablo Yepes
54 //Moved to AliROOT by ASV.
55 //------------------------------------------------------------------
61 Int_t num_of_hits = fTrack->GetNumberOfPoints();
63 // Loop over hits calculating average
66 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
69 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
70 cHit->SetXYWeight( 1./ (Double_t)(cHit->GetXerr()*cHit->GetXerr() + cHit->GetYerr()*cHit->GetYerr()) );
71 wsum += cHit->GetXYWeight() ;
72 xav += cHit->GetXYWeight() * cHit->GetX() ;
73 yav += cHit->GetXYWeight() * cHit->GetY() ;
76 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitCircle","TrackFit")<<AliL3Log::kDec<<
77 "Mismatch of hits. Counter: "<<co<<" nHits: "<<num_of_hits<<ENDLOG;
78 if (fTrack->ComesFromMainVertex() == true)
80 wsum += fVertex->GetXYWeight() ;
81 xav += fVertex->GetX() ;
82 yav += fVertex->GetY() ;
88 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
95 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
97 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint *)hits->At(hit_counter);
98 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
99 xi = cHit->GetX() - xav ;
100 yi = cHit->GetY() - yav ;
101 xxav += xi * xi * cHit->GetXYWeight() ;
102 xyav += xi * yi * cHit->GetXYWeight() ;
103 yyav += yi * yi * cHit->GetXYWeight() ;
106 if (fTrack->ComesFromMainVertex() == true)
108 xi = fVertex->GetX() - xav ;
109 yi = fVertex->GetY() - yav ;
110 xxav += xi * xi * fVertex->GetXYWeight() ;
111 xyav += xi * yi * fVertex->GetXYWeight() ;
112 yyav += yi * yi * fVertex->GetXYWeight() ;
118 //--> ROTATE COORDINATES SO THAT <XY> = 0
120 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
121 //--> & > ==> NEW : (XXAV-YYAV) > 0
122 //--> SIGN(S) = SIGN(XYAV) >
124 Double_t a = fabs( xxav - yyav ) ;
125 Double_t b = 4.0 * xyav * xyav ;
127 Double_t asqpb = a * a + b ;
128 Double_t rasqpb = sqrt ( asqpb) ;
130 Double_t splus = 1.0 + a / rasqpb ;
131 Double_t sminus = b / (asqpb * splus) ;
133 splus = sqrt (0.5 * splus ) ;
134 sminus = sqrt (0.5 * sminus) ;
136 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
138 Double_t sinrot, cosrot ;
139 if ( xxav <= yyav ) {
148 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
150 if ( xyav < 0.0 ) sinrot = - sinrot ;
152 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
153 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
154 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
155 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
157 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
159 if ( cosrot*xav+sinrot*yav < 0.0 ) {
164 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
166 Double_t rrav = xxav + yyav ;
167 Double_t rscale = sqrt(rrav) ;
172 Double_t xrrav = 0.0 ;
173 Double_t yrrav = 0.0 ;
174 Double_t rrrrav = 0.0 ;
176 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
178 //for (hit_counter=0; hit_counter<num_of_hits; hit_counter++)
179 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
181 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
182 AliL3ConfMapPoint* cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
184 xold = cHit->GetX() - xav ;
185 yold = cHit->GetY() - yav ;
187 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
189 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
190 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
195 wiriri = cHit->GetXYWeight() * riri ;
197 xyav += cHit->GetXYWeight() * xi * yi ;
198 xxav += cHit->GetXYWeight() * xixi ;
199 yyav += cHit->GetXYWeight() * yiyi ;
201 xrrav += wiriri * xi ;
202 yrrav += wiriri * yi ;
203 rrrrav += wiriri * riri ;
206 // Include vertex if required
208 if (fTrack->ComesFromMainVertex() == true)
210 xold = fVertex->GetX() - xav ;
211 yold = fVertex->GetY() - yav ;
213 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
215 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
216 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
221 wiriri = fVertex->GetXYWeight() * riri ;
223 xyav += fVertex->GetXYWeight() * xi * yi ;
224 xxav += fVertex->GetXYWeight() * xixi ;
225 yyav += fVertex->GetXYWeight() * yiyi ;
227 xrrav += wiriri * xi ;
228 yrrav += wiriri * yi ;
229 rrrrav += wiriri * riri ;
234 //--> DIVIDE BY WSUM TO MAKE AVERAGES
238 xrrav = xrrav / wsum ;
239 yrrav = yrrav / wsum ;
240 rrrrav = rrrrav / wsum ;
243 Int_t const ntry = 5 ;
245 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
246 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
248 Double_t xrrxrr = xrrav * xrrav ;
249 Double_t yrryrr = yrrav * yrrav ;
250 Double_t rrrrm1 = rrrrav - 1.0 ;
251 Double_t xxyy = xxav * yyav ;
253 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
254 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
255 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
256 Double_t c4 = - 4.0 ;
258 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
260 Double_t c2d = 2.0 * c2 ;
261 Double_t c4d = 4.0 * c4 ;
263 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
265 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
266 Double_t lamda = 0.0 ;
267 Double_t dlamda = 0.0 ;
269 Double_t chiscl = wsum * rscale * rscale ;
270 Double_t dlamax = 0.001 / chiscl ;
273 for ( int itry = 1 ; itry <= ntry ; itry++ ) {
274 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
275 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
277 lamda = lamda + dlamda ;
278 if (fabs(dlamda)< dlamax) break ;
281 Double_t chi2 = (Double_t)(chiscl * lamda) ;
283 fTrack->SetChiSq1(chi2);
284 // Double_t dchisq = chiscl * dlamda ;
286 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
288 Double_t h11 = xxav - lamda ;
289 Double_t h14 = xrrav ;
290 Double_t h22 = yyav - lamda ;
291 Double_t h24 = yrrav ;
292 Double_t h34 = 1.0 + 2.0*lamda ;
293 if ( h11 == 0.0 || h22 == 0.0 ){
294 LOG(AliL3Log::kError,"AliL3ConfMapFit::FitCircle","TrackFit")<<AliL3Log::kDec<<
295 "Problems fitting circle"<<ENDLOG;
298 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
300 Double_t ratio, kappa, beta ;
301 if ( fabs(h22) > fabs(h24) ) {
303 rootsq = ratio * ratio + rootsq ;
304 kappa = 1.0 / sqrt(rootsq) ;
305 beta = - ratio * kappa ;
309 rootsq = 1.0 + ratio * ratio * rootsq ;
310 beta = 1.0 / sqrt(rootsq) ;
311 if ( h24 > 0 ) beta = - beta ;
312 kappa = -ratio * beta ;
314 Double_t alpha = - (h14/h11) * kappa ;
316 //--> transform these into the lab coordinate system
317 //--> first get kappa and back to real dimensions
319 Double_t kappa1 = kappa / rscale ;
320 Double_t dbro = 0.5 / kappa1 ;
322 //--> next rotate alpha and beta and scale
324 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
325 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
327 //--> then translate by (xav,yav)
329 Double_t acent = (double)(xav - alphar) ;
330 Double_t bcent = (double)(yav - betar ) ;
331 Double_t radius = (double)dbro ;
335 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
337 fTrack->SetCharge(q);
340 // Get other track parameters
342 Double_t x0, y0,phi0,r0,psi,pt ;
343 if ( fTrack->ComesFromMainVertex() == true )
345 //flag = 1 ; // primary track flag
346 x0 = fVertex->GetX() ;
347 y0 = fVertex->GetY() ;
348 phi0 = fVertex->GetPhi() ;
349 r0 = fVertex->GetR() ;
350 fTrack->SetPhi0(phi0);
355 //AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)hits->Last();
356 AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)fTrack->lastHit;
357 //flag = 0 ; // primary track flag
360 phi0 = atan2(lHit->GetY(),lHit->GetX());
361 if ( phi0 < 0 ) phi0 += 2*pi;
362 r0 = sqrt ( lHit->GetX() * lHit->GetX() + lHit->GetY() * lHit->GetY() ) ;
363 fTrack->SetPhi0(phi0);
367 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
368 psi = psi + q * 0.5F * pi ;
369 if ( psi < 0 ) psi = psi + 2*pi;
371 pt = (Double_t)(BFACT * bField * radius ) ;
376 // Get errors from fast fit
378 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
384 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
385 // Fit Line in s-z plane
386 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
387 Int_t AliL3ConfMapFit::FitLine ( )
398 //find sum , sums ,sumz, sumss
401 Double_t radius = (Double_t)(fTrack->GetPt() / ( BFACT * bField ) ) ;
403 //TObjArray *hits = fTrack->GetHits();
404 //Int_t num_of_hits = fTrack->GetNumberOfPoints();
406 if ( fTrack->ComesFromMainVertex() == true )
408 dx = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetX() - fVertex->GetX();
409 dy = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetY() - fVertex->GetY() ;
413 dx = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetX() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetX() ;
414 dy = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetY() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetY() ;
415 //dx = ((AliL3ConfMapPoint *)hits->First())->GetX() - ((AliL3ConfMapPoint *)hits->Last())->GetX() ;
416 //dy = ((AliL3ConfMapPoint *)hits->First())->GetY() - ((AliL3ConfMapPoint *)hits->Last())->GetY() ;
419 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
422 if ( fabs(localPsi) < 1. )
424 total_s = 2.0 * radius * asin ( localPsi ) ;
428 total_s = 2.0 * radius * pi ;
431 AliL3ConfMapPoint *previousHit = NULL;
433 // FtfBaseHit *previousHit = 0 ;
435 //for ( startLoop() ; done() ; nextHit() ) {
438 // for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
439 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
441 // AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
442 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
443 // if ( currentHit != firstHit )
444 if(cHit != fTrack->firstHit)// hits->First())
446 dx = cHit->GetX() - previousHit->GetX() ;
447 dy = cHit->GetY() - previousHit->GetY() ;
448 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
449 fTrack->SetPsierr(dpsi);
450 s = previousHit->GetS() - 2.0 * radius * (Double_t)asin ( dpsi ) ;
455 // cHit->s = total_s ;
457 sum += cHit->GetZWeight() ;
458 ss += cHit->GetZWeight() * cHit->GetS() ;
459 sz += cHit->GetZWeight() * cHit->GetZ() ;
460 sss += cHit->GetZWeight() * cHit->GetS() * cHit->GetS() ;
461 ssz += cHit->GetZWeight() * cHit->GetS() * cHit->GetZ() ;
465 Double_t chi2,det = sum * sss - ss * ss;
466 if ( fabs(det) < 1e-20)
469 fTrack->SetChiSq2(chi2);
473 //Compute the best fitted parameters A,B
474 Double_t tanl,z0,dtanl,dz0;
476 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
477 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
479 fTrack->SetTgl(tanl);
482 // calculate chi-square
487 //for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
488 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
490 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
491 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
492 r1 = cHit->GetZ() - tanl * cHit->GetS() - z0 ;
493 chi2 += (Double_t) ( (Double_t)cHit->GetZWeight() * (r1 * r1) );
495 fTrack->SetChiSq2(chi2);
497 // calculate estimated variance
498 // varsq=chi/(double(n)-2.)
499 // calculate covariance matrix
500 // siga=sqrt(varsq*sxx/det)
501 // sigb=sqrt(varsq*sum/det)
503 dtanl = (Double_t) ( sum / det );
504 dz0 = (Double_t) ( sss / det );
506 fTrack->SetTglerr(dtanl);
507 fTrack->SetZ0err(dz0);