]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliExternalTrackParam.cxx
Adding directory with the production requests
[u/mrichter/AliRoot.git] / STEER / AliExternalTrackParam.cxx
index c0a030925d0acc0033548056d0ea4b6fe5815e67..14ff04b054c48f72a8046f2a84a8ee7a1fafa68d 100644 (file)
 // Origin: I.Belikov, CERN, Jouri.Belikov@cern.ch                            //
 ///////////////////////////////////////////////////////////////////////////////
 #include <TMatrixDSym.h>
+#include <TPolyMarker3D.h>
+#include <TVector3.h>
+
 #include "AliExternalTrackParam.h"
-#include "AliESDVertex.h"
+#include "AliVVertex.h"
 #include "AliLog.h"
 
 ClassImp(AliExternalTrackParam)
@@ -36,7 +39,7 @@ Double32_t AliExternalTrackParam::fgMostProbablePt=kMostProbablePt;
  
 //_____________________________________________________________________________
 AliExternalTrackParam::AliExternalTrackParam() :
-  AliVParticle(),
+  AliVTrack(),
   fX(0),
   fAlpha(0)
 {
@@ -49,7 +52,7 @@ AliExternalTrackParam::AliExternalTrackParam() :
 
 //_____________________________________________________________________________
 AliExternalTrackParam::AliExternalTrackParam(const AliExternalTrackParam &track):
-  AliVParticle(track),
+  AliVTrack(track),
   fX(track.fX),
   fAlpha(track.fAlpha)
 {
@@ -68,7 +71,7 @@ AliExternalTrackParam& AliExternalTrackParam::operator=(const AliExternalTrackPa
   //
   
   if (this!=&trkPar) {
-    AliVParticle::operator=(trkPar);
+    AliVTrack::operator=(trkPar);
     fX = trkPar.fX;
     fAlpha = trkPar.fAlpha;
 
@@ -83,7 +86,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)
 {
@@ -95,15 +98,140 @@ AliExternalTrackParam::AliExternalTrackParam(Double_t x, Double_t alpha,
 }
 
 //_____________________________________________________________________________
-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.)
+{
+  //
+  // Constructor from virtual track,
+  // This is not a copy contructor !
+  //
+
+  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.
   //
-  //  Sets the parameters
+  // For global radial position inside the beam pipe, alpha is the
+  // azimuthal angle of the momentum projected on (x,y).
   //
-  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];
+  // For global radial position outside the beam pipe, 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];  
+  if (radPos2 < 3.*3.) { // inside beam pipe
+     fAlpha = TMath::ATan2(pxpypz[1],pxpypz[0]);
+  } else { // outside beam pipe
+     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));
+
+  return;
 }
 
 //_____________________________________________________________________________
@@ -116,6 +244,20 @@ 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];
+}
+
+
 Double_t AliExternalTrackParam::GetP() const {
   //---------------------------------------------------------------------
   // This function returns the track momentum
@@ -201,8 +343,8 @@ Double_t AliExternalTrackParam::GetLinearD(Double_t xv,Double_t yv) const {
 }
 
 Bool_t AliExternalTrackParam::CorrectForMeanMaterial
-(Double_t xOverX0,  Double_t xTimesRho, Double_t mass, 
-Double_t (*Bethe)(Double_t)) {
+(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.
@@ -218,34 +360,56 @@ Double_t (*Bethe)(Double_t)) {
   Double_t &fC43=fC[13];
   Double_t &fC44=fC[14];
 
+  //Apply angle correction, if requested
+  if(anglecorr) {
+    Double_t angle=TMath::Sqrt((1.+ fP3*fP3)/(1.- fP2*fP2));
+    xOverX0 *=angle;
+    xTimesRho *=angle;
+  } 
+
   Double_t p=GetP();
   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*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=Bethe(p/mass)*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 (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;
+
   return kTRUE;
 }
 
@@ -276,43 +440,142 @@ Bool_t AliExternalTrackParam::CorrectForMaterial
   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;
-     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*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(beta2)*d;
+     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!
-     fP4*=(1.- e/p2*dE);
+     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)); 
-     fC44+=((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
+     cC44 += ((sigmadE*e/p2*fP4)*(sigmadE*e/p2*fP4)); 
  
   }
 
+  fC22 += cC22;
+  fC33 += cC33;
+  fC43 += cC43;
+  fC44 += cC44;
+  fP4  *= cP4;
+
   return kTRUE;
 }
 
-Double_t ApproximateBetheBloch(Double_t beta2) {
+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
+  //
+
+  Double_t beta = bg/TMath::Sqrt(1.+ bg*bg);
+
+  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 mK*mZA*(1+bg2)/bg2*
+         (0.5*TMath::Log(2*me*bg2*maxT/(mI*mI)) - bg2/(1+bg2) - d2);
+}
+
+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]
   //------------------------------------------------------------------
