]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDtrackV1.cxx
Fix
[u/mrichter/AliRoot.git] / TRD / AliTRDtrackV1.cxx
index 7f072e68d37acba48a6d178ecbcb70fd3bdaeb7c..1fcf5b500432e2c1f065a3adac573cca2e21cdc6 100644 (file)
@@ -1,3 +1,4 @@
+
 /**************************************************************************
  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
  *                                                                        *
@@ -23,6 +24,7 @@
 #include "AliTRDcluster.h"
 #include "AliTRDcalibDB.h"
 #include "AliTRDReconstructor.h"
+#include "AliTRDPIDResponse.h"
 #include "AliTRDrecoParam.h"
 
 ClassImp(AliTRDtrackV1)
@@ -41,6 +43,7 @@ ClassImp(AliTRDtrackV1)
 //_______________________________________________________________
 AliTRDtrackV1::AliTRDtrackV1() : AliKalmanTrack()
   ,fStatus(0)
+  ,fESDid(0)
   ,fDE(0.)
   ,fkReconstructor(NULL)
   ,fBackupTrack(NULL)
@@ -58,7 +61,7 @@ AliTRDtrackV1::AliTRDtrackV1() : AliKalmanTrack()
   for(int is =0; is<AliPID::kSPECIES; is++) fPID[is] = pid;
 
   for(int ip=0; ip<kNplane; ip++){
-    fTrackletIndex[ip] = 0xffff;
+    fTrackletIndex[ip] = -1;
     fTracklet[ip]      = NULL;
   }
 }
@@ -66,6 +69,7 @@ AliTRDtrackV1::AliTRDtrackV1() : AliKalmanTrack()
 //_______________________________________________________________
 AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &ref) : AliKalmanTrack(ref)
   ,fStatus(ref.fStatus)
+  ,fESDid(ref.fESDid)
   ,fDE(ref.fDE)
   ,fkReconstructor(ref.fkReconstructor)
   ,fBackupTrack(NULL)
@@ -95,6 +99,7 @@ AliTRDtrackV1::AliTRDtrackV1(const AliTRDtrackV1 &ref) : AliKalmanTrack(ref)
 //_______________________________________________________________
 AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) : AliKalmanTrack()
   ,fStatus(0)
+  ,fESDid(0)
   ,fDE(0.)
   ,fkReconstructor(NULL)
   ,fBackupTrack(NULL)
@@ -105,14 +110,15 @@ AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) : AliKalmanTrack()
   // Constructor from AliESDtrack
   //
 
+  SetESDid(t.GetID());
   SetLabel(t.GetLabel());
   SetChi2(0.0);
 
   SetMass(t.GetMass()/*0.000510*/);
   AliKalmanTrack::SetNumberOfClusters(t.GetTRDncls()); 
-  Int_t ti[kNplane]; t.GetTRDtracklets(&ti[0]);
+  Int_t ti[]={-1, -1, -1, -1, -1, -1}; t.GetTRDtracklets(&ti[0]);
   for(int ip=0; ip<kNplane; ip++){ 
-    fTrackletIndex[ip] = ti[ip] < 0 ? 0xffff : ti[ip];
+    fTrackletIndex[ip] = ti[ip];
     fTracklet[ip]      = NULL;
   }
   for(int i =0; i<3; i++) fBudget[i] = 0.;
@@ -146,6 +152,7 @@ AliTRDtrackV1::AliTRDtrackV1(const AliESDtrack &t) : AliKalmanTrack()
 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.)
   ,fkReconstructor(NULL)
   ,fBackupTrack(NULL)
@@ -160,60 +167,57 @@ AliTRDtrackV1::AliTRDtrackV1(AliTRDseedV1 * const trklts, const Double_t p[5], c
   // 2. dQdl calculation
   //
 
-  Double_t cnv = GetBz() < 1.e-5 ? 1.e5 : 1.0 / (GetBz() * kB2C);
-  //  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]
-                   , 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 };
-
+  
   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(4, Form("Pt mixing : w0[%4.2f] 1/pt0[%5.3f] w1[%4.2f] 1/pt[%5.3f]", w0, 1./p0, w1, 1./pp[4]));
