]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/tracking-ca/AliHLTTPCCAGBTracker.cxx
A tracker update: significant clean up, reorganise of the data structures, p-p track...
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCAGBTracker.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 #include "AliHLTTPCCAGBTracker.h"
20 #include "AliHLTTPCCAGBHit.h"
21 #include "AliHLTTPCCAOutTrack.h"
22 #include "AliHLTTPCCATracker.h"
23 #include "AliHLTTPCCAGBTrack.h"
24 #include "AliHLTTPCCATrackParam.h"
25 //#include "AliHLTTPCCAEventHeader.h"
26
27 #include "AliHLTTPCCAMath.h"
28 #include "TStopwatch.h"
29
30 //#define DRAW
31
32 #ifdef DRAW
33 #include "AliHLTTPCCADisplay.h"
34 #include "TApplication.h"
35 #endif //DRAW
36
37
38 AliHLTTPCCAGBTracker::AliHLTTPCCAGBTracker()
39   :
40     fSlices(0), 
41     fNSlices(0), 
42     fHits(0),
43     fNHits(0),
44     fTrackHits(0), 
45     fTracks(0), 
46     fNTracks(0),
47     fSliceTrackInfos(0),
48     fTime(0),
49     fStatNEvents(0),
50     fSliceTrackerTime(0)
51 {
52   //* constructor
53   for( Int_t i=0; i<20; i++ ) fStatTime[i] = 0;
54 }
55
56 AliHLTTPCCAGBTracker::AliHLTTPCCAGBTracker(const AliHLTTPCCAGBTracker&)
57   : 
58     fSlices(0), 
59     fNSlices(0), 
60     fHits(0),
61     fNHits(0),
62     fTrackHits(0), 
63     fTracks(0), 
64     fNTracks(0),
65     fSliceTrackInfos(0),
66     fTime(0),
67     fStatNEvents(0),
68     fSliceTrackerTime(0)
69 {
70   //* dummy
71 }
72
73 AliHLTTPCCAGBTracker &AliHLTTPCCAGBTracker::operator=(const AliHLTTPCCAGBTracker&)
74 {
75   //* dummy
76   return *this;
77 }
78
79 AliHLTTPCCAGBTracker::~AliHLTTPCCAGBTracker()
80 {
81   //* destructor
82   StartEvent();
83   if( fSliceTrackInfos ) delete[] fSliceTrackInfos;
84   fSliceTrackInfos = 0;
85   if( fSlices ) delete[] fSlices;
86   fSlices=0;
87 }
88
89 void AliHLTTPCCAGBTracker::SetNSlices( Int_t N )
90 {
91   //* set N of slices
92   StartEvent();
93   fNSlices = N;
94   if( fSliceTrackInfos ) delete[] fSliceTrackInfos;
95   fSliceTrackInfos = 0;
96   if( fSlices ) delete[] fSlices;
97   fSlices=0;
98   fSlices = new AliHLTTPCCATracker[N];
99   fSliceTrackInfos = new AliHLTTPCCAGBSliceTrackInfo *[N];
100   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
101     fSliceTrackInfos[iSlice] = 0;
102   }
103 }
104
105 void AliHLTTPCCAGBTracker::StartEvent()
106 {
107   //* clean up track and hit arrays
108
109   if( fSliceTrackInfos ){
110     for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
111       if( fSliceTrackInfos[iSlice] ) delete[] fSliceTrackInfos[iSlice];
112       fSliceTrackInfos[iSlice] = 0;
113     }
114   }
115   if( fTrackHits ) delete[] fTrackHits;
116   fTrackHits = 0;
117   if( fTracks ) delete[] fTracks;
118   fTracks = 0;
119   if( fHits ) delete[] fHits;
120   fHits=0;
121   fNHits = 0;
122   fNTracks = 0;
123   for( Int_t i=0; i<fNSlices; i++) fSlices[i].StartEvent();
124 }
125
126
127 void AliHLTTPCCAGBTracker::SetNHits( Int_t nHits )
128 {
129   //* set the number of hits
130   if( fHits ) delete[] fHits;
131   fHits = 0;
132   fHits = new AliHLTTPCCAGBHit[ nHits ];
133   fNHits = 0;
134 }  
135
136 void AliHLTTPCCAGBTracker::ReadHit( Float_t x, Float_t y, Float_t z, 
137                                     Float_t errY, Float_t errZ, Float_t amp,
138                                     Int_t ID, Int_t iSlice, Int_t iRow )
139 {
140   //* read the hit to local array
141   AliHLTTPCCAGBHit &hit = fHits[fNHits];
142   hit.X() = x;
143   hit.Y() = y;
144   hit.Z() = z;  
145   hit.ErrX() = 1.e-4;//fSlices[iSlice].Param().ErrX();
146   hit.ErrY() = errY;
147   hit.ErrZ() = errZ;
148   hit.Amp() = amp;
149   hit.ID() = ID;
150   hit.ISlice()=iSlice;
151   hit.IRow() = iRow;
152   hit.IsUsed() = 0;
153   fNHits++;
154 }
155
156 void AliHLTTPCCAGBTracker::FindTracks()
157 {
158   //* main tracking routine
159   fTime = 0;
160   fStatNEvents++;  
161 #ifdef DRAW
162   if( fStatNEvents<=1 ){
163     if( !gApplication ){
164       TApplication *myapp = new TApplication("myapp",0,0);
165     }    
166     AliHLTTPCCADisplay::Instance().Init();
167   }
168   AliHLTTPCCADisplay::Instance().SetTPCView();
169   AliHLTTPCCADisplay::Instance().DrawTPC();
170 #endif //DRAW  
171
172   if( fNHits<=0 ) return;
173   
174   std::sort(fHits,fHits+fNHits, AliHLTTPCCAGBHit::Compare );  
175
176   // Read hits, row by row
177
178   Int_t nHitsTotal = fNHits;
179   Float_t *hitY = new Float_t [nHitsTotal];
180   Float_t *hitZ = new Float_t [nHitsTotal];
181
182   Int_t sliceNHits[fNSlices];  
183   Int_t rowNHits[fNSlices][200];
184   
185   for( Int_t is=0; is<fNSlices; is++ ){
186     sliceNHits[is] = 0;
187     for( Int_t ir=0; ir<200; ir++ ) rowNHits[is][ir] = 0;    
188   }
189
190   for( Int_t ih=0; ih<nHitsTotal; ih++){
191     AliHLTTPCCAGBHit &h = fHits[ih];    
192     sliceNHits[h.ISlice()]++;
193     rowNHits[h.ISlice()][h.IRow()]++;
194   }
195   
196   Int_t firstSliceHit = 0;
197   for( Int_t is=0; is<fNSlices; is++ ){
198     fFirstSliceHit[is] = firstSliceHit;
199     Int_t rowFirstHits[200];
200     Int_t firstRowHit = 0;
201     for( Int_t ir=0; ir<200; ir++ ){
202       rowFirstHits[ir] = firstRowHit;
203       for( Int_t ih=0; ih<rowNHits[is][ir]; ih++){
204         AliHLTTPCCAGBHit &h = fHits[firstSliceHit + firstRowHit + ih];    
205         hitY[firstRowHit + ih] = h.Y();
206         hitZ[firstRowHit + ih] = h.Z(); 
207       }
208       firstRowHit+=rowNHits[is][ir];
209     }
210     fSlices[is].ReadEvent( rowFirstHits, rowNHits[is], hitY, hitZ, sliceNHits[is] );
211     
212     //Int_t data[ rowNHits[is]]
213     //AliHLTTPCCAEventHeader event;
214
215     firstSliceHit+=sliceNHits[is];
216   }
217
218
219   delete[] hitY;
220   delete[] hitZ;
221
222   TStopwatch timer1;
223   TStopwatch timer2;
224   //std::cout<<"Start CA reconstruction"<<std::endl;
225   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
226     TStopwatch timer;
227     AliHLTTPCCATracker &slice = fSlices[iSlice];
228     slice.Reconstruct();
229     timer.Stop();
230     //fTime+= timer.CpuTime();
231     //blaTime+= timer.CpuTime();
232     fStatTime[0] += timer.CpuTime();
233     fStatTime[1]+=slice.Timers()[0];
234     fStatTime[2]+=slice.Timers()[1];
235     fStatTime[3]+=slice.Timers()[2];
236     fStatTime[4]+=slice.Timers()[3];
237     fStatTime[5]+=slice.Timers()[4];
238     fStatTime[6]+=slice.Timers()[5];
239     fStatTime[7]+=slice.Timers()[6];
240     fStatTime[8]+=slice.Timers()[7];
241   }
242
243   timer2.Stop();
244   //std::cout<<"blaTime = "<<timer2.CpuTime()*1.e3<<std::endl;
245   fSliceTrackerTime = timer2.CpuTime();
246
247   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
248     AliHLTTPCCATracker &iS = fSlices[iSlice];
249     if( fSliceTrackInfos[iSlice] ) delete[] fSliceTrackInfos[iSlice];
250     fSliceTrackInfos[iSlice]=0;
251     Int_t iNTracks = *iS.NOutTracks();
252     fSliceTrackInfos[iSlice] = new AliHLTTPCCAGBSliceTrackInfo[iNTracks];
253     for( Int_t itr=0; itr<iNTracks; itr++ ){
254       fSliceTrackInfos[iSlice][itr].fPrevNeighbour = -1;
255       fSliceTrackInfos[iSlice][itr].fNextNeighbour = -1; 
256       fSliceTrackInfos[iSlice][itr].fUsed = 0;
257     }
258   }
259   
260   //std::cout<<"Start CA merging"<<std::endl;
261   TStopwatch timerMerge;
262   Merging();
263   timerMerge.Stop();
264   fStatTime[9]+=timerMerge.CpuTime();  
265   //fTime+=timerMerge.CpuTime();
266   //std::cout<<"End CA merging"<<std::endl;
267   timer1.Stop();
268   fTime+= timer1.CpuTime();
269
270 #ifdef DRAW
271   AliHLTTPCCADisplay::Instance().Ask();
272 #endif //DRAW
273 }
274
275
276 void AliHLTTPCCAGBTracker::FindTracks0()
277 {
278   //* main tracking routine
279   fTime = 0;
280   fStatNEvents++;  
281 #ifdef DRAW
282   if( fStatNEvents<=1 ){
283     if( !gApplication ){
284       TApplication *myapp = new TApplication("myapp",0,0);
285     }    
286     AliHLTTPCCADisplay::Instance().Init();
287   }
288   AliHLTTPCCADisplay::Instance().SetTPCView();
289   AliHLTTPCCADisplay::Instance().DrawTPC();
290 #endif //DRAW  
291
292   if( fNHits<=0 ) return;
293   
294   std::sort(fHits,fHits+fNHits, AliHLTTPCCAGBHit::Compare );  
295
296   // Read hits, row by row
297
298   Int_t nHitsTotal = fNHits;
299   Float_t *hitY = new Float_t [nHitsTotal];
300   Float_t *hitZ = new Float_t [nHitsTotal];
301
302   Int_t sliceNHits[fNSlices];  
303   Int_t rowNHits[fNSlices][200];
304   
305   for( Int_t is=0; is<fNSlices; is++ ){
306     sliceNHits[is] = 0;
307     for( Int_t ir=0; ir<200; ir++ ) rowNHits[is][ir] = 0;    
308   }
309
310   for( Int_t ih=0; ih<nHitsTotal; ih++){
311     AliHLTTPCCAGBHit &h = fHits[ih];    
312     sliceNHits[h.ISlice()]++;
313     rowNHits[h.ISlice()][h.IRow()]++;
314   }
315   
316   Int_t firstSliceHit = 0;
317   for( Int_t is=0; is<fNSlices; is++ ){
318     fFirstSliceHit[is] = firstSliceHit;
319     Int_t rowFirstHits[200];
320     Int_t firstRowHit = 0;
321     for( Int_t ir=0; ir<200; ir++ ){
322       rowFirstHits[ir] = firstRowHit;
323       for( Int_t ih=0; ih<rowNHits[is][ir]; ih++){
324         AliHLTTPCCAGBHit &h = fHits[firstSliceHit + firstRowHit + ih];    
325         hitY[firstRowHit + ih] = h.Y();
326         hitZ[firstRowHit + ih] = h.Z(); 
327       }
328       firstRowHit+=rowNHits[is][ir];
329     }
330     //if( is==24 ){//SG!!!
331     fSlices[is].ReadEvent( rowFirstHits, rowNHits[is], hitY, hitZ, sliceNHits[is] );
332     //}
333     
334     //Int_t data[ rowNHits[is]]
335     //AliHLTTPCCAEventHeader event;
336
337     firstSliceHit+=sliceNHits[is];
338   }
339
340   delete[] hitY;
341   delete[] hitZ;
342 }
343
344
345 void AliHLTTPCCAGBTracker::FindTracks1()
346 {
347   //* main tracking routine
348
349   TStopwatch timer2;
350   //std::cout<<"Start CA reconstruction"<<std::endl;
351   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
352     TStopwatch timer;
353     AliHLTTPCCATracker &slice = fSlices[iSlice];
354     slice.Reconstruct();
355     timer.Stop();
356     //fTime+= timer.CpuTime();
357     //blaTime+= timer.CpuTime();
358     fStatTime[0] += timer.CpuTime();
359     fStatTime[1]+=slice.Timers()[0];
360     fStatTime[2]+=slice.Timers()[1];
361     fStatTime[3]+=slice.Timers()[2];
362     fStatTime[4]+=slice.Timers()[3];
363     fStatTime[5]+=slice.Timers()[4];
364     fStatTime[6]+=slice.Timers()[5];
365     fStatTime[7]+=slice.Timers()[6];
366     fStatTime[8]+=slice.Timers()[7];
367   }
368
369   timer2.Stop();
370   //std::cout<<"blaTime = "<<timer2.CpuTime()*1.e3<<std::endl;
371   fSliceTrackerTime = timer2.CpuTime();
372 }
373
374
375 void AliHLTTPCCAGBTracker::FindTracks2()
376 {
377   //* main tracking routine
378
379   TStopwatch timer1;
380
381   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
382     AliHLTTPCCATracker &iS = fSlices[iSlice];
383     if( fSliceTrackInfos[iSlice] ) delete[] fSliceTrackInfos[iSlice];
384     fSliceTrackInfos[iSlice]=0;
385     Int_t iNTracks = *iS.NOutTracks();    
386     fSliceTrackInfos[iSlice] = new AliHLTTPCCAGBSliceTrackInfo[iNTracks];
387     for( Int_t itr=0; itr<iNTracks; itr++ ){
388       fSliceTrackInfos[iSlice][itr].fPrevNeighbour = -1;
389       fSliceTrackInfos[iSlice][itr].fNextNeighbour = -1; 
390       fSliceTrackInfos[iSlice][itr].fUsed = 0;
391     }
392   }
393   
394   //std::cout<<"Start CA merging"<<std::endl;
395   TStopwatch timerMerge;
396   Merging();
397   timerMerge.Stop();
398   fStatTime[9]+=timerMerge.CpuTime();  
399   //fTime+=timerMerge.CpuTime();
400   //std::cout<<"End CA merging"<<std::endl;
401   timer1.Stop();
402   fTime+= fSliceTrackerTime + timer1.CpuTime();
403
404 #ifdef DRAW
405   AliHLTTPCCADisplay::Instance().Ask();
406 #endif //DRAW
407 }
408
409 void AliHLTTPCCAGBTracker::Merging()
410 {
411   //* track merging between slices
412
413   Float_t dalpha = fSlices[1].Param().Alpha() - fSlices[0].Param().Alpha();
414   Int_t nextSlice[100], prevSlice[100];
415   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
416     nextSlice[iSlice] = iSlice + 1;
417     prevSlice[iSlice] = iSlice - 1;
418   }
419   nextSlice[ fNSlices/2 - 1 ] = 0;
420   prevSlice[ 0 ] = fNSlices/2 - 1;
421   nextSlice[ fNSlices - 1 ] = fNSlices/2;
422   prevSlice[ fNSlices/2 ] = fNSlices - 1;
423   
424   TStopwatch timerMerge1;
425
426   Int_t maxNSliceTracks = 0;
427   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
428     AliHLTTPCCATracker &iS = fSlices[iSlice];
429     if( maxNSliceTracks < *iS.NOutTracks() ) maxNSliceTracks = *iS.NOutTracks();
430   }
431
432   //* arrays for rotated track parameters
433
434   AliHLTTPCCATrackParam *iTrParams[2], *jTrParams[2];
435   Bool_t *iOK[2], *jOK[2];
436   for( Int_t i=0; i<2; i++ ){
437     iTrParams[i] = new AliHLTTPCCATrackParam[maxNSliceTracks];
438     jTrParams[i] = new AliHLTTPCCATrackParam[maxNSliceTracks];
439     iOK[i] = new Bool_t [maxNSliceTracks];
440     jOK[i] = new Bool_t [maxNSliceTracks];
441   }
442   
443   for( Int_t iSlice=0; iSlice<fNSlices; iSlice++ ){
444     //std::cout<<"\nMerge slice "<<iSlice<<std::endl<<std::endl;
445     AliHLTTPCCATracker &iS = fSlices[iSlice];
446     Int_t jSlice = nextSlice[iSlice];
447     AliHLTTPCCATracker &jS = fSlices[jSlice];    
448     Int_t iNTracks = *iS.NOutTracks();
449     Int_t jNTracks = *jS.NOutTracks();
450     if( iNTracks<=0 || jNTracks<=0 ) continue;
451     
452     //* prepare slice tracks for merging
453     
454     for (Int_t itr=0; itr<iNTracks; itr++) {      
455       iOK[0][itr] = 0;
456       iOK[1][itr] = 0;
457       if( iS.OutTracks()[itr].NHits()<10 ) continue;
458       AliHLTTPCCATrackParam &iT1 = iTrParams[0][itr];
459       AliHLTTPCCATrackParam &iT2 = iTrParams[1][itr];
460       iT1 = iS.OutTracks()[itr].StartPoint();
461       iT2 = iS.OutTracks()[itr].EndPoint();
462       iOK[0][itr] = iT1.Rotate( dalpha/2 - CAMath::Pi()/2 );
463       iOK[1][itr] = iT2.Rotate( dalpha/2 - CAMath::Pi()/2 );
464
465       if( iOK[0][itr] ){
466         iOK[0][itr] = iT1.TransportToX( 0, .99 );
467         if( iS.Param().RMin() > iT1.Y() || iS.Param().RMax() < iT1.Y() ) iOK[0][itr]=0;
468       }
469       if( iOK[1][itr] ){
470         iOK[1][itr] = iT2.TransportToX( 0, .99 );
471         if( iS.Param().RMin() > iT2.Y() || iS.Param().RMax() < iT2.Y() ) iOK[1][itr]=0;
472       }
473     }
474
475     for (Int_t jtr=0; jtr<jNTracks; jtr++) {      
476       jOK[0][jtr] = 0;
477       jOK[1][jtr] = 0;
478       if( jS.OutTracks()[jtr].NHits()<10 ) continue;
479       AliHLTTPCCATrackParam &jT1 = jTrParams[0][jtr];
480       AliHLTTPCCATrackParam &jT2 = jTrParams[1][jtr];
481       jT1 = jS.OutTracks()[jtr].StartPoint();
482       jT2 = jS.OutTracks()[jtr].EndPoint();
483       jOK[0][jtr] = jT1.Rotate( -dalpha/2 - CAMath::Pi()/2 );
484       jOK[1][jtr] = jT2.Rotate( -dalpha/2 - CAMath::Pi()/2 );
485       if( jOK[0][jtr] ){
486         jOK[0][jtr] = jT1.TransportToX( 0, .99 );
487         if( jS.Param().RMin() > jT1.Y() || jS.Param().RMax() < jT1.Y() ) jOK[0][jtr]=0;
488       }
489       if( jOK[1][jtr] ){
490         jOK[1][jtr] = jT2.TransportToX( 0, .99 );
491         if( jS.Param().RMin() > jT2.Y() || jS.Param().RMax() < jT2.Y() ) jOK[1][jtr]=0;
492       }
493     }
494
495     //* start merging
496     //std::cout<<"Start slice merging.."<<std::endl;
497     for (Int_t itr=0; itr<iNTracks; itr++) {      
498       if( !iOK[0][itr] && !iOK[1][itr] ) continue;
499       Int_t jBest = -1;
500       Int_t lBest = 0;
501       for (Int_t jtr=0; jtr<jNTracks; jtr++) {
502         if( jS.OutTracks()[jtr].NHits() < lBest ) continue;     
503         if( !jOK[0][jtr] && !jOK[1][jtr] ) continue;
504         for( Int_t ip=0; ip<2 && (jBest!=jtr) ; ip++ ){
505           if( !iOK[ip][itr] ) continue;
506           for( Int_t jp=0; jp<2 && (jBest!=jtr) ; jp++ ){
507             if( !jOK[jp][jtr] ) continue;         
508             AliHLTTPCCATrackParam &iT = iTrParams[ip][itr];
509             AliHLTTPCCATrackParam &jT = jTrParams[jp][jtr];
510             // check for neighbouring   
511             {
512               Float_t factor2 = 3.5*3.5;
513               Float_t d = jT.GetY() - iT.GetY();
514               Float_t s2 = jT.GetErr2Y() + iT.GetErr2Y();
515               if( d*d>factor2*s2 ){
516                 continue;
517               }
518               d = jT.GetZ() - iT.GetZ();
519               s2 = jT.GetErr2Z() + iT.GetErr2Z();
520               if( d*d>factor2*s2 ){         
521                 continue;
522               }
523               Bool_t ok = 1;
524               { // phi, kappa, DsDz signs are the same 
525                 d = jT.GetSinPhi() - iT.GetSinPhi();
526                 s2 = jT.GetErr2SinPhi() + iT.GetErr2SinPhi();
527                 if( d*d>factor2*s2 ) ok = 0;
528                 d = jT.GetKappa() - iT.GetKappa(); 
529                 s2 = jT.GetErr2Kappa() + iT.GetErr2Kappa();
530                 if( d*d>factor2*s2 ) ok = 0;
531                 d = jT.GetDzDs() - iT.GetDzDs();
532                 s2 = jT.GetErr2DzDs() + iT.GetErr2DzDs();
533                 if( d*d>factor2*s2 ) ok = 0;
534               }
535               if( !ok ){ // phi, kappa, DsDz signs are the different
536                 d = jT.GetSinPhi() + iT.GetSinPhi();
537                 s2 = jT.GetErr2SinPhi() + iT.GetErr2SinPhi();
538                 if( d*d>factor2*s2 ) continue;
539                 d = jT.GetKappa() + iT.GetKappa(); 
540                 s2 = jT.GetErr2Kappa() + iT.GetErr2Kappa();
541                 if( d*d>factor2*s2 ) continue;
542                 d = jT.GetDzDs() + iT.GetDzDs();
543                 s2 = jT.GetErr2DzDs() + iT.GetErr2DzDs();
544                 if( d*d>factor2*s2 ) continue;
545               }
546               // tracks can be matched
547
548               lBest = jS.OutTracks()[jtr].NHits();
549               jBest = jtr;
550             }
551           }
552         }
553       }
554       if( jBest>=0 ){
555         Int_t oldi = fSliceTrackInfos[jSlice][jBest].fPrevNeighbour;
556         if( oldi >= 0 ){
557           if( iS.OutTracks()[ oldi ].NHits() < iS.OutTracks()[ itr ].NHits() ){
558             fSliceTrackInfos[jSlice][jBest].fPrevNeighbour = -1;
559             fSliceTrackInfos[iSlice][oldi].fNextNeighbour = -1;
560           } else continue;
561         }
562         //SG!!!
563         fSliceTrackInfos[iSlice][itr].fNextNeighbour = jBest;
564         fSliceTrackInfos[jSlice][jBest].fPrevNeighbour = itr;   
565       }    
566     }
567   }
568
569   for( Int_t i=0; i<2; i++){
570     if( iTrParams[i] ) delete[] iTrParams[i];
571     if( jTrParams[i] ) delete[] jTrParams[i];
572     if( iOK[i] ) delete[] iOK[i];
573     if( jOK[i] ) delete[] jOK[i];
574   }
575
576   timerMerge1.Stop();
577   fStatTime[10]+=timerMerge1.CpuTime();
578
579   TStopwatch timerMerge2;
580
581   Int_t nTracksTot = 0;
582   for( Int_t iSlice = 0; iSlice<fNSlices; iSlice++ ){    
583     AliHLTTPCCATracker &slice = fSlices[iSlice];
584     nTracksTot+= *slice.NOutTracks();
585   }
586   
587   if( fTrackHits ) delete[] fTrackHits;
588   fTrackHits = 0;
589   if(fTracks ) delete[] fTracks;
590   fTracks = 0;
591   fTrackHits = new Int_t [fNHits*10];
592   fTracks = new AliHLTTPCCAGBTrack[nTracksTot];
593   fNTracks = 0;
594
595   Int_t nTrackHits = 0;
596
597   //std::cout<<"\nStart global track creation...\n"<<std::endl;  
598
599   static Int_t nRejected = 0;
600
601   Int_t maxNRows = fSlices[0].Param().NRows();
602   
603   for( Int_t iSlice = 0; iSlice<fNSlices; iSlice++ ){
604     
605     AliHLTTPCCATracker &slice = fSlices[iSlice];
606     for( Int_t itr=0; itr<*slice.NOutTracks(); itr++ ){
607       if( fSliceTrackInfos[iSlice][itr].fUsed ) continue;
608       //std::cout<<"\n slice "<<iSlice<<", track "<<itr<<"\n"<<std::endl;
609       //AliHLTTPCCAOutTrack &tCA = slice.OutTracks()[itr];
610       AliHLTTPCCAGBTrack &t = fTracks[fNTracks];
611       //t.Param() = tCA.StartPoint();
612        //t.Alpha() = slice.Param().Alpha();
613       t.NHits() = 0;
614       t.FirstHitRef() = nTrackHits;
615
616       struct FitPoint{    
617         Int_t fISlice;
618         Int_t fHitID;
619         Float_t fX, fY, fZ, fErr2Y, fErr2Z, fAmp;
620       } fitPoints[300];
621       for( Int_t i=0; i<maxNRows; i++ ) fitPoints[i].fISlice = -1;
622      
623       Int_t nHits = 0;
624       Int_t jSlice = iSlice;
625       Int_t jtr = itr;
626       do{ 
627         if( fSliceTrackInfos[jSlice][jtr].fUsed ) break;
628         fSliceTrackInfos[jSlice][jtr].fUsed = 1;
629         AliHLTTPCCATracker &jslice = fSlices[jSlice];
630         AliHLTTPCCAOutTrack &jTr = jslice.OutTracks()[jtr];
631         for( Int_t jhit=0; jhit<jTr.NHits(); jhit++){
632           Int_t id = fFirstSliceHit[jSlice] + jslice.OutTrackHits()[jTr.FirstHitRef()+jhit];      
633           AliHLTTPCCAGBHit &h = fHits[id];
634           FitPoint &p =  fitPoints[h.IRow()];
635           if( p.fISlice >=0 ) continue;
636           p.fISlice = h.ISlice();
637           p.fHitID = id;
638           p.fX = jslice.Rows()[h.IRow()].X();
639           p.fY = h.Y();
640           p.fZ = h.Z();
641           //p.fErr2Y = h.ErrY()*h.ErrY();
642           //p.fErr2Z = h.ErrZ()*h.ErrZ();
643           p.fAmp = h.Amp();
644           nHits++;          
645         }
646         jtr = fSliceTrackInfos[jSlice][jtr].fNextNeighbour;
647         jSlice = nextSlice[jSlice];     
648       } while( jtr >=0 ); 
649  
650       if( nHits < 10 ) continue;     //SG!!!
651
652       Int_t firstRow = 0, lastRow = maxNRows-1;
653       for( firstRow=0; firstRow<maxNRows; firstRow++ ){
654         if( fitPoints[firstRow].fISlice>=0 ) break;
655       }
656       for( lastRow=maxNRows-1; lastRow>=0; lastRow-- ){
657         if( fitPoints[lastRow].fISlice>=0 ) break;
658       }
659       Int_t mmidRow = (firstRow + lastRow )/2;
660       Int_t midRow = firstRow;
661       for( Int_t i=firstRow+1; i<=lastRow; i++ ){
662         if( fitPoints[i].fISlice<0 ) continue;  
663         if( CAMath::Abs(i-mmidRow)>=CAMath::Abs(midRow-mmidRow) ) continue;
664         midRow = i;
665       }
666       if( midRow==firstRow || midRow==lastRow ) continue;
667   
668       Int_t searchRows[300];
669       Int_t nSearchRows = 0;
670
671       for( Int_t i=firstRow; i<=lastRow; i++ ) searchRows[nSearchRows++] = i;
672       for( Int_t i=lastRow+1; i<maxNRows; i++ ) searchRows[nSearchRows++] = i;
673       for( Int_t i=firstRow-1; i>=0; i-- ) searchRows[nSearchRows++] = i;
674       
675       // refit 
676       
677       AliHLTTPCCATrackParam t0;
678
679       { 
680
681         {
682           FitPoint &p0 =  fitPoints[firstRow];
683           FitPoint &p1 =  fitPoints[midRow];
684           FitPoint &p2 =  fitPoints[lastRow];
685           Float_t x0=p0.fX, y0=p0.fY, z0=p0.fZ;
686           Float_t x1=p1.fX, y1=p1.fY, z1=p1.fZ;
687           Float_t x2=p2.fX, y2=p2.fY, z2=p2.fZ;
688           if( p1.fISlice!=p0.fISlice ){
689             Float_t dAlpha = fSlices[p0.fISlice].Param().Alpha() - fSlices[p1.fISlice].Param().Alpha();
690             Float_t c = CAMath::Cos(dAlpha);
691             Float_t s = CAMath::Sin(dAlpha);
692             x1 = p1.fX*c + p1.fY*s;
693             y1 = p1.fY*c - p1.fX*s;
694           }
695           if( p2.fISlice!=p0.fISlice ){
696             Float_t dAlpha = fSlices[p0.fISlice].Param().Alpha() - fSlices[p2.fISlice].Param().Alpha();
697             Float_t c = CAMath::Cos(dAlpha);
698             Float_t s = CAMath::Sin(dAlpha);
699             x2 = p2.fX*c + p2.fY*s;
700             y2 = p2.fY*c - p2.fX*s;
701           }
702
703           Float_t sp0[5] = {x0, y0, z0, .5, .5 };       
704           Float_t sp1[5] = {x1, y1, z1, .5, .5 };
705           Float_t sp2[5] = {x2, y2, z2, .5, .5 };
706           t0.ConstructXYZ3(sp0,sp1,sp2,1., 0);
707         }
708         
709         Int_t currslice = fitPoints[firstRow].fISlice;
710
711         for( Int_t rowID=0; rowID<nSearchRows; rowID++ ){
712           Int_t iRow = searchRows[rowID];         
713           FitPoint &p =  fitPoints[iRow];
714
715           if( p.fISlice>=0 ){ 
716
717             //* Existing hit
718
719             //* Rotate to the new slice
720
721             if( p.fISlice!=currslice ){ 
722               if( !t0.Rotate( fSlices[p.fISlice].Param().Alpha() - fSlices[currslice].Param().Alpha() ) ) continue;     
723               currslice = p.fISlice;
724             }
725             //* Transport to the new row
726             
727             if( !t0.TransportToX( p.fX, .99 ) ) continue;
728
729             //* Calculate hit errors
730             
731             GetErrors2( p.fISlice, iRow, t0, p.fErr2Y, p.fErr2Z );
732
733           } else { 
734             //continue; //SG!!
735             //* Search for the missed hit
736
737             Float_t factor2 = 3.5*3.5;
738
739             AliHLTTPCCATracker *cslice = &(fSlices[currslice]);
740             AliHLTTPCCARow *row = &(cslice->Rows()[iRow]);
741             if( !t0.TransportToX( row->X(), .99 ) ) continue;
742
743             if( t0.GetY() > row->MaxY() ){ //next slice
744
745               Int_t j = nextSlice[currslice];
746
747               //* Rotate to the new slice
748               
749               if( !t0.Rotate( -fSlices[currslice].Param().Alpha() +fSlices[j].Param().Alpha() ) ) continue;           
750               currslice = j;
751               cslice = &(fSlices[currslice]);
752               row = &(cslice->Rows()[iRow]);
753               if( !t0.TransportToX( row->X(), .99 ) ) continue;
754               if( CAMath::Abs(t0.GetY()) > row->MaxY() ) continue;
755
756             }else if( t0.GetY() < -row->MaxY() ){ //prev slice
757               Int_t j = prevSlice[currslice];
758               //* Rotate to the new slice
759               if( !t0.Rotate( -fSlices[currslice].Param().Alpha() +fSlices[j].Param().Alpha() ) ) break;
760               currslice = j;
761               cslice = &(fSlices[currslice]);
762               row = &(cslice->Rows()[iRow]);
763               if( !t0.TransportToX( row->X(), .99 ) ) continue;         
764               if( CAMath::Abs(t0.GetY()) > row->MaxY() ) continue;
765             }
766             
767             Int_t bestsh = -1;
768             Float_t ds = 1.e10;
769             Float_t y0 = row->Grid().YMin();
770             Float_t z0 = row->Grid().ZMin();
771             Float_t stepY = row->HstepY();
772             Float_t stepZ = row->HstepZ();
773             uint4* tmpint4 = cslice->RowData() + row->FullOffset();
774             ushort2 *hits = reinterpret_cast<ushort2*>(tmpint4);
775
776             for( Int_t ish=0; ish<row->NHits(); ish++ ){
777               AliHLTTPCCAHit sh;// = cslice->Hits()[row->FirstHit()+ish];
778               {
779                 ushort2 hh = hits[ish];
780                 sh.Y() = y0 + hh.x*stepY;
781                 sh.Z() = z0 + hh.y*stepZ;
782               }
783
784               Float_t dy = sh.Y() - t0.GetY();
785               Float_t dz = sh.Z() - t0.GetZ();
786               Float_t dds = dy*dy+dz*dz;
787               if( dds<ds ){
788                 ds = dds;
789                 bestsh = ish;
790               }
791             }
792             if( bestsh<0 ) continue;
793
794             //* Calculate hit errors
795             
796             GetErrors2( currslice, iRow, t0, p.fErr2Y, p.fErr2Z );
797
798             AliHLTTPCCAHit sh;// = cslice->Hits()[row->FirstHit()+bestsh];
799             {
800               ushort2 hh = hits[bestsh];
801               sh.Y() = y0 + hh.x*stepY;
802               sh.Z() = z0 + hh.y*stepZ;
803             }
804
805             Float_t dy = sh.Y() - t0.GetY();
806             Float_t dz = sh.Z() - t0.GetZ();
807             Float_t s2z = /*t0.GetErr2Z() + */ p.fErr2Z;
808             if( dz*dz>factor2*s2z ) continue;           
809             Float_t s2y = /*t0.GetErr2Y() + */ p.fErr2Y;
810             if( dy*dy>factor2*s2y ) continue;
811
812             p.fISlice = currslice;
813             p.fHitID = fFirstSliceHit[p.fISlice] + cslice->HitInputIDs()[row->FirstHit() + bestsh];
814             p.fX = row->X();
815             p.fY = sh.Y();
816             p.fZ = sh.Z();
817             p.fAmp = fHits[p.fHitID].Amp();
818           }
819
820           //* Update the track
821           
822           t0.Filter2( p.fY, p.fZ, p.fErr2Y, p.fErr2Z, .99 );      
823         }
824
825         //* final refit, dE/dx calculation
826         //std::cout<<"\n\nstart refit..\n"<<std::endl;
827
828         AliHLTTPCCATrackParam::AliHLTTPCCATrackFitParam fitPar;
829         {
830           Double_t sumDeDx = 0;
831           Int_t nDeDx = 0;
832           t.NHits() = 0;
833         
834           t0.CalculateFitParameters( fitPar, fSlices[0].Param().Bz() );
835
836           t0.Cov()[ 0] = .1;
837           t0.Cov()[ 1] = 0;
838           t0.Cov()[ 2] = .1;
839           t0.Cov()[ 3] = 0;
840           t0.Cov()[ 4] = 0;
841           t0.Cov()[ 5] = .1;
842           t0.Cov()[ 6] = 0;
843           t0.Cov()[ 7] = 0;
844           t0.Cov()[ 8] = 0;
845           t0.Cov()[ 9] = .1;
846           t0.Cov()[10] = 0;
847           t0.Cov()[11] = 0;
848           t0.Cov()[12] = 0;
849           t0.Cov()[13] = 0;
850           t0.Cov()[14] = .1;
851           t0.Chi2() = 0;
852           t0.NDF() = -5;        
853           Bool_t first = 1;
854           for( Int_t iRow = maxNRows-1; iRow>=0; iRow-- ){
855             FitPoint &p =  fitPoints[iRow];
856             if( p.fISlice<0 ) continue;
857             fTrackHits[nTrackHits+t.NHits()] = p.fHitID;
858             t.NHits()++;
859             
860             //* Rotate to the new slice
861
862             if( p.fISlice!=currslice ){ 
863               //std::cout<<"rotate..."<<std::endl;
864               //std::cout<<" before rotation:"<<std::endl;
865               //t0.Print();
866               if( !t0.Rotate( fSlices[p.fISlice].Param().Alpha() - fSlices[currslice].Param().Alpha() ) ) continue;     
867               //std::cout<<" after rotation:"<<std::endl;
868               //t0.Print();
869               currslice = p.fISlice;
870             }
871             //* Transport to the new row
872             
873             //std::cout<<" before transport:"<<std::endl;
874             //t0.Print();
875             
876             //if( !t0.TransportToX( p.fX, .99 ) ) continue;         
877             if( !t0.TransportToXWithMaterial( p.fX, fitPar ) ) continue;            
878             //if( !t0.TransportToX( p.fX, .99 ) ) continue;         
879             //std::cout<<" after transport:"<<std::endl;
880             //t0.Print();
881
882             //* Update the track
883             
884             if( first ){
885               t0.Cov()[ 0] = .5*.5;
886               t0.Cov()[ 1] = 0;
887               t0.Cov()[ 2] = .5*.5;
888               t0.Cov()[ 3] = 0;
889               t0.Cov()[ 4] = 0;
890               t0.Cov()[ 5] = .2*.2;
891               t0.Cov()[ 6] = 0;
892               t0.Cov()[ 7] = 0;
893               t0.Cov()[ 8] = 0;
894               t0.Cov()[ 9] = .2*.2;
895               t0.Cov()[10] = 0;
896               t0.Cov()[11] = 0;
897               t0.Cov()[12] = 0;
898               t0.Cov()[13] = 0;
899               t0.Cov()[14] = .5*.5;
900               t0.Chi2() = 0;
901               t0.NDF() = -5;
902             }
903
904             //std::cout<<" before filtration:"<<std::endl;
905             //t0.Print();
906
907             if( !t0.Filter2( p.fY, p.fZ, p.fErr2Y, p.fErr2Z, .99 ) ) continue;    
908             //std::cout<<" after filtration:"<<std::endl;
909             //t0.Print();
910               first = 0;
911             
912             if( CAMath::Abs( t0.CosPhi() )>1.e-4 ){
913               Float_t dLdX = CAMath::Sqrt(1.+t0.DzDs()*t0.DzDs())/CAMath::Abs(t0.CosPhi());
914               sumDeDx+=p.fAmp/dLdX;
915               nDeDx++;
916             } 
917           }
918           t.DeDx() = 0;
919           if( nDeDx >0 ) t.DeDx() = sumDeDx/nDeDx;
920           if( t0.GetErr2Y()<=0 ){
921             //std::cout<<"nhits = "<<t.NHits()<<", t0.GetErr2Y() = "<<t0.GetErr2Y()<<std::endl;
922             //t0.Print();
923             //exit(1);
924           }
925         }
926
927         if( t.NHits()<30 ) continue;//SG!!
928         Double_t dAlpha = 0;
929         {
930           Double_t xTPC=83.65;
931           Double_t ddAlpha = 0.00609235;
932           
933           if( t0.TransportToXWithMaterial( xTPC, fitPar ) ){
934             Double_t y=t0.GetY();
935             Double_t ymax=xTPC*CAMath::Tan(dAlpha/2.); 
936             if (y > ymax) {
937               if( t0.Rotate( ddAlpha ) ){ dAlpha=ddAlpha;  t0.TransportToXWithMaterial( xTPC, fitPar ); }
938             } else if (y <-ymax) {
939               if( t0.Rotate( -ddAlpha ) ){  dAlpha=-ddAlpha; t0.TransportToXWithMaterial( xTPC, fitPar );}
940             }       
941           }
942         }
943
944         {
945           Bool_t ok=1;
946           
947           Float_t *c = t0.Cov();
948           for( Int_t i=0; i<15; i++ ) ok = ok && finite(c[i]);
949           for( Int_t i=0; i<5; i++ ) ok = ok && finite(t0.Par()[i]);
950           ok = ok && (t0.GetX()>50);
951           
952           if( c[0]<=0 || c[2]<=0 || c[5]<=0 || c[9]<=0 || c[14]<=0 ) ok = 0;
953           //if( c[0]>5. || c[2]>5. || c[5]>2. || c[9]>2 || c[14]>2 ) ok = 0;
954           if(!ok){
955             nRejected++;
956             //std::cout<<"\n\nRejected: "<<nRejected<<"\n"<<std::endl;
957             continue;
958           }
959         }
960
961         if( CAMath::Abs(t0.Kappa())<1.e-8 ) t0.Kappa() = 1.e-8; 
962         t.Param() = t0;
963         t.Alpha() = fSlices[currslice].Param().Alpha() + dAlpha;
964         nTrackHits+= t.NHits();
965         fNTracks++;   
966       }
967     }
968   }
969   //std::cout<<"\n\nRejected: "<<nRejected<<"\n"<<std::endl;
970   timerMerge2.Stop();
971   fStatTime[11]+=timerMerge2.CpuTime();
972
973   TStopwatch timerMerge3;
974
975   //* selection  
976   //std::cout<<"Selection..."<<std::endl;
977   {
978     AliHLTTPCCAGBTrack *vtracks = new AliHLTTPCCAGBTrack [fNTracks];
979     Int_t *vhits = new Int_t [fNHits];
980     AliHLTTPCCAGBTrack **vptracks = new AliHLTTPCCAGBTrack* [fNTracks];
981
982     for( Int_t itr=0; itr<fNTracks; itr++ ){
983       vptracks[itr] = &(fTracks[itr]);
984     }
985     Int_t nTracks = 0;
986     Int_t nHits = 0;
987     std::sort(vptracks, vptracks+fNTracks, AliHLTTPCCAGBTrack::ComparePNClusters );
988     for( Int_t itr=0; itr<fNTracks; itr++ ){
989       AliHLTTPCCAGBTrack &t = *(vptracks[itr]);
990       AliHLTTPCCAGBTrack &to = vtracks[nTracks];
991       to=*(vptracks[itr]);
992       to.FirstHitRef() = nHits;
993       to.NHits() = 0;
994       for( Int_t ih=0; ih<t.NHits(); ih++ ){
995         Int_t jh = fTrackHits[t.FirstHitRef()+ih];
996         AliHLTTPCCAGBHit &h = fHits[jh];
997         if( h.IsUsed() ) continue;
998         vhits[to.FirstHitRef() + to.NHits()] = jh;
999         to.NHits()++;
1000         h.IsUsed() = 1;
1001       }
1002       if( to.NHits()<10 ) continue;//SG!!!
1003       nHits+=to.NHits();
1004       nTracks++;
1005       //std::cout<<to.Param().GetErr2Y()<<" "<<to.Param().GetErr2Z()<<std::endl;
1006     }
1007     fNTracks = nTracks;
1008     if( fTrackHits ) delete[] fTrackHits;
1009     if( fTracks ) delete[] fTracks;
1010     fTrackHits = vhits;
1011     fTracks = vtracks;
1012     delete[] vptracks;
1013   }
1014   timerMerge3.Stop();
1015   fStatTime[12]+=timerMerge3.CpuTime();
1016 }
1017
1018 void AliHLTTPCCAGBTracker::GetErrors2( Int_t iSlice, Int_t iRow, AliHLTTPCCATrackParam &t, Float_t &Err2Y, Float_t &Err2Z )
1019 {
1020   //
1021   // Use calibrated cluster error from OCDB
1022   //
1023     
1024   Float_t z = CAMath::Abs((250.-0.275)-CAMath::Abs(t.GetZ()));
1025   Int_t    type = (iRow<63) ? 0: (iRow>126) ? 1:2;
1026   Float_t cosPhiInv = CAMath::Abs(t.GetCosPhi())>1.e-2 ?1./t.GetCosPhi() :0;
1027   Float_t angleY = t.GetSinPhi()*cosPhiInv ;
1028   Float_t angleZ = t.GetDzDs()*cosPhiInv ;
1029
1030   AliHLTTPCCATracker &slice = fSlices[iSlice];
1031
1032   Err2Y = slice.Param().GetClusterError2(0,type, z,angleY);  
1033   Err2Z = slice.Param().GetClusterError2(1,type, z,angleZ);
1034 }
1035
1036
1037 void AliHLTTPCCAGBTracker::GetErrors2( AliHLTTPCCAGBHit &h, AliHLTTPCCATrackParam &t, Float_t &Err2Y, Float_t &Err2Z )
1038 {
1039   //
1040   // Use calibrated cluster error from OCDB
1041   //
1042
1043   GetErrors2( h.ISlice(), h.IRow(), t, Err2Y, Err2Z );
1044 }
1045
1046 void AliHLTTPCCAGBTracker::WriteSettings( std::ostream &out ) const
1047 {
1048   //* write settings to the file
1049   out<< NSlices()<<std::endl;  
1050   for( Int_t iSlice=0; iSlice<NSlices(); iSlice++ ){    
1051     fSlices[iSlice].Param().WriteSettings( out );
1052   }
1053 }
1054
1055 void AliHLTTPCCAGBTracker::ReadSettings( std::istream &in )
1056 {
1057   //* Read settings from the file
1058   Int_t nSlices=0;
1059   in >> nSlices;
1060   SetNSlices( nSlices );
1061   for( Int_t iSlice=0; iSlice<NSlices(); iSlice++ ){    
1062     AliHLTTPCCAParam param;
1063     param.ReadSettings ( in );
1064     fSlices[iSlice].Initialize( param ); 
1065   }
1066 }
1067
1068 void AliHLTTPCCAGBTracker::WriteEvent( std::ostream &out ) const
1069 {
1070   // write event to the file
1071
1072   out<<NHits()<<std::endl;
1073   for (Int_t ih=0; ih<NHits(); ih++) {
1074     AliHLTTPCCAGBHit &h = fHits[ih];
1075     out<<h.X()<<" ";
1076     out<<h.Y()<<" ";
1077     out<<h.Z()<<" ";
1078     out<<h.ErrY()<<" ";
1079     out<<h.ErrZ()<<" ";
1080     out<<h.Amp()<<" ";
1081     out<<h.ID()<<" ";
1082     out<<h.ISlice()<<" ";
1083     out<<h.IRow()<<std::endl;
1084   }
1085 }
1086
1087 void AliHLTTPCCAGBTracker::ReadEvent( std::istream &in ) 
1088 {
1089   //* Read event from file 
1090
1091   StartEvent();
1092   Int_t nHits;
1093   in >> nHits;
1094   SetNHits(nHits);
1095   for (Int_t i=0; i<nHits; i++) {
1096     Float_t x, y, z, errY, errZ;
1097     Float_t amp;
1098     Int_t id, iSlice, iRow;
1099     in>>x>>y>>z>>errY>>errZ>>amp>>id>>iSlice>>iRow;
1100     ReadHit( x, y, z, errY, errZ, amp, id, iSlice, iRow );
1101   }
1102 }
1103
1104 void AliHLTTPCCAGBTracker::WriteTracks( std::ostream &out ) const 
1105 {
1106   //* Write tracks to file 
1107
1108   out<<fSliceTrackerTime<<std::endl;
1109   Int_t nTrackHits = 0;
1110   for( Int_t itr=0; itr<fNTracks; itr++ ){
1111     nTrackHits+=fTracks[itr].NHits();
1112   }
1113   out<<nTrackHits<<std::endl;
1114   for( Int_t ih=0; ih<nTrackHits; ih++ ){
1115     out<< fTrackHits[ih]<<" ";
1116   }
1117   out<<std::endl;
1118   
1119   out<<NTracks()<<std::endl;
1120   for( Int_t itr=0; itr<fNTracks; itr++ ){
1121     AliHLTTPCCAGBTrack &t = fTracks[itr];    
1122     AliHLTTPCCATrackParam &p = t.Param();       
1123     out<< t.NHits()<<" ";
1124     out<< t.FirstHitRef()<<" ";
1125     out<< t.Alpha()<<" ";
1126     out<< t.DeDx()<<std::endl;
1127     out<< p.X()<<" ";
1128     out<< p.CosPhi()<<" ";
1129     out<< p.Chi2()<<" ";
1130     out<< p.NDF()<<std::endl;
1131     for( Int_t i=0; i<5; i++ ) out<<p.Par()[i]<<" ";
1132     out<<std::endl;
1133     for( Int_t i=0; i<15; i++ ) out<<p.Cov()[i]<<" ";
1134     out<<std::endl;
1135   }
1136 }
1137
1138 void AliHLTTPCCAGBTracker::ReadTracks( std::istream &in )
1139 {
1140   //* Read tracks  from file 
1141
1142   in>>fTime;
1143   fSliceTrackerTime = fTime;
1144   fStatTime[0]+=fTime;
1145   fStatNEvents++;
1146   if( fTrackHits ) delete[] fTrackHits;
1147   fTrackHits = 0;  
1148   Int_t nTrackHits = 0;
1149   in >> nTrackHits;
1150   fTrackHits = new Int_t [nTrackHits];
1151   for( Int_t ih=0; ih<nTrackHits; ih++ ){
1152     in >> TrackHits()[ih];
1153   }
1154   if( fTracks ) delete[] fTracks;
1155   fTracks = 0;  
1156   in >> fNTracks;
1157   fTracks = new AliHLTTPCCAGBTrack[fNTracks];
1158   for( Int_t itr=0; itr<NTracks(); itr++ ){
1159     AliHLTTPCCAGBTrack &t = Tracks()[itr];    
1160     AliHLTTPCCATrackParam &p = t.Param();       
1161     in>> t.NHits();
1162     in>> t.FirstHitRef();
1163     in>> t.Alpha();
1164     in>> t.DeDx();
1165     in>> p.X();
1166     in>> p.CosPhi();
1167     in>> p.Chi2();
1168     in>> p.NDF();
1169     for( Int_t i=0; i<5; i++ ) in>>p.Par()[i];
1170     for( Int_t i=0; i<15; i++ ) in>>p.Cov()[i];
1171   }
1172 }