]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDtrackV1.cxx
Modifications needed to use PID framework based mass during tracking and
[u/mrichter/AliRoot.git] / TRD / AliTRDtrackV1.cxx
index e35452ca3632711f867a8096142376fabae926d2..aceb18107cc6a117758f7af8e5ff655b1f7ca1f7 100644 (file)
@@ -1,3 +1,4 @@
+
 /**************************************************************************
  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
  *                                                                        *
 
 /* $Id$ */
 
+#include "TVectorT.h"
+#include "AliLog.h"
 #include "AliESDtrack.h"
+#include "AliTracker.h"
 
 #include "AliTRDtrackV1.h"
 #include "AliTRDcluster.h"
 #include "AliTRDcalibDB.h"
+#include "AliTRDReconstructor.h"
+#include "AliTRDPIDResponse.h"
 #include "AliTRDrecoParam.h"
+#include "AliTRDdEdxBaseUtils.h"
+#include "AliTRDdEdxCalibHistArray.h"
+#include "AliTRDdEdxCalibUtils.h"
+#include "AliTRDdEdxReconUtils.h"
 
 ClassImp(AliTRDtrackV1)
 
@@ -36,56 +46,140 @@ ClassImp(AliTRDtrackV1)
 ///////////////////////////////////////////////////////////////////////////////
 
 //_______________________________________________________________
-AliTRDtrackV1::AliTRDtrackV1() 
-  :AliTRDtrack()
+AliTRDtrackV1::AliTRDtrackV1() : AliKalmanTrack()
+  ,fStatus(0)
+  ,fESDid(0)
+  ,fDE(0.)
+  ,fTruncatedMean(0)
+  ,fNchamberdEdx(0)
+  ,fNclusterdEdx(0)
+  ,fNdEdxSlices(0)
+  ,fkReconstructor(NULL)
+  ,fBackupTrack(NULL)
+  ,fTrackLow(NULL)
+  ,fTrackHigh(NULL)
 {
   //
   // Default constructor
   //
-       
-       for(int ip=0; ip<6; ip++){
-               fTrackletIndex[ip] = -1;
-               fTracklet[ip].Reset();
-       }
+  //printf("AliTRDtrackV1::AliTRDtrackV1()\n");
+
+  for(int i =0; i<3; i++) fBudget[i] = 0.;
+  
+  Float_t pid = 1./AliPID::kSPECIES;
+  for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
+
+  for(int ip=0; ip<kNplane; ip++){
+    fTrackletIndex[ip] = -1;
+    fTracklet[ip]      = NULL;
+  }
+  SetLabel(-123456789); // reset label
 }
 
 //_______________________________________________________________
-AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) 
-  :AliTRDtrack(t)
+AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &ref) : AliKalmanTrack(ref)
+  ,fStatus(ref.fStatus)
+  ,fESDid(ref.fESDid)
+  ,fDE(ref.fDE)
+  ,fTruncatedMean(ref.fTruncatedMean)
+  ,fNchamberdEdx(ref.fNchamberdEdx)
+  ,fNclusterdEdx(ref.fNclusterdEdx)
+  ,fNdEdxSlices(ref.fNdEdxSlices)
+  ,fkReconstructor(ref.fkReconstructor)
+  ,fBackupTrack(NULL)
+  ,fTrackLow(NULL)
+  ,fTrackHigh(NULL)
 {
   //
-  // Constructor from AliESDtrack
+  // Copy constructor
   //
 
-       //AliInfo(Form("alpha %f", GetAlpha()));
-       t.GetTRDtracklets(&fTrackletIndex[0]);
-       for(int ip=0; ip<6; ip++) fTracklet[ip].Reset();
+  //printf("AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &)\n");
+  SetBit(kOwner, kFALSE);
+  for(int ip=0; ip<kNplane; ip++){ 
+    fTrackletIndex[ip] = ref.fTrackletIndex[ip];
+    fTracklet[ip]      = ref.fTracklet[ip];
+  }
+  if(ref.fTrackLow) fTrackLow = new AliExternalTrackParam(*ref.fTrackLow);
+  if(ref.fTrackHigh) fTrackHigh = new AliExternalTrackParam(*ref.fTrackHigh);
+  for (Int_t i = 0; i < 3;i++) fBudget[i]      = ref.fBudget[i];
+
+       for(Int_t is = 0; is<AliPID::kSPECIES; is++) fPID[is] = ref.fPID[is];
+  
+  AliKalmanTrack::SetNumberOfClusters(ref.GetNumberOfClusters());
 }
 
 //_______________________________________________________________
-AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &ref) 
-  :AliTRDtrack(ref)
+AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) : AliKalmanTrack()
+  ,fStatus(0)
+  ,fESDid(0)
+  ,fDE(0.)
+  ,fTruncatedMean(0)
+  ,fNchamberdEdx(0)                                                 
+  ,fNclusterdEdx(0)
+  ,fNdEdxSlices(0)
+  ,fkReconstructor(NULL)
+  ,fBackupTrack(NULL)
+  ,fTrackLow(NULL)
+  ,fTrackHigh(NULL)
 {
   //
-  // Copy constructor
+  // Constructor from AliESDtrack
   //
 
-       for(int ip=0; ip<6; ip++){ 
-               fTrackletIndex[ip] = ref.fTrackletIndex[ip];
-               fTracklet[ip]      = ref.fTracklet[ip];
-       }
+  SetESDid(t.GetID());
+  SetLabel(t.GetLabel());
+  SetChi2(0.0);
+
+  SetMass(t.GetMassForTracking());
+  AliKalmanTrack::SetNumberOfClusters(t.GetTRDncls()); 
+  Int_t ti[]={-1, -1, -1, -1, -1, -1}; t.GetTRDtracklets(&ti[0]);
+  for(int ip=0; ip<kNplane; ip++){ 
+    fTrackletIndex[ip] = ti[ip];
+    fTracklet[ip]      = NULL;
+  }
+  for(int i =0; i<3; i++) fBudget[i] = 0.;
+  
+  Float_t pid = 1./AliPID::kSPECIES;
+  for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
+
+  const AliExternalTrackParam *par = &t;
+  if (t.GetStatus() & AliESDtrack::kTRDbackup) {
+    par = t.GetOuterParam();
+    if (!par) {
+      AliError("No backup info!"); 
+      par = &t;
+    }
+  }
+  Set(par->GetX() 
+     ,par->GetAlpha()
+     ,par->GetParameter()
+     ,par->GetCovariance());
+
+  if(t.GetStatus() & AliESDtrack::kTIME) {
+    StartTimeIntegral();
+    Double_t times[10]; 
+    t.GetIntegratedTimes(times); 
+    SetIntegratedTimes(times);
+    SetIntegratedLength(t.GetIntegratedLength());
+  }
 }
 
 //_______________________________________________________________
