3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
8 //_____________________________________________________________
11 // Fit class HLT for helix
16 #include "AliL3StandardIncludes.h"
17 #include "AliL3Logging.h"
18 #include "AliL3Fitter.h"
19 #include "AliL3Vertex.h"
20 #include "AliL3Track.h"
21 #include "AliL3SpacePointData.h"
22 #include "AliL3MemHandler.h"
23 #include "AliL3Transform.h"
24 #include "AliLevel3.h"
33 AliL3Fitter::AliL3Fitter()
38 memset(fClusters,0,36*6*sizeof(AliL3SpacePointData*));
41 AliL3Fitter::AliL3Fitter(AliL3Vertex *vertex,Bool_t vertexconstraint)
46 fVertexConstraint=vertexconstraint;
47 memset(fClusters,0,36*6*sizeof(AliL3SpacePointData*));
50 AliL3Fitter::~AliL3Fitter()
53 for(Int_t i=0; i<36; i++)
55 for(Int_t j=0; j<6; j++)
58 delete [] fClusters[i][j];
63 void AliL3Fitter::LoadClusters(Char_t *path,Int_t event,Bool_t sp)
67 AliL3MemHandler *clusterfile[36][6];
68 for(Int_t s=0; s<=35; s++)
70 for(Int_t p=0; p<6; p++)
78 delete fClusters[s][p];
80 clusterfile[s][p] = new AliL3MemHandler();
81 sprintf(fname,"%s/points_%d_%d_%d.raw",path,event,s,patch);
82 if(!clusterfile[s][p]->SetBinaryInput(fname))
84 delete clusterfile[s][p];
85 clusterfile[s][p] = 0;
88 fClusters[s][p] = (AliL3SpacePointData*)clusterfile[s][p]->Allocate();
89 clusterfile[s][p]->Binary2Memory(fNcl[s][p],fClusters[s][p]);
90 clusterfile[s][p]->CloseBinaryInput();
97 void AliL3Fitter::SortTrackClusters(AliL3Track *track) const
99 //Sort the internal cluster list in each track with respect to row numbering.
100 //This may be necessary when no conventional track follower has been
101 //applied, in which the cluster list has been maintained in a more
104 Int_t nhits = track->GetNHits();
105 Int_t *ids = (Int_t*)track->GetHitNumbers();
106 Int_t *origids = new Int_t[nhits];
107 Int_t *mk = new Int_t[nhits];
110 for(k=0; k<nhits; k++) {origids[k] = ids[k]; mk[k] = -1;}
112 Int_t slice,patch,id,padrow,maxrow,maxk;
114 for(Int_t j=0; j<nhits; j++)
118 for(k=0; k<nhits; k++)
122 slice = (id>>25) & 0x7f;
123 patch = (id>>22) & 0x7;
125 AliL3SpacePointData *points = fClusters[slice][patch];
126 padrow = points[pos].fPadRow;
137 for(k=0; k<nhits; k++)
138 ids[k] = origids[mk[k]];
143 Int_t AliL3Fitter::FitHelix(AliL3Track *track)
145 //fit helix parameters
149 LOG(AliL3Log::kError,"AliL3Fitter::FitHelix","TrackFit")<<AliL3Log::kDec<<
150 "Problems during circle fit"<<ENDLOG;
155 LOG(AliL3Log::kError,"AliL3Fitter::FitHelix","TrackFit")<<AliL3Log::kDec<<
156 "Problems during line fit"<<ENDLOG;
162 Int_t AliL3Fitter::FitCircle()
164 //-----------------------------------------------------------------
165 //Fits circle parameters using algorithm
166 //described by ChErnov and Oskov in Computer Physics
169 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
170 //Moved to C by Pablo Yepes
171 //Moved to AliROOT by ASV.
172 //------------------------------------------------------------------
174 Double_t wsum = 0.0 ;
179 // Loop over hits calculating average
180 Double_t * fXYWeight = new Double_t[(fTrack->GetNHits())];
181 UInt_t *hitnum = fTrack->GetHitNumbers();
182 for(Int_t i=0; i<fTrack->GetNHits(); i++)
184 UInt_t id = hitnum[i];
185 Int_t slice = (id>>25) & 0x7f;
186 Int_t patch = (id>>22) & 0x7;
187 UInt_t pos = id&0x3fffff;
188 AliL3SpacePointData *points = fClusters[slice][patch];
189 fXYWeight[i] = 1./ (Double_t)(points[pos].fSigmaY2 + points[pos].fSigmaY2);
190 wsum += fXYWeight[i];
191 xav += fXYWeight[i]*points[pos].fX;
192 yav += fXYWeight[i]*points[pos].fY;
194 if (fVertexConstraint == kTRUE)
196 wsum += fVertex->GetXYWeight() ;
197 xav += fVertex->GetX() ;
198 yav += fVertex->GetY() ;
204 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
206 Double_t xxav = 0.0 ;
207 Double_t xyav = 0.0 ;
208 Double_t yyav = 0.0 ;
211 for(Int_t i=0; i<fTrack->GetNHits(); i++)
213 UInt_t id = hitnum[i];
214 Int_t slice = (id>>25) & 0x7f;
215 Int_t patch = (id>>22) & 0x7;
216 UInt_t pos = id&0x3fffff;
217 AliL3SpacePointData *points = fClusters[slice][patch];
219 xi = points[pos].fX -xav;
220 yi = points[pos].fY - yav ;
221 xxav += xi * xi * fXYWeight[i];
222 xyav += xi * yi * fXYWeight[i];
223 yyav += yi * yi * fXYWeight[i];
226 if (fVertexConstraint == kTRUE)
228 xi = fVertex->GetX() - xav ;
229 yi = fVertex->GetY() - yav ;
230 xxav += xi * xi * fVertex->GetXYWeight() ;
231 xyav += xi * yi * fVertex->GetXYWeight() ;
232 yyav += yi * yi * fVertex->GetXYWeight() ;
238 //--> ROTATE COORDINATES SO THAT <XY> = 0
240 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
241 //--> & > ==> NEW : (XXAV-YYAV) > 0
242 //--> SIGN(S) = SIGN(XYAV) >
244 Double_t a = fabs( xxav - yyav ) ;
245 Double_t b = 4.0 * xyav * xyav ;
247 Double_t asqpb = a * a + b ;
248 Double_t rasqpb = sqrt ( asqpb) ;
250 Double_t splus = 1.0 + a / rasqpb ;
251 Double_t sminus = b / (asqpb * splus) ;
253 splus = sqrt (0.5 * splus ) ;
254 sminus = sqrt (0.5 * sminus) ;
256 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
258 Double_t sinrot, cosrot ;
259 if ( xxav <= yyav ) {
268 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
270 if ( xyav < 0.0 ) sinrot = - sinrot ;
272 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
273 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
274 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
275 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
277 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
279 if ( cosrot*xav+sinrot*yav < 0.0 ) {
284 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
286 Double_t rrav = xxav + yyav ;
287 Double_t rscale = sqrt(rrav) ;
292 Double_t xrrav = 0.0 ;
293 Double_t yrrav = 0.0 ;
294 Double_t rrrrav = 0.0 ;
296 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
298 for(Int_t i=0; i<fTrack->GetNHits(); i++)
300 UInt_t id = hitnum[i];
301 Int_t slice = (id>>25) & 0x7f;
302 Int_t patch = (id>>22) & 0x7;
303 UInt_t pos = id&0x3fffff;
304 AliL3SpacePointData *points = fClusters[slice][patch];
306 xold = points[pos].fX - xav ;
307 yold = points[pos].fY - yav ;
309 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
311 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
312 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
317 wiriri = fXYWeight[i] * riri ;
319 xyav += fXYWeight[i] * xi * yi ;
320 xxav += fXYWeight[i] * xixi ;
321 yyav += fXYWeight[i] * yiyi ;
323 xrrav += wiriri * xi ;
324 yrrav += wiriri * yi ;
325 rrrrav += wiriri * riri ;
328 // Include vertex if required
330 if (fVertexConstraint == kTRUE)
332 xold = fVertex->GetX() - xav ;
333 yold = fVertex->GetY() - yav ;
335 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
337 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
338 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
343 wiriri = fVertex->GetXYWeight() * riri ;
345 xyav += fVertex->GetXYWeight() * xi * yi ;
346 xxav += fVertex->GetXYWeight() * xixi ;
347 yyav += fVertex->GetXYWeight() * yiyi ;
349 xrrav += wiriri * xi ;
350 yrrav += wiriri * yi ;
351 rrrrav += wiriri * riri ;
356 //--> DIVIDE BY WSUM TO MAKE AVERAGES
360 xrrav = xrrav / wsum ;
361 yrrav = yrrav / wsum ;
362 rrrrav = rrrrav / wsum ;
365 Int_t const kntry = 5 ;
367 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
368 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
370 Double_t xrrxrr = xrrav * xrrav ;
371 Double_t yrryrr = yrrav * yrrav ;
372 Double_t rrrrm1 = rrrrav - 1.0 ;
373 Double_t xxyy = xxav * yyav ;
375 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
376 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
377 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
378 Double_t c4 = - 4.0 ;
380 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
382 Double_t c2d = 2.0 * c2 ;
383 Double_t c4d = 4.0 * c4 ;
385 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
387 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
388 Double_t lamda = 0.0 ;
389 Double_t dlamda = 0.0 ;
391 Double_t chiscl = wsum * rscale * rscale ;
392 Double_t dlamax = 0.001 / chiscl ;
395 for ( int itry = 1 ; itry <= kntry ; itry++ ) {
396 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
397 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
399 lamda = lamda + dlamda ;
400 if (fabs(dlamda)< dlamax) break ;
403 //Double_t chi2 = (Double_t)(chiscl * lamda) ;
404 //fTrack->SetChiSq1(chi2);
405 // Double_t dchisq = chiscl * dlamda ;
407 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
409 Double_t h11 = xxav - lamda ;
410 Double_t h14 = xrrav ;
411 Double_t h22 = yyav - lamda ;
412 Double_t h24 = yrrav ;
413 Double_t h34 = 1.0 + 2.0*lamda ;
414 if ( h11 == 0.0 || h22 == 0.0 ){
415 LOG(AliL3Log::kError,"AliL3Fitter::FitCircle","TrackFit")<<AliL3Log::kDec<<
416 "Problems fitting circle"<<ENDLOG;
419 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
421 Double_t ratio, kappa, beta ;
422 if ( fabs(h22) > fabs(h24) ) {
424 rootsq = ratio * ratio + rootsq ;
425 kappa = 1.0 / sqrt(rootsq) ;
426 beta = - ratio * kappa ;
430 rootsq = 1.0 + ratio * ratio * rootsq ;
431 beta = 1.0 / sqrt(rootsq) ;
432 if ( h24 > 0 ) beta = - beta ;
433 kappa = -ratio * beta ;
435 Double_t alpha = - (h14/h11) * kappa ;
437 //--> transform these into the lab coordinate system
438 //--> first get kappa and back to real dimensions
440 Double_t kappa1 = kappa / rscale ;
441 Double_t dbro = 0.5 / kappa1 ;
443 //--> next rotate alpha and beta and scale
445 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
446 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
448 //--> then translate by (xav,yav)
450 Double_t acent = (double)(xav - alphar) ;
451 Double_t bcent = (double)(yav - betar ) ;
452 Double_t radius = (double)dbro ;
456 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
457 fTrack->SetCharge(q);
459 //Set the first point on the track to the space point coordinates of the innermost track
460 //This will be updated to lie on the fit later on (AliL3Track::UpdateToFirstPoint).
461 Double_t x0,y0,psi,pt ;
462 Int_t lastid=fTrack->GetNHits()-1;
463 UInt_t id = hitnum[lastid];
464 Int_t slice = (id>>25) & 0x7f;
465 Int_t patch = (id>>22) & 0x7;
466 UInt_t pos = id&0x3fffff;
467 AliL3SpacePointData *points = fClusters[slice][patch];
470 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
472 //Set the remaining fit parameters
473 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
474 psi = psi + q * 0.5F * AliL3Transform::Pi() ;
475 if ( psi < 0 ) psi = psi + 2*AliL3Transform::Pi();
477 pt = (Double_t)(AliL3Transform::GetBFact() * AliL3Transform::GetBField() * radius ) ;
480 fTrack->SetRadius(radius);
481 fTrack->SetCenterX(acent);
482 fTrack->SetCenterY(bcent);
484 // Get errors from fast fit
486 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
492 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
493 // Fit Line in s-z plane
494 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
495 Int_t AliL3Fitter::FitLine ( )
506 //find sum , sums ,sumz, sumss
509 Double_t radius = (Double_t)(fTrack->GetPt() / ( AliL3Transform::GetBFact() * AliL3Transform::GetBField() ) ) ;
511 Double_t * fS = new Double_t[(fTrack->GetNHits())];
512 Double_t *fZWeight = new Double_t[fTrack->GetNHits()];
513 UInt_t *hitnum = fTrack->GetHitNumbers();
514 if (0)//fVertexConstraint==kTRUE)
516 UInt_t id = hitnum[0];
517 Int_t slice = (id>>25) & 0x7f;
518 Int_t patch = (id>>22) & 0x7;
519 UInt_t pos = id&0x3fffff;
520 AliL3SpacePointData *points = fClusters[slice][patch];
522 dx = points[pos].fX - fVertex->GetX();
523 dy = points[pos].fY - fVertex->GetY();
527 UInt_t id = hitnum[0];
528 Int_t slice = (id>>25) & 0x7f;
529 Int_t patch = (id>>22) & 0x7;
530 UInt_t posf = id&0x3fffff;
531 AliL3SpacePointData *pointsf = fClusters[slice][patch];
532 id = hitnum[(fTrack->GetNHits()-1)];
533 slice = (id>>25) & 0x7f;
534 patch = (id>>22) & 0x7;
535 UInt_t posl = id&0x3fffff;
536 AliL3SpacePointData *pointsl = fClusters[slice][patch];
537 dx = pointsf[posf].fX - pointsl[posl].fX;
538 dy = pointsf[posf].fY - pointsl[posl].fY;
541 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
544 if ( fabs(localPsi) < 1. )
546 totals = 2.0 * radius * asin ( localPsi ) ;
550 totals = 2.0 * radius * AliL3Transform::Pi() ;
555 for(Int_t i=0; i<fTrack->GetNHits(); i++)
557 UInt_t id = hitnum[i];
558 Int_t slice = (id>>25) & 0x7f;
559 Int_t patch = (id>>22) & 0x7;
560 UInt_t pos = id&0x3fffff;
561 AliL3SpacePointData *points = fClusters[slice][patch];
563 fZWeight[i] = 1./(Double_t)(points[pos].fSigmaZ2);
567 slice = (id>>25) & 0x7f;
568 patch = (id>>22) & 0x7;
569 UInt_t lastpos = id&0x3fffff;
570 AliL3SpacePointData *lastpoints = fClusters[slice][patch];
571 dx = points[pos].fX -lastpoints[lastpos].fX;
572 dy = points[pos].fY -lastpoints[lastpos].fY;
573 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
576 fTrack->SetPsierr(dpsi);
577 s = fS[i-1] - 2.0 * radius * (Double_t)asin ( dpsi ) ;
584 ss += fZWeight[i] * fS[i];
585 sz += fZWeight[i] * points[pos].fZ;
586 sss += fZWeight[i] * fS[i] * fS[i];
587 ssz += fZWeight[i] * fS[i] * points[pos].fZ;
592 Double_t chi2,det = sum * sss - ss * ss;
593 if ( fabs(det) < 1e-20)
596 //fTrack->SetChiSq2(chi2);
600 //Compute the best fitted parameters A,B
601 Double_t tanl,z0,dtanl,dz0;
603 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
604 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
606 fTrack->SetTgl(tanl);
609 //calculate chi-square
613 for(Int_t i=0; i<fTrack->GetNHits(); i++)
615 UInt_t id = hitnum[i];
616 Int_t slice = (id>>25) & 0x7f;
617 Int_t patch = (id>>22) & 0x7;
618 UInt_t pos = id&0x3fffff;
619 AliL3SpacePointData *points = fClusters[slice][patch];
620 r1 = points[pos].fZ - tanl * fS[i] - z0 ;
621 chi2 += (Double_t) ( (Double_t)(fZWeight[i]) * (r1 * r1) );
624 //fTrack->SetChiSq2(chi2);
626 //calculate estimated variance
627 //varsq=chi/(double(n)-2.)
628 //calculate covariance matrix
629 //siga=sqrt(varsq*sxx/det)
630 //sigb=sqrt(varsq*sum/det)
632 dtanl = (Double_t) ( sum / det );
633 dz0 = (Double_t) ( sss / det );
635 fTrack->SetTglerr(dtanl);
636 fTrack->SetZ0err(dz0);