]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ITS/AliITStrackV2.cxx
Modifications needed to use PID framework based mass during tracking and
[u/mrichter/AliRoot.git] / ITS / AliITStrackV2.cxx
index 53ef4b6c61fc9fb019d13718416e32ec4931d355..86a2fe5166d3d3ed5835f59143d044eaef62261c 100644 (file)
@@ -13,6 +13,8 @@
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
+/* $Id$ */
+
 ///////////////////////////////////////////////////////////////////////////
 //                Implementation of the ITS track class
 //
 #include <TMath.h>
 
 #include "AliCluster.h"
-#include "AliESDtrack.h"
+#include "AliESDVertex.h"
+#include "AliITSReconstructor.h"
 #include "AliITStrackV2.h"
-#include "AliStrLine.h"
+#include "AliTracker.h"
+#include "AliLog.h"
+
+const Int_t AliITStrackV2::fgkWARN = 5;
 
 ClassImp(AliITStrackV2)
 
-const Int_t kWARN=5;
 
 //____________________________________________________________________________
-AliITStrackV2::AliITStrackV2():AliKalmanTrack(),
-  fX(0),
-  fAlpha(0),
+AliITStrackV2::AliITStrackV2() : AliKalmanTrack(),
+  fCheckInvariant(kTRUE),
   fdEdx(0),
-  fP0(0),
-  fP1(0),
-  fP2(0),
-  fP3(0),
-  fP4(0),
-  fC00(0),
-  fC10(0),
-  fC11(0),
-  fC20(0),
-  fC21(0),
-  fC22(0),
-  fC30(0),
-  fC31(0),
-  fC32(0),
-  fC33(0),
-  fC40(0),
-  fC41(0),
-  fC42(0),
-  fC43(0),
-  fC44(0),
   fESDtrack(0)
 {
-    for(Int_t i=0; i<kMaxLayer; i++) fIndex[i]=0;
-    for(Int_t i=0; i<4; i++) fdEdxSample[i]=0;
+  for(Int_t i=0; i<2*AliITSgeomTGeo::kNLayers; i++) {fIndex[i]=-1; fModule[i]=-1;}
+  for(Int_t i=0; i<AliITSgeomTGeo::kNLayers; i++) {fSharedWeight[i]=0;}
+  for(Int_t i=0; i<4; i++) fdEdxSample[i]=0;
 }
 
 
 //____________________________________________________________________________