-// AliTRDtrackV1::~AliTRDtrackV1()
-// {
-//     
-// }
-       
-//_______________________________________________________________
-AliTRDtrackV1::AliTRDtrackV1(AliTRDseedV1 *trklts, const Double_t p[5], const Double_t cov[15]
-             , Double_t x, Double_t alpha)
-  :AliTRDtrack()
+AliTRDtrackV1::AliTRDtrackV1(AliTRDseedV1 * const trklts, const Double_t p[5], const Double_t cov[15]
+             , Double_t x, Double_t alpha) : AliKalmanTrack()
+  ,fStatus(0)
+  ,fESDid(0)
+  ,fDE(0.)
+  ,fTruncatedMean(0)
+  ,fNchamberdEdx(0)                                                 
+  ,fNclusterdEdx(0)
+  ,fNdEdxSlices(0)
+  ,fkReconstructor(NULL)
+  ,fBackupTrack(NULL)
+  ,fTrackLow(NULL)
+  ,fTrackHigh(NULL)
 {
   //
   // The stand alone tracking constructor
@@ -95,295 +189,812 @@ AliTRDtrackV1::AliTRDtrackV1(AliTRDseedV1 *trklts, const Double_t p[5], const Do
   // 2. dQdl calculation
   //
 
-  Double_t cnv   = 1.0 / (GetBz() * kB2C);
-
+  Double_t b(GetBz());
+  Double_t cnv = (TMath::Abs(b) < 1.e-5) ? 1.e5 : 1./GetBz()/kB2C;
+  
   Double_t pp[5] = { p[0]    
-                   , p[1]
-                   , x*p[4] - p[2]
-                   , p[3]
-                   , p[4]*cnv      };
-
+                    , p[1]
+                    , p[2]
+                    , p[3]
+                    , p[4]*cnv      };
+  
   Double_t c22 = x*x*cov[14] - 2*x*cov[12] + cov[ 5];
   Double_t c32 =   x*cov[13] -     cov[ 8];
   Double_t c20 =   x*cov[10] -     cov[ 3];
   Double_t c21 =   x*cov[11] -     cov[ 4];
   Double_t c42 =   x*cov[14] -     cov[12];
-
+  
   Double_t cc[15] = { cov[ 0]
                     , cov[ 1],     cov[ 2]
                     , c20,         c21,         c22
                     , cov[ 6],     cov[ 7],     c32,     cov[ 9]
                     , cov[10]*cnv, cov[11]*cnv, c42*cnv, cov[13]*cnv, cov[14]*cnv*cnv };
   
-       Set(x,alpha,pp,cc);
-  Double_t s = GetSnp();
-  Double_t t = GetTgl();
-
-       Int_t ncl = 0, nclPlane; AliTRDcluster *c = 0x0;
-       for(int iplane=0; iplane<6; iplane++){
-               fTrackletIndex[iplane] = -1;
-               fTracklet[iplane] = trklts[iplane];
-               nclPlane = 0;
-               for(int ic = 0; ic<AliTRDseed::knTimebins; ic++){
-                       if(!fTracklet[iplane].IsUsable(ic)) continue;
-                       if(!(c = fTracklet[iplane].GetClusters(ic))) continue;
-                       
-                       fIndex[ncl]      = fTracklet[iplane].GetIndexes(ic);
-                       Double_t q = TMath::Abs(c->GetQ());
-                       fClusters[ncl]   = c;
-                       // temporary !!!
-                       // This is not correctly. Has to be updated in the AliTRDtrackerV1::FollowBackProlonagation()
-                       fdQdl[ncl]       = q * (s*s < 1.) ? TMath::Sqrt((1-s*s)/(1+t*t)) : 1.;
-                       ncl++;
-                       nclPlane++;
-               }
-               //printf("%d N clusters plane %d [%d %d].\n", iplane, nclPlane, fTracklet[iplane].GetN2(), trklts[iplane].GetN());
-       }
-       //printf("N clusters in AliTRDtrackV1 %d.\n", ncl);
-       SetNumberOfClusters(/*ncl*/);
+  Double_t mostProbablePt=AliExternalTrackParam::GetMostProbablePt();
+  Double_t p0=TMath::Sign(1/mostProbablePt,pp[4]);
+  Double_t w0=cc[14]/(cc[14] + p0*p0), w1=p0*p0/(cc[14] + p0*p0);
+  AliDebug(3, Form("Pt mixing : w0[%4.2f] pt0[%5.3f] w1[%4.2f] pt[%5.3f]", w0, 1./p0, w1, 1./pp[4]));
+  
+  pp[4] = w0*p0 + w1*pp[4];
+  cc[10]*=w1; cc[11]*=w1; cc[12]*=w1; cc[13]*=w1; cc[14]*=w1;
+  Set(x,alpha,pp,cc);
+  AliDebug(2, Form("Init @ x[%6.2f] pt[%5.3f]", x, 1./pp[4]));
+  Int_t ncls = 0;
+  for(int iplane=0; iplane<kNplane; iplane++){
+    fTrackletIndex[iplane] = -1;
+         if(!trklts[iplane].IsOK()) fTracklet[iplane] = NULL;
+    else{ 
+      fTracklet[iplane] = &trklts[iplane];
+      ncls += fTracklet[iplane]->GetN();
+    }
+  }
+  AliKalmanTrack::SetNumberOfClusters(ncls);           
+  for(int i =0; i<3; i++) fBudget[i] = 0.;
+  
+  Float_t pid = 1./AliPID::kSPECIES;
+  for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
+  SetLabel(-123456789); // reset label
 }
 
 //_______________________________________________________________
-Bool_t AliTRDtrackV1::CookLabel(Float_t wrong)
+AliTRDtrackV1::~AliTRDtrackV1()
 {
-       // set MC label for this tracklet
+  // Clean up all objects allocated by the track during its lifetime.
+  AliDebug(2, Form("Deleting track[%d]\n   fBackupTrack[%p] fTrackLow[%p] fTrackHigh[%p] Owner[%c].", fESDid, (void*)fBackupTrack, (void*)fTrackLow, (void*)fTrackHigh, TestBit(kOwner)?'y':'n'));
 
-  Int_t s[kMAXCLUSTERSPERTRACK][2];
-  for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
-    s[i][0] = -1;
-    s[i][1] =  0;
-  }
-
-  Bool_t labelAdded;
-       Int_t label;
-       AliTRDcluster *c    = 0x0;
-  for (Int_t ip = 0; ip < AliESDtrack::kNPlane; ip++) {
-               if(fTrackletIndex[ip] < 0) continue;
-               for (Int_t ic = 0; ic < AliTRDseed::knTimebins; ic++) {
-                       if(!(c = fTracklet[ip].GetClusters(ic))) continue;
-                       for (Int_t k = 0; k < 3; k++) { 
-                               label      = c->GetLabel(k);
-                               labelAdded = kFALSE; 
-                               Int_t j = 0;
-                               if (label >= 0) {
-                                       while ((!labelAdded) && (j < kMAXCLUSTERSPERTRACK)) {
-                                               if ((s[j][0] == label) || 
-                                                               (s[j][1] ==     0)) {
-                                                       s[j][0] = label; 
-                                                       s[j][1]++; 
-                                                       labelAdded = kTRUE;
-                                               }
-                                               j++;
-                                       }
-                               }
-                       }
-               }
-       }
-
-  Int_t max = 0;
-  label = -123456789;
-  for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
-    if (s[i][1] <= max) continue;
-               max   = s[i][1]; 
-               label = s[i][0];
-  }
+  if(fBackupTrack) delete fBackupTrack; fBackupTrack = NULL;
 
