coverity warnings fixed
[u/mrichter/AliRoot.git] / HLT / TPCLib / tracking-ca / AliHLTTPCCATracker.cxx
CommitLineData
326c2d4b 1// @(#) $Id$
ce565086 2// **************************************************************************
fbb9b71b 3// This file is property of and copyright by the ALICE HLT Project *
d54804bf 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. *
ce565086 17// *
d54804bf 18//***************************************************************************
326c2d4b 19
20#include "AliHLTTPCCATracker.h"
d54804bf 21#include "AliHLTTPCCARow.h"
22#include "AliHLTTPCCATrack.h"
4687b8fc 23#include "AliHLTTPCCATracklet.h"
00d07bcd 24#include "AliHLTTPCCAMath.h"
4acc2401 25#include "MemoryAssignmentHelpers.h"
326c2d4b 26
dc4788ec 27#include "TStopwatch.h"
00d07bcd 28#include "AliHLTTPCCAHitArea.h"
29#include "AliHLTTPCCANeighboursFinder.h"
30#include "AliHLTTPCCANeighboursCleaner.h"
31#include "AliHLTTPCCAStartHitsFinder.h"
32#include "AliHLTTPCCATrackletConstructor.h"
33#include "AliHLTTPCCATrackletSelector.h"
34#include "AliHLTTPCCAProcess.h"
4acc2401 35#include "AliHLTTPCCAClusterData.h"
00d07bcd 36
37#include "AliHLTTPCCATrackParam.h"
00d07bcd 38
b22af1bf 39#include "AliHLTTPCCAGPUConfig.h"
b22af1bf 40
00d07bcd 41#if !defined(HLTCA_GPUCODE)
4687b8fc 42#include <iostream>
a59a784e 43#include <iomanip>
b22af1bf 44#include <string.h>
00d07bcd 45#endif
d54804bf 46
6de2bc40 47//#define DRAW1
326c2d4b 48
6de2bc40 49#ifdef DRAW1
fbb9b71b 50#include "AliHLTTPCCADisplay.h"
31649d4b 51#endif //DRAW1
326c2d4b 52
ce565086 53#ifdef HLTCA_INTERNAL_PERFORMANCE
a59a784e 54//#include "AliHLTTPCCAPerformance.h"
ce565086 55#endif
56
fbb9b71b 57ClassImp( AliHLTTPCCATracker )
326c2d4b 58
b22af1bf 59#if !defined(HLTCA_GPUCODE)
60
61AliHLTTPCCATracker::~AliHLTTPCCATracker()
326c2d4b 62{
2fba026d 63 // destructor
7be9b0d7 64 if (!fIsGPUTracker)
65 {
b22af1bf 66 if (fCommonMem) delete fCommonMem;
67 if (fHitMemory) delete[] fHitMemory;
68 if (fTrackletMemory) delete[] fTrackletMemory;
69 if (fTrackMemory) delete[] fTrackMemory;
70 fCommonMem = NULL;
71 fHitMemory = fTrackMemory = NULL;
7be9b0d7 72 }
326c2d4b 73}
00d07bcd 74
326c2d4b 75// ----------------------------------------------------------------------------------
7be9b0d7 76void AliHLTTPCCATracker::Initialize( const AliHLTTPCCAParam &param )
326c2d4b 77{
2fba026d 78 // initialisation
79 fParam = param;
80 fParam.Update();
81 fData.InitializeRows( fParam );
4acc2401 82
2fba026d 83 StartEvent();
326c2d4b 84}
85
7be9b0d7 86void AliHLTTPCCATracker::StartEvent()
326c2d4b 87{
2fba026d 88 // start new event and fresh the memory
d54804bf 89
2fba026d 90 SetupCommonMemory();
4acc2401 91}
92
7be9b0d7 93void AliHLTTPCCATracker::SetGPUTracker()
94{
8566066c 95 //Make this a GPU Tracker
7be9b0d7 96 fIsGPUTracker = true;
b22af1bf 97 fData.SetGpuSliceData();
7be9b0d7 98}
99
8566066c 100char* AliHLTTPCCATracker::SetGPUTrackerCommonMemory(char* const pGPUMemory)
7be9b0d7 101{
8566066c 102 //Set up common Memory Pointer for GPU Tracker
2fba026d 103 fCommonMem = (commonMemoryStruct*) pGPUMemory;
104 return(pGPUMemory + sizeof(commonMemoryStruct));
7be9b0d7 105}
106
107
b22af1bf 108char* AliHLTTPCCATracker::SetGPUTrackerHitsMemory(char* pGPUMemory, int MaxNHits)
7be9b0d7 109{
8566066c 110 //Set up Hits Memory Pointers for GPU Tracker
7be9b0d7 111 fHitMemory = (char*) pGPUMemory;
112 SetPointersHits(MaxNHits);
b22af1bf 113 pGPUMemory += fHitMemorySize;
114 AssignMemory(fTrackletTmpStartHits, pGPUMemory, NHitsTotal());
115 AssignMemory(fRowStartHitCountOffset, pGPUMemory, Param().NRows());
116
117 return(pGPUMemory);
7be9b0d7 118}
119
c9b01b95 120char* AliHLTTPCCATracker::SetGPUTrackerTrackletsMemory(char* pGPUMemory, int MaxNTracks, int
121#ifndef HLTCA_GPU_ALTERNATIVE_SCHEDULER
122constructorBlockCount
123#endif
124)
b22af1bf 125{
8566066c 126 //Set up Tracklet Memory Pointers for GPU Tracker
b22af1bf 127 fTrackletMemory = (char*) pGPUMemory;
128 SetPointersTracklets(MaxNTracks);
129 pGPUMemory += fTrackletMemorySize;
2fba026d 130#ifndef HLTCA_GPU_ALTERNATIVE_SCHEDULER
b22af1bf 131 AssignMemory(fGPUTrackletTemp, pGPUMemory, MaxNTracks);
132 AssignMemory(fRowBlockTracklets, pGPUMemory, MaxNTracks * 2 * (Param().NRows() / HLTCA_GPU_SCHED_ROW_STEP + 1));
133 AssignMemory(fRowBlockPos, pGPUMemory, 2 * (Param().NRows() / HLTCA_GPU_SCHED_ROW_STEP + 1));
6f0cdd46 134 AssignMemory(fBlockStartingTracklet, pGPUMemory, constructorBlockCount);
2fba026d 135#endif
b22af1bf 136
137 return(pGPUMemory);
138}
7be9b0d7 139
140char* AliHLTTPCCATracker::SetGPUTrackerTracksMemory(char* pGPUMemory, int MaxNTracks, int MaxNHits )
141{
8566066c 142 //Set up Tracks Memory Pointer for GPU Tracker
7be9b0d7 143 fTrackMemory = (char*) pGPUMemory;
144 SetPointersTracks(MaxNTracks, MaxNHits);
b22af1bf 145 pGPUMemory += fTrackMemorySize;
146
147 return(pGPUMemory);
7be9b0d7 148}
149
b22af1bf 150void AliHLTTPCCATracker::DumpSliceData(std::ostream &out)
7be9b0d7 151{
8566066c 152 //Dump Slice Input Data to File
f0bada7f 153 out << "Slice Data (Slice" << Param().ISlice() << "):" << std::endl;
7be9b0d7 154 for (int i = 0;i < Param().NRows();i++)
155 {
2fba026d 156 if (Row(i).NHits() == 0) continue;
8566066c 157 out << "Row: " << i << std::endl;
7be9b0d7 158 for (int j = 0;j < Row(i).NHits();j++)
159 {
b22af1bf 160 if (j && j % 16 == 0) out << std::endl;
161 out << j << '-' << Data().HitDataY(Row(i), j) << '-' << Data().HitDataZ(Row(i), j) << ", ";
7be9b0d7 162 }
8566066c 163 out << std::endl;
7be9b0d7 164 }
165}
166
b22af1bf 167void AliHLTTPCCATracker::DumpLinks(std::ostream &out)
7be9b0d7 168{
8566066c 169 //Dump Links (after Neighbours Finder / Cleaner) to file
f0bada7f 170 out << "Hit Links(Slice" << Param().ISlice() << "):" << std::endl;
b22af1bf 171 for (int i = 0;i < Param().NRows();i++)
7be9b0d7 172 {
2fba026d 173 if (Row(i).NHits() == 0) continue;
8566066c 174 out << "Row: " << i << std::endl;
b22af1bf 175 for (int j = 0;j < Row(i).NHits();j++)
7be9b0d7 176 {
2fba026d 177 if (j && j % 32 == 0) out << std::endl;
b22af1bf 178 out << HitLinkUpData(Row(i), j) << "/" << HitLinkDownData(Row(i), j) << ", ";
7be9b0d7 179 }
8566066c 180 out << std::endl;
7be9b0d7 181 }
7be9b0d7 182}
183
b22af1bf 184void AliHLTTPCCATracker::DumpHitWeights(std::ostream &out)
7be9b0d7 185{
8566066c 186 //dump hit weights to file
2fba026d 187 out << "Hit Weights(Slice" << Param().ISlice() << "):" << std::endl;
188 for (int i = 0;i < Param().NRows();i++)
7be9b0d7 189 {
2fba026d 190 if (Row(i).NHits() == 0) continue;
191 out << "Row: " << i << ":" << std::endl;
192 for (int j = 0;j < Row(i).NHits();j++)
193 {
194 if (j && j % 32 == 0) out << std::endl;
195 out << HitWeight(Row(i), j) << ", ";
196 }
197 out << std::endl;
7be9b0d7 198 }
b22af1bf 199}
200
a59a784e 201int AliHLTTPCCATracker::StarthitSortComparison(const void*a, const void* b)
b22af1bf 202{
8566066c 203 //qsort helper function to sort start hits
b22af1bf 204 AliHLTTPCCAHitId* aa = (AliHLTTPCCAHitId*) a;
205 AliHLTTPCCAHitId* bb = (AliHLTTPCCAHitId*) b;
206
207 if (aa->RowIndex() != bb->RowIndex()) return(aa->RowIndex() - bb->RowIndex());
208 return(aa->HitIndex() - bb->HitIndex());
7be9b0d7 209}
210
b22af1bf 211void AliHLTTPCCATracker::DumpStartHits(std::ostream &out)
7be9b0d7 212{
8566066c 213 //sort start hits and dump to file
f0bada7f 214 out << "Start Hits: (Slice" << Param().ISlice() << ") (" << *NTracklets() << ")" << std::endl;
b22af1bf 215#ifdef HLTCA_GPU_SORT_DUMPDATA
a59a784e 216 qsort(TrackletStartHits(), *NTracklets(), sizeof(AliHLTTPCCAHitId), StarthitSortComparison);
b22af1bf 217#endif
218 for (int i = 0;i < *NTracklets();i++)
7be9b0d7 219 {
8566066c 220 out << TrackletStartHit(i).RowIndex() << "-" << TrackletStartHit(i).HitIndex() << std::endl;
7be9b0d7 221 }
8566066c 222 out << std::endl;
b22af1bf 223}
224
225void AliHLTTPCCATracker::DumpTrackHits(std::ostream &out)
226{
8566066c 227 //dump tracks to file
f0bada7f 228 out << "Tracks: (Slice" << Param().ISlice() << ") (" << *NTracks() << ")" << std::endl;
b22af1bf 229#ifdef HLTCA_GPU_SORT_DUMPDATA
230 for (int k = 0;k < Param().NRows();k++)
7be9b0d7 231 {
b22af1bf 232 for (int l = 0;l < Row(k).NHits();l++)
7be9b0d7 233 {
b22af1bf 234#endif
235 for (int j = 0;j < *NTracks();j++)
236 {
237 if (Tracks()[j].NHits() == 0 || !Tracks()[j].Alive()) continue;
238#ifdef HLTCA_GPU_SORT_DUMPDATA
239 if (TrackHits()[Tracks()[j].FirstHitID()].RowIndex() == k && TrackHits()[Tracks()[j].FirstHitID()].HitIndex() == l)
240 {
241#endif
242 for (int i = 0;i < Tracks()[j].NHits();i++)
243 {
244 out << TrackHits()[Tracks()[j].FirstHitID() + i].RowIndex() << "-" << TrackHits()[Tracks()[j].FirstHitID() + i].HitIndex() << ", ";
245 }
6f0cdd46 246#ifndef BITWISE_COMPATIBLE_DEBUG_OUTPUT
247 out << "(Track: " << j << ")";
248#endif
249 out << std::endl;
b22af1bf 250#ifdef HLTCA_GPU_SORT_DUMPDATA
251 }
252 }
253#endif
254 }
255#ifdef HLTCA_GPU_SORT_DUMPDATA
7be9b0d7 256 }
b22af1bf 257#endif
7be9b0d7 258}
259
2fba026d 260void AliHLTTPCCATracker::DumpTrackletHits(std::ostream &out)
7be9b0d7 261{
a4127b84 262 //dump tracklets to file
263 int nTracklets = *NTracklets();
264 if( nTracklets<0 ) nTracklets = 0;
265 if( nTracklets>HLTCA_GPU_MAX_TRACKLETS ) nTracklets = HLTCA_GPU_MAX_TRACKLETS;
266 out << "Tracklets: (Slice" << Param().ISlice() << ") (" << nTracklets << ")" << std::endl;
b22af1bf 267#ifdef HLTCA_GPU_SORT_DUMPDATA
a4127b84 268 AliHLTTPCCAHitId* tmpIds = new AliHLTTPCCAHitId[nTracklets];
269 AliHLTTPCCATracklet* tmpTracklets = new AliHLTTPCCATracklet[nTracklets];
270 memcpy(tmpIds, TrackletStartHits(), nTracklets * sizeof(AliHLTTPCCAHitId));
271 memcpy(tmpTracklets, Tracklets(), nTracklets * sizeof(AliHLTTPCCATracklet));
b22af1bf 272#ifdef EXTERN_ROW_HITS
a4127b84 273 int* tmpHits = new int[nTracklets * Param().NRows()];
274 memcpy(tmpHits, TrackletRowHits(), nTracklets * Param().NRows() * sizeof(int));
b22af1bf 275#endif
a4127b84 276 qsort(TrackletStartHits(), nTracklets, sizeof(AliHLTTPCCAHitId), StarthitSortComparison);
277 for (int i = 0;i < nTracklets; i++ ){
278 for (int j = 0;j < nTracklets; j++ ){
279 if (tmpIds[i].RowIndex() == TrackletStartHit(j).RowIndex() && tmpIds[i].HitIndex() == TrackletStartHit(j).HitIndex() ){
280 memcpy(&Tracklets()[j], &tmpTracklets[i], sizeof(AliHLTTPCCATracklet));
b22af1bf 281#ifdef EXTERN_ROW_HITS
a4127b84 282 if (tmpTracklets[i].NHits() ){
283 for (int k = tmpTracklets[i].FirstRow();k <= tmpTracklets[i].LastRow();k++){
284 const int pos = k * nTracklets + j;
285 if (pos < 0 || pos >= HLTCA_GPU_MAX_TRACKLETS * fParam.NRows()){
286 printf("internal error\n");
287 } else {
288 fTrackletRowHits[pos] = tmpHits[k * nTracklets + i];
289 }
290 }
b22af1bf 291 }
a4127b84 292#endif
293 break;
294 }
295 }
296 }
297 delete[] tmpIds;
298 delete[] tmpTracklets;
b22af1bf 299#ifdef EXTERN_ROW_HITS
a4127b84 300 delete[] tmpHits;
b22af1bf 301#endif
302#endif
a4127b84 303 for (int j = 0;j < nTracklets; j++ )
b22af1bf 304 {
a4127b84 305 out << "Tracklet " << std::setw(4) << j << " (Hits: " << std::setw(3) << Tracklets()[j].NHits() << ", Start: " << std::setw(3) << TrackletStartHit(j).RowIndex() << "-" << std::setw(3) << TrackletStartHit(j).HitIndex() << ", Rows: " << (Tracklets()[j].NHits() ? Tracklets()[j].FirstRow() : -1) << " - " << (Tracklets()[j].NHits() ? Tracklets()[j].LastRow() : -1) << ") ";
306 if (Tracklets()[j].NHits() == 0);
307 else if (Tracklets()[j].LastRow() > Tracklets()[j].FirstRow() && (Tracklets()[j].FirstRow() >= Param().NRows() || Tracklets()[j].LastRow() >= Param().NRows()))
308 {
b22af1bf 309#ifdef HLTCA_STANDALONE
a4127b84 310 printf("\nError: Tracklet %d First %d Last %d Hits %d", j, Tracklets()[j].FirstRow(), Tracklets()[j].LastRow(), Tracklets()[j].NHits());
311 out << " (Error: Tracklet " << j << " First " << Tracklets()[j].FirstRow() << " Last " << Tracklets()[j].LastRow() << " Hits " << Tracklets()[j].NHits() << ") ";
312 for (int i = 0;i < Param().NRows();i++)
313 {
314 //if (Tracklets()[j].RowHit(i) != -1)
f0bada7f 315#ifdef EXTERN_ROW_HITS
a4127b84 316 out << i << "-" << fTrackletRowHits[i * fCommonMem->fNTracklets + j] << ", ";
f0bada7f 317#else
a4127b84 318 out << i << "-" << Tracklets()[j].RowHit(i) << ", ";
b22af1bf 319#endif
320 }
a4127b84 321#endif
322 }
323 else if (Tracklets()[j].NHits() && Tracklets()[j].LastRow() > Tracklets()[j].FirstRow())
324 {
325 for (int i = Tracklets()[j].FirstRow();i <= Tracklets()[j].LastRow();i++)
b22af1bf 326 {
a4127b84 327 //if (Tracklets()[j].RowHit(i) != -1)
b22af1bf 328#ifdef EXTERN_ROW_HITS
a4127b84 329 out << i << "-" << fTrackletRowHits[i * fCommonMem->fNTracklets + j] << ", ";
b22af1bf 330#else
a4127b84 331 out << i << "-" << Tracklets()[j].RowHit(i) << ", ";
b22af1bf 332#endif
7be9b0d7 333 }
a4127b84 334 }
335 out << std::endl;
7be9b0d7 336 }
337}
338
339
b22af1bf 340void AliHLTTPCCATracker::SetupCommonMemory()
4acc2401 341{
2fba026d 342 // set up common memory
6de2bc40 343
2fba026d 344 if (!fIsGPUTracker)
345 {
346 if ( !fCommonMem ) {
347 // the 1600 extra bytes are not used unless fCommonMemorySize increases with a later event
348 //fCommonMemory = reinterpret_cast<char*> ( new uint4 [ fCommonMemorySize/sizeof( uint4 ) + 100] );
349 fCommonMem = new commonMemoryStruct;
350 }
fbb9b71b 351
2fba026d 352 if (fHitMemory) delete[] fHitMemory;
353 if (fTrackletMemory) delete[] fTrackletMemory;
354 if (fTrackMemory) delete[] fTrackMemory;
355 }
c259f7e4 356
2fba026d 357 fHitMemory = fTrackletMemory = fTrackMemory = 0;
b22af1bf 358
2fba026d 359 fData.Clear();
360 fCommonMem->fNTracklets = 0;
361 fCommonMem->fNTracks = 0 ;
362 fCommonMem->fNTrackHits = 0;
326c2d4b 363}
364
7be9b0d7 365void AliHLTTPCCATracker::ReadEvent( AliHLTTPCCAClusterData *clusterData )
366{
2fba026d 367 // read event
7be9b0d7 368
2fba026d 369 fClusterData = clusterData;
7be9b0d7 370
2fba026d 371 StartEvent();
7be9b0d7 372
2fba026d 373 //* Convert input hits, create grids, etc.
374 fData.InitFromClusterData( *clusterData );
b22af1bf 375 {
2fba026d 376 if (!fIsGPUTracker)
377 {
378 SetPointersHits( fData.NumberOfHits() ); // to calculate the size
379 fHitMemory = reinterpret_cast<char*> ( new uint4 [ fHitMemorySize/sizeof( uint4 ) + 100] );
380 }
381 SetPointersHits( fData.NumberOfHits() ); // set pointers for hits
b22af1bf 382 }
7be9b0d7 383}
384
fbb9b71b 385GPUhd() void AliHLTTPCCATracker::SetPointersHits( int MaxNHits )
c259f7e4 386{
2fba026d 387 // set all pointers to the event memory
c259f7e4 388
2fba026d 389 char *mem = fHitMemory;
c259f7e4 390
2fba026d 391 // extra arrays for tpc clusters
c259f7e4 392
b22af1bf 393#ifdef HLTCA_GPU_SORT_STARTHITS_2
2fba026d 394 AssignMemory( fTrackletStartHits, mem, MaxNHits + 32);
b22af1bf 395#else
2fba026d 396 AssignMemory( fTrackletStartHits, mem, MaxNHits);
b22af1bf 397#endif
398
2fba026d 399 // calculate the size
c259f7e4 400
2fba026d 401 fHitMemorySize = mem - fHitMemory;
b22af1bf 402}
c259f7e4 403
b22af1bf 404GPUhd() void AliHLTTPCCATracker::SetPointersTracklets( int MaxNTracklets )
405{
2fba026d 406 // set all pointers to the tracklets memory
407 char *mem = fTrackletMemory;
fbb9b71b 408
2fba026d 409 // memory for tracklets
c259f7e4 410
2fba026d 411 AssignMemory( fTracklets, mem, MaxNTracklets );
b22af1bf 412#ifdef EXTERN_ROW_HITS
2fba026d 413 AssignMemory( fTrackletRowHits, mem, MaxNTracklets * Param().NRows());
b22af1bf 414#endif
c259f7e4 415
2fba026d 416 fTrackletMemorySize = mem - fTrackletMemory;
c259f7e4 417}
418
419
fbb9b71b 420GPUhd() void AliHLTTPCCATracker::SetPointersTracks( int MaxNTracks, int MaxNHits )
c259f7e4 421{
2fba026d 422 // set all pointers to the tracks memory
423 char *mem = fTrackMemory;
fbb9b71b 424
2fba026d 425 // memory for selected tracks
fbb9b71b 426
2fba026d 427 AssignMemory( fTracks, mem, MaxNTracks );
428 AssignMemory( fTrackHits, mem, 2 * MaxNHits );
c259f7e4 429
2fba026d 430 // calculate the size
c259f7e4 431
2fba026d 432 fTrackMemorySize = mem - fTrackMemory;
c259f7e4 433}
434
a59a784e 435GPUh() int AliHLTTPCCATracker::CheckEmptySlice() const
7be9b0d7 436{
8566066c 437 //Check if the Slice is empty, if so set the output apropriate and tell the reconstuct procesdure to terminate
2fba026d 438 if ( NHitsTotal() < 1 ) {
439 {
440 AliHLTTPCCASliceOutput::Allocate(*fOutput, 0, 0, fOutputControl);
441 AliHLTTPCCASliceOutput* useOutput = *fOutput;
442 if (useOutput == NULL) return(1);
443 useOutput->SetNTracks( 0 );
444 useOutput->SetNTrackClusters( 0 );
445 }
326c2d4b 446
2fba026d 447 return 1;
448 }
449 return 0;
7be9b0d7 450}
451
452void AliHLTTPCCATracker::RunNeighboursFinder()
326c2d4b 453{
8566066c 454 //Run the CPU Neighbours Finder
7be9b0d7 455 AliHLTTPCCAProcess<AliHLTTPCCANeighboursFinder>( Param().NRows(), 1, *this );
456}
6de2bc40 457
7be9b0d7 458void AliHLTTPCCATracker::RunNeighboursCleaner()
459{
8566066c 460 //Run the CPU Neighbours Cleaner
7be9b0d7 461 AliHLTTPCCAProcess<AliHLTTPCCANeighboursCleaner>( Param().NRows() - 2, 1, *this );
462}
d54804bf 463
7be9b0d7 464void AliHLTTPCCATracker::RunStartHitsFinder()
465{
8566066c 466 //Run the CPU Start Hits Finder
7be9b0d7 467 AliHLTTPCCAProcess<AliHLTTPCCAStartHitsFinder>( Param().NRows() - 4, 1, *this );
468}
fbb9b71b 469
7be9b0d7 470void AliHLTTPCCATracker::RunTrackletConstructor()
471{
8566066c 472 //Run CPU Tracklet Constructor
2fba026d 473 AliHLTTPCCATrackletConstructor::AliHLTTPCCATrackletConstructorCPU(*this);
7be9b0d7 474}
fbb9b71b 475
7be9b0d7 476void AliHLTTPCCATracker::RunTrackletSelector()
477{
8566066c 478 //Run CPU Tracklet Selector
2fba026d 479 AliHLTTPCCAProcess<AliHLTTPCCATrackletSelector>( 1, fCommonMem->fNTracklets, *this );
d54804bf 480}
481
b22af1bf 482#ifdef HLTCA_STANDALONE
483void AliHLTTPCCATracker::StandalonePerfTime(int i)
484{
2fba026d 485 //Query Performance Timer for Standalone Version of Tracker
486 if (fGPUDebugLevel >= 1)
487 {
488 StandaloneQueryTime(&fPerfTimers[i]);
489 }
b22af1bf 490}
491#else
492void AliHLTTPCCATracker::StandalonePerfTime(int /*i*/) {}
493#endif
494
00d07bcd 495GPUh() void AliHLTTPCCATracker::Reconstruct()
496{
2fba026d 497 //* reconstruction of event
498 //std::cout<<"Reconstruct slice "<<fParam.ISlice()<<", nHits="<<NHitsTotal()<<std::endl;
326c2d4b 499
2fba026d 500 fTimers[0] = 0; // find neighbours
501 fTimers[1] = 0; // construct tracklets
502 fTimers[2] = 0; // fit tracklets
503 fTimers[3] = 0; // prolongation of tracklets
504 fTimers[4] = 0; // selection
505 fTimers[5] = 0; // write output
506 fTimers[6] = 0;
507 fTimers[7] = 0;
fbb9b71b 508
2fba026d 509 //if( fParam.ISlice()<1 ) return; //SG!!!
00d07bcd 510
2fba026d 511 TStopwatch timer0;
b22af1bf 512
2fba026d 513 StandalonePerfTime(0);
b22af1bf 514
2fba026d 515 if (CheckEmptySlice()) return;
4687b8fc 516
6de2bc40 517#ifdef DRAW1
2fba026d 518 //if( fParam.ISlice()==2 || fParam.ISlice()==3)
519 {
520 AliHLTTPCCADisplay::Instance().ClearView();
521 AliHLTTPCCADisplay::Instance().SetSliceView();
522 AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
523 AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
524 if ( NHitsTotal() > 0 ) {
525 AliHLTTPCCADisplay::Instance().DrawSliceHits( kRed, .5 );
526 AliHLTTPCCADisplay::Instance().Ask();
527 }
528 }
31649d4b 529#endif //DRAW1
693d2443 530
b22af1bf 531 fCommonMem->fNTracklets = fCommonMem->fNTracks = fCommonMem->fNTrackHits = 0;
4687b8fc 532
fbb9b71b 533#if !defined(HLTCA_GPUCODE)
534
2fba026d 535 if (fGPUDebugLevel >= 6)
536 {
6f0cdd46 537#ifndef BITWISE_COMPATIBLE_DEBUG_OUTPUT
2fba026d 538 *fGPUDebugOut << std::endl << std::endl << "Slice: " << Param().ISlice() << std::endl;
539 *fGPUDebugOut << "Slice Data:" << std::endl;
6f0cdd46 540#endif
2fba026d 541 DumpSliceData(*fGPUDebugOut);
542 }
b22af1bf 543
2fba026d 544 StandalonePerfTime(1);
693d2443 545
2fba026d 546 RunNeighboursFinder();
7be9b0d7 547
2fba026d 548 StandalonePerfTime(2);
b22af1bf 549
f0bada7f 550#ifdef TRACKER_KEEP_TEMPDATA
2fba026d 551 if (fLinkTmpMemory) delete[] fLinkTmpMemory;
552 fLinkTmpMemory = new char[fData.MemorySize()];
553 memcpy(fLinkTmpMemory, fData.Memory(), fData.MemorySize());
f0bada7f 554#endif
555
2fba026d 556 if (fGPUDebugLevel >= 6) DumpLinks(*fGPUDebugOut);
557
fbb9b71b 558#ifdef HLTCA_INTERNAL_PERFORMANCE
2fba026d 559 //if( Param().ISlice()<=2 )
560 //AliHLTTPCCAPerformance::Instance().LinkPerformance( Param().ISlice() );
ce565086 561#endif
562
563
f0fb467d 564#ifdef DRAW1
2fba026d 565 if ( NHitsTotal() > 0 ) {
566 AliHLTTPCCADisplay::Instance().DrawSliceLinks( -1, -1, 1 );
567 AliHLTTPCCADisplay::Instance().Ask();
568 }
31649d4b 569#endif //DRAW1
ce565086 570
2fba026d 571 RunNeighboursCleaner();
572
573 StandalonePerfTime(3);
ce565086 574
2fba026d 575 if (fGPUDebugLevel >= 6) DumpLinks(*fGPUDebugOut);
b22af1bf 576
2fba026d 577 RunStartHitsFinder();
fbb9b71b 578
2fba026d 579 StandalonePerfTime(4);
580 StandalonePerfTime(5);
c259f7e4 581
2fba026d 582 if (fGPUDebugLevel >= 6) DumpStartHits(*fGPUDebugOut);
d54804bf 583
2fba026d 584 fData.ClearHitWeights();
d54804bf 585
2fba026d 586 SetPointersTracklets( fCommonMem->fNTracklets * 2 ); // to calculate the size
587 fTrackletMemory = reinterpret_cast<char*> ( new uint4 [ fTrackletMemorySize/sizeof( uint4 ) + 100] );
588 SetPointersTracklets( fCommonMem->fNTracklets * 2 ); // set pointers for hits
b22af1bf 589
2fba026d 590 SetPointersTracks( fCommonMem->fNTracklets * 2, NHitsTotal() ); // to calculate the size
591 fTrackMemory = reinterpret_cast<char*> ( new uint4 [ fTrackMemorySize/sizeof( uint4 ) + 100] );
592 SetPointersTracks( fCommonMem->fNTracklets * 2, NHitsTotal() ); // set pointers for hits
b22af1bf 593
2fba026d 594 StandalonePerfTime(6);
595 StandalonePerfTime(7);
d54804bf 596
2fba026d 597 RunTrackletConstructor();
ce565086 598
2fba026d 599 StandalonePerfTime(8);
b22af1bf 600
2fba026d 601 if (fGPUDebugLevel >= 6) DumpTrackletHits(*fGPUDebugOut);
6f0cdd46 602#ifndef BITWISE_COMPATIBLE_DEBUG_OUTPUT
2fba026d 603 if (fGPUDebugLevel >= 6) DumpHitWeights(*fGPUDebugOut);
6f0cdd46 604#endif
d54804bf 605
2fba026d 606 //std::cout<<"Slice "<<Param().ISlice()<<": NHits="<<NHitsTotal()<<", NTracklets="<<*NTracklets()<<std::endl;
d54804bf 607
2fba026d 608 RunTrackletSelector();
d54804bf 609
2fba026d 610 StandalonePerfTime(9);
b22af1bf 611
2fba026d 612 //std::cout<<"Slice "<<Param().ISlice()<<": N start hits/tracklets/tracks = "<<nStartHits<<" "<<nStartHits<<" "<<*fNTracks<<std::endl;
d54804bf 613
2fba026d 614 if (fGPUDebugLevel >= 6) DumpTrackHits(*fGPUDebugOut);
c259f7e4 615
2fba026d 616 //std::cout<<"Memory used for slice "<<fParam.ISlice()<<" : "<<fCommonMemorySize/1024./1024.<<" + "<<fHitMemorySize/1024./1024.<<" + "<<fTrackMemorySize/1024./1024.<<" = "<<( fCommonMemorySize+fHitMemorySize+fTrackMemorySize )/1024./1024.<<" Mb "<<std::endl;
c259f7e4 617
2fba026d 618 WriteOutput();
fbb9b71b 619
2fba026d 620 StandalonePerfTime(10);
b22af1bf 621
d54804bf 622#endif
dc4788ec 623
f0fb467d 624#ifdef DRAW1
2fba026d 625 {
626 AliHLTTPCCADisplay &disp = AliHLTTPCCADisplay::Instance();
627 AliHLTTPCCATracker &slice = *this;
628 std::cout << "N out tracks = " << slice.NOutTracks() << std::endl;
629 AliHLTTPCCADisplay::Instance().SetSliceView();
630 AliHLTTPCCADisplay::Instance().SetCurrentSlice( this );
631 AliHLTTPCCADisplay::Instance().DrawSlice( this, 1 );
632 disp.DrawSliceHits( kRed, .5 );
633 disp.Ask();
634 for ( int itr = 0; itr < slice.NOutTracks(); itr++ ) {
635 std::cout << "track N " << itr << ", nhits=" << slice.OutTracks()[itr].NHits() << std::endl;
636 disp.DrawSliceOutTrack( itr, kBlue );
637 //disp.Ask();
638 //int id = slice.OutTracks()[itr].OrigTrackID();
639 //AliHLTTPCCATrack &tr = Tracks()[id];
640 //for( int ih=0; ih<tr.NHits(); ih++ ){
641 //int ic = (fTrackHits[tr.FirstHitID()+ih]);
642 //std::cout<<ih<<" "<<ID2IRow(ic)<<" "<<ID2IHit(ic)<<std::endl;
643 //}
644 //disp.DrawSliceTrack( id, kBlue );
645 //disp.Ask();
646 }
647 disp.Ask();
648 }
31649d4b 649#endif //DRAW1
693d2443 650
2fba026d 651 timer0.Stop();
652 fTimers[0] = timer0.CpuTime() / 100.;
326c2d4b 653
fbb9b71b 654}
326c2d4b 655
00d07bcd 656GPUh() void AliHLTTPCCATracker::WriteOutput()
657{
2fba026d 658 // write output
659
660 TStopwatch timer;
661
662 //cout<<"output: nTracks = "<<*fNTracks<<", nHitsTotal="<<NHitsTotal()<<std::endl;
663
664 if (fOutputControl == NULL) fOutputControl = new AliHLTTPCCASliceOutput::outputControlStruct;
665 AliHLTTPCCASliceOutput::Allocate(*fOutput, fCommonMem->fNTracks, fCommonMem->fNTrackHits, fOutputControl);
666 AliHLTTPCCASliceOutput* useOutput = *fOutput;
667 if (useOutput == NULL) return;
668
669 useOutput->SetNTracks( 0 );
670 useOutput->SetNTrackClusters( 0 );
671
672 int nStoredHits = 0;
673 int nStoredTracks = 0;
674
675 AliHLTTPCCASliceOutTrack *out = useOutput->FirstTrack();
676
677 for ( int iTr = 0; iTr < fCommonMem->fNTracks; iTr++ ) {
678 AliHLTTPCCATrack &iTrack = fTracks[iTr];
679
680 if( iTrack.NHits() < fParam.MinNTrackClusters() ) continue;
681 if( CAMath::Abs(iTrack.Param().GetQPt())> fParam.MaxTrackQPt() ) continue;
682
683 out->SetParam( iTrack.Param() );
684 int nClu = 0;
685 int iID = iTrack.FirstHitID();
686
687 for ( int ith = 0; ith < iTrack.NHits(); ith++ ) {
688 const AliHLTTPCCAHitId &ic = fTrackHits[iID + ith];
689 int iRow = ic.RowIndex();
690 int ih = ic.HitIndex();
691
692 const AliHLTTPCCARow &row = fData.Row( iRow );
693
694 int clusterIndex = fData.ClusterDataIndex( row, ih );
695 int clusterRowIndex = clusterIndex - fClusterData->RowOffset( iRow );
696
697 if ( clusterIndex < 0 || clusterIndex >= fClusterData->NumberOfClusters() ) {
698 //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
699 //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
700 //abort();
701 continue;
702 }
703 if ( clusterRowIndex < 0 || clusterRowIndex >= fClusterData->NumberOfClusters( iRow ) ) {
704 //std::cout << inpIDtot << ", " << fClusterData->NumberOfClusters()
705 //<< "; " << inpID << ", " << fClusterData->NumberOfClusters( iRow ) << std::endl;
706 //abort();
707 continue;
708 }
709
710 float origX = fClusterData->X( clusterIndex );
711 float origY = fClusterData->Y( clusterIndex );
712 float origZ = fClusterData->Z( clusterIndex );
713 int id = fClusterData->Id( clusterIndex );
714 AliHLTTPCCASliceOutCluster c;
715 c.Set( id, iRow, origX, origY, origZ );
716 out->SetCluster( nClu, c );
717 nClu++;
718 }
719
720 nStoredTracks++;
721 nStoredHits+=nClu;
722 out->SetNClusters( nClu );
723 out = out->NextTrack();
724 }
725
726 useOutput->SetNTracks( nStoredTracks );
727 useOutput->SetNTrackClusters( nStoredHits );
728
729 timer.Stop();
730 fTimers[5] += timer.CpuTime();
326c2d4b 731}
732
7be9b0d7 733#endif
734
4acc2401 735GPUh() void AliHLTTPCCATracker::FitTrackFull( const AliHLTTPCCATrack &/**/, float * /**/ ) const
fbb9b71b 736{
2fba026d 737 // fit track with material
fbb9b71b 738#ifdef XXX
2fba026d 739 //* Fit the track
740 FitTrack( iTrack, tt0 );
741 if ( iTrack.NHits() <= 3 ) return;
742
743 AliHLTTPCCATrackParam &t = iTrack.Param();
744 AliHLTTPCCATrackParam t0 = t;
745
746 t.Chi2() = 0;
747 t.NDF() = -5;
748 bool first = 1;
749
750 int iID = iTrack.FirstHitID();
751 for ( int ih = 0; ih < iTrack.NHits(); ih++, iID++ ) {
752 const AliHLTTPCCAHitId &ic = fTrackHits[iID];
753 int iRow = ic.rowIndex();
754 const AliHLTTPCCARow &row = fData.Row( iRow );
755 if ( !t0.TransportToX( row.X() ) ) continue;
756 float dy, dz;
757 const AliHLTTPCCAHit &h = ic.hitIndex();
758
759 // check for wrong hits
760 if ( 0 ) {
761 dy = t0.GetY() - h.Y();
762 dz = t0.GetZ() - h.Z();
763
764 //if( dy*dy > 3.5*3.5*(/*t0.GetErr2Y() + */h.ErrY()*h.ErrY() ) ) continue;//SG!!!
765 //if( dz*dz > 3.5*3.5*(/*t0.GetErr2Z() + */h.ErrZ()*h.ErrZ() ) ) continue;
766 }
767
768 if ( !t.TransportToX( row.X() ) ) continue;
769
770 //* Update the track
771
772 if ( first ) {
773 t.Cov()[ 0] = .5 * .5;
774 t.Cov()[ 1] = 0;
775 t.Cov()[ 2] = .5 * .5;
776 t.Cov()[ 3] = 0;
777 t.Cov()[ 4] = 0;
778 t.Cov()[ 5] = .2 * .2;
779 t.Cov()[ 6] = 0;
780 t.Cov()[ 7] = 0;
781 t.Cov()[ 8] = 0;
782 t.Cov()[ 9] = .2 * .2;
783 t.Cov()[10] = 0;
784 t.Cov()[11] = 0;
785 t.Cov()[12] = 0;
786 t.Cov()[13] = 0;
787 t.Cov()[14] = .2 * .2;
788 t.Chi2() = 0;
789 t.NDF() = -5;
790 }
791 float err2Y, err2Z;
792 GetErrors2( iRow, t, err2Y, err2Z );
793
794 if ( !t.Filter2( h.Y(), h.Z(), err2Y, err2Z ) ) continue;
795
796 first = 0;
797 }
798 /*
799 float cosPhi = iTrack.Param().GetCosPhi();
800 p0.Param().TransportToX(ID2Row( iTrack.PointID()[0] ).X());
801 p2.Param().TransportToX(ID2Row( iTrack.PointID()[1] ).X());
802 if( p0.Param().GetCosPhi()*cosPhi<0 ){ // change direction
803 float *par = p0.Param().Par();
804 float *cov = p0.Param().Cov();
805 par[2] = -par[2]; // sin phi
806 par[3] = -par[3]; // DzDs
807 par[4] = -par[4]; // kappa
808 cov[3] = -cov[3];
809 cov[4] = -cov[4];
810 cov[6] = -cov[6];
811 cov[7] = -cov[7];
812 cov[10] = -cov[10];
813 cov[11] = -cov[11];
814 p0.Param().CosPhi() = -p0.Param().GetCosPhi();
815 }
816 */
00d07bcd 817#endif
818}
dc4788ec 819
4acc2401 820GPUh() void AliHLTTPCCATracker::FitTrack( const AliHLTTPCCATrack &/*track*/, float * /*t0[]*/ ) const
fbb9b71b 821{
2fba026d 822 //* Fit the track
00d07bcd 823#ifdef XXX
2fba026d 824 AliHLTTPCCAEndPoint &p2 = ID2Point( track.PointID()[1] );
825 const AliHLTTPCCAHit &c0 = ID2Hit( fTrackHits[p0.TrackHitID()].HitID() );
826 const AliHLTTPCCAHit &c1 = ID2Hit( fTrackHits[track.HitID()[1]].HitID() );
827 const AliHLTTPCCAHit &c2 = ID2Hit( fTrackHits[p2.TrackHitID()].HitID() );
828 const AliHLTTPCCARow &row0 = ID2Row( fTrackHits[p0.TrackHitID()].HitID() );
829 const AliHLTTPCCARow &row1 = ID2Row( fTrackHits[track.HitID()[1]].HitID() );
830 const AliHLTTPCCARow &row2 = ID2Row( fTrackHits[p2.TrackHitID()].HitID() );
831 float sp0[5] = {row0.X(), c0.Y(), c0.Z(), c0.ErrY(), c0.ErrZ() };
832 float sp1[5] = {row1.X(), c1.Y(), c1.Z(), c1.ErrY(), c1.ErrZ() };
833 float sp2[5] = {row2.X(), c2.Y(), c2.Z(), c2.ErrY(), c2.ErrZ() };
834 //std::cout<<"Fit track, points ="<<sp0[0]<<" "<<sp0[1]<<" / "<<sp1[0]<<" "<<sp1[1]<<" / "<<sp2[0]<<" "<<sp2[1]<<std::endl;
835 if ( track.NHits() >= 3 ) {
836 p0.Param().ConstructXYZ3( sp0, sp1, sp2, p0.Param().CosPhi(), t0 );
837 p2.Param().ConstructXYZ3( sp2, sp1, sp0, p2.Param().CosPhi(), t0 );
838 //p2.Param() = p0.Param();
839 //p2.Param().TransportToX(row2.X());
840 //p2.Param().Par()[1] = -p2.Param().Par()[1];
841 //p2.Param().Par()[4] = -p2.Param().Par()[4];
842 } else {
843 p0.Param().X() = row0.X();
844 p0.Param().Y() = c0.Y();
845 p0.Param().Z() = c0.Z();
846 p0.Param().Err2Y() = c0.ErrY() * c0.ErrY();
847 p0.Param().Err2Z() = c0.ErrZ() * c0.ErrZ();
848 p2.Param().X() = row2.X();
849 p2.Param().Y() = c2.Y();
850 p2.Param().Z() = c2.Z();
851 p2.Param().Err2Y() = c2.ErrY() * c2.ErrY();
852 p2.Param().Err2Z() = c2.ErrZ() * c2.ErrZ();
853 }
00d07bcd 854#endif
855}
856
857
f0bada7f 858GPUdi() void AliHLTTPCCATracker::GetErrors2( int iRow, float z, float sinPhi, float cosPhi, float DzDs, float &Err2Y, float &Err2Z ) const
693d2443 859{
2fba026d 860 //
861 // Use calibrated cluster error from OCDB
862 //
693d2443 863
2fba026d 864 fParam.GetClusterErrors2( iRow, z, sinPhi, cosPhi, DzDs, Err2Y, Err2Z );
865 Err2Y*=fParam.ClusterError2CorrectionY();
866 Err2Z*=fParam.ClusterError2CorrectionZ();
693d2443 867}
868
f0bada7f 869GPUdi() void AliHLTTPCCATracker::GetErrors2( int iRow, const AliHLTTPCCATrackParam &t, float &Err2Y, float &Err2Z ) const
00d07bcd 870{
2fba026d 871 //
872 // Use calibrated cluster error from OCDB
873 //
00d07bcd 874
2fba026d 875 fParam.GetClusterErrors2( iRow, t.GetZ(), t.SinPhi(), t.GetCosPhi(), t.DzDs(), Err2Y, Err2Z );
00d07bcd 876}
877
4687b8fc 878
fbb9b71b 879#if !defined(HLTCA_GPUCODE)
4687b8fc 880
fbb9b71b 881GPUh() void AliHLTTPCCATracker::WriteEvent( std::ostream &out )
00d07bcd 882{
2fba026d 883 // write event to the file
884 for ( int iRow = 0; iRow < fParam.NRows(); iRow++ ) {
885 out << fData.Row( iRow ).HitNumberOffset() << " " << fData.Row( iRow ).NHits() << std::endl;
886 }
887 out << NHitsTotal() << std::endl;
888
889 AliHLTResizableArray<float> y( NHitsTotal() ), z( NHitsTotal() );
890
891 for ( int iRow = 0; iRow < fParam.NRows(); iRow++ ) {
892 const AliHLTTPCCARow &row = Row( iRow );
893 float y0 = row.Grid().YMin();
894 float z0 = row.Grid().ZMin();
895 float stepY = row.HstepY();
896 float stepZ = row.HstepZ();
897 for ( int ih = 0; ih < fData.Row( iRow ).NHits(); ih++ ) {
898 int id = HitInputID( row, ih );
899 y[id] = y0 + HitDataY( row, ih ) * stepY;
900 z[id] = z0 + HitDataZ( row, ih ) * stepZ;
901 }
902 }
903 for ( int ih = 0; ih < NHitsTotal(); ih++ ) {
904 out << y[ih] << " " << z[ih] << std::endl;
905 }
fbb9b71b 906}
4687b8fc 907
5cb6ddd4 908GPUh() void AliHLTTPCCATracker::WriteTracks( std::ostream &/*out*/ )
4687b8fc 909{
2fba026d 910 //* Write tracks to file --- dummy
4687b8fc 911}
912
5cb6ddd4 913GPUh() void AliHLTTPCCATracker::ReadTracks( std::istream &/*in*/ )
4687b8fc 914{
2fba026d 915 //* Read tracks from file -- dummy
326c2d4b 916}
4687b8fc 917#endif