-AliITStrackV2::AliITStrackV2(AliESDtrack& t,Bool_t c) throw (const Char_t *) :
-AliKalmanTrack() {
+AliITStrackV2::AliITStrackV2(AliESDtrack& t,Bool_t c):
+  AliKalmanTrack(),
+  fCheckInvariant(kTRUE),
+  fdEdx(t.GetITSsignal()),
+  fESDtrack(&t)
+{
   //------------------------------------------------------------------
   // Conversion ESD track -> ITS track.
   // If c==kTRUE, create the ITS track out of the constrained params.
   //------------------------------------------------------------------
-  SetNumberOfClusters(t.GetITSclusters(fIndex));
-  SetLabel(t.GetLabel());
-  SetMass(t.GetMass());
-  //
-  //
+  const AliExternalTrackParam *par=&t;
+  if (c) {
+    par=t.GetConstrainedParam();
+    if (!par) AliError("AliITStrackV2: conversion failed !\n");
+  }
+  Set(par->GetX(),par->GetAlpha(),par->GetParameter(),par->GetCovariance());
 
-  fdEdx=t.GetITSsignal();
-  fAlpha = t.GetAlpha();
-  if      (fAlpha < -TMath::Pi()) fAlpha += 2*TMath::Pi();
-  else if (fAlpha >= TMath::Pi()) fAlpha -= 2*TMath::Pi();
-
-  //Conversion of the track parameters
-  Double_t x,p[5]; 
-  if (c) t.GetConstrainedExternalParameters(fAlpha,x,p);
-  else t.GetExternalParameters(x,p);
-  fX=x;   
-  fP0=p[0]; 
-  fP1=p[1];   SaveLocalConvConst(); 
-  fP2=p[2];
-  fP3=p[3];   x=GetLocalConvConst();
-  fP4=p[4]/x; 
-
-  //Conversion of the covariance matrix
-  Double_t cv[15]; 
-  if (c) t.GetConstrainedExternalCovariance(cv);
-  else t.GetExternalCovariance(cv);
-  fC00=cv[0 ];
-  fC10=cv[1 ];   fC11=cv[2 ];
-  fC20=cv[3 ];   fC21=cv[4 ];   fC22=cv[5 ];
-  fC30=cv[6 ];   fC31=cv[7 ];   fC32=cv[8 ];   fC33=cv[9 ];
-  fC40=cv[10]/x; fC41=cv[11]/x; fC42=cv[12]/x; fC43=cv[13]/x; fC44=cv[14]/x/x;
+  SetLabel(t.GetITSLabel());
+  SetMass(t.GetMassForTracking());
+  SetNumberOfClusters(t.GetITSclusters(fIndex));
 
   if (t.GetStatus()&AliESDtrack::kTIME) {
     StartTimeIntegral();
     Double_t times[10]; t.GetIntegratedTimes(times); SetIntegratedTimes(times);
     SetIntegratedLength(t.GetIntegratedLength());
   }
-  fESDtrack=&t;
 
-  //  if (!Invariant()) throw "AliITStrackV2: conversion failed !\n";
+  for(Int_t i=0; i<AliITSgeomTGeo::kNLayers; i++) {fSharedWeight[i]=0;}
   for(Int_t i=0; i<4; i++) fdEdxSample[i]=0;
 }
 
+//____________________________________________________________________________
+void AliITStrackV2::ResetClusters() {
+  //------------------------------------------------------------------
+  // Reset the array of attached clusters.
+  //------------------------------------------------------------------
+  for (Int_t i=0; i<2*AliITSgeomTGeo::kNLayers; i++) fIndex[i]=-1;
+  for (Int_t i=0; i<AliITSgeomTGeo::kNLayers; i++) {fSharedWeight[i]=0;}
+  SetChi2(0.); 
+  SetNumberOfClusters(0);
+} 
+
+//____________________________________________________________________________
 void AliITStrackV2::UpdateESDtrack(ULong_t flags) const {
+  // Update track params
   fESDtrack->UpdateTrackParams(this,flags);
+  //
+  // set correctly the global label
+  if (fESDtrack->IsOn(AliESDtrack::kTPCin)) { 
+    // for global track the GetLabel should be negative if
+    // 1) GetTPCLabel<0
+    // 2) this->GetLabel()<0
+    // 3) GetTPCLabel() != this->GetLabel()
+    int label = fESDtrack->GetTPCLabel();
+    int itsLabel = GetLabel();
+    if (label<0 || itsLabel<0 || itsLabel!=label) label = -TMath::Abs(label);
+    fESDtrack->SetLabel(label);
+  }
+  //
+  // copy the module indices
+  Int_t i;
+  for(i=0;i<2*AliITSgeomTGeo::kNLayers;i++) {
+    //   printf("     %d\n",GetModuleIndex(i));
+    fESDtrack->SetITSModuleIndex(i,GetModuleIndex(i));
+  }
+  // copy the map of shared clusters
+  if(flags==AliESDtrack::kITSin) {
+    UChar_t itsSharedMap=0;
+    for(i=0;i<AliITSgeomTGeo::kNLayers;i++) {
+      if(fSharedWeight[i]>0) SETBIT(itsSharedMap,i);
+      
+    }
+    fESDtrack->SetITSSharedMap(itsSharedMap);
+  }
+
+  // copy the 4 dedx samples
+  Double_t sdedx[4]={0.,0.,0.,0.};
+  for(i=0; i<4; i++) sdedx[i]=fdEdxSample[i];
+  fESDtrack->SetITSdEdxSamples(sdedx);
 }
 
 //____________________________________________________________________________
-AliITStrackV2::AliITStrackV2(const AliITStrackV2& t) : AliKalmanTrack(t) {
+AliITStrackV2::AliITStrackV2(const AliITStrackV2& t) : 
+  AliKalmanTrack(t),
+  fCheckInvariant(t.fCheckInvariant),
+  fdEdx(t.fdEdx),
+  fESDtrack(t.fESDtrack) 
+{
   //------------------------------------------------------------------
   //Copy constructor
   //------------------------------------------------------------------
-  fX=t.fX;
-  fAlpha=t.fAlpha;
-  fdEdx=t.fdEdx;
-
-  fP0=t.fP0; fP1=t.fP1; fP2=t.fP2; fP3=t.fP3; fP4=t.fP4;
-
-  fC00=t.fC00;
-  fC10=t.fC10;  fC11=t.fC11;
-  fC20=t.fC20;  fC21=t.fC21;  fC22=t.fC22;
-  fC30=t.fC30;  fC31=t.fC31;  fC32=t.fC32;  fC33=t.fC33;
-  fC40=t.fC40;  fC41=t.fC41;  fC42=t.fC42;  fC43=t.fC43;  fC44=t.fC44;
-
-  Int_t n=GetNumberOfClusters();
-  for (Int_t i=0; i<n; i++) {
+  Int_t i;
+  for (i=0; i<4; i++) fdEdxSample[i]=t.fdEdxSample[i];
+  for (i=0; i<2*AliITSgeomTGeo::GetNLayers(); i++) {
     fIndex[i]=t.fIndex[i];
-    if (i<4) fdEdxSample[i]=t.fdEdxSample[i];
+    fModule[i]=t.fModule[i];
   }
-  fESDtrack=t.fESDtrack;
+  for (i=0; i<AliITSgeomTGeo::kNLayers; i++) {fSharedWeight[i]=t.fSharedWeight[i];}
 }
 
 //_____________________________________________________________________________
@@ -147,8 +154,8 @@ Int_t AliITStrackV2::Compare(const TObject *o) const {
   // This function compares tracks according to the their curvature
   //-----------------------------------------------------------------
   AliITStrackV2 *t=(AliITStrackV2*)o;
-  //Double_t co=TMath::Abs(t->Get1Pt());
-  //Double_t c =TMath::Abs(Get1Pt());
+  //Double_t co=OneOverPt();
+  //Double_t c =OneOverPt();
   Double_t co=t->GetSigmaY2()*t->GetSigmaZ2();
   Double_t c =GetSigmaY2()*GetSigmaZ2();
   if (c>co) return 1;
@@ -156,608 +163,623 @@ Int_t AliITStrackV2::Compare(const TObject *o) const {
   return 0;
 }
 
-//_____________________________________________________________________________
-void AliITStrackV2::GetExternalCovariance(Double_t cc[15]) const {
-  //-------------------------------------------------------------------------
-  // This function returns an external representation of the covriance matrix.
-  //   (See comments in AliTPCtrack.h about external track representation)
-  //-------------------------------------------------------------------------
-  Double_t a=GetLocalConvConst();
-
-  cc[0 ]=fC00;
-  cc[1 ]=fC10;   cc[2 ]=fC11;
-  cc[3 ]=fC20;   cc[4 ]=fC21;   cc[5 ]=fC22;
-  cc[6 ]=fC30;   cc[7 ]=fC31;   cc[8 ]=fC32;   cc[9 ]=fC33;
-  cc[10]=fC40*a; cc[11]=fC41*a; cc[12]=fC42*a; cc[13]=fC43*a; cc[14]=fC44*a*a;
-}
-
 //____________________________________________________________________________
-Int_t AliITStrackV2::PropagateToVertex(Double_t d,Double_t x0) {
+Bool_t 
+AliITStrackV2::PropagateToVertex(const AliESDVertex *v,Double_t d,Double_t x0) 
+{
   //------------------------------------------------------------------
   //This function propagates a track to the minimal distance from the origin
-  //------------------------------------------------------------------
-  //Double_t xv=fP2*(fX*fP2 - fP0*TMath::Sqrt(1.- fP2*fP2)); //linear approxim.
-  Double_t tgf=-(fP4*fX - fP2)/(fP4*fP0 + TMath::Sqrt(1 - fP2*fP2));
-  Double_t snf=tgf/TMath::Sqrt(1.+ tgf*tgf);
-  Double_t xv=(snf - fP2)/fP4 + fX;
-  return PropagateTo(xv,d,x0);
+  //------------------------------------------------------------------  
+  Double_t bz=GetBz();
+  if (PropagateToDCA(v,bz,kVeryBig)) {
+    Double_t xOverX0,xTimesRho; 
+    xOverX0 = d; xTimesRho = d*x0;
+    if (CorrectForMeanMaterial(xOverX0,xTimesRho,kTRUE)) return kTRUE;
+  }
+  return kFALSE;
 }
 
 //____________________________________________________________________________
-Int_t AliITStrackV2::
-GetGlobalXYZat(Double_t xk, Double_t &x, Double_t &y, Double_t &z) const {
+Bool_t AliITStrackV2::
+GetGlobalXYZat(Double_t xloc, Double_t &x, Double_t &y, Double_t &z) const {
   //------------------------------------------------------------------
   //This function returns a track position in the global system
   //------------------------------------------------------------------
-  Double_t dx=xk-fX;
-  Double_t f1=fP2, f2=f1 + fP4*dx;
-  if (TMath::Abs(f2) >= 0.9999) {
-    Int_t n=GetNumberOfClusters();
-    if (n>kWARN) 
-      Warning("GetGlobalXYZat","Propagation failed (%d) !\n",n);
-    return 0;
-  }
-
-  Double_t r1=sqrt(1.- f1*f1), r2=sqrt(1.- f2*f2);
-  
-  Double_t yk = fP0 + dx*(f1+f2)/(r1+r2);
-  Double_t zk = fP1 + dx*(f1+f2)/(f1*r2 + f2*r1)*fP3;
-
-  Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
-  x = xk*cs - yk*sn;
-  y = xk*sn + yk*cs;
-  z = zk;
-
-  return 1;
+  Double_t r[3];
+  Bool_t rc=GetXYZAt(xloc, GetBz(), r);
+  x=r[0]; y=r[1]; z=r[2]; 
+  return rc;
 }
 
 //_____________________________________________________________________________
-void AliITStrackV2::ApproximateHelixWithLine(Double_t xk, AliStrLine *line)
-{
-  //------------------------------------------------------------
-  // Approximate the track (helix) with a straight line tangent to the
-  // helix in the point defined by r (F. Prino, prino@to.infn.it)
-  //------------------------------------------------------------
-  Double_t mom[3];
-  Double_t azim = TMath::ASin(fP2)+fAlpha;
-  Double_t theta = TMath::Pi()/2. - TMath::ATan(fP3);
-  mom[0] = TMath::Sin(theta)*TMath::Cos(azim);
-  mom[1] = TMath::Sin(theta)*TMath::Sin(azim);
-  mom[2] = TMath::Cos(theta);
-  Double_t pos[3];
-  GetGlobalXYZat(xk,pos[0],pos[1],pos[2]);
-  line->SetP0(pos);
-  line->SetCd(mom);
-}
-//_____________________________________________________________________________
-Double_t AliITStrackV2::GetPredictedChi2(const AliCluster *c) const 
-{
+Double_t AliITStrackV2::GetPredictedChi2(const AliCluster *c) const {
   //-----------------------------------------------------------------
   // This function calculates a predicted chi2 increment.
   //-----------------------------------------------------------------
-  Double_t r00=c->GetSigmaY2(), r01=0., r11=c->GetSigmaZ2();
-  r00+=fC00; r01+=fC10; r11+=fC11;
-  //
-  Double_t det=r00*r11 - r01*r01;
-  if (TMath::Abs(det) < 1.e-30) {
-    Int_t n=GetNumberOfClusters();
-    if (n>kWARN) 
-      Warning("GetPredictedChi2","Singular matrix (%d) !\n",n);
-    return 1e10;
-  }
-  Double_t tmp=r00; r00=r11; r11=tmp; r01=-r01;
-
-  Double_t dy=c->GetY() - fP0, dz=c->GetZ() - fP1;
-
-  return (dy*r00*dy + 2*r01*dy*dz + dz*r11*dz)/det;
+  Double_t p[2]={c->GetY(), c->GetZ()};
+  Double_t cov[3]={c->GetSigmaY2(), 0., c->GetSigmaZ2()};
+  return AliExternalTrackParam::GetPredictedChi2(p,cov);
 }
 
 //____________________________________________________________________________
-Int_t AliITStrackV2::CorrectForMaterial(Double_t d, Double_t x0) {
+Bool_t AliITStrackV2::PropagateTo(Double_t xk, Double_t d, Double_t x0) {
   //------------------------------------------------------------------
-  //This function corrects the track parameters for crossed material
+  //This function propagates a track
   //------------------------------------------------------------------
-  Double_t p2=(1.+ fP3*fP3)/(Get1Pt()*Get1Pt());
-  Double_t beta2=p2/(p2 + GetMass()*GetMass());
-  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;
-  }
-
-  //Energy losses************************
-  if (x0!=0.) {
-     d*=x0;
-     Double_t dE=0.153e-3/beta2*(log(5940*beta2/(1-beta2)) - beta2)*d;
-     if (beta2/(1-beta2)>3.5*3.5)
-       dE=0.153e-3/beta2*(log(3.5*5940)+0.5*log(beta2/(1-beta2)) - beta2)*d;
-
-     fP4*=(1.- TMath::Sqrt(p2+GetMass()*GetMass())/p2*dE);
+  Double_t oldX=GetX(), oldY=GetY(), oldZ=GetZ();
+  
+  //Double_t bz=GetBz();
+  //if (!AliExternalTrackParam::PropagateTo(xk,bz)) return kFALSE;
+  Double_t b[3]; GetBxByBz(b);
+  if (!AliExternalTrackParam::PropagateToBxByBz(xk,b)) return kFALSE;
+  Double_t xOverX0,xTimesRho; 
+  xOverX0 = d; xTimesRho = d*x0;
+  if (!CorrectForMeanMaterial(xOverX0,xTimesRho,kTRUE)) return kFALSE;
+
+  Double_t x=GetX(), y=GetY(), z=GetZ();
+  if (IsStartedTimeIntegral() && x>oldX) {
+    Double_t l2 = (x-oldX)*(x-oldX) + (y-oldY)*(y-oldY) + (z-oldZ)*(z-oldZ);
+    AddTimeStep(TMath::Sqrt(l2));
   }
 
-  if (!Invariant()) return 0;
-
-  return 1;
+  return kTRUE;
 }
 
 //____________________________________________________________________________
-Int_t AliITStrackV2::PropagateTo(Double_t xk, Double_t d, Double_t x0) {
-  //------------------------------------------------------------------
-  //This function propagates a track
-  //------------------------------------------------------------------
-  Double_t x1=fX, x2=xk, dx=x2-x1;
-  Double_t f1=fP2, f2=f1 + fP4*dx;
-  if (TMath::Abs(f2) >= 0.98) {
-    // MI change  - don't propagate highly inclined tracks
-    //              covariance matrix distorted
-    //Int_t n=GetNumberOfClusters();
-    //if (n>kWARN) 
-    //   Warning("PropagateTo","Propagation failed !\n",n);
-    return 0;
-  }
-  Double_t lcc=GetLocalConvConst();  
-
-  // old position [SR, GSI, 17.02.2003]
-  Double_t oldX = fX, oldY = fP0, oldZ = fP1;
-
-  Double_t r1=sqrt(1.- f1*f1), r2=sqrt(1.- f2*f2);
+Bool_t AliITStrackV2::PropagateToTGeo(Double_t xToGo, Int_t nstep, Double_t &xOverX0, Double_t &xTimesRho, Bool_t addTime) {
+  //-------------------------------------------------------------------
+  //  Propagates the track to a reference plane x=xToGo in n steps.
+  //  These n steps are only used to take into account the curvature.
+  //  The material is calculated with TGeo. (L.Gaudichet)
+  //-------------------------------------------------------------------
   
-  fP0 += dx*(f1+f2)/(r1+r2);
-  fP1 += dx*(f1+f2)/(f1*r2 + f2*r1)*fP3;
-  fP2 += dx*fP4;
+  Double_t startx = GetX(), starty = GetY(), startz = GetZ();
+  Double_t sign = (startx<xToGo) ? -1.:1.;
+  Double_t step = (xToGo-startx)/TMath::Abs(nstep);
 
-  //f = F - 1
-  
-  Double_t f02=    dx/(r1*r1*r1);
-  Double_t f04=0.5*dx*dx/(r1*r1*r1);
-  Double_t f12=    dx*fP3*f1/(r1*r1*r1);
-  Double_t f14=0.5*dx*dx*fP3*f1/(r1*r1*r1);
-  Double_t f13=    dx/r1;
-  Double_t f24=    dx; 
-  
-  //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;
+  Double_t start[3], end[3], mparam[7];
+  //Double_t bz = GetBz();
+  Double_t b[3]; GetBxByBz(b);
+  Double_t bz = b[2];
+
+  Double_t x = startx;
   
-  //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;
-
-  fX=x2;
-
-  //Change of the magnetic field *************
-  SaveLocalConvConst();
-  fP4*=lcc/GetLocalConvConst();
-
-  if (!CorrectForMaterial(d,x0)) return 0;
-
-  // Integrated Time [SR, GSI, 17.02.2003]
-  if (IsStartedTimeIntegral() && fX>oldX) {
-    Double_t l2 = (fX-oldX)*(fX-oldX)+(fP0-oldY)*(fP0-oldY)+
-                  (fP1-oldZ)*(fP1-oldZ);
+  for (Int_t i=0; i<nstep; i++) {
+    
+    GetXYZ(start);   //starting global position
+    x += step;
+    if (!GetXYZAt(x, bz, end)) return kFALSE;
+    //if (!AliExternalTrackParam::PropagateTo(x, bz)) return kFALSE;
+    if (!AliExternalTrackParam::PropagateToBxByBz(x, b)) return kFALSE;
+    AliTracker::MeanMaterialBudget(start, end, mparam);
+    xTimesRho = sign*mparam[4]*mparam[0];
+    xOverX0   = mparam[1];
+    if (mparam[1]<900000) {
+      if (!AliExternalTrackParam::CorrectForMeanMaterial(xOverX0,
+                          xTimesRho,GetMass())) return kFALSE;
+    } else { // this happens when MeanMaterialBudget cannot cross a boundary
+      return kFALSE;
+    }
+  }
+
+  if (addTime && IsStartedTimeIntegral() && GetX()>startx) {
+    Double_t l2 = ( (GetX()-startx)*(GetX()-startx) +
+                   (GetY()-starty)*(GetY()-starty) +
+                   (GetZ()-startz)*(GetZ()-startz) );
     AddTimeStep(TMath::Sqrt(l2));
   }
-  //
 
-  return 1;
+  return kTRUE;
 }
 
 //____________________________________________________________________________
-Int_t AliITStrackV2::Update(const AliCluster* c, Double_t chi2, UInt_t index) {
+Bool_t AliITStrackV2::Update(const AliCluster* c, Double_t chi2, Int_t index) 
+{
   //------------------------------------------------------------------
   //This function updates track parameters
   //------------------------------------------------------------------
-  Double_t p0=fP0,p1=fP1,p2=fP2,p3=fP3,p4=fP4;
-  Double_t c00=fC00;
-  Double_t c10=fC10, c11=fC11;
-  Double_t c20=fC20, c21=fC21, c22=fC22;
-  Double_t c30=fC30, c31=fC31, c32=fC32, c33=fC33;
-  Double_t c40=fC40, c41=fC41, c42=fC42, c43=fC43, c44=fC44;
-
-
-  Double_t r00=c->GetSigmaY2(), r01=0., r11=c->GetSigmaZ2();
-  r00+=fC00; r01+=fC10; r11+=fC11;
-  Double_t det=r00*r11 - r01*r01;
-  Double_t tmp=r00; r00=r11/det; r11=tmp/det; r01=-r01/det;
-
-  Double_t k00=fC00*r00+fC10*r01, k01=fC00*r01+fC10*r11;
-  Double_t k10=fC10*r00+fC11*r01, k11=fC10*r01+fC11*r11;
-  Double_t k20=fC20*r00+fC21*r01, k21=fC20*r01+fC21*r11;
-  Double_t k30=fC30*r00+fC31*r01, k31=fC30*r01+fC31*r11;
-  Double_t k40=fC40*r00+fC41*r01, k41=fC40*r01+fC41*r11;
-
-  Double_t dy=c->GetY() - fP0, dz=c->GetZ() - fP1;
-  Double_t sf=fP2 + k20*dy + k21*dz;
-  
-  fP0 += k00*dy + k01*dz;
-  fP1 += k10*dy + k11*dz;
-  fP2  = sf;
-  fP3 += k30*dy + k31*dz;
-  fP4 += k40*dy + k41*dz;
-  
-  Double_t c01=fC10, c02=fC20, c03=fC30, c04=fC40;
-  Double_t c12=fC21, c13=fC31, c14=fC41;
-
-  fC00-=k00*fC00+k01*fC10; fC10-=k00*c01+k01*fC11;
-  fC20-=k00*c02+k01*c12;   fC30-=k00*c03+k01*c13;
-  fC40-=k00*c04+k01*c14; 
+  Double_t p[2]={c->GetY(), c->GetZ()};
+  Double_t cov[3]={c->GetSigmaY2(), c->GetSigmaYZ(), c->GetSigmaZ2()};
 
-  fC11-=k10*c01+k11*fC11;
-  fC21-=k10*c02+k11*c12;   fC31-=k10*c03+k11*c13;
-  fC41-=k10*c04+k11*c14; 
-
-  fC22-=k20*c02+k21*c12;   fC32-=k20*c03+k21*c13;
-  fC42-=k20*c04+k21*c14; 
-
-  fC33-=k30*c03+k31*c13;
-  fC43-=k30*c04+k31*c14; 
-
-  fC44-=k40*c04+k41*c14; 
+  if (!AliExternalTrackParam::Update(p,cov)) return kFALSE;
 
+  Int_t n=GetNumberOfClusters();
   if (!Invariant()) {
-     fP0=p0; fP1=p1; fP2=p2; fP3=p3; fP4=p4;
-     fC00=c00;
-     fC10=c10; fC11=c11;
-     fC20=c20; fC21=c21; fC22=c22;
-     fC30=c30; fC31=c31; fC32=c32; fC33=c33;
-     fC40=c40; fC41=c41; fC42=c42; fC43=c43; fC44=c44;
-     return 0;
+    if (n>fgkWARN) AliDebug(1,"Wrong invariant !");
+     return kFALSE;
   }
 
-  if (chi2<0) return 1;
+  if (chi2<0) return kTRUE;
+
+  // fill residuals for ITS+TPC tracks 
+  if (fESDtrack) {
+    if (fESDtrack->GetStatus()&AliESDtrack::kTPCin) {
+      AliTracker::FillResiduals(this,p,cov,c->GetVolumeId());
+    }
+  }
 
-  Int_t n=GetNumberOfClusters();
   fIndex[n]=index;
   SetNumberOfClusters(n+1);
   SetChi2(GetChi2()+chi2);
 
-  return 1;
+  return kTRUE;
 }
 
-Int_t AliITStrackV2::Invariant() const {
+Bool_t AliITStrackV2::Invariant() const {
   //------------------------------------------------------------------
   // This function is for debugging purpose only
   //------------------------------------------------------------------
+  if(!fCheckInvariant) return kTRUE;
+
   Int_t n=GetNumberOfClusters();
-  
-  if (TMath::Abs(fP2)>=0.9999){
-     if (n>kWARN) Warning("Invariant","fP2=%f\n",fP2);
-     return 0;
+  static Float_t bz = GetBz();
+  // take into account the misalignment error
+  Float_t maxMisalErrY2=0,maxMisalErrZ2=0;
+  //RS
+  const AliITSRecoParam* recopar = AliITSReconstructor::GetRecoParam();
+  if (!recopar) recopar = AliITSRecoParam::GetHighFluxParam();
+
+  for (Int_t lay=0; lay<AliITSgeomTGeo::kNLayers; lay++) {
+    maxMisalErrY2 = TMath::Max(maxMisalErrY2,recopar->GetClusterMisalErrorY(lay,bz));
+    maxMisalErrZ2 = TMath::Max(maxMisalErrZ2,recopar->GetClusterMisalErrorZ(lay,bz));
   }
-  if (fC00<=0 || fC00>9.) {
-     if (n>kWARN) Warning("Invariant","fC00=%f\n",fC00); 
-     return 0;
+  maxMisalErrY2 *= maxMisalErrY2;
+  maxMisalErrZ2 *= maxMisalErrZ2;
+  // this is because when we reset before refitting, we multiply the
+  // matrix by 10
+  maxMisalErrY2 *= 10.; 
+  maxMisalErrZ2 *= 10.;
+
+  Double_t sP2=GetParameter()[2];
+  if (TMath::Abs(sP2) >= kAlmost1){
+    if (n>fgkWARN) AliDebug(1,Form("fP2=%f\n",sP2));
+     return kFALSE;
   }
-  if (fC11<=0 || fC11>9.) {
-     if (n>kWARN) Warning("Invariant","fC11=%f\n",fC11); 
-     return 0;
+  Double_t sC00=GetCovariance()[0];
+  if (sC00<=0 || sC00>(9.+maxMisalErrY2)) {
+    if (n>fgkWARN) AliDebug(1,Form("fC00=%f\n",sC00)); 
+     return kFALSE;
   }
-  if (fC22<=0 || fC22>1.) {
-     if (n>kWARN) Warning("Invariant","fC22=%f\n",fC22); 
-     return 0;
+  Double_t sC11=GetCovariance()[2];
+  if (sC11<=0 || sC11>(9.+maxMisalErrZ2)) {
+    if (n>fgkWARN) AliDebug(1,Form("fC11=%f\n",sC11)); 
+     return kFALSE;
   }
-  if (fC33<=0 || fC33>1.) {
-     if (n>kWARN) Warning("Invariant","fC33=%f\n",fC33); 
-     return 0;
+  Double_t sC22=GetCovariance()[5];
+  if (sC22<=0 || sC22>1.) {
+    if (n>fgkWARN) AliDebug(1,Form("fC22=%f\n",sC22)); 
+     return kFALSE;
   }
-  if (fC44<=0 || fC44>6e-5) {
-     if (n>kWARN) Warning("Invariant","fC44=%f\n",fC44);
-     return 0;
+  Double_t sC33=GetCovariance()[9];
+  if (sC33<=0 || sC33>1.) {
+    if (n>fgkWARN) AliDebug(1,Form("fC33=%f\n",sC33)); 
+     return kFALSE;
   }
-  return 1;
+  Double_t sC44=GetCovariance()[14];
+  if (sC44<=0 /*|| sC44>6e-5*/) {
+    if (n>fgkWARN) AliDebug(1,Form("fC44=%f\n",sC44));
+     return kFALSE;
+  }
+
+  return kTRUE;
 }
 
 //____________________________________________________________________________
-Int_t AliITStrackV2::Propagate(Double_t alp,Double_t xk) {
+Bool_t AliITStrackV2::Propagate(Double_t alp,Double_t xk) {
   //------------------------------------------------------------------
   //This function propagates a track
   //------------------------------------------------------------------
-  Double_t alpha=fAlpha, x=fX;
-  Double_t p0=fP0,p1=fP1,p2=fP2,p3=fP3,p4=fP4;
-  Double_t c00=fC00;
-  Double_t c10=fC10, c11=fC11;
-  Double_t c20=fC20, c21=fC21, c22=fC22;
-  Double_t c30=fC30, c31=fC31, c32=fC32, c33=fC33;
-  Double_t c40=fC40, c41=fC41, c42=fC42, c43=fC43, c44=fC44;
-
-  if      (alp < -TMath::Pi()) alp += 2*TMath::Pi();
-  else if (alp >= TMath::Pi()) alp -= 2*TMath::Pi();
-  Double_t ca=TMath::Cos(alp-fAlpha), sa=TMath::Sin(alp-fAlpha);
-  Double_t sf=fP2, cf=TMath::Sqrt(1.- fP2*fP2);
-
-  // **** rotation **********************
-  {
-  fAlpha = alp;
-  fX =  x*ca + p0*sa;
-  fP0= -x*sa + p0*ca;
-  fP2=  sf*ca - cf*sa;
-
-  Double_t rr=(ca+sf/cf*sa);  
-
-  fC00 *= (ca*ca);
-  fC10 *= ca; 
-  fC20 *= ca*rr;
-  fC30 *= ca;
-  fC40 *= ca;
-  //fC11 = fC11;
-  fC21 *= rr;
-  //fC31 = fC31; 
-  //fC41 = fC41;
-  fC22 *= rr*rr;
-  fC32 *= rr;
-  fC42 *= rr;
-  //fC33=fC33;
-  //fC43=fC43;
-  //fC44=fC44;
-  }
-
-  // **** translation ******************
-  {
-  Double_t dx=xk-fX;
-  Double_t f1=fP2, f2=f1 + fP4*dx;
-  if (TMath::Abs(f2) >= 0.98) {
-    // don't propagate highly inclined tracks MI
-    return 0;
-  }
-  //    Int_t n=GetNumberOfClusters();
-  //  if (n>kWARN) 
-  //     Warning("Propagate","Propagation failed (%d) !\n",n);
-  //  return 0;
-  //}
-  Double_t lcc=GetLocalConvConst();  
-
-  Double_t r1=TMath::Sqrt(1.- f1*f1), r2=TMath::Sqrt(1.- f2*f2);
-
-  fX=xk;
-  fP0 += dx*(f1+f2)/(r1+r2);
-  fP1 += dx*(f1+f2)/(f1*r2 + f2*r1)*fP3;
-  fP2 += dx*fP4;
-
-  //Change of the magnetic field *************
-  SaveLocalConvConst();
-  fP4*=lcc/GetLocalConvConst();
-
-  //f = F - 1
-  
-  Double_t f02=    dx/(r1*r1*r1);
-  Double_t f04=0.5*dx*dx/(r1*r1*r1);
-  Double_t f12=    dx*fP3*f1/(r1*r1*r1);
-  Double_t f14=0.5*dx*dx*fP3*f1/(r1*r1*r1);
-  Double_t f13=    dx/r1;
-  Double_t f24=    dx; 
-  
-  //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;
+  //Double_t bz=GetBz();
+  //if (!AliExternalTrackParam::Propagate(alp,xk,bz)) return kFALSE;
+  Double_t b[3]; GetBxByBz(b);
+  if (!AliExternalTrackParam::PropagateBxByBz(alp,xk,b)) return kFALSE;
 
   if (!Invariant()) {
-     fAlpha=alpha; 
-     fX=x; 
-     fP0=p0; fP1=p1; fP2=p2; fP3=p3; fP4=p4;
-     fC00=c00;
-     fC10=c10; fC11=c11;
-     fC20=c20; fC21=c21; fC22=c22;
-     fC30=c30; fC31=c31; fC32=c32; fC33=c33;
-     fC40=c40; fC41=c41; fC42=c42; fC43=c43; fC44=c44;
-     return 0;
-  }
+    Int_t n=GetNumberOfClusters();
+    if (n>fgkWARN) AliDebug(1,"Wrong invariant !");
+    return kFALSE;
   }
 
-  return 1;
+  return kTRUE;
 }
 
+Bool_t AliITStrackV2::MeanBudgetToPrimVertex(Double_t xyz[3], Double_t step, Double_t &d) const {
 
-Double_t AliITStrackV2::GetD(Double_t x, Double_t y) const {
-  //------------------------------------------------------------------
-  // This function calculates the transverse impact parameter
-  // with respect to a point with global coordinates (x,y)
-  //------------------------------------------------------------------
-  Double_t xt=fX, yt=fP0;
+  //-------------------------------------------------------------------
+  //  Get the mean material budget between the actual point and the
+  //  primary vertex. (L.Gaudichet)
+  //-------------------------------------------------------------------
 
-  Double_t sn=TMath::Sin(fAlpha), cs=TMath::Cos(fAlpha);
-  Double_t a = x*cs + y*sn;
-  y = -x*sn + y*cs; x=a;
-  xt-=x; yt-=y;
+  Double_t cs=TMath::Cos(GetAlpha()), sn=TMath::Sin(GetAlpha());
+  Double_t vertexX = xyz[0]*cs + xyz[1]*sn;
 
-  sn=fP4*xt - fP2; cs=fP4*yt + TMath::Sqrt(1.- fP2*fP2);
-  a=2*(xt*fP2 - yt*TMath::Sqrt(1.- fP2*fP2))-fP4*(xt*xt + yt*yt);
-  if (fP4<0) a=-a;
-  return a/(1 + TMath::Sqrt(sn*sn + cs*cs));
-}
+  Int_t nstep = Int_t((GetX()-vertexX)/step);
+  if (nstep<1) nstep = 1;
+  step = (GetX()-vertexX)/nstep;
 
-Double_t AliITStrackV2::GetZat(Double_t x) const {
-  //------------------------------------------------------------------
-  // This function calculates the z at given x point - in current coordinate system
-  //------------------------------------------------------------------
-  Double_t x1=fX, x2=x, dx=x2-x1;
-  //
-  Double_t f1=fP2, f2=f1 + fP4*dx;
-  if (TMath::Abs(f2) >= 0.9999) {
-    return 10000000;
-  }
-  Double_t r1=sqrt(1.- f1*f1), r2=sqrt(1.- f2*f2);
-  Double_t z =  fP1 + dx*(f1+f2)/(f1*r2 + f2*r1)*fP3;
-  return z;
-}
+  //  Double_t mparam[7], densMean=0, radLength=0, length=0;
+  Double_t mparam[7];
+  Double_t p1[3], p2[3], x = GetX(), bz = GetBz();
+  GetXYZ(p1);
 
+  d=0.;
 
+  for (Int_t i=0; i<nstep; i++) {
+    x  += step;
+    if (!GetXYZAt(x, bz, p2)) return kFALSE;
+    AliTracker::MeanMaterialBudget(p1, p2, mparam);
+    if (mparam[1]>900000) return kFALSE;
+    d  += mparam[1];
 
+    p1[0] = p2[0];
+    p1[1] = p2[1];
+    p1[2] = p2[2];
+  }
 
-Int_t AliITStrackV2::Improve(Double_t x0,Double_t xyz[3],Double_t ers[3]) {
+  return kTRUE;
+}
+
+Bool_t AliITStrackV2::Improve(Double_t x0,Double_t xyz[3],Double_t ers[3]) {
   //------------------------------------------------------------------
-  //This function improves angular track parameters  
+  //This function improves angular track parameters
   //------------------------------------------------------------------
-  Double_t cs=TMath::Cos(fAlpha), sn=TMath::Sin(fAlpha);
-  //Double_t xv = xyz[0]*cs + xyz[1]*sn; // vertex
-    Double_t yv =-xyz[0]*sn + xyz[1]*cs; // in the
-    Double_t zv = xyz[2];                // local frame
-  Double_t dy=fP0-yv, dz=fP1-zv;
-  Double_t r2=fX*fX+dy*dy;
-  Double_t p2=(1.+ GetTgl()*GetTgl())/(Get1Pt()*Get1Pt());
+  //Store the initail track parameters
+
+  Double_t x = GetX();
+  Double_t alpha = GetAlpha();
+  Double_t par[] = {GetY(),GetZ(),GetSnp(),GetTgl(),GetSigned1Pt()};
+  Double_t cov[] = {
+    GetSigmaY2(),
+    GetSigmaZY(),
+    GetSigmaZ2(),
+    GetSigmaSnpY(),
+    GetSigmaSnpZ(),
+    GetSigmaSnp2(),
+    GetSigmaTglY(),
+    GetSigmaTglZ(),
+    GetSigmaTglSnp(),
+    GetSigmaTgl2(),
+    GetSigma1PtY(),
+    GetSigma1PtZ(),
+    GetSigma1PtSnp(),
+    GetSigma1PtTgl(),
+    GetSigma1Pt2()
+  }; 
+
+
+  Double_t cs=TMath::Cos(GetAlpha()), sn=TMath::Sin(GetAlpha());
+  Double_t xv = xyz[0]*cs + xyz[1]*sn; // vertex
+  Double_t yv =-xyz[0]*sn + xyz[1]*cs; // in the
+  Double_t zv = xyz[2];                // local frame
+
+  Double_t dx = x - xv, dy = par[0] - yv, dz = par[1] - zv;
+  Double_t r2=dx*dx + dy*dy;
+  Double_t p2=(1.+ GetTgl()*GetTgl())/(GetSigned1Pt()*GetSigned1Pt());
+  if (GetMass()<0) p2 *= 4; // q=2
   Double_t beta2=p2/(p2 + GetMass()*GetMass());
   x0*=TMath::Sqrt((1.+ GetTgl()*GetTgl())/(1.- GetSnp()*GetSnp()));
   Double_t theta2=14.1*14.1/(beta2*p2*1e6)*x0;
   //Double_t theta2=1.0259e-6*14*14/28/(beta2*p2)*x0*9.36*2.33;
+
+  Double_t bz=GetBz();
+  Double_t cnv=bz*kB2C;
+  Double_t curv=GetC(bz);
   {
-  Double_t dummy=4/r2-fP4*fP4;
-  if (dummy < 0) return 0;
-  Double_t parp=0.5*(fP4*fX + dy*TMath::Sqrt(dummy));
-  Double_t sigma2p = theta2*(1.- GetSnp()*GetSnp())*(1. + GetTgl()*GetTgl());
-  sigma2p += fC00/r2*(1.- dy*dy/r2)*(1.- dy*dy/r2);
-  sigma2p += ers[1]*ers[1]/r2;
-  sigma2p += 0.25*fC44*fX*fX;
-  Double_t eps2p=sigma2p/(fC22+sigma2p);
-  fP0 += fC20/(fC22+sigma2p)*(parp-fP2);
-  fP2 = eps2p*fP2 + (1-eps2p)*parp;
-  fC22 *= eps2p;
-  fC20 *= eps2p;
+    Double_t dummy = 4/r2 - curv*curv;
+    if (dummy < 0) return kFALSE;
+    Double_t parp = 0.5*(curv*dx + dy*TMath::Sqrt(dummy));
+    Double_t sigma2p = theta2*(1.-GetSnp())*(1.+GetSnp())*(1. + GetTgl()*GetTgl());
+    Double_t ovSqr2 = 1./TMath::Sqrt(r2);
+    Double_t tfact = ovSqr2*(1.-dy*ovSqr2)*(1.+dy*ovSqr2);
+    sigma2p += cov[0]*tfact*tfact;
+    sigma2p += ers[1]*ers[1]/r2;
+    sigma2p += 0.25*cov[14]*cnv*cnv*dx*dx;
+    Double_t eps2p=sigma2p/(cov[5] + sigma2p);
+    par[0] += cov[3]/(cov[5] + sigma2p)*(parp - GetSnp());
+    par[2]  = eps2p*GetSnp() + (1 - eps2p)*parp;
+    cov[5] *= eps2p;
+    cov[3] *= eps2p;
   }
   {
-  Double_t parl=0.5*fP4*dz/TMath::ASin(0.5*fP4*TMath::Sqrt(r2));
-  Double_t sigma2l=theta2;
-  sigma2l += fC11/r2+fC00*dy*dy*dz*dz/(r2*r2*r2);
-  sigma2l += ers[2]*ers[2]/r2;
-  Double_t eps2l=sigma2l/(fC33+sigma2l);
-  fP1 += fC31/(fC33+sigma2l)*(parl-fP3);
-  fP4 += fC43/(fC33+sigma2l)*(parl-fP3);
-  fP3 = eps2l*fP3 + (1-eps2l)*parl;
-  fC33 *= eps2l; fC43 *= eps2l; 
-  fC31 *= eps2l; 
-  }
-  if (!Invariant()) return 0;
-  return 1;
-} 
+    Double_t parl=0.5*curv*dz/TMath::ASin(0.5*curv*TMath::Sqrt(r2));
+    Double_t sigma2l=theta2;
+    sigma2l += cov[2]/r2 + cov[0]*dy*dy*dz*dz/(r2*r2*r2);
+    sigma2l += ers[2]*ers[2]/r2;
+    Double_t eps2l = sigma2l/(cov[9] + sigma2l);
+    par[1] += cov[7 ]/(cov[9] + sigma2l)*(parl - par[3]);
+    par[4] += cov[13]/(cov[9] + sigma2l)*(parl - par[3]);
+    par[3]  = eps2l*par[3] + (1-eps2l)*parl;
+    cov[9] *= eps2l; 
+    cov[13]*= eps2l; 
+    cov[7] *= eps2l; 
+  }
 
-void AliITStrackV2::ResetCovariance() {
-  //------------------------------------------------------------------
-  //This function makes a track forget its history :)  
-  //------------------------------------------------------------------
+  Set(x,alpha,par,cov);
 
-  fC00*=10.;
-  fC10=0.;  fC11*=10.;
-  fC20=0.;  fC21=0.;  fC22*=10.;
-  fC30=0.;  fC31=0.;  fC32=0.;  fC33*=10.;
-  fC40=0.;  fC41=0.;  fC42=0.;  fC43=0.;  fC44*=10.;
+  if (!Invariant()) return kFALSE;
 
+  return kTRUE;
 }
 
-void AliITStrackV2::CookdEdx(Double_t low, Double_t up) {
+void AliITStrackV2::CookdEdx(Double_t /*low*/, Double_t /*up*/) {
   //-----------------------------------------------------------------
   // This function calculates dE/dX within the "low" and "up" cuts.
   // Origin: Boris Batyunya, JINR, Boris.Batiounia@cern.ch 
+  // Updated: F. Prino 8-June-2009
   //-----------------------------------------------------------------
-  // The clusters order is: SSD-2, SSD-1, SDD-2, SDD-1, SPD-2, SPD-1
+  // The cluster order is: SDD-1, SDD-2, SSD-1, SSD-2
 
-  Int_t i;
   Int_t nc=0;
-  for (i=0; i<GetNumberOfClusters(); i++) {
-    Int_t idx=GetClusterIndex(i);
-    idx=(idx&0xf0000000)>>28;
-    if (idx>1) nc++; // Take only SSD and SDD
+  Float_t dedx[4];
+  for (Int_t il=0; il<4; il++) { // count good (>0) dE/dx values
+    if(fdEdxSample[il]>0.){
+      dedx[nc]= fdEdxSample[il];
+      nc++;
+    }
+  }
+  if(nc<1){
+    SetdEdx(0.);
+    return;
   }
 
-  Int_t swap;//stupid sorting
+  Int_t swap; // sort in ascending order
   do {
     swap=0;
-    for (i=0; i<nc-1; i++) {
-      if (fdEdxSample[i]<=fdEdxSample[i+1]) continue;
-      Float_t tmp=fdEdxSample[i];
-      fdEdxSample[i]=fdEdxSample[i+1]; fdEdxSample[i+1]=tmp;
+    for (Int_t i=0; i<nc-1; i++) {
+      if (dedx[i]<=dedx[i+1]) continue;
+      Float_t tmp=dedx[i];
+      dedx[i]=dedx[i+1]; 
+      dedx[i+1]=tmp;
       swap++;
     }
   } while (swap);
 
-  Int_t nl=Int_t(low*nc), nu=Int_t(up*nc); //b.b. to take two lowest dEdX
-                                           // values from four ones choose
-                                           // nu=2
-  Float_t dedx=0;
-  for (i=nl; i<nu; i++) dedx += fdEdxSample[i];
-  if (nu-nl>0) dedx /= (nu-nl);
 
-  SetdEdx(dedx);
+  Double_t sumamp=0,sumweight=0;
+  Double_t weight[4]={1.,1.,0.,0.};
+  if(nc==3) weight[1]=0.5;
+  else if(nc<3) weight[1]=0.;
+  for (Int_t i=0; i<nc; i++) {
+    sumamp+= dedx[i]*weight[i];
+    sumweight+=weight[i];
+  }
+  SetdEdx(sumamp/sumweight);
 }
 
-Double_t AliITStrackV2::
-PropagateToDCA(AliKalmanTrack *p, Double_t d, Double_t x0) {
-  //--------------------------------------------------------------
-  // Propagates this track and the argument track to the position of the
-  // distance of closest approach. 
-  // Returns the (weighed !) distance of closest approach.
-  //--------------------------------------------------------------
-  Double_t xthis, xp, dca;
-  {
-  //Temporary solution
-  Double_t b=1./GetLocalConvConst()/kB2C;
-  AliExternalTrackParam dummy1(*this), dummy2(*p); 
-  dca=dummy1.GetDCA(&dummy2,b,xthis,xp);
-  }
-  if (!PropagateTo(xthis,d,x0)) {
-    //AliWarning(" propagation failed !");
-    return 1e+33;
-  }  
+//____________________________________________________________________________
+Bool_t AliITStrackV2::
+GetPhiZat(Double_t r, Double_t &phi, Double_t &z) const {
+  //------------------------------------------------------------------
+  // This function returns the global cylindrical (phi,z) of the track 
+  // position estimated at the radius r. 
+  // The track curvature is neglected.
+  //------------------------------------------------------------------
+  Double_t d=GetD(0.,0.);
+  if (TMath::Abs(d) > r) {
+    if (r>1e-1) return kFALSE;
+    r = TMath::Abs(d);
+  }
 
-  if (!p->PropagateTo(xp,d,x0)) {
-    //AliWarning(" propagation failed !";
-    return 1e+33;
-  }  
+  Double_t rcurr=TMath::Sqrt(GetX()*GetX() + GetY()*GetY());
+  if (TMath::Abs(d) > rcurr) return kFALSE;
+  Double_t globXYZcurr[3]; GetXYZ(globXYZcurr); 
+  Double_t phicurr=TMath::ATan2(globXYZcurr[1],globXYZcurr[0]);
 
-  return dca;
-} 
+  if (GetX()>=0.) {
+    phi=phicurr+TMath::ASin(d/r)-TMath::ASin(d/rcurr);
+  } else {
+    phi=phicurr+TMath::ASin(d/r)+TMath::ASin(d/rcurr)-TMath::Pi();
+  }
+
+  // return a phi in [0,2pi[ 
+  if (phi<0.) phi+=2.*TMath::Pi();
+  else if (phi>=2.*TMath::Pi()) phi-=2.*TMath::Pi();
+  z=GetZ()+GetTgl()*(TMath::Sqrt((r-d)*(r+d))-TMath::Sqrt((rcurr-d)*(rcurr+d)));
+  return kTRUE;
+}
+//____________________________________________________________________________
+Bool_t AliITStrackV2::
+GetLocalXat(Double_t r,Double_t &xloc) const {
+  //------------------------------------------------------------------
+  // This function returns the local x of the track 
+  // position estimated at the radius r. 
+  // The track curvature is neglected.
+  //------------------------------------------------------------------
+  Double_t d=GetD(0.,0.);
+  if (TMath::Abs(d) > r) { 
+    if (r>1e-1) return kFALSE; 
+    r = TMath::Abs(d); 
+  } 
+
+  Double_t rcurr=TMath::Sqrt(GetX()*GetX() + GetY()*GetY());
+  Double_t globXYZcurr[3]; GetXYZ(globXYZcurr); 
+  Double_t phicurr=TMath::ATan2(globXYZcurr[1],globXYZcurr[0]);
+  Double_t phi;
+  if (GetX()>=0.) {
+    phi=phicurr+TMath::ASin(d/r)-TMath::ASin(d/rcurr);
+  } else {
+    phi=phicurr+TMath::ASin(d/r)+TMath::ASin(d/rcurr)-TMath::Pi();
+  }
+
+  xloc=r*(TMath::Cos(phi)*TMath::Cos(GetAlpha())
+         +TMath::Sin(phi)*TMath::Sin(GetAlpha())); 
+
+  return kTRUE;
+}
+
+//____________________________________________________________________________
+Bool_t AliITStrackV2::ImproveKalman(Double_t xyz[3],Double_t ers[3], const Double_t* xlMS, const Double_t* x2X0MS, Int_t nMS)
+{
+  // Substitute the state of the track (p_{k|k},C_{k|k}) at the k-th measumerent by its
+  // smoothed value from the k-th measurement + measurement at the vertex.
+  // Account for the MS on nMS layers at x-postions xlMS with x/x0 = x2X0MS
+  // p_{k|kv} = p_{k|k} + C_{k|k}*D^Tr_{k+1} B^{-1}_{k+1} ( vtx - D_{k+1}*p_{k|k})
+  // C_{k|kv} = C_{k|k}*( I - D^Tr_{k+1} B^{-1}_{k+1} D_{k+1} C^Tr_{k|k})
+  // 
+  // where D_{k} = H_{k} F_{k} with H being the matrix converting the tracks parameters
+  // to measurements m_{k} = H_{k} p_{k} and F_{k} the matrix propagating the track between the
+  // the point k-1 and k:  p_{k|k-1} = F_{k} p_{k-1|k-1}
+  //
+  // B_{k+1} = V_{k+1} + H_{k+1} C_{k+1|k} H^Tr_{k+1} with V_{k+1} being the error of the measurment
+  // at point k+1 (i.e. vertex), and C_{k+1|k} - error matrix extrapolated from k-th measurement to
+  // k+1 (vtx) and accounting for the MS inbetween
+  //
+  // H = {{1,0,0,0,0},{0,1,0,0,0}}
+  //
+  double covc[15], *cori = (double*) GetCovariance(),par[5] = {GetY(),GetZ(),GetSnp(),GetTgl(),GetSigned1Pt()},
+    &c00=cori[0],
+    &c01=cori[1],&c11=cori[2],
+    &c02=cori[3],&c12=cori[4],&c22=cori[5],
+    &c03=cori[6],&c13=cori[7],&c23=cori[8],&c33=cori[9],
+    &c04=cori[10],&c14=cori[11],&c24=cori[12],&c34=cori[13],&c44=cori[14],
+    // for smoothed cov matrix 
+    &cov00=covc[0],
+    &cov01=covc[1],&cov11=covc[2],
+    &cov02=covc[3],&cov12=covc[4],&cov22=covc[5],
+    &cov03=covc[6],&cov13=covc[7],&cov23=covc[8],&cov33=covc[9],
+    &cov04=covc[10],&cov14=covc[11],&cov24=covc[12],&cov34=covc[13],&cov44=covc[14];
+  //
+  double x = GetX(), alpha = GetAlpha();
+  // vertex in the track frame
+  double cs=TMath::Cos(alpha), sn=TMath::Sin(alpha);
+  double xv = xyz[0]*cs + xyz[1]*sn, yv =-xyz[0]*sn + xyz[1]*cs, zv = xyz[2];
+  double dx = xv - GetX();
+  if (TMath::Abs(dx)<=kAlmost0)  return kTRUE;
+  //
+  double cnv=GetBz()*kB2C, x2r=cnv*par[4]*dx, f1=par[2], f2=f1+x2r;
+  if (TMath::Abs(f1) >= kAlmost1 || TMath::Abs(f2) >= kAlmost1) {
+    AliInfo(Form("Fail: %+e %+e",f1,f2));
+    return kFALSE;
+  }
+  double r1=TMath::Sqrt((1.-f1)*(1.+f1)), r2=TMath::Sqrt((1.-f2)*(1.+f2)), dx2r=dx/(r1+r2);
+  // elements of matrix F_{k+1} (1s on diagonal)
+  double f02 = 2*dx2r, f04 = cnv*dx*dx2r, f13/*, f24 = cnv*dx*/;
+  if (TMath::Abs(x2r)<0.05) f13 = dx*r2+f2*(f1+f2)*dx2r; // see AliExternalTrackParam::PropagateTo
+  else {
+    double dy2dx = (f1+f2)/(r1+r2);
+    f13 = 2*TMath::ASin(0.5*TMath::Sqrt(1+dy2dx*dy2dx)*x2r)/(cnv*par[4]);
+  }  
+  // elements of matrix D_{k+1} = H_{k+1} * F_{k+1}
+  // double d00 = 1., d11 = 1.;
+  double &d02 = f02,  &d04 = f04, &d13 = f13;
+  //
+  // elements of matrix DC = D_{k+1}*C_{kk}^T
+  double dc00 = c00+c02*d02+c04*d04,   dc10 = c01+c03*d13;
+  double dc01 = c01+c12*d02+c14*d04,   dc11 = c11+c13*d13;
+  double dc02 = c02+c22*d02+c24*d04,   dc12 = c12+c23*d13;
+  double dc03 = c03+c23*d02+c34*d04,   dc13 = c13+c33*d13;
+  double dc04 = c04+c24*d02+c44*d04,   dc14 = c14+c34*d13;
+  //
+  // difference between the vertex and the the track extrapolated to vertex
+  yv -= par[0] + par[2]*d02 + par[4]*d04;
+  zv -= par[1] + par[3]*d13;
+  //
+  // y,z part of the cov.matrix extrapolated to vtx (w/o MS contribution)
+  // C_{k+1,k} = H F_{k+1} C_{k,k} F^Tr_{k+1} H^Tr = D C D^Tr
+  double cv00 = dc00+dc02*d02+dc04*d04, cv01 = dc01+dc03*d13, cv11 = dc11+dc13*d13;
+  //
+  // add MS contribution layer by layer
+  double xCurr = x;
+  double p2Curr = par[2];
+  //
+  // precalculated factors of MS contribution matrix:
+  double ms22t = (1. + par[3]*par[3]);
+  double ms33t = ms22t*ms22t;
+  double p34 = par[3]*par[4];
+  double ms34t = p34*ms22t;
+  double ms44t = p34*p34;
+  //
+  double p2=(1.+ par[3]*par[3])/(par[4]*par[4]);
+  if (GetMass()<0) p2 *= 4; // q=2
+  double beta2 = p2/(p2+GetMass()*GetMass());
+  double theta2t = 14.1*14.1/(beta2*p2*1e6) * (1. + par[3]*par[3]);
+  //
+  // account for the MS in the layers between the last measurement and the vertex
+  for (int il=0;il<nMS;il++) {
+    double dfx = xlMS[il] - xCurr;
+    xCurr = xlMS[il];
+    p2Curr += dfx*cnv*par[4];   // p2 at the scattering layer
+    double dxL=xv - xCurr;    // distance from scatering layer to vtx
+    double x2rL=cnv*par[4]*dxL, f1L=p2Curr, f2L=f1L+x2rL;
+    if (TMath::Abs(f1L) >= kAlmost1 || TMath::Abs(f2L) >= kAlmost1) {
+      AliInfo(Form("FailMS at step %d of %d: dfx:%e dxL:%e %e %e",il,nMS,dfx,dxL,f1L,f2L));
+      return kFALSE;
+    }
+    double r1L=TMath::Sqrt((1.-f1L)*(1.+f1L)), r2L=TMath::Sqrt((1.-f2L)*(1.+f2L)), dx2rL=dxL/(r1L+r2L);
+    // elements of matrix for propagation from scatering layer to vertex
+    double f02L = 2*dx2rL, f04L = cnv*dxL*dx2rL, f13L/*, f24L = cnv*dxL*/;
+    if (TMath::Abs(x2rL)<0.05) f13L = dxL*r2L+f2L*(f1L+f2L)*dx2rL; // see AliExternalTrackParam::PropagateTo
+    else {
+      double dy2dxL = (f1L+f2L)/(r1L+r2L);
+      f13L = 2*TMath::ASin(0.5*TMath::Sqrt(1+dy2dxL*dy2dxL)*x2rL)/(cnv*par[4]);
+    }
+    // MS contribution matrix:
+    double theta2 = theta2t*TMath::Abs(x2X0MS[il]);
+    double ms22 = theta2*(1.-p2Curr)*(1.+p2Curr)*ms22t;
+    double ms33 = theta2*ms33t;
+    double ms34 = theta2*ms34t;
+    double ms44 = theta2*ms44t;
+    //
+    // add  H F MS F^Tr H^Tr to cv
+    cv00 += f02L*f02L*ms22 + f04L*f04L*ms44;
+    cv01 += f04L*f13L*ms34;
+    cv11 += f13L*f13L*ms33;
+  }
+  //
+  // inverse of matrix B
+  double b11 = ers[1]*ers[1] + cv00;
+  double b00 = ers[2]*ers[2] + cv11;
+  double det = b11*b00 - cv01*cv01;
+  if (TMath::Abs(det)<kAlmost0) {
+    AliInfo(Form("Fail on det %e: %e %e %e",det,cv00,cv11,cv01));
+    return kFALSE;
+  }
+  det = 1./det;
+  b00 *= det; b11 *= det; 
+  double b01 = -cv01*det;
+  //
+  // elements of matrix DC^Tr * B^-1
+  double dcb00 = b00*dc00+b01*dc10, dcb01 = b01*dc00+b11*dc10;
+  double dcb10 = b00*dc01+b01*dc11, dcb11 = b01*dc01+b11*dc11;
+  double dcb20 = b00*dc02+b01*dc12, dcb21 = b01*dc02+b11*dc12;
+  double dcb30 = b00*dc03+b01*dc13, dcb31 = b01*dc03+b11*dc13;
+  double dcb40 = b00*dc04+b01*dc14, dcb41 = b01*dc04+b11*dc14;
+  //
+  // p_{k|k+1} = p_{k|k} +  C_{k|k}*D^Tr_{k+1} B^{-1}_{k+1} ( vtx - D_{k+1}*p_{k|k})
+  par[0] += dcb00*yv + dcb01*zv;
+  par[1] += dcb10*yv + dcb11*zv;
+  par[2] += dcb20*yv + dcb21*zv;
+  par[3] += dcb30*yv + dcb31*zv;
+  par[4] += dcb40*yv + dcb41*zv;
+  //
+  // C_{k|kv} = C_{k|k} - C_{k|k} D^Tr_{k+1} B^{-1}_{k+1} D_{k+1} C^Tr_{k|k})
+  //
+  cov00 = c00 - (dc00*dcb00 + dc10*dcb01);
+  cov01 = c01 - (dc01*dcb00 + dc11*dcb01);
+  cov02 = c02 - (dc02*dcb00 + dc12*dcb01);
+  cov03 = c03 - (dc03*dcb00 + dc13*dcb01);
+  cov04 = c04 - (dc04*dcb00 + dc14*dcb01);
+  //
+  cov11 = c11 - (dc01*dcb10 + dc11*dcb11);
+  cov12 = c12 - (dc02*dcb10 + dc12*dcb11);
+  cov13 = c13 - (dc03*dcb10 + dc13*dcb11);
+  cov14 = c14 - (dc04*dcb10 + dc14*dcb11);
+  //
+  cov22 = c22 - (dc02*dcb20 + dc12*dcb21);
+  cov23 = c23 - (dc03*dcb20 + dc13*dcb21);
+  cov24 = c24 - (dc04*dcb20 + dc14*dcb21);
+  //
+  cov33 = c33 - (dc03*dcb30 + dc13*dcb31);
+  cov34 = c34 - (dc04*dcb30 + dc14*dcb31);
+  //
+  cov44 = c44 - (dc04*dcb40 + dc14*dcb41);
+  //
+  Set(x,alpha,par,covc);
+  if (!Invariant()) {
+    AliInfo(Form("Fail on Invariant, X=%e",GetX()));
+    return kFALSE;
+  }
+  return kTRUE;
+  //
+}