]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDtrackerV1.cxx
update from Prabhat
[u/mrichter/AliRoot.git] / TRD / AliTRDtrackerV1.cxx
index c7d890e8d519559a013f4226288111e339f85c2f..3dbadd98800202a14b7d34539dce5b6b1b7162d2 100644 (file)
@@ -133,7 +133,7 @@ AliTRDtrackerV1::~AliTRDtrackerV1()
   //
   // Destructor
   //
-  
+
   if(fgRieman) delete fgRieman; fgRieman = NULL;
   if(fgTiltedRieman) delete fgTiltedRieman; fgTiltedRieman = NULL;
   if(fgTiltedRiemanConstrained) delete fgTiltedRiemanConstrained; fgTiltedRiemanConstrained = NULL;
@@ -141,7 +141,8 @@ AliTRDtrackerV1::~AliTRDtrackerV1()
   if(fTracksESD){ fTracksESD->Delete(); delete fTracksESD; }
   if(fTracks) {fTracks->Delete(); delete fTracks;}
   if(fTracklets) {fTracklets->Delete(); delete fTracklets;}
-  if(fClusters) {
+  if(IsClustersOwner() && fClusters) {
+    AliInfo(Form("tracker[%p] removing %d own clusters @ %p", (void*)this, fClusters->GetEntries(), (void*)fClusters));
     fClusters->Delete(); delete fClusters;
   }
   if(fGeom) delete fGeom;
@@ -278,237 +279,192 @@ Int_t AliTRDtrackerV1::PropagateBack(AliESDEvent *event)
   if (!fgNTimeBins) fgNTimeBins = fkReconstructor->GetNTimeBins(); 
 
   // Define scalers
-  const Int_t nSeeders(2);
-  Int_t nFound[nSeeders]  = {0},   // number of tracks found
-        nBacked    = 0,            // number of tracks backed up for refit
-        nSeeds     = 0,            // total number of ESD seeds
-        nTRDseeds  = 0,            // number of seeds in the TRD acceptance
-        nSeeder[nSeeders]= {0},    // number of ITS/TPC potential seeds
-        nGoodSeeds[nSeeders]= {0}; // number of ITS/TPC found seeds
+  Int_t nFound   = 0, // number of tracks found
+        nBacked  = 0, // number of tracks backed up for refit
+        nSeeds   = 0, // total number of ESD seeds
+        nTRDseeds= 0, // number of seeds in the TRD acceptance
+        nTPCseeds= 0; // number of TPC seeds
   Float_t foundMin = 20.0;
-  const Char_t *seeder[] = {"ITS", "TPC"};
-  Float_t *quality[nSeeders] = {0};
-  Int_t   *index[nSeeders]   = {0},
-          *idxESD[nSeeders] = {0};    // esd index of track
+  
+  Float_t *quality = NULL;
+  Int_t   *index   = NULL;
   fEventInFile  = event->GetEventNumberInFile();
   nSeeds   = event->GetNumberOfTracks();
   // Sort tracks according to quality 
   // (covariance in the yz plane)
   if(nSeeds){  
-    // first check TPC seeds
-    quality[1] = new Float_t[nSeeds]; memset(quality[1], 0, nSeeds*sizeof(Float_t));
-    idxESD[1]  = new Int_t[nSeeds];
-    for (Int_t iSeed(nSeeds); iSeed--;) {
-      AliESDtrack *seed = event->GetTrack(iSeed);
-      if(!(seed->GetStatus()&AliESDtrack::kTPCout)) continue;
-      Double_t covariance[15];
-      seed->GetExternalCovariance(covariance);
-      idxESD[1][nSeeder[1]]  = iSeed;
-      quality[1][nSeeder[1]] = covariance[0] + covariance[2];
-      nSeeder[1]++;
-    }
-    index[1]  = new Int_t[4*nSeeder[1]];
-    TMath::Sort(nSeeder[1], quality[1], index[1], kFALSE);
-    // second check rest of tracks for ITS seeds
-    quality[0] = new Float_t[nSeeds]; memset(quality[0], 0, nSeeds*sizeof(Float_t));
-    idxESD[0]  = new Int_t[nSeeds];
-    for (Int_t iSeed(nSeeds); iSeed--;) {
+    quality = new Float_t[nSeeds];
+    index   = new Int_t[4*nSeeds];
+    for (Int_t iSeed = nSeeds; iSeed--;) {
       AliESDtrack *seed = event->GetTrack(iSeed);
-      if((seed->GetStatus()&AliESDtrack::kTPCout)) continue;
       Double_t covariance[15];
       seed->GetExternalCovariance(covariance);
-      idxESD[0][nSeeder[0]]  = iSeed;
-      quality[0][nSeeder[0]] = covariance[0] + covariance[2];
-      nSeeder[0]++;
+      quality[iSeed] = covariance[0] + covariance[2];
     }
-    index[0]  = new Int_t[4*nSeeder[0]];
-    TMath::Sort(nSeeder[0], quality[0], index[0], kFALSE);
+    TMath::Sort(nSeeds, quality, index,kFALSE);
   }
-
+  
   // Propagate all seeds
   Int_t   expectedClr;
   AliTRDtrackV1 track;
-  for(Int_t iSeeder(nSeeders); iSeeder--;){
-    for (Int_t iSeed = 0; iSeed < nSeeder[iSeeder]; iSeed++) {
-      Int_t currentIndexESD = idxESD[iSeeder][index[iSeeder][iSeed]];
-      // Get the seeds in sorted sequence
-      AliESDtrack *seed = event->GetTrack(currentIndexESD);
-      Float_t p4  = seed->GetC(seed->GetBz());
-
-      // Check the seed status
-      ULong_t status(seed->GetStatus()); Bool_t kTPC(kTRUE);
-      if ((status & AliESDtrack::kTRDout) != 0) continue;
-      if (status & AliESDtrack::kTPCout){
-        AliDebug(2, Form("Found TPC seed @ idx[%4d] Sxy=%f[cm].", currentIndexESD, TMath::Sqrt(quality[iSeeder][index[iSeeder][iSeed]])));
-        // set steering parameters for TPC
-        //fkRecoParam->SetTrackParam(kTPC);
-      } else {
-        if (status & AliESDtrack::kITSout){
-          // rotate
-          Float_t  globalToTracking = AliTRDgeometry::GetAlpha()*(Int_t(seed->GetAlpha()/AliTRDgeometry::GetAlpha()) + (seed->GetAlpha()>0. ? 0.5 : -0.5));
-          AliDebug(2, Form("Found ITS seed @ idx[%4d]  Sxy=%f[cm] alpha=%7.2f[deg] g2T=%7.2f[deg]", currentIndexESD, TMath::Sqrt(quality[iSeeder][index[iSeeder][iSeed]]), seed->GetAlpha()*TMath::RadToDeg(), globalToTracking*TMath::RadToDeg()));
-          if(!seed->Rotate(globalToTracking)){
-            AliDebug(1, Form("ITS seed @ idx[%4d] failed rotation of alpha=%7.2f[deg] g2T=%7.2f[deg].", currentIndexESD, seed->GetAlpha()*TMath::RadToDeg(), globalToTracking*TMath::RadToDeg()));
-            continue;
-          }
-          kTPC=kFALSE;
-          // set steering parameters for ITS
-          //fkRecoParam->SetTrackParam(kITS);
-        } else {
-          AliDebug(1, Form("Bad seed[%4d] ITSin[%c] ITSout[%c] TPCin[%c] TPCout[%c].", currentIndexESD,
-            (status&AliESDtrack::kITSin)?'y':'n', (status&AliESDtrack::kITSout)?'y':'n', (status&AliESDtrack::kTPCin)?'y':'n', (status&AliESDtrack::kTPCout)?'y':'n'));
-          continue;
-        }
-      }
-
-      // Propagate to the entrance in the TRD mother volume
-      track.~AliTRDtrackV1();
-      new(&track) AliTRDtrackV1(*seed);
-      if(AliTRDgeometry::GetXtrdBeg() > (AliTRDReconstructor::GetMaxStep() + track.GetX()) && !PropagateToX(track, AliTRDgeometry::GetXtrdBeg(), AliTRDReconstructor::GetMaxStep())){
-        //seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
-        continue;
-      }
-      if(!AdjustSector(&track)){
-        //seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
-        continue;
-      }
-      if(TMath::Abs(track.GetSnp()) > AliTRDReconstructor::GetMaxSnp()) {
-        //seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
-        continue;
-      }
-      AliDebug(2, Form("Propagate %s seed[%4d] ESDidx[%4d].", seeder[iSeeder], iSeed, currentIndexESD));
-      nGoodSeeds[iSeeder]++;
-      // store track status at TRD entrance
-      seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
-
-      // prepare track and do propagation in the TRD
-      track.SetReconstructor(fkReconstructor);
-      track.SetKink(Bool_t(seed->GetKinkIndex(0)));
-      track.SetPrimary(status & AliESDtrack::kTPCin);
-      track.SetNonTPCseeded(!kTPC);
-      expectedClr = FollowBackProlongation(track);
+  for (Int_t iSeed = 0; iSeed < nSeeds; iSeed++) {
+  
+    // Get the seeds in sorted sequence
+    AliESDtrack *seed = event->GetTrack(index[iSeed]);
+    Float_t p4  = seed->GetC(seed->GetBz());
+  
+    // Check the seed status
+    ULong_t status = seed->GetStatus();
+    if ((status & AliESDtrack::kTRDout) != 0) continue;
+    if ((status & AliESDtrack::kTPCout)){
+      AliDebug(3, Form("Prolongate seed[%2d] which is TPC.", iSeed));
+      // set steering parameters for TPC
+      //fkRecoParam->SetTrackParam(kTPC);
+/*    } else {
+      if ((status & AliESDtrack::kITSout)){
+        AliDebug(3, Form("Prolongate seed[%2d] which is ITS.", iSeed));
+        // set steering parameters for ITS
+        //fkRecoParam->SetTrackParam(kITS);
+        // rotate
+        Float_t  globalToTracking = AliTRDgeometry::GetAlpha()*(Int_t(seed->GetAlpha()/AliTRDgeometry::GetAlpha()) + (seed->GetAlpha()>0. ? 0.5 : -0.5));
+        if(!seed->Rotate(globalToTracking)) continue;
+      } else continue;*/
+    } else continue;
+
+    // Propagate to the entrance in the TRD mother volume
+    track.~AliTRDtrackV1();
+    new(&track) AliTRDtrackV1(*seed);
+    if(AliTRDgeometry::GetXtrdBeg() > (AliTRDReconstructor::GetMaxStep() + track.GetX()) && !PropagateToX(track, AliTRDgeometry::GetXtrdBeg(), AliTRDReconstructor::GetMaxStep())){
+      seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+      continue;
+    }    
+    if(!AdjustSector(&track)){
+      seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+      continue;
+    }
+    if(TMath::Abs(track.GetSnp()) > AliTRDReconstructor::GetMaxSnp()) {
+      seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+      continue;
+    }
+    nTPCseeds++;
+    AliDebug(2, Form("TRD propagate TPC seed[%d] = %d.", iSeed, index[iSeed]));
+    // store track status at TRD entrance
+    seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
+
+    // prepare track and do propagation in the TRD
+    track.SetReconstructor(fkReconstructor);
+    track.SetKink(Bool_t(seed->GetKinkIndex(0)));
+    track.SetPrimary(status & AliESDtrack::kTPCin);
+    expectedClr = FollowBackProlongation(track);
+    // check if track entered the TRD fiducial volume
+    if(track.GetTrackIn()){ 
+      seed->UpdateTrackParams(&track, AliESDtrack::kTRDin);
+      nTRDseeds++;
+    }
+    // check if track was stopped in the TRD
+    if (expectedClr<0){      
+      seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+      continue;
+    } else {
+      nFound++;  
+      // compute PID
+      track.CookPID();
+      //compute MC label
+      track.CookLabel(1. - AliTRDReconstructor::GetLabelFraction());
+      // update calibration references using this track
+      if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(&track);
       // save calibration object
-      if (fkRecoParam->GetStreamLevel(AliTRDrecoParam::kTracker) > 0) {
+      if (fkRecoParam->GetStreamLevel(AliTRDrecoParam::kTracker) > 0) { 
         AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(track);
         calibTrack->SetOwner();
         seed->AddCalibObject(calibTrack);
       }
-  //    printf("track idx[%4d] nCl[%3d] TrkIn[%p]\n", index[iSeed], expectedClr, (void*)track.GetTrackIn());
-
-      // check if track entered the TRD fiducial volume
-      if(!track.GetTrackIn()){
-        AliDebug(2, Form("TRD missed by %s seed[%4d] ESDidx[%4d].", seeder[iSeeder], iSeed, currentIndexESD));
-        continue;
-      }
-      seed->UpdateTrackParams(&track, AliESDtrack::kTRDin);
-      nTRDseeds++;
-
-      // check if track was stopped in the TRD
-      if(expectedClr==-999){
-        AliDebug(1, Form("Track @ idx[%4d] failed prolongation due to track error [%d]", currentIndexESD, track.GetStatusTRD()));
-        continue;
-      } else if(expectedClr==0){
-        AliDebug(1, Form("Track @ idx[%4d] failed prolongation due to layer error [%d %d %d %d %d %d]", currentIndexESD,
-          track.GetStatusTRD(0), track.GetStatusTRD(1), track.GetStatusTRD(2), track.GetStatusTRD(3), track.GetStatusTRD(4), track.GetStatusTRD(5)));
-        continue;
-      } else {
-        AliDebug(2, Form("Track @ idx[%4d] #cl[%3d]", currentIndexESD, track.GetNumberOfClusters()));
-        nFound[kTPC]++;
-        // compute PID
-        track.CookPID();
-        //compute MC label
-        track.CookLabel(1. - AliTRDReconstructor::GetLabelFraction());
-        // update calibration references using this track
-        if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(&track);
-        //update ESD track
-        seed->UpdateTrackParams(&track, expectedClr>0?AliESDtrack::kTRDout:AliESDtrack::kTRDStop);
-        seed->SetTRDBudget(track.GetBudget(0));
-        track.UpdateESDtrack(seed);
-        if(expectedClr<0) continue;
-      }
-
-      // Make backup for back propagation
-      if ((TMath::Abs(track.GetC(track.GetBz()) - p4) / TMath::Abs(p4) < 0.2) || (track.Pt() > 0.8)) {
-        Int_t foundClr = track.GetNumberOfClusters();
-        if (foundClr >= foundMin) {
-          //if(track.GetBackupTrack()) UseClusters(track.GetBackupTrack());
+      //update ESD track
+      seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
+      track.UpdateESDtrack(seed);
+    }
 
-          // Sign only gold tracks
-          if (track.GetChi2() / track.GetNumberOfClusters() < 4) {
-            //if ((seed->GetKinkIndex(0)      ==   0) && (track.Pt() <  1.5)) UseClusters(&track);
-          }
-          Bool_t isGold = kFALSE;
+    // Make backup for back propagation
+    if ((TMath::Abs(track.GetC(track.GetBz()) - p4) / TMath::Abs(p4) < 0.2) || (track.Pt() > 0.8)) {
+      Int_t foundClr = track.GetNumberOfClusters();
+      if (foundClr >= foundMin) {
+        //if(track.GetBackupTrack()) UseClusters(track.GetBackupTrack());
 
-          // Full gold track
-          if (track.GetChi2() / track.GetNumberOfClusters() < 5) {
-            if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
-            nBacked++;
-            isGold = kTRUE;
-          }
-
-          // Almost gold track
-          if ((!isGold)  && (track.GetNCross() == 0) &&        (track.GetChi2() / track.GetNumberOfClusters()  < 7)) {
-            //seed->UpdateTrackParams(track, AliESDtrack::kTRDbackup);
-            if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
+        // Sign only gold tracks
+        if (track.GetChi2() / track.GetNumberOfClusters() < 4) {
+          //if ((seed->GetKinkIndex(0)      ==   0) && (track.Pt() <  1.5)) UseClusters(&track);
+        }
+        Bool_t isGold = kFALSE;
+  
+        // Full gold track
+        if (track.GetChi2() / track.GetNumberOfClusters() < 5) {
+          if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
+          nBacked++;
+          isGold = kTRUE;
+        }
+  
+        // Almost gold track
+        if ((!isGold)  && (track.GetNCross() == 0) &&  (track.GetChi2() / track.GetNumberOfClusters()  < 7)) {
+          //seed->UpdateTrackParams(track, AliESDtrack::kTRDbackup);
+          if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
+          nBacked++;
+          isGold = kTRUE;
+        }
+        
+        if ((!isGold) && (track.GetBackupTrack())) {
+          if ((track.GetBackupTrack()->GetNumberOfClusters() > foundMin) && ((track.GetBackupTrack()->GetChi2()/(track.GetBackupTrack()->GetNumberOfClusters()+1)) < 7)) {
+            seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
             nBacked++;
             isGold = kTRUE;
           }
-
-          if ((!isGold) && (track.GetBackupTrack())) {
-            if ((track.GetBackupTrack()->GetNumberOfClusters() > foundMin) && ((track.GetBackupTrack()->GetChi2()/(track.GetBackupTrack()->GetNumberOfClusters()+1)) < 7)) {
-              seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
-              nBacked++;
-              isGold = kTRUE;
-            }
-          }
         }
       }
-
-      // Propagation to the TOF
-      if(!(seed->GetStatus()&AliESDtrack::kTRDStop)) {
-        Int_t sm = track.GetSector();
-        // default value in case we have problems with the geometry.
-        Double_t xtof  = 371.;
-        //Calculate radial position of the beginning of the TOF
-        //mother volume. In order to avoid mixing of the TRD
-        //and TOF modules some hard values are needed. This are:
-        //1. The path to the TOF module.
-        //2. The width of the TOF (29.05 cm)
-        //(with the help of Annalisa de Caro Mar-17-2009)
-        if(gGeoManager){
-          gGeoManager->cd(Form("/ALIC_1/B077_1/BSEGMO%d_1/BTOF%d_1", sm, sm));
-          TGeoHMatrix *m = NULL;
-          Double_t loc[]={0., 0., -.5*29.05}, glob[3];
-
-          if((m=gGeoManager->GetCurrentMatrix())){
-            m->LocalToMaster(loc, glob);
-            xtof = TMath::Sqrt(glob[0]*glob[0]+glob[1]*glob[1]);
-          }
-        }
-        if(xtof > (AliTRDReconstructor::GetMaxStep() + track.GetX()) && !PropagateToX(track, xtof, AliTRDReconstructor::GetMaxStep())){
-          seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
-          continue;
-        }
-        if(!AdjustSector(&track)){
-          seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
-          continue;
-        }
-        if(TMath::Abs(track.GetSnp()) > AliTRDReconstructor::GetMaxSnp()){
-          seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
-          continue;
+    }
+    
+    // Propagation to the TOF
+    if(!(seed->GetStatus()&AliESDtrack::kTRDStop)) {
+      Int_t sm = track.GetSector();
+      // default value in case we have problems with the geometry.
+      Double_t xtof  = 371.; 
+      //Calculate radial position of the beginning of the TOF
+      //mother volume. In order to avoid mixing of the TRD 
+      //and TOF modules some hard values are needed. This are:
+      //1. The path to the TOF module.
+      //2. The width of the TOF (29.05 cm)
+      //(with the help of Annalisa de Caro Mar-17-2009)
+      if(gGeoManager){
+        gGeoManager->cd(Form("/ALIC_1/B077_1/BSEGMO%d_1/BTOF%d_1", sm, sm));
+        TGeoHMatrix *m = NULL;
+        Double_t loc[]={0., 0., -.5*29.05}, glob[3];
+        
+        if((m=gGeoManager->GetCurrentMatrix())){
+          m->LocalToMaster(loc, glob);
+          xtof = TMath::Sqrt(glob[0]*glob[0]+glob[1]*glob[1]);
         }
-        //seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
-        // TODO obsolete - delete
-        seed->SetTRDQuality(track.StatusForTOF());
       }
+      if(xtof > (AliTRDReconstructor::GetMaxStep() + track.GetX()) && !PropagateToX(track, xtof, AliTRDReconstructor::GetMaxStep())){
+        seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+        continue;
+      }
+      if(!AdjustSector(&track)){ 
+        seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+        continue;
+      }
+      if(TMath::Abs(track.GetSnp()) > AliTRDReconstructor::GetMaxSnp()){
+        seed->UpdateTrackParams(&track, AliESDtrack::kTRDStop);
+        continue;
+      }
+      //seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
+      // TODO obsolete - delete
+      seed->SetTRDQuality(track.StatusForTOF()); 
     }
-    if(index[iSeeder]) delete [] index[iSeeder];
-    if(idxESD[iSeeder]) delete [] idxESD[iSeeder];
-    if(quality[iSeeder]) delete [] quality[iSeeder];
+    seed->SetTRDBudget(track.GetBudget(0));
   }
+  if(index) delete [] index;
+  if(quality) delete [] quality;
 
-  AliInfo(Form("Number of seeds: ITSout[%4d] TPCout[%4d] TRDin[%4d]", nGoodSeeds[0], nGoodSeeds[1], nTRDseeds));
-  AliInfo(Form("Number of tracks: TRDout[%4d = %4d + %4d] TRDbackup[%4d]", nFound[0]+nFound[1], nFound[0], nFound[1], nBacked));
+  AliInfo(Form("Number of seeds: TPCout[%d] TRDin[%d]", nTPCseeds, nTRDseeds));
+  AliInfo(Form("Number of tracks: TRDout[%d] TRDbackup[%d]", nFound, nBacked));
 
   // run stand alone tracking
   if (fkReconstructor->IsSeeding()) Clusters2Tracks(event);
@@ -557,7 +513,7 @@ Int_t AliTRDtrackerV1::RefitInward(AliESDEvent *event)
 
     // do the propagation and processing
     Bool_t kUPDATE = kFALSE;
-    Double_t xToGo = (status & AliESDtrack::kTPCout)?250.0:80.0;
+    Double_t xTPC = 250.0;
     if(FollowProlongation(track)){     
       // Update the friend track
       if (fkRecoParam->GetStreamLevel(AliTRDrecoParam::kTracker) > 0){ 
@@ -628,10 +584,8 @@ Int_t AliTRDtrackerV1::FollowProlongation(AliTRDtrackV1 &t)
     }
     Double_t x  = tracklet->GetX();//GetX0();
     // reject tracklets which are not considered for inward refit
-    if(x > t.GetX()+AliTRDReconstructor::GetMaxStep()){
-      AliDebug(2, Form("Reject x[%7.3f] > track[%7.3f]", x, t.GetX()+AliTRDReconstructor::GetMaxStep()));
-      continue;
-    }
+    if(x > t.GetX()+AliTRDReconstructor::GetMaxStep()) continue;
+
     // append tracklet to track
     t.SetTracklet(tracklet, index);
     
@@ -670,6 +624,24 @@ Int_t AliTRDtrackerV1::FollowProlongation(AliTRDtrackV1 &t)
     Double_t cov[3]; tracklet->GetCovAt(x, cov);
     Double_t p[2] = { tracklet->GetY(), tracklet->GetZ()};
     Double_t chi2 = ((AliExternalTrackParam)t).GetPredictedChi2(p, cov);
+    if(fkReconstructor->IsDebugStreaming()){
+      Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
+      TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
+      AliExternalTrackParam param0(t);
+      AliExternalTrackParam param1(t);
+      param1.Update(p, cov);
+      TVectorD vcov(3,cov);
+      TVectorD vpar(3,p);
+      cstreamer << "FollowProlongationInfo"
+               << "EventNumber="       << eventNumber
+               << "iplane="<<iplane
+               << "vcov.="<<&vcov
+               << "vpar.="<<&vpar
+               << "tracklet.="      << tracklet
+               << "param0.="           << &param0
+               << "param1.="           << &param1
+               << "\n";
+    }
     if (chi2 < 1e+10 && ((AliExternalTrackParam&)t).Update(p, cov)){ 
       // Register info to track
       t.SetNumberOfClusters();
@@ -792,7 +764,7 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
 
     // rough estimate of the entry point
     if (!t.GetProlongation(fR[ily], y, z)){
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kProlongation);
       AliDebug(4, Form("Failed Rough Prolongation to ly[%d] x[%7.2f] y[%7.2f] z[%7.2f]", ily, fR[ily], y, z));
       break;
@@ -813,19 +785,19 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
       AliDebug(4, Form("Missing Geometry ly[%d]. Guess radial position", ily));
       // propagate to the default radial position
       if(fR[ily] > (AliTRDReconstructor::GetMaxStep() + t.GetX()) && !PropagateToX(t, fR[ily], AliTRDReconstructor::GetMaxStep())){
-        n= n?-n:-999;
+        n=-1; 
         t.SetErrStat(AliTRDtrackV1::kPropagation);
         AliDebug(4, "Failed Propagation [Missing Geometry]");
         break;
       }
       if(!AdjustSector(&t)){
-        n= n?-n:-999;
+        n=-1; 
         t.SetErrStat(AliTRDtrackV1::kAdjustSector);
         AliDebug(4, "Failed Adjust Sector [Missing Geometry]");
         break;
       }
       if(TMath::Abs(t.GetSnp()) > AliTRDReconstructor::GetMaxSnp()){
-        n= n?-n:-999;
+        n=-1; 
         t.SetErrStat(AliTRDtrackV1::kSnp);
         AliDebug(4, "Failed Max Snp [Missing Geometry]");
         break;
@@ -838,36 +810,34 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
     Double_t loc[] = {AliTRDgeometry::AnodePos()- driftLength, 0., 0.};
     Double_t glb[] = {0., 0., 0.};
     matrix->LocalToMaster(loc, glb);
-    AliDebug(3, Form("Propagate to D%3d[%02d_%d_%d] x_anode[%7.2f] (%f %f)", det, sm, stk, ily, glb[0]+driftLength, glb[1], glb[2]));
+    AliDebug(3, Form("Propagate to det[%3d] x_anode[%7.2f] (%f %f)", det, glb[0]+driftLength, glb[1], glb[2]));
 
     // Propagate to the radial distance of the current layer
     x = glb[0] - AliTRDReconstructor::GetMaxStep();
     if(x > (AliTRDReconstructor::GetMaxStep() + t.GetX()) && !PropagateToX(t, x, AliTRDReconstructor::GetMaxStep())){
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kPropagation);
-      AliDebug(4, Form("Failed Propagation to x[%7.2f]", x));
+      AliDebug(4, Form("Failed Initial Propagation to x[%7.2f]", x));
       break;
     }
     if(!AdjustSector(&t)){
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kAdjustSector);
       AliDebug(4, "Failed Adjust Sector Start");
       break;
     }
     if(TMath::Abs(t.GetSnp()) > AliTRDReconstructor::GetMaxSnp()) {
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kSnp);
       AliDebug(4, Form("Failed Max Snp[%f] MaxSnp[%f]", t.GetSnp(), AliTRDReconstructor::GetMaxSnp()));
       break;
     }
     Bool_t doRecalculate = kFALSE;
     if(sm != t.GetSector()){
-      AliDebug(3, Form("Track crossed sectors %2d -> %2d", sm, t.GetSector()));
       sm = t.GetSector(); 
       doRecalculate = kTRUE;
     }
     if(stk != fGeom->GetStack(z, ily)){
-      AliDebug(3, Form("Track crossed stacks %d -> %d in sec[%02d]", stk, fGeom->GetStack(z, ily), sm));
       stk = fGeom->GetStack(z, ily);
       doRecalculate = kTRUE;
     }
@@ -884,7 +854,7 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
 
     // check if track is well inside fiducial volume 
     if (!t.GetProlongation(x+AliTRDReconstructor::GetMaxStep(), y, z)) {
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kProlongation);
       AliDebug(4, Form("Failed Prolongation to x[%7.2f] y[%7.2f] z[%7.2f]", x+AliTRDReconstructor::GetMaxStep(), y, z));
       break;
@@ -894,11 +864,6 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
       AliDebug(4, "Failed Track on Boundary");
       continue;
     }
-    // mark track as entering the FIDUCIAL volume of TRD
-    if(kStoreIn){
-      t.SetTrackIn();
-      kStoreIn = kFALSE;
-    }
 
     ptrTracklet  = tracklets[ily];
     if(!ptrTracklet){ // BUILD TRACKLET
@@ -906,24 +871,24 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
       // check data in supermodule
       if(!fTrSec[sm].GetNChambers()){ 
         t.SetErrStat(AliTRDtrackV1::kNoClusters, ily);
-        AliDebug(4, Form("Failed NoClusters in sec[%02d]", sm));
+        AliDebug(4, "Failed NoClusters");
         continue;
       }
       if(fTrSec[sm].GetX(ily) < 1.){ 
         t.SetErrStat(AliTRDtrackV1::kNoClusters, ily);
-        AliDebug(4, Form("Failed NoX in sec[%02d]", sm));
+        AliDebug(4, "Failed NoX");
         continue;
       }
       
       // check data in chamber
       if(!(chamber = fTrSec[sm].GetChamber(stk, ily))){ 
         t.SetErrStat(AliTRDtrackV1::kNoClusters, ily);
-        AliDebug(4, Form("Failed No Data for D%03d[%02d_%d_%d]", AliTRDgeometry::GetDetector(ily, stk, sm), sm, stk, ily));
+        AliDebug(4, "Failed No Detector");
         continue;
       }
       if(chamber->GetNClusters() < fgNTimeBins*fkRecoParam ->GetFindableClusters()){ 
         t.SetErrStat(AliTRDtrackV1::kNoClusters, ily);
-        AliDebug(4, Form("Failed Not Enough Clusters[%2d] for D%03d[%02d_%d_%d]", chamber->GetNClusters(), AliTRDgeometry::GetDetector(ily, stk, sm), sm, stk, ily));
+        AliDebug(4, "Failed Not Enough Clusters in Detector");
         continue;
       }      
       // build tracklet
@@ -935,7 +900,7 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
       ptrTracklet->SetPadPlane(fGeom->GetPadPlane(ily, stk));
       ptrTracklet->SetX0(glb[0]+driftLength);
       if(!ptrTracklet->Init(&t)){
-        n= n?-n:-999;
+        n=-1; 
         t.SetErrStat(AliTRDtrackV1::kTrackletInit);
         AliDebug(4, "Failed Tracklet Init");
         break;
@@ -957,9 +922,8 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
         AliDebug(4, "Failed Attach Clusters");
         continue;
       }
-      Int_t nClTracklet(ptrTracklet->GetN());
-      AliDebug(3, Form("Number of Clusters in Tracklet: %2d", nClTracklet));
-      if(nClTracklet < Int_t(fgNTimeBins*fkRecoParam->GetFindableClusters())){
+      AliDebug(3, Form("Number of Clusters in Tracklet: %d", ptrTracklet->GetN()));
+      if(ptrTracklet->GetN() < fgNTimeBins*fkRecoParam->GetFindableClusters()){
         t.SetErrStat(AliTRDtrackV1::kNoClustersTracklet, ily);
         if(debugLevel>3){
           AliTRDseedV1 trackletCp(*ptrTracklet);
@@ -969,16 +933,10 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
           <<"tracklet.=" << &trackletCp
           << "\n";
         }
-        AliDebug(4, Form("Attached clusters %2d<%2d (%4.1f%% of TB[%2d])", nClTracklet, Int_t(fgNTimeBins*fkRecoParam->GetFindableClusters()), 1.e2*fkRecoParam->GetFindableClusters(), fgNTimeBins));
+        AliDebug(4, "Failed N Clusters Attached");
         continue;
       }
       ptrTracklet->UpdateUsed();
-      // protect against reattaching clusters
-      if(Float_t(ptrTracklet->GetNUsed())/nClTracklet > .5/*fkRecoParam->GetUsedClustersLimit()*/){
-        t.SetErrStat(AliTRDtrackV1::kNoClustersTracklet, ily);
-        AliDebug(4, Form("Used clusters %2d[%4.1f%%] exceed limit [%4.1f%%]", nClTracklet, Float_t(ptrTracklet->GetNUsed())/nClTracklet, 1.e2*.5/*fkRecoParam->GetUsedClustersLimit()*/));
-        continue;
-      }
     } else AliDebug(2, Form("Use external tracklet ly[%d]", ily));
     // propagate track to the radial position of the tracklet
 
@@ -993,25 +951,23 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
     } 
     x = ptrTracklet->GetX(); //GetX0();
     if(x > (AliTRDReconstructor::GetMaxStep() + t.GetX()) && !PropagateToX(t, x, AliTRDReconstructor::GetMaxStep())) {
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kPropagation);
       AliDebug(4, Form("Failed Propagation to Tracklet x[%7.2f]", x));
       break;
     }
     if(!AdjustSector(&t)) {
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kAdjustSector);
       AliDebug(4, "Failed Adjust Sector");
       break;
     }
     if(TMath::Abs(t.GetSnp()) > AliTRDReconstructor::GetMaxSnp()) {
-      n= n?-n:-999;
+      n=-1; 
       t.SetErrStat(AliTRDtrackV1::kSnp);
       AliDebug(4, Form("Failed Max Snp[%f] MaxSnp[%f]", t.GetSnp(), AliTRDReconstructor::GetMaxSnp()));
       break;
     }
-    // update parameters of track in.
-    if(!ily) t.SetTrackIn();
     Double_t cov[3]; ptrTracklet->GetCovAt(x, cov);
     Double_t p[2] = { ptrTracklet->GetY(), ptrTracklet->GetZ()};
     Double_t chi2 = ((AliExternalTrackParam)t).GetPredictedChi2(p, cov);
@@ -1031,10 +987,35 @@ Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
       }
       AliDebug(4, Form("Failed Chi2[%f]", chi2));
       continue; 
+    }
+     if(fkReconstructor->IsDebugStreaming()){
+      Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
+      //      TTreeSRedirector &cstreamer = *fkReconstructor->GetDebugStream(AliTRDrecoParam::kTracker);
+      AliExternalTrackParam param0(t);
+      AliExternalTrackParam param1(t);
+      param1.Update(p, cov);
+      TVectorD vcov(3,cov);
+      TVectorD vpar(3,p);
+      (*cstreamer) << "FollowBackProlongationInfo"
+               << "EventNumber="       << eventNumber
+               << "chi2="<<chi2
+               << "iplane="<<ily
+               << "vcov.="<<&vcov
+               << "vpar.="<<&vpar
+               << "tracklet.="      << ptrTracklet
+               << "param0.="           << &param0
+               << "param1.="           << &param1
+               << "\n";
+    }
+
+    // mark track as entering the FIDUCIAL volume of TRD
+    if(kStoreIn){
+      t.SetTrackIn();
+      kStoreIn = kFALSE;
     }
     if(kUseTRD){
       if(!((AliExternalTrackParam&)t).Update(p, cov)) {
-        n= n?-n:-999;
+        n=-1; 
         t.SetErrStat(AliTRDtrackV1::kUpdate);
         if(debugLevel > 2){
           UChar_t status(t.GetStatusTRD());
@@ -1395,13 +1376,13 @@ Float_t AliTRDtrackerV1::FitTiltedRieman(AliTRDseedV1 *tracklets, Bool_t sigErro
   covarPolZ.Invert();
 
   // Update the tracklets
-  Double_t x1, dy, dz;
+  Double_t dy, dz;
   Double_t cov[15];
   memset(cov, 0, sizeof(Double_t) * 15);
   for(Int_t iLayer = 0; iLayer < AliTRDtrackerV1::kNPlanes; iLayer++) {
 
     x  = tracklets[iLayer].GetX0();
-    x1 = x - xref;
+//    x1 = x - xref;
     y  = 0;
     z  = 0;
     dy = 0;
@@ -1924,7 +1905,7 @@ Int_t AliTRDtrackerV1::PropagateToX(AliTRDtrackV1 &t, Double_t xToGo, Double_t m
 
 /*    // Correct for mean material budget
     Double_t dEdx(0.),
-             bg(t.GetP()/mass);
+             bg(TMath::Abs(t.GetP()/mass));
     if(AliLog::GetDebugLevel("TRD", "AliTRDtrackerV1")>=3){
       const char *pn[] = {"rho", "x/X0", "<A>", "<Z>", "L", "<Z/A>", "Nb"};
       printf("D-AliTRDtrackerV1::PropagateTo(): x[%6.2f] bg[%6.2f]\n", xpos, bg);
@@ -1987,12 +1968,14 @@ Bool_t AliTRDtrackerV1::ReadClusters(TTree *clusterTree)
     return kFALSE;
   }
   branch->SetAddress(&clusterArray); 
-  
+
   if(!fClusters){ 
     Float_t nclusters =  fkRecoParam->GetNClusters();
     if(fkReconstructor->IsHLT()) nclusters /= AliTRDgeometry::kNsector;
     fClusters = new TClonesArray("AliTRDcluster", Int_t(nclusters));
     fClusters->SetOwner(kTRUE);
+    SetClustersOwner();
+    AliInfo(Form("Tracker owning clusters @ %p", (void*)fClusters));
   }
   
   // Loop through all entries in the tree
@@ -2026,27 +2009,26 @@ Int_t AliTRDtrackerV1::LoadClusters(TTree *cTree)
   
   fkRecoParam = fkReconstructor->GetRecoParam(); // load reco param for this event
 
-  if(!fkReconstructor->IsWritingClusters()){ 
-    fClusters = AliTRDReconstructor::GetClusters();
+//  if(!fkReconstructor->IsWritingClusters()) AliInfo(Form("IsWritingClusters[%c]", fkReconstructor->IsWritingClusters()?'y':'n'));
+  if(!(fClusters = AliTRDReconstructor::GetClusters())){
+    AliWarning("Clusters unavailable from TRD reconstructor. Trying reading from tree ...");
   } else {
     if(!ReadClusters(cTree)) {
-      AliError("Problem with reading the clusters !");
+      AliError("Reading clusters from tree failed.");
       return 1;
     }
   }
-  SetClustersOwner();
 
   if(!fClusters || !fClusters->GetEntriesFast()){ 
     AliInfo("No TRD clusters");
     return 1;
-  }
+  } else AliInfo(Form("Using :: clusters[%d] onl.tracklets[%d] onl.tracks[%d]",
+    fClusters?fClusters->GetEntriesFast():0,
+    AliTRDReconstructor::GetTracklets()?AliTRDReconstructor::GetTracklets()->GetEntriesFast():0,
+    AliTRDReconstructor::GetTracks()?AliTRDReconstructor::GetTracks()->GetEntriesFast():0));
 
-  //Int_t nin = 
   BuildTrackingContainers();  
 
-  //Int_t ncl  = fClusters->GetEntriesFast();
-  //AliInfo(Form("Clusters %d [%6.2f %% in the active volume]", ncl, 100.*float(nin)/ncl));
-
   return 0;
 }
 
@@ -2060,17 +2042,14 @@ Int_t AliTRDtrackerV1::LoadClusters(TClonesArray * const clusters)
   if(!clusters || !clusters->GetEntriesFast()){ 
     AliInfo("No TRD clusters");
     return 1;
-  }
+  } else AliInfo(Form("Using :: external.clusters[%d]", clusters->GetEntriesFast()));
+
 
   fClusters = clusters;
-  SetClustersOwner();
 
   fkRecoParam = fkReconstructor->GetRecoParam(); // load reco param for this event
   BuildTrackingContainers();  
 
-  //Int_t ncl  = fClusters->GetEntriesFast();
-  //AliInfo(Form("Clusters %d [%6.2f %% in the active volume]", ncl, 100.*float(nin)/ncl));
-
   return 0;
 }
 
@@ -2119,14 +2098,15 @@ void AliTRDtrackerV1::UnloadClusters()
     fTracklets->Delete();
     if(HasRemoveContainers()){delete fTracklets; fTracklets = NULL;}
   }
-  if(fClusters){ 
-    if(IsClustersOwner()) fClusters->Delete();
-    
-    // save clusters array in the reconstructor for further use.
-    if(!fkReconstructor->IsWritingClusters()){
-      AliTRDReconstructor::SetClusters(fClusters);
-      SetClustersOwner(kFALSE);
-    } else AliTRDReconstructor::SetClusters(NULL);
+  if(fClusters && IsClustersOwner()){
+    AliInfo(Form("tracker[%p] clearing %d own clusters @ %p", (void*)this, fClusters->GetEntries(), (void*)fClusters));
+    fClusters->Delete();
+//     
+//     // save clusters array in the reconstructor for further use.
+//     if(!fkReconstructor->IsWritingClusters()){
+//       AliTRDReconstructor::SetClusters(fClusters);
+//       SetClustersOwner(kFALSE);
+//     } else AliTRDReconstructor::SetClusters(NULL);
   }
 
   for (int i = 0; i < AliTRDgeometry::kNsector; i++) fTrSec[i].Clear();
@@ -2702,7 +2682,7 @@ Int_t AliTRDtrackerV1::MakeSeeds(AliTRDtrackingChamber **stack, AliTRDseedV1 * c
   Double_t chi2[4];
 
   // this should be data member of AliTRDtrack TODO
-  Double_t seedQuality[kMaxTracksStack];
+//  Double_t seedQuality[kMaxTracksStack];
   
   // unpack control parameters
   Int_t config  = ipar[0];
@@ -2926,8 +2906,8 @@ Int_t AliTRDtrackerV1::MakeSeeds(AliTRDtrackingChamber **stack, AliTRDseedV1 * c
         }
         //AliInfo(Form("Passed likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
       
-        // book preliminary results
-        seedQuality[ntracks] = like;
+        // book preliminry results
+        //seedQuality[ntracks] = like;
         fSeedLayer[ntracks]  = config;/*sLayer;*/
       
         // attach clusters to the extrapolation seeds