]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliExternalTrackParam.cxx
Bug corrected.
[u/mrichter/AliRoot.git] / STEER / AliExternalTrackParam.cxx
index e3c0fffb508b13769ff728de1a503955d86b00df..295830b47930627c00ffdc667aa60a650707ab91 100644 (file)
 // are implemented.
 // Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                            //
 ///////////////////////////////////////////////////////////////////////////////
+#include <cassert>
+
+#include <TVectorD.h>
 #include <TMatrixDSym.h>
+#include <TPolyMarker3D.h>
+#include <TVector3.h>
+#include <TMatrixD.h>
+
 #include "AliExternalTrackParam.h"
-#include "AliESDVertex.h"
-#include "TPolyMarker3D.h"
+#include "AliVVertex.h"
 #include "AliLog.h"
 
 ClassImp(AliExternalTrackParam)
@@ -37,7 +43,7 @@ Double32_t AliExternalTrackParam::fgMostProbablePt=kMostProbablePt;
  
 //_____________________________________________________________________________
 AliExternalTrackParam::AliExternalTrackParam() :
-  AliVParticle(),
+  AliVTrack(),
   fX(0),
   fAlpha(0)
 {
@@ -50,7 +56,7 @@ AliExternalTrackParam::AliExternalTrackParam() :
 
 //_____________________________________________________________________________
 AliExternalTrackParam::AliExternalTrackParam(const AliExternalTrackParam &track):
-  AliVParticle(track),
+  AliVTrack(track),
   fX(track.fX),
   fAlpha(track.fAlpha)
 {
@@ -59,6 +65,7 @@ AliExternalTrackParam::AliExternalTrackParam(const AliExternalTrackParam &track)
   //
   for (Int_t i = 0; i < 5; i++) fP[i] = track.fP[i];
   for (Int_t i = 0; i < 15; i++) fC[i] = track.fC[i];
+  CheckCovariance();
 }
 
 //_____________________________________________________________________________
@@ -69,12 +76,13 @@ AliExternalTrackParam& AliExternalTrackParam::operator=(const AliExternalTrackPa
   //
   
   if (this!=&trkPar) {
-    AliVParticle::operator=(trkPar);
+    AliVTrack::operator=(trkPar);
     fX = trkPar.fX;
     fAlpha = trkPar.fAlpha;
 
     for (Int_t i = 0; i < 5; i++) fP[i] = trkPar.fP[i];
     for (Int_t i = 0; i < 15; i++) fC[i] = trkPar.fC[i];
+    CheckCovariance();
   }
 
   return *this;
@@ -84,7 +92,7 @@ AliExternalTrackParam& AliExternalTrackParam::operator=(const AliExternalTrackPa
 AliExternalTrackParam::AliExternalTrackParam(Double_t x, Double_t alpha, 
                                             const Double_t param[5], 
                                             const Double_t covar[15]) :
-  AliVParticle(),
+  AliVTrack(),
   fX(x),
   fAlpha(alpha)
 {
@@ -93,18 +101,148 @@ AliExternalTrackParam::AliExternalTrackParam(Double_t x, Double_t alpha,
   //
   for (Int_t i = 0; i < 5; i++)  fP[i] = param[i];
   for (Int_t i = 0; i < 15; i++) fC[i] = covar[i];
+  CheckCovariance();
 }
 
 //_____________________________________________________________________________
-void AliExternalTrackParam::Set(Double_t x, Double_t alpha,
-                               const Double_t p[5], const Double_t cov[15]) {
+AliExternalTrackParam::AliExternalTrackParam(const AliVTrack *vTrack) :
+  AliVTrack(),
+  fX(0.),
+  fAlpha(0.)
+{
   //
-  //  Sets the parameters
+  // Constructor from virtual track,
+  // This is not a copy contructor !
   //
-  fX=x;
-  fAlpha=alpha;
-  for (Int_t i = 0; i < 5; i++)  fP[i] = p[i];
-  for (Int_t i = 0; i < 15; i++) fC[i] = cov[i];
+
+  if (vTrack->InheritsFrom("AliExternalTrackParam")) {
+     AliError("This is not a copy constructor. Use AliExternalTrackParam(const AliExternalTrackParam &) !");
+     AliWarning("Calling the default constructor...");
+     AliExternalTrackParam();
+     return;
+  }
+
+  Double_t xyz[3],pxpypz[3],cv[21];
+  vTrack->GetXYZ(xyz);
+  pxpypz[0]=vTrack->Px();
+  pxpypz[1]=vTrack->Py();
+  pxpypz[2]=vTrack->Pz();
+  vTrack->GetCovarianceXYZPxPyPz(cv);
+  Short_t sign = (Short_t)vTrack->Charge();
+
+  Set(xyz,pxpypz,cv,sign);
+}
+
+//_____________________________________________________________________________
+AliExternalTrackParam::AliExternalTrackParam(Double_t xyz[3],Double_t pxpypz[3],
+                                            Double_t cv[21],Short_t sign) :
+  AliVTrack(),
+  fX(0.),
+  fAlpha(0.)
+{
+  //
+  // constructor from the global parameters
+  //
+
+  Set(xyz,pxpypz,cv,sign);
+}
+
+//_____________________________________________________________________________
+void AliExternalTrackParam::Set(Double_t xyz[3],Double_t pxpypz[3],
+                               Double_t cv[21],Short_t sign) 
+{
+  //
+  // create external track parameters from the global parameters
+  // x,y,z,px,py,pz and their 6x6 covariance matrix
+  // A.Dainese 10.10.08
+
+  // Calculate alpha: the rotation angle of the corresponding local system.
+  //
+  // For global radial position inside the beam pipe, alpha is the
+  // azimuthal angle of the momentum projected on (x,y).
+  //
+  // For global radial position outside the ITS, alpha is the
+  // azimuthal angle of the centre of the TPC sector in which the point
+  // xyz lies
+  //
+  Double_t radPos2 = xyz[0]*xyz[0]+xyz[1]*xyz[1];  
+  Double_t radMax  = 45.; // approximately ITS outer radius
+  if (radPos2 < radMax*radMax) { // inside the ITS
+     
+     fAlpha = TMath::ATan2(pxpypz[1],pxpypz[0]);
+  } else { // outside the ITS
+     Float_t phiPos = TMath::Pi()+TMath::ATan2(-xyz[1], -xyz[0]);
+     fAlpha = 
+     TMath::DegToRad()*(20*((((Int_t)(phiPos*TMath::RadToDeg()))/20))+10);
+  }
+
+  // Get the vertex of origin and the momentum
+  TVector3 ver(xyz[0],xyz[1],xyz[2]);
+  TVector3 mom(pxpypz[0],pxpypz[1],pxpypz[2]);
+
+  // Rotate to the local coordinate system
+  ver.RotateZ(-fAlpha);
+  mom.RotateZ(-fAlpha);
+
+  // x of the reference plane
+  fX = ver.X();
+
+  Double_t charge = (Double_t)sign;
+
+  fP[0] = ver.Y();
+  fP[1] = ver.Z();
+  fP[2] = TMath::Sin(mom.Phi());
+  fP[3] = mom.Pz()/mom.Pt();
+  fP[4] = TMath::Sign(1/mom.Pt(),charge);
+
+  // Covariance matrix (formulas to be simplified)
+
+  Double_t pt=1./TMath::Abs(fP[4]);
+  Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
+  Double_t r=TMath::Sqrt((1.-fP[2])*(1.+fP[2]));
+
+  Double_t m00=-sn;// m10=cs;
+  Double_t m23=-pt*(sn + fP[2]*cs/r), m43=-pt*pt*(r*cs - fP[2]*sn);
+  Double_t m24= pt*(cs - fP[2]*sn/r), m44=-pt*pt*(r*sn + fP[2]*cs);
+  Double_t m35=pt, m45=-pt*pt*fP[3];
+
+  m43*=GetSign();
+  m44*=GetSign();
+  m45*=GetSign();
+
+  Double_t cv34 = TMath::Sqrt(cv[3 ]*cv[3 ]+cv[4 ]*cv[4 ]);
+  Double_t a1=cv[13]-cv[9]*(m23*m44+m43*m24)/m23/m43;
+  Double_t a2=m23*m24-m23*(m23*m44+m43*m24)/m43;
+  Double_t a3=m43*m44-m43*(m23*m44+m43*m24)/m23;
+  Double_t a4=cv[14]-2.*cv[9]*m24*m44/m23/m43;
+  Double_t a5=m24*m24-2.*m24*m44*m23/m43;
+  Double_t a6=m44*m44-2.*m24*m44*m43/m23;
+
+  fC[0 ] = cv[0 ]+cv[2 ];  
+  fC[1 ] = TMath::Sign(cv34,cv[3 ]/m00); 
+  fC[2 ] = cv[5 ]; 
+  fC[3 ] = (cv[10]/m44-cv[6]/m43)/(m24/m44-m23/m43)/m00; 
+  fC[10] = (cv[6]/m00-fC[3 ]*m23)/m43; 
+  fC[6 ] = (cv[15]/m00-fC[10]*m45)/m35; 
+  fC[4 ] = (cv[12]-cv[8]*m44/m43)/(m24-m23*m44/m43); 
+  fC[11] = (cv[8]-fC[4]*m23)/m43; 
+  fC[7 ] = cv[17]/m35-fC[11]*m45/m35; 
+  fC[5 ] = TMath::Abs((a4-a6*a1/a3)/(a5-a6*a2/a3));
+  fC[14] = TMath::Abs(a1/a3-a2*fC[5]/a3);
+  fC[12] = (cv[9]-fC[5]*m23*m23-fC[14]*m43*m43)/m23/m43;
+  Double_t b1=cv[18]-fC[12]*m23*m45-fC[14]*m43*m45;
+  Double_t b2=m23*m35;
+  Double_t b3=m43*m35;
+  Double_t b4=cv[19]-fC[12]*m24*m45-fC[14]*m44*m45;
+  Double_t b5=m24*m35;
+  Double_t b6=m44*m35;
+  fC[8 ] = (b4-b6*b1/b3)/(b5-b6*b2/b3);
+  fC[13] = b1/b3-b2*fC[8]/b3;
+  fC[9 ] = TMath::Abs((cv[20]-fC[14]*(m45*m45)-fC[13]*2.*m35*m45)/(m35*m35));
+
+  CheckCovariance();
+
+  return;
 }
 
 //_____________________________________________________________________________
@@ -117,6 +255,21 @@ void AliExternalTrackParam::Reset() {
   for (Int_t i = 0; i < 15; i++) fC[i] = 0;
 }
 
+//_____________________________________________________________________________
+void AliExternalTrackParam::AddCovariance(const Double_t c[15]) {
+  //
+  // Add "something" to the track covarince matrix.
+  // May be needed to account for unknown mis-calibration/mis-alignment
+  //
+    fC[0] +=c[0];
+    fC[1] +=c[1];  fC[2] +=c[2];
+    fC[3] +=c[3];  fC[4] +=c[4];  fC[5] +=c[5];
+    fC[6] +=c[6];  fC[7] +=c[7];  fC[8] +=c[8];  fC[9] +=c[9];
+    fC[10]+=c[10]; fC[11]+=c[11]; fC[12]+=c[12]; fC[13]+=c[13]; fC[14]+=c[14];
+    CheckCovariance();
+}
+
+
 Double_t AliExternalTrackParam::GetP() const {
   //---------------------------------------------------------------------
   // This function returns the track momentum
@@ -150,8 +303,8 @@ Double_t AliExternalTrackParam::GetD(Double_t x,Double_t y,Double_t b) const {
   y = -x*sn + y*cs; x=a;
   xt-=x; yt-=y;
 
-  sn=rp4*xt - fP[2]; cs=rp4*yt + TMath::Sqrt(1.- fP[2]*fP[2]);
-  a=2*(xt*fP[2] - yt*TMath::Sqrt(1.- fP[2]*fP[2]))-rp4*(xt*xt + yt*yt);
+  sn=rp4*xt - fP[2]; cs=rp4*yt + TMath::Sqrt((1.- fP[2])*(1.+fP[2]));
+  a=2*(xt*fP[2] - yt*TMath::Sqrt((1.-fP[2])*(1.+fP[2])))-rp4*(xt*xt + yt*yt);
   return  -a/(1 + TMath::Sqrt(sn*sn + cs*cs));
 }
 
@@ -163,7 +316,7 @@ GetDZ(Double_t x, Double_t y, Double_t z, Double_t b, Float_t dz[2]) const {
   // with respect to a point with global coordinates (x,y)
   // in the magnetic field "b" (kG)
   //------------------------------------------------------------------
-  Double_t f1 = fP[2], r1 = TMath::Sqrt(1. - f1*f1);
+  Double_t f1 = fP[2], r1 = TMath::Sqrt((1.-f1)*(1.+f1));
   Double_t xt=fX, yt=fP[0];
   Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
   Double_t a = x*cs + y*sn;
@@ -181,7 +334,7 @@ GetDZ(Double_t x, Double_t y, Double_t z, Double_t b, Float_t dz[2]) const {
   a=2*(xt*f1 - yt*r1)-rp4*(xt*xt + yt*yt);
   Double_t rr=TMath::Sqrt(sn*sn + cs*cs);
   dz[0] = -a/(1 + rr);
-  Double_t f2 = -sn/rr, r2 = TMath::Sqrt(1. - f2*f2);
+  Double_t f2 = -sn/rr, r2 = TMath::Sqrt((1.-f2)*(1.+f2));
   dz[1] = fP[1] + fP[3]/rp4*TMath::ASin(f2*r1 - f1*r2) - z;
 }
 
@@ -196,19 +349,22 @@ Double_t AliExternalTrackParam::GetLinearD(Double_t xv,Double_t yv) const {
   Double_t x= xv*cs + yv*sn;
   Double_t y=-xv*sn + yv*cs;
 
-  Double_t d = (fX-x)*fP[2] - (fP[0]-y)*TMath::Sqrt(1.- fP[2]*fP[2]);
+  Double_t d = (fX-x)*fP[2] - (fP[0]-y)*TMath::Sqrt((1.-fP[2])*(1.+fP[2]));
 
   return -d;
 }
 
-Bool_t AliExternalTrackParam::CorrectForMeanMaterial
-(Double_t xOverX0,  Double_t xTimesRho, Double_t mass, Bool_t anglecorr, 
- Double_t (*Bethe)(Double_t)) {
+Bool_t AliExternalTrackParam::CorrectForMeanMaterialdEdx
+(Double_t xOverX0,  Double_t xTimesRho, Double_t mass, 
+ Double_t dEdx,
+ Bool_t anglecorr) {
   //------------------------------------------------------------------
   // This function corrects the track parameters for the crossed material.
   // "xOverX0"   - X/X0, the thickness in units of the radiation length.
   // "xTimesRho" - is the product length*density (g/cm^2). 
   // "mass" - the mass of this particle (GeV/c^2).
+  // "dEdx" - mean enery loss (GeV/(g/cm^2)
+  // "anglecorr" - switch for the angular correction
   //------------------------------------------------------------------
   Double_t &fP2=fP[2];
   Double_t &fP3=fP[3];
@@ -221,7 +377,7 @@ Bool_t AliExternalTrackParam::CorrectForMeanMaterial
 
   //Apply angle correction, if requested
   if(anglecorr) {
-    Double_t angle=TMath::Sqrt((1.+ fP3*fP3)/(1.- fP2*fP2));
+    Double_t angle=TMath::Sqrt((1.+ fP3*fP3)/((1-fP2)*(1.+fP2)));
     xOverX0 *=angle;
     xTimesRho *=angle;
   } 
@@ -230,33 +386,103 @@ Bool_t AliExternalTrackParam::CorrectForMeanMaterial
   Double_t p2=p*p;
   Double_t beta2=p2/(p2 + mass*mass);
 
-  //Multiple scattering******************
+  //Calculating the multiple scattering corrections******************
+  Double_t cC22 = 0.;
+  Double_t cC33 = 0.;
+  Double_t cC43 = 0.;
+  Double_t cC44 = 0.;
   if (xOverX0 != 0) {
      Double_t theta2=14.1*14.1/(beta2*p2*1e6)*TMath::Abs(xOverX0);
      //Double_t theta2=1.0259e-6*14*14/28/(beta2*p2)*TMath::Abs(d)*9.36*2.33;
-     fC22 += theta2*(1.- fP2*fP2)*(1. + fP3*fP3);
-     fC33 += theta2*(1. + fP3*fP3)*(1. + fP3*fP3);
-     fC43 += theta2*fP3*fP4*(1. + fP3*fP3);
-     fC44 += theta2*fP3*fP4*fP3*fP4;
+     if(theta2>TMath::Pi()*TMath::Pi()) return kFALSE;
+     cC22 = theta2*((1.-fP2)*(1.+fP2))*(1. + fP3*fP3);
+     cC33 = theta2*(1. + fP3*fP3)*(1. + fP3*fP3);
+     cC43 = theta2*fP3*fP4*(1. + fP3*fP3);
+     cC44 = theta2*fP3*fP4*fP3*fP4;
   }
 
-  //Energy losses************************
+  //Calculating the energy loss corrections************************
+  Double_t cP4=1.;
   if ((xTimesRho != 0.) && (beta2 < 1.)) {
-     Double_t dE=Bethe(beta2)*xTimesRho;
+     Double_t dE=dEdx*xTimesRho;
      Double_t e=TMath::Sqrt(p2 + mass*mass);
      if ( TMath::Abs(dE) > 0.3*e ) return kFALSE; //30% energy loss is too much!
-     fP4*=(1.- e/p2*dE);
+     //cP4 = (1.- e/p2*dE);
+     if ( (1.+ dE/p2*(dE + 2*e)) < 0. ) return kFALSE;
+     cP4 = 1./TMath::Sqrt(1.+ dE/p2*(dE + 2*e));  //A precise formula by Ruben !
+     if (TMath::Abs(fP4*cP4)>100.) return kFALSE; //Do not track below 10 MeV/c
+
 
      // Approximate energy loss fluctuation (M.Ivanov)
      const Double_t knst=0.07; // To be tuned.  
      Double_t sigmadE=knst*TMath::Sqrt(TMath::Abs(dE)); 
-     fC44+=((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
+     cC44 += ((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
  
   }
 
+  //Applying the corrections*****************************
+  fC22 += cC22;
+  fC33 += cC33;
+  fC43 += cC43;
+  fC44 += cC44;
+  fP4  *= cP4;
+
+  CheckCovariance();
+
   return kTRUE;
 }
 
+Bool_t AliExternalTrackParam::CorrectForMeanMaterial
+(Double_t xOverX0,  Double_t xTimesRho, Double_t mass, 
+ Bool_t anglecorr,
+ Double_t (*Bethe)(Double_t)) {
+  //------------------------------------------------------------------
+  // This function corrects the track parameters for the crossed material.
+  // "xOverX0"   - X/X0, the thickness in units of the radiation length.
+  // "xTimesRho" - is the product length*density (g/cm^2). 
+  // "mass" - the mass of this particle (GeV/c^2).
+  // "anglecorr" - switch for the angular correction
+  // "Bethe" - function calculating the energy loss (GeV/(g/cm^2)) 
+  //------------------------------------------------------------------
+  
+  Double_t bg=GetP()/mass;
+  Double_t dEdx=Bethe(bg);
+
+  return CorrectForMeanMaterialdEdx(xOverX0,xTimesRho,mass,dEdx,anglecorr);
+}
+
+Bool_t AliExternalTrackParam::CorrectForMeanMaterialZA
+(Double_t xOverX0, Double_t xTimesRho, Double_t mass,
+ Double_t zOverA,
+ Double_t density,
+ Double_t exEnergy,
+ Double_t jp1,
+ Double_t jp2,
+ Bool_t anglecorr) {
+  //------------------------------------------------------------------
+  // This function corrects the track parameters for the crossed material
+  // using the full Geant-like Bethe-Bloch formula parameterization
+  // "xOverX0"   - X/X0, the thickness in units of the radiation length.
+  // "xTimesRho" - is the product length*density (g/cm^2). 
+  // "mass" - the mass of this particle (GeV/c^2).
+  // "density"  - mean density (g/cm^3)
+  // "zOverA"   - mean Z/A
+  // "exEnergy" - mean exitation energy (GeV)
+  // "jp1"      - density effect first junction point
+  // "jp2"      - density effect second junction point
+  // "anglecorr" - switch for the angular correction
+  //
+  //  The default values of the parameters are for silicon 
+  //
+  //------------------------------------------------------------------
+
+  Double_t bg=GetP()/mass;
+  Double_t dEdx=BetheBlochGeant(bg,density,jp1,jp2,exEnergy,zOverA);
+
+  return CorrectForMeanMaterialdEdx(xOverX0,xTimesRho,mass,dEdx,anglecorr);
+}
+
+
 
 Bool_t AliExternalTrackParam::CorrectForMaterial
 (Double_t d,  Double_t x0, Double_t mass, Double_t (*Bethe)(Double_t)) {
@@ -269,60 +495,108 @@ Bool_t AliExternalTrackParam::CorrectForMaterial
   // "x0"   - the radiation length (g/cm^2) 
   // "mass" - the mass of this particle (GeV/c^2)
   //------------------------------------------------------------------
-  Double_t &fP2=fP[2];
-  Double_t &fP3=fP[3];
-  Double_t &fP4=fP[4];
 
-  Double_t &fC22=fC[5];
-  Double_t &fC33=fC[9];
-  Double_t &fC43=fC[13];
-  Double_t &fC44=fC[14];
+  return CorrectForMeanMaterial(d,x0*d,mass,kTRUE,Bethe);
 
-  Double_t p=GetP();
-  Double_t p2=p*p;
-  Double_t beta2=p2/(p2 + mass*mass);
-  d*=TMath::Sqrt((1.+ fP3*fP3)/(1.- fP2*fP2));
+}
 
-  //Multiple scattering******************
-  if (d!=0) {
-     Double_t theta2=14.1*14.1/(beta2*p2*1e6)*TMath::Abs(d);
-     //Double_t theta2=1.0259e-6*14*14/28/(beta2*p2)*TMath::Abs(d)*9.36*2.33;
-     fC22 += theta2*(1.- fP2*fP2)*(1. + fP3*fP3);
-     fC33 += theta2*(1. + fP3*fP3)*(1. + fP3*fP3);
-     fC43 += theta2*fP3*fP4*(1. + fP3*fP3);
-     fC44 += theta2*fP3*fP4*fP3*fP4;
-  }
+Double_t AliExternalTrackParam::BetheBlochAleph(Double_t bg,
+         Double_t kp1,
+         Double_t kp2,
+         Double_t kp3,
+         Double_t kp4,
+         Double_t kp5) {
+  //
+  // This is the empirical ALEPH parameterization of the Bethe-Bloch formula.
+  // It is normalized to 1 at the minimum.
+  //
+  // bg - beta*gamma
+  // 
+  // The default values for the kp* parameters are for ALICE TPC.
+  // The returned value is in MIP units
+  //
 
-  //Energy losses************************
-  if (x0!=0. && beta2<1) {
-     d*=x0;
-     Double_t dE=Bethe(beta2)*d;
-     Double_t e=TMath::Sqrt(p2 + mass*mass);
-     if ( TMath::Abs(dE) > 0.3*e ) return kFALSE; //30% energy loss is too much!
-     fP4*=(1.- e/p2*dE);
+  Double_t beta = bg/TMath::Sqrt(1.+ bg*bg);
 
-     // Approximate energy loss fluctuation (M.Ivanov)
-     const Double_t knst=0.07; // To be tuned.  
-     Double_t sigmadE=knst*TMath::Sqrt(TMath::Abs(dE)); 
-     fC44+=((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
+  Double_t aa = TMath::Power(beta,kp4);
+  Double_t bb = TMath::Power(1./bg,kp5);
+
+  bb=TMath::Log(kp3+bb);
+  
+  return (kp2-aa-bb)*kp1/aa;
+}
+
+Double_t AliExternalTrackParam::BetheBlochGeant(Double_t bg,
+         Double_t kp0,
+         Double_t kp1,
+         Double_t kp2,
+         Double_t kp3,
+         Double_t kp4) {
+  //
+  // This is the parameterization of the Bethe-Bloch formula inspired by Geant.
+  //
+  // bg  - beta*gamma
+  // kp0 - density [g/cm^3]
+  // kp1 - density effect first junction point
+  // kp2 - density effect second junction point
+  // kp3 - mean excitation energy [GeV]
+  // kp4 - mean Z/A
+  //
+  // The default values for the kp* parameters are for silicon. 
+  // The returned value is in [GeV/(g/cm^2)].
+  // 
+
+  const Double_t mK  = 0.307075e-3; // [GeV*cm^2/g]
+  const Double_t me  = 0.511e-3;    // [GeV/c^2]
+  const Double_t rho = kp0;
+  const Double_t x0  = kp1*2.303;
+  const Double_t x1  = kp2*2.303;
+  const Double_t mI  = kp3;
+  const Double_t mZA = kp4;
+  const Double_t bg2 = bg*bg;
+  const Double_t maxT= 2*me*bg2;    // neglecting the electron mass
+  
+  //*** Density effect
+  Double_t d2=0.; 
+  const Double_t x=TMath::Log(bg);
+  const Double_t lhwI=TMath::Log(28.816*1e-9*TMath::Sqrt(rho*mZA)/mI);
+  if (x > x1) {
+    d2 = lhwI + x - 0.5;
+  } else if (x > x0) {
+    const Double_t r=(x1-x)/(x1-x0);
+    d2 = lhwI + x - 0.5 + (0.5 - lhwI - x0)*r*r*r;
   }
 
-  return kTRUE;
+  return mK*mZA*(1+bg2)/bg2*
+         (0.5*TMath::Log(2*me*bg2*maxT/(mI*mI)) - bg2/(1+bg2) - d2);
 }
 
-Double_t ApproximateBetheBloch(Double_t beta2) {
+Double_t AliExternalTrackParam::BetheBlochSolid(Double_t bg) {
   //------------------------------------------------------------------
-  // This is an approximation of the Bethe-Bloch formula with 
-  // the density effect taken into account at beta*gamma > 3.5
-  // (the approximation is reasonable only for solid materials) 
+  // This is an approximation of the Bethe-Bloch formula, 
+  // reasonable for solid materials. 
+  // All the parameters are, in fact, for Si.
+  // The returned value is in [GeV/(g/cm^2)]
   //------------------------------------------------------------------
-  if (beta2 >= 1) return kVeryBig;
 
-  if (beta2/(1-beta2)>3.5*3.5)
-     return 0.153e-3/beta2*(log(3.5*5940)+0.5*log(beta2/(1-beta2)) - beta2);
+  return BetheBlochGeant(bg);
+}
 
-  return 0.153e-3/beta2*(log(5940*beta2/(1-beta2)) - beta2);
+Double_t AliExternalTrackParam::BetheBlochGas(Double_t bg) {
+  //------------------------------------------------------------------
+  // This is an approximation of the Bethe-Bloch formula, 
+  // reasonable for gas materials.
+  // All the parameters are, in fact, for Ne.
+  // The returned value is in [GeV/(g/cm^2)]
+  //------------------------------------------------------------------
+
+  const Double_t rho = 0.9e-3;
+  const Double_t x0  = 2.;
+  const Double_t x1  = 4.;
+  const Double_t mI  = 140.e-9;
+  const Double_t mZA = 0.49555;
+
+  return BetheBlochGeant(bg,rho,x0,x1,mI,mZA);
 }
 
 Bool_t AliExternalTrackParam::Rotate(Double_t alpha) {
@@ -352,10 +626,14 @@ Bool_t AliExternalTrackParam::Rotate(Double_t alpha) {
 
   Double_t x=fX;
   Double_t ca=TMath::Cos(alpha-fAlpha), sa=TMath::Sin(alpha-fAlpha);
-  Double_t sf=fP2, cf=TMath::Sqrt(1.- fP2*fP2);
+  Double_t sf=fP2, cf=TMath::Sqrt((1.- fP2)*(1.+fP2)); // Improve precision
 
   Double_t tmp=sf*ca - cf*sa;
-  if (TMath::Abs(tmp) >= kAlmost1) return kFALSE;
+  if (TMath::Abs(tmp) >= kAlmost1) {
+     if (TMath::Abs(tmp) > 1.+ Double_t(FLT_EPSILON))  
+        AliWarning(Form("Rotation failed ! %.10e",tmp));
+     return kFALSE;
+  }
 
   fAlpha = alpha;
   fX =  x*ca + fP0*sa;
@@ -379,6 +657,8 @@ Bool_t AliExternalTrackParam::Rotate(Double_t alpha) {
   fC40 *= ca;
   fC42 *= rr;
 
+  CheckCovariance();
+
   return kTRUE;
 }
 
@@ -392,7 +672,8 @@ Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
   Double_t crv=GetC(b);
   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
 
-  Double_t f1=fP[2], f2=f1 + crv*dx;
+  Double_t x2r = crv*dx;
+  Double_t f1=fP[2], f2=f1 + x2r;
   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
 
@@ -404,12 +685,26 @@ Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
   &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
   &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
 
-  Double_t r1=TMath::Sqrt(1.- f1*f1), r2=TMath::Sqrt(1.- f2*f2);
+  Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
 
   fX=xk;
-  fP0 += dx*(f1+f2)/(r1+r2);
-  fP1 += dx*(r2 + f2*(f1+f2)/(r1+r2))*fP3;  // Many thanks to P.Hristov !
-  fP2 += dx*crv;
+  double dy2dx = (f1+f2)/(r1+r2);
+  fP0 += dx*dy2dx;
+  if (TMath::Abs(x2r)<0.05) {
+    fP1 += dx*(r2 + f2*dy2dx)*fP3;  // Many thanks to P.Hristov !
+    fP2 += x2r;
+  }
+  else { 
+    // for small dx/R the linear apporximation of the arc by the segment is OK,
+    // but at large dx/R the error is very large and leads to incorrect Z propagation
+    // angle traversed delta = 2*asin(dist_start_end / R / 2), hence the arc is: R*deltaPhi
+    // The dist_start_end is obtained from sqrt(dx^2+dy^2) = x/(r1+r2)*sqrt(2+f1*f2+r1*r2)
+    // Similarly, the rotation angle in linear in dx only for dx<<R
+    double chord = dx*TMath::Sqrt(1+dy2dx*dy2dx);   // distance from old position to new one
+    double rot = 2*TMath::ASin(0.5*chord*crv); // angular difference seen from the circle center
+    fP1 += rot/crv*fP3;
+    fP2  = TMath::Sin(rot + TMath::ASin(fP2));
+  }
 
   //f = F - 1
    
@@ -451,9 +746,65 @@ Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
   fC32 += b32;
   fC42 += b42;
 
+  CheckCovariance();
+
   return kTRUE;
 }
 
+Bool_t 
+AliExternalTrackParam::Propagate(Double_t alpha, Double_t x, Double_t b) {
+  //------------------------------------------------------------------
+  // Transform this track to the local coord. system rotated
+  // by angle "alpha" (rad) with respect to the global coord. system, 
+  // and propagate this track to the plane X=xk (cm) in the field "b" (kG)
+  //------------------------------------------------------------------
+  
+  //Save the parameters
+  Double_t as=fAlpha;
+  Double_t xs=fX;
+  Double_t ps[5], cs[15];
+  for (Int_t i=0; i<5;  i++) ps[i]=fP[i]; 
+  for (Int_t i=0; i<15; i++) cs[i]=fC[i]; 
+
+  if (Rotate(alpha))
+     if (PropagateTo(x,b)) return kTRUE;
+
+  //Restore the parameters, if the operation failed
+  fAlpha=as;
+  fX=xs;
+  for (Int_t i=0; i<5;  i++) fP[i]=ps[i]; 
+  for (Int_t i=0; i<15; i++) fC[i]=cs[i]; 
+  return kFALSE;
+}
+
+Bool_t AliExternalTrackParam::PropagateBxByBz
+(Double_t alpha, Double_t x, Double_t b[3]) {
+  //------------------------------------------------------------------
+  // Transform this track to the local coord. system rotated
+  // by angle "alpha" (rad) with respect to the global coord. system, 
+  // and propagate this track to the plane X=xk (cm),
+  // taking into account all three components of the B field, "b[3]" (kG)
+  //------------------------------------------------------------------
+  
+  //Save the parameters
+  Double_t as=fAlpha;
+  Double_t xs=fX;
+  Double_t ps[5], cs[15];
+  for (Int_t i=0; i<5;  i++) ps[i]=fP[i]; 
+  for (Int_t i=0; i<15; i++) cs[i]=fC[i]; 
+
+  if (Rotate(alpha))
+     if (PropagateToBxByBz(x,b)) return kTRUE;
+
+  //Restore the parameters, if the operation failed
+  fAlpha=as;
+  fX=xs;
+  for (Int_t i=0; i<5;  i++) fP[i]=ps[i]; 
+  for (Int_t i=0; i<15; i++) fC[i]=cs[i]; 
+  return kFALSE;
+}
+
+
 void AliExternalTrackParam::Propagate(Double_t len, Double_t x[3],
 Double_t p[3], Double_t bz) const {
   //+++++++++++++++++++++++++++++++++++++++++    
@@ -466,7 +817,7 @@ Double_t p[3], Double_t bz) const {
   //+++++++++++++++++++++++++++++++++++++++++    
   GetXYZ(x);
     
-  if (OneOverPt() < kAlmost0 || TMath::Abs(bz) < kAlmost0Field ){ //straight-line tracks
+  if (OneOverPt() < kAlmost0 || TMath::Abs(bz) < kAlmost0Field || GetC(bz) < kAlmost0){ //straight-line tracks
      Double_t unit[3]; GetDirection(unit);
      x[0]+=unit[0]*len;   
      x[1]+=unit[1]*len;   
@@ -561,7 +912,7 @@ GetPredictedChi2(Double_t p[3],Double_t covyz[3],Double_t covxyz[3]) const {
 
   Double_t f=GetSnp();
   if (TMath::Abs(f) >= kAlmost1) return kVeryBig;
-  Double_t r=TMath::Sqrt(1.- f*f);
+  Double_t r=TMath::Sqrt((1.-f)*(1.+f));
   Double_t a=f/r, b=GetTgl()/r;
 
   Double_t s2=333.*333.;  //something reasonably big (cm^2)
@@ -583,8 +934,60 @@ GetPredictedChi2(Double_t p[3],Double_t covyz[3],Double_t covxyz[3]) const {
     for (Int_t j = 0; j < 3; j++) chi2 += res[i]*res[j]*v(i,j);
 
   return chi2;  
+}
+
+Double_t AliExternalTrackParam::
+GetPredictedChi2(const AliExternalTrackParam *t) const {
+  //----------------------------------------------------------------
+  // Estimate the chi2 (5 dof) of this track with respect to the track
+  // given by the argument.
+  // The two tracks must be in the same reference system 
+  // and estimated at the same reference plane.
+  //----------------------------------------------------------------
 
+  if (TMath::Abs(1. - t->GetAlpha()/GetAlpha()) > FLT_EPSILON) {
+      AliError("The reference systems of the tracks differ !");
+      return kVeryBig;
+  }
+  if (TMath::Abs(1. - t->GetX()/GetX()) > FLT_EPSILON) {
+      AliError("The reference of the tracks planes differ !");
+      return kVeryBig;
+  }
 
+  TMatrixDSym c(5);
+    c(0,0)=GetSigmaY2(); 
+    c(1,0)=GetSigmaZY();   c(1,1)=GetSigmaZ2();
+    c(2,0)=GetSigmaSnpY(); c(2,1)=GetSigmaSnpZ(); c(2,2)=GetSigmaSnp2();
+    c(3,0)=GetSigmaTglY(); c(3,1)=GetSigmaTglZ(); c(3,2)=GetSigmaTglSnp(); c(3,3)=GetSigmaTgl2();
+    c(4,0)=GetSigma1PtY(); c(4,1)=GetSigma1PtZ(); c(4,2)=GetSigma1PtSnp(); c(4,3)=GetSigma1PtTgl(); c(4,4)=GetSigma1Pt2();
+
+    c(0,0)+=t->GetSigmaY2(); 
+    c(1,0)+=t->GetSigmaZY();  c(1,1)+=t->GetSigmaZ2();
+    c(2,0)+=t->GetSigmaSnpY();c(2,1)+=t->GetSigmaSnpZ();c(2,2)+=t->GetSigmaSnp2();
+    c(3,0)+=t->GetSigmaTglY();c(3,1)+=t->GetSigmaTglZ();c(3,2)+=t->GetSigmaTglSnp();c(3,3)+=t->GetSigmaTgl2();
+    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();
+    c(0,1)=c(1,0);
+    c(0,2)=c(2,0); c(1,2)=c(2,1);
+    c(0,3)=c(3,0); c(1,3)=c(3,1); c(2,3)=c(3,2);
+    c(0,4)=c(4,0); c(1,4)=c(4,1); c(2,4)=c(4,2); c(3,4)=c(4,3);
+
+  c.Invert();
+  if (!c.IsValid()) return kVeryBig;
+
+
+  Double_t res[5] = {
+    GetY()   - t->GetY(),
+    GetZ()   - t->GetZ(),
+    GetSnp() - t->GetSnp(),
+    GetTgl() - t->GetTgl(),
+    GetSigned1Pt() - t->GetSigned1Pt()
+  };
+
+  Double_t chi2=0.;
+  for (Int_t i = 0; i < 5; i++)
+    for (Int_t j = 0; j < 5; j++) chi2 += res[i]*res[j]*c(i,j);
+
+  return chi2;  
 }
 
 Bool_t AliExternalTrackParam::
@@ -602,7 +1005,7 @@ PropagateTo(Double_t p[3],Double_t covyz[3],Double_t covxyz[3],Double_t bz) {
 
   Double_t f=GetSnp();
   if (TMath::Abs(f) >= kAlmost1) return kFALSE;
-  Double_t r=TMath::Sqrt(1.- f*f);
+  Double_t r=TMath::Sqrt((1.-f)*(1.+f));
   Double_t a=f/r, b=GetTgl()/r;
 
   Double_t s2=333.*333.;  //something reasonably big (cm^2)
@@ -741,6 +1144,8 @@ Bool_t AliExternalTrackParam::Update(Double_t p[2], Double_t cov[3]) {
 
   fC44-=k40*c04+k41*c14; 
 
+  CheckCovariance();
+
   return kTRUE;
 }
 
@@ -774,8 +1179,12 @@ static void Evaluate(const Double_t *h, Double_t t,
   Double_t phase=h[4]*t+h[2];
   Double_t sn=TMath::Sin(phase), cs=TMath::Cos(phase);
 
-  r[0] = h[5] + (sn - h[6])/h[4];
-  r[1] = h[0] - (cs - h[7])/h[4];  
+  r[0] = h[5];
+  r[1] = h[0];
+  if (TMath::Abs(h[4])>kAlmost0) {
+     r[0] += (sn - h[6])/h[4];
+     r[1] -= (cs - h[7])/h[4];  
+  }
   r[2] = h[1] + h[3]*t;
 
   g[0] = cs; g[1]=sn; g[2]=h[3];
@@ -795,8 +1204,6 @@ Double_t b, Double_t &xthis, Double_t &xp) const {
   Double_t dz2=GetSigmaZ2() + p->GetSigmaZ2();
   Double_t dx2=dy2; 
 
-  //dx2=dy2=dz2=1.;
-
   Double_t p1[8]; GetHelixParameters(p1,b);
   p1[6]=TMath::Sin(p1[2]); p1[7]=TMath::Cos(p1[2]);
   Double_t p2[8]; p->GetHelixParameters(p2,b);
@@ -905,7 +1312,7 @@ PropagateToDCA(AliExternalTrackParam *p, Double_t b) {
 }
 
 
-Bool_t AliExternalTrackParam::PropagateToDCA(const AliESDVertex *vtx, 
+Bool_t AliExternalTrackParam::PropagateToDCA(const AliVVertex *vtx, 
 Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
   //
   // Propagate this track to the DCA to vertex "vtx", 
@@ -921,20 +1328,20 @@ Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
   Double_t alpha=GetAlpha();
   Double_t sn=TMath::Sin(alpha), cs=TMath::Cos(alpha);
   Double_t x=GetX(), y=GetParameter()[0], snp=GetParameter()[2];
-  Double_t xv= vtx->GetXv()*cs + vtx->GetYv()*sn;
-  Double_t yv=-vtx->GetXv()*sn + vtx->GetYv()*cs, zv=vtx->GetZv();
+  Double_t xv= vtx->GetX()*cs + vtx->GetY()*sn;
+  Double_t yv=-vtx->GetX()*sn + vtx->GetY()*cs, zv=vtx->GetZ();
   x-=xv; y-=yv;
 
   //Estimate the impact parameter neglecting the track curvature
-  Double_t d=TMath::Abs(x*snp - y*TMath::Sqrt(1.- snp*snp));
+  Double_t d=TMath::Abs(x*snp - y*TMath::Sqrt((1.-snp)*(1.+snp)));
   if (d > maxd) return kFALSE; 
 
   //Propagate to the DCA
-  Double_t crv=kB2C*b*GetParameter()[4];
+  Double_t crv=GetC(b);
   if (TMath::Abs(b) < kAlmost0Field) crv=0.;
 
-  Double_t tgfv=-(crv*x - snp)/(crv*y + TMath::Sqrt(1.-snp*snp));
-  sn=tgfv/TMath::Sqrt(1.+ tgfv*tgfv); cs=TMath::Sqrt(1.- sn*sn);
+  Double_t tgfv=-(crv*x - snp)/(crv*y + TMath::Sqrt((1.-snp)*(1.+snp)));
+  sn=tgfv/TMath::Sqrt(1.+ tgfv*tgfv); cs=TMath::Sqrt((1.-sn)*(1.+sn));
   if (TMath::Abs(tgfv)>0.) cs = sn/tgfv;
   else cs=1.;
 
@@ -948,7 +1355,7 @@ Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
   dz[1] = GetParameter()[1] - zv;
   
   if (covar==0) return kTRUE;
-  Double_t cov[6]; vtx->GetCovMatrix(cov);
+  Double_t cov[6]; vtx->GetCovarianceMatrix(cov);
 
   //***** Improvements by A.Dainese
   alpha=GetAlpha(); sn=TMath::Sin(alpha); cs=TMath::Cos(alpha);
@@ -961,6 +1368,63 @@ Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
   return kTRUE;
 }
 
+Bool_t AliExternalTrackParam::PropagateToDCABxByBz(const AliVVertex *vtx, 
+Double_t b[3], Double_t maxd, Double_t dz[2], Double_t covar[3]) {
+  //
+  // Propagate this track to the DCA to vertex "vtx", 
+  // if the (rough) transverse impact parameter is not bigger then "maxd". 
+  //
+  // This function takes into account all three components of the magnetic
+  // field given by the b[3] arument (kG)
+  //
+  // a) The track gets extapolated to the DCA to the vertex.
+  // b) The impact parameters and their covariance matrix are calculated.
+  //
+  //    In the case of success, the returned value is kTRUE
+  //    (otherwise, it's kFALSE)
+  //  
+  Double_t alpha=GetAlpha();
+  Double_t sn=TMath::Sin(alpha), cs=TMath::Cos(alpha);
+  Double_t x=GetX(), y=GetParameter()[0], snp=GetParameter()[2];
+  Double_t xv= vtx->GetX()*cs + vtx->GetY()*sn;
+  Double_t yv=-vtx->GetX()*sn + vtx->GetY()*cs, zv=vtx->GetZ();
+  x-=xv; y-=yv;
+
+  //Estimate the impact parameter neglecting the track curvature
+  Double_t d=TMath::Abs(x*snp - y*TMath::Sqrt((1.-snp)*(1.+snp)));
+  if (d > maxd) return kFALSE; 
+
+  //Propagate to the DCA
+  Double_t crv=GetC(b[2]);
+  if (TMath::Abs(b[2]) < kAlmost0Field) crv=0.;
+
+  Double_t tgfv=-(crv*x - snp)/(crv*y + TMath::Sqrt((1.-snp)*(1.+snp)));
+  sn=tgfv/TMath::Sqrt(1.+ tgfv*tgfv); cs=TMath::Sqrt((1.-sn)*(1.+sn));
+  if (TMath::Abs(tgfv)>0.) cs = sn/tgfv;
+  else cs=1.;
+
+  x = xv*cs + yv*sn;
+  yv=-xv*sn + yv*cs; xv=x;
+
+  if (!PropagateBxByBz(alpha+TMath::ASin(sn),xv,b)) return kFALSE;
+
+  if (dz==0) return kTRUE;
+  dz[0] = GetParameter()[0] - yv;
+  dz[1] = GetParameter()[1] - zv;
+  
+  if (covar==0) return kTRUE;
+  Double_t cov[6]; vtx->GetCovarianceMatrix(cov);
+
+  //***** Improvements by A.Dainese
+  alpha=GetAlpha(); sn=TMath::Sin(alpha); cs=TMath::Cos(alpha);
+  Double_t s2ylocvtx = cov[0]*sn*sn + cov[2]*cs*cs - 2.*cov[1]*cs*sn;
+  covar[0] = GetCovariance()[0] + s2ylocvtx;   // neglecting correlations
+  covar[1] = GetCovariance()[1];               // between (x,y) and z
+  covar[2] = GetCovariance()[2] + cov[5];      // in vertex's covariance matrix
+  //*****
+
+  return kTRUE;
+}
 
 void AliExternalTrackParam::GetDirection(Double_t d[3]) const {
   //----------------------------------------------------------------
@@ -969,7 +1433,7 @@ void AliExternalTrackParam::GetDirection(Double_t d[3]) const {
   //----------------------------------------------------------------
   Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
   Double_t snp=fP[2];
-  Double_t csp =TMath::Sqrt((1.- snp)*(1.+snp));
+  Double_t csp =TMath::Sqrt((1.-snp)*(1.+snp));
   Double_t norm=TMath::Sqrt(1.+ fP[3]*fP[3]);
   d[0]=(csp*cs - snp*sn)/norm; 
   d[1]=(snp*cs + csp*sn)/norm; 
@@ -1009,18 +1473,6 @@ Double_t AliExternalTrackParam::Py() const {
   return p[1];
 }
 
-Double_t AliExternalTrackParam::Pz() const {
-  //---------------------------------------------------------------------
-  // Returns z-component of momentum
-  // Result for (nearly) straight tracks is meaningless !
-  //---------------------------------------------------------------------
-
-  Double_t p[3]={kVeryBig,kVeryBig,kVeryBig};
-  GetPxPyPz(p);
-
-  return p[2];
-}
-
 Double_t AliExternalTrackParam::Xv() const {
   //---------------------------------------------------------------------
   // Returns x-component of first track point
@@ -1043,17 +1495,6 @@ Double_t AliExternalTrackParam::Yv() const {
   return r[1];
 }
 
-Double_t AliExternalTrackParam::Zv() const {
-  //---------------------------------------------------------------------
-  // Returns z-component of first track point
-  //---------------------------------------------------------------------
-
-  Double_t r[3]={0.,0.,0.};
-  GetXYZ(r);
-
-  return r[2];
-}
-
 Double_t AliExternalTrackParam::Theta() const {
   // return theta angle of momentum
 
@@ -1201,7 +1642,7 @@ AliExternalTrackParam::GetYAt(Double_t x, Double_t b, Double_t &y) const {
   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
   
-  Double_t r1=TMath::Sqrt(1.- f1*f1), r2=TMath::Sqrt(1.- f2*f2);
+  Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
   y = fP[0] + dx*(f1+f2)/(r1+r2);
   return kTRUE;
 }
@@ -1216,12 +1657,12 @@ AliExternalTrackParam::GetZAt(Double_t x, Double_t b, Double_t &z) const {
   Double_t dx=x-fX;
   if(TMath::Abs(dx)<=kAlmost0) {z=fP[1]; return kTRUE;}
 
-  Double_t f1=fP[2], f2=f1 + dx*fP[4]*b*kB2C;
+  Double_t f1=fP[2], f2=f1 + dx*GetC(b);
 
   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
   
-  Double_t r1=sqrt(1.- f1*f1), r2=sqrt(1.- f2*f2);
+  Double_t r1=sqrt((1.-f1)*(1.+f1)), r2=sqrt((1.-f2)*(1.+f2));
   z = fP[1] + dx*(r2 + f2*(f1+f2)/(r1+r2))*fP[3]; // Many thanks to P.Hristov !
   return kTRUE;
 }
@@ -1240,10 +1681,11 @@ AliExternalTrackParam::GetXYZAt(Double_t x, Double_t b, Double_t *r) const {
   if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
   if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
   
-  Double_t r1=TMath::Sqrt(1.- f1*f1), r2=TMath::Sqrt(1.- f2*f2);
+  Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
   r[0] = x;
   r[1] = fP[0] + dx*(f1+f2)/(r1+r2);
-  r[2] = fP[1] + dx*(f1+f2)/(f1*r2 + f2*r1)*fP[3];
+  r[2] = fP[1] + dx*(r2 + f2*(f1+f2)/(r1+r2))*fP[3];//Thanks to Andrea & Peter
+
   return Local2GlobalPosition(r,fAlpha);
 }
 
@@ -1348,3 +1790,379 @@ void AliExternalTrackParam::FillPolymarker(TPolyMarker3D *pol, Float_t magF, Flo
     counter++;
   }
 }
+
+Int_t AliExternalTrackParam::GetIndex(Int_t i, Int_t j) const {
+  //
+  Int_t min = TMath::Min(i,j);
+  Int_t max = TMath::Max(i,j);
+
+  return min+(max+1)*max/2;
+}
+
+
+void AliExternalTrackParam::g3helx3(Double_t qfield, 
+                                    Double_t step,
+                                    Double_t vect[7]) {
+/******************************************************************
+ *                                                                *
+ *       GEANT3 tracking routine in a constant field oriented     *
+ *       along axis 3                                             *
+ *       Tracking is performed with a conventional                *
+ *       helix step method                                        *
+ *                                                                *
+ *       Authors    R.Brun, M.Hansroul  *********                 *
+ *       Rewritten  V.Perevoztchikov                              *
+ *                                                                *
+ *       Rewritten in C++ by I.Belikov                            *
+ *                                                                *
+ *  qfield (kG)       - particle charge times magnetic field      *
+ *  step   (cm)       - step length along the helix               *
+ *  vect[7](cm,GeV/c) - input/output x, y, z, px/p, py/p ,pz/p, p *
+ *                                                                *
+ ******************************************************************/
+  const Int_t ix=0, iy=1, iz=2, ipx=3, ipy=4, ipz=5, ipp=6;
+  const Double_t kOvSqSix=TMath::Sqrt(1./6.);
+
+  Double_t cosx=vect[ipx], cosy=vect[ipy], cosz=vect[ipz];
+
+  Double_t rho = qfield*kB2C/vect[ipp]; 
+  Double_t tet = rho*step;
+
+  Double_t tsint, sintt, sint, cos1t; 
+  if (TMath::Abs(tet) > 0.03) {
+     sint  = TMath::Sin(tet);
+     sintt = sint/tet;
+     tsint = (tet - sint)/tet;
+     Double_t t=TMath::Sin(0.5*tet);
+     cos1t = 2*t*t/tet;
+  } else {
+     tsint = tet*tet/6.;
+     sintt = (1.-tet*kOvSqSix)*(1.+tet*kOvSqSix); // 1.- tsint;
+     sint  = tet*sintt;
+     cos1t = 0.5*tet; 
+  }
+
+  Double_t f1 = step*sintt;
+  Double_t f2 = step*cos1t;
+  Double_t f3 = step*tsint*cosz;
+  Double_t f4 = -tet*cos1t;
+  Double_t f5 = sint;
+
+  vect[ix]  += f1*cosx - f2*cosy;
+  vect[iy]  += f1*cosy + f2*cosx;
+  vect[iz]  += f1*cosz + f3;
+
+  vect[ipx] += f4*cosx - f5*cosy;
+  vect[ipy] += f4*cosy + f5*cosx;  
+
+}
+
+Bool_t AliExternalTrackParam::PropagateToBxByBz(Double_t xk, const Double_t b[3]) {
+  //----------------------------------------------------------------
+  // Extrapolate this track to the plane X=xk in the field b[].
+  //
+  // X [cm] is in the "tracking coordinate system" of this track.
+  // b[]={Bx,By,Bz} [kG] is in the Global coordidate system.
+  //----------------------------------------------------------------
+
+  Double_t dx=xk-fX;
+  if (TMath::Abs(dx)<=kAlmost0)  return kTRUE;
+  if (TMath::Abs(fP[4])<=kAlmost0) return kFALSE;
+
+  Double_t crv=GetC(b[2]);
+  if (TMath::Abs(b[2]) < kAlmost0Field) crv=0.;
+
+  Double_t x2r = crv*dx;
+  Double_t f1=fP[2], f2=f1 + x2r;
+  if (TMath::Abs(f1) >= kAlmost1) return kFALSE;
+  if (TMath::Abs(f2) >= kAlmost1) return kFALSE;
+
+
+  // Estimate the covariance matrix  
+  Double_t &fP3=fP[3], &fP4=fP[4];
+  Double_t 
+  &fC00=fC[0],
+  &fC10=fC[1],   &fC11=fC[2],  
+  &fC20=fC[3],   &fC21=fC[4],   &fC22=fC[5],
+  &fC30=fC[6],   &fC31=fC[7],   &fC32=fC[8],   &fC33=fC[9],  
+  &fC40=fC[10],  &fC41=fC[11],  &fC42=fC[12],  &fC43=fC[13], &fC44=fC[14];
+
+  Double_t r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2));
+
+  //f = F - 1
+  Double_t f02=    dx/(r1*r1*r1);            Double_t cc=crv/fP4;
+  Double_t f04=0.5*dx*dx/(r1*r1*r1);         f04*=cc;
+  Double_t f12=    dx*fP3*f1/(r1*r1*r1);
+  Double_t f14=0.5*dx*dx*fP3*f1/(r1*r1*r1);  f14*=cc;
+  Double_t f13=    dx/r1;
+  Double_t f24=    dx;                       f24*=cc;
+  
+  //b = C*ft
+  Double_t b00=f02*fC20 + f04*fC40, b01=f12*fC20 + f14*fC40 + f13*fC30;
+  Double_t b02=f24*fC40;
+  Double_t b10=f02*fC21 + f04*fC41, b11=f12*fC21 + f14*fC41 + f13*fC31;
+  Double_t b12=f24*fC41;
+  Double_t b20=f02*fC22 + f04*fC42, b21=f12*fC22 + f14*fC42 + f13*fC32;
+  Double_t b22=f24*fC42;
+  Double_t b40=f02*fC42 + f04*fC44, b41=f12*fC42 + f14*fC44 + f13*fC43;
+  Double_t b42=f24*fC44;
+  Double_t b30=f02*fC32 + f04*fC43, b31=f12*fC32 + f14*fC43 + f13*fC33;
+  Double_t b32=f24*fC43;
+  
+  //a = f*b = f*C*ft
+  Double_t a00=f02*b20+f04*b40,a01=f02*b21+f04*b41,a02=f02*b22+f04*b42;
+  Double_t a11=f12*b21+f14*b41+f13*b31,a12=f12*b22+f14*b42+f13*b32;
+  Double_t a22=f24*b42;
+
+  //F*C*Ft = C + (b + bt + a)
+  fC00 += b00 + b00 + a00;
+  fC10 += b10 + b01 + a01; 
+  fC20 += b20 + b02 + a02;
+  fC30 += b30;
+  fC40 += b40;
+  fC11 += b11 + b11 + a11;
+  fC21 += b21 + b12 + a12;
+  fC31 += b31; 
+  fC41 += b41;
+  fC22 += b22 + b22 + a22;
+  fC32 += b32;
+  fC42 += b42;
+
+  CheckCovariance();
+  
+  // Appoximate step length
+  double dy2dx = (f1+f2)/(r1+r2);
+  Double_t step = (TMath::Abs(x2r)<0.05) ? dx*TMath::Abs(r2 + f2*dy2dx)  // chord
+    : 2.*TMath::ASin(0.5*dx*TMath::Sqrt(1.+dy2dx*dy2dx)*crv)/crv;        // arc
+  step *= TMath::Sqrt(1.+ GetTgl()*GetTgl());
+
+  // Get the track's (x,y,z) and (px,py,pz) in the Global System
+  Double_t r[3]; GetXYZ(r);
+  Double_t p[3]; GetPxPyPz(p);
+  Double_t pp=GetP();
+  p[0] /= pp;
+  p[1] /= pp;
+  p[2] /= pp;
+
+
+  // Rotate to the system where Bx=By=0.
+  Double_t bt=TMath::Sqrt(b[0]*b[0] + b[1]*b[1]);
+  Double_t cosphi=1., sinphi=0.;
+  if (bt > kAlmost0) {cosphi=b[0]/bt; sinphi=b[1]/bt;}
+  Double_t bb=TMath::Sqrt(b[0]*b[0] + b[1]*b[1] + b[2]*b[2]);
+  Double_t costet=1., sintet=0.;
+  if (bb > kAlmost0) {costet=b[2]/bb; sintet=bt/bb;}
+  Double_t vect[7];
+
+  vect[0] = costet*cosphi*r[0] + costet*sinphi*r[1] - sintet*r[2];
+  vect[1] = -sinphi*r[0] + cosphi*r[1];
+  vect[2] = sintet*cosphi*r[0] + sintet*sinphi*r[1] + costet*r[2];
+
+  vect[3] = costet*cosphi*p[0] + costet*sinphi*p[1] - sintet*p[2];
+  vect[4] = -sinphi*p[0] + cosphi*p[1];
+  vect[5] = sintet*cosphi*p[0] + sintet*sinphi*p[1] + costet*p[2];
+
+  vect[6] = pp;
+
+
+  // Do the helix step
+  g3helx3(GetSign()*bb,step,vect);
+
+
+  // Rotate back to the Global System
+  r[0] = cosphi*costet*vect[0] - sinphi*vect[1] + cosphi*sintet*vect[2];
+  r[1] = sinphi*costet*vect[0] + cosphi*vect[1] + sinphi*sintet*vect[2];
+  r[2] = -sintet*vect[0] + costet*vect[2];
+
+  p[0] = cosphi*costet*vect[3] - sinphi*vect[4] + cosphi*sintet*vect[5];
+  p[1] = sinphi*costet*vect[3] + cosphi*vect[4] + sinphi*sintet*vect[5];
+  p[2] = -sintet*vect[3] + costet*vect[5];
+
+
+  // Rotate back to the Tracking System
+  Double_t cosalp = TMath::Cos(fAlpha);
+  Double_t sinalp =-TMath::Sin(fAlpha);
+
+  Double_t 
+  t    = cosalp*r[0] - sinalp*r[1];
+  r[1] = sinalp*r[0] + cosalp*r[1];  
+  r[0] = t;
+
+  t    = cosalp*p[0] - sinalp*p[1]; 
+  p[1] = sinalp*p[0] + cosalp*p[1];
+  p[0] = t; 
+
+
+  // Do the final correcting step to the target plane (linear approximation)
+  Double_t x=r[0], y=r[1], z=r[2];
+  if (TMath::Abs(dx) > kAlmost0) {
+     if (TMath::Abs(p[0]) < kAlmost0) return kFALSE;
+     dx = xk - r[0];
+     x += dx;
+     y += p[1]/p[0]*dx;
+     z += p[2]/p[0]*dx;  
+  }
+
+
+  // Calculate the track parameters
+  t=TMath::Sqrt(p[0]*p[0] + p[1]*p[1]);
+  fX    = x;
+  fP[0] = y;
+  fP[1] = z;
+  fP[2] = p[1]/t;
+  fP[3] = p[2]/t; 
+  fP[4] = GetSign()/(t*pp);
+
+  return kTRUE;
+}
+
+Bool_t AliExternalTrackParam::Translate(Double_t *vTrasl,Double_t *covV){
+  //
+  //Translation: in the event mixing, the tracks can be shifted 
+  //of the difference among primary vertices (vTrasl) and 
+  //the covariance matrix is changed accordingly 
+  //(covV = covariance of the primary vertex).
+  //Origin: "Romita, Rossella" <R.Romita@gsi.de>
+  // 
+  TVector3 translation;
+  // vTrasl coordinates in the local system
+  translation.SetXYZ(vTrasl[0],vTrasl[1],vTrasl[2]);
+  translation.RotateZ(-fAlpha);
+  translation.GetXYZ(vTrasl);
+
+ //compute the new x,y,z of the track
+  Double_t newX=fX-vTrasl[0];
+  Double_t newY=fP[0]-vTrasl[1];
+  Double_t newZ=fP[1]-vTrasl[2];
+  
+  //define the new parameters
+  Double_t newParam[5];
+  newParam[0]=newY;
+  newParam[1]=newZ;
+  newParam[2]=fP[2];
+  newParam[3]=fP[3];
+  newParam[4]=fP[4];
+
+  // recompute the covariance matrix:
+  // 1. covV in the local system
+  Double_t cosRot=TMath::Cos(fAlpha), sinRot=TMath::Sin(fAlpha);
+  TMatrixD qQi(3,3);
+  qQi(0,0) = cosRot;
+  qQi(0,1) = sinRot;
+  qQi(0,2) = 0.;
+  qQi(1,0) = -sinRot;
+  qQi(1,1) = cosRot;
+  qQi(1,2) = 0.;
+  qQi(2,0) = 0.;
+  qQi(2,1) = 0.;
+  qQi(2,2) = 1.;
+  TMatrixD uUi(3,3);
+  uUi(0,0) = covV[0];
+  uUi(0,0) = covV[0];
+  uUi(1,0) = covV[1];
+  uUi(0,1) = covV[1];
+  uUi(2,0) = covV[3];
+  uUi(0,2) = covV[3];
+  uUi(1,1) = covV[2];
+  uUi(2,2) = covV[5];
+  uUi(1,2) = covV[4];
+  if(uUi.Determinant() <= 0.) {return kFALSE;}
+  TMatrixD uUiQi(uUi,TMatrixD::kMult,qQi);
+  TMatrixD m(qQi,TMatrixD::kTransposeMult,uUiQi);
+
+  //2. compute the new covariance matrix of the track
+  Double_t sigmaXX=m(0,0);
+  Double_t sigmaXZ=m(2,0);
+  Double_t sigmaXY=m(1,0);
+  Double_t sigmaYY=GetSigmaY2()+m(1,1);
+  Double_t sigmaYZ=fC[1]+m(1,2);
+  Double_t sigmaZZ=fC[2]+m(2,2);
+  Double_t covarianceYY=sigmaYY + (-1.)*((sigmaXY*sigmaXY)/sigmaXX);
+  Double_t covarianceYZ=sigmaYZ-(sigmaXZ*sigmaXY/sigmaXX);
+  Double_t covarianceZZ=sigmaZZ-((sigmaXZ*sigmaXZ)/sigmaXX);
+
+  Double_t newCov[15];
+  newCov[0]=covarianceYY;
+  newCov[1]=covarianceYZ;
+  newCov[2]=covarianceZZ;
+  for(Int_t i=3;i<15;i++){
+    newCov[i]=fC[i];
+   }
+
+  // set the new parameters
+
+  Set(newX,fAlpha,newParam,newCov);
+
+  return kTRUE;
+ }
+
+void AliExternalTrackParam::CheckCovariance() {
+
+  // This function forces the diagonal elements of the covariance matrix to be positive.
+  // In case the diagonal element is bigger than the maximal allowed value, it is set to
+  // the limit and the off-diagonal elements that correspond to it are set to zero.
+
+    fC[0] = TMath::Abs(fC[0]);
+    if (fC[0]>kC0max) {
+      fC[0] = kC0max;
+      fC[1] = 0;
+      fC[3] = 0;
+      fC[6] = 0;
+      fC[10] = 0;
+    }
+    fC[2] = TMath::Abs(fC[2]);
+    if (fC[2]>kC2max) {
+      fC[2] = kC2max;
+      fC[1] = 0;
+      fC[4] = 0;
+      fC[7] = 0;
+      fC[11] = 0;
+    }
+    fC[5] = TMath::Abs(fC[5]);
+    if (fC[5]>kC5max) {
+      fC[5] = kC5max;
+      fC[3] = 0;
+      fC[4] = 0;
+      fC[8] = 0;
+      fC[12] = 0;
+    }
+    fC[9] = TMath::Abs(fC[9]);
+    if (fC[9]>kC9max) {
+      fC[9] = kC9max;
+      fC[6] = 0;
+      fC[7] = 0;
+      fC[8] = 0;
+      fC[13] = 0;
+    }
+    fC[14] = TMath::Abs(fC[14]);
+    if (fC[14]>kC14max) {
+      fC[14] = kC14max;
+      fC[10] = 0;
+      fC[11] = 0;
+      fC[12] = 0;
+      fC[13] = 0;
+    }
+    
+    // The part below is used for tests and normally is commented out    
+//     TMatrixDSym m(5);
+//     TVectorD eig(5);
+    
+//     m(0,0)=fC[0];
+//     m(1,0)=fC[1];  m(1,1)=fC[2];
+//     m(2,0)=fC[3];  m(2,1)=fC[4];  m(2,2)=fC[5];
+//     m(3,0)=fC[6];  m(3,1)=fC[7];  m(3,2)=fC[8];  m(3,3)=fC[9];
+//     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];
+    
+//     m(0,1)=m(1,0);
+//     m(0,2)=m(2,0); m(1,2)=m(2,1);
+//     m(0,3)=m(3,0); m(1,3)=m(3,1); m(2,3)=m(3,2);
+//     m(0,4)=m(4,0); m(1,4)=m(4,1); m(2,4)=m(4,2); m(3,4)=m(4,3);
+//     m.EigenVectors(eig);
+
+//     //    assert(eig(0)>=0 && eig(1)>=0 && eig(2)>=0 && eig(3)>=0 && eig(4)>=0);
+//     if (!(eig(0)>=0 && eig(1)>=0 && eig(2)>=0 && eig(3)>=0 && eig(4)>=0)) {
+//       AliWarning("Negative eigenvalues of the covariance matrix!");
+//       this->Print();
+//       eig.Print();
+//     }
+}