]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliExternalTrackParam.cxx
Additional comment.
[u/mrichter/AliRoot.git] / STEER / AliExternalTrackParam.cxx
index b7f4ce90f3ede87255449c0b2c2e298f8fb7e2ae..dccebd787bc11c51e1945b6c1a3362a19d7f6feb 100644 (file)
@@ -25,6 +25,9 @@
 // are implemented.
 // Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                            //
 ///////////////////////////////////////////////////////////////////////////////
+#include <cassert>
+
+#include <TVectorD.h>
 #include <TMatrixDSym.h>
 #include <TPolyMarker3D.h>
 #include <TVector3.h>
@@ -37,7 +40,7 @@
 ClassImp(AliExternalTrackParam)
 
 Double32_t AliExternalTrackParam::fgMostProbablePt=kMostProbablePt;
+Bool_t AliExternalTrackParam::fgUseLogTermMS = kFALSE;; 
 //_____________________________________________________________________________
 AliExternalTrackParam::AliExternalTrackParam() :
   AliVTrack(),
@@ -62,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();
 }
 
 //_____________________________________________________________________________
@@ -78,6 +82,7 @@ AliExternalTrackParam& AliExternalTrackParam::operator=(const AliExternalTrackPa
 
     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;
@@ -96,6 +101,7 @@ 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();
 }
 
 //_____________________________________________________________________________
@@ -159,20 +165,36 @@ void AliExternalTrackParam::Set(Double_t xyz[3],Double_t pxpypz[3],
   // azimuthal angle of the centre of the TPC sector in which the point
   // xyz lies
   //
+  const double kSafe = 1e-5;
   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
-     
+  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);
   }
-
+  //
+  Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
+  // protection:  avoid alpha being too close to 0 or +-pi/2
+  if (TMath::Abs(sn)<kSafe) {
+    fAlpha = kSafe;
+    cs=TMath::Cos(fAlpha);
+    sn=TMath::Sin(fAlpha);
+  }
+  else if (cs<kSafe) {
+    fAlpha -= TMath::Sign(kSafe, fAlpha);
+    cs=TMath::Cos(fAlpha);
+    sn=TMath::Sin(fAlpha);    
+  }
   // Get the vertex of origin and the momentum
   TVector3 ver(xyz[0],xyz[1],xyz[2]);
   TVector3 mom(pxpypz[0],pxpypz[1],pxpypz[2]);
+  //
+  // avoid momenta along axis
+  if (TMath::Abs(mom[0])<kSafe) mom[0] = TMath::Sign(kSafe*TMath::Abs(mom[1]), mom[0]);
+  if (TMath::Abs(mom[1])<kSafe) mom[1] = TMath::Sign(kSafe*TMath::Abs(mom[0]), mom[1]);
 
   // Rotate to the local coordinate system
   ver.RotateZ(-fAlpha);
@@ -191,8 +213,10 @@ void AliExternalTrackParam::Set(Double_t xyz[3],Double_t pxpypz[3],
 
   // Covariance matrix (formulas to be simplified)
 
+  if      (TMath::Abs( 1-fP[2]) < kSafe) fP[2] = 1.- kSafe; //Protection
+  else if (TMath::Abs(-1-fP[2]) < kSafe) fP[2] =-1.+ kSafe; //Protection
+
   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;
@@ -234,6 +258,8 @@ void AliExternalTrackParam::Set(Double_t xyz[3],Double_t pxpypz[3],
   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;
 }
 
@@ -258,6 +284,7 @@ void AliExternalTrackParam::AddCovariance(const Double_t c[15]) {
     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();
 }
 
 
@@ -294,8 +321,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));
 }
 
@@ -307,7 +334,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;
@@ -325,7 +352,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;
 }
 
@@ -340,19 +367,24 @@ 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). 
+  // "xTimesRho" - is the product length*density (g/cm^2).
+  //     It should be passed as negative when propagating tracks 
+  //     from the intreaction point to the outside of the central barrel. 
   // "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];
