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 "AliHLTTPCMemHandler.h"
34 #include "AliHLTTPCTransform.h"
35 //#include "AliHLTTPC.h"
41 ClassImp(AliHLTTPCFitter)
44 AliHLTTPCFitter::AliHLTTPCFitter()
51 // see header file for class documentation
53 // refer to README to build package
55 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
56 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
57 memset(fNcl,0,36*6*sizeof(UInt_t));
60 AliHLTTPCFitter::AliHLTTPCFitter(const AliHLTTPCFitter& src)
66 // dummy copy constructor according to eff C++
67 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
68 memset(fNcl,0,36*6*sizeof(UInt_t));
71 AliHLTTPCFitter& AliHLTTPCFitter::operator=(const AliHLTTPCFitter& src)
73 // dummy assignment operator according to eff C++
77 AliHLTTPCFitter::AliHLTTPCFitter(AliHLTTPCVertex *vertex,Bool_t vertexconstraint)
81 fVertexConstraint(vertexconstraint)
85 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
88 AliHLTTPCFitter::~AliHLTTPCFitter()
91 for(Int_t i=0; i<36; i++)
93 for(Int_t j=0; j<6; j++)
96 delete [] fClusters[i][j];
101 void AliHLTTPCFitter::LoadClusters(Char_t *path,Int_t event,Bool_t sp)
105 AliHLTTPCMemHandler *clusterfile[36][6];
106 for(Int_t s=0; s<=35; s++)
108 for(Int_t p=0; p<6; p++)
116 delete fClusters[s][p];
118 clusterfile[s][p] = new AliHLTTPCMemHandler();
119 sprintf(fname,"%s/points_%d_%d_%d.raw",path,event,s,patch);
120 if(!clusterfile[s][p]->SetBinaryInput(fname))
122 delete clusterfile[s][p];
123 clusterfile[s][p] = 0;
126 fClusters[s][p] = (AliHLTTPCSpacePointData*)clusterfile[s][p]->Allocate();
127 clusterfile[s][p]->Binary2Memory(fNcl[s][p],fClusters[s][p]);
128 clusterfile[s][p]->CloseBinaryInput();
135 void AliHLTTPCFitter::SortTrackClusters(AliHLTTPCTrack *track) const
137 //Sort the internal cluster list in each track with respect to row numbering.
138 //This may be necessary when no conventional track follower has been
139 //applied, in which the cluster list has been maintained in a more
142 Int_t nhits = track->GetNHits();
143 Int_t *ids = (Int_t*)track->GetHitNumbers();
144 Int_t *origids = new Int_t[nhits];
145 Int_t *mk = new Int_t[nhits];
148 for(k=0; k<nhits; k++) {origids[k] = ids[k]; mk[k] = -1;}
150 Int_t slice,patch,id,padrow,maxrow,maxk;
152 for(Int_t j=0; j<nhits; j++)
156 for(k=0; k<nhits; k++)
160 slice = (id>>25) & 0x7f;
161 patch = (id>>22) & 0x7;
163 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
164 padrow = points[pos].fPadRow;
175 for(k=0; k<nhits; k++)
176 ids[k] = origids[mk[k]];
181 Int_t AliHLTTPCFitter::FitHelix(AliHLTTPCTrack *track)
183 //fit helix parameters
187 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
188 "Problems during circle fit"<<ENDLOG;
193 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
194 "Problems during line fit"<<ENDLOG;
200 Int_t AliHLTTPCFitter::FitCircle()
202 //-----------------------------------------------------------------
203 //Fits circle parameters using algorithm
204 //described by ChErnov and Oskov in Computer Physics
207 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
208 //Moved to C by Pablo Yepes
209 //Moved to AliROOT by ASV.
210 //------------------------------------------------------------------
212 Double_t wsum = 0.0 ;
217 // Loop over hits calculating average
218 Double_t * fXYWeight = new Double_t[(fTrack->GetNHits())];
219 UInt_t *hitnum = fTrack->GetHitNumbers();
220 for(Int_t i=0; i<fTrack->GetNHits(); i++)
222 UInt_t id = hitnum[i];
223 Int_t slice = (id>>25) & 0x7f;
224 Int_t patch = (id>>22) & 0x7;
225 UInt_t pos = id&0x3fffff;
226 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
227 fXYWeight[i] = 1./ (Double_t)(points[pos].fSigmaY2 + points[pos].fSigmaY2);
228 wsum += fXYWeight[i];
229 xav += fXYWeight[i]*points[pos].fX;
230 yav += fXYWeight[i]*points[pos].fY;
232 if (fVertexConstraint == kTRUE)
234 wsum += fVertex->GetXYWeight() ;
235 xav += fVertex->GetX() ;
236 yav += fVertex->GetY() ;
242 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
244 Double_t xxav = 0.0 ;
245 Double_t xyav = 0.0 ;
246 Double_t yyav = 0.0 ;
249 for(Int_t i=0; i<fTrack->GetNHits(); i++)
251 UInt_t id = hitnum[i];
252 Int_t slice = (id>>25) & 0x7f;
253 Int_t patch = (id>>22) & 0x7;
254 UInt_t pos = id&0x3fffff;
255 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
257 xi = points[pos].fX -xav;
258 yi = points[pos].fY - yav ;
259 xxav += xi * xi * fXYWeight[i];
260 xyav += xi * yi * fXYWeight[i];
261 yyav += yi * yi * fXYWeight[i];
264 if (fVertexConstraint == kTRUE)
266 xi = fVertex->GetX() - xav ;
267 yi = fVertex->GetY() - yav ;
268 xxav += xi * xi * fVertex->GetXYWeight() ;
269 xyav += xi * yi * fVertex->GetXYWeight() ;
270 yyav += yi * yi * fVertex->GetXYWeight() ;
276 //--> ROTATE COORDINATES SO THAT <XY> = 0
278 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
279 //--> & > ==> NEW : (XXAV-YYAV) > 0
280 //--> SIGN(S) = SIGN(XYAV) >
282 Double_t a = fabs( xxav - yyav ) ;
283 Double_t b = 4.0 * xyav * xyav ;
285 Double_t asqpb = a * a + b ;
286 Double_t rasqpb = sqrt ( asqpb) ;
288 Double_t splus = 1.0 + a / rasqpb ;
289 Double_t sminus = b / (asqpb * splus) ;
291 splus = sqrt (0.5 * splus ) ;
292 sminus = sqrt (0.5 * sminus) ;
294 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
296 Double_t sinrot, cosrot ;
297 if ( xxav <= yyav ) {
306 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
308 if ( xyav < 0.0 ) sinrot = - sinrot ;
310 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
311 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
312 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
313 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
315 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
317 if ( cosrot*xav+sinrot*yav < 0.0 ) {
322 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
324 Double_t rrav = xxav + yyav ;
325 Double_t rscale = sqrt(rrav) ;
330 Double_t xrrav = 0.0 ;
331 Double_t yrrav = 0.0 ;
332 Double_t rrrrav = 0.0 ;
334 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
336 for(Int_t i=0; i<fTrack->GetNHits(); i++)
338 UInt_t id = hitnum[i];
339 Int_t slice = (id>>25) & 0x7f;
340 Int_t patch = (id>>22) & 0x7;
341 UInt_t pos = id&0x3fffff;
342 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
344 xold = points[pos].fX - xav ;
345 yold = points[pos].fY - yav ;
347 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
349 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
350 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
355 wiriri = fXYWeight[i] * riri ;
357 xyav += fXYWeight[i] * xi * yi ;
358 xxav += fXYWeight[i] * xixi ;
359 yyav += fXYWeight[i] * yiyi ;
361 xrrav += wiriri * xi ;
362 yrrav += wiriri * yi ;
363 rrrrav += wiriri * riri ;
366 // Include vertex if required
368 if (fVertexConstraint == kTRUE)
370 xold = fVertex->GetX() - xav ;
371 yold = fVertex->GetY() - yav ;
373 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
375 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
376 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
381 wiriri = fVertex->GetXYWeight() * riri ;
383 xyav += fVertex->GetXYWeight() * xi * yi ;
384 xxav += fVertex->GetXYWeight() * xixi ;
385 yyav += fVertex->GetXYWeight() * yiyi ;
387 xrrav += wiriri * xi ;
388 yrrav += wiriri * yi ;
389 rrrrav += wiriri * riri ;
394 //--> DIVIDE BY WSUM TO MAKE AVERAGES
398 xrrav = xrrav / wsum ;
399 yrrav = yrrav / wsum ;
400 rrrrav = rrrrav / wsum ;
403 Int_t const kntry = 5 ;
405 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
406 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
408 Double_t xrrxrr = xrrav * xrrav ;
409 Double_t yrryrr = yrrav * yrrav ;
410 Double_t rrrrm1 = rrrrav - 1.0 ;
411 Double_t xxyy = xxav * yyav ;
413 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
414 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
415 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
416 Double_t c4 = - 4.0 ;
418 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
420 Double_t c2d = 2.0 * c2 ;
421 Double_t c4d = 4.0 * c4 ;
423 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
425 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
426 Double_t lamda = 0.0 ;
427 Double_t dlamda = 0.0 ;
429 Double_t chiscl = wsum * rscale * rscale ;
430 Double_t dlamax = 0.001 / chiscl ;
433 for ( int itry = 1 ; itry <= kntry ; itry++ ) {
434 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
435 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
437 lamda = lamda + dlamda ;
438 if (fabs(dlamda)< dlamax) break ;
441 //Double_t chi2 = (Double_t)(chiscl * lamda) ;
442 //fTrack->SetChiSq1(chi2);
443 // Double_t dchisq = chiscl * dlamda ;
445 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
447 Double_t h11 = xxav - lamda ;
448 Double_t h14 = xrrav ;
449 Double_t h22 = yyav - lamda ;
450 Double_t h24 = yrrav ;
451 Double_t h34 = 1.0 + 2.0*lamda ;
452 if ( h11 == 0.0 || h22 == 0.0 ){
453 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitCircle","TrackFit")<<AliHLTTPCLog::kDec<<
454 "Problems fitting circle"<<ENDLOG;
457 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
459 Double_t ratio, kappa, beta ;
460 if ( fabs(h22) > fabs(h24) ) {
462 rootsq = ratio * ratio + rootsq ;
463 kappa = 1.0 / sqrt(rootsq) ;
464 beta = - ratio * kappa ;
468 rootsq = 1.0 + ratio * ratio * rootsq ;
469 beta = 1.0 / sqrt(rootsq) ;
470 if ( h24 > 0 ) beta = - beta ;
471 kappa = -ratio * beta ;
473 Double_t alpha = - (h14/h11) * kappa ;
475 //--> transform these into the lab coordinate system
476 //--> first get kappa and back to real dimensions
478 Double_t kappa1 = kappa / rscale ;
479 Double_t dbro = 0.5 / kappa1 ;
481 //--> next rotate alpha and beta and scale
483 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
484 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
486 //--> then translate by (xav,yav)
488 Double_t acent = (double)(xav - alphar) ;
489 Double_t bcent = (double)(yav - betar ) ;
490 Double_t radius = (double)dbro ;
494 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
495 fTrack->SetCharge(q);
497 //Set the first point on the track to the space point coordinates of the innermost track
498 //This will be updated to lie on the fit later on (AliHLTTPCTrack::UpdateToFirstPoint).
499 Double_t x0,y0,psi,pt ;
500 Int_t lastid=fTrack->GetNHits()-1;
501 UInt_t id = hitnum[lastid];
502 Int_t slice = (id>>25) & 0x7f;
503 Int_t patch = (id>>22) & 0x7;
504 UInt_t pos = id&0x3fffff;
505 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
508 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
510 //Set the remaining fit parameters
511 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
512 psi = psi + q * 0.5F * AliHLTTPCTransform::Pi() ;
513 if ( psi < 0 ) psi = psi + 2*AliHLTTPCTransform::Pi();
515 pt = (Double_t)(AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() * radius ) ;
518 fTrack->SetRadius(radius);
519 fTrack->SetCenterX(acent);
520 fTrack->SetCenterY(bcent);
522 // Get errors from fast fit
524 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
530 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
531 // Fit Line in s-z plane
532 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
533 Int_t AliHLTTPCFitter::FitLine ( )
544 //find sum , sums ,sumz, sumss
547 Double_t radius = (Double_t)(fTrack->GetPt() / ( AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() ) ) ;
549 Double_t * fS = new Double_t[(fTrack->GetNHits())];
550 Double_t *fZWeight = new Double_t[fTrack->GetNHits()];
551 UInt_t *hitnum = fTrack->GetHitNumbers();
552 if (0)//fVertexConstraint==kTRUE)
554 UInt_t id = hitnum[0];
555 Int_t slice = (id>>25) & 0x7f;
556 Int_t patch = (id>>22) & 0x7;
557 UInt_t pos = id&0x3fffff;
558 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
560 dx = points[pos].fX - fVertex->GetX();
561 dy = points[pos].fY - fVertex->GetY();
565 UInt_t id = hitnum[0];
566 Int_t slice = (id>>25) & 0x7f;
567 Int_t patch = (id>>22) & 0x7;
568 UInt_t posf = id&0x3fffff;
569 AliHLTTPCSpacePointData *pointsf = fClusters[slice][patch];
570 id = hitnum[(fTrack->GetNHits()-1)];
571 slice = (id>>25) & 0x7f;
572 patch = (id>>22) & 0x7;
573 UInt_t posl = id&0x3fffff;
574 AliHLTTPCSpacePointData *pointsl = fClusters[slice][patch];
575 dx = pointsf[posf].fX - pointsl[posl].fX;
576 dy = pointsf[posf].fY - pointsl[posl].fY;
579 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
582 if ( fabs(localPsi) < 1. )
584 totals = 2.0 * radius * asin ( localPsi ) ;
588 totals = 2.0 * radius * AliHLTTPCTransform::Pi() ;
593 for(Int_t i=0; i<fTrack->GetNHits(); i++)
595 UInt_t id = hitnum[i];
596 Int_t slice = (id>>25) & 0x7f;
597 Int_t patch = (id>>22) & 0x7;
598 UInt_t pos = id&0x3fffff;
599 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
601 fZWeight[i] = 1./(Double_t)(points[pos].fSigmaZ2);
605 slice = (id>>25) & 0x7f;
606 patch = (id>>22) & 0x7;
607 UInt_t lastpos = id&0x3fffff;
608 AliHLTTPCSpacePointData *lastpoints = fClusters[slice][patch];
609 dx = points[pos].fX -lastpoints[lastpos].fX;
610 dy = points[pos].fY -lastpoints[lastpos].fY;
611 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
614 fTrack->SetPsierr(dpsi);
615 s = fS[i-1] - 2.0 * radius * (Double_t)asin ( dpsi ) ;
622 ss += fZWeight[i] * fS[i];
623 sz += fZWeight[i] * points[pos].fZ;
624 sss += fZWeight[i] * fS[i] * fS[i];
625 ssz += fZWeight[i] * fS[i] * points[pos].fZ;
630 Double_t chi2,det = sum * sss - ss * ss;
631 if ( fabs(det) < 1e-20)
634 //fTrack->SetChiSq2(chi2);
638 //Compute the best fitted parameters A,B
639 Double_t tanl,z0,dtanl,dz0;
641 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
642 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
644 fTrack->SetTgl(tanl);
647 //calculate chi-square
651 for(Int_t i=0; i<fTrack->GetNHits(); i++)
653 UInt_t id = hitnum[i];
654 Int_t slice = (id>>25) & 0x7f;
655 Int_t patch = (id>>22) & 0x7;
656 UInt_t pos = id&0x3fffff;
657 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
658 r1 = points[pos].fZ - tanl * fS[i] - z0 ;
659 chi2 += (Double_t) ( (Double_t)(fZWeight[i]) * (r1 * r1) );
662 //fTrack->SetChiSq2(chi2);
664 //calculate estimated variance
665 //varsq=chi/(double(n)-2.)
666 //calculate covariance matrix
667 //siga=sqrt(varsq*sxx/det)
668 //sigb=sqrt(varsq*sum/det)
670 dtanl = (Double_t) ( sum / det );
671 dz0 = (Double_t) ( sss / det );
673 fTrack->SetTglerr(dtanl);
674 fTrack->SetZ0err(dz0);