StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AliHLTTPCCAGBTracker.cxx
1 // $Id: AliHLTTPCCAGBTracker.cxx,v 1.1 2016/02/05 23:27:27 fisyak Exp $
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 // Developed by: Igor Kulakov <I.Kulakov@gsi.de> *
11 // Maksym Zyzak <M.Zyzak@gsi.de> *
12 // *
13 // Permission to use, copy, modify and distribute this software and its *
14 // documentation strictly for non-commercial purposes is hereby granted *
15 // without fee, provided that the above copyright notice appears in all *
16 // copies and that both the copyright notice and this permission notice *
17 // appear in the supporting documentation. The authors make no claims *
18 // about the suitability of this software for any purpose. It is *
19 // provided "as is" without express or implied warranty. *
20 // *
21 //***************************************************************************
22 
23 
24 
25 #include "AliHLTTPCCAGBTracker.h"
26 #include "AliHLTTPCCAGBHit.h"
27 #include "AliHLTTPCCAOutTrack.h"
28 #include "AliHLTTPCCATracker.h"
29 #include "AliHLTTPCCAGBTrack.h"
30 #include "AliHLTTPCCATrackParam.h"
31 #include "AliHLTTPCCAMerger.h"
32 #include "AliHLTTPCCAMergerOutput.h"
33 #include "AliHLTTPCCADataCompressor.h"
34 #include "AliHLTArray.h"
35 #include "AliHLTTPCCAMath.h"
36 #include "AliHLTTPCCATrackLinearisation.h"
37 #include "AliHLTTPCCAClusterData.h"
38 #include "Stopwatch.h"
39 #include <algorithm>
40 #include <fstream>
41 #include <iostream>
42 using namespace std;
43 
44 #ifdef MAIN_DRAW
45 #include "AliHLTTPCCADisplay.h"
46 #include "AliHLTTPCCAPerformance.h"
47 #include "AliHLTTPCCASliceOutput.h"
48 #endif
49 
50 #ifdef USE_TBB
51 #include <tbb/blocked_range.h>
52 #include <tbb/parallel_for.h>
53 #include <tbb/parallel_sort.h>
54 #include <tbb/partitioner.h>
55 #include <tbb/spin_mutex.h>
56 #include <tbb/task_scheduler_init.h>
57 #endif //USE_TBB
58 using namespace std;
59 bool SINGLE_THREADED = false;
60 
61 AliHLTTPCCAGBTracker::AliHLTTPCCAGBTracker()
62  :
63  fNSlices( 0 ),
64  fHits( 0 ),
65  fExt2IntHitID( 0 ),
66  fNHits( 0 ),
67  fTrackHits( 0 ),
68  fTracks( 0 ),
69  fNTracks( 0 ),
70  fMerger( 0 ),
71  fClusterData( 0 ),
72  fTime( 0 ),
73  fStatNEvents( 0 ),
74  fSliceTrackerTime( 0 ),
75  fSliceTrackerCpuTime( 0 )
76 {
77  //* constructor
78  for ( int i = 0; i < 20; i++ ) fStatTime[i] = 0;
79  fMerger = new AliHLTTPCCAMerger;
80 }
81 
82 void AliHLTTPCCAGBTracker::Init()
83 {
84  fNSlices = 0;
85  fNHits = 0;
86  fTrackHits = 0;
87  fTracks = 0;
88  fNTracks = 0;
89  fTime = 0.;
90  fStatNEvents = 0;
91  fSliceTrackerTime = 0.;
92  fSliceTrackerCpuTime = 0.;
93  for ( int i = 0; i < 20; ++i ) {
94  fStatTime[i] = 0.;
95  }
96 }
97 
98 AliHLTTPCCAGBTracker::~AliHLTTPCCAGBTracker()
99 {
100  //* destructor
101  StartEvent();
102  if (fMerger) delete fMerger;
103 }
104 
105 void AliHLTTPCCAGBTracker::SetNSlices( int N )
106 {
107  //* set N of slices
108  StartEvent();
109  fNSlices = N;
110  fSlices.Resize( N );
111 }
112 
113 void AliHLTTPCCAGBTracker::StartEvent()
114 {
115  //* clean up track and hit arrays
116 
117  if (fTrackHits) delete[] fTrackHits;
118  fTrackHits = 0;
119  if (fTracks) delete[] fTracks;
120  fTracks = 0;
121  if (fExt2IntHitID) delete[] fExt2IntHitID;
122  fExt2IntHitID = 0;
123  fNHits = 0;
124  fNTracks = 0;
125  for ( int i = 0; i < fNSlices; i++ ) fSlices[i].StartEvent();
126 }
127 
128 
129 void AliHLTTPCCAGBTracker::SetNHits( int nHits )
130 {
131  //* set the number of hits
132  fHits.Resize( nHits );
133  fNHits = nHits;
134  if (fExt2IntHitID) delete[] fExt2IntHitID;
135  fExt2IntHitID = new int[ nHits ];
136 }
137 
138 #ifdef USE_TBB
139 class Initializer
140 {
141  AliHLTArray<int> &sliceNHits;
142  AliHLTArray<int, 2> &rowNHits;
143  public:
144  inline Initializer( AliHLTArray<int> &_sliceNHits, AliHLTArray<int, 2> &_rowNHits )
145  : sliceNHits( _sliceNHits ), rowNHits( _rowNHits ) {}
146 
147  inline void operator()( const tbb::blocked_range<int> &r ) const {
148  for ( int i = r.begin(); i < r.end(); ++i ) {
149  sliceNHits[i] = 0;
150  for ( int ir = 0; ir < 200; ++ir ) {
151  rowNHits( i, ir ) = 0;
152  }
153  }
154  }
155 };
156 
157 class ReconstructSliceTracks
158 {
160  double *fStatTime;
161  tbb::spin_mutex &fMutex;
162  public:
163  inline ReconstructSliceTracks( AliHLTArray<AliHLTTPCCATracker> &fSlices_, double *fStatTime_, tbb::spin_mutex &fMutex_ )
164  : fSlices( fSlices_ ), fStatTime( fStatTime_ ), fMutex( fMutex_ ) {}// 2.1. Data preparation is done as follows:
165 
166  inline void operator()( const tbb::blocked_range<int> &r ) const {
167  for ( int iSlice = r.begin(); iSlice < r.end(); ++iSlice ) {
168 #ifdef USE_TIMERS
169  Stopwatch timer;
170 #endif // USE_TIMERS
171  AliHLTTPCCATracker &slice = fSlices[iSlice];
172  slice.Reconstruct();
173 #ifdef USE_TIMERS
174  timer.Stop();
175 #endif // USE_TIMERS
176  tbb::spin_mutex::scoped_lock lock( fMutex );
177  //fTime+= timer.RealTime();
178  //blaTime+= timer.RealTime();
179 #ifdef USE_TIMERS
180  fStatTime[0] += timer.RealTime();
181 #endif // USE_TIMERS
182  fStatTime[1] += slice.Timer( 0 );
183  fStatTime[2] += slice.Timer( 1 );
184  fStatTime[3] += slice.Timer( 2 );
185  fStatTime[4] += slice.Timer( 3 );
186  fStatTime[5] += slice.Timer( 4 );
187  fStatTime[6] += slice.Timer( 5 );
188  fStatTime[7] += slice.Timer( 6 );
189  fStatTime[8] += slice.Timer( 7 );
190  fStatTime[11] += slice.Timer( 10 );
191  }
192  }
193 };
194 #endif //USE_TBB
195 
197 {
198  //* main tracking routine
199  fTime = 0;
200  fStatNEvents++;
201 
202 #ifdef MAIN_DRAW
203  AliHLTTPCCAPerformance::Instance().SetTracker( this );
204  AliHLTTPCCADisplay::Instance().Init();
205  AliHLTTPCCADisplay::Instance().SetGB( this );
206  AliHLTTPCCADisplay::Instance().SetTPC( fSlices[0].Param() );
207 #endif //MAIN_DRAW
208 
209 #ifdef MAIN_DRAW
210 
211 // AliHLTTPCCADisplay::Instance().SetTPCView();
212 // /** AliHLTTPCCADisplay::Instance().DrawTPC();
213 // AliHLTTPCCADisplay::Instance().DrawGBHits( *this, -1, 0.2, 1 );
214 // AliHLTTPCCADisplay::Instance().SaveCanvasToFile( "Hits.pdf");
215 // AliHLTTPCCADisplay::Instance().Ask();
216 // */
217 // // AliHLTTPCCADisplay::Instance().DrawGBHits( *this, kRed, 0.2, 2 );
218 // // AliHLTTPCCADisplay::Instance().SaveCanvasToFile( "Hits_b.pdf");
219 // // AliHLTTPCCADisplay::Instance().Ask();
220 #endif //MAIN_DRAW
221 
222  if ( fNHits <= 0 ) return; // TODO rid of it. Can be problems with performance
223 
224 #ifdef USE_TBB
225 #ifndef NUM_THREADS
226 #define NUM_THREADS SINGLE_THREADED ? 1 : tbb::task_scheduler_init::automatic
227 #endif
228  tbb::task_scheduler_init *taskScheduler = new tbb::task_scheduler_init( NUM_THREADS );
229 #undef NUM_THREADS
230 #endif //USE_TBB
231 
232  Stopwatch timer1;
233  Stopwatch timer2;
234 
235 #ifdef USE_TBB
236  tbb::parallel_sort( fHits.Data(), fHits.Data() + fNHits, AliHLTTPCCAGBHit::Compare );
237 #else //USE_TBB
238  std::sort( fHits.Data(), fHits.Data() + fNHits, AliHLTTPCCAGBHit::Compare );
248 #endif //USE_TBB
249 
250  for ( int i = 0; i < 20; ++i ) {
251  fStatTime[i] = 0.;
252  }
253 
254 // GroupHits();
255 #ifdef USE_TIMERS
256  timer1.Start();
257 #endif
258 
259  {
260  int offset = 0;
261  int nextSlice = 0;
262  int numberOfUsedSlices = 0;
263 
264  fClusterData.Resize(fNSlices);
265  do {
266  fFirstSliceHit[nextSlice] = offset;
267  AliHLTTPCCAClusterData &data = fClusterData[numberOfUsedSlices++];
268  const int iSlice = fHits.Data()[offset].ISlice();
269  data.readEvent( fHits.Data(), &offset, fNHits, fSlices[iSlice].Param().NRows8() );
270 
271  while ( nextSlice < iSlice ) {
272  fSlices[nextSlice++].StartEvent();
273  fFirstSliceHit[nextSlice] = fFirstSliceHit[nextSlice - 1];
274 
275  }
276  ++nextSlice;
278  fSlices[data.Slice()].ReadEvent( &data );
279  } while ( offset < fNHits );
280  while ( nextSlice < fNSlices ) {
281  fFirstSliceHit[nextSlice] = offset;
282  fSlices[nextSlice++].StartEvent();
283  }
284  }
285 
286 #ifdef USE_TIMERS
287  timer1.Stop();
288  fStatTime[12] = timer1.RealTime();
289 #endif
290 
292 #ifdef USE_TBB
293  tbb::spin_mutex mutex;
294 #endif //USE_TBB
295 
298  timer2.Start();
299 #ifdef USE_TBB
300  tbb::parallel_for( tbb::blocked_range<int>( 0, fNSlices, 1 ),
301  ReconstructSliceTracks( fSlices, fStatTime, mutex ) );
302 #else //USE_TBB
303  for ( int iSlice = 0; iSlice < fSlices.Size(); ++iSlice ) {
304  Stopwatch timer;
305  AliHLTTPCCATracker &slice = fSlices[iSlice];
306  slice.Reconstruct();
307  timer.Stop();
308  fStatTime[0] += timer.RealTime();
309  fStatTime[1] += slice.Timer( 0 );
310  fStatTime[2] += slice.Timer( 1 );
311  fStatTime[3] += slice.Timer( 2 );
312  fStatTime[4] += slice.Timer( 3 );
313  fStatTime[5] += slice.Timer( 4 );
314  fStatTime[6] += slice.Timer( 5 );
315  fStatTime[7] += slice.Timer( 6 );
316  fStatTime[8] += slice.Timer( 7 );
317  fStatTime[11] += slice.Timer( 10 );
318  }
319 #endif //USE_TBB
320  timer2.Stop();
321  fSliceTrackerTime = timer2.RealTime();
322  fSliceTrackerCpuTime = timer2.CpuTime();
323 
324  Stopwatch timerMerge;
325  Merge();
326  timerMerge.Stop();
327  timer1.Stop();
328  fStatTime[9] += timerMerge.RealTime();
329  fStatTime[10] += timerMerge.CpuTime();
330  //fTime+=timerMerge.RealTime();
331  //std::cout<<"Merge time = "<<timerMerge.RealTime()*1.e3<<"ms"<<std::endl;
332  //std::cout<<"End CA merging"<<std::endl;
333  fTime += timer1.RealTime();
334 #if 0
335 #ifndef NDEBUG
336  {
337  int iHit = 0;
338  for ( int i = 0; i < fNTracks; i++ ) {
339  const AliHLTTPCCAGBTrack &t = fTracks[i];
340 
341  const AliHLTTPCCAGBHit &hF = fHits[fTrackHits[iHit]];
342  const AliHLTTPCCAGBHit &hL = fHits[fTrackHits[iHit + t.NHits() - 1]];
343  assert ( t.NHits() >= 3 );
344 
345  ASSERT( CAMath::Abs( t.InnerParam().X() - hF.X() ) < .01, "Track " << i << ": " << t.InnerParam().X() << " == " << hF.X() );
346  ASSERT( CAMath::Abs( t.OuterParam().X() - hL.X() ) < .01, "Track " << i << ": " << t.OuterParam().X() << " == " << hL.X() );
347 
348  iHit += t.NHits();
349  }
350  }
351 #endif
352 #endif
353 #ifdef MAIN_DRAW
354  if ( AliHLTTPCCADisplay::Instance().DrawType() == 1 )
355  AliHLTTPCCADisplay::Instance().Ask();
356 #endif // MAIN_DRAW
357 #ifdef USE_TBB
358  if (taskScheduler) delete taskScheduler;
359 #endif //USE_TBB
360 }
361 
363 {
364  // test
365 
366 // #ifdef MAIN_DRAW
367 // AliHLTTPCCADisplay::Instance().SetTPCView();
368 // AliHLTTPCCADisplay::Instance().DrawTPC();
369 // AliHLTTPCCADisplay::Instance().DrawGBHits( *this, -1, 0.4, 1 );
370 // std::cout << "Slice tracks:" << std::endl;
371 // for ( int iSlice = 0; iSlice < fNSlices; iSlice++ ) {
372 // AliHLTTPCCATracker &slice = fSlices[iSlice];
373 // AliHLTTPCCADisplay::Instance().SetCurrentSlice( &slice );
374 // for ( int itr = 0; itr < slice.Output()->NTracks(); itr++ ) {
375 // AliHLTTPCCADisplay::Instance().DrawSliceOutTrack( itr, 2, 1 );
376 // }
377 // }
378 // AliHLTTPCCADisplay::Instance().SaveCanvasToFile( "SectorTracks.pdf" );
379 // AliHLTTPCCADisplay::Instance().Ask();
380 
381 // /* AliHLTTPCCADisplay::Instance().ClearView();
382 // AliHLTTPCCADisplay::Instance().SetTPCView();
383 // AliHLTTPCCADisplay::Instance().DrawTPC();
384 // AliHLTTPCCADisplay::Instance().DrawGBHits( *this, -1, 0.4, 1 );
385 // for ( int icl = 0; icl < NHits(); icl++ )
386 // {
387 // if(Hits()[icl].ISlice() != 6) continue;
388 // int id =Hits()[icl].ID();
389 // int iRow = Hits()[icl].IRow();
390 // int islice = Hits()[icl].ISlice();
391 // const SliceData &data = fSlices[islice].Data();
392 // const AliHLTTPCCARow &row = data.Row( iRow );
393 
394 // // int color =
395 // AliHLTTPCCADisplay::Instance().DrawGBPoint(*this,Hits()[icl].ISlice(),
396 // Hits()[icl].X(),
397 // Hits()[icl].Y(),
398 // Hits()[icl].Z(),1,1);
399 // int icl1 = fFirstSliceHit[islice] - data.ClusterDataIndex( row, id );
400 // std::cout <<"N clustera " <<icl <<" IRow "<< iRow <<" Poschitannyj "<<icl1;
401 // std::cout << std::endl;
402 // AliHLTTPCCADisplay::Instance().Ask();
403 // }
404 
405 // */
406 // #endif //MAIN_DRAW
407 
408 
409  AliHLTTPCCAMerger &merger = *fMerger;
410 
411  merger.Clear();
412  merger.SetSliceParam( fSlices[0].Param() );
413 
414  for ( int i = 0; i < fNSlices; i++ ) {
415  merger.SetSliceData( i, fSlices[i].Output() );
416  merger.SetSlices(i, &fSlices[i]);
417  }
418 
420 
428  merger.Reconstruct();
429  assert( fNTimers >= merger.NTimers()+13-1 );
430  for (int i = 0; i < merger.NTimers(); i++) {
431  fStatTime[13+i] = merger.Timer(i);
432  }
433 
439 
441  const AliHLTTPCCAMergerOutput &out = *( merger.Output() );
442 
443  if ( fTrackHits ) delete[] fTrackHits;
444  fTrackHits = 0;
445  if ( fTracks ) delete[] fTracks;
446  fTracks = 0;
447  fTrackHits = new int [out.NTrackClusters()];
448  fTracks = new AliHLTTPCCAGBTrack[out.NTracks()];
449  fNTracks = 0;
450 
451  int nTrackHits = 0;
452 
453  for ( int itr = 0; itr < out.NTracks(); itr++ ) {
454  const AliHLTTPCCAMergedTrack &track = out.Track( itr );
455 
456  AliHLTTPCCAGBTrack &trackGB = fTracks[fNTracks];
457  trackGB.SetFirstHitRef( nTrackHits );
458  trackGB.SetNHits( track.NClusters() );
459  trackGB.SetInnerParam( track.InnerParam() );
460  trackGB.SetOuterParam( track.OuterParam() );
461  trackGB.SetAlpha( track.InnerAlpha() );
462  trackGB.SetDeDx( 0 );
463 
464  for ( int icl = 0; icl < track.NClusters(); icl++ ) {
465  const DataCompressor::SliceRowCluster &iDsrc = out.ClusterIDsrc( track.FirstClusterRef() + icl );
466  unsigned int iSlice = iDsrc.Slice();
467  unsigned int iRow = iDsrc.Row();
468  unsigned int iClu = iDsrc.Cluster();
469  //const SliceData &data = fSlices[iSlice].Data();
470  //const AliHLTTPCCARow &row = data.Row( iRow );
471  fTrackHits[nTrackHits + icl] = fFirstSliceHit[iSlice] + fSlices[iSlice].ClusterData().RowOffset( iRow ) + iClu;/*data.ClusterDataIndex( row, iClu );*/
472  }
473 
474 // if(itr==1 || itr == 2) std::cout << std::endl;
475  nTrackHits += track.NClusters();
476  fNTracks++;
477  }
478 /*
479 #ifdef MAIN_DRAW
481  for ( int iSlice = 0; iSlice < fNSlices; iSlice++ ) {
482  AliHLTTPCCATracker &slice = fSlices[iSlice];
483  for(int i=0; i<slice.NOutTracks1(); i++)
484  {
485  int slice_tr_id = slice.OutTrack1(i).OrigTrackID();
486  slice.fOutTracks1[i].SetFirstHitRef(-1);
487  slice.fOutTracks1[i].SetNHits(-1);
488  for(int j=0; j<slice.NOutTracks(); j++)
489  {
490  if(slice_tr_id == slice.OutTrack(j).OrigTrackID())
491  {
492  slice.fOutTracks1[i].SetFirstHitRef(slice.OutTrack(j).FirstHitRef());
493  slice.fOutTracks1[i].SetNHits(slice.OutTrack(j).NHits());
494  }
495  }
496  }
497 }
499  AliHLTTPCCADisplay::Instance().SetTPCView();
500  AliHLTTPCCADisplay::Instance().DrawTPC();
501  AliHLTTPCCADisplay::Instance().DrawGBHits( *this, -1, 0.4, 1 );
502  int col = 2;
503  for ( int iSlice = 0; iSlice < fNSlices; iSlice++ ) {
504  AliHLTTPCCATracker &slice = fSlices[iSlice];
505  AliHLTTPCCADisplay::Instance().SetCurrentSlice( &slice );
506  for ( int itr = 0; itr < slice.NOutTracks1(); itr++ ) {
507  AliHLTTPCCADisplay::Instance().DrawSliceOutTrack1( itr, 4, 2 );
508  }
509  }
510 
511  for ( int iSlice = 0; iSlice < fNSlices; iSlice++ ) {
512  AliHLTTPCCATracker &slice = fSlices[iSlice];
513  AliHLTTPCCADisplay::Instance().SetCurrentSlice( &slice );
514  for ( int itr = 0; itr < slice.NOutTracks1(); itr++ ) {
515  if(col>9) col = 2;
516  if(col==4) col=5;
517  std::cout << iSlice << " ";
518  AliHLTTPCCADisplay::Instance().DrawSliceOutTrackParam( itr, col, 3 );
519  col++;
520  }
521  }
522  AliHLTTPCCADisplay::Instance().Ask();
523 #endif
524 */
525 // #ifdef MAIN_DRAW
526 // std::cout << "Global tracks: " << std::endl;
527 // /// AliHLTTPCCADisplay::Instance().ClearView();
528 // AliHLTTPCCADisplay::Instance().SetTPCView();
529 // AliHLTTPCCADisplay::Instance().DrawTPC();
530 // AliHLTTPCCADisplay::Instance().DrawGBHits( *this );
531 //
532 // for ( int itr = 0; itr < fNTracks; itr++ ) {
533 // /* AliHLTTPCCADisplay::Instance().ClearView();
534 // AliHLTTPCCADisplay::Instance().SetTPCView();
535 // AliHLTTPCCADisplay::Instance().DrawTPC();
536 // AliHLTTPCCADisplay::Instance().DrawGBHits( *this );
537 // AliHLTTPCCAGBTrack &trackGB = fTracks[itr];
538 //
539 // for ( int icl = 0; icl < trackGB.NHits(); icl++ ) {
540 // AliHLTTPCCADisplay::Instance().DrawGBPoint(*this,Hits()[fTrackHits[trackGB.FirstHitRef() + icl]].ISlice(),
541 // Hits()[fTrackHits[trackGB.FirstHitRef() + icl]].X(),
542 // Hits()[fTrackHits[trackGB.FirstHitRef() + icl]].Y(),
543 // Hits()[fTrackHits[trackGB.FirstHitRef() + icl]].Z());
544 // if(itr==1 || itr==2)
545 // std::cout <<"x "<< Hits()[fTrackHits[trackGB.FirstHitRef() + icl]].X() <<" y "<<Hits()[fTrackHits[trackGB.FirstHitRef() + icl]].Y()<<" ";
546 // std::cout<<(Hits()[fTrackHits[trackGB.FirstHitRef()]]).ISlice()<<" "<<
547 // (Hits()[fTrackHits[trackGB.FirstHitRef()]]).IRow()<<" "<<
548 // (Hits()[fTrackHits[trackGB.FirstHitRef()]]).ID()<<std::endl;
549 // }
550 //
551 // AliHLTTPCCADisplay::Instance().Ask();*/
552 // AliHLTTPCCADisplay::Instance().DrawGBTrack( itr, kBlue, 2. );
553 // // AliHLTTPCCADisplay::Instance().Ask();
554 // //if(itr==1 || itr==2)std::cout <<std::endl;
555 // }
556 // AliHLTTPCCADisplay::Instance().SaveCanvasToFile( "GlobalTracks.pdf" );
557 // AliHLTTPCCADisplay::Instance().Ask();
558 // #endif // MAIN_DRAW
559 }
560 
561 
562 
564  float &Alpha, int hits[], int &NTrackHits,
565  bool dir )
566 {
567  // Fit the track
568 
569  //return fMerger->FitTrack( T, Alpha, t0, Alpha, hits, NTrackHits, dir );
570 
571  float alpha0 = Alpha;
572 
574  AliHLTTPCCATrackParam t = t0;
576 
577  bool first = 1;
578 
579  t.CalculateFitParameters( fitPar );
580 
581  int hitsNew[1000];
582  int nHitsNew = 0;
583 
584  for ( int ihit = 0; ihit < NTrackHits; ihit++ ) {
585 
586  int jhit = dir ? ( NTrackHits - 1 - ihit ) : ihit;
587  AliHLTTPCCAGBHit &h = fHits[hits[jhit]];
588 
589  int iSlice = h.ISlice();
590 
591  float sliceAlpha = fSlices[0].Param().Alpha( iSlice );
592 
593  if ( CAMath::Abs( sliceAlpha - alpha0 ) > 1.e-4 ) {
594  if ( ! t.Rotate( sliceAlpha - alpha0, l, .999 ) ) continue;
595  alpha0 = sliceAlpha;
596  }
597 
598  //float x = fSliceParam.RowX( h.IRow() );
599  float x = h.X();
600 
601  if ( !t.TransportToXWithMaterial( x, l, fitPar, fSlices[0].Param().GetBz( t ) ) ) continue;
602 
603  if ( first ) {
604  t.SetCov( 0, 10 );
605  t.SetCov( 1, 0 );
606  t.SetCov( 2, 10 );
607  t.SetCov( 3, 0 );
608  t.SetCov( 4, 0 );
609  t.SetCov( 5, 1 );
610  t.SetCov( 6, 0 );
611  t.SetCov( 7, 0 );
612  t.SetCov( 8, 0 );
613  t.SetCov( 9, 1 );
614  t.SetCov( 10, 0 );
615  t.SetCov( 11, 0 );
616  t.SetCov( 12, 0 );
617  t.SetCov( 13, 0 );
618  t.SetCov( 14, 10 );
619  t.SetChi2( 0 );
620  t.SetNDF( -5 );
621  t.CalculateFitParameters( fitPar );
622  }
623 
624 
625  float err2Y, err2Z;
627  //fSlices[0].Param().GetClusterErrors2( h.IRow(), h.Z(), l.SinPhi(), l.CosPhi(), l.DzDs(), err2Y, err2Z );
628  fSlices[0].Param().GetClusterErrors2( h.IRow(), t0, err2Y, err2Z );
630  if ( !t.Filter( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
631 
632  first = 0;
633 
634  hitsNew[nHitsNew++] = hits[jhit];
635  }
636 
637  if ( CAMath::Abs( t.QPt() ) < 1.e-8 ) t.SetQPt( 1.e-8 );
638 
639  bool ok = 1;
640 
641  const float *c = t.Cov();
642  for ( int i = 0; i < 15; i++ ) ok = ok && CAMath::Finite( c[i] );
643  for ( int i = 0; i < 5; i++ ) ok = ok && CAMath::Finite( t.Par()[i] );
644  ok = ok && ( t.GetX() > 50 );
645 
646  if ( c[0] <= 0 || c[2] <= 0 || c[5] <= 0 || c[9] <= 0 || c[14] <= 0 ) ok = 0;
647  if ( c[0] > 5. || c[2] > 5. || c[5] > 2. || c[9] > 2 || c[14] > 2. ) ok = 0;
648 
649  if ( CAMath::Abs( t.SinPhi() ) > .99 ) ok = 0;
650  else if ( l.CosPhi() >= 0 ) t.SetSignCosPhi( 1 );
651  else t.SetSignCosPhi( -1 );
652 
653  if ( ok ) {
654  T = t;
655  Alpha = alpha0;
656  NTrackHits = nHitsNew;
657  for ( int i = 0; i < NTrackHits; i++ ) {
658  hits[dir ?( NTrackHits-1-i ) :i] = hitsNew[i];
659  }
660  }
661  return ok;
662 }
663 
664 
665 
666 void AliHLTTPCCAGBTracker::WriteSettings( std::ostream &out ) const
667 {
668  //* write settings to the file
669  out << NSlices() << std::endl;
670  for ( int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
671  out << fSlices[iSlice].Param();
672  }
673 }
674 
675 void AliHLTTPCCAGBTracker::ReadSettings( std::istream &in )
676 {
677  //* Read settings from the file
678  int nSlices = 0;
679  in >> nSlices;
680  SetNSlices( nSlices );
681  for ( int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
682  AliHLTTPCCAParam param;
683  in >> param;
684 
685  // remove 13 row (iRow = 12) IKu
686 // param.SetNRows(param.NRows()-1);
687 // for (int iRow = 12; iRow < param.NRows(); iRow++){
688 // param.SetRowX(param.RowX(iRow+1)-1,iRow);
689 // }
690 
691  fSlices[iSlice].Initialize( param );
692  }
693 }
694 
695 void AliHLTTPCCAGBTracker::WriteEvent( FILE *file ) const
696 {
697  // write event to the file
698 
699  const int nHits = NHits();
700  int written = std::fwrite( &nHits, sizeof( int ), 1, file );
701  assert( written == 1 );
702  written = std::fwrite( fHits.Data(), sizeof( AliHLTTPCCAGBHit ), nHits, file );
703  assert( written == nHits );
704  std::fflush( file );
705  UNUSED_PARAM1(written);
706 }
707 
708 void AliHLTTPCCAGBTracker::ReadEvent( FILE *file )
709 {
710  //* Read event from file
711 
712  StartEvent();
713  int nHits;
714  int read = std::fread( &nHits, sizeof( int ), 1, file );
715  assert( read == 1 );
716  SetNHits( nHits );
717  read = std::fread( fHits.Data(), sizeof( AliHLTTPCCAGBHit ), nHits, file );
718  assert( read == nHits );
719  UNUSED_PARAM1(read);
720  // check
721 // for (int iHit = 0; iHit <= fHits.Size(); iHit++){
722 // std::cout << "iHit " << iHit << std::endl;
723 // std::cout << fHits.Data()[iHit].X() << " "
724 // << fHits.Data()[iHit].Y() << " "
725 // << fHits.Data()[iHit].Z() << std::endl;
726 // std::cout << fHits.Data()[iHit].ErrX() << " "
727 // << fHits.Data()[iHit].ErrY() << " "
728 // << fHits.Data()[iHit].ErrZ() << std::endl;
729 // }
730 
731  // remove 13 row (iRow = 12) IKu
732 // for (int iHit = 0; iHit <= fHits.Size(); iHit++){
733 // if (fHits.Data()[iHit].IRow() >= 13)
734 // fHits.Data()[iHit].SetIRow(fHits.Data()[iHit].IRow()-1);
735 // }
736 }
737 
738 void AliHLTTPCCAGBTracker::WriteTracks( const string& prefix ) const
739 {
740  //* Write tracks to file
741  ofstream out((prefix+"tracks.data").data());
742 
743  // out << fSliceTrackerTime << std::endl;
744  // int nTrackHits = 0;
745  // for ( int itr = 0; itr < fNTracks; itr++ ) {
746  // nTrackHits += fTracks[itr].NHits();
747  // }
748  // out << nTrackHits << std::endl;
749  // for ( int ih = 0; ih < nTrackHits; ih++ ) {
750  // out << fTrackHits[ih] << " ";
751  // }
752  // out << std::endl;
753 
754  out << fNHits << std::endl;
755  out << fNTracks << std::endl;
756  for ( int itr = 0; itr < fNTracks; itr++ ) {
757  AliHLTTPCCAGBTrack &t = fTracks[itr];
758  out << t.NHits() << std::endl;
759  for ( int ih = t.FirstHitRef(); ih < t.FirstHitRef() + t.NHits(); ih++ ) {
760  out << fTrackHits[ih] << " ";
761  }
762  out << std::endl;
763  out << t.Alpha() << " ";
764  out << t.NDeDx() << " ";
765  out << t.DeDx() << std::endl;
766  out << t.InnerParam() << endl << t.OuterParam();
767  }
768 }
769 
770 void AliHLTTPCCAGBTracker::ReadTracks( std::istream &in )
771 {
772  //* Read tracks from file
773 
774  in >> fTime;
775  fSliceTrackerTime = fTime;
776  fStatTime[0] += fTime;
777  fStatNEvents++;
778  if (fTrackHits) delete[] fTrackHits;
779  fTrackHits = 0;
780  int nTrackHits = 0;
781  in >> nTrackHits;
782  fTrackHits = new int [nTrackHits];
783  for ( int ih = 0; ih < nTrackHits; ih++ ) {
784  in >> TrackHits()[ih];
785  }
786  if (fTracks) delete[] fTracks;
787  fTracks = 0;
788  in >> fNTracks;
789  fTracks = new AliHLTTPCCAGBTrack[fNTracks];
790  for ( int itr = 0; itr < fNTracks; itr++ ) {
791  AliHLTTPCCAGBTrack &t = Tracks()[itr];
792  in >> t;
793  }
794 }
795 
796 #include "BinaryStoreHelper.h"
797 
798 void AliHLTTPCCAGBTracker::StoreToFile( const char *filename ) const
799 {
800  FILE *f = std::fopen( filename, "wb" );
801  BinaryStoreWrite( fNSlices, f );
802  for ( int i = 0; i < fNSlices; ++i ) {
803  fSlices[i].StoreToFile( f );
804  }
805 
806  BinaryStoreWrite( fNHits, f );
807  BinaryStoreWrite( fHits.Data(), fNHits, f );
808  BinaryStoreWrite( fTrackHits, fNHits * 10, f );
809 
810  BinaryStoreWrite( fNTracks, f );
811  BinaryStoreWrite( fTracks, fNTracks, f );
812 
813  BinaryStoreWrite( fTime, f );
814  BinaryStoreWrite( fStatTime, 20, f );
815  BinaryStoreWrite( fStatNEvents, f );
816  BinaryStoreWrite( fFirstSliceHit, 100, f );
817 
818  BinaryStoreWrite( fSliceTrackerTime, f );
819 
820  std::fflush( f );
821  std::fclose( f );
822 }
823 
824 void AliHLTTPCCAGBTracker::RestoreFromFile( FILE *f )
825 {
826  BinaryStoreRead( fNSlices, f );
827  fSlices.Resize( fNSlices );
828  for ( int i = 0; i < fNSlices; ++i ) {
829  fSlices[i].RestoreFromFile( f );
830  }
831 
832  BinaryStoreRead( fNHits, f );
833  fHits.Resize( fNHits );
834  BinaryStoreRead( fHits.Data(), fNHits, f );
835  fTrackHits = new int[fNHits * 10];
836  BinaryStoreRead( fTrackHits, fNHits * 10, f );
837 
838  BinaryStoreRead( fNTracks, f );
839  fTracks = new AliHLTTPCCAGBTrack[fNTracks];
840  BinaryStoreRead( fTracks, fNTracks, f );
841 
842  BinaryStoreRead( fTime, f );
843  BinaryStoreRead( fStatTime, 20, f );
844  BinaryStoreRead( fStatNEvents, f );
845  BinaryStoreRead( fFirstSliceHit, 100, f );
846 
847  BinaryStoreRead( fSliceTrackerTime, f );
848 }
849 
851 /*void AliHLTTPCCAGBTracker::GroupHits() // iklm
852 {
853  const float minD = AliHLTTPCCAParameters::MinHitsMergeDist;
854 
855  bool perf = 0;
856  AliHLTTPCCAPerformance *perfomance = &AliHLTTPCCAPerformance::Instance();
857 // std::cout << perfomance->GetHitLabels().Size() << std::endl;
858  if ( perfomance->GetHitLabels().Size() ) perf = 1;
859 
860  // copy arrays
861  int nHitsOld = fNHits;
862  AliHLTResizableArray<AliHLTTPCCAGBHit> hitsOld;
863  hitsOld.Resize( nHitsOld );
864  for (int i = 0; i < nHitsOld; i++){
865  hitsOld[i] = fHits[i];
866  }
867 
868  // create arrays for new data
869  AliHLTResizableArray<AliHLTTPCCAGBHit> hitsNew;
870  hitsNew.Resize( nHitsOld );
871 // std::cout << fHits.Size() << " " << hitLabbels.Size() << std::endl;
872  // select hits
873 
874  int iHitNew = -1; // index in new arrays
875 
876  int oldIHit = -1; // old* are info of prev hit - for compare to current
877  int oldSlice = -1;
878  int oldRow = -1;
879  float oldY = -10000;
880  float oldZ = -10000;
881  for (int iHit = 0; iHit < nHitsOld; iHit++){
882  int curSlice = hitsOld[iHit].ISlice();
883  int curRow = hitsOld[iHit].IRow();
884  float curY = hitsOld[iHit].Y();
885  float curZ = hitsOld[iHit].Z();
886  if ((curSlice == oldSlice) && (curRow == oldRow)){
887 
888  float curDY = fabs(curY-oldY);
889  float curDZ = fabs(curZ-oldZ);
890  if ((curDY < minD) && (curDZ < minD)){ // TODO: minD should depend of errors
891  // merge hits // TODO: write normal merge
892  float oldErrY = hitsOld[oldIHit].ErrY();
893  float oldErrZ = hitsOld[oldIHit].ErrZ();
894  float curErrY = hitsOld[oldIHit].ErrY();
895  float curErrZ = hitsOld[oldIHit].ErrZ();
896 
897  float wOldY = 1/curErrY; // !? weights can be different
898  float wOldZ = 1/curErrZ;
899  float wCurY = 1/oldErrY;
900  float wCurZ = 1/oldErrZ;
901  float wYNorm = 1/(wOldY + wCurY);
902  float wZNorm = 1/(wOldZ + wCurZ);
903  wOldY *= wYNorm;
904  wOldZ *= wZNorm;
905  wCurY *= wYNorm;
906  wCurZ *= wZNorm;
907  // delete hit
908 
909 // std::cout << oldY << " 1 " << oldZ << std::endl;
910 // std::cout << curY << " 1 " << curZ << std::endl;
911  oldY = curY*wCurY+oldY*wOldY; // FIXME: if we just delete one hit it get better result.
912  oldZ = curZ*wCurZ+oldZ*wOldZ;
913 // std::cout << oldY << " 2 " << oldZ << std::endl;
914  hitsNew[iHitNew].SetY( oldY );
915  hitsNew[iHitNew].SetZ( oldZ );
916 // hitsNew[iHitNew].SetErrY( wYNorm ); // !?
917 // hitsNew[iHitNew].SetErrZ( wZNorm );
918  hitsNew[iHitNew].SetErrY( (curDY < wYNorm) ? wYNorm : curDY ); // !?
919  hitsNew[iHitNew].SetErrZ( (curDZ < wZNorm) ? wZNorm : curDZ );
920 // hitsNew[oldIHit].SetErrY( curErrY*wCurY+oldErrY*wOldY ); // !?
921 // hitsNew[oldIHit].SetErrZ( curErrZ*wCurZ+oldErrZ*wOldZ );
922  // merge labels
923  if (perf){
924  AliHLTResizableArray<AliHLTTPCCAHitLabel>& hitLabels = AliHLTTPCCAPerformance::Instance().GetHitLabels();
925  int oldILabel = hitsOld[oldIHit].ID();
926  int curILabel = hitsOld[iHit].ID();
927  int i = 0;
928  for (; i < 3; i++) if (hitLabels[oldILabel].fLab[i] < 0) break;
929  for (int ii = i; ii < 3; ii++) hitLabels[oldILabel].fLab[ii] = hitLabels[curILabel].fLab[ii-i];
930  }
931  continue;
932  } // if hits are close
933 
934  } // if slice and row are same
935  // leave this hit
936  iHitNew++;
937  oldIHit = iHit;
938  oldRow = curRow;
939  oldSlice = curSlice;
940  oldY = curY;
941  oldZ = curZ;
942  hitsNew[iHitNew] = hitsOld[iHit];
943  }; // for iHit
944 
945  // save new hits array
946 
947  fHits.Resize( iHitNew );
948  for (int i = 0; i < iHitNew; i++){
949  fHits[i] = hitsNew[i];
950  };
951 
952  fNHits = iHitNew;
953  std::cout << "Merge clusters hits done: " << nHitsOld << " hits -> " << iHitNew << " hits." << std::endl;
954 } // EO: AliHLTTPCCAGBTracker::GroupHits()
955 */
956 
957 void AliHLTTPCCAGBTracker::SetHits( const std::vector<AliHLTTPCCAGBHit> &hits)
958 {
959  const int NHits2 = hits.size();
960 
961  SetNHits(NHits2);
962 
963  fHits.Resize(NHits2);
964  for (int iH = 0; iH < NHits2; iH++){
965  fHits[iH] = hits[iH];
966  }
967 } // need for StRoot
968 
969 void AliHLTTPCCAGBTracker::SetHits( const AliHLTTPCCAGBHit *hits, int nHits )
970 {
971  const int NHits2 = nHits;
972 
973  SetNHits(NHits2);
974 
975  fHits.Resize(NHits2);
976  for (int iH = 0; iH < NHits2; iH++){
977  fHits[iH] = hits[iH];
978  }
979 }
980 
981 void AliHLTTPCCAGBTracker::SetSettings( const std::vector<AliHLTTPCCAParam>& settings )
982 {
983  SetNSlices( settings.size() );
984  for ( int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
985  fSlices[iSlice].Initialize( settings[iSlice] );
986  }
987 }
988 
989 void AliHLTTPCCAGBTracker::SaveHitsInFile(string prefix) const
990 {
991  ofstream ofile((prefix+"hits.data").data(),std::ios::out|std::ios::app);
992  const int Size = fHits.Size();
993  ofile << Size << std::endl;
994  for (int i = 0; i < fHits.Size(); i++){
995  const AliHLTTPCCAGBHit &l = fHits[i];
996  ofile << l;
997  }
998  ofile.close();
999 
1000 }
1001 
1002 void AliHLTTPCCAGBTracker::SaveSettingsInFile(string prefix) const
1003 {
1004  ofstream ofile((prefix+"settings.data").data(),std::ios::out|std::ios::app);
1005  WriteSettings(ofile);
1006 }
1007 
1008 bool AliHLTTPCCAGBTracker::ReadHitsFromFile(string prefix)
1009 {
1010  ifstream ifile((prefix+"hits.data").data());
1011  if ( !ifile.is_open() ) return 0;
1012  int Size;
1013  ifile >> Size;
1014  fHits.Resize(Size);
1015  SetNHits(Size);
1016  for (int i = 0; i < Size; i++){
1017  AliHLTTPCCAGBHit &l = fHits[i];
1018  ifile >> l;
1019  }
1020  ifile.close();
1021  return 1;
1022 }
1023 
1024 bool AliHLTTPCCAGBTracker::ReadSettingsFromFile(string prefix)
1025 {
1026  ifstream ifile((prefix+"settings.data").data());
1027  if ( !ifile.is_open() ) return 0;
1028  ReadSettings(ifile);
1029  return 1;
1030 }
1031 
1032 
1033 
1034 
void Resize(int x)
Definition: AliHLTArray.h:718
void SetHits(const std::vector< AliHLTTPCCAGBHit > &hits)
Try to group close hits in row formed by one track. After sort hits.
int Size() const
Definition: AliHLTArray.h:381
static bool Compare(const AliHLTTPCCAGBHit &a, const AliHLTTPCCAGBHit &b)
Hits reordering in accordance with the geometry and the track-finder needs: Hits are sorted by sector...
bool FitTrack(AliHLTTPCCATrackParam &T, AliHLTTPCCATrackParam t0, float &Alpha, int hits[], int &NTrackHits, bool dir)