Changes done to make the Cluser Finder calculate the errors in Pad and Time direction...
[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 AliL3TrackArray::~AliL3TrackArray(){
70   //Destructor
71   DeleteArray();
72 }
73
74
75 AliL3Track *AliL3TrackArray::NextTrack(){
76   if(fNTracks<fSize) return fTrack[fNTracks++];
77   SetSize(fSize+100);
78    return fTrack[fNTracks++]; 
79 }
80
81 void AliL3TrackArray::DeleteArray(){
82   for(Int_t i=0; i<fSize;i++)
83     delete fTrack[i];
84   delete[] fIsPresent;
85   delete[] fTrack;
86 }
87
88 Bool_t AliL3TrackArray::SetSize(Int_t newsize){
89   if(newsize<=fSize) return kFALSE; //shrink comes later!! 
90   if(!fSize){
91     fSize = newsize;
92     fTrack = new AliL3Track*[fSize];
93     fIsPresent = new Bool_t[fSize];
94     switch(fTrackType){
95       case 't':
96         for(Int_t i=0;i<fSize;i++){
97           fTrack[i]   = new AliL3Track();
98           fIsPresent[i] = kTRUE;
99         }
100         break;
101       case 'c':  
102         for(Int_t i=0;i<fSize;i++){
103           fTrack[i]   = new AliL3ConfMapTrack();
104           fIsPresent[i] = kTRUE;
105         }
106         break;
107       case 'h':
108         for(Int_t i=0;i<fSize;i++){
109           fTrack[i]   = new AliL3HoughTrack();
110           fIsPresent[i] = kTRUE;
111         }
112         break;
113        case 'm':
114         for(Int_t i=0;i<fSize;i++){
115           fTrack[i]   = new AliL3ModelTrack();
116           fIsPresent[i] = kTRUE;
117         }
118         break;
119       default: 
120         return kFALSE;
121     }
122     return kTRUE;
123   }
124   AliL3Track **tmp = new AliL3Track*[fSize];
125   Bool_t *pre = new Bool_t[fSize];
126   for(Int_t i=0; i<fSize;i++){
127     tmp[i] = fTrack[i];
128     pre[i] = fIsPresent[i];
129   }
130   delete[]  fTrack;
131   delete[] fIsPresent;
132   fTrack =  new AliL3Track*[newsize];
133   fIsPresent = new Bool_t[newsize];
134   for(Int_t i=0; i<fSize;i++){
135     fTrack[i]   = tmp[i];
136     fIsPresent[i] = pre[i];
137   }
138   delete[] tmp;
139   delete[] pre;
140   switch(fTrackType){
141     case 't':
142       for(Int_t i=fSize;i<newsize;i++){
143         fTrack[i]   = new AliL3Track();
144         fIsPresent[i] = kTRUE;
145       }
146       break;
147     case 'c':  
148       for(Int_t i=fSize;i<newsize;i++){
149         fTrack[i]   = new AliL3ConfMapTrack();
150         fIsPresent[i] = kTRUE;
151       }
152       break;
153     case 'h':
154       for(Int_t i=fSize;i<newsize;i++){
155         fTrack[i]   = new AliL3HoughTrack();
156         fIsPresent[i] = kTRUE;
157       }
158       break;
159     case 'm':
160       for(Int_t i=fSize;i<newsize;i++){
161         fTrack[i]   = new AliL3ModelTrack();
162         fIsPresent[i] = kTRUE;
163       }
164       break;
165     default: 
166       return kFALSE;
167   }
168   fSize = newsize;
169   return kTRUE;
170 }
171
172 void AliL3TrackArray::Reset(){
173   fNTracks=0;
174   fNAbsent=0;
175   for(Int_t i=0; i<fSize;i++)
176     fIsPresent[i] = kTRUE; 
177 }
178
179 void AliL3TrackArray::Remove(Int_t track){
180   if(fIsPresent[track]){
181     fIsPresent[track]=kFALSE;
182     fNAbsent++;
183   }
184 }
185
186 void AliL3TrackArray::FillTracks(Int_t ntracks, AliL3TrackSegmentData* tr){
187   //Read tracks from shared memory (or memory)
188   AliL3TrackSegmentData *trs = tr;
189    for(Int_t i=0; i<ntracks; i++){
190     AliL3Track *track = NextTrack(); 
191     track->SetPt(trs->fPt);
192     track->SetPsi(trs->fPsi);
193     track->SetTgl(trs->fTgl);
194     track->SetNHits(trs->fNPoints);
195     track->SetCharge(trs->fCharge);
196     track->SetFirstPoint(trs->fX,trs->fY,trs->fZ);
197     track->SetLastPoint(trs->fLastX,trs->fLastY,trs->fLastZ);
198     track->SetHits( trs->fNPoints, trs->fPointIDs );
199     UChar_t *tmpP = (UChar_t*)trs;
200     tmpP += sizeof(AliL3TrackSegmentData)+trs->fNPoints*sizeof(UInt_t);
201     trs = (AliL3TrackSegmentData*)tmpP;
202   }
203 }
204
205 void AliL3TrackArray::FillTracks(Int_t ntracks, AliL3TrackSegmentData* tr,Int_t slice){
206   //Read tracks from shared memory (or memory)
207   AliL3TrackSegmentData *trs = tr;
208   for(Int_t i=0; i<ntracks; i++){
209     AliL3Track *track = NextTrack(); 
210     track->SetPt(trs->fPt);
211     Float_t psi[1];
212     psi[0]=trs->fPsi;
213     AliL3Transform::Local2GlobalAngle(psi,slice);
214     track->SetPsi(psi[0]);
215     track->SetTgl(trs->fTgl);
216     track->SetNHits(trs->fNPoints);
217     track->SetCharge(trs->fCharge);
218     Float_t first[3];
219     first[0]=trs->fX;first[1]=trs->fY;first[2]=trs->fZ;
220     AliL3Transform::Local2Global(first,slice);
221     track->SetFirstPoint(first[0],first[1],first[2]);
222     Float_t last[3];
223     last[0]=trs->fLastX;last[1]=trs->fLastY;last[2]=trs->fLastZ;
224     AliL3Transform::Local2Global(last,slice);
225     track->SetLastPoint(last[0],last[1],last[2]);
226     track->SetHits( trs->fNPoints, trs->fPointIDs );
227     UChar_t *tmpP = (UChar_t*)trs;
228     tmpP += sizeof(AliL3TrackSegmentData)+trs->fNPoints*sizeof(UInt_t);
229     trs = (AliL3TrackSegmentData*)tmpP;
230   }
231 }
232
233 UInt_t AliL3TrackArray::GetOutSize(){
234   UInt_t count = GetOutCount();   //use only present tracks
235   UInt_t tHits = 0;
236   for(Int_t i=0;i<fNTracks;i++){  //loop over all tracks
237     AliL3Track *track = GetCheckedTrack(i);  //use only present tracks
238     if(track)                                       //use only present tracks
239       tHits += track->GetNHits();
240   }
241
242   //calculate size of track
243   return count*sizeof(AliL3TrackSegmentData)+sizeof(UInt_t)*tHits;
244 }
245
246 UInt_t AliL3TrackArray::WriteTracks(UInt_t & ntracks,AliL3TrackSegmentData* tr){
247   ntracks = GetOutCount();
248   return WriteTracks(tr);
249 }
250
251 UInt_t AliL3TrackArray::WriteTracks(AliL3TrackSegmentData* tr){
252   if(GetTrackType()=='c') return WriteConfMapTracks(tr);
253   AliL3TrackSegmentData *tP = tr;
254   UInt_t *pP;
255   UInt_t size = 0;
256   for(Int_t i=0; i<fNTracks; i++){  //loop over all tracks
257     AliL3Track *track = GetCheckedTrack(i); //use only present tracks
258     if(!track) continue;                           //use only present tracks
259     tP->fX = track->GetFirstPointX();
260     tP->fY = track->GetFirstPointY();
261     tP->fZ = track->GetFirstPointZ();
262     tP->fPt = track->GetPt();
263     tP->fLastX = track->GetLastPointX();
264     tP->fLastY = track->GetLastPointY();
265     tP->fLastZ = track->GetLastPointZ();
266     tP->fPsi = track->GetPsi();
267     tP->fTgl = track->GetTgl();
268     tP->fCharge = track->GetCharge();
269     tP->fNPoints = track->GetNHits();
270
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 void AliL3TrackArray::AddTracks(AliL3TrackArray *newtrack,Bool_t remove_old,Int_t slice)
331 {
332   if(GetTrackType() != newtrack->GetTrackType() && GetTrackType()!='t')
333     {
334       LOG(AliL3Log::kError,"AliL3TrackArray::AddTracks","Track types")
335         <<"Bad idea to add tracks of different types"<<ENDLOG;
336       return;
337     }
338   if(fSize < fNTracks+newtrack->GetNPresent())
339     SetSize(fSize+newtrack->GetSize());
340   for(Int_t i =0;i<newtrack->GetNTracks();i++){
341     AliL3Track *tpt = newtrack->GetCheckedTrack(i);
342     if(!tpt) continue;
343     if(remove_old)
344       newtrack->Remove(i);
345     AliL3Track *track = NextTrack();
346     track->Set(tpt);
347     if(slice>=0)
348       track->Rotate(slice); //Rotate track to global coordinates
349     /*
350       AliL3Track *track;
351       if(GetTrackType()=='h')
352       track = (AliL3HoughTrack*)NextTrack();
353       else
354       track = NextTrack();
355       track->Set(tpt);
356     */
357   }
358 }
359
360
361 void AliL3TrackArray::Compress(){
362   if(GetNPresent()==GetNTracks()) return;
363   AliL3Track **tmp =  new AliL3Track *[fNTracks];
364   Int_t present=0;
365   Int_t absent=GetNPresent();
366   for(Int_t i=0;i<GetNTracks();i++){
367     if(fIsPresent[i]) tmp[present++] = fTrack[i];
368     else tmp[absent++] = fTrack[i];
369   }
370   for(Int_t i=0;i<GetNTracks();i++)
371     fIsPresent[i]=kTRUE;
372
373   //Copy pointers back
374   for(Int_t i=0; i<GetNTracks();i++){
375     fTrack[i]=tmp[i];
376   }
377
378   delete[] tmp;
379
380   fNTracks = GetNPresent();
381   fNAbsent = 0;
382 }
383
384 void AliL3TrackArray::QSort(){
385   // compress an sort
386   Compress();
387   QSort(fTrack,0,fNTracks);
388 }
389
390 void AliL3TrackArray::QSort( AliL3Track **a, Int_t first, Int_t last){
391
392    // Sort array of AliL3Track pointers using a quicksort algorithm.
393    // Uses TrackCompare() to compare objects.
394    // Thanks to Root! 
395
396    static AliL3Track *tmp;
397    static int i;           // "static" to save stack space
398    int j;
399
400    while (last - first > 1) {
401       i = first;
402       j = last;
403       for (;;) {
404         while (++i < last && TrackCompare(a[i], a[first]) < 0)
405           ;
406         while (--j > first && TrackCompare(a[j], a[first]) > 0)
407           ;
408          if (i >= j)
409             break;
410
411          tmp  = a[i];
412          a[i] = a[j];
413          a[j] = tmp;
414       }
415       if (j == first) {
416          ++first;
417          continue;
418       }
419       tmp = a[first];
420       a[first] = a[j];
421       a[j] = tmp;
422       if (j - first < last - (j + 1)) {
423          QSort(a, first, j);
424          first = j + 1;   // QSort(j + 1, last);
425       } else {
426          QSort(a, j + 1, last);
427          last = j;        // QSort(first, j);
428       }
429    }
430 }
431
432 Int_t AliL3TrackArray::TrackCompare(AliL3Track *a, AliL3Track *b){
433    // Compare the two tracks.
434   
435   return b->Compare(a);
436   
437   /*
438     if(fTrackType=='h')
439     {
440     AliL3HoughTrack *tra = (AliL3HoughTrack*)a;
441     AliL3HoughTrack *trb = (AliL3HoughTrack*)b;
442     if(tra->GetWeight() < trb->GetWeight()) return 1;
443     if(tra->GetWeight() > trb->GetWeight()) return -1;
444     }
445     else
446     {
447     if(a->GetNHits() < b->GetNHits()) return 1;
448     if(a->GetNHits() > b->GetNHits()) return -1;
449     }
450     
451     return 0;
452   */
453 }
454
455