]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCtrackerMI.cxx
Removing warnings (alpha)
[u/mrichter/AliRoot.git] / TPC / AliTPCtrackerMI.cxx
index 68cfd0b54c9173b8a92c449b36a44343b97e6f24..f9a9e5898cbbcd3a243d9a93ff586a50e481aec1 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.13  2003/09/29 11:28:19  kowal2
-completly rewritten
-
-Revision 1.9.4.3  2003/06/23 14:47:10  hristov
-Minor fix
-
-Revision 1.9.4.2  2003/06/23 10:06:13  hristov
-Updated information about the overlapping clusters (M.Ivanov)
-
-Revision 1.9.4.1  2003/06/19 06:59:58  hristov
-Updated version of parallel tracking (M.Ivanov)
-
-Revision 1.9  2003/03/19 17:14:11  hristov
-Load/UnloadClusters added to the base class and the derived classes changed correspondingly. Possibility to give 2 input files for ITS and TPC tracks in PropagateBack. TRD tracker uses fEventN from the base class (T.Kuhr)
-
-Revision 1.8  2003/03/05 11:16:15  kowal2
-Logs added
-
-*/
-
-
-
-
-
-
-
-/*
-  AliTPC parallel tracker - 
-  How to use?  - 
-  run AliTPCFindClusters.C macro - clusters neccessary for tracker are founded
-  run AliTPCFindTracksMI.C macro - to find tracks
-  tracks are written to AliTPCtracks.root file
-  for comparison also seeds are written to the same file - to special branch
-*/
 
 //-------------------------------------------------------
 //          Implementation of the TPC tracker
 //
 //   Origin: Marian Ivanov   Marian.Ivanov@cern.ch
 // 
+//  AliTPC parallel tracker - 
+//  How to use?  - 
+//  run AliTPCFindClusters.C macro - clusters neccessary for tracker are founded
+//  run AliTPCFindTracksMI.C macro - to find tracks
+//  tracks are written to AliTPCtracks.root file
+//  for comparison also seeds are written to the same file - to special branch
 //-------------------------------------------------------
+
+
+/* $Id$ */
+
+
 #include <TObjArray.h>
 #include <TFile.h>
 #include <TTree.h>
+#include <TClonesArray.h>
+
 #include "Riostream.h"
 
-#include "AliTPCtrackerMI.h"
 #include "AliTPCclusterMI.h"
+#include "AliComplexCluster.h"
 #include "AliTPCParam.h"
 #include "AliTPCClustersRow.h"
-#include "AliComplexCluster.h"
 #include "AliTPCpolyTrack.h"
 #include "TStopwatch.h"
 #include "AliESD.h"
 #include "AliHelix.h"
-#include "TGraph.h"
 //
 #include "AliRunLoader.h"
+//
+#include "AliTPCreco.h" 
+#include "AliTPCtrackerMI.h"
 
 
 
@@ -80,24 +58,29 @@ ClassImp(AliTPCseed)
 ClassImp(AliTPCtrackerMI)
 
 
