]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCATracker.cxx
TPC CA Global Merger component added
[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  
442   AliHLTTPCCAHit *ffHits = new AliHLTTPCCAHit[ fNHitsTotal ];
443
444   Int_t rowDataOffset = 0;
445
446   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
447
448     AliHLTTPCCARow &row = fRows[iRow];
449     const AliHLTTPCCAGrid &grid = row.Grid();
450     
451     Int_t *c = new Int_t [grid.N() + 3 + 10];
452     Int_t *bins = new Int_t [row.NHits()];
453     Int_t *filled = new Int_t [row.Grid().N() + 3 + 10 ];
454
455     for( UInt_t bin=0; bin<row.Grid().N()+3; bin++ ) filled[bin] = 0;  
456
457     for( Int_t i=0; i<row.NHits(); i++ ){
458       Int_t j = row.FirstHit()+i;
459       Int_t bin = row.Grid().GetBin( hitsYZ[2*j], hitsYZ[2*j+1] );
460       bins[i] = bin;
461       filled[bin]++;
462     }
463
464     {
465       Int_t n=0;
466       for( UInt_t bin=0; bin<row.Grid().N()+3; bin++ ){
467         c[bin] = n;
468         n+=filled[bin];
469       }
470     }
471
472     for( Int_t i=0; i<row.NHits(); i++ ){ 
473       Int_t bin = bins[i];
474       Int_t ind = c[bin] + filled[bin]-1;
475
476      AliHLTTPCCAHit &h = ffHits[row.FirstHit()+ind];
477       fHitInputIDs[row.FirstHit()+ind] = fTmpHitInputIDs[row.FirstHit()+i];
478       h.SetY( hitsYZ[2*(row.FirstHit()+i)] );
479       h.SetZ( hitsYZ[2*(row.FirstHit()+i)+1] );
480       filled[bin]--;
481     }
482
483     {
484       Float_t y0 = row.Grid().YMin();
485       Float_t stepY = (row.Grid().YMax() - y0)*(1./65535.);
486       Float_t z0 = row.Grid().ZMin();
487       Float_t stepZ = (row.Grid().ZMax() - z0)*(1./65535.);
488       if( stepY<1.e-4 ) stepY = 1.e-4;
489       if( stepZ<1.e-4 ) stepZ = 1.e-4;
490       Float_t stepYi = 1./stepY;
491       Float_t stepZi = 1./stepZ;
492       
493       row.SetHy0( y0 );
494       row.SetHz0( z0 );
495       row.SetHstepY( stepY );
496       row.SetHstepZ( stepZ );
497       row.SetHstepYi( stepYi );
498       row.SetHstepZi( stepZi );
499       
500       row.SetFullOffset( rowDataOffset );
501       ushort2 *p= (ushort2*)( fRowData + row.FullOffset() );
502       for( Int_t ih=0; ih<row.NHits(); ih++ ){
503         Int_t ihTot = row.FirstHit()+ih;
504         AliHLTTPCCAHit &hh = ffHits[ihTot];
505         Float_t xx = ((hh.Y() - y0)*stepYi);
506         Float_t yy = ((hh.Z() - z0)*stepZi);
507         if( xx<0 || yy<0 || xx>=65536 || yy>= 65536 ){
508           std::cout<<"!!!! hit packing error!!! "<<xx<<" "<<yy<<" "<<std::endl;
509         }
510         p[ih].x = (UShort_t) xx;
511         p[ih].y = (UShort_t) yy;
512       }
513       Int_t size = row.NHits()*sizeof(ushort2);
514
515       row.SetFullGridOffset( row.NHits()*2 );   
516       UShort_t *p1 = ((UShort_t *)p) + row.FullGridOffset();
517
518       Int_t n = grid.N();
519       for( Int_t i=0; i<n; i++ ){
520         p1[i] = c[i];
521       }     
522       UShort_t a = c[n];
523       Int_t nn = n+grid.Ny()+3;
524       for( Int_t i=n; i<nn; i++ ) p1[i] = a;
525
526       size+= (nn)*sizeof(UShort_t);
527       row.SetFullLinkOffset( row.NHits()*2 + nn );
528       size+= row.NHits()*2*sizeof(Short_t);
529
530       if( size%16 ) size = size/sizeof(uint4)+1;
531       else size = size/sizeof(uint4);
532       row.SetFullSize( size );
533       //cout<<iRow<<", "<<row.fNHits<<"= "<<size*16<<"b: "<<row.fFullOffset<<" "<<row.fFullSize<<" "<<row.fFullGridOffset<<" "<<row.fFullLinkOffset<<std::endl;
534
535       rowDataOffset+=size;
536     }
537     if( c ) delete[] c;
538     if( bins ) delete[] bins;
539     if( filled ) delete[] filled;
540   }
541   delete[] ffHits;
542 }
543
544
545 GPUh() void AliHLTTPCCATracker::Reconstruct()
546 {
547   //* reconstruction of event
548   //std::cout<<"Reconstruct slice "<<fParam.ISlice()<<", nHits="<<fNHitsTotal<<std::endl;
549
550   fTimers[0] = 0; // find neighbours
551   fTimers[1] = 0; // construct tracklets
552   fTimers[2] = 0; // fit tracklets
553   fTimers[3] = 0; // prolongation of tracklets
554   fTimers[4] = 0; // selection
555   fTimers[5] = 0; // write output
556   fTimers[6] = 0;
557   fTimers[7] = 0;
558   
559   //if( fParam.ISlice()<1 ) return; //SG!!!
560
561   TStopwatch timer0;
562
563   //SetupRowData();
564   if( fNHitsTotal < 1 ){
565     {
566       SetPointersTracks(1, 1); // to calculate the size
567       fTrackMemory = reinterpret_cast<Char_t*> ( new uint4 [ fTrackMemorySize/sizeof(uint4) + 100] );   
568       SetPointersTracks(1, 1); // set pointers for tracks
569       fOutput->SetNTracks(0);
570       fOutput->SetNTrackClusters(0);
571     }
572
573     return;
574   }
575 #ifdef DRAW
576
577   AliHLTTPCCADisplay::Instance().ClearView();  
578   AliHLTTPCCADisplay::Instance().SetSliceView();
579   AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
580   AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );  
581   if( fNHitsTotal>0 ){
582     AliHLTTPCCADisplay::Instance().DrawSliceHits( kRed, 1.);  
583     AliHLTTPCCADisplay::Instance().Ask();
584   }
585 #endif
586
587   *fNTracks = 0;
588   *fNTracklets = 0;
589
590 #if !defined(HLTCA_GPUCODE)  
591  
592   AliHLTTPCCAProcess<AliHLTTPCCANeighboursFinder>( Param().NRows(), 1, *this );
593
594 #ifdef HLTCA_INTERNAL_PERFORMANCE 
595   //if( Param().ISlice()<=2 ) 
596   //AliHLTTPCCAPerformance::Instance().LinkPerformance( Param().ISlice() );
597 #endif
598
599
600 #ifdef DRAW
601   if( fNHitsTotal>0 ){
602     AliHLTTPCCADisplay::Instance().DrawSliceLinks( -1, -1, 1);  
603     AliHLTTPCCADisplay::Instance().Ask();
604   }
605 #endif
606
607
608   AliHLTTPCCAProcess<AliHLTTPCCANeighboursCleaner>( Param().NRows()-2, 1, *this );
609   AliHLTTPCCAProcess<AliHLTTPCCAStartHitsFinder>( Param().NRows()-4, 1, *this );
610
611   Int_t nStartHits = *fNTracklets;
612   
613   Int_t nThreads = 128;
614   Int_t nBlocks = fNHitsTotal/nThreads + 1;
615   if( nBlocks<12 ){
616     nBlocks = 12; 
617     nThreads = fNHitsTotal/12+1;
618     if( nThreads%32 ) nThreads = (nThreads/32+1)*32;
619   }
620       
621   nThreads = fNHitsTotal;
622   nBlocks = 1;
623
624   AliHLTTPCCAProcess<AliHLTTPCCAUsedHitsInitialiser>(nBlocks, nThreads,*this);
625
626
627   {
628     SetPointersTracks(nStartHits, fNHitsTotal); // to calculate the size
629     fTrackMemory = reinterpret_cast<Char_t*> ( new uint4 [ fTrackMemorySize/sizeof(uint4) + 100] );   
630     SetPointersTracks(nStartHits, fNHitsTotal); // set pointers for hits
631   }
632
633   Int_t nMemThreads = AliHLTTPCCATrackletConstructor::NMemThreads();
634   nThreads = 256;//96;
635   nBlocks = nStartHits/nThreads + 1;
636   if( nBlocks<30 ){
637     nBlocks = 30;
638     nThreads = (nStartHits)/30+1;
639     if( nThreads%32 ) nThreads = (nThreads/32+1)*32;
640   }
641
642   nThreads = nStartHits;
643   nBlocks = 1;
644
645   AliHLTTPCCAProcess1<AliHLTTPCCATrackletConstructor>(nBlocks, nMemThreads+nThreads,*this);
646
647   //std::cout<<"Slice "<<Param().ISlice()<<": NHits="<<fNHitsTotal<<", NTracklets="<<*NTracklets()<<std::endl;
648
649   { 
650     nThreads = 128;
651     nBlocks = nStartHits/nThreads + 1;
652     if( nBlocks<12 ){
653       nBlocks = 12;  
654       nThreads = nStartHits/12+1;
655       nThreads = (nThreads/32+1)*32;
656     }
657         
658     *fNTrackHits = 0;
659
660     nThreads = nStartHits;
661     nBlocks = 1;
662
663
664     AliHLTTPCCAProcess<AliHLTTPCCATrackletSelector>(nBlocks, nThreads,*this);
665
666     //std::cout<<"Slice "<<Param().ISlice()<<": N start hits/tracklets/tracks = "<<nStartHits<<" "<<nStartHits<<" "<<*fNTracks<<std::endl;
667    }
668
669   //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;
670
671   
672   WriteOutput();      
673   
674
675 #endif
676
677 #ifdef DRAW
678  {
679    AliHLTTPCCADisplay &disp = AliHLTTPCCADisplay::Instance();
680    AliHLTTPCCATracker &slice = *this;
681    std::cout<<"N out tracks = "<<*slice.NOutTracks()<<std::endl;
682    //disp.Ask();
683    AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
684    AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
685    disp.DrawSliceHits(-1,.5);
686    for( Int_t itr=0; itr<*slice.NOutTracks(); itr++ ){
687      std::cout<<"track N "<<itr<<", nhits="<<slice.OutTracks()[itr].NHits()<<std::endl;
688      disp.DrawSliceOutTrack( itr, kBlue );
689      //disp.Ask();
690      //int id = slice.OutTracks()[itr].OrigTrackID();
691      //AliHLTTPCCATrack &tr = Tracks()[id];
692      //for( Int_t ih=0; ih<tr.NHits(); ih++ ){       
693      //Int_t ic = (fTrackHits[tr.FirstHitID()+ih]);
694      //std::cout<<ih<<" "<<ID2IRow(ic)<<" "<<ID2IHit(ic)<<std::endl;
695      //}
696      //disp.DrawSliceTrack( id, kBlue );
697      //disp.Ask();
698    }
699    disp.Ask();
700  }
701 #endif
702
703   timer0.Stop();  
704   fTimers[0] = timer0.CpuTime()/100.;
705
706  }
707
708
709
710
711 GPUh() void AliHLTTPCCATracker::WriteOutput()
712 {
713   // write output
714
715   TStopwatch timer;
716
717   //cout<<"output: nTracks = "<<*fNTracks<<", nHitsTotal="<<fNHitsTotal<<std::endl;  
718
719   
720   fOutput->SetNTracks( *fNTracks );
721   fOutput->SetNTrackClusters( *fNTrackHits );
722   fOutput->SetPointers();
723
724   Int_t nStoredHits = 0;
725
726   for( Int_t iTr=0; iTr<*fNTracks; iTr++){
727     AliHLTTPCCATrack &iTrack = fTracks[iTr];       
728  
729     AliHLTTPCCASliceTrack out;
730     out.SetFirstClusterRef( nStoredHits );
731     out.SetNClusters( iTrack.NHits() );    
732     out.SetParam( iTrack.Param() );
733
734     fOutput->SetTrack( iTr, out );
735
736     Int_t iID = iTrack.FirstHitID();
737     for( Int_t ith=0; ith<iTrack.NHits(); ith++ ){
738       Int_t ic = (fTrackHits[iID+ith]);
739       Int_t iRow = ID2IRow(ic);
740       Int_t ih = ID2IHit(ic);
741       const AliHLTTPCCARow &row = fRows[iRow];      
742   
743       Float_t y0 = row.Grid().YMin();
744       Float_t z0 = row.Grid().ZMin();
745       Float_t stepY = row.HstepY();
746       Float_t stepZ = row.HstepZ();      
747       //Float_t x = row.X();
748
749       const uint4 *tmpint4 = RowData() + row.FullOffset();
750       const ushort2 *hits = reinterpret_cast<const ushort2*>(tmpint4);
751       ushort2 hh = hits[ih];
752
753       Float_t y = y0 + hh.x*stepY;
754       Float_t z = z0 + hh.y*stepZ;
755
756       Int_t inpIDtot = fHitInputIDs[row.FirstHit()+ih];
757       Int_t inpID = inpIDtot - row.FirstHit();
758
759       UInt_t hIDrc = AliHLTTPCCADataCompressor::IRowIClu2IDrc(iRow,inpID);
760       UShort_t hPackedYZ = 0;
761       UChar_t hPackedAmp = 0;
762       float2 hUnpackedYZ = CAMath::MakeFloat2(y,z);
763       
764       fOutput->SetClusterIDrc( nStoredHits, hIDrc  );
765       fOutput->SetClusterPackedYZ( nStoredHits, hPackedYZ );
766       fOutput->SetClusterPackedAmp( nStoredHits, hPackedAmp);
767       fOutput->SetClusterUnpackedYZ( nStoredHits, hUnpackedYZ );
768       nStoredHits++;
769     }
770   }
771   
772  
773   // old stuff 
774
775   *fNOutTrackHits = 0;
776   *fNOutTracks = 0;
777
778
779   for( Int_t iTr=0; iTr<*fNTracks; iTr++){
780
781     AliHLTTPCCATrack &iTrack = fTracks[iTr];
782
783     //cout<<"iTr = "<<iTr<<", nHits="<<iTrack.NHits()<<std::endl;
784
785     //if( !iTrack.Alive() ) continue;
786     if( iTrack.NHits()<3 ) continue;          
787     AliHLTTPCCAOutTrack &out = fOutTracks[*fNOutTracks];
788     out.SetFirstHitRef( *fNOutTrackHits );
789     out.SetNHits( 0 );
790     out.SetOrigTrackID( iTr );    
791     out.SetStartPoint( iTrack.Param() );
792     out.SetEndPoint( iTrack.Param() );    
793
794     Int_t iID = iTrack.FirstHitID();
795     Int_t nOutTrackHitsOld = *fNOutTrackHits;
796  
797     for( Int_t ith=0; ith<iTrack.NHits(); ith++ ){
798       Int_t ic = (fTrackHits[iID+ith]);
799       const AliHLTTPCCARow &row = ID2Row(ic);
800       Int_t ih = ID2IHit(ic);
801       fOutTrackHits[*fNOutTrackHits] = fHitInputIDs[row.FirstHit()+ih];      
802       (*fNOutTrackHits)++;
803       //cout<<"write i,row,hit,id="<<ith<<", "<<ID2IRow(ic)<<", "<<ih<<", "<<fHitInputIDs[row.FirstHit()+ih]<<std::endl;     
804       if( *fNOutTrackHits>=10*fNHitsTotal ){
805         std::cout<<"fNOutTrackHits>fNHitsTotal"<<std::endl;
806         //exit(0);
807         return;//SG!!!
808       }
809       out.SetNHits( out.NHits() + 1 );      
810     }    
811     if( out.NHits() >= 2 ){
812       (*fNOutTracks)++;
813     }else {
814       (*fNOutTrackHits) = nOutTrackHitsOld;
815     }
816   }
817
818
819   timer.Stop();
820   fTimers[5]+=timer.CpuTime();
821 }
822
823 GPUh() void AliHLTTPCCATracker::FitTrackFull( AliHLTTPCCATrack &/**/, Float_t * /**/ ) const 
824 {  
825   // fit track with material
826 #ifdef XXX    
827   //* Fit the track   
828   FitTrack( iTrack, tt0 );
829   if( iTrack.NHits()<=3 ) return;
830     
831   AliHLTTPCCATrackParam &t = iTrack.Param();
832   AliHLTTPCCATrackParam t0 = t;
833
834   t.Chi2() = 0;
835   t.NDF() = -5; 
836   Bool_t first = 1;
837
838   Int_t iID = iTrack.FirstHitID();
839   for( Int_t ih=0; ih<iTrack.NHits(); ih++, iID++ ){
840     Int_t *ic = &(fTrackHits[iID]);
841     Int_t iRow = ID2IRow(*ic);
842     AliHLTTPCCARow &row = fRows[iRow];      
843     if( !t0.TransportToX( row.X() ) ) continue;      
844     Float_t dy, dz;
845     AliHLTTPCCAHit &h = ID2Hit(*ic);
846
847     // check for wrong hits     
848     if(0){
849       dy = t0.GetY() - h.Y();
850       dz = t0.GetZ() - h.Z();
851       
852       //if( dy*dy > 3.5*3.5*(/*t0.GetErr2Y() + */h.ErrY()*h.ErrY() ) ) continue;//SG!!!
853       //if( dz*dz > 3.5*3.5*(/*t0.GetErr2Z() + */h.ErrZ()*h.ErrZ() ) ) continue;      
854     }
855
856     if( !t.TransportToX( row.X() ) ) continue;  
857
858     //* Update the track
859             
860     if( first ){
861       t.Cov()[ 0] = .5*.5;
862       t.Cov()[ 1] = 0;
863       t.Cov()[ 2] = .5*.5;
864       t.Cov()[ 3] = 0;
865       t.Cov()[ 4] = 0;
866       t.Cov()[ 5] = .2*.2;
867       t.Cov()[ 6] = 0;
868       t.Cov()[ 7] = 0;
869       t.Cov()[ 8] = 0;
870       t.Cov()[ 9] = .2*.2;
871       t.Cov()[10] = 0;
872       t.Cov()[11] = 0;
873       t.Cov()[12] = 0;
874       t.Cov()[13] = 0;
875       t.Cov()[14] = .2*.2;
876       t.Chi2() = 0;
877       t.NDF() = -5;
878     }
879     Float_t err2Y, err2Z;
880     GetErrors2( iRow, t, err2Y, err2Z );
881
882     if( !t.Filter2( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
883
884     first = 0;      
885   }  
886   /*
887   Float_t cosPhi = iTrack.Param().GetCosPhi();
888   p0.Param().TransportToX(ID2Row( iTrack.PointID()[0] ).X());
889   p2.Param().TransportToX(ID2Row( iTrack.PointID()[1] ).X());  
890   if( p0.Param().GetCosPhi()*cosPhi<0 ){ // change direction
891   Float_t *par = p0.Param().Par();
892   Float_t *cov = p0.Param().Cov();
893   par[2] = -par[2]; // sin phi
894   par[3] = -par[3]; // DzDs
895     par[4] = -par[4]; // kappa
896     cov[3] = -cov[3];
897     cov[4] = -cov[4];
898     cov[6] = -cov[6];
899     cov[7] = -cov[7];
900     cov[10] = -cov[10];
901     cov[11] = -cov[11];
902     p0.Param().CosPhi() = -p0.Param().GetCosPhi();
903   }
904   */
905 #endif
906 }
907
908 GPUh() void AliHLTTPCCATracker::FitTrack( AliHLTTPCCATrack &/*track*/, Float_t */*t0[]*/ ) const 
909 {      
910   //* Fit the track   
911 #ifdef XXX
912   AliHLTTPCCAEndPoint &p2 = ID2Point(track.PointID()[1]);
913   AliHLTTPCCAHit &c0 = ID2Hit(fTrackHits[p0.TrackHitID()].HitID());     
914   AliHLTTPCCAHit &c1 = ID2Hit(fTrackHits[track.HitID()[1]].HitID());    
915   AliHLTTPCCAHit &c2 = ID2Hit(fTrackHits[p2.TrackHitID()].HitID());     
916   AliHLTTPCCARow &row0 = ID2Row(fTrackHits[p0.TrackHitID()].HitID());
917   AliHLTTPCCARow &row1 = ID2Row(fTrackHits[track.HitID()[1]].HitID());
918   AliHLTTPCCARow &row2 = ID2Row(fTrackHits[p2.TrackHitID()].HitID());
919   Float_t sp0[5] = {row0.X(), c0.Y(), c0.Z(), c0.ErrY(), c0.ErrZ() };
920   Float_t sp1[5] = {row1.X(), c1.Y(), c1.Z(), c1.ErrY(), c1.ErrZ() };
921   Float_t sp2[5] = {row2.X(), c2.Y(), c2.Z(), c2.ErrY(), c2.ErrZ() };
922   //cout<<"Fit track, points ="<<sp0[0]<<" "<<sp0[1]<<" / "<<sp1[0]<<" "<<sp1[1]<<" / "<<sp2[0]<<" "<<sp2[1]<<std::endl;
923   if( track.NHits()>=3 ){    
924     p0.Param().ConstructXYZ3(sp0,sp1,sp2,p0.Param().CosPhi(), t0);
925     p2.Param().ConstructXYZ3(sp2,sp1,sp0,p2.Param().CosPhi(), t0);
926     //p2.Param() = p0.Param();
927     //p2.Param().TransportToX(row2.X());
928     //p2.Param().Par()[1] = -p2.Param().Par()[1];
929     //p2.Param().Par()[4] = -p2.Param().Par()[4];
930   } else {
931     p0.Param().X() = row0.X();
932     p0.Param().Y() = c0.Y();
933     p0.Param().Z() = c0.Z();
934     p0.Param().Err2Y() = c0.ErrY()*c0.ErrY();
935     p0.Param().Err2Z() = c0.ErrZ()*c0.ErrZ();
936     p2.Param().X() = row2.X();
937     p2.Param().Y() = c2.Y();
938     p2.Param().Z() = c2.Z();
939     p2.Param().Err2Y() = c2.ErrY()*c2.ErrY();
940     p2.Param().Err2Z() = c2.ErrZ()*c2.ErrZ();
941   }
942 #endif
943 }
944
945
946
947 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
948 {
949   //
950   // Use calibrated cluster error from OCDB
951   //
952
953   fParam.GetClusterErrors2( iRow, z, sinPhi, cosPhi, DzDs, Err2Y, Err2Z );
954 }
955
956 GPUd() void AliHLTTPCCATracker::GetErrors2( Int_t iRow, const AliHLTTPCCATrackParam &t, Float_t &Err2Y, Float_t &Err2Z ) const
957 {
958   //
959   // Use calibrated cluster error from OCDB
960   //
961
962   fParam.GetClusterErrors2( iRow, t.GetZ(), t.SinPhi(), t.CosPhi(), t.DzDs(), Err2Y, Err2Z );
963 }
964
965
966 #if !defined(HLTCA_GPUCODE)  
967
968 GPUh() void AliHLTTPCCATracker::WriteEvent( std::ostream &out ) 
969 {
970   // write event to the file
971   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++ ){
972     out<<fRows[iRow].FirstHit()<<" "<<fRows[iRow].NHits()<<std::endl;
973   } 
974   out<<fNHitsTotal<<std::endl;
975   
976   Float_t *y = new Float_t [fNHitsTotal];
977   Float_t *z = new Float_t [fNHitsTotal];
978
979   for( Int_t iRow=0; iRow<fParam.NRows(); iRow++){
980     AliHLTTPCCARow &row = fRows[iRow];
981     Float_t y0 = row.Grid().YMin();
982     Float_t z0 = row.Grid().ZMin();
983     Float_t stepY = row.HstepY();
984     Float_t stepZ = row.HstepZ();
985     const uint4* tmpint4 = RowData() + row.FullOffset();
986     const ushort2 *hits = reinterpret_cast<const ushort2*>(tmpint4);
987     for( Int_t ih=0; ih<fRows[iRow].NHits(); ih++ ){
988       Int_t ihTot = row.FirstHit() + ih;
989       Int_t id = fHitInputIDs[ihTot];
990       ushort2 hh = hits[ih];
991       y[id] = y0 + hh.x*stepY;
992       z[id] = z0 + hh.y*stepZ;
993     }
994   }
995   for( Int_t ih=0; ih<fNHitsTotal; ih++ ){
996     out<<y[ih]<<" "<<z[ih]<<std::endl;
997   }
998   delete[] y;
999   delete[] z;
1000 }
1001
1002 GPUh() void AliHLTTPCCATracker::ReadEvent( std::istream &in ) 
1003 {
1004   //* Read event from file 
1005   
1006   Int_t *rowFirstHit = new Int_t[ Param().NRows()];
1007   Int_t *rowNHits = new Int_t [ Param().NRows()];
1008
1009   for( Int_t iRow=0; iRow<Param().NRows(); iRow++ ){
1010     in>>rowFirstHit[iRow]>>rowNHits[iRow];
1011   }
1012   Int_t nHits;
1013   in >> nHits;
1014
1015   Float_t *y = new Float_t[ nHits ];
1016   Float_t *z = new Float_t[ nHits ];
1017   for( Int_t ih=0; ih<nHits; ih++ ){
1018     in>>y[ih]>>z[ih];
1019   }
1020   ReadEvent( rowFirstHit, rowNHits, y, z, nHits );
1021   if( rowFirstHit ) delete[] rowFirstHit;
1022   if( rowNHits )delete[] rowNHits;
1023   if( y )delete[] y;
1024   if( z )delete[] z;
1025
1026
1027 GPUh() void AliHLTTPCCATracker::WriteTracks( std::ostream &out ) 
1028 {
1029   //* Write tracks to file 
1030  
1031   out<<fTimers[0]<<std::endl;
1032   out<<*fNOutTrackHits<<std::endl;
1033   for( Int_t ih=0; ih<*fNOutTrackHits; ih++ ){
1034     out<< fOutTrackHits[ih]<<" ";
1035   }
1036   out<<std::endl;
1037   
1038   out<<*fNOutTracks<<std::endl;
1039
1040   for( Int_t itr=0; itr<*fNOutTracks; itr++ ){
1041     AliHLTTPCCAOutTrack &t = fOutTracks[itr];    
1042     AliHLTTPCCATrackParam p1 = t.StartPoint();  
1043     AliHLTTPCCATrackParam p2 = t.EndPoint();    
1044     out<< t.NHits()<<" ";
1045     out<< t.FirstHitRef()<<" ";
1046     out<< t.OrigTrackID()<<" ";
1047     out<<std::endl;
1048     out<< p1.X()<<" ";
1049     out<< p1.CosPhi()<<" ";
1050     out<< p1.Chi2()<<" ";
1051     out<< p1.NDF()<<std::endl;
1052     for( Int_t i=0; i<5; i++ ) out<<p1.Par()[i]<<" ";
1053     out<<std::endl;
1054     for( Int_t i=0; i<15; i++ ) out<<p1.Cov()[i]<<" ";
1055     out<<std::endl;
1056     out<< p2.X()<<" ";
1057     out<< p2.CosPhi()<<" ";
1058     out<< p2.Chi2()<<" ";
1059     out<< p2.NDF()<<std::endl;
1060     for( Int_t i=0; i<5; i++ ) out<<p2.Par()[i]<<" ";
1061     out<<std::endl;
1062     for( Int_t i=0; i<15; i++ ) out<<p2.Cov()[i]<<" ";
1063     out<<std::endl;
1064   }
1065 }
1066
1067 GPUh() void AliHLTTPCCATracker::ReadTracks( std::istream &in )
1068 {
1069   //* Read tracks  from file 
1070   in>>fTimers[0];
1071   in>>*fNOutTrackHits;  
1072
1073   for( Int_t ih=0; ih<*fNOutTrackHits; ih++ ){
1074     in>>fOutTrackHits[ih];
1075   }
1076   in>>*fNOutTracks;
1077
1078   for( Int_t itr=0; itr<*fNOutTracks; itr++ ){
1079     AliHLTTPCCAOutTrack &t = fOutTracks[itr];    
1080     AliHLTTPCCATrackParam p1, p2;
1081     Int_t i;
1082     Float_t f;
1083     in>> i; t.SetNHits( i );
1084     in>> i; t.SetFirstHitRef( i );
1085     in>> i; t.SetOrigTrackID( i );
1086     in>> f; p1.SetX( f );
1087     in>> f; p1.SetCosPhi( f );
1088     in>> f; p1.SetChi2( f );
1089     in>> i; p1.SetNDF( i );
1090     for( Int_t j=0; j<5; j++ ){ in>>f; p1.SetPar(j,f); }
1091     for( Int_t j=0; j<15; j++ ){ in>>f; p1.SetCov(j,f); }
1092     in>> f; p2.SetX( f );
1093     in>> f; p2.SetCosPhi( f );
1094     in>> f; p2.SetChi2( f );
1095     in>> i; p2.SetNDF( i );
1096     for( Int_t j=0; j<5; j++ ){ in>>f; p2.SetPar(j,f); }
1097     for( Int_t j=0; j<15; j++ ){ in>>f; p2.SetCov(j,f); }
1098     t.SetStartPoint( p1 );
1099     t.SetEndPoint( p2 );
1100   }
1101 }
1102 #endif