3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.h"
9 #include "AliL3Logging.h"
10 #include "AliL3Fitter.h"
11 #include "AliL3Vertex.h"
12 #include "AliL3Track.h"
13 #include "AliL3SpacePointData.h"
14 #include "AliL3MemHandler.h"
15 #include "AliL3Transform.h"
17 /** \class AliL3Fitter
19 //_____________________________________________________________
29 AliL3Fitter::AliL3Fitter()
33 memset(fClusters,0,36*6*sizeof(AliL3SpacePointData*));
36 AliL3Fitter::AliL3Fitter(AliL3Vertex *vertex,Bool_t vertexconstraint)
41 fVertexConstraint=vertexconstraint;
42 memset(fClusters,0,36*6*sizeof(AliL3SpacePointData*));
45 AliL3Fitter::~AliL3Fitter()
47 for(Int_t i=0; i<36; i++)
49 for(Int_t j=0; j<6; j++)
52 delete fClusters[i][j];
57 void AliL3Fitter::LoadClusters(Char_t *path,Int_t event,Bool_t sp)
60 AliL3MemHandler *clusterfile[36][6];
61 for(Int_t s=0; s<=35; s++)
63 for(Int_t p=0; p<6; p++)
71 delete fClusters[s][p];
73 clusterfile[s][p] = new AliL3MemHandler();
74 sprintf(fname,"%s/points_%d_%d_%d.raw",path,event,s,patch);
75 if(!clusterfile[s][p]->SetBinaryInput(fname))
77 delete clusterfile[s][p];
78 clusterfile[s][p] = 0;
81 fClusters[s][p] = (AliL3SpacePointData*)clusterfile[s][p]->Allocate();
82 clusterfile[s][p]->Binary2Memory(fNcl[s][p],fClusters[s][p]);
83 clusterfile[s][p]->CloseBinaryInput();
90 void AliL3Fitter::SortTrackClusters(AliL3Track *track)
92 //Sort the internal cluster list in each track with respect to row numbering.
93 //This may be necessary when no conventional track follower has been
94 //applied, in which the cluster list has been maintained in a more
97 Int_t nhits = track->GetNHits();
98 Int_t *ids = (Int_t*)track->GetHitNumbers();
99 Int_t *origids = new Int_t[nhits];
100 Int_t *mk = new Int_t[nhits];
103 for(k=0; k<nhits; k++) {origids[k] = ids[k]; mk[k] = -1;}
105 Int_t slice,patch,id,padrow,maxrow,maxk;
107 for(Int_t j=0; j<nhits; j++)
111 for(k=0; k<nhits; k++)
115 slice = (id>>25) & 0x7f;
116 patch = (id>>22) & 0x7;
118 AliL3SpacePointData *points = fClusters[slice][patch];
119 padrow = points[pos].fPadRow;
130 for(k=0; k<nhits; k++)
131 ids[k] = origids[mk[k]];
136 void AliL3Fitter::UpdateTrack(AliL3Track *track)
138 //Update the track parameters to the first point on the track.
139 //This function should be called after the track fit has been
140 //done, in order to calculate the track parameters at the
141 //first point on the track.
143 UInt_t *ids = track->GetHitNumbers();
144 Int_t nhits = track->GetNHits();
145 UInt_t id=ids[nhits-1];
146 Int_t slice = (id>>25) & 0x7f;
147 Int_t patch = (id>>22) & 0x7;
148 UInt_t pos = id&0x3fffff;
149 AliL3SpacePointData *points = fClusters[slice][patch];
150 track->SetFirstPoint(points[pos].fX,points[pos].fY,track->GetZ0());
151 track->UpdateToFirstPoint();
154 Int_t AliL3Fitter::FitHelix(AliL3Track *track)
159 LOG(AliL3Log::kError,"AliL3Fitter::FitHelix","TrackFit")<<AliL3Log::kDec<<
160 "Problems during circle fit"<<ENDLOG;
165 LOG(AliL3Log::kError,"AliL3Fitter::FitHelix","TrackFit")<<AliL3Log::kDec<<
166 "Problems during line fit"<<ENDLOG;
172 Int_t AliL3Fitter::FitCircle()
174 //-----------------------------------------------------------------
175 //Fits circle parameters using algorithm
176 //described by ChErnov and Oskov in Computer Physics
179 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
180 //Moved to C by Pablo Yepes
181 //Moved to AliROOT by ASV.
182 //------------------------------------------------------------------
184 Double_t wsum = 0.0 ;
189 // Loop over hits calculating average
190 Double_t fXYWeight[(fTrack->GetNHits())];
191 UInt_t *hitnum = fTrack->GetHitNumbers();
192 for(Int_t i=0; i<fTrack->GetNHits(); i++)
194 UInt_t id = hitnum[i];
195 Int_t slice = (id>>25) & 0x7f;
196 Int_t patch = (id>>22) & 0x7;
197 UInt_t pos = id&0x3fffff;
198 AliL3SpacePointData *points = fClusters[slice][patch];
199 fXYWeight[i] = 1./ (Double_t)(points[pos].fSigmaY2 + points[pos].fSigmaY2);
200 wsum += fXYWeight[i];
201 xav += fXYWeight[i]*points[pos].fX;
202 yav += fXYWeight[i]*points[pos].fY;
205 if (fVertexConstraint == kTRUE)
207 wsum += fVertex->GetXYWeight() ;
208 xav += fVertex->GetX() ;
209 yav += fVertex->GetY() ;
215 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
217 Double_t xxav = 0.0 ;
218 Double_t xyav = 0.0 ;
219 Double_t yyav = 0.0 ;
222 for(Int_t i=0; i<fTrack->GetNHits(); i++)
224 UInt_t id = hitnum[i];
225 Int_t slice = (id>>25) & 0x7f;
226 Int_t patch = (id>>22) & 0x7;
227 UInt_t pos = id&0x3fffff;
228 AliL3SpacePointData *points = fClusters[slice][patch];
230 xi = points[pos].fX -xav;
231 yi = points[pos].fY - yav ;
232 xxav += xi * xi * fXYWeight[i];
233 xyav += xi * yi * fXYWeight[i];
234 yyav += yi * yi * fXYWeight[i];
237 if (fVertexConstraint == kTRUE)
239 xi = fVertex->GetX() - xav ;
240 yi = fVertex->GetY() - yav ;
241 xxav += xi * xi * fVertex->GetXYWeight() ;
242 xyav += xi * yi * fVertex->GetXYWeight() ;
243 yyav += yi * yi * fVertex->GetXYWeight() ;
249 //--> ROTATE COORDINATES SO THAT <XY> = 0
251 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
252 //--> & > ==> NEW : (XXAV-YYAV) > 0
253 //--> SIGN(S) = SIGN(XYAV) >
255 Double_t a = fabs( xxav - yyav ) ;
256 Double_t b = 4.0 * xyav * xyav ;
258 Double_t asqpb = a * a + b ;
259 Double_t rasqpb = sqrt ( asqpb) ;
261 Double_t splus = 1.0 + a / rasqpb ;
262 Double_t sminus = b / (asqpb * splus) ;
264 splus = sqrt (0.5 * splus ) ;
265 sminus = sqrt (0.5 * sminus) ;
267 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
269 Double_t sinrot, cosrot ;
270 if ( xxav <= yyav ) {
279 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
281 if ( xyav < 0.0 ) sinrot = - sinrot ;
283 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
284 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
285 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
286 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
288 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
290 if ( cosrot*xav+sinrot*yav < 0.0 ) {
295 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
297 Double_t rrav = xxav + yyav ;
298 Double_t rscale = sqrt(rrav) ;
303 Double_t xrrav = 0.0 ;
304 Double_t yrrav = 0.0 ;
305 Double_t rrrrav = 0.0 ;
307 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
309 for(Int_t i=0; i<fTrack->GetNHits(); i++)
311 UInt_t id = hitnum[i];
312 Int_t slice = (id>>25) & 0x7f;
313 Int_t patch = (id>>22) & 0x7;
314 UInt_t pos = id&0x3fffff;
315 AliL3SpacePointData *points = fClusters[slice][patch];
317 xold = points[pos].fX - xav ;
318 yold = points[pos].fY - yav ;
320 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
322 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
323 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
328 wiriri = fXYWeight[i] * riri ;
330 xyav += fXYWeight[i] * xi * yi ;
331 xxav += fXYWeight[i] * xixi ;
332 yyav += fXYWeight[i] * yiyi ;
334 xrrav += wiriri * xi ;
335 yrrav += wiriri * yi ;
336 rrrrav += wiriri * riri ;
339 // Include vertex if required
341 if (fVertexConstraint == kTRUE)
343 xold = fVertex->GetX() - xav ;
344 yold = fVertex->GetY() - yav ;
346 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
348 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
349 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
354 wiriri = fVertex->GetXYWeight() * riri ;
356 xyav += fVertex->GetXYWeight() * xi * yi ;
357 xxav += fVertex->GetXYWeight() * xixi ;
358 yyav += fVertex->GetXYWeight() * yiyi ;
360 xrrav += wiriri * xi ;
361 yrrav += wiriri * yi ;
362 rrrrav += wiriri * riri ;
367 //--> DIVIDE BY WSUM TO MAKE AVERAGES
371 xrrav = xrrav / wsum ;
372 yrrav = yrrav / wsum ;
373 rrrrav = rrrrav / wsum ;
376 Int_t const ntry = 5 ;
378 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
379 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
381 Double_t xrrxrr = xrrav * xrrav ;
382 Double_t yrryrr = yrrav * yrrav ;
383 Double_t rrrrm1 = rrrrav - 1.0 ;
384 Double_t xxyy = xxav * yyav ;
386 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
387 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
388 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
389 Double_t c4 = - 4.0 ;
391 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
393 Double_t c2d = 2.0 * c2 ;
394 Double_t c4d = 4.0 * c4 ;
396 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
398 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
399 Double_t lamda = 0.0 ;
400 Double_t dlamda = 0.0 ;
402 Double_t chiscl = wsum * rscale * rscale ;
403 Double_t dlamax = 0.001 / chiscl ;
406 for ( int itry = 1 ; itry <= ntry ; itry++ ) {
407 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
408 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
410 lamda = lamda + dlamda ;
411 if (fabs(dlamda)< dlamax) break ;
414 //Double_t chi2 = (Double_t)(chiscl * lamda) ;
416 //fTrack->SetChiSq1(chi2);
417 // Double_t dchisq = chiscl * dlamda ;
419 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
421 Double_t h11 = xxav - lamda ;
422 Double_t h14 = xrrav ;
423 Double_t h22 = yyav - lamda ;
424 Double_t h24 = yrrav ;
425 Double_t h34 = 1.0 + 2.0*lamda ;
426 if ( h11 == 0.0 || h22 == 0.0 ){
427 LOG(AliL3Log::kError,"AliL3Fitter::FitCircle","TrackFit")<<AliL3Log::kDec<<
428 "Problems fitting circle"<<ENDLOG;
431 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
433 Double_t ratio, kappa, beta ;
434 if ( fabs(h22) > fabs(h24) ) {
436 rootsq = ratio * ratio + rootsq ;
437 kappa = 1.0 / sqrt(rootsq) ;
438 beta = - ratio * kappa ;
442 rootsq = 1.0 + ratio * ratio * rootsq ;
443 beta = 1.0 / sqrt(rootsq) ;
444 if ( h24 > 0 ) beta = - beta ;
445 kappa = -ratio * beta ;
447 Double_t alpha = - (h14/h11) * kappa ;
449 //--> transform these into the lab coordinate system
450 //--> first get kappa and back to real dimensions
452 Double_t kappa1 = kappa / rscale ;
453 Double_t dbro = 0.5 / kappa1 ;
455 //--> next rotate alpha and beta and scale
457 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
458 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
460 //--> then translate by (xav,yav)
462 Double_t acent = (double)(xav - alphar) ;
463 Double_t bcent = (double)(yav - betar ) ;
464 Double_t radius = (double)dbro ;
468 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
470 fTrack->SetCharge(q);
473 // Get other track parameters
475 Double_t x0, y0,phi0,r0,psi,pt ;
476 if ( fVertexConstraint == kTRUE)
478 //flag = 1 ; // primary track flag
479 x0 = fVertex->GetX() ;
480 y0 = fVertex->GetY() ;
481 phi0 = fVertex->GetPhi() ;
482 r0 = fVertex->GetR() ;
483 fTrack->SetPhi0(phi0);
488 Int_t lastid=fTrack->GetNHits()-1;
489 UInt_t id = hitnum[lastid];
490 Int_t slice = (id>>25) & 0x7f;
491 Int_t patch = (id>>22) & 0x7;
492 UInt_t pos = id&0x3fffff;
493 AliL3SpacePointData *points = fClusters[slice][patch];
495 //flag = 0 ; // primary track flag
498 phi0 = atan2(points[pos].fY,points[pos].fX);
499 if ( phi0 < 0 ) phi0 += 2*AliL3Transform::Pi();
500 r0 = sqrt ( points[pos].fX * points[pos].fX + points[pos].fY*points[pos].fY);
501 fTrack->SetPhi0(phi0);
505 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
506 psi = psi + q * 0.5F * AliL3Transform::Pi() ;
507 if ( psi < 0 ) psi = psi + 2*AliL3Transform::Pi();
509 pt = (Double_t)(AliL3Transform::GetBFact() * AliL3Transform::GetBField() * radius ) ;
512 //fTrack->SetFirstPoint(x0,y0,0);
514 // Get errors from fast fit
516 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
522 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
523 // Fit Line in s-z plane
524 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
525 Int_t AliL3Fitter::FitLine ( )
536 //find sum , sums ,sumz, sumss
539 Double_t radius = (Double_t)(fTrack->GetPt() / ( AliL3Transform::GetBFact() * AliL3Transform::GetBField() ) ) ;
541 //TObjArray *hits = fTrack->GetHits();
542 //Int_t num_of_hits = fTrack->GetNumberOfPoints();
544 Double_t fS[(fTrack->GetNHits())];
545 Double_t *fZWeight = new Double_t[fTrack->GetNHits()];
546 UInt_t *hitnum = fTrack->GetHitNumbers();
547 if (fVertexConstraint==kTRUE)
549 UInt_t id = hitnum[0];
550 Int_t slice = (id>>25) & 0x7f;
551 Int_t patch = (id>>22) & 0x7;
552 UInt_t pos = id&0x3fffff;
553 AliL3SpacePointData *points = fClusters[slice][patch];
555 dx = points[pos].fX - fVertex->GetX();
556 dy = points[pos].fY - fVertex->GetY();
560 UInt_t id = hitnum[0];
561 Int_t slice = (id>>25) & 0x7f;
562 Int_t patch = (id>>22) & 0x7;
563 UInt_t posf = id&0x3fffff;
564 AliL3SpacePointData *pointsf = fClusters[slice][patch];
565 id = hitnum[(fTrack->GetNHits()-1)];
566 slice = (id>>25) & 0x7f;
567 patch = (id>>22) & 0x7;
568 UInt_t posl = id&0x3fffff;
569 AliL3SpacePointData *pointsl = fClusters[slice][patch];
570 dx = pointsf[posf].fX - pointsl[posl].fX;
571 dy = pointsf[posf].fY - pointsl[posl].fY;
574 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
577 if ( fabs(localPsi) < 1. )
579 total_s = 2.0 * radius * asin ( localPsi ) ;
583 total_s = 2.0 * radius * AliL3Transform::Pi() ;
588 for(Int_t i=0; i<fTrack->GetNHits(); i++)
590 UInt_t id = hitnum[i];
591 Int_t slice = (id>>25) & 0x7f;
592 Int_t patch = (id>>22) & 0x7;
593 UInt_t pos = id&0x3fffff;
594 AliL3SpacePointData *points = fClusters[slice][patch];
596 fZWeight[i] = 1./(Double_t)(points[pos].fSigmaZ2);
600 slice = (id>>25) & 0x7f;
601 patch = (id>>22) & 0x7;
602 UInt_t lastpos = id&0x3fffff;
603 AliL3SpacePointData *lastpoints = fClusters[slice][patch];
604 dx = points[pos].fX -lastpoints[lastpos].fX;
605 dy = points[pos].fY -lastpoints[lastpos].fY;
606 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
609 fTrack->SetPsierr(dpsi);
610 s = fS[i-1] - 2.0 * radius * (Double_t)asin ( dpsi ) ;
617 ss += fZWeight[i] * fS[i];
618 sz += fZWeight[i] * points[pos].fZ;
619 sss += fZWeight[i] * fS[i] * fS[i];
620 ssz += fZWeight[i] * fS[i] * points[pos].fZ;
625 Double_t chi2,det = sum * sss - ss * ss;
626 if ( fabs(det) < 1e-20)
629 //fTrack->SetChiSq2(chi2);
633 //Compute the best fitted parameters A,B
634 Double_t tanl,z0,dtanl,dz0;
636 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
637 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
639 fTrack->SetTgl(tanl);
642 // calculate chi-square
647 for(Int_t i=0; i<fTrack->GetNHits(); i++)
649 UInt_t id = hitnum[i];
650 Int_t slice = (id>>25) & 0x7f;
651 Int_t patch = (id>>22) & 0x7;
652 UInt_t pos = id&0x3fffff;
653 AliL3SpacePointData *points = fClusters[slice][patch];
654 r1 = points[pos].fZ - tanl * fS[i] - z0 ;
655 chi2 += (Double_t) ( (Double_t)(fZWeight[i]) * (r1 * r1) );
658 //fTrack->SetChiSq2(chi2);
660 // calculate estimated variance
661 // varsq=chi/(double(n)-2.)
662 // calculate covariance matrix
663 // siga=sqrt(varsq*sxx/det)
664 // sigb=sqrt(varsq*sum/det)
666 dtanl = (Double_t) ( sum / det );
667 dz0 = (Double_t) ( sss / det );
669 fTrack->SetTglerr(dtanl);
670 fTrack->SetZ0err(dz0);