]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/TPCLib/tracking-ca/AliHLTTPCCATracker.cxx
Obsolete tracker output removed
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCATracker.cxx
index 6acc920969f04b18256997a8191c3f572add349a..f8fed37e68128735e9b73e89c2c19ceb89017b31 100644 (file)
 //***************************************************************************
 
 #include "AliHLTTPCCATracker.h"
-#include "AliHLTTPCCAOutTrack.h"
-#include "AliHLTTPCCAGrid.h"
 #include "AliHLTTPCCARow.h"
 #include "AliHLTTPCCATrack.h"
 #include "AliHLTTPCCATracklet.h"
 #include "AliHLTTPCCAMath.h"
-#include "AliHLTTPCCAHit.h"
 #include "MemoryAssignmentHelpers.h"
 
 #include "TStopwatch.h"
 #include "AliHLTTPCCAProcess.h"
 #include "AliHLTTPCCASliceTrack.h"
 #include "AliHLTTPCCASliceOutput.h"
-#include "AliHLTTPCCADataCompressor.h"
 #include "AliHLTTPCCAClusterData.h"
+#include "AliHLTTPCCADataCompressor.h"
 
 #include "AliHLTTPCCATrackParam.h"
 
+#include "AliHLTTPCCAGPUConfig.h"
+
 #if !defined(HLTCA_GPUCODE)
 #include <iostream>
+#include <iomanip>
+#include <string.h>
 #endif
 
 //#define DRAW1
 
 #ifdef DRAW1
 #include "AliHLTTPCCADisplay.h"
-#endif //DRAW
+#endif //DRAW1
 
 #ifdef HLTCA_INTERNAL_PERFORMANCE
-#include "AliHLTTPCCAPerformance.h"
+//#include "AliHLTTPCCAPerformance.h"
 #endif
 
+#ifdef HLTCA_STANDALONE
+#include "AliHLTTPCCAStandaloneFramework.h"
+#endif
 
 ClassImp( AliHLTTPCCATracker )
 
-GPUd() AliHLTTPCCATracker::~AliHLTTPCCATracker()
+
+#if !defined(HLTCA_GPUCODE)
+
+AliHLTTPCCATracker::~AliHLTTPCCATracker()
 {
   // destructor
        if (!fIsGPUTracker)
        {
-               delete[] fCommonMemory;
-               delete[] fHitMemory;
-               delete[] fTrackMemory;
+               if (fCommonMem) delete fCommonMem;
+               if (fHitMemory) delete[] fHitMemory;
+               if (fTrackletMemory) delete[] fTrackletMemory;
+               if (fTrackMemory) delete[] fTrackMemory;
+               fCommonMem = NULL;
+               fHitMemory = fTrackMemory = NULL;
        }
 }
 