-  if ((1. - Float_t(max)/GetNumberOfClusters()) > wrong) label = -label;
+  if(fTrackLow) delete fTrackLow; fTrackLow = NULL;
+  if(fTrackHigh) delete fTrackHigh; fTrackHigh = NULL;
 
-  SetLabel(label); 
-       
-       return kTRUE;
+  for(Int_t ip=0; ip<kNplane; ip++){
+    if(TestBit(kOwner) && fTracklet[ip]) delete fTracklet[ip];
+    fTracklet[ip] = NULL;
+    fTrackletIndex[ip] = -1;
+  }
 }
-
+       
 //_______________________________________________________________
-Bool_t AliTRDtrackV1::CookPID()
+AliTRDtrackV1 &AliTRDtrackV1::operator=(const AliTRDtrackV1 &t)
 {
   //
-  // Cook the PID information
+  // Assignment operator
   //
 
-// CookdEdx();  // truncated mean ... do we still need it ?
+  if (this != &t) {
+    AliKalmanTrack::operator=(t);
+    ((AliTRDtrackV1 &) t).Copy(*this);
+  }
 
-// CookdEdxTimBin(seed->GetID());
-       
-  // Sets the a priori probabilities
-  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) {
-    fPID[ispec] = 1.0 / AliPID::kSPECIES;      
+  return *this;
+
+}
+
+//_____________________________________________________________________________
+void AliTRDtrackV1::Copy(TObject &t) const
+{
+  //
+  // Copy function
+  //
+
+  ((AliTRDtrackV1 &) t).fStatus         = fStatus;
+  ((AliTRDtrackV1 &) t).fESDid          = fESDid;
+  ((AliTRDtrackV1 &) t).fDE             = fDE;
+  ((AliTRDtrackV1 &) t).fkReconstructor = fkReconstructor;
+  ((AliTRDtrackV1 &) t).fBackupTrack    = 0x0;
+  ((AliTRDtrackV1 &) t).fTrackLow       = 0x0;
+  ((AliTRDtrackV1 &) t).fTrackHigh      = 0x0;
+
+  for(Int_t ip = 0; ip < kNplane; ip++) { 
+    ((AliTRDtrackV1 &) t).fTrackletIndex[ip] = fTrackletIndex[ip];
+    ((AliTRDtrackV1 &) t).fTracklet[ip]      = fTracklet[ip];
   }
-       
-       // steer PID calculation @ tracklet level
-       Double_t *prob = 0x0;
-       fPIDquality = 0;
-       for(int itrklt=0; itrklt<AliESDtrack::kNPlane; itrklt++){
-    //for (Int_t iSlice = 0; iSlice < AliESDtrack::kNSlice; iSlice++) fdEdxPlane[itrklt][iSlice] = -1.;
-
-               if(fTrackletIndex[itrklt]<0) continue;
-               if(!fTracklet[itrklt].IsOK()) continue;
-               if(!(prob = fTracklet[itrklt].GetProbability())) return kFALSE;
-               
-               Int_t nspec = 0; // quality check of tracklet dEdx
-               for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
-                       if(prob[ispec] < 0.) continue;
-                       fPID[ispec] *= prob[ispec];
-                       nspec++;
-               }
-               if(!nspec) continue;
-               
-               fPIDquality++;
-       }
-  
-  // no tracklet found for PID calculations
-  if(!fPIDquality) return kTRUE;
+  if (fTrackLow) {
+    ((AliTRDtrackV1 &) t).fTrackLow  = new AliExternalTrackParam(*fTrackLow);
+  }
+  if (fTrackHigh){
+    ((AliTRDtrackV1 &) t).fTrackHigh = new AliExternalTrackParam(*fTrackHigh);
+  }
+  for (Int_t i = 0; i < 3; i++) {
+    ((AliTRDtrackV1 &) t).fBudget[i] = fBudget[i];
+  }
+  for (Int_t is = 0; is < AliPID::kSPECIES; is++) {
+    ((AliTRDtrackV1 &) t).fPID[is] = fPID[is];
+  }  
 
-       // slot for PID calculation @ track level
-       
+}
 
