]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/TPCLib/tracking-ca/AliHLTTPCCATracker.h
restoring ostream properties after printout; fixing minor memory leak on error condition
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCATracker.h
CommitLineData
326c2d4b 1//-*- Mode: C++ -*-
2// @(#) $Id$
ce565086 3// ************************************************************************
fbb9b71b 4// This file is property of and copyright by the ALICE HLT Project *
ce565086 5// ALICE Experiment at CERN, All rights reserved. *
6// See cxx source for full Copyright notice *
7// *
8//*************************************************************************
326c2d4b 9
10#ifndef ALIHLTTPCCATRACKER_H
11#define ALIHLTTPCCATRACKER_H
12
13
00d07bcd 14#include "AliHLTTPCCADef.h"
b22af1bf 15#include "AliHLTTPCCAGPUConfig.h"
326c2d4b 16#include "AliHLTTPCCAParam.h"
4687b8fc 17#include <iostream>
4acc2401 18#include "AliHLTTPCCAHitId.h"
19#include "AliHLTTPCCASliceData.h"
b22af1bf 20#include "AliHLTTPCCASliceOutput.h"
21#include "AliHLTTPCCATrackletConstructor.h"
a59a784e 22#include "AliHLTTPCCATracklet.h"
326c2d4b 23
d54804bf 24class AliHLTTPCCATrack;
d54804bf 25class AliHLTTPCCATrackParam;
4acc2401 26class AliHLTTPCCAClusterData;
a59a784e 27class AliHLTTPCCARow;
326c2d4b 28
2fba026d 29#include "TStopwatch.h"
30
326c2d4b 31/**
32 * @class AliHLTTPCCATracker
fbb9b71b 33 *
34 * Slice tracker for ALICE HLT.
dc4788ec 35 * The class reconstructs tracks in one slice of TPC.
36 * The reconstruction algorithm is based on the Cellular Automaton method
37 *
fbb9b71b 38 * The CA tracker is designed stand-alone.
dc4788ec 39 * It is integrated to the HLT framework via AliHLTTPCCATrackerComponent interface.
40 * The class is under construction.
41 *
326c2d4b 42 */
b22af1bf 43
44class AliHLTTPCCAClusterData;
45
326c2d4b 46class AliHLTTPCCATracker
47{
fbb9b71b 48 public:
326c2d4b 49
a59a784e 50 AliHLTTPCCATracker()
f0bada7f 51 :
52#ifdef HLTCA_STANDALONE
53 fStageAtSync( NULL ),
54 fLinkTmpMemory( NULL ),
55#endif
56 fParam(),
a59a784e 57 fOutputControl(),
58 fClusterData( 0 ),
59 fData(),
60 fIsGPUTracker( false ),
61 fGPUDebugLevel( 0 ),
62 fGPUDebugOut( 0 ),
63 fRowStartHitCountOffset( NULL ),
64 fTrackletTmpStartHits( NULL ),
65 fGPUTrackletTemp( NULL ),
66 fRowBlockTracklets( NULL ),
67 fRowBlockPos( NULL ),
68 fBlockStartingTracklet( NULL ),
69 fGPUParametersConst(),
70 fCommonMem( 0 ),
71 fHitMemory( 0 ),
72 fHitMemorySize( 0 ),
73 fTrackletMemory( 0 ),
74 fTrackletMemorySize( 0 ),
75 fTrackMemory( 0 ),
76 fTrackMemorySize( 0 ),
77 fTrackletStartHits( 0 ),
78 fTracklets( 0 ),
79 fTrackletRowHits( NULL ),
80 fTracks( 0 ),
81 fTrackHits( 0 ),
82 fOutput( 0 )
83 {
84 // constructor
9c442d4d 85 for( int i=0; i<10; i++ ) fTimers[i] = 0;
86 for( int i=0; i<16; i++ ) fPerfTimers[i] = 0;
a59a784e 87 }
88 ~AliHLTTPCCATracker();
89
90 struct StructGPUParameters
91 {
2fba026d 92 StructGPUParameters() : fNextTracklet(0), fScheduleFirstDynamicTracklet( 0 ), fGPUError( 0 ) {}
93 int fNextTracklet; //Next Tracklet to process
a59a784e 94 int fScheduleFirstDynamicTracklet; //Last Tracklet with fixed position in sheduling
95 int fGPUError; //Signalizes error on GPU during GPU Reconstruction, kind of return value
96 };
97
98 struct StructGPUParametersConst
99 {
100 StructGPUParametersConst() : fGPUFixedBlockCount( 0 ), fGPUiSlice( 0 ), fGPUnSlices( 0 ) {}
101 int fGPUFixedBlockCount; //Count of blocks that is used for this tracker in fixed schedule situations
78c19cec 102 int fGPUiSlice; // slice number processed by running GPU MP
103 int fGPUnSlices; // n of slices to be processed in parallel
a59a784e 104 };
105
106 struct commonMemoryStruct
107 {
108 commonMemoryStruct() : fNTracklets( 0 ), fNTracks( 0 ), fNTrackHits( 0 ), fGPUParameters() {}
109 int fNTracklets; // number of tracklets
110 int fNTracks; // number of reconstructed tracks
111 int fNTrackHits; // number of track hits
112 StructGPUParameters fGPUParameters; // GPU parameters
113 };
114
115 void Initialize( const AliHLTTPCCAParam &param );
116
117 void StartEvent();
118
119 int CheckEmptySlice() const;
cf9f6481 120 void WriteOutputPrepare();
a59a784e 121 void WriteOutput();
122
7be9b0d7 123#if !defined(HLTCA_GPUCODE)
a59a784e 124 void Reconstruct();
31649d4b 125#endif //!HLTCA_GPUCODE
a59a784e 126
127 //Make Reconstruction steps directly callable (Used for GPU debugging)
128 void RunNeighboursFinder();
129 void RunNeighboursCleaner();
130 void RunStartHitsFinder();
8566066c 131 void RunTrackletConstructor();
a59a784e 132 void RunTrackletSelector();
133
134 //GPU Tracker Interface
135 void SetGPUTracker();
136 void SetGPUDebugLevel(int Level, std::ostream *NewDebugOut = NULL) {fGPUDebugLevel = Level;if (NewDebugOut) fGPUDebugOut = NewDebugOut;}
137
8566066c 138 char* SetGPUTrackerCommonMemory(char* const pGPUMemory);
a59a784e 139 char* SetGPUTrackerHitsMemory(char* pGPUMemory, int MaxNHits);
6f0cdd46 140 char* SetGPUTrackerTrackletsMemory(char* pGPUMemory, int MaxNTracklets, int constructorBlockCount);
a59a784e 141 char* SetGPUTrackerTracksMemory(char* pGPUMemory, int MaxNTracks, int MaxNHits );
142
143 //Debugging Stuff
144 void DumpSliceData(std::ostream &out); //Dump Input Slice Data
145 void DumpLinks(std::ostream &out); //Dump all links to file (for comparison after NeighboursFinder/Cleaner)
146 void DumpStartHits(std::ostream &out); //Same for Start Hits
147 void DumpHitWeights(std::ostream &out); //....
148 void DumpTrackHits(std::ostream &out); //Same for Track Hits
149 void DumpTrackletHits(std::ostream &out); //Same for Track Hits
150
151 GPUd() void GetErrors2( int iRow, const AliHLTTPCCATrackParam &t, float &Err2Y, float &Err2Z ) const;
152 GPUd() void GetErrors2( int iRow, float z, float sinPhi, float cosPhi, float DzDs, float &Err2Y, float &Err2Z ) const;
153
154 void FitTrack( const AliHLTTPCCATrack &track, float *t0 = 0 ) const;
155 void FitTrackFull( const AliHLTTPCCATrack &track, float *t0 = 0 ) const;
156
157 void SetupCommonMemory();
158 void SetPointersHits( int MaxNHits );
159 void SetPointersTracklets ( int MaxNTracklets );
160 void SetPointersTracks( int MaxNTracks, int MaxNHits );
8566066c 161 size_t SetPointersSliceData(const AliHLTTPCCAClusterData *data, bool allocate = false) { return(fData.SetPointers(data, allocate)); }
a59a784e 162
163 void SetOutput( AliHLTTPCCASliceOutput** out ) { fOutput = out; }
164
165 void ReadEvent( AliHLTTPCCAClusterData *clusterData );
166
b22af1bf 167#if !defined(HLTCA_GPUCODE)
a59a784e 168 GPUh() void WriteEvent( std::ostream &out );
169 GPUh() void WriteTracks( std::ostream &out ) ;
170 GPUh() void ReadTracks( std::istream &in );
31649d4b 171#endif //!HLTCA_GPUCODE
a59a784e 172
173 GPUhd() const AliHLTTPCCAParam &Param() const { return fParam; }
2fba026d 174 GPUhd() const AliHLTTPCCAParam *pParam() const { return &fParam; }
a59a784e 175 GPUhd() void SetParam( const AliHLTTPCCAParam &v ) { fParam = v; }
176
8566066c 177 GPUhd() const AliHLTTPCCASliceOutput::outputControlStruct* OutputControl() const { return fOutputControl; }
178 GPUh() void SetOutputControl( AliHLTTPCCASliceOutput::outputControlStruct* const val) { fOutputControl = val; }
179
a59a784e 180 GPUhd() AliHLTTPCCAClusterData *ClusterData() const { return fClusterData; }
181 GPUhd() const AliHLTTPCCASliceData &Data() const { return fData; }
a59a784e 182
183 GPUh() void ClearSliceDataHitWeights() {fData.ClearHitWeights();}
184
185 GPUhd() const AliHLTTPCCARow &Row( int rowIndex ) const { return fData.Row( rowIndex ); }
186 GPUh() const AliHLTTPCCARow &Row( const AliHLTTPCCAHitId &HitId ) const { return fData.Row( HitId.RowIndex() ); }
187
188 GPUhd() double Timer( int i ) const { return fTimers[i]; }
189 GPUhd() void SetTimer( int i, double v ) { fTimers[i] = v; }
190
191 GPUhd() int NHitsTotal() const { return fData.NumberOfHits(); }
192
193 GPUd() void SetHitLinkUpData( const AliHLTTPCCARow &row, int hitIndex, short v ) { fData.SetHitLinkUpData( row, hitIndex, v ); }
194 GPUd() void SetHitLinkDownData( const AliHLTTPCCARow &row, int hitIndex, short v ) { fData.SetHitLinkDownData( row, hitIndex, v ); }
195 GPUd() short HitLinkUpData( const AliHLTTPCCARow &row, int hitIndex ) const { return fData.HitLinkUpData( row, hitIndex ); }
196 GPUd() short HitLinkDownData( const AliHLTTPCCARow &row, int hitIndex ) const { return fData.HitLinkDownData( row, hitIndex ); }
197
198 GPUd() const ushort2 *HitData( const AliHLTTPCCARow &row ) const { return fData.HitData(row); }
199 GPUd() const short_v *HitLinkUpData ( const AliHLTTPCCARow &row ) const { return fData.HitLinkUpData(row); }
200 GPUd() const short_v *HitLinkDownData( const AliHLTTPCCARow &row ) const { return fData.HitLinkDownData(row); }
201 GPUd() const ushort_v *FirstHitInBin( const AliHLTTPCCARow &row ) const { return fData.FirstHitInBin(row); }
202
203 GPUd() int FirstHitInBin( const AliHLTTPCCARow &row, int binIndex ) const { return fData.FirstHitInBin( row, binIndex ); }
204
205 GPUd() unsigned short HitDataY( const AliHLTTPCCARow &row, int hitIndex ) const {
206 return fData.HitDataY( row, hitIndex );
207 }
208 GPUd() unsigned short HitDataZ( const AliHLTTPCCARow &row, int hitIndex ) const {
209 return fData.HitDataZ( row, hitIndex );
210 }
211 GPUd() ushort2 HitData( const AliHLTTPCCARow &row, int hitIndex ) const {
212 return fData.HitData( row, hitIndex );
213 }
214
215 GPUhd() int HitInputID( const AliHLTTPCCARow &row, int hitIndex ) const { return fData.ClusterDataIndex( row, hitIndex ); }
216
217 /**
218 * The hit weight is used to determine whether a hit belongs to a certain tracklet or another one
219 * competing for the same hit. The tracklet that has a higher weight wins. Comparison is done
220 * using the the number of hits in the tracklet (the more hits it has the more it keeps). If
221 * tracklets have the same number of hits then it doesn't matter who gets it, but it should be
222 * only one. So a unique number (row index is good) is added in the least significant part of
223 * the weight
224 */
6f0cdd46 225 GPUd() static int CalculateHitWeight( int NHits, float chi2, int ) {
226 const float chi2_suppress = 6.f;
227 float weight = (((float) NHits * (chi2_suppress - chi2 / 500.f)) * (1e9 / chi2_suppress / 160.));
228 if (weight < 0 || weight > 2e9) weight = 0;
229 return ( (int) weight );
230 //return( (NHits << 16) + num);
a59a784e 231 }
232 GPUd() void MaximizeHitWeight( const AliHLTTPCCARow &row, int hitIndex, int weight ) {
233 fData.MaximizeHitWeight( row, hitIndex, weight );
234 }
235 GPUd() int HitWeight( const AliHLTTPCCARow &row, int hitIndex ) const {
236 return fData.HitWeight( row, hitIndex );
237 }
238
444e5682 239 GPUhd() int *NTracklets() const { return &fCommonMem->fNTracklets; }
a59a784e 240
241 GPUhd() const AliHLTTPCCAHitId &TrackletStartHit( int i ) const { return fTrackletStartHits[i]; }
242 GPUhd() AliHLTTPCCAHitId *TrackletStartHits() const { return fTrackletStartHits; }
243 GPUhd() AliHLTTPCCAHitId *TrackletTmpStartHits() const { return fTrackletTmpStartHits; }
244 GPUhd() const AliHLTTPCCATracklet &Tracklet( int i ) const { return fTracklets[i]; }
245 GPUhd() AliHLTTPCCATracklet *Tracklets() const { return fTracklets;}
444e5682 246 GPUhd() int* TrackletRowHits() const { return fTrackletRowHits; }
a59a784e 247
248 GPUhd() int *NTracks() const { return &fCommonMem->fNTracks; }
249 GPUhd() AliHLTTPCCATrack *Tracks() const { return fTracks; }
250 GPUhd() int *NTrackHits() const { return &fCommonMem->fNTrackHits; }
251 GPUhd() AliHLTTPCCAHitId *TrackHits() const { return fTrackHits; }
252
253 GPUhd() AliHLTTPCCASliceOutput** Output() const { return fOutput; }
254
444e5682 255 GPUh() commonMemoryStruct *CommonMemory() const {return(fCommonMem); }
a59a784e 256 GPUh() static size_t CommonMemorySize() { return(sizeof(AliHLTTPCCATracker::commonMemoryStruct)); }
444e5682 257 GPUh() char* HitMemory() const {return(fHitMemory); }
a59a784e 258 GPUh() size_t HitMemorySize() const {return(fHitMemorySize); }
444e5682 259 GPUh() char* TrackletMemory() {return(fTrackletMemory); }
a59a784e 260 GPUh() size_t TrackletMemorySize() const {return(fTrackletMemorySize); }
444e5682 261 GPUh() char* TrackMemory() {return(fTrackMemory); }
a59a784e 262 GPUh() size_t TrackMemorySize() const {return(fTrackMemorySize); }
444e5682 263 GPUhd() AliHLTTPCCARow* SliceDataRows() const {return(fData.Rows()); }
a59a784e 264
265 GPUhd() uint3* RowStartHitCountOffset() const {return(fRowStartHitCountOffset);}
266 GPUhd() AliHLTTPCCATrackletConstructor::AliHLTTPCCAGPUTempMemory* GPUTrackletTemp() const {return(fGPUTrackletTemp);}
267 GPUhd() int* RowBlockTracklets(int reverse, int iRowBlock) const {return(&fRowBlockTracklets[(reverse * ((fParam.NRows() / HLTCA_GPU_SCHED_ROW_STEP) + 1) + iRowBlock) * fCommonMem->fNTracklets]);}
268 GPUhd() int* RowBlockTracklets() const {return(fRowBlockTracklets);}
269 GPUhd() int4* RowBlockPos(int reverse, int iRowBlock) const {return(&fRowBlockPos[reverse * ((fParam.NRows() / HLTCA_GPU_SCHED_ROW_STEP) + 1) + iRowBlock]);}
270 GPUhd() int4* RowBlockPos() const {return(fRowBlockPos);}
271 GPUhd() uint2* BlockStartingTracklet() const {return(fBlockStartingTracklet);}
272 GPUhd() StructGPUParameters* GPUParameters() const {return(&fCommonMem->fGPUParameters);}
273 GPUhd() StructGPUParametersConst* GPUParametersConst() {return(&fGPUParametersConst);}
78c19cec 274 GPUhd() void SetGPUTextureBase(char* val) { fData.SetGPUTextureBase(val); }
275 GPUh() void SetGPUSliceDataMemory(void* const pSliceMemory, void* const pRowMemory) { fData.SetGPUSliceDataMemory(pSliceMemory, pRowMemory); }
8566066c 276
a59a784e 277 GPUh() unsigned long long int* PerfTimer(unsigned int i) {return &fPerfTimers[i]; }
f0bada7f 278
279#ifdef HLTCA_STANDALONE
280 GPUhd() char* StageAtSync() {return(fStageAtSync);}
281 GPUh() const char* LinkTmpMemory() const {return(fLinkTmpMemory);}
282#endif
283
2fba026d 284#ifdef HLTCA_STANDALONE
285 static inline void StandaloneQueryTime(unsigned long long int *i);
286 static inline void StandaloneQueryFreq(unsigned long long int *i);
287#endif //HLTCA_STANDALONE
288
a59a784e 289private:
f0bada7f 290 //Temporary Variables for Standalone measurements
291#ifdef HLTCA_STANDALONE
2fba026d 292public:
a59a784e 293 char* fStageAtSync; //Pointer to array storing current stage for every thread at every sync point
f0bada7f 294 char *fLinkTmpMemory; //tmp memory for hits after neighbours finder
2fba026d 295private:
f0bada7f 296#endif
a59a784e 297
298 AliHLTTPCCAParam fParam; // parameters
299 double fTimers[10]; // timers
300 unsigned long long int fPerfTimers[16]; // running CPU time for different parts of the algorithm
301 void StandalonePerfTime(int i);
302
303 AliHLTTPCCASliceOutput::outputControlStruct* fOutputControl; // output control
304
305 /** A pointer to the ClusterData object that the SliceData was created from. This can be used to
306 * merge clusters from inside the SliceTracker code and recreate the SliceData. */
307 AliHLTTPCCAClusterData *fClusterData; // ^
308 AliHLTTPCCASliceData fData; // The SliceData object. It is used to encapsulate the storage in memory from the access
309
310 bool fIsGPUTracker; // is it GPU tracker object
311 int fGPUDebugLevel; // debug level
312 std::ostream *fGPUDebugOut; // debug stream
313
314 //GPU Temp Arrays
315 uint3* fRowStartHitCountOffset; //Offset, length and new offset of start hits in row
316 AliHLTTPCCAHitId *fTrackletTmpStartHits; //Unsorted start hits
317 AliHLTTPCCATrackletConstructor::AliHLTTPCCAGPUTempMemory *fGPUTrackletTemp; //Temp Memory for GPU Tracklet Constructor
318 int* fRowBlockTracklets; //Reference which tracklet is processed in which rowblock next
8566066c 319 int4* fRowBlockPos; //x is last tracklet to be processed, y is last tracklet already processed, z is last tracklet to be processed in next iteration, w is initial x value to check if tracklet must be initialized
320 uint2* fBlockStartingTracklet; // First Tracklet that is to be processed by current GPU MP
321
322 StructGPUParametersConst fGPUParametersConst; // Parameters for GPU if this is a GPU tracker
323
a59a784e 324 // event
325
326 commonMemoryStruct *fCommonMem; // common event memory
327
328 char *fHitMemory; // event memory for hits
8566066c 329 size_t fHitMemorySize; // size of the event memory for hits [bytes]
330
331 char *fTrackletMemory; //event memory for tracklets
332 size_t fTrackletMemorySize; //size of the event memory for tracklets
333
a59a784e 334 char *fTrackMemory; // event memory for tracks
8566066c 335 size_t fTrackMemorySize; // size of the event memory for tracks [bytes]
336
a59a784e 337 AliHLTTPCCAHitId *fTrackletStartHits; // start hits for the tracklets
338 AliHLTTPCCATracklet *fTracklets; // tracklets
8566066c 339 int *fTrackletRowHits; //Hits for each Tracklet in each row
340
a59a784e 341 //
342 AliHLTTPCCATrack *fTracks; // reconstructed tracks
343 AliHLTTPCCAHitId *fTrackHits; // array of track hit numbers
344
345 // output
346
8566066c 347 AliHLTTPCCASliceOutput **fOutput; //address of pointer pointing to SliceOutput Object
a59a784e 348
349 // disable copy
350 AliHLTTPCCATracker( const AliHLTTPCCATracker& );
351 AliHLTTPCCATracker &operator=( const AliHLTTPCCATracker& );
352
353 static int StarthitSortComparison(const void*a, const void* b);
326c2d4b 354};
355
2fba026d 356#ifdef HLTCA_STANDALONE
357 void AliHLTTPCCATracker::StandaloneQueryTime(unsigned long long int *i)
358 {
359 #ifdef R__WIN32
360 QueryPerformanceCounter((LARGE_INTEGER*) i);
361 #else
362 timespec t;
363 clock_gettime(CLOCK_REALTIME, &t);
364 *i = (unsigned long long int) t.tv_sec * (unsigned long long int) 1000000000 + (unsigned long long int) t.tv_nsec;
365 #endif //R__WIN32
366 }
367
368 void AliHLTTPCCATracker::StandaloneQueryFreq(unsigned long long int *i)
369 {
370 #ifdef R__WIN32
371 QueryPerformanceFrequency((LARGE_INTEGER*) i);
372 #else
373 *i = 1000000000;
374 #endif //R__WIN32
375 }
376#endif //HLTCA_STANDALONE
00d07bcd 377
31649d4b 378#endif //ALIHLTTPCCATRACKER_H