-#if !defined(HLTCA_GPUCODE)
-
-
 // ----------------------------------------------------------------------------------
 void AliHLTTPCCATracker::Initialize( const AliHLTTPCCAParam &param )
 {
@@ -93,149 +100,249 @@ void AliHLTTPCCATracker::StartEvent()
 
 void AliHLTTPCCATracker::SetGPUTracker()
 {
+       //Make this a GPU Tracker
        fIsGPUTracker = true;
+       fData.SetGpuSliceData();
 }
 
-char* AliHLTTPCCATracker::SetGPUTrackerCommonMemory(char* pGPUMemory)
+char* AliHLTTPCCATracker::SetGPUTrackerCommonMemory(char* const pGPUMemory)
 {
-       fCommonMemory = (char*) pGPUMemory;
-       SetPointersCommon();
-       return(pGPUMemory + fCommonMemorySize);
+       //Set up common Memory Pointer for GPU Tracker
+  fCommonMem = (commonMemoryStruct*) pGPUMemory;
+  return(pGPUMemory + sizeof(commonMemoryStruct));
 }
 
 
-char* AliHLTTPCCATracker::SetGPUTrackerHitsMemory(char* pGPUMemory, int MaxNHits )
+char* AliHLTTPCCATracker::SetGPUTrackerHitsMemory(char* pGPUMemory, int MaxNHits)
 {
+       //Set up Hits Memory Pointers for GPU Tracker
        fHitMemory = (char*) pGPUMemory;
        SetPointersHits(MaxNHits);
-       return(pGPUMemory + fHitMemorySize);
+       pGPUMemory += fHitMemorySize;
+       AssignMemory(fTrackletTmpStartHits, pGPUMemory, NHitsTotal());
+       AssignMemory(fRowStartHitCountOffset, pGPUMemory, Param().NRows());
+
+       return(pGPUMemory);
 }
 
+char* AliHLTTPCCATracker::SetGPUTrackerTrackletsMemory(char* pGPUMemory, int MaxNTracks)
+{
+       //Set up Tracklet Memory Pointers for GPU Tracker
+       fTrackletMemory = (char*) pGPUMemory;
+       SetPointersTracklets(MaxNTracks);
+       pGPUMemory += fTrackletMemorySize;
+       AssignMemory(fGPUTrackletTemp, pGPUMemory, MaxNTracks);
+       AssignMemory(fRowBlockTracklets, pGPUMemory, MaxNTracks * 2 * (Param().NRows() / HLTCA_GPU_SCHED_ROW_STEP + 1));
+       AssignMemory(fRowBlockPos, pGPUMemory, 2 * (Param().NRows() / HLTCA_GPU_SCHED_ROW_STEP + 1));
+       AssignMemory(fBlockStartingTracklet, pGPUMemory, HLTCA_GPU_BLOCK_COUNT);
+
+       return(pGPUMemory);
+}
 
 char* AliHLTTPCCATracker::SetGPUTrackerTracksMemory(char* pGPUMemory, int MaxNTracks, int MaxNHits )
 {
+       //Set up Tracks Memory Pointer for GPU Tracker
        fTrackMemory = (char*) pGPUMemory;
        SetPointersTracks(MaxNTracks, MaxNHits);
-       return(pGPUMemory + fTrackMemorySize);
+       pGPUMemory += fTrackMemorySize;
+
+       return(pGPUMemory);
 }
 
-void AliHLTTPCCATracker::DumpLinks(std::ostream &out)
+void AliHLTTPCCATracker::DumpSliceData(std::ostream &out)
 {
+       //Dump Slice Input Data to File
+       out << "Slice Data:" << std::endl;
        for (int i = 0;i < Param().NRows();i++)
        {
-               out << "Row: " << i << endl;
+               out << "Row: " << i << std::endl;
                for (int j = 0;j < Row(i).NHits();j++)
                {
-                       out << HitLinkUpData(Row(i), j) << ", ";
+                       if (j && j % 16 == 0) out << std::endl;
+                       out << j << '-' << Data().HitDataY(Row(i), j) << '-' << Data().HitDataZ(Row(i), j) << ", ";
                }
-               out << endl;
+               out << std::endl;
        }
 }
 
-void AliHLTTPCCATracker::DumpStartHits(std::ostream &out)
+void AliHLTTPCCATracker::DumpLinks(std::ostream &out)
 {
-       for (int j = 0;j < Param().NRows();j++)
+       //Dump Links (after Neighbours Finder / Cleaner) to file
+       out << "Hit Links:" << std::endl;
+       for (int i = 0;i < Param().NRows();i++)
        {
-               for (int i = 0;i < *NTracklets();i++)
+               out << "Row: " << i << std::endl;
+               for (int j = 0;j < Row(i).NHits();j++)
                {
-                       if (TrackletStartHit(i).RowIndex() == j)
-                               out << TrackletStartHit(i).RowIndex() << "-" << TrackletStartHit(i).HitIndex() << endl;
+                       if (j && j % 32 == 0) out << std::endl;
+                       out << HitLinkUpData(Row(i), j) << "/" << HitLinkDownData(Row(i), j) << ", ";
                }
+               out << std::endl;
        }
-       out << endl;
 }
 
-void AliHLTTPCCATracker::DumpTrackHits(std::ostream &out)
+void AliHLTTPCCATracker::DumpHitWeights(std::ostream &out)
 {
-       for (int k = 0;k < Param().NRows();k++)
+       //dump hit weights to file
+    out << "Hit Weights:" << std::endl;
+    for (int i = 0;i < Param().NRows();i++)
+    {
+       out << "Row: " << i << ":" << std::endl;
+       for (int j = 0;j < Row(i).NHits();j++)
        {
-               for (int j = 0;j < *NTracks();j++)
-               {
-                       if (Tracks()[j].NHits() == 0 || !Tracks()[j].Alive()) continue;
-                       if (TrackHits()[Tracks()[j].FirstHitID()].RowIndex() == k)
-                       {
-                               for (int i = 0;i < Tracks()[j].NHits();i++)
-                               {
-                                       out << TrackHits()[Tracks()[j].FirstHitID() + i].RowIndex() << "-" << TrackHits()[Tracks()[j].FirstHitID() + i].HitIndex() << ", ";
-                               }
-                               out << "(Track: " << j << ")" << endl;
-                       }
-               }
+           if (j && j % 32 == 0) out << std::endl;
+           out << HitWeight(Row(i), j) << ", ";
        }
+       out << std::endl;
+    }
+}
+
+int AliHLTTPCCATracker::StarthitSortComparison(const void*a, const void* b)
+{
+       //qsort helper function to sort start hits
+       AliHLTTPCCAHitId* aa = (AliHLTTPCCAHitId*) a;
+       AliHLTTPCCAHitId* bb = (AliHLTTPCCAHitId*) b;
+
+       if (aa->RowIndex() != bb->RowIndex()) return(aa->RowIndex() - bb->RowIndex());
+       return(aa->HitIndex() - bb->HitIndex());
 }
 
-int trackletSortComparison(const void* a, const void* b)
+void AliHLTTPCCATracker::DumpStartHits(std::ostream &out)
 {
-       const AliHLTTPCCATracklet* aa = (AliHLTTPCCATracklet*) a;
-       const AliHLTTPCCATracklet* bb = (AliHLTTPCCATracklet*) b;
-       if (aa->NHits() == 0) return(-1);
-       if (bb->NHits() == 0) return(1);
-       if (aa->FirstRow() != bb->FirstRow())
+       //sort start hits and dump to file
+       out << "Start Hits: (" << *NTracklets() << ")" << std::endl;
+#ifdef HLTCA_GPU_SORT_DUMPDATA
+       qsort(TrackletStartHits(), *NTracklets(), sizeof(AliHLTTPCCAHitId), StarthitSortComparison);
+#endif
+       for (int i = 0;i < *NTracklets();i++)
        {
-               return(aa->FirstRow() - bb->FirstRow());
+               out << TrackletStartHit(i).RowIndex() << "-" << TrackletStartHit(i).HitIndex() << std::endl;
        }
-       for (int i = aa->FirstRow();i <= aa->LastRow();i++)
+       out << std::endl;
+}
+
+void AliHLTTPCCATracker::DumpTrackHits(std::ostream &out)
+{
+       //dump tracks to file
+       out << "Tracks: (" << *NTracks() << ")" << std::endl;
+#ifdef HLTCA_GPU_SORT_DUMPDATA
+       for (int k = 0;k < Param().NRows();k++)
        {
-               if (i >= bb->LastRow()) return(-1);
-               if (aa->RowHit(i) != bb->RowHit(i))
+               for (int l = 0;l < Row(k).NHits();l++)
                {
-                       return(aa->RowHit(i) - bb->RowHit(i));
-               }
+#endif
+                       for (int j = 0;j < *NTracks();j++)
+                       {
+                               if (Tracks()[j].NHits() == 0 || !Tracks()[j].Alive()) continue;
+#ifdef HLTCA_GPU_SORT_DUMPDATA
+                               if (TrackHits()[Tracks()[j].FirstHitID()].RowIndex() == k && TrackHits()[Tracks()[j].FirstHitID()].HitIndex() == l)
+                               {
+#endif
+                                       for (int i = 0;i < Tracks()[j].NHits();i++)
+                                       {
+                                               out << TrackHits()[Tracks()[j].FirstHitID() + i].RowIndex() << "-" << TrackHits()[Tracks()[j].FirstHitID() + i].HitIndex() << ", ";
+                                       }
+                                       out << "(Track: " << j << ")" << std::endl;
+#ifdef HLTCA_GPU_SORT_DUMPDATA
+                               }
+                       }       
+#endif
+               }       
+#ifdef HLTCA_GPU_SORT_DUMPDATA
        }
-       return(0);
+#endif
 }
 
 void AliHLTTPCCATracker::DumpTrackletHits(std::ostream &out)
 {
-       qsort(Tracklets(), *NTracklets(), sizeof(AliHLTTPCCATracklet), trackletSortComparison);
-       for (int k = 0;k < Param().NRows();k++)
+       //dump tracklets to file
+       out << "Tracklets: (" << *NTracklets() << ")" << std::endl;
+#ifdef HLTCA_GPU_SORT_DUMPDATA
+       AliHLTTPCCAHitId* tmpIds = new AliHLTTPCCAHitId[*NTracklets()];
+       AliHLTTPCCATracklet* tmpTracklets = new AliHLTTPCCATracklet[*NTracklets()];
+       memcpy(tmpIds, TrackletStartHits(), *NTracklets() * sizeof(AliHLTTPCCAHitId));
+       memcpy(tmpTracklets, Tracklets(), *NTracklets() * sizeof(AliHLTTPCCATracklet));
+#ifdef EXTERN_ROW_HITS
+       int* tmpHits = new int[*NTracklets() * Param().NRows()];
+       memcpy(tmpHits, TrackletRowHits(), *NTracklets() * Param().NRows() * sizeof(int));
+#endif
+       qsort(TrackletStartHits(), *NTracklets(), sizeof(AliHLTTPCCAHitId), StarthitSortComparison);
+       for (int i = 0;i < *NTracklets();i++)
        {
                for (int j = 0;j < *NTracklets();j++)
                {
-                       if (Tracklets()[j].NHits() == 0) continue;
-                       if (Tracklets()[j].LastRow() > Tracklets()[j].FirstRow() && (Tracklets()[j].FirstRow() >= Param().NRows() || Tracklets()[j].LastRow() >= Param().NRows()))
-                       {
-                               printf("\nError: First %d Last %d Num %d", Tracklets()[j].FirstRow(), Tracklets()[j].LastRow(), Tracklets()[j].NHits());
-                       }
-                       else if (Tracklets()[j].NHits() && Tracklets()[j].FirstRow() == k && Tracklets()[j].LastRow() > Tracklets()[j].FirstRow())
+                       if (tmpIds[i].RowIndex() == TrackletStartHit(j).RowIndex() && tmpIds[i].HitIndex() == TrackletStartHit(j).HitIndex())
                        {
-                               for (int i = Tracklets()[j].FirstRow();i <= Tracklets()[j].LastRow();i++)
+                               memcpy(&Tracklets()[j], &tmpTracklets[i], sizeof(AliHLTTPCCATracklet));
+#ifdef EXTERN_ROW_HITS
+                               if (tmpTracklets[i].NHits())
                                {
-                                       if (Tracklets()[j].RowHit(i) != -1)
-                                               out << i << "-" << Tracklets()[j].RowHit(i) << ", ";
+                                       for (int k = tmpTracklets[i].FirstRow();k <= tmpTracklets[i].LastRow();k++)
+                                       {
+                                               fTrackletRowHits[k * *NTracklets() + j] = tmpHits[k * *NTracklets() + i];
+                                       }
                                }
-                               out << endl;
+#endif
+                               break;
+                       }
+               }
+       }
+       delete[] tmpIds;
+       delete[] tmpTracklets;
+#ifdef EXTERN_ROW_HITS
+       delete[] tmpHits;
+#endif
+#endif
+       for (int j = 0;j < *NTracklets();j++)
+       {
+               out << "Tracklet " << j << " (Hits: " << std::setw(3) << Tracklets()[j].NHits() << ", Start: " << std::setw(3) << TrackletStartHit(j).RowIndex() << "-" << std::setw(3) << TrackletStartHit(j).HitIndex() << ") ";
+               if (Tracklets()[j].NHits() == 0);
+               else if (Tracklets()[j].LastRow() > Tracklets()[j].FirstRow() && (Tracklets()[j].FirstRow() >= Param().NRows() || Tracklets()[j].LastRow() >= Param().NRows()))
+               {
+#ifdef HLTCA_STANDALONE
+                       printf("\nError: First %d Last %d Num %d", Tracklets()[j].FirstRow(), Tracklets()[j].LastRow(), Tracklets()[j].NHits());
+#endif
+               }
+               else if (Tracklets()[j].NHits() && Tracklets()[j].LastRow() > Tracklets()[j].FirstRow())
+               {
+                       for (int i = Tracklets()[j].FirstRow();i <= Tracklets()[j].LastRow();i++)
+                       {
+                               //if (Tracklets()[j].RowHit(i) != -1)
+#ifdef EXTERN_ROW_HITS
+                                       out << i << "-" << fTrackletRowHits[i * fCommonMem->fNTracklets + j] << ", ";
+#else
+                                       out << i << "-" << Tracklets()[j].RowHit(i) << ", ";
+#endif
                        }
                }
+               out << std::endl;
        }
 }
 
 