-  // normalize probabilities
-  Double_t probTotal = 0.0;
-  for (Int_t is = 0; is < AliPID::kSPECIES; is++) probTotal += fPID[is];
+//_______________________________________________________________
+Int_t AliTRDtrackV1::CookLabel(Float_t wrong, Int_t *labs, Float_t *freq)
+{
+// Set MC label for this track
+// On demand i.e. if arrays "labs" and "freq" are allocated by user returns :
+//   nlabs = the no of distinct labels
+//   labs  = array of distinct labels in decreasing order of frequency
+//   freq  = frequency of each label  in decreasing order
 
+  Int_t ncl(0);
+  if(!(ncl = GetNumberOfClusters())) return 0;
 
-  if (probTotal <= 0.0) {
-    AliWarning("The total probability over all species <= 0. This may be caused by some error in the reference data.");
+  Int_t s[2][kMAXCLUSTERSPERTRACK];
+  for (Int_t i = 0; i < kMAXCLUSTERSPERTRACK; i++) {
+    s[0][i] = -1;
+    s[1][i] =  0;
+  }
+
+  Int_t label(-123456789), nlabels(0);
+  AliTRDcluster *c(NULL);
+  for (Int_t ip(0); ip < AliTRDgeometry::kNlayer; ip++) {
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
+    for (Int_t ic(0); ic < AliTRDseedV1::kNclusters; ic++) {
+      if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
+      for (Int_t k(0); k < 3; k++) {
+        if ((label = c->GetLabel(k)) < 0) continue;
+        Int_t j(0);
+        while(j < kMAXCLUSTERSPERTRACK){
+          if(s[0][j]!=label && s[1][j]!=0){j++; continue;}
+          if(!s[1][j]) nlabels++;
+          s[0][j] = label; s[1][j]++;
+          break;
+        }
+      }
+    }
+  }
+  //printf("  Found %4d labels\n", nlabels);
+  Float_t prob(1.);
+  if(!nlabels){
+    AliError(Form("No MC labels found for track %d.", fESDid));
+    return 0;
+  } else if(nlabels==1) {
+    label = s[0][0];
+    if(labs && freq){labs[0]=label; freq[0]=1.;}
+  } else {
+    Int_t idx[kMAXCLUSTERSPERTRACK];
+    TMath::Sort(nlabels, s[1], idx);
+    label = s[0][idx[0]]; prob = s[1][idx[0]]/Float_t(ncl);
+    if(labs && freq){
+      for (Int_t i(0); i<nlabels; i++){
+        labs[i] = s[0][idx[i]];
+        freq[i] = s[1][idx[i]]/Float_t(ncl);
+      }
+    }
+  }
+  SetLabel((1.-prob > wrong)?-label:label);
+  return nlabels;
+}
+
+//_______________________________________________________________
+Bool_t AliTRDtrackV1::CookPID()
+{
+//
+// Cook the PID information for the track by delegating the omonim function of the tracklets. 
+// Computes the number of tracklets used. The tracklet information are considered independent. 
+// For the moment no global track measurement of PID is performed as for example to estimate 
+// bremsstrahlung probability based on global chi2 of the track.
+//
+// The status bit AliESDtrack::kTRDpid is set during the call of AliTRDtrackV1::UpdateESDtrack().The PID performance of the 
+//TRD for tracks with 6 tacklets is displayed below.
+//Begin_Html
+//<img src="TRD/trackPID.gif">
+//End_Html
+//
+  const AliTRDPIDResponse *pidResponse = AliTRDcalibDB::Instance()->GetPIDResponse(fkReconstructor->GetRecoParam()->GetPIDmethod());
+  if(!pidResponse){
+    AliError("PID Response not available");
     return kFALSE;
   }
+  Double_t dEdx[kNplane * (Int_t)AliTRDseedV1::kNdEdxSlices] = {0.};
+  Float_t trackletP[kNplane] = {0.};
+
+  fNdEdxSlices = pidResponse->GetNumberOfSlices();
+  for(Int_t iseed = 0; iseed < kNplane; iseed++){
+    if(!fTracklet[iseed]) continue;
+    trackletP[iseed] = fTracklet[iseed]->GetMomentum();
+//    if(pidResponse->GetPIDmethod() == AliTRDPIDResponse::kLQ1D){
+      dEdx[iseed] = fTracklet[iseed]->GetdQdl();
+/*    } else {
+      fTracklet[iseed]->CookdEdx(fNdEdxSlices);
+      const Float_t *trackletdEdx = fTracklet[iseed]->GetdEdx();
+      for(Int_t islice = 0; islice < fNdEdxSlices; islice++){
+        dEdx[iseed*fNdEdxSlices + islice] = trackletdEdx[islice];
+      }
+    }*/
+  }
+  pidResponse->GetResponse(fNdEdxSlices, dEdx, trackletP, fPID);
+
+  static Int_t nprint = 0;
+  if(!nprint){
+    AliTRDdEdxBaseUtils::PrintControl();
+    nprint++;
+  }
+
+  //do truncated mean
+  AliTRDdEdxCalibUtils::SetObjArray(AliTRDcalibDB::Instance()->GetPHQ());
+  const Double_t mag    = AliTRDdEdxBaseUtils::IsExBOn() ? GetBz()  : -1;
+  const Double_t charge = AliTRDdEdxBaseUtils::IsExBOn() ? Charge() : -1;
+  fTruncatedMean = CookTruncatedMean(0, mag, charge, kTRUE, fNchamberdEdx, fNclusterdEdx);
+
+  return kTRUE;
+}
+
+//___________________________________________________________
+UChar_t AliTRDtrackV1::GetNumberOfTrackletsPID() const
+{
+// Retrieve number of tracklets used for PID calculation. 
+
+  UChar_t nPID = 0;
+  for(int ip=0; ip<kNplane; ip++){
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
+    if(!fTracklet[ip]->IsOK()) continue;
+    
+    nPID++;
+  }
+  return nPID;
+}
+
+//_______________________________________________________________
+AliTRDcluster* AliTRDtrackV1::GetCluster(Int_t id)
+{
+  // Get the cluster at a certain position in the track
+  Int_t n = 0;
+  for(Int_t ip=0; ip<kNplane; ip++){
+    if(!fTracklet[ip]) continue;
+    if(n+fTracklet[ip]->GetN() <= id){ 
+      n+=fTracklet[ip]->GetN();
+      continue;
+    }
+    AliTRDcluster *c = NULL;
+    for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
+      if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
+
+      if(n<id){n++; continue;}
+      return c;
+    }
+  }
+  return NULL;
+}
+
+//_______________________________________________________________
+Int_t  AliTRDtrackV1::GetClusterIndex(Int_t id) const
+{
+  // Get the cluster index at a certain position in the track
+  Int_t n = 0;
+  for(Int_t ip=0; ip<kNplane; ip++){
+    if(!fTracklet[ip]) continue;
+    if(n+fTracklet[ip]->GetN() <= id){ 
+      n+=fTracklet[ip]->GetN();
+      continue;
+    }
+    for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
+      if(!(fTracklet[ip]->GetClusters(ic))) continue;
+      if(n<id){n++; continue;}
+      return fTracklet[ip]->GetIndexes(ic);
+    }
+  }
+  return -1;
+}
+
+//_______________________________________________________________
+Double_t AliTRDtrackV1::GetPredictedChi2(const AliTRDseedV1 *trklt, Double_t *cov) const
+{
+// Compute chi2 between tracklet and track. The value is calculated at the radial position of the track
+// equal to the reference radial position of the tracklet (see AliTRDseedV1)
+// 
+// The chi2 estimator is computed according to the following formula
+// BEGIN_LATEX
+// #chi^{2}=(X_{trklt}-X_{track})(C_{trklt}+C_{track})^{-1}(X_{trklt}-X_{track})^{T}
+// END_LATEX
+// where X=(y z), the position of the track/tracklet in the yz plane
+// 
+
+  Double_t p[2]   = { trklt->GetY(), trklt->GetZ()};
+  trklt->GetCovAt(trklt->GetX(), cov);
+  return AliExternalTrackParam::GetPredictedChi2(p, cov);
+}
+
+//_______________________________________________________________
+Int_t AliTRDtrackV1::GetSector() const
+{
+  return Int_t(GetAlpha()/AliTRDgeometry::GetAlpha() + (GetAlpha()>0. ? 0 : AliTRDgeometry::kNsector));
+}
 
