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