-class TPCFastMath {
+class AliTPCFastMath {
 public:
-  TPCFastMath();  
+  AliTPCFastMath();  
   static Double_t FastAsin(Double_t x);   
  private: 
-  static Double_t fgFastAsin[20000];
+  static Double_t fgFastAsin[20000];  //lookup table for fast asin computation
 };
 
-Double_t TPCFastMath::fgFastAsin[20000];
+Double_t AliTPCFastMath::fgFastAsin[20000];
+AliTPCFastMath gAliTPCFastMath;
 
-TPCFastMath::TPCFastMath(){
+AliTPCFastMath::AliTPCFastMath(){
+  //
+  // initialized lookup table;
   for (Int_t i=0;i<10000;i++){
     fgFastAsin[2*i] = TMath::ASin(i/10000.);
     fgFastAsin[2*i+1] = (TMath::ASin((i+1)/10000.)-fgFastAsin[2*i]);
   }
 }
 
-Double_t TPCFastMath::FastAsin(Double_t x){
+Double_t AliTPCFastMath::FastAsin(Double_t x){
+  //
+  // return asin using lookup table
   if (x>0){
     Int_t index = int(x*10000);
     return fgFastAsin[2*index]+(x*10000.-index)*fgFastAsin[2*index+1];
@@ -107,11 +90,13 @@ Double_t TPCFastMath::FastAsin(Double_t x){
   return -(fgFastAsin[2*index]+(x*10000.-index)*fgFastAsin[2*index+1]);
 }
 
-TPCFastMath gTPCMath;
 
 
 
 Int_t AliTPCtrackerMI::UpdateTrack(AliTPCseed * track, Int_t accept){
+  //
+  //update track information using current cluster - track->fCurrentCluster
+
 
   AliTPCclusterMI* c =track->fCurrentCluster;
   if (accept>0) track->fCurrentClusterIndex1 |=0x8000;  //sign not accepted clusters
@@ -124,8 +109,7 @@ Int_t AliTPCtrackerMI::UpdateTrack(AliTPCseed * track, Int_t accept){
   track->fSector = sec;
   //  Int_t index = i&0xFFFF;
   if (sec>=fParam->GetNInnerSector()) track->fRow += fParam->GetNRowLow(); 
-  track->SetClusterIndex2(track->fRow, i);
-  
+  track->SetClusterIndex2(track->fRow, i);  
   //track->fFirstPoint = row;
   //if ( track->fLastPoint<row) track->fLastPoint =row;
   //  if (track->fRow<0 || track->fRow>160) {
@@ -293,14 +277,16 @@ void AliTPCtrackerMI::SetIO()
   //
   fNewIO   =  kTRUE;
   fInput   =  AliRunLoader::GetTreeR("TPC", kFALSE,AliConfig::fgkDefaultEventFolderName);
+  
   fOutput  =  AliRunLoader::GetTreeT("TPC", kTRUE,AliConfig::fgkDefaultEventFolderName);
-  AliTPCtrack *iotrack= new AliTPCtrack;
-  //    iotrack->fHelixIn   = new TClonesArray("AliHelix");
-  //iotrack->fHelixOut  = new TClonesArray("AliHelix");    
-  fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
-  delete iotrack;
+  if (fOutput){
+    AliTPCtrack *iotrack= new AliTPCtrack;
+    fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
+    delete iotrack;
+  }
 }
 
+
 void AliTPCtrackerMI::SetIO(TTree * input, TTree * output, AliESD * event)
 {
 
@@ -349,35 +335,59 @@ void AliTPCtrackerMI::SetIO(TTree * input, TTree * output, AliESD * event)
   fEvent  = event;  
 }
 
-void AliTPCtrackerMI::WriteTracks()
+void AliTPCtrackerMI::FillESD(TObjArray* arr)
 {
   //
-  // write tracks to the given output tree -
-  // output specified with SetIO routine
-  if (!fSeeds)  return;
+  //
+  //fill esds using updated tracks
   if (fEvent){
     // write tracks to the event
     // store index of the track
-    Int_t nseed=fSeeds->GetEntriesFast();
+    Int_t nseed=arr->GetEntriesFast();
     for (Int_t i=0; i<nseed; i++) {
-      AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
+      AliTPCseed *pt=(AliTPCseed*)arr->UncheckedAt(i);    
       if (!pt) continue; 
-      AliESDtrack iotrack;
-      iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);       
-      //iotrack.SetTPCindex(i);
-      fEvent->AddTrack(&iotrack);         
+      pt->PropagateTo(fParam->GetInnerRadiusLow());
+      if (pt->GetNumberOfClusters()>70) {
+       AliESDtrack iotrack;
+       iotrack.UpdateTrackParams(pt,AliESDtrack::kTPCin);      
+       //iotrack.SetTPCindex(i);
+       fEvent->AddTrack(&iotrack);
+      }        
     }
   }
+}
+
+void AliTPCtrackerMI::WriteTracks(TTree * tree)
+{
+  //
+  // write tracks from seed array to selected tree
+  //
+  fOutput  = tree;
+  if (fOutput){
+    AliTPCtrack *iotrack= new AliTPCtrack;
+    fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
+  }
+  WriteTracks();
+}
+
+void AliTPCtrackerMI::WriteTracks()
+{
+  //
+  // write tracks to the given output tree -
+  // output specified with SetIO routine
+  if (!fSeeds)  return;
+  if (!fOutput){
+    SetIO();
+  }
 
-  
   if (fOutput){
     AliTPCtrack *iotrack= 0;
     Int_t nseed=fSeeds->GetEntriesFast();
-    for (Int_t i=0; i<nseed; i++) {
-      iotrack= (AliTPCtrack*)fSeeds->UncheckedAt(i);
-      if (iotrack) break;      
-    }
-    
+    //for (Int_t i=0; i<nseed; i++) {
+    //  iotrack= (AliTPCtrack*)fSeeds->UncheckedAt(i);
+    //  if (iotrack) break;      
+    //}    
     //TBranch * br = fOutput->Branch("tracks","AliTPCtrack",&iotrack,32000,100);
     TBranch * br = fOutput->GetBranch("tracks");
     br->SetAddress(&iotrack);
@@ -385,15 +395,19 @@ void AliTPCtrackerMI::WriteTracks()
     for (Int_t i=0; i<nseed; i++) {
       AliTPCseed *pt=(AliTPCseed*)fSeeds->UncheckedAt(i);    
       if (!pt) continue;    
-      iotrack = pt;
+      AliTPCtrack * track = new AliTPCtrack(*pt);
+      iotrack = track;
       pt->fLab2 =i; 
       //      br->SetAddress(&iotrack);
       fOutput->Fill();
+      delete track;
       iotrack =0;
     }
+    //fOutput->GetDirectory()->cd();
+    //fOutput->Write();
   }
-    // delete iotrack;
-    //
+  // delete iotrack;
+  //
   if (fSeedTree){
     //write the full seed information if specified in debug mode
       
@@ -823,10 +837,10 @@ void AliTPCseed::Modify(Double_t factor)
     return;
   }
   fC00*=factor;
-  fC10*=factor;  fC11*=factor;
-  fC20*=factor;  fC21*=factor;  fC22*=factor;
-  fC30*=factor;  fC31*=factor;  fC32*=factor;  fC33*=factor;
-  fC40*=factor;  fC41*=factor;  fC42*=factor;  fC43*=factor;  fC44*=factor;
+  fC10*=0;  fC11*=factor;
+  fC20*=0;  fC21*=0;  fC22*=factor;
+  fC30*=0;  fC31*=0;  fC32*=0;  fC33*=factor;
+  fC40*=0;  fC41*=0;  fC42*=0;  fC43*=0;  fC44*=factor;
   SetNumberOfClusters(0);
   fNFoundable =0;
   SetChi2(0);
@@ -874,7 +888,7 @@ Int_t  AliTPCseed::GetProlongation(Double_t xk, Double_t &y, Double_t & z) const
     dz = dx*fP3*(c1+c2)/(c1*r2 + c2*r1);
   }
   */
-  dz =  fP3*TPCFastMath::FastAsin(delta)/fP4;
+  dz =  fP3*AliTPCFastMath::FastAsin(delta)/fP4;
   //
   y+=dy;
   z+=dz;
@@ -964,7 +978,7 @@ void AliTPCtrackerMI::RotateToLocal(AliTPCseed *seed)
 
 
 //_____________________________________________________________________________
-Int_t AliTPCseed::Update(const AliTPCclusterMI *c, Double_t chisq, UInt_t index) {
+Int_t AliTPCseed::Update(const AliTPCclusterMI *c, Double_t chisq, UInt_t /*index*/) {
   //-----------------------------------------------------------------
   // This function associates a cluster with this track.
   //-----------------------------------------------------------------
@@ -1022,7 +1036,7 @@ Int_t AliTPCseed::Update(const AliTPCclusterMI *c, Double_t chisq, UInt_t index)
 
 
 //_____________________________________________________________________________
-Double_t AliTPCtrackerMI::f1old(Double_t x1,Double_t y1,
+Double_t AliTPCtrackerMI::F1old(Double_t x1,Double_t y1,
                    Double_t x2,Double_t y2,
                    Double_t x3,Double_t y3) 
 {
@@ -1043,7 +1057,7 @@ Double_t AliTPCtrackerMI::f1old(Double_t x1,Double_t y1,
 
 
 //_____________________________________________________________________________
-Double_t AliTPCtrackerMI::f1(Double_t x1,Double_t y1,
+Double_t AliTPCtrackerMI::F1(Double_t x1,Double_t y1,
                    Double_t x2,Double_t y2,
                    Double_t x3,Double_t y3) 
 {
@@ -1069,7 +1083,7 @@ Double_t AliTPCtrackerMI::f1(Double_t x1,Double_t y1,
 }
 
 
-Double_t AliTPCtrackerMI::f2(Double_t x1,Double_t y1,
+Double_t AliTPCtrackerMI::F2(Double_t x1,Double_t y1,
                    Double_t x2,Double_t y2,
                    Double_t x3,Double_t y3) 
 {
@@ -1099,7 +1113,7 @@ Double_t AliTPCtrackerMI::f2(Double_t x1,Double_t y1,
 
 
 //_____________________________________________________________________________
-Double_t AliTPCtrackerMI::f2old(Double_t x1,Double_t y1,
+Double_t AliTPCtrackerMI::F2old(Double_t x1,Double_t y1,
                    Double_t x2,Double_t y2,
                    Double_t x3,Double_t y3) 
 {
@@ -1118,7 +1132,7 @@ Double_t AliTPCtrackerMI::f2old(Double_t x1,Double_t y1,
 }
 
 //_____________________________________________________________________________
-Double_t AliTPCtrackerMI::f3(Double_t x1,Double_t y1, 
+Double_t AliTPCtrackerMI::F3(Double_t x1,Double_t y1, 
                    Double_t x2,Double_t y2,
                    Double_t z1,Double_t z2) 
 {
@@ -1129,7 +1143,7 @@ Double_t AliTPCtrackerMI::f3(Double_t x1,Double_t y1,
 }
 
 
-Double_t AliTPCtrackerMI::f3n(Double_t x1,Double_t y1, 
+Double_t AliTPCtrackerMI::F3n(Double_t x1,Double_t y1, 
                    Double_t x2,Double_t y2,
                    Double_t z1,Double_t z2, Double_t c) 
 {
@@ -1144,8 +1158,8 @@ Double_t AliTPCtrackerMI::f3n(Double_t x1,Double_t y1,
   Double_t d  =  TMath::Sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
   if (TMath::Abs(d*c*0.5)>1) return 0;
   //  Double_t   angle2    =  TMath::ASin(d*c*0.5);
-  //  Double_t   angle2    =  TPCFastMath::FastAsin(d*c*0.5);
-  Double_t   angle2    = (d*c*0.5>0.1)? TMath::ASin(d*c*0.5): TPCFastMath::FastAsin(d*c*0.5);
+  //  Double_t   angle2    =  AliTPCFastMath::FastAsin(d*c*0.5);
+  Double_t   angle2    = (d*c*0.5>0.1)? TMath::ASin(d*c*0.5): AliTPCFastMath::FastAsin(d*c*0.5);
 
   angle2  = (z1-z2)*c/(angle2*2.);
   return angle2;
@@ -1175,10 +1189,10 @@ Bool_t   AliTPCtrackerMI::GetProlongation(Double_t x1, Double_t x2, Double_t x[5
   if (TMath::Abs(delta)>0.01){
     dz = x[3]*TMath::ASin(delta)/x[4];
   }else{
-    dz = x[3]*TPCFastMath::FastAsin(delta)/x[4];
+    dz = x[3]*AliTPCFastMath::FastAsin(delta)/x[4];
   }
   
-  //dz = x[3]*TPCFastMath::FastAsin(delta)/x[4];
+  //dz = x[3]*AliTPCFastMath::FastAsin(delta)/x[4];
 
   y+=dy;
   z+=dz;
@@ -1186,7 +1200,13 @@ Bool_t   AliTPCtrackerMI::GetProlongation(Double_t x1, Double_t x2, Double_t x[5
   return kTRUE;  
 }
 
-
+Int_t  AliTPCtrackerMI::LoadClusters (TTree *tree)
+{
+  //
+  //
+  fInput = tree;
+  return LoadClusters();
+}
 
 Int_t  AliTPCtrackerMI::LoadClusters()
 {
@@ -1250,20 +1270,22 @@ void AliTPCtrackerMI::UnloadClusters()
   for (Int_t sec = 0;sec<fkNOS;sec++)
     for (Int_t row = 0;row<nrows;row++){
       AliTPCRow*  tpcrow = &(fOuterSec[sec%fkNOS][row]);
-      if (tpcrow){
-       if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
-       if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
-      }
+      //      if (tpcrow){
+      //       if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
+      //       if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
+      //}
+      tpcrow->ResetClusters();
     }
   //
   nrows = fInnerSec->GetNRows();
   for (Int_t sec = 0;sec<fkNIS;sec++)
     for (Int_t row = 0;row<nrows;row++){
       AliTPCRow*  tpcrow = &(fInnerSec[sec%fkNIS][row]);
-      if (tpcrow){
-       if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
-       if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
-      }
+      //if (tpcrow){
+      //       if (tpcrow->fClusters1) delete []tpcrow->fClusters1; 
+      //if (tpcrow->fClusters2) delete []tpcrow->fClusters2; 
+      //}
+      tpcrow->ResetClusters();
     }
 
   return ;
@@ -1376,7 +1398,7 @@ AliTPCclusterMI *AliTPCtrackerMI::GetClusterMI(Int_t index) const {
   //--------------------------------------------------------------------
   Int_t sec=(index&0xff000000)>>24; 
   Int_t row=(index&0x00ff0000)>>16; 
-  Int_t ncl=(index&0x0000ffff)>>00;
+  Int_t ncl=(index&0x00007fff)>>00;
 
   const AliTPCRow * tpcrow=0;
   AliTPCclusterMI * clrow =0;
@@ -1429,10 +1451,10 @@ Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
       if (!t.Rotate(-fSectors->GetAlpha())) 
        return 0;
     }
-    if (!t.PropagateTo(x)) {
-      return 0;
-    } 
-    y=t.GetY();
+    //if (!t.PropagateTo(x)) {
+    //  return 0;
+    //}
+    return 1;
   }
   //
   // update current shape info every 3 pad-row
@@ -1477,6 +1499,7 @@ Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
     t.fCurrentCluster = cl; 
     t.fRow = nr;
     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
+    if (fIteration>0) accept =0;
     if (accept<3) { 
       //if founded cluster is acceptible
       UpdateTrack(&t,accept);
@@ -1495,7 +1518,7 @@ Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
     t.fCurrentCluster = cl; 
     t.fRow = nr;
     Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
-    
+    /*    
     if (t.fCurrentCluster->IsUsed(10)){
       //
       //     
@@ -1506,11 +1529,11 @@ Int_t AliTPCtrackerMI::FollowToNext(AliTPCseed& t, Int_t nr) {
        return 0;
       }
     }
-    
+    */
     if (accept<3) UpdateTrack(&t,accept);
 
   } else {  
-    if (t.fNFoundable*0.5 > t.GetNumberOfClusters()) t.fRemoval=10;
+    if ( fIteration==0 && t.fNFoundable*0.5 > t.GetNumberOfClusters()) t.fRemoval=10;
     
   }
   return 1;
@@ -1530,7 +1553,6 @@ Int_t AliTPCtrackerMI::FollowToNextFast(AliTPCseed& t, Int_t nr) {
   //
   //
   if (TMath::Abs(y)>ymax){
-    return 0;
     
     if (y > ymax) {
       t.fRelativeSector= (t.fRelativeSector+1) % fN;
@@ -1638,10 +1660,11 @@ Int_t AliTPCtrackerMI::UpdateClusters(AliTPCseed& t,  Int_t nr) {
       if (!t.Rotate(-fSectors->GetAlpha())) 
        return 0;
     }
-    if (!t.PropagateTo(x)){
-      return 0;
-    }
-    y = t.GetY();    
+    //    if (!t.PropagateTo(x)){
+    //  return 0;
+    //}
+    return 1;
+    //y = t.GetY();    
   }
   //
 
@@ -1672,12 +1695,28 @@ Int_t AliTPCtrackerMI::UpdateClusters(AliTPCseed& t,  Int_t nr) {
   Double_t roady = 1.;
   Double_t roadz = 1.;
   //
+
+  if (!cl){
+    index = t.GetClusterIndex2(nr);    
+    if ( (index>0) && (index&0x8000)==0){
+      cl = t.fClusterPointer[nr];
+      if ( (cl==0) && (index>0)) cl = GetClusterMI(index);
+      t.fCurrentClusterIndex1 = index;
+      if (cl) {
+       t.fCurrentCluster  = cl;
+       return 1;
+      }
+    }
+  }
+
   if (krow) {    
     //cl = krow.FindNearest2(y+10,z,roady,roadz,index);      
     cl = krow.FindNearest2(y,z,roady,roadz,index);      
   }
-  t.fCurrentCluster  = cl;
+
   if (cl) t.fCurrentClusterIndex1 = krow.GetIndex(index);   
+  t.fCurrentCluster  = cl;
+
   return 1;
 }
 
@@ -1691,7 +1730,7 @@ Int_t AliTPCtrackerMI::FollowToNextCluster(AliTPCseed & t, Int_t nr) {
 
   if (t.fCurrentCluster) {
     t.fRow = nr; 
-    Bool_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
+    Int_t accept = AcceptCluster(&t,t.fCurrentCluster,1.);
     
     if (t.fCurrentCluster->IsUsed(10)){
       //
@@ -1703,10 +1742,10 @@ Int_t AliTPCtrackerMI::FollowToNextCluster(AliTPCseed & t, Int_t nr) {
        t.fRemoval =10;
        return 0;
       }
-    }
-    
-    if (accept<3) UpdateTrack(&t,accept);
-   
+    }   
+    if (fIteration>0) accept = 0;
+    if (accept<3)  UpdateTrack(&t,accept);  
   } else {
     if (fIteration==0){
       if ( ( (t.GetSigmaY2()+t.GetSigmaZ2())>0.16)&& t.GetNumberOfClusters()>18) t.fRemoval=10;      
@@ -1735,6 +1774,10 @@ Int_t AliTPCtrackerMI::FollowProlongation(AliTPCseed& t, Int_t rf, Int_t step) {
     
   Int_t first = GetRowNumber(xt)-1;
   for (Int_t nr= first; nr>=rf; nr-=step) {    
+    if (nr<fInnerSec->GetNRows()) 
+      fSectors = fInnerSec;
+    else
+      fSectors = fOuterSec;
     if (FollowToNext(t,nr)==0) 
       if (!t.IsActive()) return 0;
     
@@ -1785,7 +1828,11 @@ Int_t AliTPCtrackerMI::FollowBackProlongation(AliTPCseed& t, Int_t rf) {
   if (first<0) first=0;
   for (Int_t nr=first+1; nr<=rf; nr++) {
     //if ( (t.GetSnp()<0.9))
-      FollowToNext(t,nr);                                                             
+    if (nr<fInnerSec->GetNRows()) 
+      fSectors = fInnerSec;
+    else
+      fSectors = fOuterSec;
+    FollowToNext(t,nr);                                                             
   }   
   return 1;
 }
@@ -2028,7 +2075,7 @@ void  AliTPCtrackerMI::RemoveDouble(TObjArray * arr, Float_t factor1, Float_t fa
 
 
 
-void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode)
+void AliTPCtrackerMI::SortTracks(TObjArray * arr, Int_t mode) const
 {
   //
   //sort tracks in array according mode criteria
@@ -2273,7 +2320,7 @@ void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fde
     if ((pt->GetDensityFirst(40)<0.75) && pt->GetNumberOfClusters()<meann) continue; 
     
     
-    Double_t mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
+    Double_t mindens = TMath::Max(double(mdensity-sdensity*fdensity*bfactor),0.65);
     Double_t minn    = TMath::Max(Int_t(meann-fnumber*smeann*bfactor),50);
    
     //    if (pt->fBConstrain) mindens = TMath::Max(mdensity-sdensity*fdensity*bfactor,0.65);
@@ -2302,7 +2349,7 @@ void AliTPCtrackerMI::SignClusters(TObjArray * arr, Float_t fnumber, Float_t fde
 }
 
 
-void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2)
+void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, Float_t th1, Float_t th2) const
 {
   // stop not active tracks
   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
@@ -2322,7 +2369,7 @@ void  AliTPCtrackerMI::StopNotActive(TObjArray * arr, Int_t row0, Float_t th0, F
 
 
 void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0, Float_t th1,
- Float_t th2)
+ Float_t th2) const
 {
   // stop not active tracks
   // take th1 as threshold for number of founded to number of foundable on last 10 active rows
@@ -2336,7 +2383,7 @@ void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0,
     return;
   }
 
-  for (Int_t i=row0; i++;i<maxindex){
+  for (Int_t i=row0; i<maxindex; i++){
     Int_t index = seed->GetClusterIndex2(i);
     if (index!=-1) foundable++;
     //if (!c) continue;
@@ -2356,6 +2403,36 @@ void  AliTPCtrackerMI::StopNotActive(AliTPCseed * seed, Int_t row0, Float_t th0,
 }
 
 
+Int_t AliTPCtrackerMI::RefitInward(AliESD *event)
+{
+  //
+  // back propagation of ESD tracks
+  //
+  //return 0;
+  fEvent = event;
+  ReadSeeds(event,2);
+  fIteration=2;
+  //PrepareForProlongation(fSeeds,1);
+  PropagateForward2(fSeeds);
+  Int_t nseed = fSeeds->GetEntriesFast();
+  for (Int_t i=0;i<nseed;i++){
+    AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
+    seed->PropagateTo(fParam->GetInnerRadiusLow());
+    AliESDtrack *esd=event->GetTrack(i);
+    seed->CookdEdx(0.02,0.6);
+    CookLabel(seed,0.1); //For comparison only
+    if (seed->GetNumberOfClusters()>20){
+      esd->UpdateTrackParams(seed,AliESDtrack::kTPCrefit);
+    }
+    else{
+      //printf("problem\n");
+    }
+  }
+  fEvent =0;
+  //WriteTracks();
+  return 0;
+}
+
 
 Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
 {
@@ -2364,24 +2441,27 @@ Int_t AliTPCtrackerMI::PropagateBack(AliESD *event)
   //
 
   fEvent = event;
-  ReadSeeds(event);
+  fIteration = 1;
+  ReadSeeds(event,0);
   PropagateBack(fSeeds);
   Int_t nseed = fSeeds->GetEntriesFast();
   for (Int_t i=0;i<nseed;i++){
     AliTPCseed * seed = (AliTPCseed*) fSeeds->UncheckedAt(i);
     AliESDtrack *esd=event->GetTrack(i);
-    seed->CookdEdx(0.02,0.06);
+    seed->CookdEdx(0.02,0.6);
     CookLabel(seed,0.1); //For comparison only
     esd->UpdateTrackParams(seed,AliESDtrack::kTPCout);
   }
   fEvent =0;
-  WriteTracks();
+  //WriteTracks();
   return 0;
 }
 
 
 void AliTPCtrackerMI::DeleteSeeds()
 {
+  //
+  //delete Seeds
   Int_t nseed = fSeeds->GetEntriesFast();
   for (Int_t i=0;i<nseed;i++){
     AliTPCseed * seed = (AliTPCseed*)fSeeds->At(i);
@@ -2391,7 +2471,7 @@ void AliTPCtrackerMI::DeleteSeeds()
   fSeeds =0;
 }
 
-void AliTPCtrackerMI::ReadSeeds(AliESD *event)
+void AliTPCtrackerMI::ReadSeeds(AliESD *event, Int_t direction)
 {
   //
   //read seeds from the event
@@ -2408,9 +2488,11 @@ void AliTPCtrackerMI::ReadSeeds(AliESD *event)
   for (Int_t i=0; i<nentr; i++) {
     AliESDtrack *esd=event->GetTrack(i);
     ULong_t status=esd->GetStatus();    
-    const AliTPCtrack t(*esd);
+    AliTPCtrack t(*esd);
     AliTPCseed *seed = new AliTPCseed(t,t.GetAlpha());
-    if (status==AliESDtrack::kTPCin) seed->Modify(0.8);
+    if ((status==AliESDtrack::kTPCin)&&(direction==1)) seed->ResetCovariance(); 
+    if ( direction ==2 &&(status & AliESDtrack::kTRDrefit) == 0 ) seed->ResetCovariance();
+
     //
     //
     // rotate to the local coordinate system
@@ -2424,11 +2506,19 @@ void AliTPCtrackerMI::ReadSeeds(AliESD *event)
     alpha =ns*fSectors->GetAlpha() + fSectors->GetAlphaShift();
     alpha-=seed->GetAlpha();  
     if (!seed->Rotate(alpha)) continue;
+    seed->fEsd = esd;
     //
-    seed->PropagateTo(fSectors->GetX(0));
+    //seed->PropagateTo(fSectors->GetX(0));
     //
     //    Int_t index = esd->GetTPCindex();
     //AliTPCseed * seed2= (AliTPCseed*)fSeeds->At(index);
+    //if (direction==2){
+    //  AliTPCseed * seed2  = ReSeed(seed,0.,0.5,1.);
+    //  if (seed2) {
+    // delete seed;
+    // seed = seed2;
+    //  }
+    //}
     
     fSeeds->AddLast(seed);
   }
@@ -2566,8 +2656,8 @@ void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
        
        //Double_t dfi0   = 2.*TMath::ASin(dvertex*c0*0.5);
        //Double_t dfi1   = 2.*TMath::ASin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);
-       Double_t dfi0   = 2.*TPCFastMath::FastAsin(dvertex*c0*0.5);
-       Double_t dfi1   = 2.*TPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
+       Double_t dfi0   = 2.*AliTPCFastMath::FastAsin(dvertex*c0*0.5);
+       Double_t dfi1   = 2.*AliTPCFastMath::FastAsin(TMath::Sqrt(yy0*yy0+(1-xx0)*(1-xx0))*dvertex*c0*0.5);  
        //
        //
        Double_t z0  =  kcl->GetZ();  
@@ -2643,17 +2733,17 @@ void AliTPCtrackerMI::MakeSeeds3(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
        Double_t sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
        //Double_t sy3=25000*x[4]*x[4]*60+0.5, sy=0.1, sz=0.1;
 
-       Double_t f40=(f1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
-       Double_t f42=(f1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
-       Double_t f43=(f1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
-       Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
-       Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
-       Double_t f23=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
+       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
+       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
+       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
+       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
+       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
+       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
        
-       Double_t f30=(f3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
-       Double_t f31=(f3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
-       Double_t f32=(f3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
-       Double_t f34=(f3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
+       Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
+       Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
+       Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
+       Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
        
         c[0]=sy1;
         c[1]=0.;       c[2]=sz1;
@@ -2918,14 +3008,14 @@ void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
                        
       x[0]=y1;
       x[1]=z1;
-      x[4]=f1(x1,y1,x2,y2,x3,y3);
+      x[4]=F1(x1,y1,x2,y2,x3,y3);
       //if (TMath::Abs(x[4]) >= cuts[0]) continue;
       nin0++;
       //
-      x[2]=f2(x1,y1,x2,y2,x3,y3);
+      x[2]=F2(x1,y1,x2,y2,x3,y3);
       nin1++;
       //
-      x[3]=f3n(x1,y1,x2,y2,z1,z2,x[4]);
+      x[3]=F3n(x1,y1,x2,y2,z1,z2,x[4]);
       //if (TMath::Abs(x[3]) > cuts[3]) continue;
       nin2++;
       //
@@ -2934,17 +3024,17 @@ void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
       Double_t sy2=0.1,  sz2=0.1;
       Double_t sy3=0.1,  sy=0.1, sz=0.1;
       
-      Double_t f40=(f1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
-      Double_t f42=(f1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
-      Double_t f43=(f1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
-      Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
-      Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
-      Double_t f23=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
+      Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
+      Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
+      Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
+      Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
+      Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
+      Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
       
-      Double_t f30=(f3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
-      Double_t f31=(f3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
-      Double_t f32=(f3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
-      Double_t f34=(f3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
+      Double_t f30=(F3(x1,y1+sy,x2,y2,z1,z2)-x[3])/sy;
+      Double_t f31=(F3(x1,y1,x2,y2,z1+sz,z2)-x[3])/sz;
+      Double_t f32=(F3(x1,y1,x2,y2+sy,z1,z2)-x[3])/sy;
+      Double_t f34=(F3(x1,y1,x2,y2,z1,z2+sz)-x[3])/sz;
       
       c[0]=sy1;
       c[1]=0.;       c[2]=sz1; 
@@ -3012,8 +3102,8 @@ void AliTPCtrackerMI::MakeSeeds5(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
 
 
 //_____________________________________________________________________________
-void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t cuts[4],
-                                Float_t deltay, Bool_t bconstrain) {
+void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2, Float_t */*cuts[4]*/,
+                                Float_t deltay, Bool_t /*bconstrain*/) {
   //-----------------------------------------------------------------
   // This function creates track seeds - without vertex constraint
   //-----------------------------------------------------------------
@@ -3190,14 +3280,14 @@ void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
        }
        x[0]=y1;
        x[1]=z1;
-       x[4]=f1(x1,y1,x2,y2,x3,y3);
+       x[4]=F1(x1,y1,x2,y2,x3,y3);
                
        //      if (TMath::Abs(x[4]) >= cuts[0]) continue;  //
-       x[2]=f2(x1,y1,x2,y2,x3,y3);
+       x[2]=F2(x1,y1,x2,y2,x3,y3);
        
        //if (TMath::Abs(x[4]*x1-x[2]) >= cuts[1]) continue;
-       //x[3]=f3(x1,y1,x2,y2,z1,z2);
-       x[3]=f3n(x1,y1,x3,y3,z1,z3,x[4]);
+       //x[3]=F3(x1,y1,x2,y2,z1,z2);
+       x[3]=F3n(x1,y1,x3,y3,z1,z3,x[4]);
        //if (TMath::Abs(x[3]) > cuts[3]) continue;
 
        
@@ -3210,17 +3300,17 @@ void AliTPCtrackerMI::MakeSeeds2(TObjArray * arr, Int_t sec, Int_t i1, Int_t i2,
          sy3=25000*x[4]*x[4]+0.1, sy=0.1, sz=0.1;
        }
        
-       Double_t f40=(f1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
-       Double_t f42=(f1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
-       Double_t f43=(f1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
-       Double_t f20=(f2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
-       Double_t f22=(f2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
-       Double_t f23=(f2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
-
-       Double_t f30=(f3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
-       Double_t f31=(f3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
-       Double_t f32=(f3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
-       Double_t f34=(f3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
+       Double_t f40=(F1(x1,y1+sy,x2,y2,x3,y3)-x[4])/sy;
+       Double_t f42=(F1(x1,y1,x2,y2+sy,x3,y3)-x[4])/sy;
+       Double_t f43=(F1(x1,y1,x2,y2,x3,y3+sy)-x[4])/sy;
+       Double_t f20=(F2(x1,y1+sy,x2,y2,x3,y3)-x[2])/sy;
+       Double_t f22=(F2(x1,y1,x2,y2+sy,x3,y3)-x[2])/sy;
+       Double_t f23=(F2(x1,y1,x2,y2,x3,y3+sy)-x[2])/sy;
+
+       Double_t f30=(F3(x1,y1+sy,x3,y3,z1,z3)-x[3])/sy;
+       Double_t f31=(F3(x1,y1,x3,y3,z1+sz,z3)-x[3])/sz;
+       Double_t f32=(F3(x1,y1,x3,y3+sy,z1,z3)-x[3])/sy;
+       Double_t f34=(F3(x1,y1,x3,y3,z1,z3+sz)-x[3])/sz;
 
        
        c[0]=sy1;
@@ -3272,11 +3362,11 @@ AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1,
 {
   //
   //
-  //reseed
+  //reseed using track points
   Int_t p0 = int(r0*track->GetNumberOfClusters());     // point 0 
   Int_t p1 = int(r1*track->GetNumberOfClusters());
   Int_t p2 = int(r2*track->GetNumberOfClusters());   // last point
-  Int_t pp2;
+  Int_t pp2=0;
   Double_t  x0[3],x1[3],x2[3];
   x0[0]=-1;
   x0[0]=-1;
@@ -3284,6 +3374,9 @@ AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1,
 
   // find track position at given ratio of the length
   Int_t  sec0, sec1, sec2;
+  sec0=0;
+  sec1=0;
+  sec2=0;
   Int_t index=-1;
   Int_t clindex;
   for (Int_t i=0;i<160;i++){
@@ -3348,10 +3441,10 @@ AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1,
   //
   x[0]=x2[1];
   x[1]=x2[2];
-  x[4]=f1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
+  x[4]=F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
   //  if (x[4]>1) return 0;
-  x[2]=f2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
-  x[3]=f3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
+  x[2]=F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]);
+  x[3]=F3n(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2],x[4]);
   //if (TMath::Abs(x[3]) > 2.2)  return 0;
   //if (TMath::Abs(x[2]) > 1.99) return 0;
   //  
@@ -3361,17 +3454,17 @@ AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1,
   Double_t sy2=0.01+track->GetSigmaY2(), sz2=0.01+track->GetSigmaZ2();
   Double_t sy3=0.01+track->GetSigmaY2();
   //
-  Double_t f40=(f1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
-  Double_t f42=(f1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
-  Double_t f43=(f1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
-  Double_t f20=(f2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
-  Double_t f22=(f2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
-  Double_t f23=(f2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
-  //
-  Double_t f30=(f3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
-  Double_t f31=(f3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
-  Double_t f32=(f3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
-  Double_t f34=(f3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
+  Double_t f40=(F1(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[4])/sy;
+  Double_t f42=(F1(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[4])/sy;
+  Double_t f43=(F1(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[4])/sy;
+  Double_t f20=(F2(x2[0],x2[1]+sy,x1[0],x1[1],x0[0],x0[1])-x[2])/sy;
+  Double_t f22=(F2(x2[0],x2[1],x1[0],x1[1]+sy,x0[0],x0[1])-x[2])/sy;
+  Double_t f23=(F2(x2[0],x2[1],x1[0],x1[1],x0[0],x0[1]+sy)-x[2])/sy;
+  //
+  Double_t f30=(F3(x2[0],x2[1]+sy,x0[0],x0[1],x2[2],x0[2])-x[3])/sy;
+  Double_t f31=(F3(x2[0],x2[1],x0[0],x0[1],x2[2]+sz,x0[2])-x[3])/sz;
+  Double_t f32=(F3(x2[0],x2[1],x0[0],x0[1]+sy,x2[2],x0[2])-x[3])/sy;
+  Double_t f34=(F3(x2[0],x2[1],x0[0],x0[1],x2[2],x0[2]+sz)-x[3])/sz;
   
   
   c[0]=sy1;
@@ -3397,6 +3490,119 @@ AliTPCseed *AliTPCtrackerMI::MakeSeed(AliTPCseed *track, Float_t r0, Float_t r1,
   return seed;
 }
 
+
+AliTPCseed *AliTPCtrackerMI::ReSeed(AliTPCseed *track, Float_t r0, Float_t r1, Float_t r2)
+{
+  //
+  //
+  //reseed using founded clusters 
+  //
+  // Find the number of clusters
+  Int_t nclusters = 0;
+  for (Int_t irow=0;irow<160;irow++){
+    if (track->GetClusterIndex(irow)>0) nclusters++;
+  }
+  //
+  Int_t ipos[3];
+  ipos[0] = TMath::Max(int(r0*nclusters),0);             // point 0 cluster
+  ipos[1] = TMath::Min(int(r1*nclusters),nclusters-1);   // 
+  ipos[2] = TMath::Min(int(r2*nclusters),nclusters-1);   // last point
+  //
+  //
+  Double_t  xyz[3][3];
+  Int_t     row[3],sec[3]={0,0,0};
+  //
+  // find track row position at given ratio of the length
+  Int_t index=-1;
+  for (Int_t irow=0;irow<160;irow++){    
+    if (track->GetClusterIndex2(irow)<0) continue;
+    index++;
+    for (Int_t ipoint=0;ipoint<3;ipoint++){
+      if (index<=ipos[ipoint]) row[ipoint] = irow;
+    }        
+  }
+  //
+  //Get cluster and sector position
+  for (Int_t ipoint=0;ipoint<3;ipoint++){
+    Int_t clindex = track->GetClusterIndex2(row[ipoint]);
+    AliTPCclusterMI * cl = GetClusterMI(clindex);
+    if (cl==0) {
+      printf("Bug\n");
+      //      AliTPCclusterMI * cl = GetClusterMI(clindex);
+      return 0;
+    }
+    sec[ipoint]     = ((clindex&0xff000000)>>24)%18;
+    xyz[ipoint][0]  = GetXrow(row[ipoint]);
+    xyz[ipoint][1]  = cl->GetY();
+    xyz[ipoint][2]  = cl->GetZ();
+  }
+  //
+  //
+  // Calculate seed state vector and covariance matrix
+
+  Double_t alpha, cs,sn, xx2,yy2;
+  //
+  alpha = (sec[1]-sec[2])*fSectors->GetAlpha();
+  cs = TMath::Cos(alpha);
+  sn = TMath::Sin(alpha); 
+  xx2= xyz[1][0]*cs-xyz[1][1]*sn;
+  yy2= xyz[1][0]*sn+xyz[1][1]*cs;
+  xyz[1][0] = xx2;
+  xyz[1][1] = yy2;
+  //
+  alpha = (sec[0]-sec[2])*fSectors->GetAlpha();
+  cs = TMath::Cos(alpha);
+  sn = TMath::Sin(alpha); 
+  xx2= xyz[0][0]*cs-xyz[0][1]*sn;
+  yy2= xyz[0][0]*sn+xyz[0][1]*cs;
+  xyz[0][0] = xx2;
+  xyz[0][1] = yy2;
+  //
+  //
+  //
+  Double_t x[5],c[15];
+  //
+  x[0]=xyz[2][1];
+  x[1]=xyz[2][2];
+  x[4]=F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
+  x[2]=F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]);
+  x[3]=F3n(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2],x[4]);
+  //  
+  Double_t sy =0.1,  sz =0.1;
+  //
+  Double_t sy1=0.2, sz1=0.2;
+  Double_t sy2=0.2, sz2=0.2;
+  Double_t sy3=0.2;
+  //
+  Double_t f40=(F1(xyz[2][0],xyz[2][1]+sy,xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1])-x[4])/sy;
+  Double_t f42=(F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1]+sy,xyz[0][0],xyz[0][1])-x[4])/sy;
+  Double_t f43=(F1(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]+sy)-x[4])/sy;
+  Double_t f20=(F2(xyz[2][0],xyz[2][1]+sy,xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1])-x[2])/sy;
+  Double_t f22=(F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1]+sy,xyz[0][0],xyz[0][1])-x[2])/sy;
+  Double_t f23=(F2(xyz[2][0],xyz[2][1],xyz[1][0],xyz[1][1],xyz[0][0],xyz[0][1]+sy)-x[2])/sy;
+  //
+  Double_t f30=(F3(xyz[2][0],xyz[2][1]+sy,xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2])-x[3])/sy;
+  Double_t f31=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2]+sz,xyz[0][2])-x[3])/sz;
+  Double_t f32=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1]+sy,xyz[2][2],xyz[0][2])-x[3])/sy;
+  Double_t f34=(F3(xyz[2][0],xyz[2][1],xyz[0][0],xyz[0][1],xyz[2][2],xyz[0][2]+sz)-x[3])/sz;
+  
+  
+  c[0]=sy1;
+  c[1]=0.;       c[2]=sz1;
+  c[3]=f20*sy1;  c[4]=0.;       c[5]=f20*sy1*f20+f22*sy2*f22+f23*sy3*f23;
+  c[6]=f30*sy1;  c[7]=f31*sz1;  c[8]=f30*sy1*f20+f32*sy2*f22;
+  c[9]=f30*sy1*f30+f31*sz1*f31+f32*sy2*f32+f34*sz2*f34;
+  c[10]=f40*sy1; c[11]=0.; c[12]=f40*sy1*f20+f42*sy2*f22+f43*sy3*f23;
+  c[13]=f30*sy1*f40+f32*sy2*f42;
+  c[14]=f40*sy1*f40+f42*sy2*f42+f43*sy3*f43;
+  
+  //  Int_t row1 = fSectors->GetRowNumber(xyz[2][0]);
+  AliTPCseed *seed=new  AliTPCseed(0, x, c, xyz[2][0], sec[2]*fSectors->GetAlpha()+fSectors->GetAlphaShift());
+  seed->fLastPoint  = row[2];
+  seed->fFirstPoint = row[2];  
+  return seed;
+}
+
 Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed, Float_t th)
 {
   //
@@ -3476,9 +3682,9 @@ Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed, Float_t th)
   Double_t xh[5],xm = x[padm];  
   xh[0]=yt[i2];
   xh[1]=zt[i2];
-  xh[4]=f1(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);  
-  xh[2]=f2(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);
-  xh[3]=f3n(xt[i2],yt[i2],xt[i1],yt[i1],zt[i2],zt[i1],xh[4]);
+  xh[4]=F1(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);  
+  xh[2]=F2(xt[i2],yt[i2],xt[padm],yt[padm],xt[i1],yt[i1]);
+  xh[3]=F3n(xt[i2],yt[i2],xt[i1],yt[i1],zt[i2],zt[i1],xh[4]);
   //
   //
   for (Int_t i=i1;i<=i2;i++){
@@ -3487,8 +3693,8 @@ Int_t  AliTPCtrackerMI::CheckKinkPoint(AliTPCseed*seed, Float_t th)
     GetProlongation(x[i2], x[i],xh,yy,zz);
     if (TMath::Abs(y[i]-yy)>4||TMath::Abs(z[i]-zz)>4){
       //Double_t xxh[5];
-      //xxh[4]=f1old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);  
-      //xxh[2]=f2old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);
+      //xxh[4]=F1old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);  
+      //xxh[2]=F2old(x[i2],y[i2],x[padm],y[padm],x[i1],y[i1]);
       printf("problem\n");
     }
     y[i] = y[i] - yy;
@@ -3709,6 +3915,18 @@ Int_t AliTPCtrackerMI::ReadSeeds(const TFile *inp) {
   return 0;
 }
 
+Int_t AliTPCtrackerMI::Clusters2Tracks (AliESD *esd)
+{
+  //
+  fEvent = esd;
+  Clusters2Tracks();
+  if (!fSeeds) return 1;
+  FillESD(fSeeds);
+  return 0;
+  //
+}
+
+
 //_____________________________________________________________________________
 Int_t AliTPCtrackerMI::Clusters2Tracks() {
   //-----------------------------------------------------------------
@@ -3716,14 +3934,7 @@ Int_t AliTPCtrackerMI::Clusters2Tracks() {
   //-----------------------------------------------------------------
   TDirectory *savedir=gDirectory; 
   TStopwatch timer;
-  //
-  if (!fInput) SetIO();  //set default IO using loaders
-  if (!fInput){
-     cerr<<"AliTPCtrackerMI::Clusters2Tracks(): input file is not open !\n";
-     return 1;
-  }
-  LoadClusters();
-  //
+
   fIteration = 0;
   fSeeds = Tracking();
 
@@ -3749,9 +3960,6 @@ Int_t AliTPCtrackerMI::Clusters2Tracks() {
     } 
   }
   RemoveDouble(fSeeds,0.2,0.6,11);
-  //RemoveUsed(fSeeds,0.9,0.9,6);
-  //RemoveUsed(fSeeds,0.8,0.8,6);
-  //RemoveUsed(fSeeds,0.7,0.7,6);
   RemoveUsed(fSeeds,0.5,0.5,6);
 
   //
@@ -3769,10 +3977,10 @@ Int_t AliTPCtrackerMI::Clusters2Tracks() {
     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
     if ((pt->IsActive() || (pt->fRemoval==10) )){
       cerr<<found++<<'\r';      
+      pt->fLab2 = i;
     }
     else
       delete fSeeds->RemoveAt(i);
-    pt->fLab2 = i;
   }
 
   
@@ -3796,33 +4004,39 @@ Int_t AliTPCtrackerMI::Clusters2Tracks() {
     //if ((pt->IsActive() || (pt->fRemoval==10) )&& nc>50 &&pt->GetNumberOfClusters()>0.4*pt->fNFoundable){
     if ((pt->IsActive() || (pt->fRemoval==10) )){
       cerr<<found++<<'\r';      
+      pt->fLab2 = i;
     }
     else
       delete fSeeds->RemoveAt(i);
-    pt->fLab2 = i;
+    //AliTPCseed * seed1 = ReSeed(pt,0.05,0.5,1);
+    //if (seed1){
+    //  FollowProlongation(*seed1,0);
+    //  Int_t n = seed1->GetNumberOfClusters();
+    //  printf("fP4\t%f\t%f\n",seed1->GetC(),pt->GetC());
+    //  printf("fN\t%d\t%d\n", seed1->GetNumberOfClusters(),pt->GetNumberOfClusters());
+    //
+    //}
+    //AliTPCseed * seed2 = ReSeed(pt,0.95,0.5,0.05);
+    
   }
 
   SortTracks(fSeeds, 1);
   
-  /*
+  /*    
   fIteration = 1;
-  PrepareForBackProlongation(fSeeds,0.5);
+  PrepareForBackProlongation(fSeeds,5.);
   PropagateBack(fSeeds);
   printf("Time for back propagation: \t");timer.Print();timer.Start();
   
   fIteration = 2;
   
-  PrepareForProlongation(fSeeds,1.);
-  PropagateForward();
-  
-  fSectors = fOuterSec;
-  ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
-  fSectors = fInnerSec;
-  ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
+  PrepareForProlongation(fSeeds,5.);
+  PropagateForward2(fSeeds);
+   
   printf("Time for FORWARD propagation: \t");timer.Print();timer.Start();
   // RemoveUsed(fSeeds,0.7,0.7,6);
   //RemoveOverlap(fSeeds,0.9,7,kTRUE);
+   
   nseed=fSeeds->GetEntriesFast();
   found = 0;
   for (Int_t i=0; i<nseed; i++) {
@@ -3848,20 +4062,10 @@ Int_t AliTPCtrackerMI::Clusters2Tracks() {
   //  fNTracks = found;
   printf("Time for overlap removal, track writing and dedx cooking: \t"); timer.Print();timer.Start();
   //
-  if (fOutput) {
-    WriteTracks();
-  }
-  if (!fNewIO)  fOutput->Write();
-  else
-    AliRunLoader::GetDetectorLoader("TPC",AliConfig::fgkDefaultEventFolderName)->WriteTracks("OVERWRITE");
-
-
   cerr<<"Number of found tracks : "<<"\t"<<found<<endl;  
   savedir->cd();
-  //if (seedtree) delete seedtree;
   //  UnloadClusters();
-  //printf("Time for unloading cluster: \t"); timer.Print();timer.Start();
-  
+  //  
   return 0;
 }
 
@@ -4059,7 +4263,7 @@ TObjArray * AliTPCtrackerMI::Tracking()
 }
 
 
-void AliTPCtrackerMI::SumTracks(TObjArray *arr1,TObjArray *arr2)
+void AliTPCtrackerMI::SumTracks(TObjArray *arr1,TObjArray *arr2) const
 {
   //
   //sum tracks to common container
@@ -4115,7 +4319,11 @@ void  AliTPCtrackerMI::ParallelTracking(TObjArray * arr, Int_t rfirst, Int_t rla
 
 
   //
-  for (Int_t nr=rfirst; nr>=rlast; nr--){      
+  for (Int_t nr=rfirst; nr>=rlast; nr--){ 
+    if (nr<fInnerSec->GetNRows()) 
+      fSectors = fInnerSec;
+    else
+      fSectors = fOuterSec;
     // make indexes with the cluster tracks for given       
 
     // find nearest cluster
@@ -4143,7 +4351,7 @@ void  AliTPCtrackerMI::ParallelTracking(TObjArray * arr, Int_t rfirst, Int_t rla
   }    
 }
 
-void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac)
+void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac) const
 {
   //
   //
@@ -4179,7 +4387,7 @@ void AliTPCtrackerMI::PrepareForBackProlongation(TObjArray * arr,Float_t fac)
 
 
 }
-void AliTPCtrackerMI::PrepareForProlongation(TObjArray * arr, Float_t fac)
+void AliTPCtrackerMI::PrepareForProlongation(TObjArray * arr, Float_t fac) const
 {
   //
   //
@@ -4209,17 +4417,16 @@ Int_t AliTPCtrackerMI::PropagateBack(TObjArray * arr)
     if (pt) { 
       AliTPCseed *pt2 = new AliTPCseed(*pt);
       fSectors = fInnerSec;
-      FollowBackProlongation(*pt,fSectors->GetNRows()-1);
-      fSectors = fOuterSec;
-      FollowBackProlongation(*pt,fSectors->GetNRows()-1);
-      fSectors = fOuterSec;
-      if (pt->GetNumberOfClusters()<35 && pt->GetLabel()>0 ){
+      //FollowBackProlongation(*pt,fInnerSec->GetNRows()-1);
+      //fSectors = fOuterSec;
+      FollowBackProlongation(*pt,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);
+     
+      if (pt->GetNumberOfClusters()<20 && pt->GetLabel()>0 ){
        printf("\n%d",pt->GetLabel());
        fSectors = fInnerSec;
-       FollowBackProlongation(*pt2,fSectors->GetNRows()-1);
-       fSectors = fOuterSec;
-       FollowBackProlongation(*pt2,fSectors->GetNRows()-1);
-       fSectors = fOuterSec;
+       //FollowBackProlongation(*pt2,fInnerSec->GetNRows()-1);
+       //fSectors = fOuterSec;
+       FollowBackProlongation(*pt2,fInnerSec->GetNRows()+fOuterSec->GetNRows()-1);
       }
     }      
   }
@@ -4237,19 +4444,11 @@ Int_t AliTPCtrackerMI::PropagateForward2(TObjArray * arr)
     AliTPCseed *pt = (AliTPCseed*)arr->UncheckedAt(i);
     if (pt) { 
       AliTPCseed *pt2 = new AliTPCseed(*pt);
-      fSectors = fOuterSec;
-      FollowProlongation(*pt,0);
-      fSectors = fOuterSec;
       FollowProlongation(*pt,0);
-      fSectors = fInnerSec;
       if (pt->GetNumberOfClusters()<35 && pt->GetLabel()>0 ){
-       printf("\n%d",pt->GetLabel());
-       fSectors = fOuterSec;
        FollowProlongation(*pt2,0);
-       fSectors = fOuterSec;
-       FollowProlongation(*pt2,0);
-       fSectors = fOuterSec;
       }
+      delete pt2;
     }      
   }
   return 0;
@@ -4258,10 +4457,25 @@ Int_t AliTPCtrackerMI::PropagateForward2(TObjArray * arr)
 
 Int_t AliTPCtrackerMI::PropagateForward()
 {
+  //
+  // propagate track forward
+  UnsignClusters();
+  Int_t nseed = fSeeds->GetEntriesFast();
+  for (Int_t i=0;i<nseed;i++){
+    AliTPCseed *pt = (AliTPCseed*)fSeeds->UncheckedAt(i);
+    if (pt){
+      AliTPCseed &t = *pt;
+      Double_t alpha=t.GetAlpha() - fSectors->GetAlphaShift();
+      if (alpha > 2.*TMath::Pi()) alpha -= 2.*TMath::Pi();  
+      if (alpha < 0.            ) alpha += 2.*TMath::Pi();  
+      t.fRelativeSector = Int_t(alpha/fSectors->GetAlpha()+0.0001)%fN;
+    }
+  }
+  
   fSectors = fOuterSec;
-  ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
+  ParallelTracking(fSeeds,fOuterSec->GetNRows()+fInnerSec->GetNRows()-1,fInnerSec->GetNRows());
   fSectors = fInnerSec;
-  ParallelTracking(fSeeds,fSectors->GetNRows()-1,0);
+  ParallelTracking(fSeeds,fInnerSec->GetNRows()-1,0);
   //WriteTracks();
   return 1;
 }
@@ -4367,8 +4581,8 @@ void AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong) const {
   //--------------------------------------------------------------------
   Int_t noc=t->GetNumberOfClusters();
   if (noc<10){
-    printf("\nnot founded prolongation\n\n\n");
-    t->Dump();
+    //printf("\nnot founded prolongation\n\n\n");
+    //t->Dump();
     return ;
   }
   Int_t lb[160];
@@ -4442,6 +4656,30 @@ void AliTPCtrackerMI::CookLabel(AliTPCseed *t, Float_t wrong) const {
   //delete[] clusters;
 }
 
+
+Int_t  AliTPCtrackerMI::AliTPCSector::GetRowNumber(Double_t x) const 
+{
+  //return pad row number for this x
+  Double_t r;
+  if (fN < 64){
+    r=fRow[fN-1].GetX();
+    if (x > r) return fN;
+    r=fRow[0].GetX();
+    if (x < r) return -1;
+    return Int_t((x-r)/fPadPitchLength + 0.5);}
+  else{    
+    r=fRow[fN-1].GetX();
+    if (x > r) return fN;
+    r=fRow[0].GetX();
+    if (x < r) return -1;
+    Double_t r1=fRow[64].GetX();
+    if(x<r1){       
+      return Int_t((x-r)/f1PadPitchLength + 0.5);}
+    else{
+      return (Int_t((x-r1)/f2PadPitchLength + 0.5)+64);} 
+  }
+}
+
 //_________________________________________________________________________
 void AliTPCtrackerMI::AliTPCSector::Setup(const AliTPCParam *par, Int_t f) {
   //-----------------------------------------------------------------------
@@ -4475,9 +4713,19 @@ void AliTPCtrackerMI::AliTPCSector::Setup(const AliTPCParam *par, Int_t f) {
   } 
 }
 
+AliTPCtrackerMI::AliTPCRow::AliTPCRow() {
+  //
+  // default constructor
+  fN=0;
+  fN1=0;
+  fN2=0;
+  fClusters1=0;
+  fClusters2=0;
+}
 
 AliTPCtrackerMI::AliTPCRow::~AliTPCRow(){
   //
+
 }
 
 
@@ -4498,6 +4746,20 @@ AliTPCtrackerMI::AliTPCRow::InsertCluster(const AliTPCclusterMI* c, UInt_t index
   fIndex[i]=index; fClusters[i]=c; fN++;
 }
 
+void AliTPCtrackerMI::AliTPCRow::ResetClusters() {
+   //
+   // reset clusters
+   fN  = 0; 
+   fN1 = 0;
+   fN2 = 0;
+   //delete[] fClusterArray; 
+   if (fClusters1) delete []fClusters1; 
+   if (fClusters2) delete []fClusters2; 
+   //fClusterArray=0;
+   fClusters1 = 0;
+   fClusters2 = 0;
+}
+
 
 //___________________________________________________________________
 Int_t AliTPCtrackerMI::AliTPCRow::Find(Double_t z) const {
@@ -4607,7 +4869,6 @@ AliTPCseed::AliTPCseed():AliTPCtrack(){
   fEPoints = 0;
   fNFoundable =0;
   fNShared  =0;
-  //  fTrackPoints =0;
   fRemoval = 0;
   fSort =0;
   fFirstPoint =0;
@@ -4615,9 +4876,12 @@ AliTPCseed::AliTPCseed():AliTPCtrack(){
   fBSigned = kFALSE;
   fSeed1 =-1;
   fSeed2 =-1;
+  fCurrentCluster =0;
 }
 
 AliTPCseed::AliTPCseed(const AliTPCtrack &t):AliTPCtrack(t){
+  //
+  //copy constructor
   fPoints = 0;
   fEPoints = 0;
   fNShared  =0; 
@@ -4639,9 +4903,13 @@ AliTPCseed::AliTPCseed(const AliTPCtrack &t):AliTPCtrack(t){
   fBSigned = kFALSE;
   fSeed1 =-1;
   fSeed2 =-1;
+  fCurrentCluster =0;
+
 }
 
 AliTPCseed::AliTPCseed(const AliKalmanTrack &t, Double_t a):AliTPCtrack(t,a){
+  //
+  //copy constructor
   fRow=0;
   for (Int_t i=0;i<160;i++) {
     fClusterPointer[i] = 0;
@@ -4661,6 +4929,7 @@ AliTPCseed::AliTPCseed(const AliKalmanTrack &t, Double_t a):AliTPCtrack(t,a){
   fBSigned = kFALSE;
   fSeed1 =-1;
   fSeed2 =-1;
+  fCurrentCluster =0;
 }
 
 AliTPCseed::AliTPCseed(UInt_t index, const Double_t xx[5], const Double_t cc[15], 
@@ -4668,6 +4937,7 @@ AliTPCseed::AliTPCseed(UInt_t index, const Double_t xx[5], const Double_t cc[15]
   AliTPCtrack(index, xx, cc, xr, alpha) {
   //
   //
+  //constructor
   fRow =0;
   for (Int_t i=0;i<200;i++) SetClusterIndex2(i,-3);
   for (Int_t i=0;i<160;i++) fClusterPointer[i]=0;
@@ -4685,9 +4955,13 @@ AliTPCseed::AliTPCseed(UInt_t index, const Double_t xx[5], const Double_t cc[15]
   fBSigned = kFALSE;
   fSeed1 =-1;
   fSeed2 =-1;
+  fCurrentCluster =0;
+
 }
 
 AliTPCseed::~AliTPCseed(){
+  //
+  // destructor
   if (fPoints) delete fPoints;
   fPoints =0;
   if (fEPoints) delete fEPoints;