3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 /** \class AliHLTTPCFitter
8 //_____________________________________________________________
11 // Fit class HLT for helix
16 #include "AliHLTTPCLogging.h"
17 #include "AliHLTTPCFitter.h"
18 #include "AliHLTTPCVertex.h"
19 #include "AliHLTTPCTrack.h"
20 #include "AliHLTTPCSpacePointData.h"
21 #include "AliHLTTPCMemHandler.h"
22 #include "AliHLTTPCTransform.h"
23 #include "AliHLTTPC.h"
29 ClassImp(AliHLTTPCFitter)
32 AliHLTTPCFitter::AliHLTTPCFitter()
37 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
40 AliHLTTPCFitter::AliHLTTPCFitter(AliHLTTPCVertex *vertex,Bool_t vertexconstraint)
45 fVertexConstraint=vertexconstraint;
46 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
49 AliHLTTPCFitter::~AliHLTTPCFitter()
52 for(Int_t i=0; i<36; i++)
54 for(Int_t j=0; j<6; j++)
57 delete [] fClusters[i][j];
62 void AliHLTTPCFitter::LoadClusters(Char_t *path,Int_t event,Bool_t sp)
66 AliHLTTPCMemHandler *clusterfile[36][6];
67 for(Int_t s=0; s<=35; s++)
69 for(Int_t p=0; p<6; p++)
77 delete fClusters[s][p];
79 clusterfile[s][p] = new AliHLTTPCMemHandler();
80 sprintf(fname,"%s/points_%d_%d_%d.raw",path,event,s,patch);
81 if(!clusterfile[s][p]->SetBinaryInput(fname))
83 delete clusterfile[s][p];
84 clusterfile[s][p] = 0;
87 fClusters[s][p] = (AliHLTTPCSpacePointData*)clusterfile[s][p]->Allocate();
88 clusterfile[s][p]->Binary2Memory(fNcl[s][p],fClusters[s][p]);
89 clusterfile[s][p]->CloseBinaryInput();
96 void AliHLTTPCFitter::SortTrackClusters(AliHLTTPCTrack *track) const
98 //Sort the internal cluster list in each track with respect to row numbering.
99 //This may be necessary when no conventional track follower has been
100 //applied, in which the cluster list has been maintained in a more
103 Int_t nhits = track->GetNHits();
104 Int_t *ids = (Int_t*)track->GetHitNumbers();
105 Int_t *origids = new Int_t[nhits];
106 Int_t *mk = new Int_t[nhits];
109 for(k=0; k<nhits; k++) {origids[k] = ids[k]; mk[k] = -1;}
111 Int_t slice,patch,id,padrow,maxrow,maxk;
113 for(Int_t j=0; j<nhits; j++)
117 for(k=0; k<nhits; k++)
121 slice = (id>>25) & 0x7f;
122 patch = (id>>22) & 0x7;
124 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
125 padrow = points[pos].fPadRow;
136 for(k=0; k<nhits; k++)
137 ids[k] = origids[mk[k]];
142 Int_t AliHLTTPCFitter::FitHelix(AliHLTTPCTrack *track)
144 //fit helix parameters
148 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
149 "Problems during circle fit"<<ENDLOG;
154 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
155 "Problems during line fit"<<ENDLOG;
161 Int_t AliHLTTPCFitter::FitCircle()
163 //-----------------------------------------------------------------
164 //Fits circle parameters using algorithm
165 //described by ChErnov and Oskov in Computer Physics
168 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
169 //Moved to C by Pablo Yepes
170 //Moved to AliROOT by ASV.
171 //------------------------------------------------------------------
173 Double_t wsum = 0.0 ;
178 // Loop over hits calculating average
179 Double_t * fXYWeight = new Double_t[(fTrack->GetNHits())];
180 UInt_t *hitnum = fTrack->GetHitNumbers();
181 for(Int_t i=0; i<fTrack->GetNHits(); i++)
183 UInt_t id = hitnum[i];
184 Int_t slice = (id>>25) & 0x7f;
185 Int_t patch = (id>>22) & 0x7;
186 UInt_t pos = id&0x3fffff;
187 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
188 fXYWeight[i] = 1./ (Double_t)(points[pos].fSigmaY2 + points[pos].fSigmaY2);
189 wsum += fXYWeight[i];
190 xav += fXYWeight[i]*points[pos].fX;
191 yav += fXYWeight[i]*points[pos].fY;
193 if (fVertexConstraint == kTRUE)
195 wsum += fVertex->GetXYWeight() ;
196 xav += fVertex->GetX() ;
197 yav += fVertex->GetY() ;
203 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
205 Double_t xxav = 0.0 ;
206 Double_t xyav = 0.0 ;
207 Double_t yyav = 0.0 ;
210 for(Int_t i=0; i<fTrack->GetNHits(); i++)
212 UInt_t id = hitnum[i];
213 Int_t slice = (id>>25) & 0x7f;
214 Int_t patch = (id>>22) & 0x7;
215 UInt_t pos = id&0x3fffff;
216 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
218 xi = points[pos].fX -xav;
219 yi = points[pos].fY - yav ;
220 xxav += xi * xi * fXYWeight[i];
221 xyav += xi * yi * fXYWeight[i];
222 yyav += yi * yi * fXYWeight[i];
225 if (fVertexConstraint == kTRUE)
227 xi = fVertex->GetX() - xav ;
228 yi = fVertex->GetY() - yav ;
229 xxav += xi * xi * fVertex->GetXYWeight() ;
230 xyav += xi * yi * fVertex->GetXYWeight() ;
231 yyav += yi * yi * fVertex->GetXYWeight() ;
237 //--> ROTATE COORDINATES SO THAT <XY> = 0
239 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
240 //--> & > ==> NEW : (XXAV-YYAV) > 0
241 //--> SIGN(S) = SIGN(XYAV) >
243 Double_t a = fabs( xxav - yyav ) ;
244 Double_t b = 4.0 * xyav * xyav ;
246 Double_t asqpb = a * a + b ;
247 Double_t rasqpb = sqrt ( asqpb) ;
249 Double_t splus = 1.0 + a / rasqpb ;
250 Double_t sminus = b / (asqpb * splus) ;
252 splus = sqrt (0.5 * splus ) ;
253 sminus = sqrt (0.5 * sminus) ;
255 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
257 Double_t sinrot, cosrot ;
258 if ( xxav <= yyav ) {
267 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
269 if ( xyav < 0.0 ) sinrot = - sinrot ;
271 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
272 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
273 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
274 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
276 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
278 if ( cosrot*xav+sinrot*yav < 0.0 ) {
283 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
285 Double_t rrav = xxav + yyav ;
286 Double_t rscale = sqrt(rrav) ;
291 Double_t xrrav = 0.0 ;
292 Double_t yrrav = 0.0 ;
293 Double_t rrrrav = 0.0 ;
295 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
297 for(Int_t i=0; i<fTrack->GetNHits(); i++)
299 UInt_t id = hitnum[i];
300 Int_t slice = (id>>25) & 0x7f;
301 Int_t patch = (id>>22) & 0x7;
302 UInt_t pos = id&0x3fffff;
303 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
305 xold = points[pos].fX - xav ;
306 yold = points[pos].fY - yav ;
308 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
310 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
311 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
316 wiriri = fXYWeight[i] * riri ;
318 xyav += fXYWeight[i] * xi * yi ;
319 xxav += fXYWeight[i] * xixi ;
320 yyav += fXYWeight[i] * yiyi ;
322 xrrav += wiriri * xi ;
323 yrrav += wiriri * yi ;
324 rrrrav += wiriri * riri ;
327 // Include vertex if required
329 if (fVertexConstraint == kTRUE)
331 xold = fVertex->GetX() - xav ;
332 yold = fVertex->GetY() - yav ;
334 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
336 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
337 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
342 wiriri = fVertex->GetXYWeight() * riri ;
344 xyav += fVertex->GetXYWeight() * xi * yi ;
345 xxav += fVertex->GetXYWeight() * xixi ;
346 yyav += fVertex->GetXYWeight() * yiyi ;
348 xrrav += wiriri * xi ;
349 yrrav += wiriri * yi ;
350 rrrrav += wiriri * riri ;
355 //--> DIVIDE BY WSUM TO MAKE AVERAGES
359 xrrav = xrrav / wsum ;
360 yrrav = yrrav / wsum ;
361 rrrrav = rrrrav / wsum ;
364 Int_t const kntry = 5 ;
366 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
367 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
369 Double_t xrrxrr = xrrav * xrrav ;
370 Double_t yrryrr = yrrav * yrrav ;
371 Double_t rrrrm1 = rrrrav - 1.0 ;
372 Double_t xxyy = xxav * yyav ;
374 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
375 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
376 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
377 Double_t c4 = - 4.0 ;
379 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
381 Double_t c2d = 2.0 * c2 ;
382 Double_t c4d = 4.0 * c4 ;
384 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
386 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
387 Double_t lamda = 0.0 ;
388 Double_t dlamda = 0.0 ;
390 Double_t chiscl = wsum * rscale * rscale ;
391 Double_t dlamax = 0.001 / chiscl ;
394 for ( int itry = 1 ; itry <= kntry ; itry++ ) {
395 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
396 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
398 lamda = lamda + dlamda ;
399 if (fabs(dlamda)< dlamax) break ;
402 //Double_t chi2 = (Double_t)(chiscl * lamda) ;
403 //fTrack->SetChiSq1(chi2);
404 // Double_t dchisq = chiscl * dlamda ;
406 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
408 Double_t h11 = xxav - lamda ;
409 Double_t h14 = xrrav ;
410 Double_t h22 = yyav - lamda ;
411 Double_t h24 = yrrav ;
412 Double_t h34 = 1.0 + 2.0*lamda ;
413 if ( h11 == 0.0 || h22 == 0.0 ){
414 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitCircle","TrackFit")<<AliHLTTPCLog::kDec<<
415 "Problems fitting circle"<<ENDLOG;
418 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
420 Double_t ratio, kappa, beta ;
421 if ( fabs(h22) > fabs(h24) ) {
423 rootsq = ratio * ratio + rootsq ;
424 kappa = 1.0 / sqrt(rootsq) ;
425 beta = - ratio * kappa ;
429 rootsq = 1.0 + ratio * ratio * rootsq ;
430 beta = 1.0 / sqrt(rootsq) ;
431 if ( h24 > 0 ) beta = - beta ;
432 kappa = -ratio * beta ;
434 Double_t alpha = - (h14/h11) * kappa ;
436 //--> transform these into the lab coordinate system
437 //--> first get kappa and back to real dimensions
439 Double_t kappa1 = kappa / rscale ;
440 Double_t dbro = 0.5 / kappa1 ;
442 //--> next rotate alpha and beta and scale
444 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
445 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
447 //--> then translate by (xav,yav)
449 Double_t acent = (double)(xav - alphar) ;
450 Double_t bcent = (double)(yav - betar ) ;
451 Double_t radius = (double)dbro ;
455 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
456 fTrack->SetCharge(q);
458 //Set the first point on the track to the space point coordinates of the innermost track
459 //This will be updated to lie on the fit later on (AliHLTTPCTrack::UpdateToFirstPoint).
460 Double_t x0,y0,psi,pt ;
461 Int_t lastid=fTrack->GetNHits()-1;
462 UInt_t id = hitnum[lastid];
463 Int_t slice = (id>>25) & 0x7f;
464 Int_t patch = (id>>22) & 0x7;
465 UInt_t pos = id&0x3fffff;
466 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
469 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
471 //Set the remaining fit parameters
472 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
473 psi = psi + q * 0.5F * AliHLTTPCTransform::Pi() ;
474 if ( psi < 0 ) psi = psi + 2*AliHLTTPCTransform::Pi();
476 pt = (Double_t)(AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() * radius ) ;
479 fTrack->SetRadius(radius);
480 fTrack->SetCenterX(acent);
481 fTrack->SetCenterY(bcent);
483 // Get errors from fast fit
485 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
491 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
492 // Fit Line in s-z plane
493 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
494 Int_t AliHLTTPCFitter::FitLine ( )
505 //find sum , sums ,sumz, sumss
508 Double_t radius = (Double_t)(fTrack->GetPt() / ( AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() ) ) ;
510 Double_t * fS = new Double_t[(fTrack->GetNHits())];
511 Double_t *fZWeight = new Double_t[fTrack->GetNHits()];
512 UInt_t *hitnum = fTrack->GetHitNumbers();
513 if (0)//fVertexConstraint==kTRUE)
515 UInt_t id = hitnum[0];
516 Int_t slice = (id>>25) & 0x7f;
517 Int_t patch = (id>>22) & 0x7;
518 UInt_t pos = id&0x3fffff;
519 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
521 dx = points[pos].fX - fVertex->GetX();
522 dy = points[pos].fY - fVertex->GetY();
526 UInt_t id = hitnum[0];
527 Int_t slice = (id>>25) & 0x7f;
528 Int_t patch = (id>>22) & 0x7;
529 UInt_t posf = id&0x3fffff;
530 AliHLTTPCSpacePointData *pointsf = fClusters[slice][patch];
531 id = hitnum[(fTrack->GetNHits()-1)];
532 slice = (id>>25) & 0x7f;
533 patch = (id>>22) & 0x7;
534 UInt_t posl = id&0x3fffff;
535 AliHLTTPCSpacePointData *pointsl = fClusters[slice][patch];
536 dx = pointsf[posf].fX - pointsl[posl].fX;
537 dy = pointsf[posf].fY - pointsl[posl].fY;
540 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
543 if ( fabs(localPsi) < 1. )
545 totals = 2.0 * radius * asin ( localPsi ) ;
549 totals = 2.0 * radius * AliHLTTPCTransform::Pi() ;
554 for(Int_t i=0; i<fTrack->GetNHits(); i++)
556 UInt_t id = hitnum[i];
557 Int_t slice = (id>>25) & 0x7f;
558 Int_t patch = (id>>22) & 0x7;
559 UInt_t pos = id&0x3fffff;
560 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
562 fZWeight[i] = 1./(Double_t)(points[pos].fSigmaZ2);
566 slice = (id>>25) & 0x7f;
567 patch = (id>>22) & 0x7;
568 UInt_t lastpos = id&0x3fffff;
569 AliHLTTPCSpacePointData *lastpoints = fClusters[slice][patch];
570 dx = points[pos].fX -lastpoints[lastpos].fX;
571 dy = points[pos].fY -lastpoints[lastpos].fY;
572 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
575 fTrack->SetPsierr(dpsi);
576 s = fS[i-1] - 2.0 * radius * (Double_t)asin ( dpsi ) ;
583 ss += fZWeight[i] * fS[i];
584 sz += fZWeight[i] * points[pos].fZ;
585 sss += fZWeight[i] * fS[i] * fS[i];
586 ssz += fZWeight[i] * fS[i] * points[pos].fZ;
591 Double_t chi2,det = sum * sss - ss * ss;
592 if ( fabs(det) < 1e-20)
595 //fTrack->SetChiSq2(chi2);
599 //Compute the best fitted parameters A,B
600 Double_t tanl,z0,dtanl,dz0;
602 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
603 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
605 fTrack->SetTgl(tanl);
608 //calculate chi-square
612 for(Int_t i=0; i<fTrack->GetNHits(); i++)
614 UInt_t id = hitnum[i];
615 Int_t slice = (id>>25) & 0x7f;
616 Int_t patch = (id>>22) & 0x7;
617 UInt_t pos = id&0x3fffff;
618 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
619 r1 = points[pos].fZ - tanl * fS[i] - z0 ;
620 chi2 += (Double_t) ( (Double_t)(fZWeight[i]) * (r1 * r1) );
623 //fTrack->SetChiSq2(chi2);
625 //calculate estimated variance
626 //varsq=chi/(double(n)-2.)
627 //calculate covariance matrix
628 //siga=sqrt(varsq*sxx/det)
629 //sigb=sqrt(varsq*sum/det)
631 dtanl = (Double_t) ( sum / det );
632 dz0 = (Double_t) ( sss / det );
634 fTrack->SetTglerr(dtanl);
635 fTrack->SetZ0err(dz0);