-  for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) fPID[iSpecies] /= probTotal;
+//_______________________________________________________________
+Bool_t AliTRDtrackV1::IsEqual(const TObject *o) const
+{
+  // Checks whether two tracks are equal
+  if (!o) return kFALSE;
+  const AliTRDtrackV1 *inTrack = dynamic_cast<const AliTRDtrackV1*>(o);
+  if (!inTrack) return kFALSE;
+  
+  //if ( fPIDquality != inTrack->GetPIDquality() ) return kFALSE;
+  
+  if(memcmp(fPID, inTrack->fPID, AliPID::kSPECIES*sizeof(Double32_t))) return kFALSE;
+  if(memcmp(fBudget, inTrack->fBudget, 3*sizeof(Double32_t))) return kFALSE;
+  if(memcmp(&fDE, &inTrack->fDE, sizeof(Double32_t))) return kFALSE;
+  if(memcmp(&fFakeRatio, &inTrack->fFakeRatio, sizeof(Double32_t))) return kFALSE;
+  if(memcmp(&fChi2, &inTrack->fChi2, sizeof(Double32_t))) return kFALSE;
+  if(memcmp(&fMass, &inTrack->fMass, sizeof(Double32_t))) return kFALSE;
+  if( fLab != inTrack->fLab ) return kFALSE;
+  if( fN != inTrack->fN ) return kFALSE;
+  Double32_t l(0.), in(0.);
+  l = GetIntegratedLength(); in = inTrack->GetIntegratedLength();
+  if(memcmp(&l, &in, sizeof(Double32_t))) return kFALSE;
+  l=GetX(); in=inTrack->GetX();
+  if(memcmp(&l, &in, sizeof(Double32_t))) return kFALSE;
+  l = GetAlpha(); in = inTrack->GetAlpha();
+  if(memcmp(&l, &in, sizeof(Double32_t))) return kFALSE;
+  if(memcmp(GetParameter(), inTrack->GetParameter(), 5*sizeof(Double32_t))) return kFALSE;
+  if(memcmp(GetCovariance(), inTrack->GetCovariance(), 15*sizeof(Double32_t))) return kFALSE;
+  
+  for (Int_t iTracklet = 0; iTracklet < kNplane; iTracklet++){
+    AliTRDseedV1 *curTracklet = fTracklet[iTracklet];
+    AliTRDseedV1 *inTracklet = inTrack->GetTracklet(iTracklet);
+    if (curTracklet && inTracklet){
+      if (! curTracklet->IsEqual(inTracklet) ) {
+        curTracklet->Print();
+        inTracklet->Print();
+        return kFALSE;
+      }
+    } else {
+      // if one tracklet exists, and corresponding 
+      // in other track doesn't - return kFALSE
+      if(inTracklet || curTracklet) return kFALSE;
+    }
+  }
 
   return kTRUE;
 }
 
 //_______________________________________________________________
-Float_t AliTRDtrackV1::GetMomentum(Int_t plane) const
+Bool_t AliTRDtrackV1::IsElectron() const
+{
+  if(GetPID(0) > fkReconstructor->GetRecoParam()->GetPIDThreshold(GetP())) return kTRUE;
+  return kFALSE;
+}
+
+       
+//_____________________________________________________________________________
+Int_t AliTRDtrackV1::MakeBackupTrack()
+{
+//
+// Creates a backup track
+// TO DO update quality check of the track.
+//
+
+  Float_t occupancy(0.); Int_t n(0), ncls(0);
+  for(Int_t il(AliTRDgeometry::kNlayer); il--;){ 
+    if(!fTracklet[il]) continue;
+    n++; 
+    occupancy+=fTracklet[il]->GetTBoccupancy()/AliTRDseedV1::kNtb;
+    ncls += fTracklet[il]->GetN();
+  }
+  if(!n) return -1;
+  occupancy/=n;
+
+  //Float_t ratio1 = Float_t(t.GetNumberOfClusters()+1) / Float_t(t.GetNExpected()+1);  
+  
+  Int_t failedCutId(0);
+  if(GetChi2()/n > 5.0) failedCutId=1; 
+  if(occupancy < 0.7) failedCutId=2;
+  //if(ratio1 >   0.6) && 
+  //if(ratio0+ratio1  >   1.5) && 
+  if(GetNCross() != 0)  failedCutId=3;
+  if(TMath::Abs(GetSnp()) > 0.85) failedCutId=4;
+  if(ncls < 20) failedCutId=5;
+
+  if(failedCutId){ 
+    AliDebug(2, Form("\n"
+      "chi2/tracklet < 5.0   [%c] %5.2f\n"
+      "occupancy > 0.7       [%c] %4.2f\n"
+      "NCross == 0           [%c] %d\n"
+      "Abs(snp) < 0.85       [%c] %4.2f\n"
+      "NClusters > 20        [%c] %d"
+      ,(GetChi2()/n<5.0)?'y':'n', GetChi2()/n
+      ,(occupancy>0.7)?'y':'n', occupancy
+      ,(GetNCross()==0)?'y':'n', GetNCross()
+      ,(TMath::Abs(GetSnp())<0.85)?'y':'n', TMath::Abs(GetSnp())
+      ,(ncls>20)?'y':'n', ncls
+    ));
+    return failedCutId;
+  }
+
+  if(fBackupTrack) {
+    fBackupTrack->~AliTRDtrackV1();
+    new(fBackupTrack) AliTRDtrackV1((AliTRDtrackV1&)(*this));
+    return 0;
+  }
+  fBackupTrack = new AliTRDtrackV1((AliTRDtrackV1&)(*this));
+  return 0;
+}
+
+//_____________________________________________________________________________
+Int_t AliTRDtrackV1::GetProlongation(Double_t xk, Double_t &y, Double_t &z) const
 {
   //
-  // Get the momentum at a given plane
+  // Find a prolongation at given x
+  // Return -1 if it does not exist
+  //  
+
+  Double_t bz = GetBz();
+  if (!AliExternalTrackParam::GetYAt(xk,bz,y)) return -1;
+  if (!AliExternalTrackParam::GetZAt(xk,bz,z)) return -1;
+
+  return 1;  
+
+}
+
+//_____________________________________________________________________________
+Bool_t AliTRDtrackV1::PropagateTo(Double_t xk, Double_t xx0, Double_t xrho)
+{
+  //
+  // Propagates this track to a reference plane defined by "xk" [cm] 
+  // correcting for the mean crossed material.
   //
+  // "xx0"  - thickness/rad.length [units of the radiation length] 
+  // "xrho" - thickness*density    [g/cm^2] 
+  // 
+
+  if (TMath::Abs(xk - GetX())<AliTRDReconstructor::GetEpsilon()*0.1) return kTRUE; // 10% of the tracker precision
+
+  Double_t xyz0[3] = {GetX(), GetY(), GetZ()}, // track position BEFORE propagation 
+           b[3];    // magnetic field 
+  GetBxByBz(b);
+  if(!AliExternalTrackParam::PropagateToBxByBz(xk,b)) return kFALSE;
+  // local track position AFTER propagation 
+  Double_t xyz1[3] = {GetX(), GetY(), GetZ()};
+//  printf("x0[%6.2f] -> x1[%6.2f] dx[%6.2f] rho[%f]\n", xyz0[0], xyz1[0], xyz0[0]-xk, xrho/TMath::Abs(xyz0[0]-xk));
+  if(xyz0[0] < xk) {
+    xrho = -xrho;
+    if (IsStartedTimeIntegral()) {
+      Double_t l2  = TMath::Sqrt((xyz1[0]-xyz0[0])*(xyz1[0]-xyz0[0]) 
+                               + (xyz1[1]-xyz0[1])*(xyz1[1]-xyz0[1]) 
+                               + (xyz1[2]-xyz0[2])*(xyz1[2]-xyz0[2]));
+      Double_t crv = AliExternalTrackParam::GetC(b[2]);
+      if (TMath::Abs(l2*crv) > 0.0001) {
+        // Make correction for curvature if neccesary
+        l2 = 0.5 * TMath::Sqrt((xyz1[0]-xyz0[0])*(xyz1[0]-xyz0[0]) 
+                             + (xyz1[1]-xyz0[1])*(xyz1[1]-xyz0[1]));
+        l2 = 2.0 * TMath::ASin(l2 * crv) / crv;
+        l2 = TMath::Sqrt(l2*l2 + (xyz1[2]-xyz0[2])*(xyz1[2]-xyz0[2]));
+      }
+      AddTimeStep(l2);
+    }
+  }
+  if (!AliExternalTrackParam::CorrectForMeanMaterial(xx0, xrho, fMass)) return kFALSE;
+
+
+  {
+
+    // Energy losses
+    Double_t p2    = (1.0 + GetTgl()*GetTgl()) / (GetSigned1Pt()*GetSigned1Pt());
+    if (fMass<0) p2 *= 4; // q=2
+    Double_t beta2 = p2 / (p2 + fMass*fMass);
+    if ((beta2 < 1.0e-10) || 
+        ((5940.0 * beta2/(1.0 - beta2 + 1.0e-10) - beta2) < 0.0)) {
+      return kFALSE;
+    }
+
+    Double_t dE    = 0.153e-3 / beta2 
+                   * (TMath::Log(5940.0 * beta2/(1.0 - beta2 + 1.0e-10)) - beta2)
+                   * xrho;
+    if (fMass<0) dE *= 4; // q=2
+    fBudget[0] += xrho;
+
+    /*
+    // Suspicious part - think about it ?
+    Double_t kinE =  TMath::Sqrt(p2);
+    if (dE > 0.8*kinE) dE = 0.8 * kinE;  //      
+    if (dE < 0)        dE = 0.0;         // Not valid region for Bethe bloch 
+    */
+    fDE += dE;
+
+    /*
+    // Suspicious ! I.B.
+    Double_t sigmade = 0.07 * TMath::Sqrt(TMath::Abs(dE));   // Energy loss fluctuation 
+    Double_t sigmac2 = sigmade*sigmade*fC*fC*(p2+fMass*fMass)/(p2*p2);
+    fCcc += sigmac2;
+    fCee += fX*fX * sigmac2;  
+    */
+
+  }
 
-       return plane >=0 && plane < 6 && fTrackletIndex[plane] >= 0 ? fTracklet[plane].GetMomentum() : -1.;
+  return kTRUE;
 }
 
