L3 becomes HLT
[u/mrichter/AliRoot.git] / HLT / src / AliHLTTrackArray.cxx
CommitLineData
3e87ef69 1// @(#) $Id$
95a00d93 2
71df269a 3// Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
3e87ef69 4//*-- Copyright &copy ALICE HLT Group
108615fc 5
4aa41877 6#include "AliHLTStandardIncludes.h"
7
8#include "AliHLTLogging.h"
9#include "AliHLTTrackArray.h"
10#include "AliHLTHoughTrack.h"
11#include "AliHLTModelTrack.h"
12#include "AliHLTConfMapTrack.h"
13#include "AliHLTTrackSegmentData.h"
14#include "AliHLTTransform.h"
15#include "AliHLTConfMapPoint.h"
16
17/** \class AliHLTTrackArray
3e87ef69 18<pre>
108615fc 19//_____________________________________________________________
4aa41877 20// AliHLTTrackArray
108615fc 21//
71df269a 22// Track array class
108615fc 23//
3e87ef69 24</pre>
25*/
108615fc 26
5929c18d 27#if __GNUC__ >= 3
28using namespace std;
29#endif
30
4aa41877 31ClassImp(AliHLTTrackArray)
108615fc 32
4aa41877 33AliHLTTrackArray::AliHLTTrackArray()
b1ed0288 34{
108615fc 35 //Default constructor
36 fSize = 0;
37 fNTracks=0;
38 fNAbsent=0;
39 fTrackType='t';
40 SetSize();
41}
42
43
4aa41877 44AliHLTTrackArray::AliHLTTrackArray(Int_t ntrack)
b1ed0288 45{
108615fc 46 //Constructor.
47 fSize = 0;
48 fNTracks=0;
49 fNAbsent=0;
50 fTrackType='t';
51 SetSize(ntrack);
52}
53
4aa41877 54AliHLTTrackArray::AliHLTTrackArray(char* tracktype,Int_t ntrack)
b1ed0288 55{
108615fc 56 //Constructor.
57 fSize = 0;
58 fNTracks=0;
59 fNAbsent=0;
4aa41877 60 if(strcmp(tracktype,"AliHLTTrack")==0) fTrackType='t';
61 if(strcmp(tracktype,"AliHLTConfMapTrack")==0) fTrackType='c';
62 if(strcmp(tracktype,"AliHLTHoughTrack")==0) fTrackType='h';
63 if(strcmp(tracktype,"AliHLTModelTrack")==0) fTrackType='m';
108615fc 64 SetSize(ntrack);
65}
66
4aa41877 67AliHLTTrackArray::AliHLTTrackArray(char* tracktype)
b1ed0288 68{
108615fc 69 //Constructor.
70 fSize = 0;
71 fNTracks=0;
72 fNAbsent=0;
4aa41877 73 if(strcmp(tracktype,"AliHLTTrack")==0) fTrackType='t';
74 if(strcmp(tracktype,"AliHLTConfMapTrack")==0) fTrackType='c';
75 if(strcmp(tracktype,"AliHLTHoughTrack")==0) fTrackType='h';
76 if(strcmp(tracktype,"AliHLTModelTrack")==0) fTrackType='m';
108615fc 77 SetSize();
78}
79
4aa41877 80AliHLTTrackArray::~AliHLTTrackArray()
b1ed0288 81{
108615fc 82 //Destructor
83 DeleteArray();
84}
85
86
4aa41877 87AliHLTTrack *AliHLTTrackArray::NextTrack()
b1ed0288 88{
89 //next track in array
108615fc 90 if(fNTracks<fSize) return fTrack[fNTracks++];
91 SetSize(fSize+100);
92 return fTrack[fNTracks++];
93}
94
4aa41877 95void AliHLTTrackArray::DeleteArray()
b1ed0288 96{
97 //delete array
108615fc 98 for(Int_t i=0; i<fSize;i++)
99 delete fTrack[i];
100 delete[] fIsPresent;
101 delete[] fTrack;
102}
103
4aa41877 104Bool_t AliHLTTrackArray::SetSize(Int_t newsize)
b1ed0288 105{
106 //set size
108615fc 107 if(newsize<=fSize) return kFALSE; //shrink comes later!!
108 if(!fSize){
109 fSize = newsize;
4aa41877 110 fTrack = new AliHLTTrack*[fSize];
108615fc 111 fIsPresent = new Bool_t[fSize];
112 switch(fTrackType){
113 case 't':
114 for(Int_t i=0;i<fSize;i++){
4aa41877 115 fTrack[i] = new AliHLTTrack();
108615fc 116 fIsPresent[i] = kTRUE;
117 }
118 break;
119 case 'c':
120 for(Int_t i=0;i<fSize;i++){
4aa41877 121 fTrack[i] = new AliHLTConfMapTrack();
108615fc 122 fIsPresent[i] = kTRUE;
123 }
124 break;
125 case 'h':
126 for(Int_t i=0;i<fSize;i++){
4aa41877 127 fTrack[i] = new AliHLTHoughTrack();
108615fc 128 fIsPresent[i] = kTRUE;
129 }
130 break;
95a00d93 131 case 'm':
132 for(Int_t i=0;i<fSize;i++){
4aa41877 133 fTrack[i] = new AliHLTModelTrack();
95a00d93 134 fIsPresent[i] = kTRUE;
135 }
136 break;
108615fc 137 default:
138 return kFALSE;
139 }
140 return kTRUE;
141 }
4aa41877 142 AliHLTTrack **tmp = new AliHLTTrack*[fSize];
108615fc 143 Bool_t *pre = new Bool_t[fSize];
144 for(Int_t i=0; i<fSize;i++){
145 tmp[i] = fTrack[i];
146 pre[i] = fIsPresent[i];
147 }
148 delete[] fTrack;
149 delete[] fIsPresent;
4aa41877 150 fTrack = new AliHLTTrack*[newsize];
108615fc 151 fIsPresent = new Bool_t[newsize];
152 for(Int_t i=0; i<fSize;i++){
153 fTrack[i] = tmp[i];
154 fIsPresent[i] = pre[i];
155 }
156 delete[] tmp;
157 delete[] pre;
158 switch(fTrackType){
159 case 't':
160 for(Int_t i=fSize;i<newsize;i++){
4aa41877 161 fTrack[i] = new AliHLTTrack();
108615fc 162 fIsPresent[i] = kTRUE;
163 }
164 break;
165 case 'c':
166 for(Int_t i=fSize;i<newsize;i++){
4aa41877 167 fTrack[i] = new AliHLTConfMapTrack();
108615fc 168 fIsPresent[i] = kTRUE;
169 }
170 break;
171 case 'h':
172 for(Int_t i=fSize;i<newsize;i++){
4aa41877 173 fTrack[i] = new AliHLTHoughTrack();
108615fc 174 fIsPresent[i] = kTRUE;
175 }
176 break;
95a00d93 177 case 'm':
178 for(Int_t i=fSize;i<newsize;i++){
4aa41877 179 fTrack[i] = new AliHLTModelTrack();
95a00d93 180 fIsPresent[i] = kTRUE;
181 }
182 break;
108615fc 183 default:
184 return kFALSE;
185 }
186 fSize = newsize;
187 return kTRUE;
188}
189
4aa41877 190void AliHLTTrackArray::Reset()
b1ed0288 191{
192 //reset
108615fc 193 fNTracks=0;
194 fNAbsent=0;
195 for(Int_t i=0; i<fSize;i++)
196 fIsPresent[i] = kTRUE;
197}
198
4aa41877 199void AliHLTTrackArray::Remove(Int_t track)
b1ed0288 200{
201 //remove track
108615fc 202 if(fIsPresent[track]){
203 fIsPresent[track]=kFALSE;
204 fNAbsent++;
205 }
206}
207
4aa41877 208void AliHLTTrackArray::FillTracks(Int_t ntracks, AliHLTTrackSegmentData* tr){
108615fc 209 //Read tracks from shared memory (or memory)
4aa41877 210 AliHLTTrackSegmentData *trs = tr;
95a00d93 211 for(Int_t i=0; i<ntracks; i++){
4aa41877 212 AliHLTTrack *track = NextTrack();
108615fc 213 track->SetPt(trs->fPt);
214 track->SetPsi(trs->fPsi);
215 track->SetTgl(trs->fTgl);
f644512a 216 track->SetPterr(trs->fPterr);
217 track->SetPsierr(trs->fPsierr);
218 track->SetTglerr(trs->fTglerr);
108615fc 219 track->SetNHits(trs->fNPoints);
220 track->SetCharge(trs->fCharge);
221 track->SetFirstPoint(trs->fX,trs->fY,trs->fZ);
222 track->SetLastPoint(trs->fLastX,trs->fLastY,trs->fLastZ);
223 track->SetHits( trs->fNPoints, trs->fPointIDs );
de3c3890 224#ifdef ROWHOUGHPARAMS
0bd0c1ef 225 if(GetTrackType()=='h') {
4aa41877 226 ((AliHLTHoughTrack *)track)->SetWeight(trs->fWeight);
227 ((AliHLTHoughTrack *)track)->SetBinXY(trs->fBinX,trs->fBinY,trs->fBinXSize,trs->fBinYSize);
0bd0c1ef 228 }
de3c3890 229 track->SetMCid(trs->fTrackID);
230 track->SetRowRange(trs->fRowRange1,trs->fRowRange2);
231 track->SetSector(trs->fSector);
232 track->SetPID(trs->fPID);
0bd0c1ef 233#endif
108615fc 234 UChar_t *tmpP = (UChar_t*)trs;
4aa41877 235 tmpP += sizeof(AliHLTTrackSegmentData)+trs->fNPoints*sizeof(UInt_t);
236 trs = (AliHLTTrackSegmentData*)tmpP;
108615fc 237 }
238}
239
4aa41877 240void AliHLTTrackArray::FillTracks(Int_t ntracks, AliHLTTrackSegmentData* tr,Int_t slice)
b1ed0288 241{
108615fc 242 //Read tracks from shared memory (or memory)
4aa41877 243 AliHLTTrackSegmentData *trs = tr;
108615fc 244 for(Int_t i=0; i<ntracks; i++){
4aa41877 245 AliHLTTrack *track = NextTrack();
108615fc 246 track->SetPt(trs->fPt);
f644512a 247 track->SetPterr(trs->fPterr);
108615fc 248 Float_t psi[1];
249 psi[0]=trs->fPsi;
4aa41877 250 AliHLTTransform::Local2GlobalAngle(psi,slice);
108615fc 251 track->SetPsi(psi[0]);
252 track->SetTgl(trs->fTgl);
f644512a 253 track->SetPsierr(trs->fPsierr);
254 track->SetTglerr(trs->fTglerr);
108615fc 255 track->SetNHits(trs->fNPoints);
256 track->SetCharge(trs->fCharge);
257 Float_t first[3];
258 first[0]=trs->fX;first[1]=trs->fY;first[2]=trs->fZ;
4aa41877 259 AliHLTTransform::Local2Global(first,slice);
108615fc 260 track->SetFirstPoint(first[0],first[1],first[2]);
261 Float_t last[3];
262 last[0]=trs->fLastX;last[1]=trs->fLastY;last[2]=trs->fLastZ;
4aa41877 263 AliHLTTransform::Local2Global(last,slice);
108615fc 264 track->SetLastPoint(last[0],last[1],last[2]);
265 track->SetHits( trs->fNPoints, trs->fPointIDs );
de3c3890 266#ifdef ROWHOUGHPARAMS
0bd0c1ef 267 if(GetTrackType()=='h') {
4aa41877 268 ((AliHLTHoughTrack *)track)->SetWeight(trs->fWeight);
269 ((AliHLTHoughTrack *)track)->SetBinXY(trs->fBinX,trs->fBinY,trs->fBinXSize,trs->fBinYSize);
0bd0c1ef 270 }
de3c3890 271 track->SetMCid(trs->fTrackID);
272 track->SetRowRange(trs->fRowRange1,trs->fRowRange2);
273 track->SetSector(slice);
274 track->SetPID(trs->fPID);
0bd0c1ef 275#endif
1242f919 276 UChar_t *tmpP = (UChar_t*)trs;
4aa41877 277 tmpP += sizeof(AliHLTTrackSegmentData)+trs->fNPoints*sizeof(UInt_t);
278 trs = (AliHLTTrackSegmentData*)tmpP;
108615fc 279 }
280}
281
4aa41877 282UInt_t AliHLTTrackArray::GetOutSize()
b1ed0288 283{
284 //get size for IO
108615fc 285 UInt_t count = GetOutCount(); //use only present tracks
286 UInt_t tHits = 0;
287 for(Int_t i=0;i<fNTracks;i++){ //loop over all tracks
4aa41877 288 AliHLTTrack *track = GetCheckedTrack(i); //use only present tracks
108615fc 289 if(track) //use only present tracks
290 tHits += track->GetNHits();
291 }
292
293 //calculate size of track
4aa41877 294 return count*sizeof(AliHLTTrackSegmentData)+sizeof(UInt_t)*tHits;
108615fc 295}
296
4aa41877 297UInt_t AliHLTTrackArray::WriteTracks(UInt_t & ntracks,AliHLTTrackSegmentData* tr)
b1ed0288 298{
299 //write tracks
108615fc 300 ntracks = GetOutCount();
301 return WriteTracks(tr);
302}
303
4aa41877 304UInt_t AliHLTTrackArray::WriteTracks(AliHLTTrackSegmentData* tr)
b1ed0288 305{
1f1942b8 306 //if(GetTrackType()=='c') return WriteConfMapTracks(tr);
4aa41877 307 AliHLTTrackSegmentData *tP = tr;
108615fc 308 UInt_t *pP;
309 UInt_t size = 0;
310 for(Int_t i=0; i<fNTracks; i++){ //loop over all tracks
4aa41877 311 AliHLTTrack *track = GetCheckedTrack(i); //use only present tracks
108615fc 312 if(!track) continue; //use only present tracks
313 tP->fX = track->GetFirstPointX();
314 tP->fY = track->GetFirstPointY();
315 tP->fZ = track->GetFirstPointZ();
316 tP->fPt = track->GetPt();
f644512a 317 tP->fPterr = track->GetPterr();
108615fc 318 tP->fLastX = track->GetLastPointX();
319 tP->fLastY = track->GetLastPointY();
320 tP->fLastZ = track->GetLastPointZ();
321 tP->fPsi = track->GetPsi();
322 tP->fTgl = track->GetTgl();
f644512a 323 tP->fPsierr = track->GetPsierr();
324 tP->fTglerr = track->GetTglerr();
108615fc 325 tP->fCharge = track->GetCharge();
326 tP->fNPoints = track->GetNHits();
de3c3890 327#ifdef ROWHOUGHPARAMS
0bd0c1ef 328 if(GetTrackType()=='h') {
4aa41877 329 tP->fWeight = ((AliHLTHoughTrack *)track)->GetWeight();
330 tP->fBinX = ((AliHLTHoughTrack *)track)->GetBinX();
331 tP->fBinY = ((AliHLTHoughTrack *)track)->GetBinY();
332 tP->fBinXSize = ((AliHLTHoughTrack *)track)->GetSizeX();
333 tP->fBinYSize = ((AliHLTHoughTrack *)track)->GetSizeY();
0bd0c1ef 334 }
de3c3890 335 tP->fTrackID = track->GetMCid();
336 tP->fRowRange1 = track->GetFirstRow();
337 tP->fRowRange2 = track->GetLastRow();
338 tP->fSector = track->GetSector();
339 tP->fPID = track->GetPID();
0bd0c1ef 340#endif
108615fc 341 pP = (UInt_t*)track->GetHitNumbers();
342 for (UInt_t j=0;j<tP->fNPoints;j++){
343 tP->fPointIDs[j] = pP[j];
344 }
345 Byte_t *tmpP = (Byte_t *)tP;
4aa41877 346 tmpP += sizeof(AliHLTTrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
347 size += sizeof(AliHLTTrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
348 tP = (AliHLTTrackSegmentData*)tmpP;
108615fc 349 }
350 return size;
351}
352
4aa41877 353UInt_t AliHLTTrackArray::WriteConfMapTracks(AliHLTTrackSegmentData* tr)
b1ed0288 354{
108615fc 355 // use first and last point objects
4aa41877 356 AliHLTTrackSegmentData *tP = tr;
108615fc 357 UInt_t *pP;
358 UInt_t size = 0;
359 for(Int_t i=0; i<fNTracks; i++){ //loop over all tracks
4aa41877 360 AliHLTConfMapTrack *track =(AliHLTConfMapTrack *) GetCheckedTrack(i); //use only present tracks
108615fc 361 if(!track) continue; //use only present tracks
4aa41877 362 AliHLTConfMapPoint *hit = (AliHLTConfMapPoint*)track->GetLastHit();
363 AliHLTConfMapPoint *lastHit = (AliHLTConfMapPoint*)track->GetFirstHit();
108615fc 364 tP->fX = hit->GetX();
365 tP->fY = hit->GetY();
366 tP->fZ = hit->GetZ();
367 tP->fLastX = lastHit->GetX();
368 tP->fLastY = lastHit->GetY();
369 tP->fLastZ = lastHit->GetZ();
370
371// tP->fX = track->GetFirstPointX();
372// tP->fY = track->GetFirstPointY();
373// tP->fZ = track->GetFirstPointZ();
374 tP->fPt = track->GetPt();
f644512a 375 tP->fPterr = track->GetPterr();
108615fc 376// tP->fLastX = track->GetLastPointX();
377// tP->fLastY = track->GetLastPointY();
378// tP->fLastZ = track->GetLastPointZ();
379 tP->fPsi = track->GetPsi();
380 tP->fTgl = track->GetTgl();
f644512a 381 tP->fPsierr = track->GetPsierr();
382 tP->fTglerr = track->GetTglerr();
108615fc 383 tP->fCharge = track->GetCharge();
de3c3890 384#ifdef ROWHOUGHPARAMS
385 tP->fTrackID = track->GetMCid();
386 tP->fRowRange1 = track->GetFirstRow();
387 tP->fRowRange2 = track->GetLastRow();
388 tP->fSector = track->GetSector();
389 tP->fPID = track->GetPID();
390#endif
108615fc 391 tP->fNPoints = track->GetNHits();
392 pP = (UInt_t*)track->GetHitNumbers();
393 for (UInt_t j=0;j<tP->fNPoints;j++){
394 tP->fPointIDs[j] = pP[j];
395 }
396 Byte_t *tmpP = (Byte_t *)tP;
4aa41877 397 tmpP += sizeof(AliHLTTrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
398 size +=sizeof(AliHLTTrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
399 tP = (AliHLTTrackSegmentData*)tmpP;
108615fc 400 }
401 return size;
402}
403
4aa41877 404void AliHLTTrackArray::AddLast(AliHLTTrack *track)
2e843b99 405{
b1ed0288 406 //add track to last position
4aa41877 407 AliHLTTrack *tpt = NextTrack();
2e843b99 408 tpt->Set(track);
44fd1eb4 409
2e843b99 410}
411
4aa41877 412void AliHLTTrackArray::AddTracks(AliHLTTrackArray *newtrack,Bool_t remove_old,Int_t slice)
71df269a 413{
b1ed0288 414 //add tracks
d5fe1d26 415 if(GetTrackType() != newtrack->GetTrackType() && GetTrackType()!='t')
416 {
4aa41877 417 LOG(AliHLTLog::kError,"AliHLTTrackArray::AddTracks","Track types")
d5fe1d26 418 <<"Bad idea to add tracks of different types"<<ENDLOG;
419 return;
420 }
108615fc 421 if(fSize < fNTracks+newtrack->GetNPresent())
422 SetSize(fSize+newtrack->GetSize());
423 for(Int_t i =0;i<newtrack->GetNTracks();i++){
4aa41877 424 AliHLTTrack *tpt = newtrack->GetCheckedTrack(i);
108615fc 425 if(!tpt) continue;
15846008 426 if(remove_old)
427 newtrack->Remove(i);
4aa41877 428 AliHLTTrack *track = NextTrack();
ce54fcc8 429 track->Set(tpt);
71df269a 430 if(slice>=0)
431 track->Rotate(slice); //Rotate track to global coordinates
203925a9 432 /*
4aa41877 433 AliHLTTrack *track;
203925a9 434 if(GetTrackType()=='h')
4aa41877 435 track = (AliHLTHoughTrack*)NextTrack();
203925a9 436 else
437 track = NextTrack();
438 track->Set(tpt);
439 */
108615fc 440 }
441}
442
4aa41877 443void AliHLTTrackArray::Compress()
b1ed0288 444{
445 //compress array
108615fc 446 if(GetNPresent()==GetNTracks()) return;
4aa41877 447 AliHLTTrack **tmp = new AliHLTTrack *[fNTracks];
108615fc 448 Int_t present=0;
449 Int_t absent=GetNPresent();
450 for(Int_t i=0;i<GetNTracks();i++){
451 if(fIsPresent[i]) tmp[present++] = fTrack[i];
452 else tmp[absent++] = fTrack[i];
453 }
454 for(Int_t i=0;i<GetNTracks();i++)
455 fIsPresent[i]=kTRUE;
456
457 //Copy pointers back
458 for(Int_t i=0; i<GetNTracks();i++){
459 fTrack[i]=tmp[i];
460 }
461
462 delete[] tmp;
463
464 fNTracks = GetNPresent();
465 fNAbsent = 0;
466}
467
4aa41877 468void AliHLTTrackArray::QSort()
b1ed0288 469{
470 // compress and sort
108615fc 471 Compress();
472 QSort(fTrack,0,fNTracks);
473}
474
4aa41877 475void AliHLTTrackArray::QSort( AliHLTTrack **a, Int_t first, Int_t last)
b1ed0288 476{
4aa41877 477 // Sort array of AliHLTTrack pointers using a quicksort algorithm.
108615fc 478 // Uses TrackCompare() to compare objects.
479 // Thanks to Root!
480
4aa41877 481 static AliHLTTrack *tmp;
108615fc 482 static int i; // "static" to save stack space
483 int j;
484
485 while (last - first > 1) {
486 i = first;
487 j = last;
488 for (;;) {
203925a9 489 while (++i < last && TrackCompare(a[i], a[first]) < 0)
490 ;
491 while (--j > first && TrackCompare(a[j], a[first]) > 0)
492 ;
108615fc 493 if (i >= j)
494 break;
495
496 tmp = a[i];
497 a[i] = a[j];
498 a[j] = tmp;
499 }
500 if (j == first) {
501 ++first;
502 continue;
503 }
504 tmp = a[first];
505 a[first] = a[j];
506 a[j] = tmp;
507 if (j - first < last - (j + 1)) {
508 QSort(a, first, j);
509 first = j + 1; // QSort(j + 1, last);
510 } else {
511 QSort(a, j + 1, last);
512 last = j; // QSort(first, j);
513 }
514 }
515}
516
4aa41877 517Int_t AliHLTTrackArray::TrackCompare(AliHLTTrack *a, AliHLTTrack *b) const
b1ed0288 518{
108615fc 519 // Compare the two tracks.
203925a9 520
521 return b->Compare(a);
522
523 /*
524 if(fTrackType=='h')
525 {
4aa41877 526 AliHLTHoughTrack *tra = (AliHLTHoughTrack*)a;
527 AliHLTHoughTrack *trb = (AliHLTHoughTrack*)b;
203925a9 528 if(tra->GetWeight() < trb->GetWeight()) return 1;
529 if(tra->GetWeight() > trb->GetWeight()) return -1;
530 }
531 else
532 {
533 if(a->GetNHits() < b->GetNHits()) return 1;
534 if(a->GetNHits() > b->GetNHits()) return -1;
535 }
536
537 return 0;
538 */
108615fc 539}
540
541