+  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);
+  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] = 0xffff;
-               if(!trklts[iplane].IsOK()) fTracklet[iplane] = NULL;
+  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;
-
 }
 
 //_______________________________________________________________
 AliTRDtrackV1::~AliTRDtrackV1()
 {
-  //AliInfo("");
-  //printf("I-AliTRDtrackV1::~AliTRDtrackV1() : Owner[%s]\n", TestBit(kOwner)?"YES":"NO");
+  // 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'));
 
   if(fBackupTrack) delete fBackupTrack; fBackupTrack = NULL;
 
@@ -223,10 +227,60 @@ AliTRDtrackV1::~AliTRDtrackV1()
   for(Int_t ip=0; ip<kNplane; ip++){
     if(TestBit(kOwner) && fTracklet[ip]) delete fTracklet[ip];
     fTracklet[ip] = NULL;
-    fTrackletIndex[ip] = 0xffff;
+    fTrackletIndex[ip] = -1;
   }
 }
        
+//_______________________________________________________________
+AliTRDtrackV1 &AliTRDtrackV1::operator=(const AliTRDtrackV1 &t)
+{
+  //
+  // Assignment operator
+  //
+
+  if (this != &t) {
+    ((AliTRDtrackV1 &) t).Copy(*this);
+  }
+
+  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];
+  }
+  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];
+  }  
+
+}
+
 //_______________________________________________________________
 Bool_t AliTRDtrackV1::CookLabel(Float_t wrong)
 {
@@ -243,7 +297,7 @@ Bool_t AliTRDtrackV1::CookLabel(Float_t wrong)
   Int_t label;
   AliTRDcluster *c    = NULL;
   for (Int_t ip = 0; ip < kNplane; ip++) {
-    if(fTrackletIndex[ip] == 0xffff) continue;
+    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++) { 
@@ -294,29 +348,31 @@ Bool_t AliTRDtrackV1::CookPID()
 //<img src="TRD/trackPID.gif">
 //End_Html
 //
-  
-  /*Reset the a priori probabilities*/
-  Double_t pid = 1. / AliPID::kSPECIES;
-  for(int ispec=0; ispec<AliPID::kSPECIES; ispec++) fPID[ispec] = pid; 
-
-  UChar_t fPIDquality = SetNumberOfTrackletsPID(kTRUE);
-  // no tracklet found for PID calculations
-  if(!fPIDquality) return kFALSE;
-  
-  // slot for PID calculation @ track level for bremsstrahlung TODO
-  
-  // normalize probabilities
-  Double_t probTotal = 0.0;
-  for (Int_t is = 0; is < AliPID::kSPECIES; is++) probTotal += fPID[is];
-  
-  
-  if (probTotal <= 0.0) {
-    AliWarning("The total probability over all species <= 0. This may be caused by some error in the reference data.");
+  const AliTRDPIDResponse *pidResponse = AliTRDcalibDB::Instance()->GetPIDResponse(fkReconstructor->GetRecoParam()->GetPIDmethod());
+  if(!pidResponse){
+    AliError("PID Response not available");
     return kFALSE;
   }
-  
-  for (Int_t iSpecies = 0; iSpecies < AliPID::kSPECIES; iSpecies++) fPID[iSpecies] /= probTotal;
-  
+  Int_t nslices = pidResponse->GetNumberOfSlices();
+  Double_t dEdx[kNplane * (Int_t)AliTRDPIDResponse::kNslicesNN];
+  Float_t trackletP[kNplane];
+  memset(dEdx, 0, sizeof(Double_t) * kNplane * (Int_t)AliTRDPIDResponse::kNslicesNN);
+  memset(trackletP, 0, sizeof(Float_t)*kNplane);
+  for(Int_t iseed = 0; iseed < kNplane; iseed++){
+    if(!fTracklet[iseed]) continue;
+    trackletP[iseed] = fTracklet[iseed]->GetMomentum();
+    fTracklet[iseed]->SetPID();
+    if(pidResponse->GetPIDmethod() == AliTRDPIDResponse::kLQ1D){
+      dEdx[iseed] = fTracklet[iseed]->GetdQdl();
+    } else {
+      fTracklet[iseed]->CookdEdx(nslices);
+      const Float_t *trackletdEdx = fTracklet[iseed]->GetdEdx();
+      for(Int_t islice = 0; islice < nslices; islice++){
+        dEdx[iseed*nslices + islice] = trackletdEdx[islice];
+      }
+    }
+  }
+  pidResponse->GetResponse(nslices, dEdx, trackletP, fPID);
   return kTRUE;
 }
 
@@ -326,52 +382,15 @@ UChar_t AliTRDtrackV1::GetNumberOfTrackletsPID() const
 // Retrieve number of tracklets used for PID calculation. 
 
   UChar_t nPID = 0;
-  Float_t *prob = NULL;
   for(int ip=0; ip<kNplane; ip++){
-    if(fTrackletIndex[ip] == 0xffff) continue;
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
     if(!fTracklet[ip]->IsOK()) continue;
-    if(!(prob = fTracklet[ip]->GetProbability(kFALSE))) continue;
-    
-    Int_t nspec = 0; // quality check of tracklet dEdx
-    for(int ispec=0; ispec<AliPID::kSPECIES; ispec++){
-      if(prob[ispec] < 0.) continue;
-      nspec++;
-    }
-    if(!nspec) continue;
     
-    fTracklet[ip]->SetPID();
     nPID++;
   }
   return nPID;
 }
 
-//___________________________________________________________
-UChar_t AliTRDtrackV1::SetNumberOfTrackletsPID(Bool_t recalc)
-{
-// Retrieve number of tracklets used for PID calculation. // Recalculated PID at tracklet level by quering the PID DB.
-
-  UChar_t fPIDquality = 0;
-  
-  // steer PID calculation @ tracklet level
-  Float_t *prob = NULL;
-  for(int ip=0; ip<kNplane; ip++){
-    if(fTrackletIndex[ip] == 0xffff) continue;
-    if(!fTracklet[ip]->IsOK()) continue;
-    if(!(prob = fTracklet[ip]->GetProbability(recalc))) return 0;
-    
-    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++;
-  }
-  return fPIDquality;
-}
-
 //_______________________________________________________________
 AliTRDcluster* AliTRDtrackV1::GetCluster(Int_t id)
 {
@@ -405,10 +424,8 @@ Int_t  AliTRDtrackV1::GetClusterIndex(Int_t id) const
       n+=fTracklet[ip]->GetN();
       continue;
     }
-    AliTRDcluster *c = NULL;
     for(Int_t ic=AliTRDseedV1::kNclusters; ic--;){
-      if(!(c = fTracklet[ip]->GetClusters(ic))) continue;
-
+      if(!(fTracklet[ip]->GetClusters(ic))) continue;
       if(n<id){n++; continue;}
       return fTracklet[ip]->GetIndexes(ic);
     }
@@ -450,33 +467,23 @@ Bool_t AliTRDtrackV1::IsEqual(const TObject *o) const
   
   //if ( fPIDquality != inTrack->GetPIDquality() ) return kFALSE;
   
-  for(Int_t i = 0; i < AliPID::kSPECIES; i++){
-    if ( fPID[i] != inTrack->GetPID(i) ) return kFALSE;
-  }
-  
-  for (Int_t i = 0; i < 3; i++){
-    if ( fBudget[i] != inTrack->GetBudget(i) ) return kFALSE;
-  }
-  if ( fDE != inTrack->GetEdep() ) return kFALSE;
-  if ( fFakeRatio != inTrack->GetFakeRatio() ) return kFALSE;
-  if ( fChi2 != inTrack->GetChi2() ) return kFALSE;
-  if ( fMass != inTrack->GetMass() ) return kFALSE;
-  if ( fLab != inTrack->GetLabel() ) return kFALSE;
-  if ( fN != inTrack->GetNumberOfClusters() ) return kFALSE;
-  if ( AliKalmanTrack::GetIntegratedLength() != inTrack->GetIntegratedLength() ) return kFALSE;
-  
-  if ( GetX() != inTrack->GetX() ) return kFALSE;
-  if ( GetAlpha() != inTrack->GetAlpha() ) return kFALSE;
-  const Double_t *inP = inTrack->GetParameter();
-  const Double_t *curP = GetParameter();
-  for (Int_t i = 0; i < 5; i++){
-    if ( curP[i] != inP[i]) return kFALSE;
-  }
-  const Double_t *inC = inTrack->GetCovariance();
-  const Double_t *curC = GetCovariance();
-  for (Int_t i = 0; i < 15; i++){
-    if ( curC[i] != inC[i]) 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];
@@ -506,22 +513,61 @@ Bool_t AliTRDtrackV1::IsElectron() const
 
        
 //_____________________________________________________________________________
-void AliTRDtrackV1::MakeBackupTrack()
+Int_t AliTRDtrackV1::MakeBackupTrack()
 {
-  //
-  // Creates a backup track
-  //
+//
+// 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]->GetOccupancyTB();
+    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;
+    return 0;
   }
   fBackupTrack = new AliTRDtrackV1((AliTRDtrackV1&)(*this));
+  return 0;
 }
 
 //_____________________________________________________________________________