-  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 0.153e-3/beta2*(log(5940*beta2/(1-beta2)) - beta2);
+  return BetheBlochGeant(bg);
+}
+
+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]
+  //------------------------------------------------------------------
+
+  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) {
@@ -444,6 +707,33 @@ Bool_t AliExternalTrackParam::PropagateTo(Double_t xk, Double_t b) {
   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;
+}
+
+
 void AliExternalTrackParam::Propagate(Double_t len, Double_t x[3],
 Double_t p[3], Double_t bz) const {
   //+++++++++++++++++++++++++++++++++++++++++    
@@ -456,7 +746,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;   
@@ -640,6 +930,40 @@ PropagateTo(Double_t p[3],Double_t covyz[3],Double_t covxyz[3],Double_t bz) {
   return kTRUE;  
 }
 
+Double_t *AliExternalTrackParam::GetResiduals(
+Double_t *p,Double_t *cov,Bool_t updated) const {
+  //------------------------------------------------------------------
+  // Returns the track residuals with the space point "p" having
+  // the covariance matrix "cov".
+  // If "updated" is kTRUE, the track parameters expected to be updated,
+  // otherwise they must be predicted.  
+  //------------------------------------------------------------------
+  static Double_t res[2];
+
+  Double_t r00=cov[0], r01=cov[1], r11=cov[2];
+  if (updated) {
+     r00-=fC[0]; r01-=fC[1]; r11-=fC[2];
+  } else {
+     r00+=fC[0]; r01+=fC[1]; r11+=fC[2];
+  }
+  Double_t det=r00*r11 - r01*r01;
+
+  if (TMath::Abs(det) < kAlmost0) return 0;
+
+  Double_t tmp=r00; r00=r11/det; r11=tmp/det;
+
+  if (r00 < 0.) return 0;
+  if (r11 < 0.) return 0;
+
+  Double_t dy = fP[0] - p[0];
+  Double_t dz = fP[1] - p[1];
+
+  res[0]=dy*TMath::Sqrt(r00);
+  res[1]=dz*TMath::Sqrt(r11);
+
+  return res;
+}
+
 Bool_t AliExternalTrackParam::Update(Double_t p[2], Double_t cov[3]) {
   //------------------------------------------------------------------
   // Update the track parameters with the space point "p" having
@@ -730,8 +1054,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];
@@ -751,8 +1079,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);
@@ -798,10 +1124,10 @@ Double_t b, Double_t &xthis, Double_t &xp) const {
      if (TMath::Abs(dt1)/(TMath::Abs(t1)+1.e-3) < 1.e-4)
      if (TMath::Abs(dt2)/(TMath::Abs(t2)+1.e-3) < 1.e-4) {
         if ((gt1*gt1+gt2*gt2) > 1.e-4/dy2/dy2) 
-         AliWarning(" stopped at not a stationary point !");
+         AliDebug(1," stopped at not a stationary point !");
         Double_t lmb=h11+h22; lmb=lmb-TMath::Sqrt(lmb*lmb-4*det);
         if (lmb < 0.) 
-         AliWarning(" stopped at not a minimum !");
+         AliDebug(1," stopped at not a minimum !");
         break;
      }
 
@@ -814,7 +1140,7 @@ Double_t b, Double_t &xthis, Double_t &xp) const {
        if (dd<dm) break;
         dt1*=0.5; dt2*=0.5;
         if (div>512) {
-           AliWarning(" overshoot !"); break;
+         AliDebug(1," overshoot !"); break;
         }   
      }
      dm=dd;
@@ -824,7 +1150,7 @@ Double_t b, Double_t &xthis, Double_t &xp) const {
 
   }
 
-  if (max<=0) AliWarning(" too many iterations !");
+  if (max<=0) AliDebug(1," too many iterations !");
 
   Double_t cs=TMath::Cos(GetAlpha());
   Double_t sn=TMath::Sin(GetAlpha());