-void  AliHLTTPCCATracker::SetupCommonMemory()
+void AliHLTTPCCATracker::SetupCommonMemory()
 {
   // set up common memory
 
   if (!fIsGPUTracker)
   {
-    if ( !fCommonMemory ) {
-      SetPointersCommon(); // just to calculate the size
+    if ( !fCommonMem ) {
       // the 1600 extra bytes are not used unless fCommonMemorySize increases with a later event
-      fCommonMemory = reinterpret_cast<char*> ( new uint4 [ fCommonMemorySize/sizeof( uint4 ) + 100] );
-      SetPointersCommon();// set pointers
+      //fCommonMemory = reinterpret_cast<char*> ( new uint4 [ fCommonMemorySize/sizeof( uint4 ) + 100] );
+         fCommonMem = new commonMemoryStruct;
     }
 
-    delete[] fHitMemory;
-    delete[] fTrackMemory;
-    fHitMemory = 0;
-    fTrackMemory = 0;
+       if (fHitMemory) delete[] fHitMemory;
+       if (fTrackletMemory) delete[] fTrackletMemory;
+       if (fTrackMemory) delete[] fTrackMemory;
   }
 
+  fHitMemory = fTrackletMemory = fTrackMemory = 0;
+
   fData.Clear();
-  *fNTracklets = 0;
-  *fNTracks = 0 ;
-  *fNTrackHits = 0;
-  *fNOutTracks = 0;
-  *fNOutTrackHits = 0;
+  fCommonMem->fNTracklets = 0;
+  fCommonMem->fNTracks = 0 ;
+  fCommonMem->fNTrackHits = 0;
 }
 
 void AliHLTTPCCATracker::ReadEvent( AliHLTTPCCAClusterData *clusterData )
@@ -248,36 +355,16 @@ void AliHLTTPCCATracker::ReadEvent( AliHLTTPCCAClusterData *clusterData )
 
   //* Convert input hits, create grids, etc.
   fData.InitFromClusterData( *clusterData );
-
   {
-    SetPointersHits( fData.NumberOfHits() ); // to calculate the size
-    fHitMemory = reinterpret_cast<char*> ( new uint4 [ fHitMemorySize/sizeof( uint4 ) + 100] );
+    if (!fIsGPUTracker)
+       {
+               SetPointersHits( fData.NumberOfHits() ); // to calculate the size
+               fHitMemory = reinterpret_cast<char*> ( new uint4 [ fHitMemorySize/sizeof( uint4 ) + 100] );
+       }
     SetPointersHits( fData.NumberOfHits() ); // set pointers for hits
-    *fNTracklets = 0;
-    *fNTracks = 0 ;
-    *fNOutTracks = 0;
-    *fNOutTrackHits = 0;
   }
 }
 
