]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/STEERBase/AliExternalTrackParam.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / STEER / STEERBase / AliExternalTrackParam.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 // Implementation of the external track parameterisation class.              //
21 //                                                                           //
22 // This parameterisation is used to exchange tracks between the detectors.   //
23 // A set of functions returning the position and the momentum of tracks      //
24 // in the global coordinate system as well as the track impact parameters    //
25 // are implemented.
26 // Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                            //
27 ///////////////////////////////////////////////////////////////////////////////
28 #include <cassert>
29
30 #include <TVectorD.h>
31 #include <TMatrixDSym.h>
32 #include <TPolyMarker3D.h>
33 #include <TVector3.h>
34 #include <TMatrixD.h>
35
36 #include "AliExternalTrackParam.h"
37 #include "AliVVertex.h"
38 #include "AliLog.h"
39
40 ClassImp(AliExternalTrackParam)
41
42 Double32_t AliExternalTrackParam::fgMostProbablePt=kMostProbablePt;
43 Bool_t AliExternalTrackParam::fgUseLogTermMS = kFALSE;; 
44 //_____________________________________________________________________________
45 AliExternalTrackParam::AliExternalTrackParam() :
46   AliVTrack(),
47   fX(0),
48   fAlpha(0)
49 {
50   //
51   // default constructor
52   //
53   for (Int_t i = 0; i < 5; i++) fP[i] = 0;
54   for (Int_t i = 0; i < 15; i++) fC[i] = 0;
55 }
56
57 //_____________________________________________________________________________
58 AliExternalTrackParam::AliExternalTrackParam(const AliExternalTrackParam &track):
59   AliVTrack(track),
60   fX(track.fX),
61   fAlpha(track.fAlpha)
62 {
63   //
64   // copy constructor
65   //
66   for (Int_t i = 0; i < 5; i++) fP[i] = track.fP[i];
67   for (Int_t i = 0; i < 15; i++) fC[i] = track.fC[i];
68   CheckCovariance();
69 }
70
71 //_____________________________________________________________________________
72 AliExternalTrackParam& AliExternalTrackParam::operator=(const AliExternalTrackParam &trkPar)
73 {
74   //
75   // assignment operator
76   //
77   
78   if (this!=&trkPar) {
79     AliVTrack::operator=(trkPar);
80     fX = trkPar.fX;
81     fAlpha = trkPar.fAlpha;
82
83     for (Int_t i = 0; i < 5; i++) fP[i] = trkPar.fP[i];
84     for (Int_t i = 0; i < 15; i++) fC[i] = trkPar.fC[i];
85     CheckCovariance();
86   }
87
88   return *this;
89 }
90
91 //_____________________________________________________________________________
92 AliExternalTrackParam::AliExternalTrackParam(Double_t x, Double_t alpha, 
93                                              const Double_t param[5], 
94                                              const Double_t covar[15]) :
95   AliVTrack(),
96   fX(x),
97   fAlpha(alpha)
98 {
99   //
100   // create external track parameters from given arguments
101   //
102   for (Int_t i = 0; i < 5; i++)  fP[i] = param[i];
103   for (Int_t i = 0; i < 15; i++) fC[i] = covar[i];
104   CheckCovariance();
105 }
106
107 //_____________________________________________________________________________
108 void AliExternalTrackParam::CopyFromVTrack(const AliVTrack *vTrack)
109 {
110   //
111   // Recreate TrackParams from VTrack
112   // This is not a copy contructor !
113   //
114   if (!vTrack) {
115     AliError("Source VTrack is NULL");
116     return;
117   }
118   if (this==vTrack) {
119     AliError("Copy of itself is requested");
120     return;
121   }
122   //
123   if (vTrack->InheritsFrom(AliExternalTrackParam::Class())) {
124     AliDebug(1,"Source itself is AliExternalTrackParam, using assignment operator");
125     *this = *(AliExternalTrackParam*)vTrack;
126     return;
127   }
128   //
129   AliVTrack::operator=( *vTrack );
130   //
131   Double_t xyz[3],pxpypz[3],cv[21];
132   vTrack->GetXYZ(xyz);
133   pxpypz[0]=vTrack->Px();
134   pxpypz[1]=vTrack->Py();
135   pxpypz[2]=vTrack->Pz();
136   vTrack->GetCovarianceXYZPxPyPz(cv);
137   Short_t sign = (Short_t)vTrack->Charge();
138   Set(xyz,pxpypz,cv,sign);
139 }
140
141 //_____________________________________________________________________________
142 AliExternalTrackParam::AliExternalTrackParam(const AliVTrack *vTrack) :
143   AliVTrack(),
144   fX(0.),
145   fAlpha(0.)
146 {
147   //
148   // Constructor from virtual track,
149   // This is not a copy contructor !
150   //
151
152   if (vTrack->InheritsFrom("AliExternalTrackParam")) {
153      AliError("This is not a copy constructor. Use AliExternalTrackParam(const AliExternalTrackParam &) !");
154      AliWarning("Calling the default constructor...");
155      AliExternalTrackParam();
156      return;
157   }
158
159   Double_t xyz[3],pxpypz[3],cv[21];
160   vTrack->GetXYZ(xyz);
161   pxpypz[0]=vTrack->Px();
162   pxpypz[1]=vTrack->Py();
163   pxpypz[2]=vTrack->Pz();
164   vTrack->GetCovarianceXYZPxPyPz(cv);
165   Short_t sign = (Short_t)vTrack->Charge();
166
167   Set(xyz,pxpypz,cv,sign);
168 }
169
170 //_____________________________________________________________________________
171 AliExternalTrackParam::AliExternalTrackParam(Double_t xyz[3],Double_t pxpypz[3],
172                                              Double_t cv[21],Short_t sign) :
173   AliVTrack(),
174   fX(0.),
175   fAlpha(0.)
176 {
177   //
178   // constructor from the global parameters
179   //
180
181   Set(xyz,pxpypz,cv,sign);
182 }
183
184 //_____________________________________________________________________________
185 void AliExternalTrackParam::Set(Double_t xyz[3],Double_t pxpypz[3],
186                                 Double_t cv[21],Short_t sign) 
187 {
188   //
189   // create external track parameters from the global parameters
190   // x,y,z,px,py,pz and their 6x6 covariance matrix
191   // A.Dainese 10.10.08
192
193   // Calculate alpha: the rotation angle of the corresponding local system.
194   //
195   // For global radial position inside the beam pipe, alpha is the
196   // azimuthal angle of the momentum projected on (x,y).
197   //
198   // For global radial position outside the ITS, alpha is the
199   // azimuthal angle of the centre of the TPC sector in which the point
200   // xyz lies
201   //
202   const double kSafe = 1e-5;
203   Double_t radPos2 = xyz[0]*xyz[0]+xyz[1]*xyz[1];  
204   Double_t radMax  = 45.; // approximately ITS outer radius
205   if (radPos2 < radMax*radMax) { // inside the ITS     
206      fAlpha = TMath::ATan2(pxpypz[1],pxpypz[0]);
207   } else { // outside the ITS
208      Float_t phiPos = TMath::Pi()+TMath::ATan2(-xyz[1], -xyz[0]);
209      fAlpha = 
210      TMath::DegToRad()*(20*((((Int_t)(phiPos*TMath::RadToDeg()))/20))+10);
211   }
212   //
213   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
214   // protection:  avoid alpha being too close to 0 or +-pi/2
215   if (TMath::Abs(sn)<2*kSafe) {
216     if (fAlpha>0) fAlpha += fAlpha< TMath::Pi()/2. ?  2*kSafe : -2*kSafe;
217     else          fAlpha += fAlpha>-TMath::Pi()/2. ? -2*kSafe :  2*kSafe;
218     cs=TMath::Cos(fAlpha);
219     sn=TMath::Sin(fAlpha);
220   }
221   else if (TMath::Abs(cs)<2*kSafe) {
222     if (fAlpha>0) fAlpha += fAlpha> TMath::Pi()/2. ? 2*kSafe : -2*kSafe;
223     else          fAlpha += fAlpha>-TMath::Pi()/2. ? 2*kSafe : -2*kSafe;
224     cs=TMath::Cos(fAlpha);
225     sn=TMath::Sin(fAlpha);
226   }
227   // Get the vertex of origin and the momentum
228   TVector3 ver(xyz[0],xyz[1],xyz[2]);
229   TVector3 mom(pxpypz[0],pxpypz[1],pxpypz[2]);
230   //
231   // avoid momenta along axis
232   if (TMath::Abs(mom[0])<kSafe) mom[0] = TMath::Sign(kSafe*TMath::Abs(mom[1]), mom[0]);
233   if (TMath::Abs(mom[1])<kSafe) mom[1] = TMath::Sign(kSafe*TMath::Abs(mom[0]), mom[1]);
234
235   // Rotate to the local coordinate system
236   ver.RotateZ(-fAlpha);
237   mom.RotateZ(-fAlpha);
238
239   // x of the reference plane
240   fX = ver.X();
241
242   Double_t charge = (Double_t)sign;
243
244   fP[0] = ver.Y();
245   fP[1] = ver.Z();
246   fP[2] = TMath::Sin(mom.Phi());
247   fP[3] = mom.Pz()/mom.Pt();
248   fP[4] = TMath::Sign(1/mom.Pt(),charge);
249
250   // Covariance matrix (formulas to be simplified)
251
252   if      (TMath::Abs( 1-fP[2]) < kSafe) fP[2] = 1.- kSafe; //Protection
253   else if (TMath::Abs(-1-fP[2]) < kSafe) fP[2] =-1.+ kSafe; //Protection
254
255   Double_t pt=1./TMath::Abs(fP[4]);
256   Double_t r=TMath::Sqrt((1.-fP[2])*(1.+fP[2]));
257
258   Double_t m00=-sn;// m10=cs;
259   Double_t m23=-pt*(sn + fP[2]*cs/r), m43=-pt*pt*(r*cs - fP[2]*sn);
260   Double_t m24= pt*(cs - fP[2]*sn/r), m44=-pt*pt*(r*sn + fP[2]*cs);
261   Double_t m35=pt, m45=-pt*pt*fP[3];
262
263   m43*=GetSign();
264   m44*=GetSign();
265   m45*=GetSign();
266
267   Double_t cv34 = TMath::Sqrt(cv[3 ]*cv[3 ]+cv[4 ]*cv[4 ]);
268   Double_t a1=cv[13]-cv[9]*(m23*m44+m43*m24)/m23/m43;
269   Double_t a2=m23*m24-m23*(m23*m44+m43*m24)/m43;
270   Double_t a3=m43*m44-m43*(m23*m44+m43*m24)/m23;
271   Double_t a4=cv[14]-2.*cv[9]*m24*m44/m23/m43;
272   Double_t a5=m24*m24-2.*m24*m44*m23/m43;
273   Double_t a6=m44*m44-2.*m24*m44*m43/m23;
274
275   fC[0 ] = cv[0 ]+cv[2 ];  
276   fC[1 ] = TMath::Sign(cv34,cv[3 ]/m00); 
277   fC[2 ] = cv[5 ]; 
278   fC[3 ] = (cv[10]*m43-cv[6]*m44)/(m24*m43-m23*m44)/m00; 
279   fC[10] = (cv[6]/m00-fC[3 ]*m23)/m43; 
280   fC[6 ] = (cv[15]/m00-fC[10]*m45)/m35; 
281   fC[4 ] = (cv[12]*m43-cv[8]*m44)/(m24*m43-m23*m44); 
282   fC[11] = (cv[8]-fC[4]*m23)/m43; 
283   fC[7 ] = cv[17]/m35-fC[11]*m45/m35; 
284   fC[5 ] = TMath::Abs((a4*a3-a6*a1)/(a5*a3-a6*a2));
285   fC[14] = TMath::Abs((a1-a2*fC[5])/a3);
286   fC[12] = (cv[9]-fC[5]*m23*m23-fC[14]*m43*m43)/m23/m43;
287   Double_t b1=cv[18]-fC[12]*m23*m45-fC[14]*m43*m45;
288   Double_t b2=m23*m35;
289   Double_t b3=m43*m35;
290   Double_t b4=cv[19]-fC[12]*m24*m45-fC[14]*m44*m45;
291   Double_t b5=m24*m35;
292   Double_t b6=m44*m35;
293   fC[8 ] = (b4-b6*b1/b3)/(b5-b6*b2/b3);
294   fC[13] = b1/b3-b2*fC[8]/b3;
295   fC[9 ] = TMath::Abs((cv[20]-fC[14]*(m45*m45)-fC[13]*2.*m35*m45)/(m35*m35));
296
297   CheckCovariance();
298
299   return;
300 }
301
302 //_____________________________________________________________________________
303 void AliExternalTrackParam::Reset() {
304   //
305   // Resets all the parameters to 0 
306   //
307   fX=fAlpha=0.;
308   for (Int_t i = 0; i < 5; i++) fP[i] = 0;
309   for (Int_t i = 0; i < 15; i++) fC[i] = 0;
310 }
311
312 //_____________________________________________________________________________
313 void AliExternalTrackParam::AddCovariance(const Double_t c[15]) {
314   //
315   // Add "something" to the track covarince matrix.
316   // May be needed to account for unknown mis-calibration/mis-alignment
317   //
318     fC[0] +=c[0];
319     fC[1] +=c[1];  fC[2] +=c[2];
320     fC[3] +=c[3];  fC[4] +=c[4];  fC[5] +=c[5];
321     fC[6] +=c[6];  fC[7] +=c[7];  fC[8] +=c[8];  fC[9] +=c[9];
322     fC[10]+=c[10]; fC[11]+=c[11]; fC[12]+=c[12]; fC[13]+=c[13]; fC[14]+=c[14];
323     CheckCovariance();
324 }
325
326
327 Double_t AliExternalTrackParam::GetP() const {
328   //---------------------------------------------------------------------
329   // This function returns the track momentum
330   // Results for (nearly) straight tracks are meaningless !
331   //---------------------------------------------------------------------
332   if (TMath::Abs(fP[4])<=kAlmost0) return kVeryBig;
333   return TMath::Sqrt(1.+ fP[3]*fP[3])/TMath::Abs(fP[4]);
334 }
335
336 Double_t AliExternalTrackParam::Get1P() const {
337   //---------------------------------------------------------------------
338   // This function returns the 1/(track momentum)
339   //---------------------------------------------------------------------
340   return TMath::Abs(fP[4])/TMath::Sqrt(1.+ fP[3]*fP[3]);
341 }
342
343 //_______________________________________________________________________
344 Double_t AliExternalTrackParam::GetD(Double_t x,Double_t y,Double_t b) const {
345   //------------------------------------------------------------------
346   // This function calculates the transverse impact parameter
347   // with respect to a point with global coordinates (x,y)
348   // in the magnetic field "b" (kG)
349   //------------------------------------------------------------------
350   if (TMath::Abs(b) < kAlmost0Field) return GetLinearD(x,y);
351   Double_t rp4=GetC(b);
352
353   Double_t xt=fX, yt=fP[0];
354
355   Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
356   Double_t a = x*cs + y*sn;
357   y = -x*sn + y*cs; x=a;
358   xt-=x; yt-=y;
359
360   sn=rp4*xt - fP[2]; cs=rp4*yt + TMath::Sqrt((1.- fP[2])*(1.+fP[2]));
361   a=2*(xt*fP[2] - yt*TMath::Sqrt((1.-fP[2])*(1.+fP[2])))-rp4*(xt*xt + yt*yt);
362   return  -a/(1 + TMath::Sqrt(sn*sn + cs*cs));
363 }
364
365 //_______________________________________________________________________
366 void AliExternalTrackParam::
367 GetDZ(Double_t x, Double_t y, Double_t z, Double_t b, Float_t dz[2]) const {
368   //------------------------------------------------------------------
369   // This function calculates the transverse and longitudinal impact parameters
370   // with respect to a point with global coordinates (x,y)
371   // in the magnetic field "b" (kG)
372   //------------------------------------------------------------------
373   Double_t f1 = fP[2], r1 = TMath::Sqrt((1.-f1)*(1.+f1));
374   Double_t xt=fX, yt=fP[0];
375   Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
376   Double_t a = x*cs + y*sn;
377   y = -x*sn + y*cs; x=a;
378   xt-=x; yt-=y;
379
380   Double_t rp4=GetC(b);
381   if ((TMath::Abs(b) < kAlmost0Field) || (TMath::Abs(rp4) < kAlmost0)) {
382      dz[0] = -(xt*f1 - yt*r1);
383      dz[1] = fP[1] + (dz[0]*f1 - xt)/r1*fP[3] - z;
384      return;
385   }
386
387   sn=rp4*xt - f1; cs=rp4*yt + r1;
388   a=2*(xt*f1 - yt*r1)-rp4*(xt*xt + yt*yt);
389   Double_t rr=TMath::Sqrt(sn*sn + cs*cs);
390   dz[0] = -a/(1 + rr);
391   Double_t f2 = -sn/rr, r2 = TMath::Sqrt((1.-f2)*(1.+f2));
392   dz[1] = fP[1] + fP[3]/rp4*TMath::ASin(f2*r1 - f1*r2) - z;
393 }
394
395 //_______________________________________________________________________
396 Double_t AliExternalTrackParam::GetLinearD(Double_t xv,Double_t yv) const {
397   //------------------------------------------------------------------
398   // This function calculates the transverse impact parameter
399   // with respect to a point with global coordinates (xv,yv)
400   // neglecting the track curvature.
401   //------------------------------------------------------------------
402   Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
403   Double_t x= xv*cs + yv*sn;
404   Double_t y=-xv*sn + yv*cs;
405
406   Double_t d = (fX-x)*fP[2] - (fP[0]-y)*TMath::Sqrt((1.-fP[2])*(1.+fP[2]));
407
408   return -d;
409 }
410
411 Bool_t AliExternalTrackParam::CorrectForMeanMaterialdEdx
412 (Double_t xOverX0,  Double_t xTimesRho, Double_t mass, 
413  Double_t dEdx,
414  Bool_t anglecorr) {
415   //------------------------------------------------------------------
416   // This function corrects the track parameters for the crossed material.
417   // "xOverX0"   - X/X0, the thickness in units of the radiation length.
418   // "xTimesRho" - is the product length*density (g/cm^2).
419   //     It should be passed as negative when propagating tracks 
420   //     from the intreaction point to the outside of the central barrel. 
421   // "mass" - the mass of this particle (GeV/c^2).
422   // "dEdx" - mean enery loss (GeV/(g/cm^2)
423   // "anglecorr" - switch for the angular correction
424   //------------------------------------------------------------------
425   Double_t &fP2=fP[2];
426   Double_t &fP3=fP[3];
427   Double_t &fP4=fP[4];
428
429   Double_t &fC22=fC[5];
430   Double_t &fC33=fC[9];
431   Double_t &fC43=fC[13];
432   Double_t &fC44=fC[14];
433
434   //Apply angle correction, if requested
435   if(anglecorr) {
436     Double_t angle=TMath::Sqrt((1.+ fP3*fP3)/((1-fP2)*(1.+fP2)));
437     xOverX0 *=angle;
438     xTimesRho *=angle;
439   } 
440
441   Double_t p=GetP();
442   Double_t p2=p*p;
443   Double_t beta2=p2/(p2 + mass*mass);
444
445   //Calculating the multiple scattering corrections******************
446   Double_t cC22 = 0.;
447   Double_t cC33 = 0.;
448   Double_t cC43 = 0.;
449   Double_t cC44 = 0.;
450   if (xOverX0 != 0) {
451     //Double_t theta2=1.0259e-6*14*14/28/(beta2*p2)*TMath::Abs(d)*9.36*2.33;
452     Double_t theta2=0.0136*0.0136/(beta2*p2)*TMath::Abs(xOverX0);
453     if (GetUseLogTermMS()) {
454       double lt = 1+0.038*TMath::Log(TMath::Abs(xOverX0));
455       if (lt>0) theta2 *= lt*lt;
456     }
457     if(theta2>TMath::Pi()*TMath::Pi()) return kFALSE;
458     cC22 = theta2*((1.-fP2)*(1.+fP2))*(1. + fP3*fP3);
459     cC33 = theta2*(1. + fP3*fP3)*(1. + fP3*fP3);
460     cC43 = theta2*fP3*fP4*(1. + fP3*fP3);
461     cC44 = theta2*fP3*fP4*fP3*fP4;
462   }
463
464   //Calculating the energy loss corrections************************
465   Double_t cP4=1.;
466   if ((xTimesRho != 0.) && (beta2 < 1.)) {
467      Double_t dE=dEdx*xTimesRho;
468      Double_t e=TMath::Sqrt(p2 + mass*mass);
469      if ( TMath::Abs(dE) > 0.3*e ) return kFALSE; //30% energy loss is too much!
470      //cP4 = (1.- e/p2*dE);
471      if ( (1.+ dE/p2*(dE + 2*e)) < 0. ) return kFALSE;
472      cP4 = 1./TMath::Sqrt(1.+ dE/p2*(dE + 2*e));  //A precise formula by Ruben !
473      if (TMath::Abs(fP4*cP4)>100.) return kFALSE; //Do not track below 10 MeV/c
474
475
476      // Approximate energy loss fluctuation (M.Ivanov)
477      const Double_t knst=0.07; // To be tuned.  
478      Double_t sigmadE=knst*TMath::Sqrt(TMath::Abs(dE)); 
479      cC44 += ((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
480  
481   }
482
483   //Applying the corrections*****************************
484   fC22 += cC22;
485   fC33 += cC33;
486   fC43 += cC43;
487   fC44 += cC44;
488   fP4  *= cP4;
489
490   CheckCovariance();
491
492   return kTRUE;
493 }
494
495 Bool_t AliExternalTrackParam::CorrectForMeanMaterial
496 (Double_t xOverX0,  Double_t xTimesRho, Double_t mass, 
497  Bool_t anglecorr,
498  Double_t (*Bethe)(Double_t)) {
499   //------------------------------------------------------------------
500   // This function corrects the track parameters for the crossed material.
501   // "xOverX0"   - X/X0, the thickness in units of the radiation length.
502   // "xTimesRho" - is the product length*density (g/cm^2). 
503   //     It should be passed as negative when propagating tracks 
504   //     from the intreaction point to the outside of the central barrel. 
505   // "mass" - the mass of this particle (GeV/c^2).
506   // "anglecorr" - switch for the angular correction
507   // "Bethe" - function calculating the energy loss (GeV/(g/cm^2)) 
508   //------------------------------------------------------------------
509   Double_t bg=GetP()/mass;
510   if (bg<kAlmost0) {
511     AliError(Form("Non-positive beta*gamma = %e, mass = %e",bg,mass));
512     return kFALSE;
513   }
514   Double_t dEdx=Bethe(bg);
515
516   return CorrectForMeanMaterialdEdx(xOverX0,xTimesRho,mass,dEdx,anglecorr);
517 }
518
519 Bool_t AliExternalTrackParam::CorrectForMeanMaterialZA
520 (Double_t xOverX0, Double_t xTimesRho, Double_t mass,
521  Double_t zOverA,
522  Double_t density,
523  Double_t exEnergy,
524  Double_t jp1,
525  Double_t jp2,
526  Bool_t anglecorr) {
527   //------------------------------------------------------------------
528   // This function corrects the track parameters for the crossed material
529   // using the full Geant-like Bethe-Bloch formula parameterization
530   // "xOverX0"   - X/X0, the thickness in units of the radiation length.
531   // "xTimesRho" - is the product length*density (g/cm^2). 
532   //     It should be passed as negative when propagating tracks 
533   //     from the intreaction point to the outside of the central barrel. 
534   // "mass" - the mass of this particle (GeV/c^2).
535   // "density"  - mean density (g/cm^3)
536   // "zOverA"   - mean Z/A
537   // "exEnergy" - mean exitation energy (GeV)
538   // "jp1"      - density effect first junction point
539   // "jp2"      - density effect second junction point
540   // "anglecorr" - switch for the angular correction
541   //
542   //  The default values of the parameters are for silicon 
543   //
544   //------------------------------------------------------------------
545
546   Double_t bg=GetP()/mass;
547   Double_t dEdx=BetheBlochGeant(bg,density,jp1,jp2,exEnergy,zOverA);
548
549   return CorrectForMeanMaterialdEdx(xOverX0,xTimesRho,mass,dEdx,anglecorr);
550 }
551
552
553
554 Bool_t AliExternalTrackParam::CorrectForMaterial
555 (Double_t d,  Double_t x0, Double_t mass, Double_t (*Bethe)(Double_t)) {
556   //------------------------------------------------------------------
557   //                    Deprecated function !   
558   //       Better use CorrectForMeanMaterial instead of it.
559   //
560   // This function corrects the track parameters for the crossed material
561   // "d"    - the thickness (fraction of the radiation length)
562   //     It should be passed as negative when propagating tracks 
563   //     from the intreaction point to the outside of the central barrel. 
564   // "x0"   - the radiation length (g/cm^2) 
565   // "mass" - the mass of this particle (GeV/c^2)
566   //------------------------------------------------------------------
567
568   return CorrectForMeanMaterial(d,x0*d,mass,kTRUE,Bethe);
569
570 }
571
572 Double_t AliExternalTrackParam::BetheBlochAleph(Double_t bg,
573          Double_t kp1,
574          Double_t kp2,
575          Double_t kp3,
576          Double_t kp4,
577          Double_t kp5) {
578   //
579   // This is the empirical ALEPH parameterization of the Bethe-Bloch formula.
580   // It is normalized to 1 at the minimum.
581   //
582   // bg - beta*gamma
583   // 
584   // The default values for the kp* parameters are for ALICE TPC.
585   // The returned value is in MIP units
586   //
587
588   Double_t beta = bg/TMath::Sqrt(1.+ bg*bg);
589
590   Double_t aa = TMath::Power(beta,kp4);
591   Double_t bb = TMath::Power(1./bg,kp5);
592
593   bb=TMath::Log(kp3+bb);
594   
595   return (kp2-aa-bb)*kp1/aa;
596 }
597
598 Double_t AliExternalTrackParam::BetheBlochGeant(Double_t bg,
599          Double_t kp0,
600          Double_t kp1,
601          Double_t kp2,
602          Double_t kp3,
603          Double_t kp4) {
604   //
605   // This is the parameterization of the Bethe-Bloch formula inspired by Geant.
606   //
607   // bg  - beta*gamma
608   // kp0 - density [g/cm^3]
609   // kp1 - density effect first junction point
610   // kp2 - density effect second junction point
611   // kp3 - mean excitation energy [GeV]
612   // kp4 - mean Z/A
613   //
614   // The default values for the kp* parameters are for silicon. 
615   // The returned value is in [GeV/(g/cm^2)].
616   // 
617
618   const Double_t mK  = 0.307075e-3; // [GeV*cm^2/g]
619   const Double_t me  = 0.511e-3;    // [GeV/c^2]
620   const Double_t rho = kp0;
621   const Double_t x0  = kp1*2.303;
622   const Double_t x1  = kp2*2.303;
623   const Double_t mI  = kp3;
624   const Double_t mZA = kp4;
625   const Double_t bg2 = bg*bg;
626   const Double_t maxT= 2*me*bg2;    // neglecting the electron mass
627   
628   //*** Density effect
629   Double_t d2=0.; 
630   const Double_t x=TMath::Log(bg);
631   const Double_t lhwI=TMath::Log(28.816*1e-9*TMath::Sqrt(rho*mZA)/mI);
632   if (x > x1) {
633     d2 = lhwI + x - 0.5;
634   } else if (x > x0) {
635     const Double_t r=(x1-x)/(x1-x0);
636     d2 = lhwI + x - 0.5 + (0.5 - lhwI - x0)*r*r*r;
637   }
638
639   return mK*mZA*(1+bg2)/bg2*
640          (0.5*TMath::Log(2*me*bg2*maxT/(mI*mI)) - bg2/(1+bg2) - d2);
641 }
642
643 Double_t AliExternalTrackParam::BetheBlochSolid(Double_t bg) {
644   //------------------------------------------------------------------
645   // This is an approximation of the Bethe-Bloch formula, 
646   // reasonable for solid materials. 
647   // All the parameters are, in fact, for Si.
648   // The returned value is in [GeV/(g/cm^2)]
649   //------------------------------------------------------------------
650
651   return BetheBlochGeant(bg);
652 }
653
654 Double_t AliExternalTrackParam::BetheBlochGas(Double_t bg) {
655   //------------------------------------------------------------------
656   // This is an approximation of the Bethe-Bloch formula, 
657   // reasonable for gas materials.
658   // All the parameters are, in fact, for Ne.
659   // The returned value is in [GeV/(g/cm^2)]
660   //------------------------------------------------------------------
661
662   const Double_t rho = 0.9e-3;
663   const Double_t x0  = 2.;
664   const Double_t x1  = 4.;
665   const Double_t mI  = 140.e-9;
666   const Double_t mZA = 0.49555;
667
668   return BetheBlochGeant(bg,rho,x0,x1,mI,mZA);
669 }
670
671 Bool_t AliExternalTrackParam::Rotate(Double_t alpha) {
672   //------------------------------------------------------------------
673   // Transform this track to the local coord. system rotated
674   // by angle "alpha" (rad) with respect to the global coord. system. 
675   //------------------------------------------------------------------
676   if (TMath::Abs(fP[2]) >= kAlmost1) {
677      AliError(Form("Precondition is not satisfied: |sin(phi)|>1 ! %f",fP[2])); 
678      return kFALSE;
679   }
680  
681   if      (alpha < -TMath::Pi()) alpha += 2*TMath::Pi();
682   else if (alpha >= TMath::Pi()) alpha -= 2*TMath::Pi();
683
684   Double_t &fP0=fP[0];
685   Double_t &fP2=fP[2];
686   Double_t &fC00=fC[0];
687   Double_t &fC10=fC[1];
688   Double_t &fC20=fC[3];
689   Double_t &fC21=fC[4];
690   Double_t &fC22=fC[5];
691   Double_t &fC30=fC[6];
692   Double_t &fC32=fC[8];
693   Double_t &fC40=fC[10];
694   Double_t &fC42=fC[12];
695
696   Double_t x=fX;
697   Double_t ca=TMath::Cos(alpha-fAlpha), sa=TMath::Sin(alpha-fAlpha);
698   Double_t sf=fP2, cf=TMath::Sqrt((1.- fP2)*(1.+fP2)); // Improve precision
699   // RS: check if rotation does no invalidate track model (cos(local_phi)>=0, i.e. particle
700   // direction in local frame is along the X axis
701   if ((cf*ca+sf*sa)<0) {
702     AliDebug(1,Form("Rotation failed: local cos(phi) would become %.2f",cf*ca+sf*sa));
703     return kFALSE;
704   }
705   //
706   Double_t tmp=sf*ca - cf*sa;
707
708   if (TMath::Abs(tmp) >= kAlmost1) {
709      if (TMath::Abs(tmp) > 1.+ Double_t(FLT_EPSILON))  
710         AliWarning(Form("Rotation failed ! %.10e",tmp));
711      return kFALSE;
712   }
713   fAlpha = alpha;
714   fX =  x*ca + fP0*sa;
715   fP0= -x*sa + fP0*ca;
716   fP2=  tmp;
717
718   if (TMath::Abs(cf)<kAlmost0) {
719     AliError(Form("Too small cosine value %f",cf)); 
720     cf = kAlmost0;
721   } 
722
723   Double_t rr=(ca+sf/cf*sa);  
724
725   fC00 *= (ca*ca);
726   fC10 *= ca;
727   fC20 *= ca*rr;
728   fC21 *= rr;
729   fC22 *= rr*rr;
730   fC30 *= ca;
731   fC32 *= rr;
732   fC40 *= ca;
733   fC42 *= rr;
734
735   CheckCovariance();
736
737   return kTRUE;
738 }
739
740 Bool_t AliExternalTrackParam::Invert() {
741   //------------------------------------------------------------------
742   // Transform this track to the local coord. system rotated by 180 deg. 
743   //------------------------------------------------------------------
744   fX = -fX;
745   fAlpha += TMath::Pi();
746   while (fAlpha < -TMath::Pi()) fAlpha += 2*TMath::Pi();
747   while (fAlpha >= TMath::Pi()) fAlpha -= 2*TMath::Pi();
748   //
749   fP[0] = -fP[0];
750   //fP[2] = -fP[2];
751   fP[3] = -fP[3];
752   fP[4] = -fP[4];
753   //
754   fC[1] = -fC[1]; // since the fP1 and fP2 are not inverted, their covariances with others change sign
755   fC[3] = -fC[3];
756   fC[7] = -fC[7];
757   fC[8] = -fC[8]; 
758   fC[11] = -fC[11]; 
759   fC[12] = -fC[12]; 
760   //
761   return kTRUE;
762 }
763
764 Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
765   //----------------------------------------------------------------
766   // Propagate this track to the plane X=xk (cm) in the field "b" (kG)
767   //----------------------------------------------------------------
768   Double_t dx=xk-fX;
769   if (TMath::Abs(dx)<=kAlmost0)  return kTRUE;
770
771   Double_t crv=GetC(b);
772   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
773
774   Double_t x2r = crv*dx;
775   Double_t f1=fP[2], f2=f1 + x2r;
776   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
777   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
778   if (TMath::Abs(fP[4])< kAlmost0) return kFALSE;
779
780   Double_t &fP0=fP[0], &fP1=fP[1], &fP2=fP[2], &fP3=fP[3], &fP4=fP[4];
781   Double_t 
782   &fC00=fC[0],
783   &fC10=fC[1],   &fC11=fC[2],  
784   &fC20=fC[3],   &fC21=fC[4],   &fC22=fC[5],
785   &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
786   &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
787
788   Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
789   if (TMath::Abs(r1)<kAlmost0)  return kFALSE;
790   if (TMath::Abs(r2)<kAlmost0)  return kFALSE;
791
792   fX=xk;
793   double dy2dx = (f1+f2)/(r1+r2);
794   fP0 += dx*dy2dx;
795   if (TMath::Abs(x2r)<0.05) {
796     fP1 += dx*(r2 + f2*dy2dx)*fP3;  // Many thanks to P.Hristov !
797     fP2 += x2r;
798   }
799   else { 
800     // for small dx/R the linear apporximation of the arc by the segment is OK,
801     // but at large dx/R the error is very large and leads to incorrect Z propagation
802     // angle traversed delta = 2*asin(dist_start_end / R / 2), hence the arc is: R*deltaPhi
803     // The dist_start_end is obtained from sqrt(dx^2+dy^2) = x/(r1+r2)*sqrt(2+f1*f2+r1*r2)
804     // Similarly, the rotation angle in linear in dx only for dx<<R
805     double chord = dx*TMath::Sqrt(1+dy2dx*dy2dx);   // distance from old position to new one
806     double rot = 2*TMath::ASin(0.5*chord*crv); // angular difference seen from the circle center
807     fP1 += rot/crv*fP3;
808     fP2  = TMath::Sin(rot + TMath::ASin(fP2));
809   }
810
811   //f = F - 1
812   /*
813   Double_t f02=    dx/(r1*r1*r1);            Double_t cc=crv/fP4;
814   Double_t f04=0.5*dx*dx/(r1*r1*r1);         f04*=cc;
815   Double_t f12=    dx*fP3*f1/(r1*r1*r1);
816   Double_t f14=0.5*dx*dx*fP3*f1/(r1*r1*r1);  f14*=cc;
817   Double_t f13=    dx/r1;
818   Double_t f24=    dx;                       f24*=cc;
819   */
820   Double_t rinv = 1./r1;
821   Double_t r3inv = rinv*rinv*rinv;
822   Double_t f24=    x2r/fP4;
823   Double_t f02=    dx*r3inv;
824   Double_t f04=0.5*f24*f02;
825   Double_t f12=    f02*fP3*f1;
826   Double_t f14=0.5*f24*f02*fP3*f1;
827   Double_t f13=    dx*rinv;
828
829   //b = C*ft
830   Double_t b00=f02*fC20 + f04*fC40, b01=f12*fC20 + f14*fC40 + f13*fC30;
831   Double_t b02=f24*fC40;
832   Double_t b10=f02*fC21 + f04*fC41, b11=f12*fC21 + f14*fC41 + f13*fC31;
833   Double_t b12=f24*fC41;
834   Double_t b20=f02*fC22 + f04*fC42, b21=f12*fC22 + f14*fC42 + f13*fC32;
835   Double_t b22=f24*fC42;
836   Double_t b40=f02*fC42 + f04*fC44, b41=f12*fC42 + f14*fC44 + f13*fC43;
837   Double_t b42=f24*fC44;
838   Double_t b30=f02*fC32 + f04*fC43, b31=f12*fC32 + f14*fC43 + f13*fC33;
839   Double_t b32=f24*fC43;
840   
841   //a = f*b = f*C*ft
842   Double_t a00=f02*b20+f04*b40,a01=f02*b21+f04*b41,a02=f02*b22+f04*b42;
843   Double_t a11=f12*b21+f14*b41+f13*b31,a12=f12*b22+f14*b42+f13*b32;
844   Double_t a22=f24*b42;
845
846   //F*C*Ft = C + (b + bt + a)
847   fC00 += b00 + b00 + a00;
848   fC10 += b10 + b01 + a01; 
849   fC20 += b20 + b02 + a02;
850   fC30 += b30;
851   fC40 += b40;
852   fC11 += b11 + b11 + a11;
853   fC21 += b21 + b12 + a12;
854   fC31 += b31; 
855   fC41 += b41;
856   fC22 += b22 + b22 + a22;
857   fC32 += b32;
858   fC42 += b42;
859
860   CheckCovariance();
861
862   return kTRUE;
863 }
864
865 Bool_t AliExternalTrackParam::PropagateParamOnlyTo(Double_t xk, Double_t b) {
866   //----------------------------------------------------------------
867   // Propagate this track to the plane X=xk (cm) in the field "b" (kG)
868   // Only parameters are propagated, not the matrix. To be used for small 
869   // distances only (<mm, i.e. misalignment)
870   //----------------------------------------------------------------
871   Double_t dx=xk-fX;
872   if (TMath::Abs(dx)<=kAlmost0)  return kTRUE;
873
874   Double_t crv=GetC(b);
875   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
876
877   Double_t x2r = crv*dx;
878   Double_t f1=fP[2], f2=f1 + x2r;
879   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
880   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
881   if (TMath::Abs(fP[4])< kAlmost0) return kFALSE;
882
883   Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
884   if (TMath::Abs(r1)<kAlmost0)  return kFALSE;
885   if (TMath::Abs(r2)<kAlmost0)  return kFALSE;
886
887   fX=xk;
888   double dy2dx = (f1+f2)/(r1+r2);
889   fP[0] += dx*dy2dx;
890   fP[1] += dx*(r2 + f2*dy2dx)*fP[3];  // Many thanks to P.Hristov !
891   fP[2] += x2r;
892
893   return kTRUE;
894 }
895
896 Bool_t 
897 AliExternalTrackParam::Propagate(Double_t alpha, Double_t x, Double_t b) {
898   //------------------------------------------------------------------
899   // Transform this track to the local coord. system rotated
900   // by angle "alpha" (rad) with respect to the global coord. system, 
901   // and propagate this track to the plane X=xk (cm) in the field "b" (kG)
902   //------------------------------------------------------------------
903   
904   //Save the parameters
905   Double_t as=fAlpha;
906   Double_t xs=fX;
907   Double_t ps[5], cs[15];
908   for (Int_t i=0; i<5;  i++) ps[i]=fP[i]; 
909   for (Int_t i=0; i<15; i++) cs[i]=fC[i]; 
910
911   if (Rotate(alpha))
912      if (PropagateTo(x,b)) return kTRUE;
913
914   //Restore the parameters, if the operation failed
915   fAlpha=as;
916   fX=xs;
917   for (Int_t i=0; i<5;  i++) fP[i]=ps[i]; 
918   for (Int_t i=0; i<15; i++) fC[i]=cs[i]; 
919   return kFALSE;
920 }
921
922 Bool_t AliExternalTrackParam::PropagateBxByBz
923 (Double_t alpha, Double_t x, Double_t b[3]) {
924   //------------------------------------------------------------------
925   // Transform this track to the local coord. system rotated
926   // by angle "alpha" (rad) with respect to the global coord. system, 
927   // and propagate this track to the plane X=xk (cm),
928   // taking into account all three components of the B field, "b[3]" (kG)
929   //------------------------------------------------------------------
930   
931   //Save the parameters
932   Double_t as=fAlpha;
933   Double_t xs=fX;
934   Double_t ps[5], cs[15];
935   for (Int_t i=0; i<5;  i++) ps[i]=fP[i]; 
936   for (Int_t i=0; i<15; i++) cs[i]=fC[i]; 
937
938   if (Rotate(alpha))
939      if (PropagateToBxByBz(x,b)) return kTRUE;
940
941   //Restore the parameters, if the operation failed
942   fAlpha=as;
943   fX=xs;
944   for (Int_t i=0; i<5;  i++) fP[i]=ps[i]; 
945   for (Int_t i=0; i<15; i++) fC[i]=cs[i]; 
946   return kFALSE;
947 }
948
949
950 void AliExternalTrackParam::Propagate(Double_t len, Double_t x[3],
951 Double_t p[3], Double_t bz) const {
952   //+++++++++++++++++++++++++++++++++++++++++    
953   // Origin: K. Shileev (Kirill.Shileev@cern.ch)
954   // Extrapolate track along simple helix in magnetic field
955   // Arguments: len -distance alogn helix, [cm]
956   //            bz  - mag field, [kGaus]   
957   // Returns: x and p contain extrapolated positon and momentum  
958   // The momentum returned for straight-line tracks is meaningless !
959   //+++++++++++++++++++++++++++++++++++++++++    
960   GetXYZ(x);
961     
962   if (OneOverPt() < kAlmost0 || TMath::Abs(bz) < kAlmost0Field || GetC(bz) < kAlmost0){ //straight-line tracks
963      Double_t unit[3]; GetDirection(unit);
964      x[0]+=unit[0]*len;   
965      x[1]+=unit[1]*len;   
966      x[2]+=unit[2]*len;
967
968      p[0]=unit[0]/kAlmost0;   
969      p[1]=unit[1]/kAlmost0;   
970      p[2]=unit[2]/kAlmost0;   
971   } else {
972      GetPxPyPz(p);
973      Double_t pp=GetP();
974      Double_t a = -kB2C*bz*GetSign();
975      Double_t rho = a/pp;
976      x[0] += p[0]*TMath::Sin(rho*len)/a - p[1]*(1-TMath::Cos(rho*len))/a;
977      x[1] += p[1]*TMath::Sin(rho*len)/a + p[0]*(1-TMath::Cos(rho*len))/a;
978      x[2] += p[2]*len/pp;
979
980      Double_t p0=p[0];
981      p[0] = p0  *TMath::Cos(rho*len) - p[1]*TMath::Sin(rho*len);
982      p[1] = p[1]*TMath::Cos(rho*len) + p0  *TMath::Sin(rho*len);
983   }
984 }
985
986 Bool_t AliExternalTrackParam::Intersect(Double_t pnt[3], Double_t norm[3],
987 Double_t bz) const {
988   //+++++++++++++++++++++++++++++++++++++++++    
989   // Origin: K. Shileev (Kirill.Shileev@cern.ch)
990   // Finds point of intersection (if exists) of the helix with the plane. 
991   // Stores result in fX and fP.   
992   // Arguments: planePoint,planeNorm - the plane defined by any plane's point 
993   // and vector, normal to the plane
994   // Returns: kTrue if helix intersects the plane, kFALSE otherwise.
995   //+++++++++++++++++++++++++++++++++++++++++    
996   Double_t x0[3]; GetXYZ(x0); //get track position in MARS
997   
998   //estimates initial helix length up to plane
999   Double_t s=
1000     (pnt[0]-x0[0])*norm[0] + (pnt[1]-x0[1])*norm[1] + (pnt[2]-x0[2])*norm[2];
1001   Double_t dist=99999,distPrev=dist;
1002   Double_t x[3],p[3]; 
1003   while(TMath::Abs(dist)>0.00001){
1004     //calculates helix at the distance s from x0 ALONG the helix
1005     Propagate(s,x,p,bz);
1006
1007     //distance between current helix position and plane
1008     dist=(x[0]-pnt[0])*norm[0]+(x[1]-pnt[1])*norm[1]+(x[2]-pnt[2])*norm[2];
1009
1010     if(TMath::Abs(dist) >= TMath::Abs(distPrev)) {return kFALSE;}
1011     distPrev=dist;
1012     s-=dist;
1013   }
1014   //on exit pnt is intersection point,norm is track vector at that point, 
1015   //all in MARS
1016   for (Int_t i=0; i<3; i++) {pnt[i]=x[i]; norm[i]=p[i];}
1017   return kTRUE;
1018 }
1019
1020 Double_t 
1021 AliExternalTrackParam::GetPredictedChi2(Double_t p[2],Double_t cov[3]) const {
1022   //----------------------------------------------------------------
1023   // Estimate the chi2 of the space point "p" with the cov. matrix "cov"
1024   //----------------------------------------------------------------
1025   Double_t sdd = fC[0] + cov[0]; 
1026   Double_t sdz = fC[1] + cov[1];
1027   Double_t szz = fC[2] + cov[2];
1028   Double_t det = sdd*szz - sdz*sdz;
1029
1030   if (TMath::Abs(det) < kAlmost0) return kVeryBig;
1031
1032   Double_t d = fP[0] - p[0];
1033   Double_t z = fP[1] - p[1];
1034
1035   return (d*szz*d - 2*d*sdz*z + z*sdd*z)/det;
1036 }
1037
1038 Double_t AliExternalTrackParam::
1039 GetPredictedChi2(Double_t p[3],Double_t covyz[3],Double_t covxyz[3]) const {
1040   //----------------------------------------------------------------
1041   // Estimate the chi2 of the 3D space point "p" and
1042   // the full covariance matrix "covyz" and "covxyz"
1043   //
1044   // Cov(x,x) ... :   covxyz[0]
1045   // Cov(y,x) ... :   covxyz[1]  covyz[0]
1046   // Cov(z,x) ... :   covxyz[2]  covyz[1]  covyz[2]
1047   //----------------------------------------------------------------
1048
1049   Double_t res[3] = {
1050     GetX() - p[0],
1051     GetY() - p[1],
1052     GetZ() - p[2]
1053   };
1054
1055   Double_t f=GetSnp();
1056   if (TMath::Abs(f) >= kAlmost1) return kVeryBig;
1057   Double_t r=TMath::Sqrt((1.-f)*(1.+f));
1058   Double_t a=f/r, b=GetTgl()/r;
1059
1060   Double_t s2=333.*333.;  //something reasonably big (cm^2)
1061  
1062   TMatrixDSym v(3);
1063   v(0,0)=  s2;  v(0,1)=  a*s2;                 v(0,2)=  b*s2;;
1064   v(1,0)=a*s2;  v(1,1)=a*a*s2 + GetSigmaY2();  v(1,2)=a*b*s2 + GetSigmaZY();
1065   v(2,0)=b*s2;  v(2,1)=a*b*s2 + GetSigmaZY();  v(2,2)=b*b*s2 + GetSigmaZ2();
1066
1067   v(0,0)+=covxyz[0]; v(0,1)+=covxyz[1]; v(0,2)+=covxyz[2];
1068   v(1,0)+=covxyz[1]; v(1,1)+=covyz[0];  v(1,2)+=covyz[1];
1069   v(2,0)+=covxyz[2]; v(2,1)+=covyz[1];  v(2,2)+=covyz[2];
1070
1071   v.Invert();
1072   if (!v.IsValid()) return kVeryBig;
1073
1074   Double_t chi2=0.;
1075   for (Int_t i = 0; i < 3; i++)
1076     for (Int_t j = 0; j < 3; j++) chi2 += res[i]*res[j]*v(i,j);
1077
1078   return chi2;  
1079 }
1080
1081 Double_t AliExternalTrackParam::
1082 GetPredictedChi2(const AliExternalTrackParam *t) const {
1083   //----------------------------------------------------------------
1084   // Estimate the chi2 (5 dof) of this track with respect to the track
1085   // given by the argument.
1086   // The two tracks must be in the same reference system 
1087   // and estimated at the same reference plane.
1088   //----------------------------------------------------------------
1089
1090   if (TMath::Abs(1. - t->GetAlpha()/GetAlpha()) > FLT_EPSILON) {
1091       AliError("The reference systems of the tracks differ !");
1092       return kVeryBig;
1093   }
1094   if (TMath::Abs(1. - t->GetX()/GetX()) > FLT_EPSILON) {
1095       AliError("The reference of the tracks planes differ !");
1096       return kVeryBig;
1097   }
1098
1099   TMatrixDSym c(5);
1100     c(0,0)=GetSigmaY2(); 
1101     c(1,0)=GetSigmaZY();   c(1,1)=GetSigmaZ2();
1102     c(2,0)=GetSigmaSnpY(); c(2,1)=GetSigmaSnpZ(); c(2,2)=GetSigmaSnp2();
1103     c(3,0)=GetSigmaTglY(); c(3,1)=GetSigmaTglZ(); c(3,2)=GetSigmaTglSnp(); c(3,3)=GetSigmaTgl2();
1104     c(4,0)=GetSigma1PtY(); c(4,1)=GetSigma1PtZ(); c(4,2)=GetSigma1PtSnp(); c(4,3)=GetSigma1PtTgl(); c(4,4)=GetSigma1Pt2();
1105
1106     c(0,0)+=t->GetSigmaY2(); 
1107     c(1,0)+=t->GetSigmaZY();  c(1,1)+=t->GetSigmaZ2();
1108     c(2,0)+=t->GetSigmaSnpY();c(2,1)+=t->GetSigmaSnpZ();c(2,2)+=t->GetSigmaSnp2();
1109     c(3,0)+=t->GetSigmaTglY();c(3,1)+=t->GetSigmaTglZ();c(3,2)+=t->GetSigmaTglSnp();c(3,3)+=t->GetSigmaTgl2();
1110     c(4,0)+=t->GetSigma1PtY();c(4,1)+=t->GetSigma1PtZ();c(4,2)+=t->GetSigma1PtSnp();c(4,3)+=t->GetSigma1PtTgl();c(4,4)+=t->GetSigma1Pt2();
1111     c(0,1)=c(1,0);
1112     c(0,2)=c(2,0); c(1,2)=c(2,1);
1113     c(0,3)=c(3,0); c(1,3)=c(3,1); c(2,3)=c(3,2);
1114     c(0,4)=c(4,0); c(1,4)=c(4,1); c(2,4)=c(4,2); c(3,4)=c(4,3);
1115
1116   c.Invert();
1117   if (!c.IsValid()) return kVeryBig;
1118
1119
1120   Double_t res[5] = {
1121     GetY()   - t->GetY(),
1122     GetZ()   - t->GetZ(),
1123     GetSnp() - t->GetSnp(),
1124     GetTgl() - t->GetTgl(),
1125     GetSigned1Pt() - t->GetSigned1Pt()
1126   };
1127
1128   Double_t chi2=0.;
1129   for (Int_t i = 0; i < 5; i++)
1130     for (Int_t j = 0; j < 5; j++) chi2 += res[i]*res[j]*c(i,j);
1131
1132   return chi2;  
1133 }
1134
1135 Bool_t AliExternalTrackParam::
1136 PropagateTo(Double_t p[3],Double_t covyz[3],Double_t covxyz[3],Double_t bz) {
1137   //----------------------------------------------------------------
1138   // Propagate this track to the plane 
1139   // the 3D space point "p" (with the covariance matrix "covyz" and "covxyz")
1140   // belongs to.
1141   // The magnetic field is "bz" (kG)
1142   //
1143   // The track curvature and the change of the covariance matrix
1144   // of the track parameters are negleted !
1145   // (So the "step" should be small compared with 1/curvature)
1146   //----------------------------------------------------------------
1147
1148   Double_t f=GetSnp();
1149   if (TMath::Abs(f) >= kAlmost1) return kFALSE;
1150   Double_t r=TMath::Sqrt((1.-f)*(1.+f));
1151   Double_t a=f/r, b=GetTgl()/r;
1152
1153   Double_t s2=333.*333.;  //something reasonably big (cm^2)
1154  
1155   TMatrixDSym tV(3);
1156   tV(0,0)=  s2;  tV(0,1)=  a*s2;  tV(0,2)=  b*s2;
1157   tV(1,0)=a*s2;  tV(1,1)=a*a*s2;  tV(1,2)=a*b*s2;
1158   tV(2,0)=b*s2;  tV(2,1)=a*b*s2;  tV(2,2)=b*b*s2;
1159
1160   TMatrixDSym pV(3);
1161   pV(0,0)=covxyz[0]; pV(0,1)=covxyz[1]; pV(0,2)=covxyz[2];
1162   pV(1,0)=covxyz[1]; pV(1,1)=covyz[0];  pV(1,2)=covyz[1];
1163   pV(2,0)=covxyz[2]; pV(2,1)=covyz[1];  pV(2,2)=covyz[2];
1164
1165   TMatrixDSym tpV(tV);
1166   tpV+=pV;
1167   tpV.Invert();
1168   if (!tpV.IsValid()) return kFALSE;
1169
1170   TMatrixDSym pW(3),tW(3);
1171   for (Int_t i=0; i<3; i++)
1172     for (Int_t j=0; j<3; j++) {
1173       pW(i,j)=tW(i,j)=0.;
1174       for (Int_t k=0; k<3; k++) {
1175         pW(i,j) += tV(i,k)*tpV(k,j);
1176         tW(i,j) += pV(i,k)*tpV(k,j);
1177       }
1178     }
1179
1180   Double_t t[3] = {GetX(), GetY(), GetZ()};
1181
1182   Double_t x=0.;
1183   for (Int_t i=0; i<3; i++) x += (tW(0,i)*t[i] + pW(0,i)*p[i]);  
1184   Double_t crv=GetC(bz);
1185   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
1186   f += crv*(x-fX);
1187   if (TMath::Abs(f) >= kAlmost1) return kFALSE;
1188   fX=x;  
1189
1190   fP[0]=0.;
1191   for (Int_t i=0; i<3; i++) fP[0] += (tW(1,i)*t[i] + pW(1,i)*p[i]);  
1192   fP[1]=0.;
1193   for (Int_t i=0; i<3; i++) fP[1] += (tW(2,i)*t[i] + pW(2,i)*p[i]);  
1194
1195   return kTRUE;  
1196 }
1197
1198 Double_t *AliExternalTrackParam::GetResiduals(
1199 Double_t *p,Double_t *cov,Bool_t updated) const {
1200   //------------------------------------------------------------------
1201   // Returns the track residuals with the space point "p" having
1202   // the covariance matrix "cov".
1203   // If "updated" is kTRUE, the track parameters expected to be updated,
1204   // otherwise they must be predicted.  
1205   //------------------------------------------------------------------
1206   static Double_t res[2];
1207
1208   Double_t r00=cov[0], r01=cov[1], r11=cov[2];
1209   if (updated) {
1210      r00-=fC[0]; r01-=fC[1]; r11-=fC[2];
1211   } else {
1212      r00+=fC[0]; r01+=fC[1]; r11+=fC[2];
1213   }
1214   Double_t det=r00*r11 - r01*r01;
1215
1216   if (TMath::Abs(det) < kAlmost0) return 0;
1217
1218   Double_t tmp=r00; r00=r11/det; r11=tmp/det;
1219
1220   if (r00 < 0.) return 0;
1221   if (r11 < 0.) return 0;
1222
1223   Double_t dy = fP[0] - p[0];
1224   Double_t dz = fP[1] - p[1];
1225
1226   res[0]=dy*TMath::Sqrt(r00);
1227   res[1]=dz*TMath::Sqrt(r11);
1228
1229   return res;
1230 }
1231
1232 Bool_t AliExternalTrackParam::Update(Double_t p[2], Double_t cov[3]) {
1233   //------------------------------------------------------------------
1234   // Update the track parameters with the space point "p" having
1235   // the covariance matrix "cov"
1236   //------------------------------------------------------------------
1237   Double_t &fP0=fP[0], &fP1=fP[1], &fP2=fP[2], &fP3=fP[3], &fP4=fP[4];
1238   Double_t 
1239   &fC00=fC[0],
1240   &fC10=fC[1],   &fC11=fC[2],  
1241   &fC20=fC[3],   &fC21=fC[4],   &fC22=fC[5],
1242   &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
1243   &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
1244
1245   Double_t r00=cov[0], r01=cov[1], r11=cov[2];
1246   r00+=fC00; r01+=fC10; r11+=fC11;
1247   Double_t det=r00*r11 - r01*r01;
1248
1249   if (TMath::Abs(det) < kAlmost0) return kFALSE;
1250
1251
1252   Double_t tmp=r00; r00=r11/det; r11=tmp/det; r01=-r01/det;
1253  
1254   Double_t k00=fC00*r00+fC10*r01, k01=fC00*r01+fC10*r11;
1255   Double_t k10=fC10*r00+fC11*r01, k11=fC10*r01+fC11*r11;
1256   Double_t k20=fC20*r00+fC21*r01, k21=fC20*r01+fC21*r11;
1257   Double_t k30=fC30*r00+fC31*r01, k31=fC30*r01+fC31*r11;
1258   Double_t k40=fC40*r00+fC41*r01, k41=fC40*r01+fC41*r11;
1259
1260   Double_t dy=p[0] - fP0, dz=p[1] - fP1;
1261   Double_t sf=fP2 + k20*dy + k21*dz;
1262   if (TMath::Abs(sf) > kAlmost1) return kFALSE;  
1263   
1264   fP0 += k00*dy + k01*dz;
1265   fP1 += k10*dy + k11*dz;
1266   fP2  = sf;
1267   fP3 += k30*dy + k31*dz;
1268   fP4 += k40*dy + k41*dz;
1269   
1270   Double_t c01=fC10, c02=fC20, c03=fC30, c04=fC40;
1271   Double_t c12=fC21, c13=fC31, c14=fC41;
1272
1273   fC00-=k00*fC00+k01*fC10; fC10-=k00*c01+k01*fC11;
1274   fC20-=k00*c02+k01*c12;   fC30-=k00*c03+k01*c13;
1275   fC40-=k00*c04+k01*c14; 
1276
1277   fC11-=k10*c01+k11*fC11;
1278   fC21-=k10*c02+k11*c12;   fC31-=k10*c03+k11*c13;
1279   fC41-=k10*c04+k11*c14; 
1280
1281   fC22-=k20*c02+k21*c12;   fC32-=k20*c03+k21*c13;
1282   fC42-=k20*c04+k21*c14; 
1283
1284   fC33-=k30*c03+k31*c13;
1285   fC43-=k30*c04+k31*c14; 
1286   
1287   fC44-=k40*c04+k41*c14; 
1288
1289   CheckCovariance();
1290
1291   return kTRUE;
1292 }
1293
1294 void 
1295 AliExternalTrackParam::GetHelixParameters(Double_t hlx[6], Double_t b) const {
1296   //--------------------------------------------------------------------
1297   // External track parameters -> helix parameters 
1298   // "b" - magnetic field (kG)
1299   //--------------------------------------------------------------------
1300   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
1301   
1302   hlx[0]=fP[0]; hlx[1]=fP[1]; hlx[2]=fP[2]; hlx[3]=fP[3];
1303
1304   hlx[5]=fX*cs - hlx[0]*sn;               // x0
1305   hlx[0]=fX*sn + hlx[0]*cs;               // y0
1306 //hlx[1]=                                 // z0
1307   hlx[2]=TMath::ASin(hlx[2]) + fAlpha;    // phi0
1308 //hlx[3]=                                 // tgl
1309   hlx[4]=GetC(b);                         // C
1310 }
1311
1312
1313 static void Evaluate(const Double_t *h, Double_t t,
1314                      Double_t r[3],  //radius vector
1315                      Double_t g[3],  //first defivatives
1316                      Double_t gg[3]) //second derivatives
1317 {
1318   //--------------------------------------------------------------------
1319   // Calculate position of a point on a track and some derivatives
1320   //--------------------------------------------------------------------
1321   Double_t phase=h[4]*t+h[2];
1322   Double_t sn=TMath::Sin(phase), cs=TMath::Cos(phase);
1323
1324   r[0] = h[5];
1325   r[1] = h[0];
1326   if (TMath::Abs(h[4])>kAlmost0) {
1327      r[0] += (sn - h[6])/h[4];
1328      r[1] -= (cs - h[7])/h[4];  
1329   }
1330   r[2] = h[1] + h[3]*t;
1331
1332   g[0] = cs; g[1]=sn; g[2]=h[3];
1333   
1334   gg[0]=-h[4]*sn; gg[1]=h[4]*cs; gg[2]=0.;
1335 }
1336
1337 Double_t AliExternalTrackParam::GetDCA(const AliExternalTrackParam *p, 
1338 Double_t b, Double_t &xthis, Double_t &xp) const {
1339   //------------------------------------------------------------
1340   // Returns the (weighed !) distance of closest approach between 
1341   // this track and the track "p".
1342   // Other returned values:
1343   //   xthis, xt - coordinates of tracks' reference planes at the DCA 
1344   //-----------------------------------------------------------
1345   Double_t dy2=GetSigmaY2() + p->GetSigmaY2();
1346   Double_t dz2=GetSigmaZ2() + p->GetSigmaZ2();
1347   Double_t dx2=dy2; 
1348
1349   Double_t p1[8]; GetHelixParameters(p1,b);
1350   p1[6]=TMath::Sin(p1[2]); p1[7]=TMath::Cos(p1[2]);
1351   Double_t p2[8]; p->GetHelixParameters(p2,b);
1352   p2[6]=TMath::Sin(p2[2]); p2[7]=TMath::Cos(p2[2]);
1353
1354
1355   Double_t r1[3],g1[3],gg1[3]; Double_t t1=0.;
1356   Evaluate(p1,t1,r1,g1,gg1);
1357   Double_t r2[3],g2[3],gg2[3]; Double_t t2=0.;
1358   Evaluate(p2,t2,r2,g2,gg2);
1359
1360   Double_t dx=r2[0]-r1[0], dy=r2[1]-r1[1], dz=r2[2]-r1[2];
1361   Double_t dm=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
1362
1363   Int_t max=27;
1364   while (max--) {
1365      Double_t gt1=-(dx*g1[0]/dx2 + dy*g1[1]/dy2 + dz*g1[2]/dz2);
1366      Double_t gt2=+(dx*g2[0]/dx2 + dy*g2[1]/dy2 + dz*g2[2]/dz2);
1367      Double_t h11=(g1[0]*g1[0] - dx*gg1[0])/dx2 + 
1368                   (g1[1]*g1[1] - dy*gg1[1])/dy2 +
1369                   (g1[2]*g1[2] - dz*gg1[2])/dz2;
1370      Double_t h22=(g2[0]*g2[0] + dx*gg2[0])/dx2 + 
1371                   (g2[1]*g2[1] + dy*gg2[1])/dy2 +
1372                   (g2[2]*g2[2] + dz*gg2[2])/dz2;
1373      Double_t h12=-(g1[0]*g2[0]/dx2 + g1[1]*g2[1]/dy2 + g1[2]*g2[2]/dz2);
1374
1375      Double_t det=h11*h22-h12*h12;
1376
1377      Double_t dt1,dt2;
1378      if (TMath::Abs(det)<1.e-33) {
1379         //(quasi)singular Hessian
1380         dt1=-gt1; dt2=-gt2;
1381      } else {
1382         dt1=-(gt1*h22 - gt2*h12)/det; 
1383         dt2=-(h11*gt2 - h12*gt1)/det;
1384      }
1385
1386      if ((dt1*gt1+dt2*gt2)>0) {dt1=-dt1; dt2=-dt2;}
1387
1388      //check delta(phase1) ?
1389      //check delta(phase2) ?
1390
1391      if (TMath::Abs(dt1)/(TMath::Abs(t1)+1.e-3) < 1.e-4)
1392      if (TMath::Abs(dt2)/(TMath::Abs(t2)+1.e-3) < 1.e-4) {
1393         if ((gt1*gt1+gt2*gt2) > 1.e-4/dy2/dy2) 
1394           AliDebug(1," stopped at not a stationary point !");
1395         Double_t lmb=h11+h22; lmb=lmb-TMath::Sqrt(lmb*lmb-4*det);
1396         if (lmb < 0.) 
1397           AliDebug(1," stopped at not a minimum !");
1398         break;
1399      }
1400
1401      Double_t dd=dm;
1402      for (Int_t div=1 ; ; div*=2) {
1403         Evaluate(p1,t1+dt1,r1,g1,gg1);
1404         Evaluate(p2,t2+dt2,r2,g2,gg2);
1405         dx=r2[0]-r1[0]; dy=r2[1]-r1[1]; dz=r2[2]-r1[2];
1406         dd=dx*dx/dx2 + dy*dy/dy2 + dz*dz/dz2;
1407         if (dd<dm) break;
1408         dt1*=0.5; dt2*=0.5;
1409         if (div>512) {
1410           AliDebug(1," overshoot !"); break;
1411         }   
1412      }
1413      dm=dd;
1414
1415      t1+=dt1;
1416      t2+=dt2;
1417
1418   }
1419
1420   if (max<=0) AliDebug(1," too many iterations !");
1421
1422   Double_t cs=TMath::Cos(GetAlpha());
1423   Double_t sn=TMath::Sin(GetAlpha());
1424   xthis=r1[0]*cs + r1[1]*sn;
1425
1426   cs=TMath::Cos(p->GetAlpha());
1427   sn=TMath::Sin(p->GetAlpha());
1428   xp=r2[0]*cs + r2[1]*sn;
1429
1430   return TMath::Sqrt(dm*TMath::Sqrt(dy2*dz2));
1431 }
1432  
1433 Double_t AliExternalTrackParam::
1434 PropagateToDCA(AliExternalTrackParam *p, Double_t b) {
1435   //--------------------------------------------------------------
1436   // Propagates this track and the argument track to the position of the
1437   // distance of closest approach.
1438   // Returns the (weighed !) distance of closest approach.
1439   //--------------------------------------------------------------
1440   Double_t xthis,xp;
1441   Double_t dca=GetDCA(p,b,xthis,xp);
1442
1443   if (!PropagateTo(xthis,b)) {
1444     //AliWarning(" propagation failed !");
1445     return 1e+33;
1446   }
1447
1448   if (!p->PropagateTo(xp,b)) {
1449     //AliWarning(" propagation failed !";
1450     return 1e+33;
1451   }
1452
1453   return dca;
1454 }
1455
1456
1457 Bool_t AliExternalTrackParam::PropagateToDCA(const AliVVertex *vtx, 
1458 Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
1459   //
1460   // Propagate this track to the DCA to vertex "vtx", 
1461   // if the (rough) transverse impact parameter is not bigger then "maxd". 
1462   //            Magnetic field is "b" (kG).
1463   //
1464   // a) The track gets extapolated to the DCA to the vertex.
1465   // b) The impact parameters and their covariance matrix are calculated.
1466   //
1467   //    In the case of success, the returned value is kTRUE
1468   //    (otherwise, it's kFALSE)
1469   //  
1470   Double_t alpha=GetAlpha();
1471   Double_t sn=TMath::Sin(alpha), cs=TMath::Cos(alpha);
1472   Double_t x=GetX(), y=GetParameter()[0], snp=GetParameter()[2];
1473   Double_t xv= vtx->GetX()*cs + vtx->GetY()*sn;
1474   Double_t yv=-vtx->GetX()*sn + vtx->GetY()*cs, zv=vtx->GetZ();
1475   x-=xv; y-=yv;
1476
1477   //Estimate the impact parameter neglecting the track curvature
1478   Double_t d=TMath::Abs(x*snp - y*TMath::Sqrt((1.-snp)*(1.+snp)));
1479   if (d > maxd) return kFALSE; 
1480
1481   //Propagate to the DCA
1482   Double_t crv=GetC(b);
1483   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
1484
1485   Double_t tgfv=-(crv*x - snp)/(crv*y + TMath::Sqrt((1.-snp)*(1.+snp)));
1486   sn=tgfv/TMath::Sqrt(1.+ tgfv*tgfv); cs=TMath::Sqrt((1.-sn)*(1.+sn));
1487   if (TMath::Abs(tgfv)>0.) cs = sn/tgfv;
1488   else cs=1.;
1489
1490   x = xv*cs + yv*sn;
1491   yv=-xv*sn + yv*cs; xv=x;
1492
1493   if (!Propagate(alpha+TMath::ASin(sn),xv,b)) return kFALSE;
1494
1495   if (dz==0) return kTRUE;
1496   dz[0] = GetParameter()[0] - yv;
1497   dz[1] = GetParameter()[1] - zv;
1498   
1499   if (covar==0) return kTRUE;
1500   Double_t cov[6]; vtx->GetCovarianceMatrix(cov);
1501
1502   //***** Improvements by A.Dainese
1503   alpha=GetAlpha(); sn=TMath::Sin(alpha); cs=TMath::Cos(alpha);
1504   Double_t s2ylocvtx = cov[0]*sn*sn + cov[2]*cs*cs - 2.*cov[1]*cs*sn;
1505   covar[0] = GetCovariance()[0] + s2ylocvtx;   // neglecting correlations
1506   covar[1] = GetCovariance()[1];               // between (x,y) and z
1507   covar[2] = GetCovariance()[2] + cov[5];      // in vertex's covariance matrix
1508   //*****
1509
1510   return kTRUE;
1511 }
1512
1513 Bool_t AliExternalTrackParam::PropagateToDCABxByBz(const AliVVertex *vtx, 
1514 Double_t b[3], Double_t maxd, Double_t dz[2], Double_t covar[3]) {
1515   //
1516   // Propagate this track to the DCA to vertex "vtx", 
1517   // if the (rough) transverse impact parameter is not bigger then "maxd". 
1518   //
1519   // This function takes into account all three components of the magnetic
1520   // field given by the b[3] arument (kG)
1521   //
1522   // a) The track gets extapolated to the DCA to the vertex.
1523   // b) The impact parameters and their covariance matrix are calculated.
1524   //
1525   //    In the case of success, the returned value is kTRUE
1526   //    (otherwise, it's kFALSE)
1527   //  
1528   Double_t alpha=GetAlpha();
1529   Double_t sn=TMath::Sin(alpha), cs=TMath::Cos(alpha);
1530   Double_t x=GetX(), y=GetParameter()[0], snp=GetParameter()[2];
1531   Double_t xv= vtx->GetX()*cs + vtx->GetY()*sn;
1532   Double_t yv=-vtx->GetX()*sn + vtx->GetY()*cs, zv=vtx->GetZ();
1533   x-=xv; y-=yv;
1534
1535   //Estimate the impact parameter neglecting the track curvature
1536   Double_t d=TMath::Abs(x*snp - y*TMath::Sqrt((1.-snp)*(1.+snp)));
1537   if (d > maxd) return kFALSE; 
1538
1539   //Propagate to the DCA
1540   Double_t crv=GetC(b[2]);
1541   if (TMath::Abs(b[2]) < kAlmost0Field) crv=0.;
1542
1543   Double_t tgfv=-(crv*x - snp)/(crv*y + TMath::Sqrt((1.-snp)*(1.+snp)));
1544   sn=tgfv/TMath::Sqrt(1.+ tgfv*tgfv); cs=TMath::Sqrt((1.-sn)*(1.+sn));
1545   if (TMath::Abs(tgfv)>0.) cs = sn/tgfv;
1546   else cs=1.;
1547
1548   x = xv*cs + yv*sn;
1549   yv=-xv*sn + yv*cs; xv=x;
1550
1551   if (!PropagateBxByBz(alpha+TMath::ASin(sn),xv,b)) return kFALSE;
1552
1553   if (dz==0) return kTRUE;
1554   dz[0] = GetParameter()[0] - yv;
1555   dz[1] = GetParameter()[1] - zv;
1556   
1557   if (covar==0) return kTRUE;
1558   Double_t cov[6]; vtx->GetCovarianceMatrix(cov);
1559
1560   //***** Improvements by A.Dainese
1561   alpha=GetAlpha(); sn=TMath::Sin(alpha); cs=TMath::Cos(alpha);
1562   Double_t s2ylocvtx = cov[0]*sn*sn + cov[2]*cs*cs - 2.*cov[1]*cs*sn;
1563   covar[0] = GetCovariance()[0] + s2ylocvtx;   // neglecting correlations
1564   covar[1] = GetCovariance()[1];               // between (x,y) and z
1565   covar[2] = GetCovariance()[2] + cov[5];      // in vertex's covariance matrix
1566   //*****
1567
1568   return kTRUE;
1569 }
1570
1571 void AliExternalTrackParam::GetDirection(Double_t d[3]) const {
1572   //----------------------------------------------------------------
1573   // This function returns a unit vector along the track direction
1574   // in the global coordinate system.
1575   //----------------------------------------------------------------
1576   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
1577   Double_t snp=fP[2];
1578   Double_t csp =TMath::Sqrt((1.-snp)*(1.+snp));
1579   Double_t norm=TMath::Sqrt(1.+ fP[3]*fP[3]);
1580   d[0]=(csp*cs - snp*sn)/norm; 
1581   d[1]=(snp*cs + csp*sn)/norm; 
1582   d[2]=fP[3]/norm;
1583 }
1584
1585 Bool_t AliExternalTrackParam::GetPxPyPz(Double_t p[3]) const {
1586   //---------------------------------------------------------------------
1587   // This function returns the global track momentum components
1588   // Results for (nearly) straight tracks are meaningless !
1589   //---------------------------------------------------------------------
1590   p[0]=fP[4]; p[1]=fP[2]; p[2]=fP[3];
1591   return Local2GlobalMomentum(p,fAlpha);
1592 }
1593
1594 Double_t AliExternalTrackParam::Px() const {
1595   //---------------------------------------------------------------------
1596   // Returns x-component of momentum
1597   // Result for (nearly) straight tracks is meaningless !
1598   //---------------------------------------------------------------------
1599
1600   Double_t p[3]={kVeryBig,kVeryBig,kVeryBig};
1601   GetPxPyPz(p);
1602
1603   return p[0];
1604 }
1605
1606 Double_t AliExternalTrackParam::Py() const {
1607   //---------------------------------------------------------------------
1608   // Returns y-component of momentum
1609   // Result for (nearly) straight tracks is meaningless !
1610   //---------------------------------------------------------------------
1611
1612   Double_t p[3]={kVeryBig,kVeryBig,kVeryBig};
1613   GetPxPyPz(p);
1614
1615   return p[1];
1616 }
1617
1618 Double_t AliExternalTrackParam::Xv() const {
1619   //---------------------------------------------------------------------
1620   // Returns x-component of first track point
1621   //---------------------------------------------------------------------
1622
1623   Double_t r[3]={0.,0.,0.};
1624   GetXYZ(r);
1625
1626   return r[0];
1627 }
1628
1629 Double_t AliExternalTrackParam::Yv() const {
1630   //---------------------------------------------------------------------
1631   // Returns y-component of first track point
1632   //---------------------------------------------------------------------
1633
1634   Double_t r[3]={0.,0.,0.};
1635   GetXYZ(r);
1636
1637   return r[1];
1638 }
1639
1640 Double_t AliExternalTrackParam::Theta() const {
1641   // return theta angle of momentum
1642
1643   return 0.5*TMath::Pi() - TMath::ATan(fP[3]);
1644 }
1645
1646 Double_t AliExternalTrackParam::Phi() const {
1647   //---------------------------------------------------------------------
1648   // Returns the azimuthal angle of momentum
1649   // 0 <= phi < 2*pi
1650   //---------------------------------------------------------------------
1651
1652   Double_t phi=TMath::ASin(fP[2]) + fAlpha;
1653   if (phi<0.) phi+=2.*TMath::Pi();
1654   else if (phi>=2.*TMath::Pi()) phi-=2.*TMath::Pi();
1655  
1656   return phi;
1657 }
1658
1659 Double_t AliExternalTrackParam::M() const {
1660   // return particle mass
1661
1662   // No mass information available so far.
1663   // Redifine in derived class!
1664
1665   return -999.;
1666 }
1667
1668 Double_t AliExternalTrackParam::E() const {
1669   // return particle energy
1670
1671   // No PID information available so far.
1672   // Redifine in derived class!
1673
1674   return -999.;
1675 }
1676
1677 Double_t AliExternalTrackParam::Eta() const { 
1678   // return pseudorapidity
1679
1680   return -TMath::Log(TMath::Tan(0.5 * Theta())); 
1681 }
1682
1683 Double_t AliExternalTrackParam::Y() const {
1684   // return rapidity
1685
1686   // No PID information available so far.
1687   // Redifine in derived class!
1688
1689   return -999.;
1690 }
1691
1692 Bool_t AliExternalTrackParam::GetXYZ(Double_t *r) const {
1693   //---------------------------------------------------------------------
1694   // This function returns the global track position
1695   //---------------------------------------------------------------------
1696   r[0]=fX; r[1]=fP[0]; r[2]=fP[1];
1697   return Local2GlobalPosition(r,fAlpha);
1698 }
1699
1700 Bool_t AliExternalTrackParam::GetCovarianceXYZPxPyPz(Double_t cv[21]) const {
1701   //---------------------------------------------------------------------
1702   // This function returns the global covariance matrix of the track params
1703   // 
1704   // Cov(x,x) ... :   cv[0]
1705   // Cov(y,x) ... :   cv[1]  cv[2]
1706   // Cov(z,x) ... :   cv[3]  cv[4]  cv[5]
1707   // Cov(px,x)... :   cv[6]  cv[7]  cv[8]  cv[9]
1708   // Cov(py,x)... :   cv[10] cv[11] cv[12] cv[13] cv[14]
1709   // Cov(pz,x)... :   cv[15] cv[16] cv[17] cv[18] cv[19] cv[20]
1710   //
1711   // Results for (nearly) straight tracks are meaningless !
1712   //---------------------------------------------------------------------
1713   if (TMath::Abs(fP[4])<=kAlmost0) {
1714      for (Int_t i=0; i<21; i++) cv[i]=0.;
1715      return kFALSE;
1716   }
1717   if (TMath::Abs(fP[2]) > kAlmost1) {
1718      for (Int_t i=0; i<21; i++) cv[i]=0.;
1719      return kFALSE;
1720   }
1721   Double_t pt=1./TMath::Abs(fP[4]);
1722   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
1723   Double_t r=TMath::Sqrt((1.-fP[2])*(1.+fP[2]));
1724
1725   Double_t m00=-sn, m10=cs;
1726   Double_t m23=-pt*(sn + fP[2]*cs/r), m43=-pt*pt*(r*cs - fP[2]*sn);
1727   Double_t m24= pt*(cs - fP[2]*sn/r), m44=-pt*pt*(r*sn + fP[2]*cs);
1728   Double_t m35=pt, m45=-pt*pt*fP[3];
1729
1730   m43*=GetSign();
1731   m44*=GetSign();
1732   m45*=GetSign();
1733
1734   cv[0 ] = fC[0]*m00*m00;
1735   cv[1 ] = fC[0]*m00*m10; 
1736   cv[2 ] = fC[0]*m10*m10;
1737   cv[3 ] = fC[1]*m00; 
1738   cv[4 ] = fC[1]*m10; 
1739   cv[5 ] = fC[2];
1740   cv[6 ] = m00*(fC[3]*m23 + fC[10]*m43); 
1741   cv[7 ] = m10*(fC[3]*m23 + fC[10]*m43); 
1742   cv[8 ] = fC[4]*m23 + fC[11]*m43; 
1743   cv[9 ] = m23*(fC[5]*m23 + fC[12]*m43)  +  m43*(fC[12]*m23 + fC[14]*m43);
1744   cv[10] = m00*(fC[3]*m24 + fC[10]*m44); 
1745   cv[11] = m10*(fC[3]*m24 + fC[10]*m44); 
1746   cv[12] = fC[4]*m24 + fC[11]*m44; 
1747   cv[13] = m23*(fC[5]*m24 + fC[12]*m44)  +  m43*(fC[12]*m24 + fC[14]*m44);
1748   cv[14] = m24*(fC[5]*m24 + fC[12]*m44)  +  m44*(fC[12]*m24 + fC[14]*m44);
1749   cv[15] = m00*(fC[6]*m35 + fC[10]*m45); 
1750   cv[16] = m10*(fC[6]*m35 + fC[10]*m45); 
1751   cv[17] = fC[7]*m35 + fC[11]*m45; 
1752   cv[18] = m23*(fC[8]*m35 + fC[12]*m45)  +  m43*(fC[13]*m35 + fC[14]*m45);
1753   cv[19] = m24*(fC[8]*m35 + fC[12]*m45)  +  m44*(fC[13]*m35 + fC[14]*m45); 
1754   cv[20] = m35*(fC[9]*m35 + fC[13]*m45)  +  m45*(fC[13]*m35 + fC[14]*m45);
1755
1756   return kTRUE;
1757 }
1758
1759
1760 Bool_t 
1761 AliExternalTrackParam::GetPxPyPzAt(Double_t x, Double_t b, Double_t *p) const {
1762   //---------------------------------------------------------------------
1763   // This function returns the global track momentum extrapolated to
1764   // the radial position "x" (cm) in the magnetic field "b" (kG)
1765   //---------------------------------------------------------------------
1766   p[0]=fP[4]; 
1767   p[1]=fP[2]+(x-fX)*GetC(b); 
1768   p[2]=fP[3];
1769   return Local2GlobalMomentum(p,fAlpha);
1770 }
1771
1772 Bool_t 
1773 AliExternalTrackParam::GetYAt(Double_t x, Double_t b, Double_t &y) const {
1774   //---------------------------------------------------------------------
1775   // This function returns the local Y-coordinate of the intersection 
1776   // point between this track and the reference plane "x" (cm). 
1777   // Magnetic field "b" (kG)
1778   //---------------------------------------------------------------------
1779   Double_t dx=x-fX;
1780   if(TMath::Abs(dx)<=kAlmost0) {y=fP[0]; return kTRUE;}
1781
1782   Double_t f1=fP[2], f2=f1 + dx*GetC(b);
1783
1784   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
1785   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
1786   
1787   Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
1788   y = fP[0] + dx*(f1+f2)/(r1+r2);
1789   return kTRUE;
1790 }
1791
1792 Bool_t 
1793 AliExternalTrackParam::GetZAt(Double_t x, Double_t b, Double_t &z) const {
1794   //---------------------------------------------------------------------
1795   // This function returns the local Z-coordinate of the intersection 
1796   // point between this track and the reference plane "x" (cm). 
1797   // Magnetic field "b" (kG)
1798   //---------------------------------------------------------------------
1799   Double_t dx=x-fX;
1800   if(TMath::Abs(dx)<=kAlmost0) {z=fP[1]; return kTRUE;}
1801
1802   Double_t f1=fP[2], f2=f1 + dx*GetC(b);
1803
1804   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
1805   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
1806   
1807   Double_t r1=sqrt((1.-f1)*(1.+f1)), r2=sqrt((1.-f2)*(1.+f2));
1808   z = fP[1] + dx*(r2 + f2*(f1+f2)/(r1+r2))*fP[3]; // Many thanks to P.Hristov !
1809   return kTRUE;
1810 }
1811
1812 Bool_t 
1813 AliExternalTrackParam::GetXYZAt(Double_t x, Double_t b, Double_t *r) const {
1814   //---------------------------------------------------------------------
1815   // This function returns the global track position extrapolated to
1816   // the radial position "x" (cm) in the magnetic field "b" (kG)
1817   //---------------------------------------------------------------------
1818   Double_t dx=x-fX;
1819   if(TMath::Abs(dx)<=kAlmost0) return GetXYZ(r);
1820
1821   Double_t f1=fP[2], f2=f1 + dx*GetC(b);
1822
1823   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
1824   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
1825   
1826   Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
1827   r[0] = x;
1828   r[1] = fP[0] + dx*(f1+f2)/(r1+r2);
1829   r[2] = fP[1] + dx*(r2 + f2*(f1+f2)/(r1+r2))*fP[3];//Thanks to Andrea & Peter
1830
1831   return Local2GlobalPosition(r,fAlpha);
1832 }
1833
1834 //_____________________________________________________________________________
1835 void AliExternalTrackParam::Print(Option_t* /*option*/) const
1836 {
1837 // print the parameters and the covariance matrix
1838
1839   printf("AliExternalTrackParam: x = %-12g  alpha = %-12g\n", fX, fAlpha);
1840   printf("  parameters: %12g %12g %12g %12g %12g\n",
1841          fP[0], fP[1], fP[2], fP[3], fP[4]);
1842   printf("  covariance: %12g\n", fC[0]);
1843   printf("              %12g %12g\n", fC[1], fC[2]);
1844   printf("              %12g %12g %12g\n", fC[3], fC[4], fC[5]);
1845   printf("              %12g %12g %12g %12g\n", 
1846          fC[6], fC[7], fC[8], fC[9]);
1847   printf("              %12g %12g %12g %12g %12g\n", 
1848          fC[10], fC[11], fC[12], fC[13], fC[14]);
1849 }
1850
1851 Double_t AliExternalTrackParam::GetSnpAt(Double_t x,Double_t b) const {
1852   //
1853   // Get sinus at given x
1854   //
1855   Double_t crv=GetC(b);
1856   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
1857   Double_t dx = x-fX;
1858   Double_t res = fP[2]+dx*crv;
1859   return res;
1860 }
1861
1862 Bool_t AliExternalTrackParam::GetDistance(AliExternalTrackParam *param2, Double_t x, Double_t dist[3], Double_t bz){
1863   //------------------------------------------------------------------------
1864   // Get the distance between two tracks at the local position x 
1865   // working in the local frame of this track.
1866   // Origin :   Marian.Ivanov@cern.ch
1867   //-----------------------------------------------------------------------
1868   Double_t xyz[3];
1869   Double_t xyz2[3];
1870   xyz[0]=x;
1871   if (!GetYAt(x,bz,xyz[1])) return kFALSE;
1872   if (!GetZAt(x,bz,xyz[2])) return kFALSE;
1873   //  
1874   //
1875   if (TMath::Abs(GetAlpha()-param2->GetAlpha())<kAlmost0){
1876     xyz2[0]=x;
1877     if (!param2->GetYAt(x,bz,xyz2[1])) return kFALSE;
1878     if (!param2->GetZAt(x,bz,xyz2[2])) return kFALSE;
1879   }else{
1880     //
1881     Double_t xyz1[3];
1882     Double_t dfi = param2->GetAlpha()-GetAlpha();
1883     Double_t ca = TMath::Cos(dfi), sa = TMath::Sin(dfi);
1884     xyz2[0] =  xyz[0]*ca+xyz[1]*sa;
1885     xyz2[1] = -xyz[0]*sa+xyz[1]*ca;
1886     //
1887     xyz1[0]=xyz2[0];
1888     if (!param2->GetYAt(xyz2[0],bz,xyz1[1])) return kFALSE;
1889     if (!param2->GetZAt(xyz2[0],bz,xyz1[2])) return kFALSE;
1890     //
1891     xyz2[0] =  xyz1[0]*ca-xyz1[1]*sa;
1892     xyz2[1] = +xyz1[0]*sa+xyz1[1]*ca;
1893     xyz2[2] = xyz1[2];
1894   }
1895   dist[0] = xyz[0]-xyz2[0];
1896   dist[1] = xyz[1]-xyz2[1];
1897   dist[2] = xyz[2]-xyz2[2];
1898
1899   return kTRUE;
1900 }
1901
1902
1903 //
1904 // Draw functionality.
1905 // Origin: Marian Ivanov, Marian.Ivanov@cern.ch
1906 //
1907
1908 void  AliExternalTrackParam::DrawTrack(Float_t magf, Float_t minR, Float_t maxR, Float_t stepR){
1909   //
1910   // Draw track line
1911   //
1912   if (minR>maxR) return ;
1913   if (stepR<=0) return ;
1914   Int_t npoints = TMath::Nint((maxR-minR)/stepR)+1;
1915   if (npoints<1) return;
1916   TPolyMarker3D *polymarker = new TPolyMarker3D(npoints);
1917   FillPolymarker(polymarker, magf,minR,maxR,stepR);
1918   polymarker->Draw();
1919 }
1920
1921 //
1922 void AliExternalTrackParam::FillPolymarker(TPolyMarker3D *pol, Float_t magF, Float_t minR, Float_t maxR, Float_t stepR){
1923   //
1924   // Fill points in the polymarker
1925   //
1926   Int_t counter=0;
1927   for (Double_t r=minR; r<maxR; r+=stepR){
1928     Double_t point[3];
1929     GetXYZAt(r,magF,point);
1930     pol->SetPoint(counter,point[0],point[1], point[2]);
1931     //    printf("xyz\t%f\t%f\t%f\n",point[0], point[1],point[2]);
1932     counter++;
1933   }
1934 }
1935
1936 Int_t AliExternalTrackParam::GetIndex(Int_t i, Int_t j) const {
1937   //
1938   Int_t min = TMath::Min(i,j);
1939   Int_t max = TMath::Max(i,j);
1940
1941   return min+(max+1)*max/2;
1942 }
1943
1944
1945 void AliExternalTrackParam::g3helx3(Double_t qfield, 
1946                                     Double_t step,
1947                                     Double_t vect[7]) {
1948 /******************************************************************
1949  *                                                                *
1950  *       GEANT3 tracking routine in a constant field oriented     *
1951  *       along axis 3                                             *
1952  *       Tracking is performed with a conventional                *
1953  *       helix step method                                        *
1954  *                                                                *
1955  *       Authors    R.Brun, M.Hansroul  *********                 *
1956  *       Rewritten  V.Perevoztchikov                              *
1957  *                                                                *
1958  *       Rewritten in C++ by I.Belikov                            *
1959  *                                                                *
1960  *  qfield (kG)       - particle charge times magnetic field      *
1961  *  step   (cm)       - step length along the helix               *
1962  *  vect[7](cm,GeV/c) - input/output x, y, z, px/p, py/p ,pz/p, p *
1963  *                                                                *
1964  ******************************************************************/
1965   const Int_t ix=0, iy=1, iz=2, ipx=3, ipy=4, ipz=5, ipp=6;
1966   const Double_t kOvSqSix=TMath::Sqrt(1./6.);
1967
1968   Double_t cosx=vect[ipx], cosy=vect[ipy], cosz=vect[ipz];
1969
1970   Double_t rho = qfield*kB2C/vect[ipp]; 
1971   Double_t tet = rho*step;
1972
1973   Double_t tsint, sintt, sint, cos1t; 
1974   if (TMath::Abs(tet) > 0.03) {
1975      sint  = TMath::Sin(tet);
1976      sintt = sint/tet;
1977      tsint = (tet - sint)/tet;
1978      Double_t t=TMath::Sin(0.5*tet);
1979      cos1t = 2*t*t/tet;
1980   } else {
1981      tsint = tet*tet/6.;
1982      sintt = (1.-tet*kOvSqSix)*(1.+tet*kOvSqSix); // 1.- tsint;
1983      sint  = tet*sintt;
1984      cos1t = 0.5*tet; 
1985   }
1986
1987   Double_t f1 = step*sintt;
1988   Double_t f2 = step*cos1t;
1989   Double_t f3 = step*tsint*cosz;
1990   Double_t f4 = -tet*cos1t;
1991   Double_t f5 = sint;
1992
1993   vect[ix]  += f1*cosx - f2*cosy;
1994   vect[iy]  += f1*cosy + f2*cosx;
1995   vect[iz]  += f1*cosz + f3;
1996
1997   vect[ipx] += f4*cosx - f5*cosy;
1998   vect[ipy] += f4*cosy + f5*cosx;  
1999
2000 }
2001
2002 Bool_t AliExternalTrackParam::PropagateToBxByBz(Double_t xk, const Double_t b[3]) {
2003   //----------------------------------------------------------------
2004   // Extrapolate this track to the plane X=xk in the field b[].
2005   //
2006   // X [cm] is in the "tracking coordinate system" of this track.
2007   // b[]={Bx,By,Bz} [kG] is in the Global coordidate system.
2008   //----------------------------------------------------------------
2009
2010   Double_t dx=xk-fX;
2011   if (TMath::Abs(dx)<=kAlmost0)  return kTRUE;
2012   if (TMath::Abs(fP[4])<=kAlmost0) return kFALSE;
2013   // Do not propagate tracks outside the ALICE detector
2014   if (TMath::Abs(dx)>1e5 ||
2015       TMath::Abs(GetY())>1e5 ||
2016       TMath::Abs(GetZ())>1e5) {
2017     AliWarning(Form("Anomalous track, target X:%f",xk));
2018     Print();
2019     return kFALSE;
2020   }
2021
2022   Double_t crv=GetC(b[2]);
2023   if (TMath::Abs(b[2]) < kAlmost0Field) crv=0.;
2024
2025   Double_t x2r = crv*dx;
2026   Double_t f1=fP[2], f2=f1 + x2r;
2027   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
2028   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
2029
2030
2031   // Estimate the covariance matrix  
2032   Double_t &fP3=fP[3], &fP4=fP[4];
2033   Double_t 
2034   &fC00=fC[0],
2035   &fC10=fC[1],   &fC11=fC[2],  
2036   &fC20=fC[3],   &fC21=fC[4],   &fC22=fC[5],
2037   &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
2038   &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
2039
2040   Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
2041
2042   //f = F - 1
2043   /*
2044   Double_t f02=    dx/(r1*r1*r1);            Double_t cc=crv/fP4;
2045   Double_t f04=0.5*dx*dx/(r1*r1*r1);         f04*=cc;
2046   Double_t f12=    dx*fP3*f1/(r1*r1*r1);
2047   Double_t f14=0.5*dx*dx*fP3*f1/(r1*r1*r1);  f14*=cc;
2048   Double_t f13=    dx/r1;
2049   Double_t f24=    dx;                       f24*=cc;
2050   */
2051   Double_t rinv = 1./r1;
2052   Double_t r3inv = rinv*rinv*rinv;
2053   Double_t f24=    x2r/fP4;
2054   Double_t f02=    dx*r3inv;
2055   Double_t f04=0.5*f24*f02;
2056   Double_t f12=    f02*fP3*f1;
2057   Double_t f14=0.5*f24*f02*fP3*f1;
2058   Double_t f13=    dx*rinv;
2059  
2060   //b = C*ft
2061   Double_t b00=f02*fC20 + f04*fC40, b01=f12*fC20 + f14*fC40 + f13*fC30;
2062   Double_t b02=f24*fC40;
2063   Double_t b10=f02*fC21 + f04*fC41, b11=f12*fC21 + f14*fC41 + f13*fC31;
2064   Double_t b12=f24*fC41;
2065   Double_t b20=f02*fC22 + f04*fC42, b21=f12*fC22 + f14*fC42 + f13*fC32;
2066   Double_t b22=f24*fC42;
2067   Double_t b40=f02*fC42 + f04*fC44, b41=f12*fC42 + f14*fC44 + f13*fC43;
2068   Double_t b42=f24*fC44;
2069   Double_t b30=f02*fC32 + f04*fC43, b31=f12*fC32 + f14*fC43 + f13*fC33;
2070   Double_t b32=f24*fC43;
2071   
2072   //a = f*b = f*C*ft
2073   Double_t a00=f02*b20+f04*b40,a01=f02*b21+f04*b41,a02=f02*b22+f04*b42;
2074   Double_t a11=f12*b21+f14*b41+f13*b31,a12=f12*b22+f14*b42+f13*b32;
2075   Double_t a22=f24*b42;
2076
2077   //F*C*Ft = C + (b + bt + a)
2078   fC00 += b00 + b00 + a00;
2079   fC10 += b10 + b01 + a01; 
2080   fC20 += b20 + b02 + a02;
2081   fC30 += b30;
2082   fC40 += b40;
2083   fC11 += b11 + b11 + a11;
2084   fC21 += b21 + b12 + a12;
2085   fC31 += b31; 
2086   fC41 += b41;
2087   fC22 += b22 + b22 + a22;
2088   fC32 += b32;
2089   fC42 += b42;
2090
2091   CheckCovariance();
2092   
2093   // Appoximate step length
2094   double dy2dx = (f1+f2)/(r1+r2);
2095   Double_t step = (TMath::Abs(x2r)<0.05) ? dx*TMath::Abs(r2 + f2*dy2dx)  // chord
2096     : 2.*TMath::ASin(0.5*dx*TMath::Sqrt(1.+dy2dx*dy2dx)*crv)/crv;        // arc
2097   step *= TMath::Sqrt(1.+ GetTgl()*GetTgl());
2098
2099   // Get the track's (x,y,z) and (px,py,pz) in the Global System
2100   Double_t r[3]; GetXYZ(r);
2101   Double_t p[3]; GetPxPyPz(p);
2102   Double_t pp=GetP();
2103   p[0] /= pp;
2104   p[1] /= pp;
2105   p[2] /= pp;
2106
2107
2108   // Rotate to the system where Bx=By=0.
2109   Double_t bt=TMath::Sqrt(b[0]*b[0] + b[1]*b[1]);
2110   Double_t cosphi=1., sinphi=0.;
2111   if (bt > kAlmost0) {cosphi=b[0]/bt; sinphi=b[1]/bt;}
2112   Double_t bb=TMath::Sqrt(b[0]*b[0] + b[1]*b[1] + b[2]*b[2]);
2113   Double_t costet=1., sintet=0.;
2114   if (bb > kAlmost0) {costet=b[2]/bb; sintet=bt/bb;}
2115   Double_t vect[7];
2116
2117   vect[0] = costet*cosphi*r[0] + costet*sinphi*r[1] - sintet*r[2];
2118   vect[1] = -sinphi*r[0] + cosphi*r[1];
2119   vect[2] = sintet*cosphi*r[0] + sintet*sinphi*r[1] + costet*r[2];
2120
2121   vect[3] = costet*cosphi*p[0] + costet*sinphi*p[1] - sintet*p[2];
2122   vect[4] = -sinphi*p[0] + cosphi*p[1];
2123   vect[5] = sintet*cosphi*p[0] + sintet*sinphi*p[1] + costet*p[2];
2124
2125   vect[6] = pp;
2126
2127
2128   // Do the helix step
2129   g3helx3(GetSign()*bb,step,vect);
2130
2131
2132   // Rotate back to the Global System
2133   r[0] = cosphi*costet*vect[0] - sinphi*vect[1] + cosphi*sintet*vect[2];
2134   r[1] = sinphi*costet*vect[0] + cosphi*vect[1] + sinphi*sintet*vect[2];
2135   r[2] = -sintet*vect[0] + costet*vect[2];
2136
2137   p[0] = cosphi*costet*vect[3] - sinphi*vect[4] + cosphi*sintet*vect[5];
2138   p[1] = sinphi*costet*vect[3] + cosphi*vect[4] + sinphi*sintet*vect[5];
2139   p[2] = -sintet*vect[3] + costet*vect[5];
2140
2141
2142   // Rotate back to the Tracking System
2143   Double_t cosalp = TMath::Cos(fAlpha);
2144   Double_t sinalp =-TMath::Sin(fAlpha);
2145
2146   Double_t 
2147   t    = cosalp*r[0] - sinalp*r[1];
2148   r[1] = sinalp*r[0] + cosalp*r[1];  
2149   r[0] = t;
2150
2151   t    = cosalp*p[0] - sinalp*p[1]; 
2152   p[1] = sinalp*p[0] + cosalp*p[1];
2153   p[0] = t; 
2154
2155
2156   // Do the final correcting step to the target plane (linear approximation)
2157   Double_t x=r[0], y=r[1], z=r[2];
2158   if (TMath::Abs(dx) > kAlmost0) {
2159      if (TMath::Abs(p[0]) < kAlmost0) return kFALSE;
2160      dx = xk - r[0];
2161      x += dx;
2162      y += p[1]/p[0]*dx;
2163      z += p[2]/p[0]*dx;  
2164   }
2165
2166
2167   // Calculate the track parameters
2168   t=TMath::Sqrt(p[0]*p[0] + p[1]*p[1]);
2169   fX    = x;
2170   fP[0] = y;
2171   fP[1] = z;
2172   fP[2] = p[1]/t;
2173   fP[3] = p[2]/t; 
2174   fP[4] = GetSign()/(t*pp);
2175
2176   return kTRUE;
2177 }
2178
2179 Bool_t AliExternalTrackParam::Translate(Double_t *vTrasl,Double_t *covV){
2180   //
2181   //Translation: in the event mixing, the tracks can be shifted 
2182   //of the difference among primary vertices (vTrasl) and 
2183   //the covariance matrix is changed accordingly 
2184   //(covV = covariance of the primary vertex).
2185   //Origin: "Romita, Rossella" <R.Romita@gsi.de>
2186   // 
2187   TVector3 translation;
2188   // vTrasl coordinates in the local system
2189   translation.SetXYZ(vTrasl[0],vTrasl[1],vTrasl[2]);
2190   translation.RotateZ(-fAlpha);
2191   translation.GetXYZ(vTrasl);
2192
2193  //compute the new x,y,z of the track
2194   Double_t newX=fX-vTrasl[0];
2195   Double_t newY=fP[0]-vTrasl[1];
2196   Double_t newZ=fP[1]-vTrasl[2];
2197   
2198   //define the new parameters
2199   Double_t newParam[5];
2200   newParam[0]=newY;
2201   newParam[1]=newZ;
2202   newParam[2]=fP[2];
2203   newParam[3]=fP[3];
2204   newParam[4]=fP[4];
2205
2206   // recompute the covariance matrix:
2207   // 1. covV in the local system
2208   Double_t cosRot=TMath::Cos(fAlpha), sinRot=TMath::Sin(fAlpha);
2209   TMatrixD qQi(3,3);
2210   qQi(0,0) = cosRot;
2211   qQi(0,1) = sinRot;
2212   qQi(0,2) = 0.;
2213   qQi(1,0) = -sinRot;
2214   qQi(1,1) = cosRot;
2215   qQi(1,2) = 0.;
2216   qQi(2,0) = 0.;
2217   qQi(2,1) = 0.;
2218   qQi(2,2) = 1.;
2219   TMatrixD uUi(3,3);
2220   uUi(0,0) = covV[0];
2221   uUi(0,0) = covV[0];
2222   uUi(1,0) = covV[1];
2223   uUi(0,1) = covV[1];
2224   uUi(2,0) = covV[3];
2225   uUi(0,2) = covV[3];
2226   uUi(1,1) = covV[2];
2227   uUi(2,2) = covV[5];
2228   uUi(1,2) = covV[4];
2229   if(uUi.Determinant() <= 0.) {return kFALSE;}
2230   TMatrixD uUiQi(uUi,TMatrixD::kMult,qQi);
2231   TMatrixD m(qQi,TMatrixD::kTransposeMult,uUiQi);
2232
2233   //2. compute the new covariance matrix of the track
2234   Double_t sigmaXX=m(0,0);
2235   Double_t sigmaXZ=m(2,0);
2236   Double_t sigmaXY=m(1,0);
2237   Double_t sigmaYY=GetSigmaY2()+m(1,1);
2238   Double_t sigmaYZ=fC[1]+m(1,2);
2239   Double_t sigmaZZ=fC[2]+m(2,2);
2240   Double_t covarianceYY=sigmaYY + (-1.)*((sigmaXY*sigmaXY)/sigmaXX);
2241   Double_t covarianceYZ=sigmaYZ-(sigmaXZ*sigmaXY/sigmaXX);
2242   Double_t covarianceZZ=sigmaZZ-((sigmaXZ*sigmaXZ)/sigmaXX);
2243
2244   Double_t newCov[15];
2245   newCov[0]=covarianceYY;
2246   newCov[1]=covarianceYZ;
2247   newCov[2]=covarianceZZ;
2248   for(Int_t i=3;i<15;i++){
2249     newCov[i]=fC[i];
2250    }
2251
2252   // set the new parameters
2253
2254   Set(newX,fAlpha,newParam,newCov);
2255
2256   return kTRUE;
2257  }
2258
2259 void AliExternalTrackParam::CheckCovariance() {
2260
2261   // This function forces the diagonal elements of the covariance matrix to be positive.
2262   // In case the diagonal element is bigger than the maximal allowed value, it is set to
2263   // the limit and the off-diagonal elements that correspond to it are set to zero.
2264   fC[0] = TMath::Abs(fC[0]);
2265   if (fC[0]>kC0max) {
2266     double scl = TMath::Sqrt(kC0max/fC[0]);
2267     fC[0] = kC0max;
2268     fC[1] *= scl;
2269     fC[3] *= scl;
2270     fC[6] *= scl;
2271     fC[10] *= scl;
2272   }
2273   fC[2] = TMath::Abs(fC[2]);
2274   if (fC[2]>kC2max) {
2275     double scl = TMath::Sqrt(kC2max/fC[2]);
2276     fC[2] = kC2max;
2277     fC[1] *= scl;
2278     fC[4] *= scl;
2279     fC[7] *= scl;
2280     fC[11] *= scl;
2281   }
2282   fC[5] = TMath::Abs(fC[5]);
2283   if (fC[5]>kC5max) {
2284     double scl = TMath::Sqrt(kC5max/fC[5]);
2285     fC[5] = kC5max;
2286     fC[3] *= scl;
2287     fC[4] *= scl;
2288     fC[8] *= scl;
2289     fC[12] *= scl;
2290   }
2291   fC[9] = TMath::Abs(fC[9]);
2292   if (fC[9]>kC9max) {
2293     double scl = TMath::Sqrt(kC9max/fC[9]);
2294     fC[9] = kC9max;
2295     fC[6] *= scl;
2296     fC[7] *= scl;
2297     fC[8] *= scl;
2298     fC[13] *= scl;
2299   }
2300   fC[14] = TMath::Abs(fC[14]);
2301   if (fC[14]>kC14max) {
2302     double scl = TMath::Sqrt(kC14max/fC[14]);
2303     fC[14] = kC14max;
2304     fC[10] *= scl;
2305     fC[11] *= scl;
2306     fC[12] *= scl;
2307     fC[13] *= scl;
2308   }
2309       
2310     // The part below is used for tests and normally is commented out    
2311 //     TMatrixDSym m(5);
2312 //     TVectorD eig(5);
2313     
2314 //     m(0,0)=fC[0];
2315 //     m(1,0)=fC[1];  m(1,1)=fC[2];
2316 //     m(2,0)=fC[3];  m(2,1)=fC[4];  m(2,2)=fC[5];
2317 //     m(3,0)=fC[6];  m(3,1)=fC[7];  m(3,2)=fC[8];  m(3,3)=fC[9];
2318 //     m(4,0)=fC[10]; m(4,1)=fC[11]; m(4,2)=fC[12]; m(4,3)=fC[13]; m(4,4)=fC[14];
2319     
2320 //     m(0,1)=m(1,0);
2321 //     m(0,2)=m(2,0); m(1,2)=m(2,1);
2322 //     m(0,3)=m(3,0); m(1,3)=m(3,1); m(2,3)=m(3,2);
2323 //     m(0,4)=m(4,0); m(1,4)=m(4,1); m(2,4)=m(4,2); m(3,4)=m(4,3);
2324 //     m.EigenVectors(eig);
2325
2326 //     //    assert(eig(0)>=0 && eig(1)>=0 && eig(2)>=0 && eig(3)>=0 && eig(4)>=0);
2327 //     if (!(eig(0)>=0 && eig(1)>=0 && eig(2)>=0 && eig(3)>=0 && eig(4)>=0)) {
2328 //       AliWarning("Negative eigenvalues of the covariance matrix!");
2329 //       this->Print();
2330 //       eig.Print();
2331 //     }
2332 }
2333
2334 Bool_t AliExternalTrackParam::ConstrainToVertex(const AliVVertex* vtx, Double_t b[3])
2335 {
2336   // Constrain TPC inner params constrained
2337   //
2338   if (!vtx) 
2339     return kFALSE;
2340
2341   Double_t dz[2], cov[3];
2342   if (!PropagateToDCABxByBz(vtx, b, 3, dz, cov)) 
2343     return kFALSE; 
2344
2345   Double_t covar[6]; 
2346   vtx->GetCovarianceMatrix(covar);
2347   
2348   Double_t p[2]= { fP[0] - dz[0], fP[1] - dz[1] };
2349   Double_t c[3]= { covar[2], 0., covar[5] };
2350   
2351   Double_t chi2C = GetPredictedChi2(p,c);
2352   if (chi2C>kVeryBig) 
2353     return kFALSE; 
2354
2355   if (!Update(p,c)) 
2356     return kFALSE; 
2357
2358   return kTRUE;
2359 }
2360
2361 //___________________________________________________________________________________________
2362 Bool_t AliExternalTrackParam::GetXatLabR(Double_t r,Double_t &x, Double_t bz, Int_t dir) const
2363 {
2364   // Get local X of the track position estimated at the radius lab radius r. 
2365   // The track curvature is accounted exactly
2366   //
2367   // The flag "dir" can be used to remove the ambiguity of which intersection to take (out of 2 possible)
2368   // 0  - take the intersection closest to the current track position
2369   // >0 - go along the track (increasing fX)
2370   // <0 - go backward (decreasing fX)
2371   //
2372   const Double_t &fy=fP[0], &sn = fP[2];
2373   //
2374   double crv = GetC(bz);
2375   if (TMath::Abs(crv)<=kAlmost0) { // this is a straight track
2376     if (TMath::Abs(sn)>=kAlmost1) { // || to Y axis
2377       double det = (r-fX)*(r+fX);
2378       if (det<0) return kFALSE;     // does not reach raduis r
2379       x = fX;
2380       if (dir==0) return kTRUE;
2381       det = TMath::Sqrt(det);
2382       if (dir>0) {                       // along the track direction
2383         if (sn>0) {if (fy>det)  return kFALSE;} // track is along Y axis and above the circle
2384         else      {if (fy<-det) return kFALSE;} // track is against Y axis amd belo the circle
2385       }
2386       else {                                    // agains track direction
2387         if (sn>0) {if (fy<-det) return kFALSE;} // track is along Y axis
2388         else if (fy>det)  return kFALSE;        // track is against Y axis
2389       }
2390     }
2391     else if (TMath::Abs(sn)<=kAlmost0) { // || to X axis
2392       double det = (r-fy)*(r+fy);
2393       if (det<0) return kFALSE;     // does not reach raduis r
2394       det = TMath::Sqrt(det);
2395       if (!dir) {
2396         x = fX>0  ? det : -det;    // choose the solution requiring the smalest step
2397         return kTRUE;
2398       }
2399       else if (dir>0) {                    // along the track direction
2400         if      (fX > det) return kFALSE;  // current point is in on the right from the circle
2401         else if (fX <-det) x = -det;       // on the left
2402         else               x =  det;       // within the circle
2403       }
2404       else {                               // against the track direction
2405         if      (fX <-det) return kFALSE;  
2406         else if (fX > det) x =  det;
2407         else               x = -det;
2408       }
2409     }
2410     else {                                 // general case of straight line
2411       double cs = TMath::Sqrt((1-sn)*(1+sn));
2412       double xsyc = fX*sn-fy*cs;
2413       double det = (r-xsyc)*(r+xsyc);
2414       if (det<0) return kFALSE;    // does not reach raduis r
2415       det = TMath::Sqrt(det);
2416       double xcys = fX*cs+fy*sn;
2417       double t = -xcys;
2418       if (dir==0) t += t>0 ? -det:det;  // chose the solution requiring the smalest step
2419       else if (dir>0) {                 // go in increasing fX direction. ( t+-det > 0)
2420         if (t>=-det) t += -det;         // take minimal step giving t>0
2421         else return kFALSE;             // both solutions have negative t
2422       }
2423       else {                            // go in increasing fX direction. (t+-det < 0)
2424         if (t<det) t -= det;            // take minimal step giving t<0
2425         else return kFALSE;             // both solutions have positive t
2426       }
2427       x = fX + cs*t;
2428     }
2429   }
2430   else {                                 // helix
2431     // get center of the track circle
2432     double tR = 1./crv;   // track radius (for the moment signed)
2433     double cs = TMath::Sqrt((1-sn)*(1+sn));
2434     double x0 = fX - sn*tR;
2435     double y0 = fy + cs*tR;
2436     double r0 = TMath::Sqrt(x0*x0+y0*y0);
2437     //    printf("Xc:%+e Yc:%+e\n",x0,y0);
2438     //
2439     if (r0<=kAlmost0) return kFALSE;            // the track is concentric to circle
2440     tR = TMath::Abs(tR);
2441     double tR2r0 = tR/r0;
2442     double g = 0.5*(r*r/(r0*tR) - tR2r0 - 1./tR2r0);
2443     double det = (1.-g)*(1.+g);
2444     if (det<0) return kFALSE;         // does not reach raduis r
2445     det = TMath::Sqrt(det);
2446     //
2447     // the intersection happens in 2 points: {x0+tR*C,y0+tR*S} 
2448     // with C=f*c0+-|s0|*det and S=f*s0-+c0 sign(s0)*det
2449     // where s0 and c0 make direction for the circle center (=x0/r0 and y0/r0)
2450     //
2451     double tmp = 1.+g*tR2r0;
2452     x = x0*tmp; 
2453     double y = y0*tmp;
2454     if (TMath::Abs(y0)>kAlmost0) { // when y0==0 the x,y is unique
2455       double dfx = tR2r0*TMath::Abs(y0)*det;
2456       double dfy = tR2r0*x0*TMath::Sign(det,y0);
2457       if (dir==0) {                    // chose the one which corresponds to smallest step 
2458         double delta = (x-fX)*dfx-(y-fy)*dfy; // the choice of + in C will lead to smaller step if delta<0
2459         if (delta<0) x += dfx;
2460         else         x -= dfx;
2461       }
2462       else if (dir>0) {  // along track direction: x must be > fX
2463         x -= dfx; // try the smallest step (dfx is positive)
2464         if (x<fX && (x+=dfx+dfx)<fX) return kFALSE;
2465       }
2466       else { // backward: x must be < fX
2467         x += dfx; // try the smallest step (dfx is positive)
2468         if (x>fX && (x-=dfx+dfx)>fX) return kFALSE;
2469       }
2470     }
2471     else { // special case: track touching the circle just in 1 point
2472       if ( (dir>0&&x<fX) || (dir<0&&x>fX) ) return kFALSE; 
2473     }
2474   }
2475   //
2476   return kTRUE;
2477 }