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 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
52 memset(fNcl,0,36*6*sizeof(UInt_t));
55 AliHLTTPCFitter::AliHLTTPCFitter(const AliHLTTPCFitter& src)
61 // dummy copy constructor according to eff C++
62 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
63 memset(fNcl,0,36*6*sizeof(UInt_t));
66 AliHLTTPCFitter& AliHLTTPCFitter::operator=(const AliHLTTPCFitter& src)
68 // dummy assignment operator according to eff C++
72 AliHLTTPCFitter::AliHLTTPCFitter(AliHLTTPCVertex *vertex,Bool_t vertexconstraint)
77 fVertexConstraint=vertexconstraint;
78 memset(fClusters,0,36*6*sizeof(AliHLTTPCSpacePointData*));
81 AliHLTTPCFitter::~AliHLTTPCFitter()
84 for(Int_t i=0; i<36; i++)
86 for(Int_t j=0; j<6; j++)
89 delete [] fClusters[i][j];
94 void AliHLTTPCFitter::LoadClusters(Char_t *path,Int_t event,Bool_t sp)
98 AliHLTTPCMemHandler *clusterfile[36][6];
99 for(Int_t s=0; s<=35; s++)
101 for(Int_t p=0; p<6; p++)
109 delete fClusters[s][p];
111 clusterfile[s][p] = new AliHLTTPCMemHandler();
112 sprintf(fname,"%s/points_%d_%d_%d.raw",path,event,s,patch);
113 if(!clusterfile[s][p]->SetBinaryInput(fname))
115 delete clusterfile[s][p];
116 clusterfile[s][p] = 0;
119 fClusters[s][p] = (AliHLTTPCSpacePointData*)clusterfile[s][p]->Allocate();
120 clusterfile[s][p]->Binary2Memory(fNcl[s][p],fClusters[s][p]);
121 clusterfile[s][p]->CloseBinaryInput();
128 void AliHLTTPCFitter::SortTrackClusters(AliHLTTPCTrack *track) const
130 //Sort the internal cluster list in each track with respect to row numbering.
131 //This may be necessary when no conventional track follower has been
132 //applied, in which the cluster list has been maintained in a more
135 Int_t nhits = track->GetNHits();
136 Int_t *ids = (Int_t*)track->GetHitNumbers();
137 Int_t *origids = new Int_t[nhits];
138 Int_t *mk = new Int_t[nhits];
141 for(k=0; k<nhits; k++) {origids[k] = ids[k]; mk[k] = -1;}
143 Int_t slice,patch,id,padrow,maxrow,maxk;
145 for(Int_t j=0; j<nhits; j++)
149 for(k=0; k<nhits; k++)
153 slice = (id>>25) & 0x7f;
154 patch = (id>>22) & 0x7;
156 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
157 padrow = points[pos].fPadRow;
168 for(k=0; k<nhits; k++)
169 ids[k] = origids[mk[k]];
174 Int_t AliHLTTPCFitter::FitHelix(AliHLTTPCTrack *track)
176 //fit helix parameters
180 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
181 "Problems during circle fit"<<ENDLOG;
186 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitHelix","TrackFit")<<AliHLTTPCLog::kDec<<
187 "Problems during line fit"<<ENDLOG;
193 Int_t AliHLTTPCFitter::FitCircle()
195 //-----------------------------------------------------------------
196 //Fits circle parameters using algorithm
197 //described by ChErnov and Oskov in Computer Physics
200 //Written in FORTRAN by Jawluen Tang, Physics department , UT-Austin
201 //Moved to C by Pablo Yepes
202 //Moved to AliROOT by ASV.
203 //------------------------------------------------------------------
205 Double_t wsum = 0.0 ;
210 // Loop over hits calculating average
211 Double_t * fXYWeight = new Double_t[(fTrack->GetNHits())];
212 UInt_t *hitnum = fTrack->GetHitNumbers();
213 for(Int_t i=0; i<fTrack->GetNHits(); i++)
215 UInt_t id = hitnum[i];
216 Int_t slice = (id>>25) & 0x7f;
217 Int_t patch = (id>>22) & 0x7;
218 UInt_t pos = id&0x3fffff;
219 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
220 fXYWeight[i] = 1./ (Double_t)(points[pos].fSigmaY2 + points[pos].fSigmaY2);
221 wsum += fXYWeight[i];
222 xav += fXYWeight[i]*points[pos].fX;
223 yav += fXYWeight[i]*points[pos].fY;
225 if (fVertexConstraint == kTRUE)
227 wsum += fVertex->GetXYWeight() ;
228 xav += fVertex->GetX() ;
229 yav += fVertex->GetY() ;
235 // CALCULATE <X**2>, <XY>, AND <Y**2> WITH <X> = 0, & <Y> = 0
237 Double_t xxav = 0.0 ;
238 Double_t xyav = 0.0 ;
239 Double_t yyav = 0.0 ;
242 for(Int_t i=0; i<fTrack->GetNHits(); i++)
244 UInt_t id = hitnum[i];
245 Int_t slice = (id>>25) & 0x7f;
246 Int_t patch = (id>>22) & 0x7;
247 UInt_t pos = id&0x3fffff;
248 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
250 xi = points[pos].fX -xav;
251 yi = points[pos].fY - yav ;
252 xxav += xi * xi * fXYWeight[i];
253 xyav += xi * yi * fXYWeight[i];
254 yyav += yi * yi * fXYWeight[i];
257 if (fVertexConstraint == kTRUE)
259 xi = fVertex->GetX() - xav ;
260 yi = fVertex->GetY() - yav ;
261 xxav += xi * xi * fVertex->GetXYWeight() ;
262 xyav += xi * yi * fVertex->GetXYWeight() ;
263 yyav += yi * yi * fVertex->GetXYWeight() ;
269 //--> ROTATE COORDINATES SO THAT <XY> = 0
271 //--> SIGN(C**2 - S**2) = SIGN(XXAV - YYAV) >
272 //--> & > ==> NEW : (XXAV-YYAV) > 0
273 //--> SIGN(S) = SIGN(XYAV) >
275 Double_t a = fabs( xxav - yyav ) ;
276 Double_t b = 4.0 * xyav * xyav ;
278 Double_t asqpb = a * a + b ;
279 Double_t rasqpb = sqrt ( asqpb) ;
281 Double_t splus = 1.0 + a / rasqpb ;
282 Double_t sminus = b / (asqpb * splus) ;
284 splus = sqrt (0.5 * splus ) ;
285 sminus = sqrt (0.5 * sminus) ;
287 //-> FIRST REQUIRE : SIGN(C**2 - S**2) = SIGN(XXAV - YYAV)
289 Double_t sinrot, cosrot ;
290 if ( xxav <= yyav ) {
299 //-> REQUIRE : SIGN(S) = SIGN(XYAV) * SIGN(C) (ASSUMING SIGN(C) > 0)
301 if ( xyav < 0.0 ) sinrot = - sinrot ;
303 //--> WE NOW HAVE THE SMALLEST ANGLE THAT GUARANTEES <X**2> > <Y**2>
304 //--> TO GET THE SIGN OF THE CHARGE RIGHT, THE NEW X-AXIS MUST POINT
305 //--> OUTWARD FROM THE ORGIN. WE ARE FREE TO CHANGE SIGNS OF BOTH
306 //--> COSROT AND SINROT SIMULTANEOUSLY TO ACCOMPLISH THIS.
308 //--> CHOOSE SIGN OF C WISELY TO BE ABLE TO GET THE SIGN OF THE CHARGE
310 if ( cosrot*xav+sinrot*yav < 0.0 ) {
315 //-> NOW GET <R**2> AND RSCALE= SQRT(<R**2>)
317 Double_t rrav = xxav + yyav ;
318 Double_t rscale = sqrt(rrav) ;
323 Double_t xrrav = 0.0 ;
324 Double_t yrrav = 0.0 ;
325 Double_t rrrrav = 0.0 ;
327 Double_t xixi, yiyi, riri, wiriri, xold, yold ;
329 for(Int_t i=0; i<fTrack->GetNHits(); i++)
331 UInt_t id = hitnum[i];
332 Int_t slice = (id>>25) & 0x7f;
333 Int_t patch = (id>>22) & 0x7;
334 UInt_t pos = id&0x3fffff;
335 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
337 xold = points[pos].fX - xav ;
338 yold = points[pos].fY - yav ;
340 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
342 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
343 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
348 wiriri = fXYWeight[i] * riri ;
350 xyav += fXYWeight[i] * xi * yi ;
351 xxav += fXYWeight[i] * xixi ;
352 yyav += fXYWeight[i] * yiyi ;
354 xrrav += wiriri * xi ;
355 yrrav += wiriri * yi ;
356 rrrrav += wiriri * riri ;
359 // Include vertex if required
361 if (fVertexConstraint == kTRUE)
363 xold = fVertex->GetX() - xav ;
364 yold = fVertex->GetY() - yav ;
366 //--> ROTATE SO THAT <XY> = 0 & DIVIDE BY RSCALE SO THAT <R**2> = 1
368 xi = ( cosrot * xold + sinrot * yold ) / rscale ;
369 yi = ( -sinrot * xold + cosrot * yold ) / rscale ;
374 wiriri = fVertex->GetXYWeight() * riri ;
376 xyav += fVertex->GetXYWeight() * xi * yi ;
377 xxav += fVertex->GetXYWeight() * xixi ;
378 yyav += fVertex->GetXYWeight() * yiyi ;
380 xrrav += wiriri * xi ;
381 yrrav += wiriri * yi ;
382 rrrrav += wiriri * riri ;
387 //--> DIVIDE BY WSUM TO MAKE AVERAGES
391 xrrav = xrrav / wsum ;
392 yrrav = yrrav / wsum ;
393 rrrrav = rrrrav / wsum ;
396 Int_t const kntry = 5 ;
398 //--> USE THESE TO GET THE COEFFICIENTS OF THE 4-TH ORDER POLYNIMIAL
399 //--> DON'T PANIC - THE THIRD ORDER TERM IS ZERO !
401 Double_t xrrxrr = xrrav * xrrav ;
402 Double_t yrryrr = yrrav * yrrav ;
403 Double_t rrrrm1 = rrrrav - 1.0 ;
404 Double_t xxyy = xxav * yyav ;
406 Double_t c0 = rrrrm1*xxyy - xrrxrr*yyav - yrryrr*xxav ;
407 Double_t c1 = - rrrrm1 + xrrxrr + yrryrr - 4.0*xxyy ;
408 Double_t c2 = 4.0 + rrrrm1 - 4.0*xxyy ;
409 Double_t c4 = - 4.0 ;
411 //--> COEFFICIENTS OF THE DERIVATIVE - USED IN NEWTON-RAPHSON ITERATIONS
413 Double_t c2d = 2.0 * c2 ;
414 Double_t c4d = 4.0 * c4 ;
416 //--> 0'TH VALUE OF LAMDA - LINEAR INTERPOLATION BETWEEN P(0) & P(YYAV)
418 // LAMDA = YYAV * C0 / (C0 + YRRSQ * (XXAV-YYAV))
419 Double_t lamda = 0.0 ;
420 Double_t dlamda = 0.0 ;
422 Double_t chiscl = wsum * rscale * rscale ;
423 Double_t dlamax = 0.001 / chiscl ;
426 for ( int itry = 1 ; itry <= kntry ; itry++ ) {
427 p = c0 + lamda * (c1 + lamda * (c2 + lamda * lamda * c4 )) ;
428 pd = (c1 + lamda * (c2d + lamda * lamda * c4d)) ;
430 lamda = lamda + dlamda ;
431 if (fabs(dlamda)< dlamax) break ;
434 //Double_t chi2 = (Double_t)(chiscl * lamda) ;
435 //fTrack->SetChiSq1(chi2);
436 // Double_t dchisq = chiscl * dlamda ;
438 //--> NOW CALCULATE THE MATRIX ELEMENTS FOR ALPHA, BETA & KAPPA
440 Double_t h11 = xxav - lamda ;
441 Double_t h14 = xrrav ;
442 Double_t h22 = yyav - lamda ;
443 Double_t h24 = yrrav ;
444 Double_t h34 = 1.0 + 2.0*lamda ;
445 if ( h11 == 0.0 || h22 == 0.0 ){
446 LOG(AliHLTTPCLog::kError,"AliHLTTPCFitter::FitCircle","TrackFit")<<AliHLTTPCLog::kDec<<
447 "Problems fitting circle"<<ENDLOG;
450 Double_t rootsq = (h14*h14)/(h11*h11) + 4.0*h34 ;
452 Double_t ratio, kappa, beta ;
453 if ( fabs(h22) > fabs(h24) ) {
455 rootsq = ratio * ratio + rootsq ;
456 kappa = 1.0 / sqrt(rootsq) ;
457 beta = - ratio * kappa ;
461 rootsq = 1.0 + ratio * ratio * rootsq ;
462 beta = 1.0 / sqrt(rootsq) ;
463 if ( h24 > 0 ) beta = - beta ;
464 kappa = -ratio * beta ;
466 Double_t alpha = - (h14/h11) * kappa ;
468 //--> transform these into the lab coordinate system
469 //--> first get kappa and back to real dimensions
471 Double_t kappa1 = kappa / rscale ;
472 Double_t dbro = 0.5 / kappa1 ;
474 //--> next rotate alpha and beta and scale
476 Double_t alphar = (cosrot * alpha - sinrot * beta)* dbro ;
477 Double_t betar = (sinrot * alpha + cosrot * beta)* dbro ;
479 //--> then translate by (xav,yav)
481 Double_t acent = (double)(xav - alphar) ;
482 Double_t bcent = (double)(yav - betar ) ;
483 Double_t radius = (double)dbro ;
487 Int_t q = ( ( yrrav < 0 ) ? 1 : -1 ) ;
488 fTrack->SetCharge(q);
490 //Set the first point on the track to the space point coordinates of the innermost track
491 //This will be updated to lie on the fit later on (AliHLTTPCTrack::UpdateToFirstPoint).
492 Double_t x0,y0,psi,pt ;
493 Int_t lastid=fTrack->GetNHits()-1;
494 UInt_t id = hitnum[lastid];
495 Int_t slice = (id>>25) & 0x7f;
496 Int_t patch = (id>>22) & 0x7;
497 UInt_t pos = id&0x3fffff;
498 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
501 fTrack->SetFirstPoint(x0,y0,0); //Z-value is set in FitLine
503 //Set the remaining fit parameters
504 psi = (Double_t)atan2(bcent-y0,acent-x0) ;
505 psi = psi + q * 0.5F * AliHLTTPCTransform::Pi() ;
506 if ( psi < 0 ) psi = psi + 2*AliHLTTPCTransform::Pi();
508 pt = (Double_t)(AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() * radius ) ;
511 fTrack->SetRadius(radius);
512 fTrack->SetCenterX(acent);
513 fTrack->SetCenterY(bcent);
515 // Get errors from fast fit
517 //if ( getPara()->getErrors ) getErrorsCircleFit ( acent, bcent, radius ) ;
523 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
524 // Fit Line in s-z plane
525 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
526 Int_t AliHLTTPCFitter::FitLine ( )
537 //find sum , sums ,sumz, sumss
540 Double_t radius = (Double_t)(fTrack->GetPt() / ( AliHLTTPCTransform::GetBFact() * AliHLTTPCTransform::GetBField() ) ) ;
542 Double_t * fS = new Double_t[(fTrack->GetNHits())];
543 Double_t *fZWeight = new Double_t[fTrack->GetNHits()];
544 UInt_t *hitnum = fTrack->GetHitNumbers();
545 if (0)//fVertexConstraint==kTRUE)
547 UInt_t id = hitnum[0];
548 Int_t slice = (id>>25) & 0x7f;
549 Int_t patch = (id>>22) & 0x7;
550 UInt_t pos = id&0x3fffff;
551 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
553 dx = points[pos].fX - fVertex->GetX();
554 dy = points[pos].fY - fVertex->GetY();
558 UInt_t id = hitnum[0];
559 Int_t slice = (id>>25) & 0x7f;
560 Int_t patch = (id>>22) & 0x7;
561 UInt_t posf = id&0x3fffff;
562 AliHLTTPCSpacePointData *pointsf = fClusters[slice][patch];
563 id = hitnum[(fTrack->GetNHits()-1)];
564 slice = (id>>25) & 0x7f;
565 patch = (id>>22) & 0x7;
566 UInt_t posl = id&0x3fffff;
567 AliHLTTPCSpacePointData *pointsl = fClusters[slice][patch];
568 dx = pointsf[posf].fX - pointsl[posl].fX;
569 dy = pointsf[posf].fY - pointsl[posl].fY;
572 Double_t localPsi = 0.5F * sqrt ( dx*dx + dy*dy ) / radius ;
575 if ( fabs(localPsi) < 1. )
577 totals = 2.0 * radius * asin ( localPsi ) ;
581 totals = 2.0 * radius * AliHLTTPCTransform::Pi() ;
586 for(Int_t i=0; i<fTrack->GetNHits(); i++)
588 UInt_t id = hitnum[i];
589 Int_t slice = (id>>25) & 0x7f;
590 Int_t patch = (id>>22) & 0x7;
591 UInt_t pos = id&0x3fffff;
592 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
594 fZWeight[i] = 1./(Double_t)(points[pos].fSigmaZ2);
598 slice = (id>>25) & 0x7f;
599 patch = (id>>22) & 0x7;
600 UInt_t lastpos = id&0x3fffff;
601 AliHLTTPCSpacePointData *lastpoints = fClusters[slice][patch];
602 dx = points[pos].fX -lastpoints[lastpos].fX;
603 dy = points[pos].fY -lastpoints[lastpos].fY;
604 dpsi = 0.5 * (Double_t)sqrt ( dx*dx + dy*dy ) / radius ;
607 fTrack->SetPsierr(dpsi);
608 s = fS[i-1] - 2.0 * radius * (Double_t)asin ( dpsi ) ;
615 ss += fZWeight[i] * fS[i];
616 sz += fZWeight[i] * points[pos].fZ;
617 sss += fZWeight[i] * fS[i] * fS[i];
618 ssz += fZWeight[i] * fS[i] * points[pos].fZ;
623 Double_t chi2,det = sum * sss - ss * ss;
624 if ( fabs(det) < 1e-20)
627 //fTrack->SetChiSq2(chi2);
631 //Compute the best fitted parameters A,B
632 Double_t tanl,z0,dtanl,dz0;
634 tanl = (Double_t)((sum * ssz - ss * sz ) / det );
635 z0 = (Double_t)((sz * sss - ssz * ss ) / det );
637 fTrack->SetTgl(tanl);
640 //calculate chi-square
644 for(Int_t i=0; i<fTrack->GetNHits(); i++)
646 UInt_t id = hitnum[i];
647 Int_t slice = (id>>25) & 0x7f;
648 Int_t patch = (id>>22) & 0x7;
649 UInt_t pos = id&0x3fffff;
650 AliHLTTPCSpacePointData *points = fClusters[slice][patch];
651 r1 = points[pos].fZ - tanl * fS[i] - z0 ;
652 chi2 += (Double_t) ( (Double_t)(fZWeight[i]) * (r1 * r1) );
655 //fTrack->SetChiSq2(chi2);
657 //calculate estimated variance
658 //varsq=chi/(double(n)-2.)
659 //calculate covariance matrix
660 //siga=sqrt(varsq*sxx/det)
661 //sigb=sqrt(varsq*sum/det)
663 dtanl = (Double_t) ( sum / det );
664 dz0 = (Double_t) ( sss / det );
666 fTrack->SetTglerr(dtanl);
667 fTrack->SetZ0err(dz0);