@@ -365,7 +397,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;
   } 
@@ -380,22 +412,28 @@ Bool_t AliExternalTrackParam::CorrectForMeanMaterial
   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;
-     if(theta2>TMath::Pi()*TMath::Pi()) return kFALSE;
-     cC22 = theta2*(1.- fP2*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;
+    //Double_t theta2=1.0259e-6*14*14/28/(beta2*p2)*TMath::Abs(d)*9.36*2.33;
+    Double_t theta2=0.0136*0.0136/(beta2*p2)*TMath::Abs(xOverX0);
+    if (GetUseLogTermMS()) {
+      double lt = 1+0.038*TMath::Log(TMath::Abs(xOverX0));
+      if (lt>0) theta2 *= lt*lt;
+    }
+    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;
   }
 
   //Calculating the energy loss corrections************************
   Double_t cP4=1.;
   if ((xTimesRho != 0.) && (beta2 < 1.)) {
-     Double_t dE=Bethe(p/mass)*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!
-     cP4 = (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
 
 
@@ -413,9 +451,66 @@ Bool_t AliExternalTrackParam::CorrectForMeanMaterial
   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). 
+  //     It should be passed as negative when propagating tracks 
+  //     from the intreaction point to the outside of the central barrel. 
+  // "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). 
+  //     It should be passed as negative when propagating tracks 
+  //     from the intreaction point to the outside of the central barrel. 
+  // "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)) {
@@ -425,61 +520,14 @@ Bool_t AliExternalTrackParam::CorrectForMaterial
   //
   // This function corrects the track parameters for the crossed material
   // "d"    - the thickness (fraction of the radiation length)
+  //     It should be passed as negative when propagating tracks 
+  //     from the intreaction point to the outside of the central barrel. 
   // "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];
-
-  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******************
-  Double_t cC22 = 0.;
-  Double_t cC33 = 0.;
-  Double_t cC43 = 0.;
-  Double_t cC44 = 0.;
-  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;
-     if(theta2>TMath::Pi()*TMath::Pi()) return kFALSE;
-     cC22 = theta2*(1.- fP2*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************************
-  Double_t cP4=1.;
-  if (x0!=0. && beta2<1) {
-     d*=x0;
-     Double_t dE=Bethe(p/mass)*d;
-     Double_t e=TMath::Sqrt(p2 + mass*mass);
-     if ( TMath::Abs(dE) > 0.3*e ) return kFALSE; //30% energy loss is too much!
-     cP4 = (1.- e/p2*dE);
-
-     // Approximate energy loss fluctuation (M.Ivanov)
-     const Double_t knst=0.07; // To be tuned.  
-     Double_t sigmadE=knst*TMath::Sqrt(TMath::Abs(dE)); 
-     cC44 += ((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
-  }
-
-  fC22 += cC22;
-  fC33 += cC33;
-  fC43 += cC43;
-  fC44 += cC44;
-  fP4  *= cP4;
+  return CorrectForMeanMaterial(d,x0*d,mass,kTRUE,Bethe);
 
-  return kTRUE;
 }
 
 Double_t AliExternalTrackParam::BetheBlochAleph(Double_t bg,
@@ -608,11 +656,12 @@ 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) {
-     AliError(Form("Rotation failed ! %.10e",tmp)); 
+     if (TMath::Abs(tmp) > 1.+ Double_t(FLT_EPSILON))  
+        AliWarning(Form("Rotation failed ! %.10e",tmp));
      return kFALSE;
   }
 
@@ -638,6 +687,8 @@ Bool_t AliExternalTrackParam::Rotate(Double_t alpha) {
   fC40 *= ca;
   fC42 *= rr;
 
+  CheckCovariance();
+
   return kTRUE;
 }
 
@@ -651,9 +702,11 @@ 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;
+  if (TMath::Abs(fP[4])< kAlmost0) return kFALSE;
 
   Double_t &fP0=fP[0], &fP1=fP[1], &fP2=fP[2], &fP3=fP[3], &fP4=fP[4];
   Double_t 
@@ -663,12 +716,28 @@ 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));
+  if (TMath::Abs(r1)<kAlmost0)  return kFALSE;
+  if (TMath::Abs(r2)<kAlmost0)  return kFALSE;
 
   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
    
@@ -710,6 +779,8 @@ Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
   fC32 += b32;
   fC42 += b42;
 
+  CheckCovariance();
+
   return kTRUE;
 }
 