-
-GPUhd() void  AliHLTTPCCATracker::SetPointersCommon()
-{
-  // set all pointers to the event memory
-
-  char *mem = fCommonMemory;
-  AssignMemory( fNTracklets, mem, 1 );
-  AssignMemory( fNTracks, mem, 1 );
-  AssignMemory( fNTrackHits, mem, 1 );
-  AssignMemory( fNOutTracks, mem, 1 );
-  AssignMemory( fNOutTrackHits, mem, 1 );
-
-  // calculate the size
-
-  fCommonMemorySize = mem - fCommonMemory;
-}
-
-
 GPUhd() void  AliHLTTPCCATracker::SetPointersHits( int MaxNHits )
 {
   // set all pointers to the event memory
@@ -286,58 +373,58 @@ GPUhd() void  AliHLTTPCCATracker::SetPointersHits( int MaxNHits )
 
   // extra arrays for tpc clusters
 
-  AssignMemory( fTrackletStartHits, mem, MaxNHits );
+#ifdef HLTCA_GPU_SORT_STARTHITS_2
+  AssignMemory( fTrackletStartHits, mem, MaxNHits + 32);
+#else
+  AssignMemory( fTrackletStartHits, mem, MaxNHits);
+#endif
 
-  // arrays for track hits
+  // calculate the size
 
-  AssignMemory( fTrackHits, mem, 10 * MaxNHits );
+  fHitMemorySize = mem - fHitMemory;
+}
 
-  AssignMemory( fOutTrackHits, mem, 10 * MaxNHits );
+GPUhd() void  AliHLTTPCCATracker::SetPointersTracklets( int MaxNTracklets )
+{
+  // set all pointers to the tracklets memory
+  char *mem = fTrackletMemory;
 
-  // calculate the size
+  // memory for tracklets
 
-  fHitMemorySize = mem - fHitMemory;
+  AssignMemory( fTracklets, mem, MaxNTracklets );
+#ifdef EXTERN_ROW_HITS
+  AssignMemory( fTrackletRowHits, mem, MaxNTracklets * Param().NRows());
+#endif
+
+  fTrackletMemorySize = mem - fTrackletMemory;
 }
 
 
 GPUhd() void  AliHLTTPCCATracker::SetPointersTracks( int MaxNTracks, int MaxNHits )
 {
   // set all pointers to the tracks memory
-
   char *mem = fTrackMemory;
 
-  // memory for tracklets
-
-  AssignMemory( fTracklets, mem, MaxNTracks );
-
   // memory for selected tracks
 
   AssignMemory( fTracks, mem, MaxNTracks );
-
-  // memory for output
-
-  AlignTo < sizeof( void * ) > ( mem );
-  fOutput = reinterpret_cast<AliHLTTPCCASliceOutput *>( mem );
-  mem += AliHLTTPCCASliceOutput::EstimateSize( MaxNTracks, MaxNHits );
-
-  // memory for output tracks
-
-  AssignMemory( fOutTracks, mem, MaxNTracks );
+  AssignMemory( fTrackHits, mem, 2 * MaxNHits );
 
   // calculate the size
 
   fTrackMemorySize = mem - fTrackMemory;
 }
 
