]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCATracker.cxx
e452f453abb055d4bb2605fa008538076f26fab9
[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 #include "MemoryAssignmentHelpers.h"
29
30 #include "TStopwatch.h"
31 #include "AliHLTTPCCAHitArea.h"
32 #include "AliHLTTPCCANeighboursFinder.h"
33 #include "AliHLTTPCCANeighboursCleaner.h"
34 #include "AliHLTTPCCAStartHitsFinder.h"
35 #include "AliHLTTPCCATrackletConstructor.h"
36 #include "AliHLTTPCCATrackletSelector.h"
37 #include "AliHLTTPCCAProcess.h"
38 #include "AliHLTTPCCASliceTrack.h"
39 #include "AliHLTTPCCASliceOutput.h"
40 #include "AliHLTTPCCADataCompressor.h"
41 #include "AliHLTTPCCAClusterData.h"
42
43 #include "AliHLTTPCCATrackParam.h"
44
45 #if !defined(HLTCA_GPUCODE)
46 #include <iostream>
47 #endif
48
49 //#define DRAW1
50
51 #ifdef DRAW1
52 #include "AliHLTTPCCADisplay.h"
53 #endif //DRAW
54
55 #ifdef HLTCA_INTERNAL_PERFORMANCE
56 #include "AliHLTTPCCAPerformance.h"
57 #endif
58
59
60 ClassImp( AliHLTTPCCATracker )
61
62 #if !defined(HLTCA_GPUCODE)
63
64 AliHLTTPCCATracker::AliHLTTPCCATracker()
65     :
66     fParam(),
67     fClusterData( 0 ),
68     fData(),
69     fCommonMemory( 0 ),
70     fCommonMemorySize( 0 ),
71     fHitMemory( 0 ),
72     fHitMemorySize( 0 ),
73     fTrackMemory( 0 ),
74     fTrackMemorySize( 0 ),
75     fNTracklets( 0 ),
76     fTrackletStartHits( 0 ),
77     fTracklets( 0 ),
78     fNTracks( 0 ),
79     fTracks( 0 ),
80     fNTrackHits( 0 ),
81     fTrackHits( 0 ),
82     fOutput( 0 ),
83     fNOutTracks( 0 ),
84     fOutTracks( 0 ),
85     fNOutTrackHits( 0 ),
86     fOutTrackHits( 0 )
87 {
88   // constructor
89 }
90
91 GPUd() AliHLTTPCCATracker::~AliHLTTPCCATracker()
92 {
93   // destructor
94   delete[] fCommonMemory;
95   delete[] fHitMemory;
96   delete[] fTrackMemory;
97 }
98 #endif
99
100
101
102 // ----------------------------------------------------------------------------------
103 GPUd() void AliHLTTPCCATracker::Initialize( const AliHLTTPCCAParam &param )
104 {
105   // initialisation
106   fParam = param;
107   fParam.Update();
108   fData.InitializeRows( fParam );
109
110   StartEvent();
111 }
112
113 GPUd() void AliHLTTPCCATracker::StartEvent()
114 {
115   // start new event and fresh the memory
116
117   SetupCommonMemory();
118   *fNTrackHits = 0;
119 }
120
121 void  AliHLTTPCCATracker::SetupCommonMemory()
122 {
123   // set up common memory
124
125   if ( !fCommonMemory ) {
126     SetPointersCommon(); // just to calculate the size
127     // the 1600 extra bytes are not used unless fCommonMemorySize increases with a later event
128     fCommonMemory = reinterpret_cast<char*> ( new uint4 [ fCommonMemorySize/sizeof( uint4 ) + 100] );
129     SetPointersCommon();// set pointers
130   }
131
132   delete[] fHitMemory;
133   fHitMemory = 0;
134   delete[] fTrackMemory;
135   fTrackMemory = 0;
136
137   fData.Clear();
138   *fNTracklets = 0;
139   *fNTracks = 0 ;
140   *fNTrackHits = 0;
141   *fNOutTracks = 0;
142   *fNOutTrackHits = 0;
143 }
144
145 GPUhd() void  AliHLTTPCCATracker::SetPointersCommon()
146 {
147   // set all pointers to the event memory
148
149   char *mem = fCommonMemory;
150   AssignMemory( fNTracklets, mem, 1 );
151   AssignMemory( fNTracks, mem, 1 );
152   AssignMemory( fNTrackHits, mem, 1 );
153   AssignMemory( fNOutTracks, mem, 1 );
154   AssignMemory( fNOutTrackHits, mem, 1 );
155
156   // calculate the size
157
158   fCommonMemorySize = mem - fCommonMemory;
159 }
160
161
162 GPUhd() void  AliHLTTPCCATracker::SetPointersHits( int MaxNHits )
163 {
164   // set all pointers to the event memory
165
166   char *mem = fHitMemory;
167
168   // extra arrays for tpc clusters
169
170   AssignMemory( fTrackletStartHits, mem, MaxNHits );
171
172   // arrays for track hits
173
174   AssignMemory( fTrackHits, mem, 10 * MaxNHits );
175
176   AssignMemory( fOutTrackHits, mem, 10 * MaxNHits );
177
178   // calculate the size
179
180   fHitMemorySize = mem - fHitMemory;
181 }
182
183
184 GPUhd() void  AliHLTTPCCATracker::SetPointersTracks( int MaxNTracks, int MaxNHits )
185 {
186   // set all pointers to the tracks memory
187
188   char *mem = fTrackMemory;
189
190   // memory for tracklets
191
192   AssignMemory( fTracklets, mem, MaxNTracks );
193
194   // memory for selected tracks
195
196   AssignMemory( fTracks, mem, MaxNTracks );
197
198   // memory for output
199
200   AlignTo < sizeof( void * ) > ( mem );
201   fOutput = reinterpret_cast<AliHLTTPCCASliceOutput *>( mem );
202   mem += AliHLTTPCCASliceOutput::EstimateSize( MaxNTracks, MaxNHits );
203
204   // memory for output tracks
205
206   AssignMemory( fOutTracks, mem, MaxNTracks );
207
208   // calculate the size
209
210   fTrackMemorySize = mem - fTrackMemory;
211 }
212
213
214 void AliHLTTPCCATracker::ReadEvent( AliHLTTPCCAClusterData *clusterData )
215 {
216   // read event
217
218   fClusterData = clusterData;
219
220   StartEvent();
221
222   //* Convert input hits, create grids, etc.
223   fData.InitFromClusterData( *clusterData );
224
225   {
226     SetPointersHits( fData.NumberOfHits() ); // to calculate the size
227     fHitMemory = reinterpret_cast<char*> ( new uint4 [ fHitMemorySize/sizeof( uint4 ) + 100] );
228     SetPointersHits( fData.NumberOfHits() ); // set pointers for hits
229     *fNTracklets = 0;
230     *fNTracks = 0 ;
231     *fNOutTracks = 0;
232     *fNOutTrackHits = 0;
233   }
234 }
235
236 GPUh() void AliHLTTPCCATracker::Reconstruct()
237 {
238   //* reconstruction of event
239   //std::cout<<"Reconstruct slice "<<fParam.ISlice()<<", nHits="<<NHitsTotal()<<std::endl;
240
241   fTimers[0] = 0; // find neighbours
242   fTimers[1] = 0; // construct tracklets
243   fTimers[2] = 0; // fit tracklets
244   fTimers[3] = 0; // prolongation of tracklets
245   fTimers[4] = 0; // selection
246   fTimers[5] = 0; // write output
247   fTimers[6] = 0;
248   fTimers[7] = 0;
249
250   //if( fParam.ISlice()<1 ) return; //SG!!!
251
252   TStopwatch timer0;
253
254   if ( NHitsTotal() < 1 ) {
255     {
256       SetPointersTracks( 1, 1 ); // to calculate the size
257       fTrackMemory = reinterpret_cast<char*> ( new uint4 [ fTrackMemorySize/sizeof( uint4 ) + 100] );
258       SetPointersTracks( 1, 1 ); // set pointers for tracks
259       fOutput->SetNTracks( 0 );
260       fOutput->SetNTrackClusters( 0 );
261     }
262
263     return;
264   }
265 #ifdef DRAW1
266   //if( fParam.ISlice()==15){
267   AliHLTTPCCADisplay::Instance().ClearView();
268   AliHLTTPCCADisplay::Instance().SetSliceView();
269   AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
270   AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
271   if ( NHitsTotal() > 0 ) {
272     AliHLTTPCCADisplay::Instance().DrawSliceHits( kRed, .5 );
273     AliHLTTPCCADisplay::Instance().Ask();
274   }
275   //}
276 #endif
277
278   *fNTracks = 0;
279   *fNTracklets = 0;
280
281 #if !defined(HLTCA_GPUCODE)
282
283   AliHLTTPCCAProcess<AliHLTTPCCANeighboursFinder>( Param().NRows(), 1, *this );
284
285 #ifdef HLTCA_INTERNAL_PERFORMANCE
286   //if( Param().ISlice()<=2 )
287   //AliHLTTPCCAPerformance::Instance().LinkPerformance( Param().ISlice() );
288 #endif
289
290
291 #ifdef DRAW
292   if ( NHitsTotal() > 0 ) {
293     AliHLTTPCCADisplay::Instance().DrawSliceLinks( -1, -1, 1 );
294     AliHLTTPCCADisplay::Instance().Ask();
295   }
296 #endif
297
298
299   AliHLTTPCCAProcess<AliHLTTPCCANeighboursCleaner>( Param().NRows() - 2, 1, *this );
300   AliHLTTPCCAProcess<AliHLTTPCCAStartHitsFinder>( Param().NRows() - 4, 1, *this );
301
302   int nStartHits = *fNTracklets;
303
304   int nThreads = 128;
305   int nBlocks = NHitsTotal() / nThreads + 1;
306   if ( nBlocks < 12 ) {
307     nBlocks = 12;
308     nThreads = NHitsTotal() / 12 + 1;
309     if ( nThreads % 32 ) nThreads = ( nThreads / 32 + 1 ) * 32;
310   }
311
312   nThreads = NHitsTotal();
313   nBlocks = 1;
314
315   fData.ClearHitWeights();
316   //AliHLTTPCCAProcess<AliHLTTPCCAUsedHitsInitialiser>( nBlocks, nThreads, *this );
317
318
319   {
320     SetPointersTracks( nStartHits*2, NHitsTotal() ); // to calculate the size
321     fTrackMemory = reinterpret_cast<char*> ( new uint4 [ fTrackMemorySize/sizeof( uint4 ) + 100] );
322     SetPointersTracks( nStartHits*2, NHitsTotal() ); // set pointers for hits
323   }
324
325   int nMemThreads = AliHLTTPCCATrackletConstructor::NMemThreads();
326   nThreads = 256;//96;
327   nBlocks = nStartHits / nThreads + 1;
328   if ( nBlocks < 30 ) {
329     nBlocks = 30;
330     nThreads = ( nStartHits ) / 30 + 1;
331     if ( nThreads % 32 ) nThreads = ( nThreads / 32 + 1 ) * 32;
332   }
333
334   nThreads = nStartHits;
335   nBlocks = 1;
336
337   AliHLTTPCCAProcess1<AliHLTTPCCATrackletConstructor>( nBlocks, nMemThreads + nThreads, *this );
338
339   //std::cout<<"Slice "<<Param().ISlice()<<": NHits="<<NHitsTotal()<<", NTracklets="<<*NTracklets()<<std::endl;
340
341   {
342     nThreads = 128;
343     nBlocks = nStartHits / nThreads + 1;
344     if ( nBlocks < 12 ) {
345       nBlocks = 12;
346       nThreads = nStartHits / 12 + 1;
347       nThreads = ( nThreads / 32 + 1 ) * 32;
348     }
349
350     *fNTrackHits = 0;
351
352     nThreads = nStartHits;
353     nBlocks = 1;
354
355
356     AliHLTTPCCAProcess<AliHLTTPCCATrackletSelector>( nBlocks, nThreads, *this );
357
358     //std::cout<<"Slice "<<Param().ISlice()<<": N start hits/tracklets/tracks = "<<nStartHits<<" "<<nStartHits<<" "<<*fNTracks<<std::endl;
359   }
360
361   //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;
362
363
364   WriteOutput();
365
366
367 #endif
368
369 #ifdef DRAW
370   {
371     AliHLTTPCCADisplay &disp = AliHLTTPCCADisplay::Instance();
372     AliHLTTPCCATracker &slice = *this;
373     std::cout << "N out tracks = " << *slice.NOutTracks() << std::endl;
374     //disp.Ask();
375     AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
376     AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
377     disp.DrawSliceHits( -1, .5 );
378     for ( int itr = 0; itr < *slice.NOutTracks(); itr++ ) {
379       std::cout << "track N " << itr << ", nhits=" << slice.OutTracks()[itr].NHits() << std::endl;
380       disp.DrawSliceOutTrack( itr, kBlue );
381       //disp.Ask();
382       //int id = slice.OutTracks()[itr].OrigTrackID();
383       //AliHLTTPCCATrack &tr = Tracks()[id];
384       //for( int ih=0; ih<tr.NHits(); ih++ ){
385       //int ic = (fTrackHits[tr.FirstHitID()+ih]);
386       //std::cout<<ih<<" "<<ID2IRow(ic)<<" "<<ID2IHit(ic)<<std::endl;
387       //}
388       //disp.DrawSliceTrack( id, kBlue );
389       //disp.Ask();
390     }
391     disp.Ask();
392   }
393 #endif
394
395   timer0.Stop();
396   fTimers[0] = timer0.CpuTime() / 100.;
397
398 }
399
400
401
402
403 GPUh() void AliHLTTPCCATracker::WriteOutput()
404 {
405   // write output
406
407   TStopwatch timer;
408
409   //cout<<"output: nTracks = "<<*fNTracks<<", nHitsTotal="<<NHitsTotal()<<std::endl;
410
411   fOutput->SetNTracks( *fNTracks );
412   fOutput->SetNTrackClusters( *fNTrackHits );
413   fOutput->SetPointers();
414
415   int nStoredHits = 0;
416
417   for ( int iTr = 0; iTr < *fNTracks; iTr++ ) {
418     AliHLTTPCCATrack &iTrack = fTracks[iTr];
419
420     AliHLTTPCCASliceTrack out;
421     out.SetFirstClusterRef( nStoredHits );
422     out.SetNClusters( iTrack.NHits() );
423     out.SetParam( iTrack.Param() );
424
425     fOutput->SetTrack( iTr, out );
426
427     int iID = iTrack.FirstHitID();
428     for ( int ith = 0; ith < iTrack.NHits(); ith++ ) {
429       const AliHLTTPCCAHitId &ic = fTrackHits[iID + ith];
430       int iRow = ic.RowIndex();
431       int ih = ic.HitIndex();
432
433       const AliHLTTPCCARow &row = fData.Row( iRow );
434
435       //float y0 = row.Grid().YMin();
436       //float z0 = row.Grid().ZMin();
437       //float stepY = row.HstepY();
438       //float stepZ = row.HstepZ();
439       //float x = row.X();
440
441       //const uint4 *tmpint4 = RowData() + row.FullOffset();
442       //const ushort2 *hits = reinterpret_cast<const ushort2*>(tmpint4);
443       //ushort2 hh = hits[ih];
444       //float y = y0 + hh.x*stepY;
445       //float z = z0 + hh.y*stepZ;
446
447       int clusterIndex = fData.ClusterDataIndex( row, ih );
448       int clusterRowIndex = clusterIndex - fClusterData->RowOffset( iRow );
449
450       if ( clusterIndex < 0 || clusterIndex >= fClusterData->NumberOfClusters() ) {
451         //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
452         //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
453         //abort();
454         continue;
455       }
456       if ( clusterRowIndex < 0 || clusterRowIndex >= fClusterData->NumberOfClusters( iRow ) ) {
457         //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
458         //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
459         //abort();
460         continue;
461       }
462
463       float origX = fClusterData->X( clusterIndex );
464       float origY = fClusterData->Y( clusterIndex );
465       float origZ = fClusterData->Z( clusterIndex );
466
467
468       int id = fClusterData->Id( clusterIndex );
469
470       unsigned short hPackedYZ = 0;
471       UChar_t hPackedAmp = 0;
472       float2 hUnpackedYZ;
473       hUnpackedYZ.x = origY;
474       hUnpackedYZ.y = origZ;
475       float hUnpackedX = origX;
476
477       fOutput->SetClusterId( nStoredHits, id  );
478       fOutput->SetClusterRow( nStoredHits, ( unsigned char ) iRow  );
479       fOutput->SetClusterPackedYZ( nStoredHits, hPackedYZ );
480       fOutput->SetClusterPackedAmp( nStoredHits, hPackedAmp );
481       fOutput->SetClusterUnpackedYZ( nStoredHits, hUnpackedYZ );
482       fOutput->SetClusterUnpackedX( nStoredHits, hUnpackedX );
483       nStoredHits++;
484     }
485   }
486
487
488   // old stuff
489
490   *fNOutTrackHits = 0;
491   *fNOutTracks = 0;
492
493
494   for ( int iTr = 0; iTr < *fNTracks; iTr++ ) {
495
496     const AliHLTTPCCATrack &iTrack = fTracks[iTr];
497
498     //std::cout<<"iTr = "<<iTr<<", nHits="<<iTrack.NHits()<<std::endl;
499
500     //if( !iTrack.Alive() ) continue;
501     if ( iTrack.NHits() < 3 ) continue;
502     AliHLTTPCCAOutTrack &out = fOutTracks[*fNOutTracks];
503     out.SetFirstHitRef( *fNOutTrackHits );
504     out.SetNHits( 0 );
505     out.SetOrigTrackID( iTr );
506     out.SetStartPoint( iTrack.Param() );
507     out.SetEndPoint( iTrack.Param() );
508
509     int iID = iTrack.FirstHitID();
510     int nOutTrackHitsOld = *fNOutTrackHits;
511
512     for ( int ith = 0; ith < iTrack.NHits(); ith++ ) {
513       const AliHLTTPCCAHitId &ic = fTrackHits[iID + ith];
514       const AliHLTTPCCARow &row = Row( ic );
515       int ih = ic.HitIndex();
516       fOutTrackHits[*fNOutTrackHits] = HitInputID( row, ih );
517       ( *fNOutTrackHits )++;
518       //std::cout<<"write i,row,hit,id="<<ith<<", "<<ID2IRow(ic)<<", "<<ih<<", "<<HitInputID( row, ih )<<std::endl;
519       if ( *fNOutTrackHits >= 10*NHitsTotal() ) {
520         std::cout << "fNOutTrackHits>NHitsTotal()" << std::endl;
521         //exit(0);
522         return;//SG!!!
523       }
524       out.SetNHits( out.NHits() + 1 );
525     }
526     if ( out.NHits() >= 2 ) {
527       ( *fNOutTracks )++;
528     } else {
529       ( *fNOutTrackHits ) = nOutTrackHitsOld;
530     }
531   }
532
533
534   timer.Stop();
535   fTimers[5] += timer.CpuTime();
536 }
537
538 GPUh() void AliHLTTPCCATracker::FitTrackFull( const AliHLTTPCCATrack &/**/, float * /**/ ) const
539 {
540   // fit track with material
541 #ifdef XXX
542   //* Fit the track
543   FitTrack( iTrack, tt0 );
544   if ( iTrack.NHits() <= 3 ) return;
545
546   AliHLTTPCCATrackParam &t = iTrack.Param();
547   AliHLTTPCCATrackParam t0 = t;
548
549   t.Chi2() = 0;
550   t.NDF() = -5;
551   bool first = 1;
552
553   int iID = iTrack.FirstHitID();
554   for ( int ih = 0; ih < iTrack.NHits(); ih++, iID++ ) {
555     const AliHLTTPCCAHitId &ic = fTrackHits[iID];
556     int iRow = ic.rowIndex();
557     const AliHLTTPCCARow &row = fData.Row( iRow );
558     if ( !t0.TransportToX( row.X() ) ) continue;
559     float dy, dz;
560     const AliHLTTPCCAHit &h = ic.hitIndex();
561
562     // check for wrong hits
563     if ( 0 ) {
564       dy = t0.GetY() - h.Y();
565       dz = t0.GetZ() - h.Z();
566
567       //if( dy*dy > 3.5*3.5*(/*t0.GetErr2Y() + */h.ErrY()*h.ErrY() ) ) continue;//SG!!!
568       //if( dz*dz > 3.5*3.5*(/*t0.GetErr2Z() + */h.ErrZ()*h.ErrZ() ) ) continue;
569     }
570
571     if ( !t.TransportToX( row.X() ) ) continue;
572
573     //* Update the track
574
575     if ( first ) {
576       t.Cov()[ 0] = .5 * .5;
577       t.Cov()[ 1] = 0;
578       t.Cov()[ 2] = .5 * .5;
579       t.Cov()[ 3] = 0;
580       t.Cov()[ 4] = 0;
581       t.Cov()[ 5] = .2 * .2;
582       t.Cov()[ 6] = 0;
583       t.Cov()[ 7] = 0;
584       t.Cov()[ 8] = 0;
585       t.Cov()[ 9] = .2 * .2;
586       t.Cov()[10] = 0;
587       t.Cov()[11] = 0;
588       t.Cov()[12] = 0;
589       t.Cov()[13] = 0;
590       t.Cov()[14] = .2 * .2;
591       t.Chi2() = 0;
592       t.NDF() = -5;
593     }
594     float err2Y, err2Z;
595     GetErrors2( iRow, t, err2Y, err2Z );
596
597     if ( !t.Filter2( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
598
599     first = 0;
600   }
601   /*
602   float cosPhi = iTrack.Param().GetCosPhi();
603   p0.Param().TransportToX(ID2Row( iTrack.PointID()[0] ).X());
604   p2.Param().TransportToX(ID2Row( iTrack.PointID()[1] ).X());
605   if( p0.Param().GetCosPhi()*cosPhi<0 ){ // change direction
606   float *par = p0.Param().Par();
607   float *cov = p0.Param().Cov();
608   par[2] = -par[2]; // sin phi
609   par[3] = -par[3]; // DzDs
610     par[4] = -par[4]; // kappa
611     cov[3] = -cov[3];
612     cov[4] = -cov[4];
613     cov[6] = -cov[6];
614     cov[7] = -cov[7];
615     cov[10] = -cov[10];
616     cov[11] = -cov[11];
617     p0.Param().CosPhi() = -p0.Param().GetCosPhi();
618   }
619   */
620 #endif
621 }
622
623 GPUh() void AliHLTTPCCATracker::FitTrack( const AliHLTTPCCATrack &/*track*/, float * /*t0[]*/ ) const
624 {
625   //* Fit the track
626 #ifdef XXX
627   AliHLTTPCCAEndPoint &p2 = ID2Point( track.PointID()[1] );
628   const AliHLTTPCCAHit &c0 = ID2Hit( fTrackHits[p0.TrackHitID()].HitID() );
629   const AliHLTTPCCAHit &c1 = ID2Hit( fTrackHits[track.HitID()[1]].HitID() );
630   const AliHLTTPCCAHit &c2 = ID2Hit( fTrackHits[p2.TrackHitID()].HitID() );
631   const AliHLTTPCCARow &row0 = ID2Row( fTrackHits[p0.TrackHitID()].HitID() );
632   const AliHLTTPCCARow &row1 = ID2Row( fTrackHits[track.HitID()[1]].HitID() );
633   const AliHLTTPCCARow &row2 = ID2Row( fTrackHits[p2.TrackHitID()].HitID() );
634   float sp0[5] = {row0.X(), c0.Y(), c0.Z(), c0.ErrY(), c0.ErrZ() };
635   float sp1[5] = {row1.X(), c1.Y(), c1.Z(), c1.ErrY(), c1.ErrZ() };
636   float sp2[5] = {row2.X(), c2.Y(), c2.Z(), c2.ErrY(), c2.ErrZ() };
637   //std::cout<<"Fit track, points ="<<sp0[0]<<" "<<sp0[1]<<" / "<<sp1[0]<<" "<<sp1[1]<<" / "<<sp2[0]<<" "<<sp2[1]<<std::endl;
638   if ( track.NHits() >= 3 ) {
639     p0.Param().ConstructXYZ3( sp0, sp1, sp2, p0.Param().CosPhi(), t0 );
640     p2.Param().ConstructXYZ3( sp2, sp1, sp0, p2.Param().CosPhi(), t0 );
641     //p2.Param() = p0.Param();
642     //p2.Param().TransportToX(row2.X());
643     //p2.Param().Par()[1] = -p2.Param().Par()[1];
644     //p2.Param().Par()[4] = -p2.Param().Par()[4];
645   } else {
646     p0.Param().X() = row0.X();
647     p0.Param().Y() = c0.Y();
648     p0.Param().Z() = c0.Z();
649     p0.Param().Err2Y() = c0.ErrY() * c0.ErrY();
650     p0.Param().Err2Z() = c0.ErrZ() * c0.ErrZ();
651     p2.Param().X() = row2.X();
652     p2.Param().Y() = c2.Y();
653     p2.Param().Z() = c2.Z();
654     p2.Param().Err2Y() = c2.ErrY() * c2.ErrY();
655     p2.Param().Err2Z() = c2.ErrZ() * c2.ErrZ();
656   }
657 #endif
658 }
659
660
661 GPUd() void AliHLTTPCCATracker::GetErrors2( int iRow, float z, float sinPhi, float cosPhi, float DzDs, float &Err2Y, float &Err2Z ) const
662 {
663   //
664   // Use calibrated cluster error from OCDB
665   //
666
667   fParam.GetClusterErrors2( iRow, z, sinPhi, cosPhi, DzDs, Err2Y, Err2Z );
668 }
669
670 GPUd() void AliHLTTPCCATracker::GetErrors2( int iRow, const AliHLTTPCCATrackParam &t, float &Err2Y, float &Err2Z ) const
671 {
672   //
673   // Use calibrated cluster error from OCDB
674   //
675
676   fParam.GetClusterErrors2( iRow, t.GetZ(), t.SinPhi(), t.GetCosPhi(), t.DzDs(), Err2Y, Err2Z );
677 }
678
679
680 #if !defined(HLTCA_GPUCODE)
681
682 GPUh() void AliHLTTPCCATracker::WriteEvent( std::ostream &out )
683 {
684   // write event to the file
685   for ( int iRow = 0; iRow < fParam.NRows(); iRow++ ) {
686     out << fData.Row( iRow ).HitNumberOffset() << " " << fData.Row( iRow ).NHits() << std::endl;
687   }
688   out << NHitsTotal() << std::endl;
689
690   AliHLTResizableArray<float> y( NHitsTotal() ), z( NHitsTotal() );
691
692   for ( int iRow = 0; iRow < fParam.NRows(); iRow++ ) {
693     const AliHLTTPCCARow &row = Row( iRow );
694     float y0 = row.Grid().YMin();
695     float z0 = row.Grid().ZMin();
696     float stepY = row.HstepY();
697     float stepZ = row.HstepZ();
698     for ( int ih = 0; ih < fData.Row( iRow ).NHits(); ih++ ) {
699       int id = HitInputID( row, ih );
700       y[id] = y0 + HitDataY( row, ih ) * stepY;
701       z[id] = z0 + HitDataZ( row, ih ) * stepZ;
702     }
703   }
704   for ( int ih = 0; ih < NHitsTotal(); ih++ ) {
705     out << y[ih] << " " << z[ih] << std::endl;
706   }
707 }
708
709 GPUh() void AliHLTTPCCATracker::WriteTracks( std::ostream &out )
710 {
711   //* Write tracks to file
712
713   out << fTimers[0] << std::endl;
714   out << *fNOutTrackHits << std::endl;
715   for ( int ih = 0; ih < *fNOutTrackHits; ih++ ) {
716     out << fOutTrackHits[ih] << " ";
717   }
718   out << std::endl;
719
720   out << *fNOutTracks << std::endl;
721
722   for ( int itr = 0; itr < *fNOutTracks; itr++ ) {
723     AliHLTTPCCAOutTrack &t = fOutTracks[itr];
724     AliHLTTPCCATrackParam p1 = t.StartPoint();
725     AliHLTTPCCATrackParam p2 = t.EndPoint();
726     out << t.NHits() << " ";
727     out << t.FirstHitRef() << " ";
728     out << t.OrigTrackID() << " ";
729     out << std::endl;
730     out << p1.X() << " ";
731     out << p1.SignCosPhi() << " ";
732     out << p1.Chi2() << " ";
733     out << p1.NDF() << std::endl;
734     for ( int i = 0; i < 5; i++ ) out << p1.Par()[i] << " ";
735     out << std::endl;
736     for ( int i = 0; i < 15; i++ ) out << p1.Cov()[i] << " ";
737     out << std::endl;
738     out << p2.X() << " ";
739     out << p2.SignCosPhi() << " ";
740     out << p2.Chi2() << " ";
741     out << p2.NDF() << std::endl;
742     for ( int i = 0; i < 5; i++ ) out << p2.Par()[i] << " ";
743     out << std::endl;
744     for ( int i = 0; i < 15; i++ ) out << p2.Cov()[i] << " ";
745     out << std::endl;
746   }
747 }
748
749 GPUh() void AliHLTTPCCATracker::ReadTracks( std::istream &in )
750 {
751   //* Read tracks  from file
752   in >> fTimers[0];
753   in >> *fNOutTrackHits;
754
755   for ( int ih = 0; ih < *fNOutTrackHits; ih++ ) {
756     in >> fOutTrackHits[ih];
757   }
758   in >> *fNOutTracks;
759
760   for ( int itr = 0; itr < *fNOutTracks; itr++ ) {
761     AliHLTTPCCAOutTrack &t = fOutTracks[itr];
762     AliHLTTPCCATrackParam p1, p2;
763     int i;
764     float f;
765     in >> i; t.SetNHits( i );
766     in >> i; t.SetFirstHitRef( i );
767     in >> i; t.SetOrigTrackID( i );
768     in >> f; p1.SetX( f );
769     in >> f; p1.SetSignCosPhi( f );
770     in >> f; p1.SetChi2( f );
771     in >> i; p1.SetNDF( i );
772     for ( int j = 0; j < 5; j++ ) { in >> f; p1.SetPar( j, f ); }
773     for ( int j = 0; j < 15; j++ ) { in >> f; p1.SetCov( j, f ); }
774     in >> f; p2.SetX( f );
775     in >> f; p2.SetSignCosPhi( f );
776     in >> f; p2.SetChi2( f );
777     in >> i; p2.SetNDF( i );
778     for ( int j = 0; j < 5; j++ ) { in >> f; p2.SetPar( j, f ); }
779     for ( int j = 0; j < 15; j++ ) { in >> f; p2.SetCov( j, f ); }
780     t.SetStartPoint( p1 );
781     t.SetEndPoint( p2 );
782   }
783 }
784 #endif