-//_______________________________________________________________
-Double_t AliTRDtrackV1::GetPredictedChi2(const AliTRDseedV1 *trklt) const
+//_____________________________________________________________________________
+Int_t   AliTRDtrackV1::PropagateToR(Double_t r,Double_t step)
 {
   //
-  // Get the predicted chi2
+  // Propagate track to the radial position
+  // Rotation always connected to the last track position
   //
 
-  Double_t x      = trklt->GetX0();
-  Double_t p[2]   = { trklt->GetYat(x)
-                    , trklt->GetZat(x) };
-  Double_t cov[3];
-       trklt->GetCovAt(x, cov);
+  Double_t xyz0[3];
+  Double_t xyz1[3];
+  Double_t y;
+  Double_t z; 
+
+  Double_t radius = TMath::Sqrt(GetX()*GetX() + GetY()*GetY());
+  // Direction +-
+  Double_t dir    = (radius > r) ? -1.0 : 1.0;   
+
+  for (Double_t x = radius+dir*step; dir*x < dir*r; x += dir*step) {
+
+    GetXYZ(xyz0);      
+    Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
+    Rotate(alpha,kTRUE);
+    GetXYZ(xyz0);      
+    if(GetProlongation(x,y,z)<0) return -1;
+    xyz1[0] = x * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
+    xyz1[1] = x * TMath::Sin(alpha) - y * TMath::Cos(alpha);
+    xyz1[2] = z;
+    Double_t param[7];
+    if(AliTracker::MeanMaterialBudget(xyz0,xyz1,param)<=0.) return -1;
+    if (param[1] <= 0) {
+      param[1] = 100000000;
+    }
+    PropagateTo(x,param[1],param[0]*param[4]);
+
+  } 
+
+  GetXYZ(xyz0);        
+  Double_t alpha = TMath::ATan2(xyz0[1],xyz0[0]);
+  Rotate(alpha,kTRUE);
+  GetXYZ(xyz0);        
+  if(GetProlongation(r,y,z)<0) return -1;
+  xyz1[0] = r * TMath::Cos(alpha) + y * TMath::Sin(alpha); 
+  xyz1[1] = r * TMath::Sin(alpha) - y * TMath::Cos(alpha);
+  xyz1[2] = z;
+  Double_t param[7];
+  if(AliTracker::MeanMaterialBudget(xyz0,xyz1,param) <= 0.) return -1;
+
+  if (param[1] <= 0) {
+    param[1] = 100000000;
+  }
+  PropagateTo(r,param[1],param[0]*param[4]);
 
-  return AliExternalTrackParam::GetPredictedChi2(p, cov);
+  return 0;
 
 }
 
