451d6590f2b08248a93a0d1a12b9c09df6454cd7
[u/mrichter/AliRoot.git] / HLT / hough / AliL3HoughMerger.cxx
1 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
2 //*-- Copyright &copy ASV 
3
4 #include <math.h>
5 #include <stdlib.h>
6 #include "AliL3Logging.h"
7 #include "AliL3Transform.h"
8 #include "AliL3TrackArray.h"
9 #include "AliL3HoughTrack.h"
10 #include "AliL3HoughMerger.h"
11 #include "AliL3HoughTransformer.h"
12
13 //_____________________________________________________________
14 // AliL3HoughMerger
15 //
16 // Patch merging class for Hough tracklets
17
18 ClassImp(AliL3HoughMerger)
19
20   
21 AliL3HoughMerger::AliL3HoughMerger()
22 {
23   //Default constructor
24 }
25
26
27 AliL3HoughMerger::AliL3HoughMerger(Int_t nsubsectors) : AliL3Merger(nsubsectors,"AliL3HoughTrack")
28 {
29   //Constructor
30   Is2Global(kFALSE);
31   SetParameters(0.001,0.1,0.05);
32 }
33
34
35 AliL3HoughMerger::~AliL3HoughMerger()
36 {
37  
38 }
39
40 void AliL3HoughMerger::FillTracks(AliL3TrackArray *tracks,Int_t patch)
41 {
42   if(tracks->GetNTracks()==0)
43     LOG(AliL3Log::kWarning,"AliL3HoughMerger::FillTracks","Track Array")
44       <<"Adding empty track array"<<ENDLOG;
45   
46   GetInTracks(patch)->AddTracks(tracks,kFALSE);//Copy tracks
47   printf("Filling %d tracks to merger\n",tracks->GetNTracks());
48 }
49
50 void AliL3HoughMerger::SetParameters(Double_t maxkappa,Double_t maxpsi,Double_t maxphi0)
51 {
52   fMaxKappa = maxkappa;
53   fMaxPsi = maxpsi;
54   fMaxPhi0 = maxphi0;
55 }
56
57 Bool_t AliL3HoughMerger::IsTrack(AliL3Track *innertrack,AliL3Track *outertrack)
58 {
59   //Check if the tracks can be merged, called by the track merger
60   
61   AliL3HoughTrack *tr1 = (AliL3HoughTrack*)innertrack;
62   AliL3HoughTrack *tr2 = (AliL3HoughTrack*)outertrack;
63   
64   if( (!tr1->IsPoint()) || (!tr2->IsPoint()) )  return kFALSE; 
65   if(abs(tr1->GetEtaIndex() - tr2->GetEtaIndex()) > 1) return kFALSE;
66   if(tr1->GetCharge() != tr2->GetCharge()) return kFALSE;
67   if(fabs(tr1->GetPhi0() - tr2->GetPhi0()) > fMaxPhi0) return kFALSE;
68   if(fabs(tr1->GetKappa() - tr2->GetKappa()) > fMaxKappa) return kFALSE;
69   
70   /*
71     if( (!tr1->IsPoint()) || (!tr2->IsPoint()) )  return kFALSE; 
72     if(fabs(innertrack->GetPointY()-outertrack->GetPointY()) >fMaxY) return kFALSE;
73     if(fabs(innertrack->GetPointZ()-outertrack->GetPointZ()) >fMaxZ) return kFALSE;
74     if(fabs(innertrack->GetKappa()-outertrack->GetKappa())   >fMaxKappa) return kFALSE;
75     if(GetAngle(innertrack->GetPointPsi(),outertrack->GetPointPsi()) >fMaxPsi) return kFALSE;
76     if(fabs(innertrack->GetTgl()-outertrack->GetTgl()) >fMaxTgl) return kFALSE;
77   */
78   
79   return kTRUE;//Tracks could be merged
80 }
81
82 void AliL3HoughMerger::AddTrack(AliL3TrackArray *mergedtrack,AliL3Track *track)
83 {
84   AliL3Track *t[1];
85   t[0] = track;
86   MultiMerge(mergedtrack,t,1);
87 }
88
89 AliL3Track *AliL3HoughMerger::MultiMerge(AliL3TrackArray *mergedtrack,AliL3Track **tracks, Int_t ntrack)
90 {
91   //Called by the track merger
92
93   AliL3HoughTrack *newtrack = (AliL3HoughTrack*)mergedtrack->NextTrack();
94   AliL3HoughTrack **trs = (AliL3HoughTrack**)tracks;
95   Int_t weight=0;
96
97   //Sum up the total weight:
98   for(Int_t i=ntrack-1; i>=0; i--)
99     weight += trs[i]->GetWeight();
100   
101   AliL3HoughTrack *tpt=trs[0];//This is the innermost track
102   AliL3HoughTrack *tpl=trs[ntrack-1];
103   newtrack->SetTrackParameters(tpt->GetKappa(),tpt->GetPhi0(),weight);
104   newtrack->SetEtaIndex(tpt->GetEtaIndex());
105   newtrack->SetEta(tpt->GetEta());
106   newtrack->SetPsi(tpt->GetPsi());
107   newtrack->SetCenterX(tpt->GetCenterX());
108   newtrack->SetCenterY(tpt->GetCenterY());
109   newtrack->SetFirstPoint(tpt->GetFirstPointX(),tpt->GetFirstPointY(),tpt->GetFirstPointZ());
110   newtrack->SetLastPoint(tpl->GetLastPointX(),tpl->GetLastPointY(),tpl->GetLastPointZ());
111   newtrack->SetCharge(tpt->GetCharge());
112   newtrack->SetRowRange(tpt->GetFirstRow(),tpl->GetLastRow());
113   
114   return (AliL3Track*)newtrack;
115
116 }
117
118 void AliL3HoughMerger::MergePatches(Bool_t slow)
119 {
120   //Merge tracks from across the patches.
121   
122   fSlow = slow;
123   AliL3TrackArray *tracks;
124   AliL3HoughTrack *track;
125   for(Int_t i=0; i<GetNIn(); i++)
126     {
127       tracks = GetInTracks(i);
128       for(Int_t j=0; j<tracks->GetNTracks(); j++)
129         {
130           track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
131           if(!track) continue;
132           track->UpdateToFirstRow();
133         }
134     }
135   Merge();
136   
137 }
138
139 void AliL3HoughMerger::Merge()
140 {
141   Double_t edge0 = PI/18.;
142   Double_t edge1 = 2*PI - edge0;
143   AliL3TrackArray *ttt = GetOutTracks();
144   
145   Int_t subsec = GetNIn() - 2; 
146   for(Int_t i=subsec;i>=0;i--){
147     AliL3TrackArray *tout = GetOutTracks();
148     if(i==subsec) tout = GetInTracks(subsec+1);
149     AliL3TrackArray *tin = GetInTracks(i);
150     Double_t xval = AliL3Transform::Row2X(AliL3Transform::GetLastRow(i));
151     Double_t xmax = AliL3Transform::Row2X(AliL3Transform::GetLastRow(i+1));
152     Double_t ymax = xval*tan(edge0);
153     for(Int_t out=0;out<tout->GetNTracks();out++){
154       AliL3Track *outtrack=tout->GetCheckedTrack(out);
155       if(!outtrack) continue;
156       //outtrack->CalculateHelix();
157       outtrack->CalculatePoint(xval);
158       if(outtrack->IsPoint()&&fabs(outtrack->GetPointY())>ymax){
159         tout->Remove(out);
160       }
161     }
162     //    tout->Compress();
163     for(Int_t in=0;in<tin->GetNTracks();in++){
164       AliL3Track *intrack=(AliL3Track*)tin->GetTrack(in);
165       //intrack->CalculateHelix();
166       intrack->CalculatePoint(xval);
167     }
168     tin->QSort();
169     tout->QSort();
170
171     if(fSlow) SlowMerge(ttt,tin,tout,xval);
172     else Merge(ttt,tin,tout);
173
174     /*
175     //Add the tracks that cross the sector boundary:
176     for(Int_t in=0;in<tin->GetNTracks();in++){
177       AliL3Track *intrack=(AliL3Track*)tin->GetCheckedTrack(in);
178       if(!intrack) continue;
179       if(intrack->CalculateEdgePoint(edge0)){
180         if(intrack->GetPointX()<xmax ){
181           AddTrack(ttt,intrack);
182           tin->Remove(in);
183         }
184       } 
185       else if(intrack->CalculateEdgePoint(edge1)){
186         if(intrack->GetPointX()<xmax ){
187           AddTrack(ttt,intrack);
188           tin->Remove(in);
189         }
190       }
191     }
192     */
193   } // end subsector loop
194   LOG(AliL3Log::kInformational,"AliL3HoughMerger::Merge","Result")
195     <<AliL3Log::kDec<<"Total Merged Tracks: "<<GetOutTracks()->GetNPresent()
196     <<ENDLOG;
197 }
198
199 Int_t AliL3HoughMerger::Merge(AliL3TrackArray* mergedtrack,AliL3TrackArray *tracksin,AliL3TrackArray *tracksout)
200 {
201   
202   AliL3Track *tracks[2];
203   const Int_t  kNOut=tracksout->GetNTracks();
204   const Int_t  kNIn =tracksin->GetNTracks();
205   const Int_t  kNMerged =mergedtrack->GetNTracks();
206
207   Bool_t *ismatchedin  = new Bool_t[kNIn];
208   for(Int_t in =0;in<kNIn;in++)
209     ismatchedin[in]=kFALSE;
210   Bool_t *ismatchedout = new Bool_t[kNOut];
211   for(Int_t out =0;out<kNOut;out++)
212     ismatchedout[out] = kFALSE;
213   for(Int_t out =0;out<kNOut;out++){
214     AliL3Track *outertrack=(AliL3Track*)tracksout->GetCheckedTrack(out);
215     if(!outertrack) continue;
216     for(Int_t in =0;in<kNIn;in++){
217       if(ismatchedin[in]) continue;
218       AliL3Track *innertrack=(AliL3Track*)tracksin->GetCheckedTrack(in);
219       if(!innertrack) continue;
220       if(outertrack==innertrack) continue;
221       
222       if(IsTrack(innertrack,outertrack)) //They can be merged
223         {
224           tracks[0]=innertrack; tracks[1]=outertrack; 
225           SortTracks(tracks,2); //Sort the tracks according to minimum x-point
226           //if(tracks[0]->GetLastPointX()<tracks[1]->GetFirstPointX()){
227           MultiMerge(mergedtrack,tracks,2);
228           tracksout->Remove(out);
229           tracksin->Remove(in);
230           ismatchedin[in]=kTRUE;
231           ismatchedout[out]=kTRUE;
232           break;
233           // }
234         }
235     }
236   }
237   
238   Int_t nmerged = mergedtrack->GetNTracks()-kNMerged;
239   LOG(AliL3Log::kInformational,"AliL3HoughMerger::Merge","Result")
240     <<AliL3Log::kDec<<"Merged Tracks: "<<nmerged<<ENDLOG;
241   delete[] ismatchedin;
242   delete[] ismatchedout;
243   return nmerged;
244 }
245
246 void AliL3HoughMerger::SlowMerge(AliL3TrackArray *mergedtrack,AliL3TrackArray *tracksin,AliL3TrackArray *tracksout,Double_t xval)
247 {
248   void *ntuple=GetNtuple();
249   const Int_t  kNOut=tracksout->GetNTracks();
250   const Int_t  kNIn =tracksin->GetNTracks();
251   const Int_t  kNMerged =mergedtrack->GetNTracks();
252   AliL3Track *tracks[2];
253   Bool_t merge = kTRUE;
254   while(merge){
255     Int_t inmin=-1,outmin=-1;
256     Double_t min=10;
257     for(Int_t out=0;out<kNOut;out++){
258     AliL3Track *outertrack=tracksout->GetCheckedTrack(out);
259     if(!outertrack) continue;
260       for(Int_t in=0;in<kNIn;in++){
261         AliL3Track *innertrack=tracksin->GetCheckedTrack(in);
262         if(!innertrack) continue;
263         Double_t diff = TrackDiff(innertrack,outertrack);
264         if(diff>=0&&diff<min){
265           min=diff;
266           inmin=in;
267           outmin=out; 
268         }
269       } 
270     }
271     if(inmin>=0&&outmin>=0){
272       AliL3Track *outertrack=tracksout->GetTrack(outmin);
273       AliL3Track *innertrack=tracksin->GetTrack(inmin);
274       tracks[0]=innertrack;
275       tracks[1]=outertrack;
276       SortTracks(tracks,2);
277       Print(tracks);
278       MultiMerge(mergedtrack,tracks,2);
279       outertrack->CalculatePoint(xval);
280       innertrack->CalculatePoint(xval);
281       FillNtuple(ntuple,innertrack,outertrack);
282       tracksout->Remove(outmin);
283       tracksin->Remove(inmin);
284       //      tracksout->Compress();
285       //      tracksin->Compress(); 
286     }
287     else merge = kFALSE;
288   }
289   LOG(AliL3Log::kInformational,"AliL3HoughMerger::SlowMerge","Result")
290     <<AliL3Log::kDec<<"Merged Tracks: "
291     <<mergedtrack->GetNTracks()-kNMerged<<ENDLOG;
292   char name[256] = "ntuple_t.root";
293   for(Int_t i=0;i<GetNIn();i++)
294     if(tracksin==GetInTracks(i))
295       sprintf(name,"ntuple_t_%d.root",i);
296   WriteNtuple(name,ntuple);
297 }
298
299 void AliL3HoughMerger::Print(AliL3Track **tracks)
300 {
301   AliL3HoughTrack *tr1 = (AliL3HoughTrack*)tracks[0];
302   AliL3HoughTrack *tr2 = (AliL3HoughTrack*)tracks[1];
303   Double_t kappadiff = fabs(tr1->GetKappa()-tr2->GetKappa());
304   Double_t phi0diff = fabs(tr1->GetPhi0()-tr2->GetPhi0());
305   cout << "---------Difference in merged tracks---------"<<endl;
306   cout << "Kappa: "<<kappadiff<<" Phi0 : "<<phi0diff<<endl;
307   
308 }