@@ -739,6 +810,33 @@ AliExternalTrackParam::Propagate(Double_t alpha, Double_t x, Double_t b) {
   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 {
@@ -847,7 +945,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)
@@ -940,7 +1038,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)
@@ -1079,6 +1177,8 @@ Bool_t AliExternalTrackParam::Update(Double_t p[2], Double_t cov[3]) {
 
   fC44-=k40*c04+k41*c14; 
 
+  CheckCovariance();
+
   return kTRUE;
 }
 
@@ -1266,15 +1366,15 @@ Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
   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=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.;
 
@@ -1301,6 +1401,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 {
   //----------------------------------------------------------------
@@ -1309,7 +1466,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; 
@@ -1349,18 +1506,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
@@ -1383,17 +1528,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
 
@@ -1720,6 +1854,7 @@ void AliExternalTrackParam::g3helx3(Double_t qfield,
  *                                                                *
  ******************************************************************/
   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];
 
@@ -1727,7 +1862,7 @@ void AliExternalTrackParam::g3helx3(Double_t qfield,
   Double_t tet = rho*step;
 
   Double_t tsint, sintt, sint, cos1t; 
-  if (TMath::Abs(tet) > 0.15) {
+  if (TMath::Abs(tet) > 0.03) {
      sint  = TMath::Sin(tet);
      sintt = sint/tet;
      tsint = (tet - sint)/tet;
@@ -1735,7 +1870,7 @@ void AliExternalTrackParam::g3helx3(Double_t qfield,
      cos1t = 2*t*t/tet;
   } else {
      tsint = tet*tet/6.;
-     sintt = 1.- tsint;
+     sintt = (1.-tet*kOvSqSix)*(1.+tet*kOvSqSix); // 1.- tsint;
      sint  = tet*sintt;
      cos1t = 0.5*tet; 
   }
@@ -1765,11 +1900,21 @@ Bool_t AliExternalTrackParam::PropagateToBxByBz(Double_t xk, const Double_t b[3]
 
   Double_t dx=xk-fX;
   if (TMath::Abs(dx)<=kAlmost0)  return kTRUE;
+  if (TMath::Abs(fP[4])<=kAlmost0) return kFALSE;
+  // Do not propagate tracks outside the ALICE detector
+  if (TMath::Abs(dx)>1e5 ||
+      TMath::Abs(GetY())>1e5 ||
+      TMath::Abs(GetZ())>1e5) {
+    AliWarning(Form("Anomalous track, target X:%f",xk));
+    Print();
+    return kFALSE;
+  }
 
   Double_t crv=GetC(b[2]);
   if (TMath::Abs(b[2]) < 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;
 
@@ -1783,7 +1928,7 @@ Bool_t AliExternalTrackParam::PropagateToBxByBz(Double_t xk, const Double_t b[3]
   &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));
 
   //f = F - 1
   Double_t f02=    dx/(r1*r1*r1);            Double_t cc=crv/fP4;
@@ -1824,12 +1969,14 @@ Bool_t AliExternalTrackParam::PropagateToBxByBz(Double_t xk, const Double_t b[3]
   fC32 += b32;
   fC42 += b42;
 
+  CheckCovariance();
   
   // Appoximate step length
-  Double_t step=dx*TMath::Abs(r2 + f2*(f1+f2)/(r1+r2));
+  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);
@@ -1989,3 +2136,74 @@ Bool_t AliExternalTrackParam::Translate(Double_t *vTrasl,Double_t *covV){
 
   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();
+//     }
+}