-//_______________________________________________________________
-Bool_t AliTRDtrackV1::IsOwner() const
+//_____________________________________________________________________________
+void AliTRDtrackV1::Print(Option_t *o) const
+{
+  // Print track status
+  AliInfo(Form("PID [%4.1f %4.1f %4.1f %4.1f %4.1f]", 1.E2*fPID[0], 1.E2*fPID[1], 1.E2*fPID[2], 1.E2*fPID[3], 1.E2*fPID[4]));
+  AliInfo(Form("Material[%5.2f %5.2f %5.2f]", fBudget[0], fBudget[1], fBudget[2]));
+
+  AliInfo(Form("x[%7.2f] t[%7.4f] alpha[%f] mass[%f]", GetX(), GetIntegratedLength(), GetAlpha(), fMass));
+  AliInfo(Form("Ntr[%1d] NtrPID[%1d] Ncl[%3d] lab[%3d]", GetNumberOfTracklets(), GetNumberOfTrackletsPID(), fN, fLab));
+
+  printf("|X| = (");
+  const Double_t *curP = GetParameter();
+  for (Int_t i = 0; i < 5; i++) printf("%7.2f ", curP[i]);
+  printf(")\n");
+
+  printf("|V| = \n");
+  const Double_t *curC = GetCovariance();
+  for (Int_t i = 0, j=4, k=0; i<15; i++, k++){
+    printf("%7.2f ", curC[i]);
+    if(k==j){ 
+      printf("\n");
+      k=-1; j--;
+    }
+  }
+  if(strcmp(o, "a")!=0) return;
+
+  for(Int_t ip=0; ip<kNplane; ip++){
+    if(!fTracklet[ip]) continue;
+    fTracklet[ip]->Print(o);
+  }
+}
+
+
+//_____________________________________________________________________________
+Bool_t AliTRDtrackV1::Rotate(Double_t alpha, Bool_t absolute)
 {
   //
-  // Check whether track owns the tracklets
-  //
+  // Rotates track parameters in R*phi plane
+  // if absolute rotation alpha is in global system
+  // otherwise alpha rotation is relative to the current rotation angle
+  //  
 
-       for (Int_t ip = 0; ip < AliESDtrack::kNPlane; ip++) {
-               if(fTrackletIndex[ip] < 0) continue;
-               if(!fTracklet[ip].IsOwner()) return kFALSE;
-       }
-       return kTRUE;
+  if (absolute) alpha -= GetAlpha();
+  //else fNRotate++;
+
+  return AliExternalTrackParam::Rotate(GetAlpha()+alpha);
 }
-       
+
 //___________________________________________________________
 void AliTRDtrackV1::SetNumberOfClusters() 
 {
 // Calculate the number of clusters attached to this track
        
-       Int_t ncls = 0;
-       for(int ip=0; ip<6; ip++){
-               if(fTrackletIndex[ip] >= 0) ncls += fTracklet[ip].GetN();
-       }
-       AliKalmanTrack::SetNumberOfClusters(ncls);      
+  Int_t ncls = 0;
+  for(int ip=0; ip<kNplane; ip++){
+    if(fTracklet[ip] && fTrackletIndex[ip] >= 0) ncls += fTracklet[ip]->GetN();
+  }
+  AliKalmanTrack::SetNumberOfClusters(ncls);   
 }
 
        
 //_______________________________________________________________
-void AliTRDtrackV1::SetOwner(Bool_t own)
+void AliTRDtrackV1::SetOwner()
 {
   //
   // Toggle ownership of tracklets
   //
 
-       for (Int_t ip = 0; ip < AliESDtrack::kNPlane; ip++) {
-               if(fTrackletIndex[ip] < 0) continue;
-               //AliInfo(Form("p[%d] index[%d]", ip, fTrackletIndex[ip]));
-               fTracklet[ip].SetOwner(own);
-       }
+  if(TestBit(kOwner)) return;
+  for (Int_t ip = 0; ip < kNplane; ip++) {
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
+    fTracklet[ip] = new AliTRDseedV1(*fTracklet[ip]);
+    fTracklet[ip]->SetOwner();
+  }
+  SetBit(kOwner);
 }
 
 //_______________________________________________________________
-void AliTRDtrackV1::SetTracklet(AliTRDseedV1 *trklt, Int_t plane, Int_t index)
+void AliTRDtrackV1::SetTracklet(AliTRDseedV1 *const trklt, Int_t index)
 {
   //
   // Set the tracklets
   //
+  Int_t plane = trklt->GetPlane();
+
+  fTracklet[plane]      = trklt;
+  fTrackletIndex[plane] = index;
+}
 
-       if(plane < 0 || plane >= AliESDtrack::kNPlane) return;
-       fTracklet[plane]      = (*trklt);
-       fTrackletIndex[plane] = index;
+//_______________________________________________________________
+void AliTRDtrackV1::SetTrackIn()
+{
+//  Save location of birth for the TRD track
+//  If the pointer is not valid allocate memory
+//
+  const AliExternalTrackParam *op = dynamic_cast<const AliExternalTrackParam*>(this);
+
+  //printf("SetTrackIn() : fTrackLow[%p]\n", (void*)fTrackLow);
+  if(fTrackLow){
+    fTrackLow->~AliExternalTrackParam();
+    new(fTrackLow) AliExternalTrackParam(*op);
+  } else fTrackLow = new AliExternalTrackParam(*op);
 }
 
 //_______________________________________________________________
