b179daecee1e99d49b0b2089727fc36c4a0082d5
[u/mrichter/AliRoot.git] / HLT / src / AliL3TrackArray.cxx
1 //$Id$
2
3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright &copy ULI
5
6 #include <math.h>
7 #include <string.h>
8 #include <iostream.h>
9 #include "AliL3Logging.h"
10 #include "AliL3TrackArray.h"
11 #include "AliL3HoughTrack.h"
12 #include "AliL3ModelTrack.h"
13 #include "AliL3ConfMapTrack.h"
14 #include "AliL3TrackSegmentData.h"
15 #include "AliL3Transform.h"
16 #include "AliL3ConfMapPoint.h"
17
18 //_____________________________________________________________
19 // AliL3TrackArray
20 //
21 // Track array class 
22 //
23
24 ClassImp(AliL3TrackArray)
25
26 AliL3TrackArray::AliL3TrackArray(){
27   //Default constructor
28   fSize = 0;
29   fNTracks=0;
30   fNAbsent=0;
31   fTrackType='t';
32   SetSize();
33 }
34
35
36 AliL3TrackArray::AliL3TrackArray(Int_t ntrack){
37   //Constructor.
38   fSize = 0;
39   fNTracks=0;
40   fNAbsent=0;
41   fTrackType='t';
42   SetSize(ntrack);
43 }
44
45 AliL3TrackArray::AliL3TrackArray(char* tracktype,Int_t ntrack){
46   //Constructor.
47   fSize = 0;
48   fNTracks=0;
49   fNAbsent=0;
50   if(strcmp(tracktype,"AliL3Track")==0) fTrackType='t';
51   if(strcmp(tracktype,"AliL3ConfMapTrack")==0) fTrackType='c';
52   if(strcmp(tracktype,"AliL3HoughTrack")==0) fTrackType='h';
53   if(strcmp(tracktype,"AliL3ModelTrack")==0) fTrackType='m';
54   SetSize(ntrack);
55 }
56
57 AliL3TrackArray::AliL3TrackArray(char* tracktype){
58   //Constructor.
59   fSize = 0;
60   fNTracks=0;
61   fNAbsent=0;
62   if(strcmp(tracktype,"AliL3Track")==0) fTrackType='t';
63   if(strcmp(tracktype,"AliL3ConfMapTrack")==0) fTrackType='c';
64   if(strcmp(tracktype,"AliL3HoughTrack")==0) fTrackType='h';
65   if(strcmp(tracktype,"AliL3ModelTrack")==0) fTrackType='m';
66   SetSize();
67 }
68
69
70 AliL3TrackArray::~AliL3TrackArray(){
71   //Destructor
72   DeleteArray();
73 }
74
75
76 AliL3Track *AliL3TrackArray::NextTrack(){
77   if(fNTracks<fSize) return fTrack[fNTracks++];
78   SetSize(fSize+100);
79    return fTrack[fNTracks++]; 
80 }
81
82 void AliL3TrackArray::DeleteArray(){
83   for(Int_t i=0; i<fSize;i++)
84     delete fTrack[i];
85   delete[] fIsPresent;
86   delete[] fTrack;
87 }
88
89 Bool_t AliL3TrackArray::SetSize(Int_t newsize){
90   if(newsize<=fSize) return kFALSE; //shrink comes later!! 
91   if(!fSize){
92     fSize = newsize;
93     fTrack = new AliL3Track*[fSize];
94     fIsPresent = new Bool_t[fSize];
95     switch(fTrackType){
96       case 't':
97         for(Int_t i=0;i<fSize;i++){
98           fTrack[i]   = new AliL3Track();
99           fIsPresent[i] = kTRUE;
100         }
101         break;
102       case 'c':  
103         for(Int_t i=0;i<fSize;i++){
104           fTrack[i]   = new AliL3ConfMapTrack();
105           fIsPresent[i] = kTRUE;
106         }
107         break;
108       case 'h':
109         for(Int_t i=0;i<fSize;i++){
110           fTrack[i]   = new AliL3HoughTrack();
111           fIsPresent[i] = kTRUE;
112         }
113         break;
114        case 'm':
115         for(Int_t i=0;i<fSize;i++){
116           fTrack[i]   = new AliL3ModelTrack();
117           fIsPresent[i] = kTRUE;
118         }
119         break;
120       default: 
121         return kFALSE;
122     }
123     return kTRUE;
124   }
125   AliL3Track **tmp = new AliL3Track*[fSize];
126   Bool_t *pre = new Bool_t[fSize];
127   for(Int_t i=0; i<fSize;i++){
128     tmp[i] = fTrack[i];
129     pre[i] = fIsPresent[i];
130   }
131   delete[]  fTrack;
132   delete[] fIsPresent;
133   fTrack =  new AliL3Track*[newsize];
134   fIsPresent = new Bool_t[newsize];
135   for(Int_t i=0; i<fSize;i++){
136     fTrack[i]   = tmp[i];
137     fIsPresent[i] = pre[i];
138   }
139   delete[] tmp;
140   delete[] pre;
141   switch(fTrackType){
142     case 't':
143       for(Int_t i=fSize;i<newsize;i++){
144         fTrack[i]   = new AliL3Track();
145         fIsPresent[i] = kTRUE;
146       }
147       break;
148     case 'c':  
149       for(Int_t i=fSize;i<newsize;i++){
150         fTrack[i]   = new AliL3ConfMapTrack();
151         fIsPresent[i] = kTRUE;
152       }
153       break;
154     case 'h':
155       for(Int_t i=fSize;i<newsize;i++){
156         fTrack[i]   = new AliL3HoughTrack();
157         fIsPresent[i] = kTRUE;
158       }
159       break;
160     case 'm':
161       for(Int_t i=fSize;i<newsize;i++){
162         fTrack[i]   = new AliL3ModelTrack();
163         fIsPresent[i] = kTRUE;
164       }
165       break;
166     default: 
167       return kFALSE;
168   }
169   fSize = newsize;
170   return kTRUE;
171 }
172
173 void AliL3TrackArray::Reset(){
174   fNTracks=0;
175   fNAbsent=0;
176   for(Int_t i=0; i<fSize;i++)
177     fIsPresent[i] = kTRUE; 
178 }
179
180 void AliL3TrackArray::Remove(Int_t track){
181   if(fIsPresent[track]){
182     fIsPresent[track]=kFALSE;
183     fNAbsent++;
184   }
185 }
186
187 void AliL3TrackArray::FillTracks(Int_t ntracks, AliL3TrackSegmentData* tr){
188   //Read tracks from shared memory (or memory)
189   AliL3TrackSegmentData *trs = tr;
190    for(Int_t i=0; i<ntracks; i++){
191     AliL3Track *track = NextTrack(); 
192     track->SetPt(trs->fPt);
193     track->SetPsi(trs->fPsi);
194     track->SetTgl(trs->fTgl);
195     track->SetNHits(trs->fNPoints);
196     track->SetCharge(trs->fCharge);
197     track->SetFirstPoint(trs->fX,trs->fY,trs->fZ);
198     track->SetLastPoint(trs->fLastX,trs->fLastY,trs->fLastZ);
199     track->SetHits( trs->fNPoints, trs->fPointIDs );
200     UChar_t *tmpP = (UChar_t*)trs;
201     tmpP += sizeof(AliL3TrackSegmentData)+trs->fNPoints*sizeof(UInt_t);
202     trs = (AliL3TrackSegmentData*)tmpP;
203   }
204 }
205
206 void AliL3TrackArray::FillTracks(Int_t ntracks, AliL3TrackSegmentData* tr,Int_t slice){
207   //Read tracks from shared memory (or memory)
208   AliL3TrackSegmentData *trs = tr;
209   for(Int_t i=0; i<ntracks; i++){
210     AliL3Track *track = NextTrack(); 
211     track->SetPt(trs->fPt);
212     Float_t psi[1];
213     psi[0]=trs->fPsi;
214     AliL3Transform::Local2GlobalAngle(psi,slice);
215     track->SetPsi(psi[0]);
216     track->SetTgl(trs->fTgl);
217     track->SetNHits(trs->fNPoints);
218     track->SetCharge(trs->fCharge);
219     Float_t first[3];
220     first[0]=trs->fX;first[1]=trs->fY;first[2]=trs->fZ;
221     AliL3Transform::Local2Global(first,slice);
222     track->SetFirstPoint(first[0],first[1],first[2]);
223     Float_t last[3];
224     last[0]=trs->fLastX;last[1]=trs->fLastY;last[2]=trs->fLastZ;
225     AliL3Transform::Local2Global(last,slice);
226     track->SetLastPoint(last[0],last[1],last[2]);
227     track->SetHits( trs->fNPoints, trs->fPointIDs );
228     UChar_t *tmpP = (UChar_t*)trs;
229     tmpP += sizeof(AliL3TrackSegmentData)+trs->fNPoints*sizeof(UInt_t);
230     trs = (AliL3TrackSegmentData*)tmpP;
231   }
232 }
233
234 UInt_t AliL3TrackArray::GetOutSize(){
235   UInt_t count = GetOutCount();   //use only present tracks
236   UInt_t tHits = 0;
237   for(Int_t i=0;i<fNTracks;i++){  //loop over all tracks
238     AliL3Track *track = GetCheckedTrack(i);  //use only present tracks
239     if(track)                                       //use only present tracks
240       tHits += track->GetNHits();
241   }
242
243   //calculate size of track
244   return count*sizeof(AliL3TrackSegmentData)+sizeof(UInt_t)*tHits;
245 }
246
247 UInt_t AliL3TrackArray::WriteTracks(UInt_t & ntracks,AliL3TrackSegmentData* tr){
248   ntracks = GetOutCount();
249   return WriteTracks(tr);
250 }
251
252 UInt_t AliL3TrackArray::WriteTracks(AliL3TrackSegmentData* tr){
253   if(GetTrackType()=='c') return WriteConfMapTracks(tr);
254   AliL3TrackSegmentData *tP = tr;
255   UInt_t *pP;
256   UInt_t size = 0;
257   for(Int_t i=0; i<fNTracks; i++){  //loop over all tracks
258     AliL3Track *track = GetCheckedTrack(i); //use only present tracks
259     if(!track) continue;                           //use only present tracks
260     tP->fX = track->GetFirstPointX();
261     tP->fY = track->GetFirstPointY();
262     tP->fZ = track->GetFirstPointZ();
263     tP->fPt = track->GetPt();
264     tP->fLastX = track->GetLastPointX();
265     tP->fLastY = track->GetLastPointY();
266     tP->fLastZ = track->GetLastPointZ();
267     tP->fPsi = track->GetPsi();
268     tP->fTgl = track->GetTgl();
269     tP->fCharge = track->GetCharge();
270     tP->fNPoints = track->GetNHits();
271     pP = (UInt_t*)track->GetHitNumbers();
272     for (UInt_t j=0;j<tP->fNPoints;j++){
273       tP->fPointIDs[j] = pP[j];
274     }
275     Byte_t *tmpP = (Byte_t *)tP;
276     tmpP += sizeof(AliL3TrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
277     size += sizeof(AliL3TrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
278     tP = (AliL3TrackSegmentData*)tmpP;
279   }
280   return size;
281 }
282
283 UInt_t AliL3TrackArray::WriteConfMapTracks(AliL3TrackSegmentData* tr){
284   // use first and last point objects
285   AliL3TrackSegmentData *tP = tr;
286   UInt_t *pP;
287   UInt_t size = 0;
288   for(Int_t i=0; i<fNTracks; i++){  //loop over all tracks
289     AliL3ConfMapTrack *track =(AliL3ConfMapTrack *) GetCheckedTrack(i); //use only present tracks
290     if(!track) continue;                           //use only present tracks
291     AliL3ConfMapPoint *hit = (AliL3ConfMapPoint*)track->lastHit;
292     AliL3ConfMapPoint *lastHit = (AliL3ConfMapPoint*)track->firstHit;
293     tP->fX = hit->GetX();
294     tP->fY = hit->GetY();
295     tP->fZ = hit->GetZ();
296     tP->fLastX = lastHit->GetX();
297     tP->fLastY = lastHit->GetY();
298     tP->fLastZ = lastHit->GetZ();
299    
300 //    tP->fX = track->GetFirstPointX();
301 //    tP->fY = track->GetFirstPointY();
302 //    tP->fZ = track->GetFirstPointZ();
303     tP->fPt = track->GetPt();
304 //    tP->fLastX = track->GetLastPointX();
305 //    tP->fLastY = track->GetLastPointY();
306 //    tP->fLastZ = track->GetLastPointZ();
307     tP->fPsi = track->GetPsi();
308     tP->fTgl = track->GetTgl();
309     tP->fCharge = track->GetCharge();
310     tP->fNPoints = track->GetNHits();
311     pP = (UInt_t*)track->GetHitNumbers();
312     for (UInt_t j=0;j<tP->fNPoints;j++){
313       tP->fPointIDs[j] = pP[j];
314     }
315     Byte_t *tmpP = (Byte_t *)tP;
316     tmpP += sizeof(AliL3TrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
317     size +=sizeof(AliL3TrackSegmentData)+tP->fNPoints*sizeof(UInt_t);
318     tP = (AliL3TrackSegmentData*)tmpP;
319   }
320   return size;
321 }
322
323 void AliL3TrackArray::AddLast(AliL3Track *track)
324 {
325   AliL3Track *tpt = NextTrack();
326   tpt->Set(track);
327   
328 }
329
330
331 void AliL3TrackArray::AddTracks(AliL3TrackArray *newtrack,Bool_t remove_old,Int_t slice)
332 {
333   if(GetTrackType() != newtrack->GetTrackType() && GetTrackType()!='t')
334     {
335       LOG(AliL3Log::kError,"AliL3TrackArray::AddTracks","Track types")
336         <<"Bad idea to add tracks of different types"<<ENDLOG;
337       return;
338     }
339   if(fSize < fNTracks+newtrack->GetNPresent())
340     SetSize(fSize+newtrack->GetSize());
341   for(Int_t i =0;i<newtrack->GetNTracks();i++){
342     AliL3Track *tpt = newtrack->GetCheckedTrack(i);
343     if(!tpt) continue;
344     if(remove_old)
345       newtrack->Remove(i);
346     AliL3Track *track = NextTrack();
347     track->Set(tpt);
348     if(slice>=0)
349       track->Rotate(slice); //Rotate track to global coordinates
350     /*
351       AliL3Track *track;
352       if(GetTrackType()=='h')
353       track = (AliL3HoughTrack*)NextTrack();
354       else
355       track = NextTrack();
356       track->Set(tpt);
357     */
358   }
359 }
360
361
362 void AliL3TrackArray::Compress(){
363   if(GetNPresent()==GetNTracks()) return;
364   AliL3Track **tmp =  new AliL3Track *[fNTracks];
365   Int_t present=0;
366   Int_t absent=GetNPresent();
367   for(Int_t i=0;i<GetNTracks();i++){
368     if(fIsPresent[i]) tmp[present++] = fTrack[i];
369     else tmp[absent++] = fTrack[i];
370   }
371   for(Int_t i=0;i<GetNTracks();i++)
372     fIsPresent[i]=kTRUE;
373
374   //Copy pointers back
375   for(Int_t i=0; i<GetNTracks();i++){
376     fTrack[i]=tmp[i];
377   }
378
379   delete[] tmp;
380
381   fNTracks = GetNPresent();
382   fNAbsent = 0;
383 }
384
385 void AliL3TrackArray::QSort(){
386   // compress an sort
387   Compress();
388   QSort(fTrack,0,fNTracks);
389 }
390
391 void AliL3TrackArray::QSort( AliL3Track **a, Int_t first, Int_t last){
392
393    // Sort array of AliL3Track pointers using a quicksort algorithm.
394    // Uses TrackCompare() to compare objects.
395    // Thanks to Root! 
396
397    static AliL3Track *tmp;
398    static int i;           // "static" to save stack space
399    int j;
400
401    while (last - first > 1) {
402       i = first;
403       j = last;
404       for (;;) {
405         while (++i < last && TrackCompare(a[i], a[first]) < 0)
406           ;
407         while (--j > first && TrackCompare(a[j], a[first]) > 0)
408           ;
409          if (i >= j)
410             break;
411
412          tmp  = a[i];
413          a[i] = a[j];
414          a[j] = tmp;
415       }
416       if (j == first) {
417          ++first;
418          continue;
419       }
420       tmp = a[first];
421       a[first] = a[j];
422       a[j] = tmp;
423       if (j - first < last - (j + 1)) {
424          QSort(a, first, j);
425          first = j + 1;   // QSort(j + 1, last);
426       } else {
427          QSort(a, j + 1, last);
428          last = j;        // QSort(first, j);
429       }
430    }
431 }
432
433 Int_t AliL3TrackArray::TrackCompare(AliL3Track *a, AliL3Track *b){
434    // Compare the two tracks.
435   
436   return b->Compare(a);
437   
438   /*
439     if(fTrackType=='h')
440     {
441     AliL3HoughTrack *tra = (AliL3HoughTrack*)a;
442     AliL3HoughTrack *trb = (AliL3HoughTrack*)b;
443     if(tra->GetWeight() < trb->GetWeight()) return 1;
444     if(tra->GetWeight() > trb->GetWeight()) return -1;
445     }
446     else
447     {
448     if(a->GetNHits() < b->GetNHits()) return 1;
449     if(a->GetNHits() > b->GetNHits()) return -1;
450     }
451     
452     return 0;
453   */
454 }
455
456