-Int_t AliTRDtrackV1::GetProlongation(Double_t xk, Double_t &y, Double_t &z)
+Int_t AliTRDtrackV1::GetProlongation(Double_t xk, Double_t &y, Double_t &z) const
 {
   //
   // Find a prolongation at given x
@@ -547,7 +593,7 @@ Bool_t AliTRDtrackV1::PropagateTo(Double_t xk, Double_t xx0, Double_t xrho)
   // "xrho" - thickness*density    [g/cm^2] 
   // 
 
-  if (xk == GetX()) return kTRUE;
+  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 
@@ -562,7 +608,7 @@ Bool_t AliTRDtrackV1::PropagateTo(Double_t xk, Double_t xx0, Double_t xrho)
       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[3]);
+      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]) 
@@ -725,7 +771,7 @@ void AliTRDtrackV1::SetNumberOfClusters()
        
   Int_t ncls = 0;
   for(int ip=0; ip<kNplane; ip++){
-    if(fTracklet[ip] && fTrackletIndex[ip] != 0xffff) ncls += fTracklet[ip]->GetN();
+    if(fTracklet[ip] && fTrackletIndex[ip] >= 0) ncls += fTracklet[ip]->GetN();
   }
   AliKalmanTrack::SetNumberOfClusters(ncls);   
 }
