Changes done to make the Cluser Finder calculate the errors in Pad and Time direction...
[u/mrichter/AliRoot.git] / HLT / src / AliL3Merger.cxx
1 //$Id$
2
3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright &copy Uli 
5
6 #include "AliL3Logging.h"
7 #include <math.h>
8 #include <iostream.h>
9 #include "AliL3Merger.h"
10 #include "AliL3Track.h"
11 #include "AliL3TrackSegmentData.h"
12 #include "AliL3Transform.h"
13 #include "AliL3TrackArray.h"
14
15 #ifdef use_root //use root ntuple for slow merge
16 #include <TNtuple.h>
17 #include <TTree.h>
18 #include <TFile.h>
19 #endif
20
21 //_____________________________________________________________
22 // AliL3Merger
23 //
24 // The L3 merger base class
25 //
26
27 ClassImp(AliL3Merger)
28
29 AliL3Merger::AliL3Merger(){
30   //Default constructor
31   SetArray(0);
32 }
33
34
35 AliL3Merger::AliL3Merger(Int_t ntrackarrays,Char_t *tracktype){
36   //Constructor.
37   
38   if(strcmp(tracktype,"AliL3Track")==0) fTrackType='t';
39   else if(strcmp(tracktype,"AliL3ConfMapTrack")==0) fTrackType='c';
40   else if(strcmp(tracktype,"AliL3HoughTrack")==0) fTrackType='h';
41   else
42     LOG(AliL3Log::kError,"AliL3Merger::AliL3Merger","Track types")
43       <<"Unknown tracktype"<<ENDLOG;
44   SetArray(ntrackarrays);
45   fCurrentTracks=0;
46
47 }
48
49 AliL3Merger::~AliL3Merger(){
50   //Destructor
51   DeleteArray();
52 }
53
54 void AliL3Merger::DeleteArray(){
55   for(Int_t i=0; i<fNIn;i++)
56     delete fInTrack[i];
57   delete[] (Byte_t*) fInTrack;
58   delete fOutTrack;
59 }
60
61 void AliL3Merger::SetArray(Int_t nin){
62   fNIn = nin;
63   fInTrack = (AliL3TrackArray **) new Byte_t[fNIn*sizeof(AliL3TrackArray *)];
64   for(Int_t i=0; i<fNIn;i++){
65     if(fTrackType=='h')
66       fInTrack[i] = new AliL3TrackArray("AliL3HoughTrack");
67     else
68       fInTrack[i] = new AliL3TrackArray("AliL3Track");
69     
70   }
71   if(fTrackType=='h')
72     fOutTrack= new AliL3TrackArray("AliL3HoughTrack");
73   else
74     fOutTrack= new AliL3TrackArray("AliL3Track");
75 }
76
77 void AliL3Merger::Reset(){
78   for(Int_t i=0; i<fNIn;i++){
79     fInTrack[i]->Reset();
80   }
81   fOutTrack->Reset();
82 }
83
84 void AliL3Merger::FillTracks(Int_t ntracks, AliL3TrackSegmentData* tr){
85   //Read tracks from shared memory (or memory)
86   AliL3TrackArray *destination = GetInTracks(fCurrentTracks);
87   if(Is2Global())
88     destination->FillTracks(ntracks, tr, fSlice);
89     //destination->FillTracks(ntracks, tr, fSlice, fTransformer);
90   else
91     destination->FillTracks(ntracks, tr);
92 }
93
94 void AliL3Merger::AddAllTracks(){
95   for(Int_t i=0; i<GetNIn();i++){
96     AliL3TrackArray *in = GetInTracks(i);
97     AliL3TrackArray *out = GetOutTracks();
98     out->AddTracks(in);
99   }
100 }
101
102 void AliL3Merger::SortGlobalTracks(AliL3Track **tracks, Int_t ntrack){
103   AliL3Track **tmp = new AliL3Track*[ntrack]; 
104   for(Int_t i=0;i<ntrack;i++) tmp[i] = tracks[i];
105   Int_t *t = new Int_t[ntrack];
106   for(Int_t i=0;i<ntrack;i++) t[i]=-1;
107
108   for(Int_t j=0;j<ntrack;j++){
109     Double_t minr=300;
110     Int_t    mini=0;
111     for(Int_t i=0;i<ntrack;i++){
112      if(!tracks[i]) continue;
113        Double_t rr=pow(tracks[i]->GetFirstPointX(),2)+pow(tracks[i]->GetFirstPointY(),2);
114        Double_t r=sqrt(rr);
115        if(r<minr){
116          minr=r;
117          mini=i;
118        }
119     }
120     t[j]=mini;
121     tracks[mini]=0;
122   }
123   for(Int_t i=0;i<ntrack;i++) tracks[i] = tmp[t[i]];
124   delete[] t;
125   delete[] tmp;
126 }
127
128
129 void AliL3Merger::SortTracks(AliL3Track **tracks, Int_t ntrack){
130   AliL3Track **tmp = new  AliL3Track*[ntrack];
131   for(Int_t i=0;i<ntrack;i++) tmp[i] = tracks[i];
132   Int_t *t = new Int_t[ntrack];
133   for(Int_t i=0;i<ntrack;i++) t[i]=-1;
134
135   for(Int_t j=0;j<ntrack;j++){
136     Double_t minx=300; 
137     Int_t    mini=0;
138     for(Int_t i=0;i<ntrack;i++){
139      if(!tracks[i]) continue;
140        if(tracks[i]->GetFirstPointX()<minx){
141          minx=tracks[i]->GetFirstPointX();
142          mini=i;
143        }     
144     }
145     t[j]=mini;  
146     tracks[mini]=0;
147   }
148   for(Int_t i=0;i<ntrack;i++) tracks[i] = tmp[t[i]];
149   delete[] t;
150   delete[] tmp;
151 }
152
153 void AliL3Merger::AddTrack(AliL3TrackArray *mergedtrack,AliL3Track *track){
154   AliL3Track *t[1];
155   t[0] = track;
156   MultiMerge(mergedtrack,t,1);
157 }
158
159 AliL3Track * AliL3Merger::MergeTracks(AliL3TrackArray *mergedtrack,AliL3Track *t0,AliL3Track *t1){
160   AliL3Track *t[2];
161   t[0] = t0; 
162   t[1] = t1;
163   SortTracks(t,2);
164   return MultiMerge(mergedtrack,t,2);
165 }
166
167 AliL3Track * AliL3Merger::MultiMerge(AliL3TrackArray *mergedtracks,AliL3Track **tracks, Int_t ntrack){
168 // merge the tracks!!
169
170 //check npoints
171   Int_t nps = 0;
172   for(Int_t i=0;i<ntrack;i++){
173     nps+=tracks[i]->GetNHits();
174   }
175   if(nps>176){
176     LOG(AliL3Log::kWarning,"AliL3Merger::MultiMerge","Adding Points")
177     <<AliL3Log::kDec<<"Too many Points: "<<nps<<ENDLOG;
178     return 0;
179   }
180
181   //create new track
182   AliL3Track *newtrack = mergedtracks->NextTrack();
183   //copy points
184   UInt_t nn[176];
185   nps = 0;
186
187 //  for(Int_t i=0;i<ntrack;i++){
188   for(Int_t i=ntrack-1;i>=0;i--){
189     memcpy(&nn[nps],tracks[i]->GetHitNumbers(),tracks[i]->GetNHits()*sizeof(UInt_t));
190     nps+=tracks[i]->GetNHits();
191   }
192   AliL3Track *tpf=tracks[0];
193   AliL3Track *tpl=tracks[ntrack-1];
194   AliL3Track *best = tpf;
195   if(tpf->GetNHits()<tpl->GetNHits() && Is2Global())
196     best = tpl;//Best means = most points and therefore best fit (in global case)
197   
198   newtrack->SetNHits(nps);
199   newtrack->SetHits(nps,nn);
200   newtrack->SetFirstPoint(tpf->GetFirstPointX(),tpf->GetFirstPointY(),tpf->GetFirstPointZ());
201   newtrack->SetLastPoint(tpl->GetLastPointX(),tpl->GetLastPointY(),tpl->GetLastPointZ());
202   newtrack->SetPt(best->GetPt());
203   newtrack->SetPsi(best->GetPsi());
204   newtrack->SetTgl(best->GetTgl());
205   newtrack->SetCharge(tpf->GetCharge());
206   return newtrack;
207 }
208
209 void* AliL3Merger::GetNtuple(char *varlist){
210 #ifdef use_root
211   TNtuple* nt = new TNtuple("ntuple","ntuple",varlist);
212   return (void*) nt;
213 #else
214   return 0;
215 #endif
216 }
217
218 void* AliL3Merger::GetNtuple(){
219 #ifdef use_root
220   TNtuple* nt = new TNtuple("ntuple","ntuple",
221        "dx:dy:dz:dk:dpsi:dtgl:dq:disx:disy:disz:dis:n0:n1:diff:drx:dry:drz");
222   return (void*) nt;
223 #else
224   return 0;
225 #endif
226 }
227
228 Bool_t AliL3Merger::WriteNtuple(char *filename, void* nt){
229 #ifdef use_root
230   TNtuple *ntuple=(TNtuple *) nt;
231   TFile *f = new TFile(filename,"RECREATE");
232   ntuple->Write();
233   f->Close();
234   delete ntuple; 
235   return kTRUE; 
236 #else
237   return kFALSE;
238 #endif
239 }
240
241 void AliL3Merger::FillNtuple(void *nt,AliL3Track *innertrack,AliL3Track *outertrack){
242   Float_t data[17];
243   if(outertrack->IsPoint()&&innertrack->IsPoint()){
244     data[0] =Float_t(innertrack->GetPointX()-outertrack->GetPointX());
245     data[1] =Float_t(innertrack->GetPointY()-outertrack->GetPointY());
246     data[2] =Float_t(innertrack->GetPointZ()-outertrack->GetPointZ());
247     data[3] =Float_t(innertrack->GetKappa()-outertrack->GetKappa());
248     Double_t psi= innertrack->GetPointPsi() - outertrack->GetPointPsi();
249     if(psi>PI) psi-=2*PI;
250     if(psi<-PI)psi+=2*PI;
251     data[4] =Float_t(psi);
252     data[5] =Float_t(innertrack->GetTgl()-outertrack->GetTgl());
253     data[6] =Float_t(innertrack->GetCharge()-outertrack->GetCharge());
254     data[7] =Float_t(innertrack->GetLastPointX()-outertrack->GetFirstPointX());
255     data[8] =Float_t(innertrack->GetLastPointY()-outertrack->GetFirstPointY());
256     data[9] =Float_t(innertrack->GetLastPointZ()-outertrack->GetFirstPointZ());
257     data[10] =sqrt(pow(data[7],2)+pow(data[8],2)+pow(data[9],2));
258     data[11]= outertrack->GetNHits();
259     data[12]= innertrack->GetNHits();
260     data[13] = Float_t(TrackDiff(innertrack,outertrack));
261 #ifdef use_root
262     TNtuple *ntuple = (TNtuple *) nt;
263     ntuple->Fill(data);
264 #endif
265   }
266 }
267
268 void AliL3Merger::FillNtuple(void *nt,Float_t *data){
269 #ifdef use_root
270     TNtuple *ntuple = (TNtuple *) nt;
271     ntuple->Fill(data);
272 #endif
273 }
274
275 Double_t AliL3Merger::GetAngle(Double_t a1,Double_t a2){
276   Double_t da = a1 - a2 +4*PI;
277   da = fmod(da,2*PI);
278   if(da>PI) da = 2*PI -da;
279   return da;
280 }
281
282 void AliL3Merger::SetParameter(Double_t maxy, Double_t maxz, Double_t maxkappa, Double_t maxpsi, Double_t maxtgl){
283   fMaxY = maxy;
284   fMaxZ = maxz;
285   fMaxKappa = maxkappa;
286   fMaxPsi = maxpsi;
287   fMaxTgl = maxtgl;
288 }
289
290 Bool_t AliL3Merger::IsTrack(AliL3Track *innertrack,AliL3Track *outertrack){
291
292   if(innertrack->GetCharge()!=outertrack->GetCharge()) return kFALSE;
293   if( (!innertrack->IsPoint()) || (!outertrack->IsPoint()) )  return kFALSE; 
294   if(innertrack->GetNHits()+outertrack->GetNHits()>176) return kFALSE;
295
296   if(fabs(innertrack->GetPointY()-outertrack->GetPointY()) >fMaxY) return kFALSE;
297   if(fabs(innertrack->GetPointZ()-outertrack->GetPointZ()) >fMaxZ) return kFALSE;
298   if(fabs(innertrack->GetKappa()-outertrack->GetKappa())   >fMaxKappa) return kFALSE;
299   if(GetAngle(innertrack->GetPointPsi(),outertrack->GetPointPsi()) >fMaxPsi) return kFALSE;
300   if(fabs(innertrack->GetTgl()-outertrack->GetTgl()) >fMaxTgl) return kFALSE;
301   //if no rejection up to this point: merge!!
302   return kTRUE;
303 }
304
305 Bool_t AliL3Merger::IsRTrack(AliL3Track *innertrack,AliL3Track *outertrack){
306   return IsTrack(innertrack,outertrack);
307 }
308
309 Double_t AliL3Merger::TrackDiff(AliL3Track *innertrack,AliL3Track *outertrack){
310   Double_t diff =-1;
311   Double_t x[4],y[4],z[4],dy[4],dz[4];
312   AliL3Track *tracks[2]; 
313
314   tracks[0] = innertrack;
315   tracks[1] = outertrack;
316   SortGlobalTracks(tracks,2);
317   innertrack = tracks[0]; 
318   outertrack = tracks[1];
319   
320   x[0] = innertrack->GetFirstPointX();
321   x[1] = innertrack->GetLastPointX();
322   x[2] = outertrack->GetFirstPointX();
323   x[3] = outertrack->GetLastPointX();
324
325   y[0] = innertrack->GetFirstPointY();
326   y[1] = innertrack->GetLastPointY();
327   y[2] = outertrack->GetFirstPointY();
328   y[3] = outertrack->GetLastPointY();
329
330   z[0] = innertrack->GetFirstPointZ();
331   z[1] = innertrack->GetLastPointZ();
332   z[2] = outertrack->GetFirstPointZ();
333   z[3] = outertrack->GetLastPointZ();
334
335   
336   outertrack->CalculatePoint(x[0]);
337   if(!outertrack->IsPoint()) return diff;
338   dy[0] = fabs(y[0] - outertrack->GetPointY());
339   dz[0] = fabs(z[0] - outertrack->GetPointZ());
340   
341   outertrack->CalculatePoint(x[1]);
342   if(!outertrack->IsPoint()) return diff;
343   dy[1] = fabs(y[1] - outertrack->GetPointY());
344   dz[1] = fabs(z[1] - outertrack->GetPointZ());
345   
346   innertrack->CalculatePoint(x[2]);
347   if(!innertrack->IsPoint()) return diff;
348   dy[2] = fabs(y[2] - innertrack->GetPointY());
349   dz[2] = fabs(z[2] - innertrack->GetPointZ());
350   
351   innertrack->CalculatePoint(x[3]);
352   if(!innertrack->IsPoint()) return diff;
353   dy[3] = fabs(y[3] - innertrack->GetPointY());
354   dz[3] = fabs(z[3] - innertrack->GetPointZ());
355
356   diff=0;
357   for(Int_t i=0;i<4;i++)
358     diff+=sqrt(dy[i]*dy[i]+dz[i]*dz[i]);
359   return diff; 
360 }
361
362 void AliL3Merger::PrintDiff(AliL3Track *innertrack,AliL3Track *outertrack)
363 {
364   if(!innertrack->IsPoint()||!outertrack->IsPoint()){
365     LOG(AliL3Log::kInformational,"AliL3Merger::PrintDiff","No Points")<<ENDLOG;
366     //cerr<<"AliL3Merger::PrintDiff: No Points"<<endl;
367     //cerr<<"---------------------------"<<endl;
368     return;
369   } 
370
371   Double_t dx = innertrack->GetPointX()-outertrack->GetPointX();
372   Double_t dy = innertrack->GetPointY()-outertrack->GetPointY();
373   Double_t dz = innertrack->GetPointZ()-outertrack->GetPointZ();
374   Double_t dk = innertrack->GetKappa()-outertrack->GetKappa();
375   Double_t dpsi= innertrack->GetPointPsi() - outertrack->GetPointPsi();
376   if(dpsi>PI) dpsi-=2*PI;
377   if(dpsi<-PI)dpsi+=2*PI;
378   //Double_t dpsi = GetAngle(innertrack->GetPointPsi(),outertrack->GetPointPsi());
379   Double_t dtgl= innertrack->GetTgl()-outertrack->GetTgl();
380   Double_t dq =innertrack->GetCharge()-outertrack->GetCharge();
381
382   LOG(AliL3Log::kInformational,"AliL3Merger::PrintDiff","Points") <<"dx: "<<dx<<" dy: "<<dy<<" dz: "<<dz<<" dk: "<<dk<<" dpsi: "<<dpsi<<" dtgl: "<<dtgl<<" dq: "<<dq<<ENDLOG;
383   //fprintf(stderr,"dx: %4f dy: %4f dz: %4f dk: %4f dpsi: %4f dtgl: %4f dq: %4f\n",dx,dy,dz,dk,dpsi,dtgl,dq);
384   //cerr<<"---------------------------"<<endl;
385   
386 }
387
388 void AliL3Merger::Print(){
389   // print some infos
390   for(Int_t i=0; i<fNIn; i++){
391     AliL3TrackArray *ttt= GetInTracks(i);
392     for(Int_t j =0;j<ttt->GetNTracks();j++){
393       AliL3Track *track=ttt->GetCheckedTrack(j);
394       if(!track) continue;
395       track->CalculateHelix();
396 //      Double_t angle = atan2(track->GetLastPointY(),track->GetLastPointX());
397 //      if(angle<0) angle+=PI;
398       if(track->CalculatePoint(135))
399 //      if(!track->CalculateEdgePoint(angle)) cerr<<"**************"<<endl;     
400 //      if(track->CalculatePoint(track->GetLastPointX()))
401 //      if(track->CalculatePoint(0))
402       {
403 //      PrintTrack(track);
404 //      track->CalculateReferencePoint(PI/180.);
405       track->CalculateReferencePoint(0.001);
406       Float_t dx=(float)track->GetPointX()-track->GetPointX();
407       Float_t dy=(float)track->GetPointY()-track->GetPointY();
408       Float_t dz=(float)track->GetPointZ()-track->GetPointZ();
409       LOG(AliL3Log::kInformational,"AliL3Merger::Print","RefPoint") <<"npt: "<<track->GetNHits()<<" dx: "<<dx<<" dy: "<<dy<<" dz: "<<dz<<ENDLOG;
410
411       //fprintf(stderr,"npt: %3d dx: %8.5f dy: %8.5f dz: %8.5f\n",track->GetNHits(),dx,dy,dz);
412       //cerr<<"---------------------------"<<endl;
413       }
414     }  
415   }
416 }
417
418 void AliL3Merger::PrintTrack(AliL3Track *track){
419   fprintf(stderr,"npt: %3d pt: %.2f psi: %.2f tgl: %5.2f q: %2d\n",
420     track->GetNHits(),track->GetPt(),track->GetPsi(),
421     track->GetTgl(),track->GetCharge());
422   fprintf(stderr,
423     "x1: %6.2f y1: %6.2f z1: %6.2f xl: %6.2f yl: %6.2f zl: %6.2f\n",
424     track->GetFirstPointX(),track->GetFirstPointY(),track->GetFirstPointZ(),
425     track->GetLastPointX(),track->GetLastPointY(),track->GetLastPointZ());
426   if(track->IsPoint()){
427     fprintf(stderr,
428       "R: %.2f Xc: %.2f Yc: %.2f Xp: %.2f Yp: %.2f Zp: %.2f Psip: %.2f\n",
429       track->GetRadius(),track->GetCenterX(),track->GetCenterY(),
430       track->GetPointX(),track->GetPointY(),track->GetPointZ(),
431       track->GetPointPsi());
432   }
433 }