3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliHLTTPCRootTypes.h"
7 #include "AliHLTTPCLogging.h"
8 #include "AliHLTTPCVertex.h"
9 #include "AliHLTTPCConfMapTrack.h"
10 #include "AliHLTTPCConfMapPoint.h"
11 #include "AliHLTTPCTransform.h"
12 #include "AliHLTTPCConfMapFit.h"
14 /** \class AliHLTTPCConfMapFit
16 //_____________________________________________________________
17 // AliHLTTPCConfMapFit
19 // Fit class for conformal mapping tracking
27 ClassImp(AliHLTTPCConfMapFit)
30 AliHLTTPCConfMapFit::AliHLTTPCConfMapFit(AliHLTTPCConfMapTrack *track,AliHLTTPCVertex *vertex)
37 Int_t AliHLTTPCConfMapFit::FitHelix()
42 LOG(AliHLTTPCLog::kError,"AliHLTTPCConfMapFit::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
43 "Problems during circle fit"<<ENDLOG;
48 LOG(AliHLTTPCLog::kError,"AliHLTTPCConfMapFit::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
49 "Problems during line fit"<<ENDLOG;
55 Int_t AliHLTTPCConfMapFit::FitCircle()
57 //-----------------------------------------------------------------
58 //Fits circle parameters using algorithm
59 //described by ChErnov and Oskov in Computer Physics
62 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
63 //Moved to C by Pablo Yepes
64 //Moved to AliROOT by ASV.
65 //------------------------------------------------------------------
71 Int_t num_of_hits = fTrack->GetNumberOfPoints();
73 // Loop over hits calculating average
76 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
79 AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)fTrack->GetCurrentHit();
80 cHit->SetXYWeight( 1./ (Double_t)(cHit->GetXerr()*cHit->GetXerr() + cHit->GetYerr()*cHit->GetYerr()) );
81 wsum += cHit->GetXYWeight() ;
82 xav += cHit->GetXYWeight() * cHit->GetX() ;
83 yav += cHit->GetXYWeight() * cHit->GetY() ;
86 LOG(AliHLTTPCLog::kError,"AliHLTTPCConfMapFit::FitCircle","TrackFit")<<AliHLTTPCLog::kDec<<
87 "Mismatch of hits. Counter: "<<co<<" nHits: "<<num_of_hits<<ENDLOG;
88 if (fTrack->ComesFromMainVertex() == true)
90 wsum += fVertex->GetXYWeight() ;
91 xav += fVertex->GetX() ;
92 yav += fVertex->GetY() ;
98 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
100 Double_t xxav = 0.0 ;
101 Double_t xyav = 0.0 ;
102 Double_t yyav = 0.0 ;
105 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
107 //AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint *)hits->At(hit_counter);
108 AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)fTrack->GetCurrentHit();
109 xi = cHit->GetX() - xav ;
110 yi = cHit->GetY() - yav ;
111 xxav += xi * xi * cHit->GetXYWeight() ;
112 xyav += xi * yi * cHit->GetXYWeight() ;
113 yyav += yi * yi * cHit->GetXYWeight() ;
116 if (fTrack->ComesFromMainVertex() == true)
118 xi = fVertex->GetX() - xav ;
119 yi = fVertex->GetY() - yav ;
120 xxav += xi * xi * fVertex->GetXYWeight() ;
121 xyav += xi * yi * fVertex->GetXYWeight() ;
122 yyav += yi * yi * fVertex->GetXYWeight() ;
128 //--> ROTATE COORDINATES SO THAT <XY> = 0
130 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
131 //--> & > ==> NEW : (XXAV-YYAV) > 0
132 //--> SIGN(S) = SIGN(XYAV) >
134 Double_t a = fabs( xxav - yyav ) ;
135 Double_t b = 4.0 * xyav * xyav ;
137 Double_t asqpb = a * a + b ;
138 Double_t rasqpb = sqrt ( asqpb) ;
140 Double_t splus = 1.0 + a / rasqpb ;
141 Double_t sminus = b / (asqpb * splus) ;
143 splus = sqrt (0.5 * splus ) ;
144 sminus = sqrt (0.5 * sminus) ;
146 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
148 Double_t sinrot, cosrot ;
149 if ( xxav <= yyav ) {
158 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
160 if ( xyav < 0.0 ) sinrot = - sinrot ;
162 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
163 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
164 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
165 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
167 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
169 if ( cosrot*xav+sinrot*yav < 0.0 ) {
174 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
176 Double_t rrav = xxav + yyav ;
177 Double_t rscale = sqrt(rrav) ;
182 Double_t xrrav = 0.0 ;
183 Double_t yrrav = 0.0 ;
184 Double_t rrrrav = 0.0 ;
186 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
188 //for (hit_counter=0; hit_counter<num_of_hits; hit_counter++)
189 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
191 //AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)hits->At(hit_counter);
192 AliHLTTPCConfMapPoint* cHit = (AliHLTTPCConfMapPoint*)fTrack->GetCurrentHit();
194 xold = cHit->GetX() - xav ;
195 yold = cHit->GetY() - yav ;
197 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
199 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
200 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
205 wiriri = cHit->GetXYWeight() * riri ;
207 xyav += cHit->GetXYWeight() * xi * yi ;
208 xxav += cHit->GetXYWeight() * xixi ;
209 yyav += cHit->GetXYWeight() * yiyi ;
211 xrrav += wiriri * xi ;
212 yrrav += wiriri * yi ;
213 rrrrav += wiriri * riri ;
216 // Include vertex if required
218 if (fTrack->ComesFromMainVertex() == true)
220 xold = fVertex->GetX() - xav ;
221 yold = fVertex->GetY() - yav ;
223 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
225 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
226 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
231 wiriri = fVertex->GetXYWeight() * riri ;
233 xyav += fVertex->GetXYWeight() * xi * yi ;
234 xxav += fVertex->GetXYWeight() * xixi ;
235 yyav += fVertex->GetXYWeight() * yiyi ;
237 xrrav += wiriri * xi ;
238 yrrav += wiriri * yi ;
239 rrrrav += wiriri * riri ;
244 //--> DIVIDE BY WSUM TO MAKE AVERAGES
248 xrrav = xrrav / wsum ;
249 yrrav = yrrav / wsum ;
250 rrrrav = rrrrav / wsum ;
253 Int_t const ntry = 5 ;
255 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
256 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
258 Double_t xrrxrr = xrrav * xrrav ;
259 Double_t yrryrr = yrrav * yrrav ;
260 Double_t rrrrm1 = rrrrav - 1.0 ;
261 Double_t xxyy = xxav * yyav ;
263 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
264 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
265 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
266 Double_t c4 = - 4.0 ;
268 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
270 Double_t c2d = 2.0 * c2 ;
271 Double_t c4d = 4.0 * c4 ;
273 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
275 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
276 Double_t lamda = 0.0 ;
277 Double_t dlamda = 0.0 ;
279 Double_t chiscl = wsum * rscale * rscale ;
280 Double_t dlamax = 0.001 / chiscl ;
283 for ( int itry = 1 ; itry <= ntry ; itry++ ) {
284 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
285 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
287 lamda = lamda + dlamda ;
288 if (fabs(dlamda)< dlamax) break ;
291 Double_t chi2 = (Double_t)(chiscl * lamda) ;
293 fTrack->SetChiSq1(chi2);
294 // Double_t dchisq = chiscl * dlamda ;
296 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
298 Double_t h11 = xxav - lamda ;
299 Double_t h14 = xrrav ;
300 Double_t h22 = yyav - lamda ;
301 Double_t h24 = yrrav ;
302 Double_t h34 = 1.0 + 2.0*lamda ;
303 if ( h11 == 0.0 || h22 == 0.0 ){
304 LOG(AliHLTTPCLog::kError,"AliHLTTPCConfMapFit::FitCircle","TrackFit")<<AliHLTTPCLog::kDec<<
305 "Problems fitting circle"<<ENDLOG;
308 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
310 Double_t ratio, kappa, beta ;
311 if ( fabs(h22) > fabs(h24) ) {
313 rootsq = ratio * ratio + rootsq ;
314 kappa = 1.0 / sqrt(rootsq) ;
315 beta = - ratio * kappa ;
319 rootsq = 1.0 + ratio * ratio * rootsq ;
320 beta = 1.0 / sqrt(rootsq) ;
321 if ( h24 > 0 ) beta = - beta ;
322 kappa = -ratio * beta ;
324 Double_t alpha = - (h14/h11) * kappa ;
326 //--> transform these into the lab coordinate system
327 //--> first get kappa and back to real dimensions
329 Double_t kappa1 = kappa / rscale ;
330 Double_t dbro = 0.5 / kappa1 ;
332 //--> next rotate alpha and beta and scale
334 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
335 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
337 //--> then translate by (xav,yav)
339 Double_t acent = (double)(xav - alphar) ;
340 Double_t bcent = (double)(yav - betar ) ;
341 Double_t radius = (double)dbro ;
345 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
347 fTrack->SetCharge(q);
350 //Set the first point on the track to the space point coordinates of the innermost track
351 //This will be updated to lie on the fit later on (AliHLTTPCTrack::UpdateToFirstPoint).
352 Double_t x0,y0,psi,pt ;
353 AliHLTTPCConfMapPoint *lHit = (AliHLTTPCConfMapPoint*)fTrack->GetLastHit();
356 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
358 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
359 psi = psi + q * AliHLTTPCTransform::PiHalf();
360 if ( psi < 0 ) psi = psi + AliHLTTPCTransform::TwoPi();
361 pt = (Double_t)(AliHLTTPCTransform::GetBFieldValue() * radius ) ;
363 //Update the track parameters with the parameters from this fit:
366 fTrack->SetRadius(radius);
367 fTrack->SetCenterX(acent);
368 fTrack->SetCenterY(bcent);
371 // Get errors from fast fit
373 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
379 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
380 // Fit Line in s-z plane
381 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
382 Int_t AliHLTTPCConfMapFit::FitLine ( )
393 //find sum , sums ,sumz, sumss
396 Double_t radius = (Double_t)(fTrack->GetPt() / AliHLTTPCTransform::GetBFieldValue() ) ;
398 //TObjArray *hits = fTrack->GetHits();
399 //Int_t num_of_hits = fTrack->GetNumberOfPoints();
401 if (0)// fTrack->ComesFromMainVertex() == true )
403 dx = ((AliHLTTPCConfMapPoint*)fTrack->GetFirstHit())->GetX() - fVertex->GetX();
404 dy = ((AliHLTTPCConfMapPoint*)fTrack->GetFirstHit())->GetY() - fVertex->GetY() ;
408 dx = ((AliHLTTPCConfMapPoint *)fTrack->GetFirstHit())->GetX() - ((AliHLTTPCConfMapPoint *)fTrack->GetLastHit())->GetX() ;
409 dy = ((AliHLTTPCConfMapPoint *)fTrack->GetFirstHit())->GetY() - ((AliHLTTPCConfMapPoint *)fTrack->GetLastHit())->GetY() ;
410 //dx = ((AliHLTTPCConfMapPoint *)hits->First())->GetX() - ((AliHLTTPCConfMapPoint *)hits->Last())->GetX() ;
411 //dy = ((AliHLTTPCConfMapPoint *)hits->First())->GetY() - ((AliHLTTPCConfMapPoint *)hits->Last())->GetY() ;
414 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
417 if ( fabs(localPsi) < 1. )
419 total_s = 2.0 * radius * asin ( localPsi ) ;
423 total_s = 2.0 * radius * AliHLTTPCTransform::Pi() ;
426 AliHLTTPCConfMapPoint *previousHit = NULL;
428 // FtfBaseHit *previousHit = 0 ;
430 //for ( startLoop() ; done() ; nextHit() ) {
433 // for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
434 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
436 // AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)hits->At(hit_counter);
437 AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)fTrack->GetCurrentHit();
438 // if ( GetCurrentHit() != GetFirstHit() )
439 if(cHit != fTrack->GetFirstHit())// hits->First())
441 dx = cHit->GetX() - previousHit->GetX() ;
442 dy = cHit->GetY() - previousHit->GetY() ;
443 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
444 fTrack->SetPsierr(dpsi);
445 s = previousHit->GetS() - 2.0 * radius * (Double_t)asin ( dpsi ) ;
450 // cHit->s = total_s ;
452 sum += cHit->GetZWeight() ;
453 ss += cHit->GetZWeight() * cHit->GetS() ;
454 sz += cHit->GetZWeight() * cHit->GetZ() ;
455 sss += cHit->GetZWeight() * cHit->GetS() * cHit->GetS() ;
456 ssz += cHit->GetZWeight() * cHit->GetS() * cHit->GetZ() ;
460 Double_t chi2,det = sum * sss - ss * ss;
461 if ( fabs(det) < 1e-20)
464 fTrack->SetChiSq2(chi2);
468 //Compute the best fitted parameters A,B
469 Double_t tanl,z0,dtanl,dz0;
471 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
472 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
474 fTrack->SetTgl(tanl);
477 // calculate chi-square
482 //for(hit_counter=0; hit_counter<num_of_hits; hit_counter++)
483 for(fTrack->StartLoop(); fTrack->LoopDone(); fTrack->GetNextHit())
485 //AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)hits->At(hit_counter);
486 AliHLTTPCConfMapPoint *cHit = (AliHLTTPCConfMapPoint*)fTrack->GetCurrentHit();
487 r1 = cHit->GetZ() - tanl * cHit->GetS() - z0 ;
488 chi2 += (Double_t) ( (Double_t)cHit->GetZWeight() * (r1 * r1) );
490 fTrack->SetChiSq2(chi2);
492 // calculate estimated variance
493 // varsq=chi/(double(n)-2.)
494 // calculate covariance matrix
495 // siga=sqrt(varsq*sxx/det)
496 // sigb=sqrt(varsq*sum/det)
498 dtanl = (Double_t) ( sum / det );
499 dz0 = (Double_t) ( sss / det );
501 fTrack->SetTglerr(dtanl);
502 fTrack->SetZ0err(dz0);