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"
35 #include "AliHLTTPCCAMath.h"
36 #include "AliHLTTPCCATrackLinearisation.h"
37 #include "AliHLTTPCCAClusterData.h"
38 #include "Stopwatch.h"
45 #include "AliHLTTPCCADisplay.h"
46 #include "AliHLTTPCCAPerformance.h"
47 #include "AliHLTTPCCASliceOutput.h"
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>
59 bool SINGLE_THREADED =
false;
61 AliHLTTPCCAGBTracker::AliHLTTPCCAGBTracker()
74 fSliceTrackerTime( 0 ),
75 fSliceTrackerCpuTime( 0 )
78 for (
int i = 0; i < 20; i++ ) fStatTime[i] = 0;
82 void AliHLTTPCCAGBTracker::Init()
91 fSliceTrackerTime = 0.;
92 fSliceTrackerCpuTime = 0.;
93 for (
int i = 0; i < 20; ++i ) {
98 AliHLTTPCCAGBTracker::~AliHLTTPCCAGBTracker()
102 if (fMerger)
delete fMerger;
105 void AliHLTTPCCAGBTracker::SetNSlices(
int N )
113 void AliHLTTPCCAGBTracker::StartEvent()
117 if (fTrackHits)
delete[] fTrackHits;
119 if (fTracks)
delete[] fTracks;
121 if (fExt2IntHitID)
delete[] fExt2IntHitID;
125 for (
int i = 0; i < fNSlices; i++ ) fSlices[i].StartEvent();
129 void AliHLTTPCCAGBTracker::SetNHits(
int nHits )
134 if (fExt2IntHitID)
delete[] fExt2IntHitID;
135 fExt2IntHitID =
new int[ nHits ];
145 : sliceNHits( _sliceNHits ), rowNHits( _rowNHits ) {}
147 inline void operator()(
const tbb::blocked_range<int> &r )
const {
148 for (
int i = r.begin(); i < r.end(); ++i ) {
150 for (
int ir = 0; ir < 200; ++ir ) {
151 rowNHits( i, ir ) = 0;
157 class ReconstructSliceTracks
161 tbb::spin_mutex &fMutex;
164 : fSlices( fSlices_ ), fStatTime( fStatTime_ ), fMutex( fMutex_ ) {}
166 inline void operator()(
const tbb::blocked_range<int> &r )
const {
167 for (
int iSlice = r.begin(); iSlice < r.end(); ++iSlice ) {
176 tbb::spin_mutex::scoped_lock lock( fMutex );
180 fStatTime[0] += timer.RealTime();
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 );
203 AliHLTTPCCAPerformance::Instance().SetTracker(
this );
204 AliHLTTPCCADisplay::Instance().Init();
205 AliHLTTPCCADisplay::Instance().SetGB(
this );
206 AliHLTTPCCADisplay::Instance().SetTPC( fSlices[0].Param() );
222 if ( fNHits <= 0 )
return;
226 #define NUM_THREADS SINGLE_THREADED ? 1 : tbb::task_scheduler_init::automatic
228 tbb::task_scheduler_init *taskScheduler =
new tbb::task_scheduler_init( NUM_THREADS );
250 for (
int i = 0; i < 20; ++i ) {
262 int numberOfUsedSlices = 0;
264 fClusterData.
Resize(fNSlices);
266 fFirstSliceHit[nextSlice] = offset;
268 const int iSlice = fHits.
Data()[offset].ISlice();
269 data.readEvent( fHits.
Data(), &offset, fNHits, fSlices[iSlice].Param().NRows8() );
271 while ( nextSlice < iSlice ) {
272 fSlices[nextSlice++].StartEvent();
273 fFirstSliceHit[nextSlice] = fFirstSliceHit[nextSlice - 1];
278 fSlices[data.
Slice()].ReadEvent( &data );
279 }
while ( offset < fNHits );
280 while ( nextSlice < fNSlices ) {
281 fFirstSliceHit[nextSlice] = offset;
282 fSlices[nextSlice++].StartEvent();
288 fStatTime[12] = timer1.RealTime();
293 tbb::spin_mutex mutex;
300 tbb::parallel_for( tbb::blocked_range<int>( 0, fNSlices, 1 ),
301 ReconstructSliceTracks( fSlices, fStatTime, mutex ) );
303 for (
int iSlice = 0; iSlice < fSlices.
Size(); ++iSlice ) {
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 );
321 fSliceTrackerTime = timer2.RealTime();
322 fSliceTrackerCpuTime = timer2.CpuTime();
328 fStatTime[9] += timerMerge.RealTime();
329 fStatTime[10] += timerMerge.CpuTime();
333 fTime += timer1.RealTime();
338 for (
int i = 0; i < fNTracks; i++ ) {
343 assert ( t.NHits() >= 3 );
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() );
354 if ( AliHLTTPCCADisplay::Instance().DrawType() == 1 )
355 AliHLTTPCCADisplay::Instance().Ask();
358 if (taskScheduler)
delete taskScheduler;
412 merger.SetSliceParam( fSlices[0].Param() );
414 for (
int i = 0; i < fNSlices; i++ ) {
415 merger.SetSliceData( i, fSlices[i].Output() );
416 merger.SetSlices(i, &fSlices[i]);
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);
443 if ( fTrackHits )
delete[] fTrackHits;
445 if ( fTracks )
delete[] fTracks;
447 fTrackHits =
new int [out.NTrackClusters()];
453 for (
int itr = 0; itr < out.NTracks(); itr++ ) {
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 );
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();
471 fTrackHits[nTrackHits + icl] = fFirstSliceHit[iSlice] + fSlices[iSlice].ClusterData().RowOffset( iRow ) + iClu;
475 nTrackHits += track.NClusters();
564 float &Alpha,
int hits[],
int &NTrackHits,
571 float alpha0 = Alpha;
579 t.CalculateFitParameters( fitPar );
584 for (
int ihit = 0; ihit < NTrackHits; ihit++ ) {
586 int jhit = dir ? ( NTrackHits - 1 - ihit ) : ihit;
589 int iSlice = h.ISlice();
591 float sliceAlpha = fSlices[0].Param().Alpha( iSlice );
593 if ( CAMath::Abs( sliceAlpha - alpha0 ) > 1.e-4 ) {
594 if ( ! t.Rotate( sliceAlpha - alpha0, l, .999 ) )
continue;
601 if ( !t.TransportToXWithMaterial( x, l, fitPar, fSlices[0].Param().GetBz( t ) ) )
continue;
621 t.CalculateFitParameters( fitPar );
628 fSlices[0].Param().GetClusterErrors2( h.IRow(), t0, err2Y, err2Z );
630 if ( !t.Filter( h.Y(), h.Z(), err2Y, err2Z ) )
continue;
634 hitsNew[nHitsNew++] = hits[jhit];
637 if ( CAMath::Abs( t.QPt() ) < 1.e-8 ) t.SetQPt( 1.e-8 );
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 );
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;
649 if ( CAMath::Abs( t.SinPhi() ) > .99 ) ok = 0;
650 else if ( l.CosPhi() >= 0 ) t.SetSignCosPhi( 1 );
651 else t.SetSignCosPhi( -1 );
656 NTrackHits = nHitsNew;
657 for (
int i = 0; i < NTrackHits; i++ ) {
658 hits[dir ?( NTrackHits-1-i ) :i] = hitsNew[i];
666 void AliHLTTPCCAGBTracker::WriteSettings( std::ostream &out )
const
669 out << NSlices() << std::endl;
670 for (
int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
671 out << fSlices[iSlice].Param();
675 void AliHLTTPCCAGBTracker::ReadSettings( std::istream &in )
680 SetNSlices( nSlices );
681 for (
int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
691 fSlices[iSlice].Initialize( param );
695 void AliHLTTPCCAGBTracker::WriteEvent( FILE *file )
const
699 const int nHits = NHits();
700 int written = std::fwrite( &nHits,
sizeof(
int ), 1, file );
701 assert( written == 1 );
703 assert( written == nHits );
705 UNUSED_PARAM1(written);
708 void AliHLTTPCCAGBTracker::ReadEvent( FILE *file )
714 int read = std::fread( &nHits,
sizeof(
int ), 1, file );
718 assert( read == nHits );
738 void AliHLTTPCCAGBTracker::WriteTracks(
const string& prefix )
const
741 ofstream out((prefix+
"tracks.data").
data());
754 out << fNHits << std::endl;
755 out << fNTracks << std::endl;
756 for (
int itr = 0; itr < fNTracks; itr++ ) {
758 out << t.NHits() << std::endl;
759 for (
int ih = t.FirstHitRef(); ih < t.FirstHitRef() + t.NHits(); ih++ ) {
760 out << fTrackHits[ih] <<
" ";
763 out << t.Alpha() <<
" ";
764 out << t.NDeDx() <<
" ";
765 out << t.DeDx() << std::endl;
766 out << t.InnerParam() << endl << t.OuterParam();
770 void AliHLTTPCCAGBTracker::ReadTracks( std::istream &in )
775 fSliceTrackerTime = fTime;
776 fStatTime[0] += fTime;
778 if (fTrackHits)
delete[] fTrackHits;
782 fTrackHits =
new int [nTrackHits];
783 for (
int ih = 0; ih < nTrackHits; ih++ ) {
784 in >> TrackHits()[ih];
786 if (fTracks)
delete[] fTracks;
790 for (
int itr = 0; itr < fNTracks; itr++ ) {
796 #include "BinaryStoreHelper.h"
798 void AliHLTTPCCAGBTracker::StoreToFile(
const char *filename )
const
800 FILE *f = std::fopen( filename,
"wb" );
801 BinaryStoreWrite( fNSlices, f );
802 for (
int i = 0; i < fNSlices; ++i ) {
803 fSlices[i].StoreToFile( f );
806 BinaryStoreWrite( fNHits, f );
807 BinaryStoreWrite( fHits.
Data(), fNHits, f );
808 BinaryStoreWrite( fTrackHits, fNHits * 10, f );
810 BinaryStoreWrite( fNTracks, f );
811 BinaryStoreWrite( fTracks, fNTracks, f );
813 BinaryStoreWrite( fTime, f );
814 BinaryStoreWrite( fStatTime, 20, f );
815 BinaryStoreWrite( fStatNEvents, f );
816 BinaryStoreWrite( fFirstSliceHit, 100, f );
818 BinaryStoreWrite( fSliceTrackerTime, f );
824 void AliHLTTPCCAGBTracker::RestoreFromFile( FILE *f )
826 BinaryStoreRead( fNSlices, f );
827 fSlices.
Resize( fNSlices );
828 for (
int i = 0; i < fNSlices; ++i ) {
829 fSlices[i].RestoreFromFile( f );
832 BinaryStoreRead( fNHits, f );
834 BinaryStoreRead( fHits.
Data(), fNHits, f );
835 fTrackHits =
new int[fNHits * 10];
836 BinaryStoreRead( fTrackHits, fNHits * 10, f );
838 BinaryStoreRead( fNTracks, f );
840 BinaryStoreRead( fTracks, fNTracks, f );
842 BinaryStoreRead( fTime, f );
843 BinaryStoreRead( fStatTime, 20, f );
844 BinaryStoreRead( fStatNEvents, f );
845 BinaryStoreRead( fFirstSliceHit, 100, f );
847 BinaryStoreRead( fSliceTrackerTime, f );
959 const int NHits2 = hits.size();
964 for (
int iH = 0; iH < NHits2; iH++){
965 fHits[iH] = hits[iH];
971 const int NHits2 = nHits;
976 for (
int iH = 0; iH < NHits2; iH++){
977 fHits[iH] = hits[iH];
981 void AliHLTTPCCAGBTracker::SetSettings(
const std::vector<AliHLTTPCCAParam>& settings )
983 SetNSlices( settings.size() );
984 for (
int iSlice = 0; iSlice < NSlices(); iSlice++ ) {
985 fSlices[iSlice].Initialize( settings[iSlice] );
989 void AliHLTTPCCAGBTracker::SaveHitsInFile(
string prefix)
const
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++){
1002 void AliHLTTPCCAGBTracker::SaveSettingsInFile(
string prefix)
const
1004 ofstream ofile((prefix+
"settings.data").
data(),std::ios::out|std::ios::app);
1005 WriteSettings(ofile);
1008 bool AliHLTTPCCAGBTracker::ReadHitsFromFile(
string prefix)
1010 ifstream ifile((prefix+
"hits.data").
data());
1011 if ( !ifile.is_open() )
return 0;
1016 for (
int i = 0; i < Size; i++){
1024 bool AliHLTTPCCAGBTracker::ReadSettingsFromFile(
string prefix)
1026 ifstream ifile((prefix+
"settings.data").
data());
1027 if ( !ifile.is_open() )
return 0;
1028 ReadSettings(ifile);
void SetHits(const std::vector< AliHLTTPCCAGBHit > &hits)
Try to group close hits in row formed by one track. After sort hits.
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)