@@ -740,7 +786,7 @@ void AliTRDtrackV1::SetOwner()
 
   if(TestBit(kOwner)) return;
   for (Int_t ip = 0; ip < kNplane; ip++) {
-    if(fTrackletIndex[ip] == 0xffff) continue;
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
     fTracklet[ip] = new AliTRDseedV1(*fTracklet[ip]);
     fTracklet[ip]->SetOwner();
   }
@@ -760,41 +806,47 @@ void AliTRDtrackV1::SetTracklet(AliTRDseedV1 *const trklt, Int_t index)
 }
 
 //_______________________________________________________________
-void AliTRDtrackV1::SetTrackLow()
+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);
-  fTrackLow = fTrackLow ? new(fTrackLow) AliExternalTrackParam(*op) : new AliExternalTrackParam(*op);
+
+  //printf("SetTrackIn() : fTrackLow[%p]\n", (void*)fTrackLow);
+  if(fTrackLow){
+    fTrackLow->~AliExternalTrackParam();
+    new(fTrackLow) AliExternalTrackParam(*op);
+  } else fTrackLow = new AliExternalTrackParam(*op);
 }
 
 //_______________________________________________________________
-void AliTRDtrackV1::SetTrackHigh(const AliExternalTrackParam *op)
+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);
-  fTrackHigh = fTrackHigh ? new(fTrackHigh) AliExternalTrackParam(*op) : new AliExternalTrackParam(*op);
+  if(fTrackHigh){
+    fTrackHigh->~AliExternalTrackParam();
+    new(fTrackHigh) AliExternalTrackParam(*op);
+  } else fTrackHigh = new AliExternalTrackParam(*op);
 }
 
 //_______________________________________________________________
 void AliTRDtrackV1::UnsetTracklet(Int_t plane)
 {
-  if(plane<0 && plane >= kNplane) return;
-  fTrackletIndex[plane] = 0xffff;
+  if(plane<0) return;
+  fTrackletIndex[plane] = -1;
   fTracklet[plane] = NULL;
 }
 
 
 //_______________________________________________________________
