]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCATracker.cxx
0f857fd3edb0d0a8939cfdecc4257602d8a6d8be
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCATracker.cxx
1 // @(#) $Id$
2 // **************************************************************************
3 // This file is property of and copyright by the ALICE HLT Project          * 
4 // ALICE Experiment at CERN, All rights reserved.                           *
5 //                                                                          *
6 // Primary Authors: Sergey Gorbunov <sergey.gorbunov@kip.uni-heidelberg.de> *
7 //                  Ivan Kisel <kisel@kip.uni-heidelberg.de>                *
8 //                  for The ALICE HLT Project.                              *
9 //                                                                          *
10 // Permission to use, copy, modify and distribute this software and its     *
11 // documentation strictly for non-commercial purposes is hereby granted     *
12 // without fee, provided that the above copyright notice appears in all     *
13 // copies and that both the copyright notice and this permission notice     *
14 // appear in the supporting documentation. The authors make no claims       *
15 // about the suitability of this software for any purpose. It is            *
16 // provided "as is" without express or implied warranty.                    *
17 //                                                                          *
18 //***************************************************************************
19
20 #include "AliHLTTPCCATracker.h"
21 #include "AliHLTTPCCAOutTrack.h"
22 #include "AliHLTTPCCAGrid.h"
23 #include "AliHLTTPCCARow.h"
24 #include "AliHLTTPCCATrack.h"
25 #include "AliHLTTPCCATracklet.h"
26 #include "AliHLTTPCCAMath.h"
27 #include "AliHLTTPCCAHit.h"
28
29 #include "TStopwatch.h"
30 #include "AliHLTTPCCAHitArea.h"
31 #include "AliHLTTPCCANeighboursFinder.h"
32 #include "AliHLTTPCCANeighboursCleaner.h"
33 #include "AliHLTTPCCAStartHitsFinder.h"
34 #include "AliHLTTPCCATrackletConstructor.h"
35 #include "AliHLTTPCCATrackletSelector.h"
36 #include "AliHLTTPCCAProcess.h"
37 #include "AliHLTTPCCAUsedHitsInitialiser.h"
38 #include "AliHLTTPCCASliceTrack.h"
39 #include "AliHLTTPCCASliceOutput.h"
40 #include "AliHLTTPCCADataCompressor.h"
41
42 #include "AliHLTTPCCATrackParam.h"
43
44 #if !defined(HLTCA_GPUCODE)
45 #include <iostream>
46 #endif
47
48 //#define DRAW
49
50 #ifdef DRAW
51   #include "AliHLTTPCCADisplay.h"
52 #endif //DRAW
53
54 #ifdef HLTCA_INTERNAL_PERFORMANCE
55  #include "AliHLTTPCCAPerformance.h"
56 #endif
57
58
59 ClassImp(AliHLTTPCCATracker)
60
61 #if !defined(HLTCA_GPUCODE)  
62
63 AliHLTTPCCATracker::AliHLTTPCCATracker()
64   :
65   fParam(),
66   fNHitsTotal(0),  
67   fCommonMemory(0), 
68   fCommonMemorySize(0),
69   fHitMemory(0), 
70   fHitMemorySize(0),
71   fTrackMemory(0), 
72   fTrackMemorySize(0),
73   fInputEvent(0),     
74   fInputEventSize(0), 
75   fRowData(0),     
76   fRowDataSize(0), 
77   fHitInputIDs(0), 
78   fHitWeights(0),  
79   fNTracklets(0),
80   fTrackletStartHits(0),
81   fTracklets(0), 
82   fNTracks(0),   
83   fTracks(0), 
84   fNTrackHits(0),
85   fTrackHits(0),
86   fOutput(0),
87   fNOutTracks(0),
88   fOutTracks(0), 
89   fNOutTrackHits(0),
90   fOutTrackHits(0),
91   fTmpHitInputIDs(0)
92 {
93   // constructor
94 }
95
96 AliHLTTPCCATracker::AliHLTTPCCATracker( const AliHLTTPCCATracker& )
97   :
98   fParam(),
99   fNHitsTotal(0),
100   fCommonMemory(0), 
101   fCommonMemorySize(0),
102   fHitMemory(0), 
103   fHitMemorySize(0),
104   fTrackMemory(0), 
105   fTrackMemorySize(0),
106   fInputEvent(0),     
107   fInputEventSize(0), 
108   fRowData(0),     
109   fRowDataSize(0), 
110   fHitInputIDs(0), 
111   fHitWeights(0),  
112   fNTracklets(0),
113   fTrackletStartHits(0),
114   fTracklets(0), 
115   fNTracks(0),   
116   fTracks(0), 
117   fNTrackHits(0),
118   fTrackHits(0),
119   fOutput(0),
120   fNOutTracks(0),
121   fOutTracks(0), 
122   fNOutTrackHits(0),
123   fOutTrackHits(0),
124   fTmpHitInputIDs(0)
125 {
126   // dummy
127 }
128
129 AliHLTTPCCATracker &AliHLTTPCCATracker::operator=( const AliHLTTPCCATracker& )
130 {
131   // dummy
132   fCommonMemory = 0;
133   fHitMemory = 0;
134   fTrackMemory = 0;
135   return *this;
136 }
137
138 GPUd() AliHLTTPCCATracker::~AliHLTTPCCATracker()
139 {
140   // destructor
141   if( fCommonMemory ) delete[] fCommonMemory;
142   if( fHitMemory ) delete[] fHitMemory;
143   if( fTrackMemory ) delete[] fTrackMemory;
144   if( fTmpHitInputIDs ) delete[] fTmpHitInputIDs;
145 }
146 #endif
147
148
149
150 // ----------------------------------------------------------------------------------
151 GPUd() void AliHLTTPCCATracker::Initialize( const AliHLTTPCCAParam &param )
152 {
153   // initialisation
154   fParam = param;
155   fParam.Update();
156   for( Int_t irow=0; irow<fParam.NRows(); irow++ ){
157     fRows[irow].SetX( fParam.RowX(irow) );
158     fRows[irow].SetMaxY( CAMath::Tan( fParam.DAlpha()/2.)*fRows[irow].X() );
159   }
160   StartEvent();
161 }
162
163 GPUd() void AliHLTTPCCATracker::StartEvent()
164 {
165   // start new event and fresh the memory  
166
167   if( !fCommonMemory ){
168     SetPointersCommon(); // just to calculate the size
169     fCommonMemory = reinterpret_cast<Char_t*> ( new uint4 [ fCommonMemorySize/sizeof(uint4) + 100] );
170     SetPointersCommon();// set pointers
171   }
172   
173   if( fHitMemory ) delete[] fHitMemory;
174   fHitMemory = 0;
175   if( fTrackMemory ) delete[] fTrackMemory;
176   fTrackMemory = 0;
177
178   fNHitsTotal = 0;
179   *fNTracklets = 0;
180   *fNTracks = 0 ;
181   *fNTrackHits = 0;
182   *fNOutTracks = 0;
183   *fNOutTrackHits = 0;
184   if( fTmpHitInputIDs ) delete[] fTmpHitInputIDs;
185   fTmpHitInputIDs = 0;
186 }
187
188
189
190 GPUhd() void  AliHLTTPCCATracker::SetPointersCommon()
191 {
192   // set all pointers to the event memory
193
194   ULong_t mem = (ULong_t) fCommonMemory;  
195   UInt_t sI = sizeof(Int_t);
196   
197   // set common memory
198
199   fNTracklets = (Int_t*) mem;
200   mem+= sI;
201   fNTracks = (Int_t*) mem;
202   mem+= sI;
203   fNTrackHits = (Int_t*) mem;
204   mem+= sI;  
205   fNOutTracks = (Int_t*) mem;
206   mem+= sI;
207   fNOutTrackHits = (Int_t*) mem;
208   mem+= sI;
209
210   // calculate the size
211
212   fCommonMemorySize = mem - (ULong_t) fCommonMemory;
213 }
214
215
216 GPUhd() void  AliHLTTPCCATracker::SetPointersHits( Int_t MaxNHits )
217 {
218   // set all pointers to the event memory
219
220   Int_t gridSizeTotal = 2*(2*MaxNHits + 10*Param().NRows());
221   //gridSizeTotal *=100;//SG!!!
222
223   ULong_t mem = (ULong_t) fHitMemory;  
224   UInt_t sI = sizeof(Int_t);
225   UInt_t sF = sizeof(Float_t);
226   UInt_t sS = sizeof(Short_t);
227   UInt_t s4 = sizeof(uint4);
228    
229   // set input event
230
231   mem = ( mem/s4 + 1 )*s4;
232   fInputEvent = (Char_t*) mem;  
233   fInputEventSize = (1+fParam.NRows()*2 + 1)*sI + (MaxNHits*2)*sF;
234   mem+= fInputEventSize;
235
236   // set cluster data for TPC rows
237
238   mem = ( mem/s4 + 1 )*s4;
239   fRowData = (uint4*) mem;
240   fRowDataSize = ( 2*MaxNHits*sS +  //  yz
241                    gridSizeTotal*sS + // grid
242                    2*MaxNHits*sS +  // link up,link down
243                    fParam.NRows()*s4   // row alignment
244                    );
245   mem += fRowDataSize;
246
247   // extra arrays for tpc clusters
248   
249   mem = ( mem/sI + 1 )*sI;
250
251   fHitInputIDs = (Int_t*) mem;
252   mem+= MaxNHits*sI;
253
254   fTrackletStartHits = (Int_t*) mem;
255   mem+= MaxNHits*sI;
256
257   fHitWeights = (Int_t*) mem;
258   mem+=  MaxNHits*sI;
259
260   // arrays for track hits
261
262   fTrackHits = (Int_t*) mem;
263   mem+= 10*MaxNHits*sI;//SG!!!
264   
265   fOutTrackHits = (Int_t*) mem;
266   mem+= 10*MaxNHits*sI; //SG!!!
267
268   // calculate the size
269
270   fHitMemorySize = mem - (ULong_t) fHitMemory;
271 }
272
273
274 GPUhd() void  AliHLTTPCCATracker::SetPointersTracks( Int_t MaxNTracks, Int_t MaxNHits )
275 {
276   // set all pointers to the tracks memory
277
278   ULong_t mem = (ULong_t) fTrackMemory;  
279   
280   // memory for tracklets
281
282   mem = ( mem/sizeof(AliHLTTPCCATracklet) + 1 )*sizeof(AliHLTTPCCATracklet);
283   fTracklets = (AliHLTTPCCATracklet *) mem;
284   mem+= MaxNTracks*sizeof(AliHLTTPCCATracklet);  
285   
286   // memory for selected tracks
287   
288   mem = ( mem/sizeof(AliHLTTPCCATrack) + 1 )*sizeof(AliHLTTPCCATrack);
289   fTracks = (AliHLTTPCCATrack*) mem;
290   mem+= MaxNTracks*sizeof(AliHLTTPCCATrack);  
291   
292   // memory for output
293
294   mem = ( mem/sizeof(AliHLTTPCCASliceOutput) + 1 )*sizeof(AliHLTTPCCASliceOutput);
295   fOutput = (AliHLTTPCCASliceOutput*) mem;
296   mem+= AliHLTTPCCASliceOutput::EstimateSize(MaxNTracks, MaxNHits);
297   
298   // memory for output tracks
299
300   mem = ( mem/sizeof(AliHLTTPCCAOutTrack) + 1 )*sizeof(AliHLTTPCCAOutTrack);
301   
302   fOutTracks = (AliHLTTPCCAOutTrack*) mem;
303   mem+= MaxNTracks*sizeof(AliHLTTPCCAOutTrack);  
304
305   // calculate the size
306
307   fTrackMemorySize = mem - (ULong_t) fTrackMemory;
308 }
309
310
311
312 GPUd() void AliHLTTPCCATracker::ReadEvent( const Int_t *RowFirstHit, const Int_t *RowNHits, const Float_t *Y, const Float_t *Z, Int_t NHits )
313 {
314   //* Read event
315
316   StartEvent();
317   
318   fNHitsTotal = NHits;
319   
320   {
321     SetPointersHits(NHits); // to calculate the size
322     fHitMemory = reinterpret_cast<Char_t*> ( new uint4 [ fHitMemorySize/sizeof(uint4) + 100] );   
323     SetPointersHits(NHits); // set pointers for hits
324     *fNTracklets = 0;
325     *fNTracks = 0 ;
326     *fNOutTracks = 0;
327     *fNOutTrackHits = 0;
328   }
329
330   reinterpret_cast<Int_t*>( fInputEvent )[0] = fParam.NRows();
331   reinterpret_cast<Int_t*>( fInputEvent )[1+fParam.NRows()*2] = NHits;
332   Int_t *rowHeaders = reinterpret_cast<Int_t*>( fInputEvent ) +1;
333   Float_t *hitsYZ = reinterpret_cast<Float_t*>( fInputEvent ) + 1+fParam.NRows()*2+1;
334   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
335     rowHeaders[iRow*2  ] = RowFirstHit[iRow];
336     rowHeaders[iRow*2+1] = RowNHits[iRow];
337   }
338   for( Int_t iHit=0; iHit<NHits; iHit++ ){
339     hitsYZ[iHit*2  ] = Y[iHit];
340     hitsYZ[iHit*2+1] = Z[iHit];
341   }
342
343   //SG cell finder - test code
344
345   if( fTmpHitInputIDs ) delete[] fTmpHitInputIDs;
346   fTmpHitInputIDs = new Int_t [NHits];
347   const Float_t areaY = .5;
348   const Float_t areaZ = .5;
349   Int_t newRowNHitsTotal = 0;
350   Bool_t *usedHits = new Bool_t [NHits];
351   for( Int_t iHit=0; iHit<NHits; iHit++ ) usedHits[iHit] = 0;
352   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
353     rowHeaders[iRow*2  ] = newRowNHitsTotal; // new first hit
354     rowHeaders[iRow*2+1] = 0; // new N hits
355     Int_t newRowNHits = 0;
356     Int_t oldRowFirstHit = RowFirstHit[iRow];
357     Int_t oldRowLastHit = oldRowFirstHit + RowNHits[iRow];
358     for( Int_t iHit=oldRowFirstHit; iHit<oldRowLastHit; iHit++ ){
359       if( usedHits[iHit] ) continue;
360       Float_t y0 = Y[iHit];
361       Float_t z0 = Z[iHit];
362       Float_t cy = y0;
363       Float_t cz = z0;
364       Int_t nclu = 1;
365       usedHits[iHit]=1;
366       if(0) for( Int_t jHit=iHit+1; jHit<oldRowLastHit; jHit++ ){//SG!!!
367         //if( usedHits[jHit] ) continue;
368         Float_t dy = Y[jHit] - y0;
369         Float_t dz = Z[jHit] - z0;
370         if( CAMath::Abs(dy)<areaY && CAMath::Abs(dz)<areaZ ){
371           cy+=Y[jHit];
372           cz+=Z[jHit];
373           nclu++;
374           usedHits[jHit]=1;
375         }
376       }
377       Int_t id = newRowNHitsTotal+newRowNHits;
378       hitsYZ[id*2  ] = cy/nclu;
379       hitsYZ[id*2+1] = cz/nclu;
380       fTmpHitInputIDs[id] = iHit;
381       newRowNHits++;
382     }
383     rowHeaders[iRow*2+1] = newRowNHits;
384     newRowNHitsTotal+=newRowNHits;
385   }
386   fNHitsTotal = newRowNHitsTotal;
387   reinterpret_cast<Int_t*>( fInputEvent )[1+fParam.NRows()*2] = newRowNHitsTotal;
388
389   delete[] usedHits;  
390   SetupRowData();
391 }
392
393
394 GPUd() void AliHLTTPCCATracker::SetupRowData()
395 {
396   //* Convert input hits, create grids, etc.
397
398   fNHitsTotal = reinterpret_cast<Int_t*>( fInputEvent )[1+fParam.NRows()*2];
399   Int_t *rowHeaders = reinterpret_cast<Int_t*>( fInputEvent ) +1;
400   Float_t *hitsYZ = reinterpret_cast<Float_t*>( fInputEvent ) + 1+fParam.NRows()*2+1;
401
402   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
403     
404     AliHLTTPCCARow &row = fRows[iRow];
405     row.SetFirstHit( rowHeaders[iRow*2] );
406     row.SetNHits( rowHeaders[iRow*2+1] );
407     Float_t yMin=1.e3, yMax=-1.e3, zMin=1.e3, zMax=-1.e3;
408     Int_t nGrid =  row.NHits();
409     for( Int_t i=0; i<row.NHits(); i++ ){
410       Int_t j = row.FirstHit()+i;
411       Float_t y = hitsYZ[j*2];
412       Float_t z = hitsYZ[j*2+1];
413       if( yMax < y ) yMax = y;
414       if( yMin > y ) yMin = y;
415       if( zMax < z ) zMax = z;
416       if( zMin > z ) zMin = z;
417     }
418     if( nGrid <= 0 ){
419       yMin = yMax = zMin = zMax = 0;
420       nGrid = 1;
421     }
422
423     AliHLTTPCCAGrid grid;
424     grid.Create( yMin, yMax, zMin, zMax, nGrid );
425     
426     Float_t sy = ( CAMath::Abs( grid.StepYInv() ) >1.e-4 ) ?1./grid.StepYInv() :1;
427     Float_t sz = ( CAMath::Abs( grid.StepZInv() ) >1.e-4 ) ?1./grid.StepZInv() :1;
428     
429     //cout<<"grid n = "<<row.Grid().N()<<" "<<sy<<" "<<sz<<" "<<yMin<<" "<<yMax<<" "<<zMin<<" "<<zMax<<endl;
430     
431     Bool_t recreate=0;
432     if( sy < 2. ) { recreate = 1; sy = 2; }
433     if( sz < 2. ) { recreate = 1; sz = 2; }
434     //recreate = 1;//SG!!!
435     //sy=2;
436     //sz=2;
437     if( recreate ) grid.Create( yMin, yMax, zMin, zMax, sy, sz );
438     row.SetGrid( grid );
439   }
440
441   AliHLTTPCCAHit ffHits[fNHitsTotal];
442
443   Int_t rowDataOffset = 0;
444
445   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
446
447     AliHLTTPCCARow &row = fRows[iRow];
448     const AliHLTTPCCAGrid &grid = row.Grid();
449
450     Int_t c[grid.N()+3+10];
451     Int_t bins[row.NHits()];
452     Int_t filled[ row.Grid().N() +3+10 ];
453
454     for( UInt_t bin=0; bin<row.Grid().N()+3; bin++ ) filled[bin] = 0;  
455
456     for( Int_t i=0; i<row.NHits(); i++ ){
457       Int_t j = row.FirstHit()+i;
458       Int_t bin = row.Grid().GetBin( hitsYZ[2*j], hitsYZ[2*j+1] );
459       bins[i] = bin;
460       filled[bin]++;
461     }
462
463     {
464       Int_t n=0;
465       for( UInt_t bin=0; bin<row.Grid().N()+3; bin++ ){
466         c[bin] = n;
467         n+=filled[bin];
468       }
469     }
470
471     for( Int_t i=0; i<row.NHits(); i++ ){ 
472       Int_t bin = bins[i];
473       Int_t ind = c[bin] + filled[bin]-1;
474
475      AliHLTTPCCAHit &h = ffHits[row.FirstHit()+ind];
476       fHitInputIDs[row.FirstHit()+ind] = fTmpHitInputIDs[row.FirstHit()+i];
477       h.SetY( hitsYZ[2*(row.FirstHit()+i)] );
478       h.SetZ( hitsYZ[2*(row.FirstHit()+i)+1] );
479       filled[bin]--;
480     }
481
482     {
483       Float_t y0 = row.Grid().YMin();
484       Float_t stepY = (row.Grid().YMax() - y0)*(1./65535.);
485       Float_t z0 = row.Grid().ZMin();
486       Float_t stepZ = (row.Grid().ZMax() - z0)*(1./65535.);
487       if( stepY<1.e-4 ) stepY = 1.e-4;
488       if( stepZ<1.e-4 ) stepZ = 1.e-4;
489       Float_t stepYi = 1./stepY;
490       Float_t stepZi = 1./stepZ;
491       
492       row.SetHy0( y0 );
493       row.SetHz0( z0 );
494       row.SetHstepY( stepY );
495       row.SetHstepZ( stepZ );
496       row.SetHstepYi( stepYi );
497       row.SetHstepZi( stepZi );
498       
499       row.SetFullOffset( rowDataOffset );
500       ushort2 *p= (ushort2*)( fRowData + row.FullOffset() );
501       for( Int_t ih=0; ih<row.NHits(); ih++ ){
502         Int_t ihTot = row.FirstHit()+ih;
503         AliHLTTPCCAHit &hh = ffHits[ihTot];
504         Float_t xx = ((hh.Y() - y0)*stepYi);
505         Float_t yy = ((hh.Z() - z0)*stepZi);
506         if( xx<0 || yy<0 || xx>=65536 || yy>= 65536 ){
507           std::cout<<"!!!! hit packing error!!! "<<xx<<" "<<yy<<" "<<std::endl;
508         }
509         p[ih].x = (UShort_t) xx;
510         p[ih].y = (UShort_t) yy;
511       }
512       Int_t size = row.NHits()*sizeof(ushort2);
513
514       row.SetFullGridOffset( row.NHits()*2 );   
515       UShort_t *p1 = ((UShort_t *)p) + row.FullGridOffset();
516
517       Int_t n = grid.N();
518       for( Int_t i=0; i<n; i++ ){
519         p1[i] = c[i];
520       }     
521       UShort_t a = c[n];
522       Int_t nn = n+grid.Ny()+3;
523       for( Int_t i=n; i<nn; i++ ) p1[i] = a;
524
525       size+= (nn)*sizeof(UShort_t);
526       row.SetFullLinkOffset( row.NHits()*2 + nn );
527       size+= row.NHits()*2*sizeof(Short_t);
528
529       if( size%16 ) size = size/sizeof(uint4)+1;
530       else size = size/sizeof(uint4);
531       row.SetFullSize( size );
532       //cout<<iRow<<", "<<row.fNHits<<"= "<<size*16<<"b: "<<row.fFullOffset<<" "<<row.fFullSize<<" "<<row.fFullGridOffset<<" "<<row.fFullLinkOffset<<std::endl;
533
534       rowDataOffset+=size;
535     }  
536   } 
537 }
538
539
540 GPUh() void AliHLTTPCCATracker::Reconstruct()
541 {
542   //* reconstruction of event
543   //std::cout<<"Reconstruct slice "<<fParam.ISlice()<<", nHits="<<fNHitsTotal<<std::endl;
544
545   fTimers[0] = 0; // find neighbours
546   fTimers[1] = 0; // construct tracklets
547   fTimers[2] = 0; // fit tracklets
548   fTimers[3] = 0; // prolongation of tracklets
549   fTimers[4] = 0; // selection
550   fTimers[5] = 0; // write output
551   fTimers[6] = 0;
552   fTimers[7] = 0;
553   
554   //if( fParam.ISlice()<1 ) return; //SG!!!
555
556   TStopwatch timer0;
557
558   //SetupRowData();
559   if( fNHitsTotal < 1 ){
560     {
561       SetPointersTracks(1, 1); // to calculate the size
562       fTrackMemory = reinterpret_cast<Char_t*> ( new uint4 [ fTrackMemorySize/sizeof(uint4) + 100] );   
563       SetPointersTracks(1, 1); // set pointers for tracks
564       fOutput->SetNTracks(0);
565       fOutput->SetNTrackClusters(0);
566     }
567
568     return;
569   }
570 #ifdef DRAW
571
572   AliHLTTPCCADisplay::Instance().ClearView();  
573   AliHLTTPCCADisplay::Instance().SetSliceView();
574   AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
575   AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );  
576   if( fNHitsTotal>0 ){
577     AliHLTTPCCADisplay::Instance().DrawSliceHits( kRed, 1.);  
578     AliHLTTPCCADisplay::Instance().Ask();
579   }
580 #endif
581
582   *fNTracks = 0;
583   *fNTracklets = 0;
584
585 #if !defined(HLTCA_GPUCODE)  
586  
587   AliHLTTPCCAProcess<AliHLTTPCCANeighboursFinder>( Param().NRows(), 1, *this );
588
589 #ifdef HLTCA_INTERNAL_PERFORMANCE 
590   //if( Param().ISlice()<=2 ) 
591   //AliHLTTPCCAPerformance::Instance().LinkPerformance( Param().ISlice() );
592 #endif
593
594
595 #ifdef DRAW
596   if( fNHitsTotal>0 ){
597     AliHLTTPCCADisplay::Instance().DrawSliceLinks( -1, -1, 1);  
598     AliHLTTPCCADisplay::Instance().Ask();
599   }
600 #endif
601
602
603   AliHLTTPCCAProcess<AliHLTTPCCANeighboursCleaner>( Param().NRows()-2, 1, *this );
604   AliHLTTPCCAProcess<AliHLTTPCCAStartHitsFinder>( Param().NRows()-4, 1, *this );
605
606   Int_t nStartHits = *fNTracklets;
607   
608   Int_t nThreads = 128;
609   Int_t nBlocks = fNHitsTotal/nThreads + 1;
610   if( nBlocks<12 ){
611     nBlocks = 12; 
612     nThreads = fNHitsTotal/12+1;
613     if( nThreads%32 ) nThreads = (nThreads/32+1)*32;
614   }
615       
616   nThreads = fNHitsTotal;
617   nBlocks = 1;
618
619   AliHLTTPCCAProcess<AliHLTTPCCAUsedHitsInitialiser>(nBlocks, nThreads,*this);
620
621
622   {
623     SetPointersTracks(nStartHits, fNHitsTotal); // to calculate the size
624     fTrackMemory = reinterpret_cast<Char_t*> ( new uint4 [ fTrackMemorySize/sizeof(uint4) + 100] );   
625     SetPointersTracks(nStartHits, fNHitsTotal); // set pointers for hits
626   }
627
628   Int_t nMemThreads = AliHLTTPCCATrackletConstructor::NMemThreads();
629   nThreads = 256;//96;
630   nBlocks = nStartHits/nThreads + 1;
631   if( nBlocks<30 ){
632     nBlocks = 30;
633     nThreads = (nStartHits)/30+1;
634     if( nThreads%32 ) nThreads = (nThreads/32+1)*32;
635   }
636
637   nThreads = nStartHits;
638   nBlocks = 1;
639
640   AliHLTTPCCAProcess1<AliHLTTPCCATrackletConstructor>(nBlocks, nMemThreads+nThreads,*this);
641
642   //std::cout<<"Slice "<<Param().ISlice()<<": NHits="<<fNHitsTotal<<", NTracklets="<<*NTracklets()<<std::endl;
643
644   { 
645     nThreads = 128;
646     nBlocks = nStartHits/nThreads + 1;
647     if( nBlocks<12 ){
648       nBlocks = 12;  
649       nThreads = nStartHits/12+1;
650       nThreads = (nThreads/32+1)*32;
651     }
652         
653     *fNTrackHits = 0;
654
655     nThreads = nStartHits;
656     nBlocks = 1;
657
658
659     AliHLTTPCCAProcess<AliHLTTPCCATrackletSelector>(nBlocks, nThreads,*this);
660
661     //std::cout<<"Slice "<<Param().ISlice()<<": N start hits/tracklets/tracks = "<<nStartHits<<" "<<nStartHits<<" "<<*fNTracks<<std::endl;
662    }
663
664   //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;
665
666   
667   WriteOutput();      
668   
669
670 #endif
671
672 #ifdef DRAW
673  {
674    AliHLTTPCCADisplay &disp = AliHLTTPCCADisplay::Instance();
675    AliHLTTPCCATracker &slice = *this;
676    std::cout<<"N out tracks = "<<*slice.NOutTracks()<<std::endl;
677    //disp.Ask();
678    AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
679    AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
680    disp.DrawSliceHits(-1,.5);
681    for( Int_t itr=0; itr<*slice.NOutTracks(); itr++ ){
682      std::cout<<"track N "<<itr<<", nhits="<<slice.OutTracks()[itr].NHits()<<std::endl;
683      disp.DrawSliceOutTrack( itr, kBlue );
684      //disp.Ask();
685      //int id = slice.OutTracks()[itr].OrigTrackID();
686      //AliHLTTPCCATrack &tr = Tracks()[id];
687      //for( Int_t ih=0; ih<tr.NHits(); ih++ ){       
688      //Int_t ic = (fTrackHits[tr.FirstHitID()+ih]);
689      //std::cout<<ih<<" "<<ID2IRow(ic)<<" "<<ID2IHit(ic)<<std::endl;
690      //}
691      //disp.DrawSliceTrack( id, kBlue );
692      //disp.Ask();
693    }
694    disp.Ask();
695  }
696 #endif
697
698   timer0.Stop();  
699   fTimers[0] = timer0.CpuTime()/100.;
700
701  }
702
703
704
705
706 GPUh() void AliHLTTPCCATracker::WriteOutput()
707 {
708   // write output
709
710   TStopwatch timer;
711
712   //cout<<"output: nTracks = "<<*fNTracks<<", nHitsTotal="<<fNHitsTotal<<std::endl;  
713
714   
715   fOutput->SetNTracks( *fNTracks );
716   fOutput->SetNTrackClusters( *fNTrackHits );
717   fOutput->SetPointers();
718
719   Int_t nStoredHits = 0;
720
721   for( Int_t iTr=0; iTr<*fNTracks; iTr++){
722     AliHLTTPCCATrack &iTrack = fTracks[iTr];       
723  
724     AliHLTTPCCASliceTrack out;
725     out.SetFirstClusterRef( nStoredHits );
726     out.SetNClusters( iTrack.NHits() );    
727     out.SetParam( iTrack.Param() );
728
729     fOutput->SetTrack( iTr, out );
730
731     Int_t iID = iTrack.FirstHitID();
732     for( Int_t ith=0; ith<iTrack.NHits(); ith++ ){
733       Int_t ic = (fTrackHits[iID+ith]);
734       Int_t iRow = ID2IRow(ic);
735       Int_t ih = ID2IHit(ic);
736       const AliHLTTPCCARow &row = fRows[iRow];      
737   
738       Float_t y0 = row.Grid().YMin();
739       Float_t z0 = row.Grid().ZMin();
740       Float_t stepY = row.HstepY();
741       Float_t stepZ = row.HstepZ();      
742       //Float_t x = row.X();
743
744       const uint4 *tmpint4 = RowData() + row.FullOffset();
745       const ushort2 *hits = reinterpret_cast<const ushort2*>(tmpint4);
746       ushort2 hh = hits[ih];
747
748       Float_t y = y0 + hh.x*stepY;
749       Float_t z = z0 + hh.y*stepZ;
750
751       Int_t inpIDtot = fHitInputIDs[row.FirstHit()+ih];
752       Int_t inpID = inpIDtot - row.FirstHit();
753
754       UInt_t hIDrc = AliHLTTPCCADataCompressor::IRowIClu2IDrc(iRow,inpID);
755       UShort_t hPackedYZ = 0;
756       UChar_t hPackedAmp = 0;
757       float2 hUnpackedYZ = CAMath::MakeFloat2(y,z);
758       
759       fOutput->SetClusterIDrc( nStoredHits, hIDrc  );
760       fOutput->SetClusterPackedYZ( nStoredHits, hPackedYZ );
761       fOutput->SetClusterPackedAmp( nStoredHits, hPackedAmp);
762       fOutput->SetClusterUnpackedYZ( nStoredHits, hUnpackedYZ );
763       nStoredHits++;
764     }
765   }
766   
767  
768   // old stuff 
769
770   *fNOutTrackHits = 0;
771   *fNOutTracks = 0;
772
773
774   for( Int_t iTr=0; iTr<*fNTracks; iTr++){
775
776     AliHLTTPCCATrack &iTrack = fTracks[iTr];
777
778     //cout<<"iTr = "<<iTr<<", nHits="<<iTrack.NHits()<<std::endl;
779
780     //if( !iTrack.Alive() ) continue;
781     if( iTrack.NHits()<3 ) continue;          
782     AliHLTTPCCAOutTrack &out = fOutTracks[*fNOutTracks];
783     out.SetFirstHitRef( *fNOutTrackHits );
784     out.SetNHits( 0 );
785     out.SetOrigTrackID( iTr );    
786     out.SetStartPoint( iTrack.Param() );
787     out.SetEndPoint( iTrack.Param() );    
788
789     Int_t iID = iTrack.FirstHitID();
790     Int_t nOutTrackHitsOld = *fNOutTrackHits;
791  
792     for( Int_t ith=0; ith<iTrack.NHits(); ith++ ){
793       Int_t ic = (fTrackHits[iID+ith]);
794       const AliHLTTPCCARow &row = ID2Row(ic);
795       Int_t ih = ID2IHit(ic);
796       fOutTrackHits[*fNOutTrackHits] = fHitInputIDs[row.FirstHit()+ih];      
797       (*fNOutTrackHits)++;
798       //cout<<"write i,row,hit,id="<<ith<<", "<<ID2IRow(ic)<<", "<<ih<<", "<<fHitInputIDs[row.FirstHit()+ih]<<std::endl;     
799       if( *fNOutTrackHits>=10*fNHitsTotal ){
800         std::cout<<"fNOutTrackHits>fNHitsTotal"<<std::endl;
801         //exit(0);
802         return;//SG!!!
803       }
804       out.SetNHits( out.NHits() + 1 );      
805     }    
806     if( out.NHits() >= 2 ){
807       (*fNOutTracks)++;
808     }else {
809       (*fNOutTrackHits) = nOutTrackHitsOld;
810     }
811   }
812
813
814   timer.Stop();
815   fTimers[5]+=timer.CpuTime();
816 }
817
818 GPUh() void AliHLTTPCCATracker::FitTrackFull( AliHLTTPCCATrack &/**/, Float_t * /**/ ) const 
819 {  
820   // fit track with material
821 #ifdef XXX    
822   //* Fit the track   
823   FitTrack( iTrack, tt0 );
824   if( iTrack.NHits()<=3 ) return;
825     
826   AliHLTTPCCATrackParam &t = iTrack.Param();
827   AliHLTTPCCATrackParam t0 = t;
828
829   t.Chi2() = 0;
830   t.NDF() = -5; 
831   Bool_t first = 1;
832
833   Int_t iID = iTrack.FirstHitID();
834   for( Int_t ih=0; ih<iTrack.NHits(); ih++, iID++ ){
835     Int_t *ic = &(fTrackHits[iID]);
836     Int_t iRow = ID2IRow(*ic);
837     AliHLTTPCCARow &row = fRows[iRow];      
838     if( !t0.TransportToX( row.X() ) ) continue;      
839     Float_t dy, dz;
840     AliHLTTPCCAHit &h = ID2Hit(*ic);
841
842     // check for wrong hits     
843     if(0){
844       dy = t0.GetY() - h.Y();
845       dz = t0.GetZ() - h.Z();
846       
847       //if( dy*dy > 3.5*3.5*(/*t0.GetErr2Y() + */h.ErrY()*h.ErrY() ) ) continue;//SG!!!
848       //if( dz*dz > 3.5*3.5*(/*t0.GetErr2Z() + */h.ErrZ()*h.ErrZ() ) ) continue;      
849     }
850
851     if( !t.TransportToX( row.X() ) ) continue;  
852
853     //* Update the track
854             
855     if( first ){
856       t.Cov()[ 0] = .5*.5;
857       t.Cov()[ 1] = 0;
858       t.Cov()[ 2] = .5*.5;
859       t.Cov()[ 3] = 0;
860       t.Cov()[ 4] = 0;
861       t.Cov()[ 5] = .2*.2;
862       t.Cov()[ 6] = 0;
863       t.Cov()[ 7] = 0;
864       t.Cov()[ 8] = 0;
865       t.Cov()[ 9] = .2*.2;
866       t.Cov()[10] = 0;
867       t.Cov()[11] = 0;
868       t.Cov()[12] = 0;
869       t.Cov()[13] = 0;
870       t.Cov()[14] = .2*.2;
871       t.Chi2() = 0;
872       t.NDF() = -5;
873     }
874     Float_t err2Y, err2Z;
875     GetErrors2( iRow, t, err2Y, err2Z );
876
877     if( !t.Filter2( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
878
879     first = 0;      
880   }  
881   /*
882   Float_t cosPhi = iTrack.Param().GetCosPhi();
883   p0.Param().TransportToX(ID2Row( iTrack.PointID()[0] ).X());
884   p2.Param().TransportToX(ID2Row( iTrack.PointID()[1] ).X());  
885   if( p0.Param().GetCosPhi()*cosPhi<0 ){ // change direction
886   Float_t *par = p0.Param().Par();
887   Float_t *cov = p0.Param().Cov();
888   par[2] = -par[2]; // sin phi
889   par[3] = -par[3]; // DzDs
890     par[4] = -par[4]; // kappa
891     cov[3] = -cov[3];
892     cov[4] = -cov[4];
893     cov[6] = -cov[6];
894     cov[7] = -cov[7];
895     cov[10] = -cov[10];
896     cov[11] = -cov[11];
897     p0.Param().CosPhi() = -p0.Param().GetCosPhi();
898   }
899   */
900 #endif
901 }
902
903 GPUh() void AliHLTTPCCATracker::FitTrack( AliHLTTPCCATrack &/*track*/, Float_t */*t0[]*/ ) const 
904 {      
905   //* Fit the track   
906 #ifdef XXX
907   AliHLTTPCCAEndPoint &p2 = ID2Point(track.PointID()[1]);
908   AliHLTTPCCAHit &c0 = ID2Hit(fTrackHits[p0.TrackHitID()].HitID());     
909   AliHLTTPCCAHit &c1 = ID2Hit(fTrackHits[track.HitID()[1]].HitID());    
910   AliHLTTPCCAHit &c2 = ID2Hit(fTrackHits[p2.TrackHitID()].HitID());     
911   AliHLTTPCCARow &row0 = ID2Row(fTrackHits[p0.TrackHitID()].HitID());
912   AliHLTTPCCARow &row1 = ID2Row(fTrackHits[track.HitID()[1]].HitID());
913   AliHLTTPCCARow &row2 = ID2Row(fTrackHits[p2.TrackHitID()].HitID());
914   Float_t sp0[5] = {row0.X(), c0.Y(), c0.Z(), c0.ErrY(), c0.ErrZ() };
915   Float_t sp1[5] = {row1.X(), c1.Y(), c1.Z(), c1.ErrY(), c1.ErrZ() };
916   Float_t sp2[5] = {row2.X(), c2.Y(), c2.Z(), c2.ErrY(), c2.ErrZ() };
917   //cout<<"Fit track, points ="<<sp0[0]<<" "<<sp0[1]<<" / "<<sp1[0]<<" "<<sp1[1]<<" / "<<sp2[0]<<" "<<sp2[1]<<std::endl;
918   if( track.NHits()>=3 ){    
919     p0.Param().ConstructXYZ3(sp0,sp1,sp2,p0.Param().CosPhi(), t0);
920     p2.Param().ConstructXYZ3(sp2,sp1,sp0,p2.Param().CosPhi(), t0);
921     //p2.Param() = p0.Param();
922     //p2.Param().TransportToX(row2.X());
923     //p2.Param().Par()[1] = -p2.Param().Par()[1];
924     //p2.Param().Par()[4] = -p2.Param().Par()[4];
925   } else {
926     p0.Param().X() = row0.X();
927     p0.Param().Y() = c0.Y();
928     p0.Param().Z() = c0.Z();
929     p0.Param().Err2Y() = c0.ErrY()*c0.ErrY();
930     p0.Param().Err2Z() = c0.ErrZ()*c0.ErrZ();
931     p2.Param().X() = row2.X();
932     p2.Param().Y() = c2.Y();
933     p2.Param().Z() = c2.Z();
934     p2.Param().Err2Y() = c2.ErrY()*c2.ErrY();
935     p2.Param().Err2Z() = c2.ErrZ()*c2.ErrZ();
936   }
937 #endif
938 }
939
940
941
942 GPUd() void AliHLTTPCCATracker::GetErrors2( Int_t iRow, Float_t z, Float_t sinPhi, Float_t cosPhi, Float_t DzDs, Float_t &Err2Y, Float_t &Err2Z ) const
943 {
944   //
945   // Use calibrated cluster error from OCDB
946   //
947
948   fParam.GetClusterErrors2( iRow, z, sinPhi, cosPhi, DzDs, Err2Y, Err2Z );
949 }
950
951 GPUd() void AliHLTTPCCATracker::GetErrors2( Int_t iRow, const AliHLTTPCCATrackParam &t, Float_t &Err2Y, Float_t &Err2Z ) const
952 {
953   //
954   // Use calibrated cluster error from OCDB
955   //
956
957   fParam.GetClusterErrors2( iRow, t.GetZ(), t.SinPhi(), t.CosPhi(), t.DzDs(), Err2Y, Err2Z );
958 }
959
960
961 #if !defined(HLTCA_GPUCODE)  
962
963 GPUh() void AliHLTTPCCATracker::WriteEvent( std::ostream &out ) 
964 {
965   // write event to the file
966   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
967     out<<fRows[iRow].FirstHit()<<" "<<fRows[iRow].NHits()<<std::endl;
968   } 
969   out<<fNHitsTotal<<std::endl;
970
971   Float_t y[fNHitsTotal], z[fNHitsTotal];
972  
973   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++){
974     AliHLTTPCCARow &row = fRows[iRow];
975     Float_t y0 = row.Grid().YMin();
976     Float_t z0 = row.Grid().ZMin();
977     Float_t stepY = row.HstepY();
978     Float_t stepZ = row.HstepZ();
979     const uint4* tmpint4 = RowData() + row.FullOffset();
980     const ushort2 *hits = reinterpret_cast<const ushort2*>(tmpint4);
981     for( Int_t ih=0; ih<fRows[iRow].NHits(); ih++ ){
982       Int_t ihTot = row.FirstHit() + ih;
983       Int_t id = fHitInputIDs[ihTot];
984       ushort2 hh = hits[ih];
985       y[id] = y0 + hh.x*stepY;
986       z[id] = z0 + hh.y*stepZ;
987     }
988   }
989   for( Int_t ih=0; ih<fNHitsTotal; ih++ ){
990     out<<y[ih]<<" "<<z[ih]<<std::endl;
991   }
992 }
993
994 GPUh() void AliHLTTPCCATracker::ReadEvent( std::istream &in ) 
995 {
996   //* Read event from file 
997
998   Int_t rowFirstHit[Param().NRows()], rowNHits[Param().NRows()];  
999   for( Int_t iRow=0; iRow<Param().NRows(); iRow++ ){
1000     in>>rowFirstHit[iRow]>>rowNHits[iRow];
1001   }
1002   Int_t nHits;
1003   in >> nHits;
1004   Float_t y[nHits], z[nHits];
1005   for( Int_t ih=0; ih<nHits; ih++ ){
1006     in>>y[ih]>>z[ih];
1007   }
1008   ReadEvent( rowFirstHit, rowNHits, y, z, nHits );
1009
1010
1011 GPUh() void AliHLTTPCCATracker::WriteTracks( std::ostream &out ) 
1012 {
1013   //* Write tracks to file 
1014  
1015   out<<fTimers[0]<<std::endl;
1016   out<<*fNOutTrackHits<<std::endl;
1017   for( Int_t ih=0; ih<*fNOutTrackHits; ih++ ){
1018     out<< fOutTrackHits[ih]<<" ";
1019   }
1020   out<<std::endl;
1021   
1022   out<<*fNOutTracks<<std::endl;
1023
1024   for( Int_t itr=0; itr<*fNOutTracks; itr++ ){
1025     AliHLTTPCCAOutTrack &t = fOutTracks[itr];    
1026     AliHLTTPCCATrackParam p1 = t.StartPoint();  
1027     AliHLTTPCCATrackParam p2 = t.EndPoint();    
1028     out<< t.NHits()<<" ";
1029     out<< t.FirstHitRef()<<" ";
1030     out<< t.OrigTrackID()<<" ";
1031     out<<std::endl;
1032     out<< p1.X()<<" ";
1033     out<< p1.CosPhi()<<" ";
1034     out<< p1.Chi2()<<" ";
1035     out<< p1.NDF()<<std::endl;
1036     for( Int_t i=0; i<5; i++ ) out<<p1.Par()[i]<<" ";
1037     out<<std::endl;
1038     for( Int_t i=0; i<15; i++ ) out<<p1.Cov()[i]<<" ";
1039     out<<std::endl;
1040     out<< p2.X()<<" ";
1041     out<< p2.CosPhi()<<" ";
1042     out<< p2.Chi2()<<" ";
1043     out<< p2.NDF()<<std::endl;
1044     for( Int_t i=0; i<5; i++ ) out<<p2.Par()[i]<<" ";
1045     out<<std::endl;
1046     for( Int_t i=0; i<15; i++ ) out<<p2.Cov()[i]<<" ";
1047     out<<std::endl;
1048   }
1049 }
1050
1051 GPUh() void AliHLTTPCCATracker::ReadTracks( std::istream &in )
1052 {
1053   //* Read tracks  from file 
1054   in>>fTimers[0];
1055   in>>*fNOutTrackHits;  
1056
1057   for( Int_t ih=0; ih<*fNOutTrackHits; ih++ ){
1058     in>>fOutTrackHits[ih];
1059   }
1060   in>>*fNOutTracks;
1061
1062   for( Int_t itr=0; itr<*fNOutTracks; itr++ ){
1063     AliHLTTPCCAOutTrack &t = fOutTracks[itr];    
1064     AliHLTTPCCATrackParam p1, p2;
1065     Int_t i;
1066     Float_t f;
1067     in>> i; t.SetNHits( i );
1068     in>> i; t.SetFirstHitRef( i );
1069     in>> i; t.SetOrigTrackID( i );
1070     in>> f; p1.SetX( f );
1071     in>> f; p1.SetCosPhi( f );
1072     in>> f; p1.SetChi2( f );
1073     in>> i; p1.SetNDF( i );
1074     for( Int_t j=0; j<5; j++ ){ in>>f; p1.SetPar(j,f); }
1075     for( Int_t j=0; j<15; j++ ){ in>>f; p1.SetCov(j,f); }
1076     in>> f; p2.SetX( f );
1077     in>> f; p2.SetCosPhi( f );
1078     in>> f; p2.SetChi2( f );
1079     in>> i; p2.SetNDF( i );
1080     for( Int_t j=0; j<5; j++ ){ in>>f; p2.SetPar(j,f); }
1081     for( Int_t j=0; j<15; j++ ){ in>>f; p2.SetCov(j,f); }
1082     t.SetStartPoint( p1 );
1083     t.SetEndPoint( p2 );
1084   }
1085 }
1086 #endif