-GPUh() int AliHLTTPCCATracker::CheckEmptySlice()
+GPUh() int AliHLTTPCCATracker::CheckEmptySlice() const
 {
+       //Check if the Slice is empty, if so set the output apropriate and tell the reconstuct procesdure to terminate
   if ( NHitsTotal() < 1 ) {
     {
-      SetPointersTracks( 1, 1 ); // to calculate the size
-      fTrackMemory = reinterpret_cast<char*> ( new uint4 [ fTrackMemorySize/sizeof( uint4 ) + 100] );
-      SetPointersTracks( 1, 1 ); // set pointers for tracks
-      fOutput->SetNTracks( 0 );
-      fOutput->SetNTrackClusters( 0 );
+      AliHLTTPCCASliceOutput::Allocate(*fOutput, 0, 0, fOutputControl);
+      AliHLTTPCCASliceOutput* useOutput = *fOutput;
+      if (useOutput == NULL) return(1);
+      useOutput->SetNTracks( 0 );
+      useOutput->SetNTrackClusters( 0 );
     }
 
     return 1;
@@ -347,29 +434,47 @@ GPUh() int AliHLTTPCCATracker::CheckEmptySlice()
 
 void AliHLTTPCCATracker::RunNeighboursFinder()
 {
+       //Run the CPU Neighbours Finder
        AliHLTTPCCAProcess<AliHLTTPCCANeighboursFinder>( Param().NRows(), 1, *this );
 }
 
 void AliHLTTPCCATracker::RunNeighboursCleaner()
 {
+       //Run the CPU Neighbours Cleaner
        AliHLTTPCCAProcess<AliHLTTPCCANeighboursCleaner>( Param().NRows() - 2, 1, *this );
 }
 
 void AliHLTTPCCATracker::RunStartHitsFinder()
 {
+       //Run the CPU Start Hits Finder
        AliHLTTPCCAProcess<AliHLTTPCCAStartHitsFinder>( Param().NRows() - 4, 1, *this );
 }
 
 void AliHLTTPCCATracker::RunTrackletConstructor()
 {
-  AliHLTTPCCAProcess1<AliHLTTPCCATrackletConstructor>( 1, TRACKLET_CONSTRUCTOR_NMEMTHREDS + *fNTracklets, *this );
+       //Run CPU Tracklet Constructor
+  AliHLTTPCCATrackletConstructor::AliHLTTPCCATrackletConstructorNewCPU(*this);
 }
 
 void AliHLTTPCCATracker::RunTrackletSelector()
 {
-  AliHLTTPCCAProcess<AliHLTTPCCATrackletSelector>( 1, *fNTracklets, *this );
+       //Run CPU Tracklet Selector
+  AliHLTTPCCAProcess<AliHLTTPCCATrackletSelector>( 1, fCommonMem->fNTracklets, *this );
 }
 
+#ifdef HLTCA_STANDALONE
+void AliHLTTPCCATracker::StandalonePerfTime(int i)
+{
+  //Query Performance Timer for Standalone Version of Tracker
+  if (fGPUDebugLevel >= 1)
+  {
+    AliHLTTPCCAStandaloneFramework::StandaloneQueryTime(&fPerfTimers[i]);
+  }
+}
+#else
+void AliHLTTPCCATracker::StandalonePerfTime(int /*i*/) {}
+#endif
+
 GPUh() void AliHLTTPCCATracker::Reconstruct()
 {
   //* reconstruction of event
@@ -387,10 +492,14 @@ GPUh() void AliHLTTPCCATracker::Reconstruct()
   //if( fParam.ISlice()<1 ) return; //SG!!!
 
   TStopwatch timer0;
+
+  StandalonePerfTime(0);
+
   if (CheckEmptySlice()) return;
 
 #ifdef DRAW1
-  //if( fParam.ISlice()==15){
+  //if( fParam.ISlice()==2 || fParam.ISlice()==3)
+    {
   AliHLTTPCCADisplay::Instance().ClearView();
   AliHLTTPCCADisplay::Instance().SetSliceView();
   AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
@@ -399,101 +508,105 @@ GPUh() void AliHLTTPCCATracker::Reconstruct()
     AliHLTTPCCADisplay::Instance().DrawSliceHits( kRed, .5 );
     AliHLTTPCCADisplay::Instance().Ask();
   }
-  //}
-#endif
+  }
+#endif //DRAW1
 
-  *fNTracks = 0;
-  *fNTracklets = 0;
+       fCommonMem->fNTracklets = fCommonMem->fNTracks = fCommonMem->fNTrackHits = 0;
 
 #if !defined(HLTCA_GPUCODE)
 
-  if (fGPUDebugLevel >= 3)
+  if (fGPUDebugLevel >= 6)
   {
-         *fGPUDebugOut << endl << endl << "Slice: " << Param().ISlice() << endl;
+         *fGPUDebugOut << std::endl << std::endl << "Slice: " << Param().ISlice() << std::endl;
+         *fGPUDebugOut << "Slice Data:" << std::endl;
+         DumpSliceData(*fGPUDebugOut);
   }
 
+  StandalonePerfTime(1);
+
   RunNeighboursFinder();
 
-  if (fGPUDebugLevel >= 3)
-  {
-         *fGPUDebugOut << "Neighbours Finder:" << endl;
-         DumpLinks(*fGPUDebugOut);
-  }
+  StandalonePerfTime(2);
+
+  if (fGPUDebugLevel >= 6) DumpLinks(*fGPUDebugOut);
+  
 #ifdef HLTCA_INTERNAL_PERFORMANCE
   //if( Param().ISlice()<=2 )
   //AliHLTTPCCAPerformance::Instance().LinkPerformance( Param().ISlice() );
 #endif
 
 
-#ifdef DRAW
+#ifdef DRAW1
   if ( NHitsTotal() > 0 ) {
     AliHLTTPCCADisplay::Instance().DrawSliceLinks( -1, -1, 1 );
     AliHLTTPCCADisplay::Instance().Ask();
   }
-#endif
+#endif //DRAW1
 
   RunNeighboursCleaner();
 
-  if (fGPUDebugLevel >= 3)
-  {
-         *fGPUDebugOut << "Neighbours Cleaner:" << endl;
-         DumpLinks(*fGPUDebugOut);
-  }
+  StandalonePerfTime(3);
+
+  if (fGPUDebugLevel >= 6) DumpLinks(*fGPUDebugOut);
 
   RunStartHitsFinder();
 
-  if (fGPUDebugLevel >= 3)
-  {
-         *fGPUDebugOut << "Start Hits: (" << *fNTracklets << ")" << endl;
-         DumpStartHits(*fGPUDebugOut);
-  }
-  
-  if (fGPUDebugLevel >= 2) printf("%3d ", *fNTracklets);
+  StandalonePerfTime(4);
+  StandalonePerfTime(5);
 
+  if (fGPUDebugLevel >= 6) DumpStartHits(*fGPUDebugOut);
+  
   fData.ClearHitWeights();
 
-  SetPointersTracks( *fNTracklets * 2, NHitsTotal() ); // to calculate the size
+  SetPointersTracklets( fCommonMem->fNTracklets * 2 ); // to calculate the size
+  fTrackletMemory = reinterpret_cast<char*> ( new uint4 [ fTrackletMemorySize/sizeof( uint4 ) + 100] );
+  SetPointersTracklets( fCommonMem->fNTracklets * 2 ); // set pointers for hits
+
+  SetPointersTracks( fCommonMem->fNTracklets * 2, NHitsTotal() ); // to calculate the size
   fTrackMemory = reinterpret_cast<char*> ( new uint4 [ fTrackMemorySize/sizeof( uint4 ) + 100] );
-  SetPointersTracks( *fNTracklets * 2, NHitsTotal() ); // set pointers for hits
+  SetPointersTracks( fCommonMem->fNTracklets * 2, NHitsTotal() ); // set pointers for hits
+
+  StandalonePerfTime(6);
+  StandalonePerfTime(7);
 
   RunTrackletConstructor();
 
-  if (fGPUDebugLevel >= 3)
-  {
-         *fGPUDebugOut << "Tracklet Hits:" << endl;
-         DumpTrackletHits(*fGPUDebugOut);
-  }
+  StandalonePerfTime(8);
+
+  if (fGPUDebugLevel >= 6) DumpTrackletHits(*fGPUDebugOut);
+  if (fGPUDebugLevel >= 6) DumpHitWeights(*fGPUDebugOut);
 
   //std::cout<<"Slice "<<Param().ISlice()<<": NHits="<<NHitsTotal()<<", NTracklets="<<*NTracklets()<<std::endl;
 
   RunTrackletSelector();
 
+  StandalonePerfTime(9);
+
   //std::cout<<"Slice "<<Param().ISlice()<<": N start hits/tracklets/tracks = "<<nStartHits<<" "<<nStartHits<<" "<<*fNTracks<<std::endl;
 
-  if (fGPUDebugLevel >= 3)
-  {
-         *fGPUDebugOut << "Track Hits: (" << *NTracks() << ")" << endl;
-         DumpTrackHits(*fGPUDebugOut);
-  }
+  if (fGPUDebugLevel >= 6) DumpTrackHits(*fGPUDebugOut);
 
   //std::cout<<"Memory used for slice "<<fParam.ISlice()<<" : "<<fCommonMemorySize/1024./1024.<<" + "<<fHitMemorySize/1024./1024.<<" + "<<fTrackMemorySize/1024./1024.<<" = "<<( fCommonMemorySize+fHitMemorySize+fTrackMemorySize )/1024./1024.<<" Mb "<<std::endl;
 
   WriteOutput();
 
+  StandalonePerfTime(10);
+
 #endif
 
-#ifdef DRAW
+#ifdef DRAW1
   {
     AliHLTTPCCADisplay &disp = AliHLTTPCCADisplay::Instance();
     AliHLTTPCCATracker &slice = *this;
-    std::cout << "N out tracks = " << *slice.NOutTracks() << std::endl;
-    //disp.Ask();
+    std::cout << "N out tracks = " << slice.NOutTracks() << std::endl;
+    AliHLTTPCCADisplay::Instance().SetSliceView();
     AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
     AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
-    disp.DrawSliceHits( -1, .5 );
-    for ( int itr = 0; itr < *slice.NOutTracks(); itr++ ) {
+    disp.DrawSliceHits( kRed, .5 );
+    disp.Ask();
+    for ( int itr = 0; itr < slice.NOutTracks(); itr++ ) {
       std::cout << "track N " << itr << ", nhits=" << slice.OutTracks()[itr].NHits() << std::endl;
-      disp.DrawSliceOutTrack( itr, kBlue );
+      disp.DrawSliceOutTrack( itr, kBlue );      
       //disp.Ask();
       //int id = slice.OutTracks()[itr].OrigTrackID();
       //AliHLTTPCCATrack &tr = Tracks()[id];
@@ -506,7 +619,7 @@ GPUh() void AliHLTTPCCATracker::Reconstruct()
     }
     disp.Ask();
   }
-#endif
+#endif //DRAW1
 
   timer0.Stop();
   fTimers[0] = timer0.CpuTime() / 100.;
@@ -520,130 +633,81 @@ GPUh() void AliHLTTPCCATracker::WriteOutput()
   TStopwatch timer;
 
   //cout<<"output: nTracks = "<<*fNTracks<<", nHitsTotal="<<NHitsTotal()<<std::endl;
+  
+  if (fOutputControl == NULL) fOutputControl = new AliHLTTPCCASliceOutput::outputControlStruct;
+  AliHLTTPCCASliceOutput::Allocate(*fOutput, fCommonMem->fNTracks, fCommonMem->fNTrackHits, fOutputControl);
+  AliHLTTPCCASliceOutput* useOutput = *fOutput;
+  if (useOutput == NULL) return;
 
-  fOutput->SetNTracks( *fNTracks );
-  fOutput->SetNTrackClusters( *fNTrackHits );
-  fOutput->SetPointers();
+  useOutput->SetNTracks( fCommonMem->fNTracks );
+  useOutput->SetNTrackClusters( fCommonMem->fNTrackHits );
 
   int nStoredHits = 0;
 
-  for ( int iTr = 0; iTr < *fNTracks; iTr++ ) {
+  for ( int iTr = 0; iTr < fCommonMem->fNTracks; iTr++ ) {
     AliHLTTPCCATrack &iTrack = fTracks[iTr];
-
     AliHLTTPCCASliceTrack out;
     out.SetFirstClusterRef( nStoredHits );
     out.SetNClusters( iTrack.NHits() );
     out.SetParam( iTrack.Param() );
-
-    fOutput->SetTrack( iTr, out );
-
+   
+    useOutput->SetTrack( iTr, out );
+   
     int iID = iTrack.FirstHitID();
     for ( int ith = 0; ith < iTrack.NHits(); ith++ ) {
       const AliHLTTPCCAHitId &ic = fTrackHits[iID + ith];
       int iRow = ic.RowIndex();
       int ih = ic.HitIndex();
-
+      
       const AliHLTTPCCARow &row = fData.Row( iRow );
-
+      
       //float y0 = row.Grid().YMin();
       //float z0 = row.Grid().ZMin();
       //float stepY = row.HstepY();
       //float stepZ = row.HstepZ();
       //float x = row.X();
-
+      
       //const uint4 *tmpint4 = RowData() + row.FullOffset();
       //const ushort2 *hits = reinterpret_cast<const ushort2*>(tmpint4);
       //ushort2 hh = hits[ih];
       //float y = y0 + hh.x*stepY;
       //float z = z0 + hh.y*stepZ;
-
+      
       int clusterIndex = fData.ClusterDataIndex( row, ih );
       int clusterRowIndex = clusterIndex - fClusterData->RowOffset( iRow );
-
+      
       if ( clusterIndex < 0 || clusterIndex >= fClusterData->NumberOfClusters() ) {
-        //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
-        //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
-        //abort();
-        continue;
+       //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
+       //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
+       //abort();
+       continue;
       }
       if ( clusterRowIndex < 0 || clusterRowIndex >= fClusterData->NumberOfClusters( iRow ) ) {
-        //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
-        //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
-        //abort();
-        continue;
+       //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
+       //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
+       //abort();
+       continue;
       }
-
+      
       float origX = fClusterData->X( clusterIndex );
       float origY = fClusterData->Y( clusterIndex );
       float origZ = fClusterData->Z( clusterIndex );
-
-
+      
       int id = fClusterData->Id( clusterIndex );
-
-      unsigned short hPackedYZ = 0;
-      UChar_t hPackedAmp = 0;
-      float2 hUnpackedYZ;
-      hUnpackedYZ.x = origY;
-      hUnpackedYZ.y = origZ;
-      float hUnpackedX = origX;
-
-      fOutput->SetClusterId( nStoredHits, id  );
-      fOutput->SetClusterRow( nStoredHits, ( unsigned char ) iRow  );
-      fOutput->SetClusterPackedYZ( nStoredHits, hPackedYZ );
-      fOutput->SetClusterPackedAmp( nStoredHits, hPackedAmp );
-      fOutput->SetClusterUnpackedYZ( nStoredHits, hUnpackedYZ );
-      fOutput->SetClusterUnpackedX( nStoredHits, hUnpackedX );
+      AliHLTTPCCACompressedCluster cXYZ = AliHLTTPCCADataCompressor::PackXYZ( iRow, origX, origY, origZ );
+      
+      //float2 hUnpackedYZ;
+      //hUnpackedYZ.x = origY;
+      //hUnpackedYZ.y = origZ;
+      //float hUnpackedX = origX;
+      
+      useOutput->SetClusterId( nStoredHits, id  );
+      useOutput->SetClusterRow( nStoredHits, ( unsigned char ) iRow  );
+      useOutput->SetClusterPackedXYZ( nStoredHits, cXYZ );
       nStoredHits++;
     }
   }
 
-
-  // old stuff
-
-  *fNOutTrackHits = 0;
-  *fNOutTracks = 0;
-
-
-  for ( int iTr = 0; iTr < *fNTracks; iTr++ ) {
-
-    const AliHLTTPCCATrack &iTrack = fTracks[iTr];
-
-    //std::cout<<"iTr = "<<iTr<<", nHits="<<iTrack.NHits()<<std::endl;
-
-    //if( !iTrack.Alive() ) continue;
-    if ( iTrack.NHits() < 3 ) continue;
-    AliHLTTPCCAOutTrack &out = fOutTracks[*fNOutTracks];
-    out.SetFirstHitRef( *fNOutTrackHits );
-    out.SetNHits( 0 );
-    out.SetOrigTrackID( iTr );
-    out.SetStartPoint( iTrack.Param() );
-    out.SetEndPoint( iTrack.Param() );
-
-    int iID = iTrack.FirstHitID();
-    int nOutTrackHitsOld = *fNOutTrackHits;
-
-    for ( int ith = 0; ith < iTrack.NHits(); ith++ ) {
-      const AliHLTTPCCAHitId &ic = fTrackHits[iID + ith];
-      const AliHLTTPCCARow &row = Row( ic );
-      int ih = ic.HitIndex();
-      fOutTrackHits[*fNOutTrackHits] = HitInputID( row, ih );
-      ( *fNOutTrackHits )++;
-      //std::cout<<"write i,row,hit,id="<<ith<<", "<<ID2IRow(ic)<<", "<<ih<<", "<<HitInputID( row, ih )<<std::endl;
-      if ( *fNOutTrackHits >= 10*NHitsTotal() ) {
-        std::cout << "fNOutTrackHits>NHitsTotal()" << std::endl;
-        //exit(0);
-        return;//SG!!!
-      }
-      out.SetNHits( out.NHits() + 1 );
-    }
-    if ( out.NHits() >= 2 ) {
-      ( *fNOutTracks )++;
-    } else {
-      ( *fNOutTrackHits ) = nOutTrackHitsOld;
-    }
-  }
-
-
   timer.Stop();
   fTimers[5] += timer.CpuTime();
 }
@@ -780,6 +844,8 @@ GPUd() void AliHLTTPCCATracker::GetErrors2( int iRow, float z, float sinPhi, flo
   //
 
   fParam.GetClusterErrors2( iRow, z, sinPhi, cosPhi, DzDs, Err2Y, Err2Z );
+  Err2Y*=fParam.ClusterError2CorrectionY();
+  Err2Z*=fParam.ClusterError2CorrectionZ();
 }
 
 GPUd() void AliHLTTPCCATracker::GetErrors2( int iRow, const AliHLTTPCCATrackParam &t, float &Err2Y, float &Err2Z ) const
@@ -821,79 +887,13 @@ GPUh() void AliHLTTPCCATracker::WriteEvent( std::ostream &out )
   }
 }
 
-GPUh() void AliHLTTPCCATracker::WriteTracks( std::ostream &out )
+GPUh() void AliHLTTPCCATracker::WriteTracks( std::ostream &/*out*/ )
 {
-  //* Write tracks to file
-
-  out << fTimers[0] << std::endl;
-  out << *fNOutTrackHits << std::endl;
-  for ( int ih = 0; ih < *fNOutTrackHits; ih++ ) {
-    out << fOutTrackHits[ih] << " ";
-  }
-  out << std::endl;
-
-  out << *fNOutTracks << std::endl;
-
-  for ( int itr = 0; itr < *fNOutTracks; itr++ ) {
-    AliHLTTPCCAOutTrack &t = fOutTracks[itr];
-    AliHLTTPCCATrackParam p1 = t.StartPoint();
-    AliHLTTPCCATrackParam p2 = t.EndPoint();
-    out << t.NHits() << " ";
-    out << t.FirstHitRef() << " ";
-    out << t.OrigTrackID() << " ";
-    out << std::endl;
-    out << p1.X() << " ";
-    out << p1.SignCosPhi() << " ";
-    out << p1.Chi2() << " ";
-    out << p1.NDF() << std::endl;
-    for ( int i = 0; i < 5; i++ ) out << p1.Par()[i] << " ";
-    out << std::endl;
-    for ( int i = 0; i < 15; i++ ) out << p1.Cov()[i] << " ";
-    out << std::endl;
-    out << p2.X() << " ";
-    out << p2.SignCosPhi() << " ";
-    out << p2.Chi2() << " ";
-    out << p2.NDF() << std::endl;
-    for ( int i = 0; i < 5; i++ ) out << p2.Par()[i] << " ";
-    out << std::endl;
-    for ( int i = 0; i < 15; i++ ) out << p2.Cov()[i] << " ";
-    out << std::endl;
-  }
+  //* Write tracks to file --- dummy
 }
 
-GPUh() void AliHLTTPCCATracker::ReadTracks( std::istream &in )
+GPUh() void AliHLTTPCCATracker::ReadTracks( std::istream &/*in*/ )
 {
-  //* Read tracks  from file
-  in >> fTimers[0];
-  in >> *fNOutTrackHits;
-
-  for ( int ih = 0; ih < *fNOutTrackHits; ih++ ) {
-    in >> fOutTrackHits[ih];
-  }
-  in >> *fNOutTracks;
-
-  for ( int itr = 0; itr < *fNOutTracks; itr++ ) {
-    AliHLTTPCCAOutTrack &t = fOutTracks[itr];
-    AliHLTTPCCATrackParam p1, p2;
-    int i;
-    float f;
-    in >> i; t.SetNHits( i );
-    in >> i; t.SetFirstHitRef( i );
-    in >> i; t.SetOrigTrackID( i );
-    in >> f; p1.SetX( f );
-    in >> f; p1.SetSignCosPhi( f );
-    in >> f; p1.SetChi2( f );
-    in >> i; p1.SetNDF( i );
-    for ( int j = 0; j < 5; j++ ) { in >> f; p1.SetPar( j, f ); }
-    for ( int j = 0; j < 15; j++ ) { in >> f; p1.SetCov( j, f ); }
-    in >> f; p2.SetX( f );
-    in >> f; p2.SetSignCosPhi( f );
-    in >> f; p2.SetChi2( f );
-    in >> i; p2.SetNDF( i );
-    for ( int j = 0; j < 5; j++ ) { in >> f; p2.SetPar( j, f ); }
-    for ( int j = 0; j < 15; j++ ) { in >> f; p2.SetCov( j, f ); }
-    t.SetStartPoint( p1 );
-    t.SetEndPoint( p2 );
-  }
+  //* Read tracks  from file -- dummy
 }
 #endif