@@ -861,11 +1187,10 @@ PropagateToDCA(AliExternalTrackParam *p, Double_t b) {
 }
 
 
-
-
-Bool_t AliExternalTrackParam::PropagateToDCA(const AliESDVertex *vtx, Double_t b, Double_t maxd){
+Bool_t AliExternalTrackParam::PropagateToDCA(const AliVVertex *vtx, 
+Double_t b, Double_t maxd, Double_t dz[2], Double_t covar[3]) {
   //
-  // Try to relate this track to the vertex "vtx", 
+  // Propagate this track to the DCA to vertex "vtx", 
   // if the (rough) transverse impact parameter is not bigger then "maxd". 
   //            Magnetic field is "b" (kG).
   //
@@ -878,8 +1203,8 @@ Bool_t AliExternalTrackParam::PropagateToDCA(const AliESDVertex *vtx, Double_t b
   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;
+  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
@@ -887,65 +1212,37 @@ Bool_t AliExternalTrackParam::PropagateToDCA(const AliESDVertex *vtx, Double_t b
   if (d > maxd) return kFALSE; 
 
   //Propagate to the DCA
-  Double_t crv=0.299792458e-3*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);
+  if (TMath::Abs(tgfv)>0.) cs = sn/tgfv;
+  else cs=1.;
 
   x = xv*cs + yv*sn;
   yv=-xv*sn + yv*cs; xv=x;
 
   if (!Propagate(alpha+TMath::ASin(sn),xv,b)) return kFALSE;
-  return kTRUE;
-}
-
-
 
+  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);
 
-Bool_t Local2GlobalMomentum(Double_t p[3],Double_t alpha) {
-  //----------------------------------------------------------------
-  // This function performs local->global transformation of the
-  // track momentum.
-  // When called, the arguments are:
-  //    p[0] = 1/pt of the track;
-  //    p[1] = sine of local azim. angle of the track momentum;
-  //    p[2] = tangent of the track momentum dip angle;
-  //   alpha - rotation angle. 
-  // The result is returned as:
-  //    p[0] = px
-  //    p[1] = py
-  //    p[2] = pz
-  // Results for (nearly) straight tracks are meaningless !
-  //----------------------------------------------------------------
-  if (TMath::Abs(p[0])<=kAlmost0) return kFALSE;
-  if (TMath::Abs(p[1])> kAlmost1) return kFALSE;
-
-  Double_t pt=1./TMath::Abs(p[0]);
-  Double_t cs=TMath::Cos(alpha), sn=TMath::Sin(alpha);
-  Double_t r=TMath::Sqrt(1 - p[1]*p[1]);
-  p[0]=pt*(r*cs - p[1]*sn); p[1]=pt*(p[1]*cs + r*sn); p[2]=pt*p[2];
+  //***** 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;
 }
 
-Bool_t Local2GlobalPosition(Double_t r[3],Double_t alpha) {
-  //----------------------------------------------------------------
-  // This function performs local->global transformation of the
-  // track position.
-  // When called, the arguments are:
-  //    r[0] = local x
-  //    r[1] = local y
-  //    r[2] = local z
-  //   alpha - rotation angle. 
-  // The result is returned as:
-  //    r[0] = global x
-  //    r[1] = global y
-  //    r[2] = global z
-  //----------------------------------------------------------------
-  Double_t cs=TMath::Cos(alpha), sn=TMath::Sin(alpha), x=r[0];
-  r[0]=x*cs - r[1]*sn; r[1]=x*sn + r[1]*cs;
-
-  return kTRUE;
-}
 
 void AliExternalTrackParam::GetDirection(Double_t d[3]) const {
   //----------------------------------------------------------------
@@ -961,7 +1258,7 @@ void AliExternalTrackParam::GetDirection(Double_t d[3]) const {
   d[2]=fP[3]/norm;
 }
 
-Bool_t AliExternalTrackParam::GetPxPyPz(Double_t *p) const {
+Bool_t AliExternalTrackParam::GetPxPyPz(Double_t p[3]) const {
   //---------------------------------------------------------------------
   // This function returns the global track momentum components
   // Results for (nearly) straight tracks are meaningless !
@@ -1006,10 +1303,43 @@ Double_t AliExternalTrackParam::Pz() const {
   return p[2];
 }
 
+Double_t AliExternalTrackParam::Xv() const {
+  //---------------------------------------------------------------------
+  // Returns x-component of first track point
+  //---------------------------------------------------------------------
+
+  Double_t r[3]={0.,0.,0.};
+  GetXYZ(r);
+
+  return r[0];
+}
+
+Double_t AliExternalTrackParam::Yv() const {
+  //---------------------------------------------------------------------
+  // Returns y-component of first track point
+  //---------------------------------------------------------------------
+
+  Double_t r[3]={0.,0.,0.};
+  GetXYZ(r);
+
+  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
 
-  return TMath::ATan2(Pt(), Pz());
+  return 0.5*TMath::Pi() - TMath::ATan(fP[3]);
 }
 
 Double_t AliExternalTrackParam::Phi() const {
@@ -1153,7 +1483,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;
 }
@@ -1168,12 +1498,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;
 }
@@ -1192,10 +1522,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);
 }
 
@@ -1226,3 +1557,85 @@ Double_t AliExternalTrackParam::GetSnpAt(Double_t x,Double_t b) const {
   Double_t res = fP[2]+dx*crv;
   return res;
 }
+
+Bool_t AliExternalTrackParam::GetDistance(AliExternalTrackParam *param2, Double_t x, Double_t dist[3], Double_t bz){
+  //------------------------------------------------------------------------
+  // Get the distance between two tracks at the local position x 
+  // working in the local frame of this track.
+  // Origin :   Marian.Ivanov@cern.ch
+  //-----------------------------------------------------------------------
+  Double_t xyz[3];
+  Double_t xyz2[3];
+  xyz[0]=x;
+  if (!GetYAt(x,bz,xyz[1])) return kFALSE;
+  if (!GetZAt(x,bz,xyz[2])) return kFALSE;
+  //  
+  //
+  if (TMath::Abs(GetAlpha()-param2->GetAlpha())<kAlmost0){
+    xyz2[0]=x;
+    if (!param2->GetYAt(x,bz,xyz2[1])) return kFALSE;
+    if (!param2->GetZAt(x,bz,xyz2[2])) return kFALSE;
+  }else{
+    //
+    Double_t xyz1[3];
+    Double_t dfi = param2->GetAlpha()-GetAlpha();
+    Double_t ca = TMath::Cos(dfi), sa = TMath::Sin(dfi);
+    xyz2[0] =  xyz[0]*ca+xyz[1]*sa;
+    xyz2[1] = -xyz[0]*sa+xyz[1]*ca;
+    //
+    xyz1[0]=xyz2[0];
+    if (!param2->GetYAt(xyz2[0],bz,xyz1[1])) return kFALSE;
+    if (!param2->GetZAt(xyz2[0],bz,xyz1[2])) return kFALSE;
+    //
+    xyz2[0] =  xyz1[0]*ca-xyz1[1]*sa;
+    xyz2[1] = +xyz1[0]*sa+xyz1[1]*ca;
+    xyz2[2] = xyz1[2];
+  }
+  dist[0] = xyz[0]-xyz2[0];
+  dist[1] = xyz[1]-xyz2[1];
+  dist[2] = xyz[2]-xyz2[2];
+
+  return kTRUE;
+}
+
+
+//
+// Draw functionality.
+// Origin: Marian Ivanov, Marian.Ivanov@cern.ch
+//
+
+void  AliExternalTrackParam::DrawTrack(Float_t magf, Float_t minR, Float_t maxR, Float_t stepR){
+  //
+  // Draw track line
+  //
+  if (minR>maxR) return ;
+  if (stepR<=0) return ;
+  Int_t npoints = TMath::Nint((maxR-minR)/stepR)+1;
+  if (npoints<1) return;
+  TPolyMarker3D *polymarker = new TPolyMarker3D(npoints);
+  FillPolymarker(polymarker, magf,minR,maxR,stepR);
+  polymarker->Draw();
+}
+
+//
+void AliExternalTrackParam::FillPolymarker(TPolyMarker3D *pol, Float_t magF, Float_t minR, Float_t maxR, Float_t stepR){
+  //
+  // Fill points in the polymarker
+  //
+  Int_t counter=0;
+  for (Double_t r=minR; r<maxR; r+=stepR){
+    Double_t point[3];
+    GetXYZAt(r,magF,point);
+    pol->SetPoint(counter,point[0],point[1], point[2]);
+    printf("xyz\t%f\t%f\t%f\n",point[0], point[1],point[2]);
+    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;
+}