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)
27 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 //Set the first point on the track to the space point coordinates of the innermost track
347 //This will be updated to lie on the fit later on (AliL3Track::UpdateToFirstPoint).
348 Double_t x0,y0,psi,pt ;
349 AliL3ConfMapPoint *lHit = (AliL3ConfMapPoint*)fTrack->lastHit;
352 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
354 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
355 psi = psi + q * AliL3Transform::PiHalf();
356 if ( psi < 0 ) psi = psi + AliL3Transform::TwoPi();
357 pt = (Double_t)(AliL3Transform::GetBFieldValue() * radius ) ;
359 //Update the track parameters with the parameters from this fit:
362 fTrack->SetRadius(radius);
363 fTrack->SetCenterX(acent);
364 fTrack->SetCenterY(bcent);
367 // Get errors from fast fit
369 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
375 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
376 // Fit Line in s-z plane
377 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
378 Int_t AliL3ConfMapFit::FitLine ( )
389 //find sum , sums ,sumz, sumss
392 Double_t radius = (Double_t)(fTrack->GetPt() / AliL3Transform::GetBFieldValue() ) ;
394 //TObjArray *hits = fTrack->GetHits();
395 //Int_t num_of_hits = fTrack->GetNumberOfPoints();
397 if (0)// fTrack->ComesFromMainVertex() == true )
399 dx = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetX() - fVertex->GetX();
400 dy = ((AliL3ConfMapPoint*)fTrack->firstHit)->GetY() - fVertex->GetY() ;
404 dx = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetX() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetX() ;
405 dy = ((AliL3ConfMapPoint *)fTrack->firstHit)->GetY() - ((AliL3ConfMapPoint *)fTrack->lastHit)->GetY() ;
406 //dx = ((AliL3ConfMapPoint *)hits->First())->GetX() - ((AliL3ConfMapPoint *)hits->Last())->GetX() ;
407 //dy = ((AliL3ConfMapPoint *)hits->First())->GetY() - ((AliL3ConfMapPoint *)hits->Last())->GetY() ;
410 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
413 if ( fabs(localPsi) < 1. )
415 total_s = 2.0 * radius * asin ( localPsi ) ;
419 total_s = 2.0 * radius * AliL3Transform::Pi() ;
422 AliL3ConfMapPoint *previousHit = NULL;
424 // FtfBaseHit *previousHit = 0 ;
426 //for ( startLoop() ; done() ; nextHit() ) {
429 // for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
430 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
432 // AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
433 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
434 // if ( currentHit != firstHit )
435 if(cHit != fTrack->firstHit)// hits->First())
437 dx = cHit->GetX() - previousHit->GetX() ;
438 dy = cHit->GetY() - previousHit->GetY() ;
439 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
440 fTrack->SetPsierr(dpsi);
441 s = previousHit->GetS() - 2.0 * radius * (Double_t)asin ( dpsi ) ;
446 // cHit->s = total_s ;
448 sum += cHit->GetZWeight() ;
449 ss += cHit->GetZWeight() * cHit->GetS() ;
450 sz += cHit->GetZWeight() * cHit->GetZ() ;
451 sss += cHit->GetZWeight() * cHit->GetS() * cHit->GetS() ;
452 ssz += cHit->GetZWeight() * cHit->GetS() * cHit->GetZ() ;
456 Double_t chi2,det = sum * sss - ss * ss;
457 if ( fabs(det) < 1e-20)
460 fTrack->SetChiSq2(chi2);
464 //Compute the best fitted parameters A,B
465 Double_t tanl,z0,dtanl,dz0;
467 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
468 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
470 fTrack->SetTgl(tanl);
473 // calculate chi-square
478 //for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
479 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
481 //AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)hits->At(hit_counter);
482 AliL3ConfMapPoint *cHit = (AliL3ConfMapPoint*)fTrack->currentHit;
483 r1 = cHit->GetZ() - tanl * cHit->GetS() - z0 ;
484 chi2 += (Double_t) ( (Double_t)cHit->GetZWeight() * (r1 * r1) );
486 fTrack->SetChiSq2(chi2);
488 // calculate estimated variance
489 // varsq=chi/(double(n)-2.)
490 // calculate covariance matrix
491 // siga=sqrt(varsq*sxx/det)
492 // sigb=sqrt(varsq*sum/det)
494 dtanl = (Double_t) ( sum / det );
495 dz0 = (Double_t) ( sss / det );
497 fTrack->SetTglerr(dtanl);
498 fTrack->SetZ0err(dz0);