-Bool_t  AliTRDtrackV1::Update(Double_t *p, Double_t *cov, Double_t chi2)
+void AliTRDtrackV1::UpdateChi2(Float_t chi2)
 {
-  //
-  // Update track 
-  //
-  AliDebug(2, Form("Point:\n  p=[%6.2f %6.2f]\n  V=[%6.2f %6.2f]\n    [%6.2f %6.2f]", p[0], p[1], cov[0], cov[1], cov[1], cov[2]));
-  if(!AliExternalTrackParam::Update(p, cov)) return kFALSE;
-
-  // Register info to track
-  SetNumberOfClusters();
+// Update chi2/track with one tracklet contribution
   SetChi2(GetChi2() + chi2);
-  return kTRUE;
 }
 
 //_______________________________________________________________
@@ -804,7 +856,7 @@ void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
   // Update the TRD PID information in the ESD track
   //
 
-  Int_t nslices = fkReconstructor->GetRecoParam()->IsEightSlices() ? (Int_t)AliTRDpidUtil::kNNslices : (Int_t)AliTRDpidUtil::kLQslices;
+//   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
@@ -812,16 +864,26 @@ void AliTRDtrackV1::UpdateESDtrack(AliESDtrack *track)
   // 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
-  track->SetNumberOfTRDslices((nslices+2)*AliTRDgeometry::kNlayer);
+  track->SetNumberOfTRDslices((AliTRDPIDResponse::kNslicesNN+3)*AliTRDgeometry::kNlayer);
   // store raw signals
   Float_t p, sp; Double_t spd;
   for (Int_t ip = 0; ip < kNplane; ip++) {
-    if(fTrackletIndex[ip] == 0xffff) continue;
+    if(fTrackletIndex[ip]<0 || !fTracklet[ip]) continue;
     if(!fTracklet[ip]->HasPID()) continue;
+    //printf("Setting slices for tracklet %d\n", ip);
+    fTracklet[ip]->CookdEdx(AliTRDPIDResponse::kNslicesNN);
     const Float_t *dedx = fTracklet[ip]->GetdEdx();
-    for (Int_t js = 0; js < nslices; js++, dedx++) track->SetTRDslice(*dedx, ip, js);
-    p = fTracklet[ip]->GetMomentum(&sp); spd = sp;
+    for (Int_t js = 0; js < AliTRDPIDResponse::kNslicesNN; js++, dedx++){
+      //printf("Slice %d, dEdx %f\n", js, *dedx);
+      track->SetTRDslice(*dedx, ip, js+1);
+    }
+    p = fTracklet[ip]->GetMomentum(&sp); 
+    // 04.01.11 A.Bercuci
+    // store global dQdl per tracklet instead of momentum error
+    spd = sp;
     track->SetTRDmomentum(p, ip, &spd);
+    //printf("Total Charge %f\n", fTracklet[ip]->GetdQdl());
+    track->SetTRDslice(fTracklet[ip]->GetdQdl(), ip, 0); // Set Summed dEdx into the first slice
   }
   // store PID probabilities
   track->SetTRDpid(fPID);