-Bool_t  AliTRDtrackV1::Update(AliTRDseedV1 *trklt, Double_t chisq)
+void AliTRDtrackV1::SetTrackOut(const AliExternalTrackParam *op)
+{
+//  Save location of death for the TRD track
+//  If the pointer is not valid allocate memory
+//
+  if(!op) op = dynamic_cast<const AliExternalTrackParam*>(this);
+  if(fTrackHigh){
+    fTrackHigh->~AliExternalTrackParam();
+    new(fTrackHigh) AliExternalTrackParam(*op);
+  } else fTrackHigh = new AliExternalTrackParam(*op);
+}
+
+//_______________________________________________________________
+void AliTRDtrackV1::UnsetTracklet(Int_t plane)
+{
+  if(plane<0) return;
+  fTrackletIndex[plane] = -1;
+  fTracklet[plane] = NULL;
+}
+
+
+//_______________________________________________________________
+void AliTRDtrackV1::UpdateChi2(Float_t chi2)
+{
+// Update chi2/track with one tracklet contribution
+  SetChi2(GetChi2() + chi2);
+}
+
+//_______________________________________________________________
+void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
 {
   //
-  // Update track parameters
+  // Update the TRD PID information in the ESD track
   //
 
-  Double_t x      = trklt->GetX0();
-  Double_t p[2]   = { trklt->GetYat(x)
-                    , trklt->GetZat(x) };
-  Double_t cov[3];
-       trklt->GetCovAt(x, cov);
-       
-//     Print();
-//     AliInfo(Form("cov[%f %f %f]", cov[0], cov[1], cov[2]));
-
-  if(!AliExternalTrackParam::Update(p, cov)) return kFALSE;
-       //Print();
-
-  // Register info to track
-//   Int_t n      = GetNumberOfClusters();
-//   fIndex[n]    = index;
-//   fClusters[n] = c;
-  SetNumberOfClusters(/*GetNumberOfClusters()+trklt->GetN()*/);
-  SetChi2(GetChi2() + chisq);
-       
-       // update tracklet
-       trklt->SetMomentum(GetP());
-       trklt->SetSnp(GetSnp());
-       trklt->SetTgl(GetTgl());
-       return kTRUE;
+//   Int_t nslices = AliTRDcalibDB::Instance()->GetPIDResponse(fkReconstructor->GetRecoParam()->GetPIDmethod())->GetNumberOfSlices();
+  // number of tracklets used for PID calculation
+  UChar_t nPID = GetNumberOfTrackletsPID();
+  // number of tracklets attached to the track
+  UChar_t nTrk = GetNumberOfTracklets();
+  // pack the two numbers together and store them in the ESD
+  track->SetTRDntracklets(nPID | (nTrk<<3));
+  // allocate space to store raw PID signals dEdx & momentum
+  // independent of the method used to calculate PID (see below AliTRDPIDResponse)
+  track->SetNumberOfTRDslices((AliTRDseedV1::kNdEdxSlices+2)*AliTRDgeometry::kNlayer);
+  // store raw signals
+  Float_t p, sp; Double_t spd;
+  for (Int_t ip = 0; ip < kNplane; ip++) {
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
+    // Fill TRD dEdx info into ESD track
+    // a. Set Summed dEdx into the first slice
+    track->SetTRDslice(fTracklet[ip]->GetdQdl(), ip, 0); 
+    // b. Set NN dEdx slices
+    fTracklet[ip]->CookdEdx(AliTRDPIDResponse::kNslicesNN);
+    const Float_t *dedx = fTracklet[ip]->GetdEdx();
+    for (Int_t js(0), ks(1); js < AliTRDPIDResponse::kNslicesNN; js++, ks++, dedx++){
+      if(ks>=AliTRDseedV1::kNdEdxSlices){
+        AliError(Form("Exceed allocated space for dEdx slices."));
+        break;
+      }
+      track->SetTRDslice(*dedx, ip, ks);
+    }
+    // fill TRD momentum info into ESD track
+    p = fTracklet[ip]->GetMomentum(&sp); spd = sp;
+    track->SetTRDmomentum(p, ip, &spd);
+    // store global quality per tracklet instead of momentum error
+    // 26.09.11 A.Bercuci
+    // first implementation store no. of time bins filled in tracklet (5bits  see "y" bits) and
+    // no. of double clusters in case of pad row cross (4bits see "x" bits)
+    // bit map for tracklet quality xxxxyyyyy
+    // 27.10.11 A.Bercuci
+    // add chamber status bit "z" bit
+    // bit map for tracklet quality zxxxxyyyyy
+    // 12.11.11 A.Bercuci
+    // fit tracklet quality into the field fTRDTimeBin [Char_t]
+    // bit map for tracklet quality zxxyyyyy
+    // The information should be retrieved by the following functions of AliESDtrack for each TRD layer
+    // GetTRDtrkltOccupancy(layer) -> no of TB filled in tracklet
+    // GetTRDtrkltClCross(layer)   -> no of TB filled in crossing pad rows
+    // IsTRDtrkltChmbGood(layer)   -> status of the chamber from which the tracklet is found
+    Int_t nCross(fTracklet[ip]->IsRowCross()?fTracklet[ip]->GetTBcross():0); if(nCross>3) nCross = 3;
+    Char_t trackletQ = Char_t(fTracklet[ip]->GetTBoccupancy() | (nCross<<5) | (fTracklet[ip]->IsChmbGood()<<7));
+    track->SetTRDTimBin(trackletQ, ip);
+  }
+  // store PID probabilities
+  track->SetTRDpid(fPID);
+
+  //store truncated mean
+  track->SetTRDsignal(fTruncatedMean);
+  track->SetTRDNchamberdEdx(fNchamberdEdx);
+  track->SetTRDNclusterdEdx(fNclusterdEdx);
 }
 
 //_______________________________________________________________
-void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
+Double_t  AliTRDtrackV1::CookTruncatedMean(const Bool_t kinvq, const Double_t mag, const Int_t charge, const Int_t kcalib, Int_t &nch, Int_t &ncls, TVectorD *Qs, TVectorD *Xs, Int_t timeBin0, Int_t timeBin1, Int_t tstep) const
 {
   //
-  // Update the ESD track
+  //Origin: Xianguo Lu <xianguo.lu@cern.ch>, Marian Ivanov <marian.ivanov@cern.ch>
   //
-       
-       // copy dEdx to ESD
-       Float_t *dedx = 0x0;
-       for (Int_t ip = 0; ip < AliESDtrack::kNPlane; ip++) {
-               if(fTrackletIndex[ip] < 0) continue;
-               fTracklet[ip].CookdEdx(AliESDtrack::kNSlice);
-               dedx = fTracklet[ip].GetdEdx();
-               for (Int_t js = 0; js < AliESDtrack::kNSlice; js++) track->SetTRDsignals(dedx[js], ip, js);
-               //track->SetTRDTimBin(fTimBinPlane[i], i);
-       }
-
-       // copy PID to ESD
-       track->SetTRDpid(fPID);
-       track->SetTRDpidQuality(fPIDquality);
+
+  TVectorD arrayQ(200), arrayX(200);
+  ncls = AliTRDdEdxReconUtils::GetArrayClusterQ(kinvq, &arrayQ, &arrayX, this, timeBin0, timeBin1, tstep);
+
+  const TObjArray *cobj = kcalib ? AliTRDdEdxCalibUtils::GetObj(kinvq, mag, charge) : NULL;
+  
+  const Double_t tmean = AliTRDdEdxReconUtils::ToyCook(kinvq, ncls, &arrayQ, &arrayX, cobj);
+
+  nch = AliTRDdEdxReconUtils::UpdateArrayX(ncls, &arrayX);
+
+  if(Qs && Xs){
+    (*Qs)=arrayQ;
+    (*Xs)=arrayX;
+  }
+
+  //printf("\ntest %.10f %d %d\n", tmean, nch, ncls);
+
+  return tmean;
+}
+
+//_______________________________________________________________
+TObject* AliTRDtrackV1::Clone(const char* newname) const
+{
+  // temporary override TObject::Clone to avoid crashes in reco
+  AliTRDtrackV1* src = (AliTRDtrackV1*)this;
+  Bool_t isown = src->IsOwner();
+  AliInfo(Form("src_owner %d",isown));
+  AliTRDtrackV1* dst = new AliTRDtrackV1(*src);
+  if (isown) {
+    src->SetBit(kOwner);
+    dst->SetOwner();
+  }
+  return dst;
 }