2 // Original: AliHLTFitter.cxx,v 1.14 2005/06/14 10:55:21 cvetan
4 /**************************************************************************
5 * This file is property of and copyright by the ALICE HLT Project *
6 * ALICE Experiment at CERN, All rights reserved. *
8 * Primary Authors: Anders Vestbo, maintained by *
9 * Matthias Richter <Matthias.Richter@ift.uib.no> *
10 * for The ALICE HLT Project. *
12 * Permission to use, copy, modify and distribute this software and its *
13 * documentation strictly for non-commercial purposes is hereby granted *
14 * without fee, provided that the above copyright notice appears in all *
15 * copies and that both the copyright notice and this permission notice *
16 * appear in the supporting documentation. The authors make no claims *
17 * about the suitability of this software for any purpose. It is *
18 * provided "as is" without express or implied warranty. *
19 **************************************************************************/
21 /** @file AliHLTTPCFitter.cxx
22 @author Anders Vestbo, maintained by Matthias Richter
24 @brief Fit class HLT for helix
28 #include "AliHLTTPCLogging.h"
29 #include "AliHLTTPCFitter.h"
30 #include "AliHLTTPCVertex.h"
31 #include "AliHLTTPCTrack.h"
32 #include "AliHLTTPCSpacePointData.h"
33 #include "AliHLTTPCTransform.h"
34 //#include "AliHLTTPC.h"
40 ClassImp(AliHLTTPCFitter)
43 AliHLTTPCFitter::AliHLTTPCFitter()
50 // see header file for class documentation
52 // refer to README to build package
54 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
55 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
56 memset(fNcl,0,36*6*sizeof(UInt_t));
59 AliHLTTPCFitter::AliHLTTPCFitter(AliHLTTPCVertex *vertex,Bool_t vertexconstraint)
63 fVertexConstraint(vertexconstraint)
67 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
70 AliHLTTPCFitter::~AliHLTTPCFitter()
73 for(Int_t i=0; i<36; i++)
75 for(Int_t j=0; j<6; j++)
78 delete [] fClusters[i][j];
83 void AliHLTTPCFitter::SortTrackClusters(AliHLTTPCTrack *track) const
85 //Sort the internal cluster list in each track with respect to row numbering.
86 //This may be necessary when no conventional track follower has been
87 //applied, in which the cluster list has been maintained in a more
90 Int_t nhits = track->GetNHits();
91 Int_t *ids = (Int_t*)track->GetHitNumbers();
92 Int_t *origids = new Int_t[nhits];
93 Int_t *mk = new Int_t[nhits];
96 for(k=0; k<nhits; k++) {origids[k] = ids[k]; mk[k] = -1;}
98 Int_t slice,patch,id,padrow,maxrow,maxk;
100 for(Int_t j=0; j<nhits; j++)
104 for(k=0; k<nhits; k++)
108 slice = AliHLTTPCSpacePointData::GetSlice(id);
109 patch = AliHLTTPCSpacePointData::GetPatch(id);
110 pos = AliHLTTPCSpacePointData::GetNumber(id);
111 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
112 padrow = points[pos].fPadRow;
123 for(k=0; k<nhits; k++)
124 ids[k] = origids[mk[k]];
129 Int_t AliHLTTPCFitter::FitHelix(AliHLTTPCTrack *track)
131 //fit helix parameters
135 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
136 "Problems during circle fit"<<ENDLOG;
141 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
142 "Problems during line fit"<<ENDLOG;
148 Int_t AliHLTTPCFitter::FitCircle()
150 //-----------------------------------------------------------------
151 //Fits circle parameters using algorithm
152 //described by ChErnov and Oskov in Computer Physics
155 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
156 //Moved to C by Pablo Yepes
157 //Moved to AliROOT by ASV.
158 //------------------------------------------------------------------
160 if (!fTrack) return -1;
162 Double_t wsum = 0.0 ;
167 // Loop over hits calculating average
168 Double_t * fXYWeight = new Double_t[(fTrack->GetNHits())];
169 UInt_t *hitnum = fTrack->GetHitNumbers();
170 if (!fXYWeight || !hitnum) {
171 if (fXYWeight) delete [] fXYWeight;
174 memset(fXYWeight, 0, fTrack->GetNHits()*sizeof(Double_t));
175 for(Int_t i=0; i<fTrack->GetNHits(); i++)
177 UInt_t id = hitnum[i];
178 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
179 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
180 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
181 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
182 fXYWeight[i] = 1./ (Double_t)(points[pos].fSigmaY2 + points[pos].fSigmaY2);
183 wsum += fXYWeight[i];
184 xav += fXYWeight[i]*points[pos].fX;
185 yav += fXYWeight[i]*points[pos].fY;
187 if (fVertexConstraint == kTRUE)
189 wsum += fVertex->GetXYWeight() ;
190 xav += fVertex->GetX() ;
191 yav += fVertex->GetY() ;
197 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
199 Double_t xxav = 0.0 ;
200 Double_t xyav = 0.0 ;
201 Double_t yyav = 0.0 ;
204 for(Int_t i=0; i<fTrack->GetNHits(); i++)
206 UInt_t id = hitnum[i];
207 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
208 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
209 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
210 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
212 xi = points[pos].fX -xav;
213 yi = points[pos].fY - yav ;
214 xxav += xi * xi * fXYWeight[i];
215 xyav += xi * yi * fXYWeight[i];
216 yyav += yi * yi * fXYWeight[i];
219 if (fVertexConstraint == kTRUE)
221 xi = fVertex->GetX() - xav ;
222 yi = fVertex->GetY() - yav ;
223 xxav += xi * xi * fVertex->GetXYWeight() ;
224 xyav += xi * yi * fVertex->GetXYWeight() ;
225 yyav += yi * yi * fVertex->GetXYWeight() ;
231 //--> ROTATE COORDINATES SO THAT <XY> = 0
233 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
234 //--> & > ==> NEW : (XXAV-YYAV) > 0
235 //--> SIGN(S) = SIGN(XYAV) >
237 Double_t a = fabs( xxav - yyav ) ;
238 Double_t b = 4.0 * xyav * xyav ;
240 Double_t asqpb = a * a + b ;
241 Double_t rasqpb = sqrt ( asqpb) ;
243 Double_t splus = 1.0 + a / rasqpb ;
244 Double_t sminus = b / (asqpb * splus) ;
246 splus = sqrt (0.5 * splus ) ;
247 sminus = sqrt (0.5 * sminus) ;
249 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
251 Double_t sinrot, cosrot ;
252 if ( xxav <= yyav ) {
261 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
263 if ( xyav < 0.0 ) sinrot = - sinrot ;
265 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
266 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
267 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
268 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
270 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
272 if ( cosrot*xav+sinrot*yav < 0.0 ) {
277 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
279 Double_t rrav = xxav + yyav ;
280 Double_t rscale = sqrt(rrav) ;
285 Double_t xrrav = 0.0 ;
286 Double_t yrrav = 0.0 ;
287 Double_t rrrrav = 0.0 ;
289 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
291 for(Int_t i=0; i<fTrack->GetNHits(); i++)
293 UInt_t id = hitnum[i];
294 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
295 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
296 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
297 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
299 xold = points[pos].fX - xav ;
300 yold = points[pos].fY - yav ;
302 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
304 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
305 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
310 wiriri = fXYWeight[i] * riri ;
312 xyav += fXYWeight[i] * xi * yi ;
313 xxav += fXYWeight[i] * xixi ;
314 yyav += fXYWeight[i] * yiyi ;
316 xrrav += wiriri * xi ;
317 yrrav += wiriri * yi ;
318 rrrrav += wiriri * riri ;
321 // Include vertex if required
323 if (fVertexConstraint == kTRUE)
325 xold = fVertex->GetX() - xav ;
326 yold = fVertex->GetY() - yav ;
328 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
330 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
331 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
336 wiriri = fVertex->GetXYWeight() * riri ;
338 xyav += fVertex->GetXYWeight() * xi * yi ;
339 xxav += fVertex->GetXYWeight() * xixi ;
340 yyav += fVertex->GetXYWeight() * yiyi ;
342 xrrav += wiriri * xi ;
343 yrrav += wiriri * yi ;
344 rrrrav += wiriri * riri ;
349 //--> DIVIDE BY WSUM TO MAKE AVERAGES
353 xrrav = xrrav / wsum ;
354 yrrav = yrrav / wsum ;
355 rrrrav = rrrrav / wsum ;
358 Int_t const kntry = 5 ;
360 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
361 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
363 Double_t xrrxrr = xrrav * xrrav ;
364 Double_t yrryrr = yrrav * yrrav ;
365 Double_t rrrrm1 = rrrrav - 1.0 ;
366 Double_t xxyy = xxav * yyav ;
368 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
369 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
370 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
371 Double_t c4 = - 4.0 ;
373 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
375 Double_t c2d = 2.0 * c2 ;
376 Double_t c4d = 4.0 * c4 ;
378 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
380 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
381 Double_t lamda = 0.0 ;
382 Double_t dlamda = 0.0 ;
384 Double_t chiscl = wsum * rscale * rscale ;
385 Double_t dlamax = 0.001 / chiscl ;
388 for ( int itry = 1 ; itry <= kntry ; itry++ ) {
389 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
390 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
392 lamda = lamda + dlamda ;
393 if (fabs(dlamda)< dlamax) break ;
396 //Double_t chi2 = (Double_t)(chiscl * lamda) ;
397 //fTrack->SetChiSq1(chi2);
398 // Double_t dchisq = chiscl * dlamda ;
400 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
402 Double_t h11 = xxav - lamda ;
403 Double_t h14 = xrrav ;
404 Double_t h22 = yyav - lamda ;
405 Double_t h24 = yrrav ;
406 Double_t h34 = 1.0 + 2.0*lamda ;
407 if ( h11 == 0.0 || h22 == 0.0 ){
408 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitCircle","TrackFit")<<AliHLTTPCLog::kDec<<
409 "Problems fitting circle"<<ENDLOG;
413 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
415 Double_t ratio, kappa, beta ;
416 if ( fabs(h22) > fabs(h24) ) {
418 rootsq = ratio * ratio + rootsq ;
419 kappa = 1.0 / sqrt(rootsq) ;
420 beta = - ratio * kappa ;
424 rootsq = 1.0 + ratio * ratio * rootsq ;
425 beta = 1.0 / sqrt(rootsq) ;
426 if ( h24 > 0 ) beta = - beta ;
427 kappa = -ratio * beta ;
429 Double_t alpha = - (h14/h11) * kappa ;
431 //--> transform these into the lab coordinate system
432 //--> first get kappa and back to real dimensions
434 Double_t kappa1 = kappa / rscale ;
435 Double_t dbro = 0.5 / kappa1 ;
437 //--> next rotate alpha and beta and scale
439 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
440 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
442 //--> then translate by (xav,yav)
444 Double_t acent = (double)(xav - alphar) ;
445 Double_t bcent = (double)(yav - betar ) ;
446 Double_t radius = (double)dbro ;
450 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
451 fTrack->SetCharge(q);
453 //Set the first point on the track to the space point coordinates of the innermost track
454 //This will be updated to lie on the fit later on (AliHLTTPCTrack::UpdateToFirstPoint).
455 Double_t x0,y0,psi,pt ;
456 Int_t lastid=fTrack->GetNHits()-1;
457 UInt_t id = hitnum[lastid];
458 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
459 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
460 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
461 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
464 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
466 //Set the remaining fit parameters
467 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
468 psi = psi + q * 0.5F * AliHLTTPCTransform::Pi() ;
469 if ( psi < 0 ) psi = psi + 2*AliHLTTPCTransform::Pi();
471 pt = (Double_t)(AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() * radius ) ;
474 fTrack->SetRadius(radius);
475 fTrack->SetCenterX(acent);
476 fTrack->SetCenterY(bcent);
478 // Get errors from fast fit
480 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
486 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
487 // Fit Line in s-z plane
488 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
489 Int_t AliHLTTPCFitter::FitLine ( )
491 // Fit Line in s-z plane
492 if (!fTrack) return -1;
503 //find sum , sums ,sumz, sumss
506 Double_t radius = (Double_t)(fTrack->GetPt() / ( AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() ) ) ;
508 Double_t * fS = new Double_t[(fTrack->GetNHits())];
509 Double_t *fZWeight = new Double_t[fTrack->GetNHits()];
510 UInt_t *hitnum = fTrack->GetHitNumbers();
511 if (!fS || !fZWeight || !hitnum) {
512 if (fS) delete [] fS;
513 if (fZWeight) delete [] fZWeight;
516 memset(fS, 0, fTrack->GetNHits()*sizeof(Double_t));
517 memset(fZWeight, 0, fTrack->GetNHits()*sizeof(Double_t));
518 if (0)//fVertexConstraint==kTRUE)
520 UInt_t id = hitnum[0];
521 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
522 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
523 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
524 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
526 dx = points[pos].fX - fVertex->GetX();
527 dy = points[pos].fY - fVertex->GetY();
531 UInt_t id = hitnum[0];
532 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
533 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
534 UInt_t posf = AliHLTTPCSpacePointData::GetNumber(id);
535 AliHLTTPCSpacePointData *pointsf = fClusters[slice][patch];
536 id = hitnum[(fTrack->GetNHits()-1)];
537 slice = AliHLTTPCSpacePointData::GetSlice(id);
538 patch = AliHLTTPCSpacePointData::GetPatch(id);
539 UInt_t posl = AliHLTTPCSpacePointData::GetNumber(id);
540 AliHLTTPCSpacePointData *pointsl = fClusters[slice][patch];
541 dx = pointsf[posf].fX - pointsl[posl].fX;
542 dy = pointsf[posf].fY - pointsl[posl].fY;
545 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
548 if ( fabs(localPsi) < 1. )
550 totals = 2.0 * radius * asin ( localPsi ) ;
554 totals = 2.0 * radius * AliHLTTPCTransform::Pi() ;
559 for(Int_t i=0; i<fTrack->GetNHits(); i++)
561 UInt_t id = hitnum[i];
562 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
563 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
564 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
565 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
567 fZWeight[i] = 1./(Double_t)(points[pos].fSigmaZ2);
571 slice = AliHLTTPCSpacePointData::GetSlice(id);
572 patch = AliHLTTPCSpacePointData::GetPatch(id);
573 UInt_t lastpos = AliHLTTPCSpacePointData::GetNumber(id);
574 AliHLTTPCSpacePointData *lastpoints = fClusters[slice][patch];
575 dx = points[pos].fX -lastpoints[lastpos].fX;
576 dy = points[pos].fY -lastpoints[lastpos].fY;
577 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
583 fTrack->SetPsierr(dpsi);
584 s = fS[i-1] - 2.0 * radius * (Double_t)asin ( dpsi ) ;
591 ss += fZWeight[i] * fS[i];
592 sz += fZWeight[i] * points[pos].fZ;
593 sss += fZWeight[i] * fS[i] * fS[i];
594 ssz += fZWeight[i] * fS[i] * points[pos].fZ;
599 Double_t chi2,det = sum * sss - ss * ss;
600 if ( fabs(det) < 1e-20)
603 //fTrack->SetChiSq2(chi2);
609 //Compute the best fitted parameters A,B
610 Double_t tanl,z0,dtanl,dz0;
612 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
613 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
615 fTrack->SetTgl(tanl);
618 //calculate chi-square
622 for(Int_t i=0; i<fTrack->GetNHits(); i++)
624 UInt_t id = hitnum[i];
625 Int_t slice = AliHLTTPCSpacePointData::GetSlice(id);
626 Int_t patch = AliHLTTPCSpacePointData::GetPatch(id);
627 UInt_t pos = AliHLTTPCSpacePointData::GetNumber(id);
628 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
629 r1 = points[pos].fZ - tanl * fS[i] - z0 ;
630 chi2 += (Double_t) ( (Double_t)(fZWeight[i]) * (r1 * r1) );
633 //fTrack->SetChiSq2(chi2);
635 //calculate estimated variance
636 //varsq=chi/(double(n)-2.)
637 //calculate covariance matrix
638 //siga=sqrt(varsq*sxx/det)
639 //sigb=sqrt(varsq*sum/det)
641 dtanl = (Double_t) ( sum / det );
642 dz0 = (Double_t) ( sss / det );
644 fTrack->SetTglerr(dtanl);
645 fTrack->SetZ0err(dz0);