5e7cee177fe2ae37e7a11aef9c2233da44aa48b7
[u/mrichter/AliRoot.git] / HLT / hough / AliL3HoughIntMerger.cxx
1 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
2 //*-- Copyright &copy ASV 
3
4 #include <stdlib.h>
5 #include <math.h>
6 #include <iostream.h>
7 #include "AliL3Logging.h"
8 #include "AliL3HoughIntMerger.h"
9 #include "AliL3HoughTrack.h"
10 #include "AliL3TrackSegmentData.h"
11 #include "AliL3Transform.h"
12 #include "AliL3TrackArray.h"
13
14 //_____________________________________________________________
15 // Hough Inter merger
16 // Merging of multiple reconstructed tracks
17
18 ClassImp(AliL3HoughIntMerger)
19
20 AliL3HoughIntMerger::AliL3HoughIntMerger() : AliL3Merger(1,"AliL3HoughTrack")
21 {
22   //Default constructor
23   fRowMax = fRowMin = 0;
24   SetParameters(0.001,0.05,10);
25   Is2Global(kFALSE);
26 }
27
28
29 AliL3HoughIntMerger::~AliL3HoughIntMerger()
30 {
31   //Destructor
32   
33 }
34
35 void AliL3HoughIntMerger::SetParameters(Double_t maxkappa, Double_t maxphi0, Double_t maxtgl)
36 {
37   fMaxKappa = maxkappa;
38   fMaxPhi0 = maxphi0;
39   fMaxTgl = maxtgl;
40 }
41
42 void AliL3HoughIntMerger::FillTracks(AliL3TrackArray *tracks)
43 {
44   if(tracks->GetNTracks()==0)
45     LOG(AliL3Log::kWarning,"AliL3HoughIntMerger::FillTracks","Track Array")
46       <<"Adding empty track array"<<ENDLOG;
47   
48   GetInTracks(0)->AddTracks(tracks,kFALSE);//Copy tracks
49   printf("Filling %d tracks to intermerger\n",tracks->GetNTracks());
50 }
51
52 Bool_t AliL3HoughIntMerger::IsTrack(AliL3Track *innertrack,AliL3Track *outertrack)
53 {
54   //Check if the tracks can be merged, called by the track merger
55   
56   AliL3HoughTrack *tr1 = (AliL3HoughTrack*)innertrack;
57   AliL3HoughTrack *tr2 = (AliL3HoughTrack*)outertrack;
58   
59   if(abs(tr1->GetEtaIndex() - tr2->GetEtaIndex()) > 1) return kFALSE;
60   if(tr1->GetCharge()!=tr2->GetCharge()) return kFALSE;
61   if(fabs(tr1->GetKappa()-tr2->GetKappa())   >fMaxKappa) return kFALSE;
62   if(fabs(tr1->GetPhi0()-tr2->GetPhi0()) > fMaxPhi0) return kFALSE;
63
64   return kTRUE;//Tracks could be merged
65 }
66
67 AliL3Track *AliL3HoughIntMerger::MultiMerge(AliL3TrackArray *mergedtrack,AliL3Track **tracks, Int_t ntrack)
68 {
69   //Called by the track merger
70
71   AliL3HoughTrack *newtrack = (AliL3HoughTrack*)mergedtrack->NextTrack();
72   AliL3HoughTrack **trs = (AliL3HoughTrack**)tracks;
73       
74   AliL3HoughTrack *tpt=trs[0];//this is the "best" track
75   //AliL3HoughTrack *tpl=trs[ntrack-1];
76   newtrack->Set(tpt);
77   return (AliL3Track*)newtrack;
78 }
79
80
81
82 void AliL3HoughIntMerger::MMerge()
83 {
84   GetInTracks(0)->QSort();
85   while(Merge());
86   GetOutTracks()->AddTracks(GetInTracks(0));
87 }
88
89 Int_t AliL3HoughIntMerger::Merge()
90 {
91   
92   AliL3TrackArray * tracks = GetInTracks(0);
93   const Int_t  kNIn =tracks->GetNTracks();
94   AliL3Track *tr[2];
95
96   for(Int_t out=0;out<kNIn;out++)
97     {
98       AliL3HoughTrack *outertrack=(AliL3HoughTrack*)tracks->GetCheckedTrack(out);
99       if(!outertrack) continue;
100       for(Int_t in=0;in<kNIn;in++)
101         {
102           if(in==out) continue;
103           AliL3HoughTrack *innertrack=(AliL3HoughTrack*)tracks->GetCheckedTrack(in);
104           if(!innertrack) continue;
105           if(IsTrack(innertrack,outertrack))
106             {
107               tr[0]=innertrack;
108               tr[1]=outertrack;
109               SortTracks(tr,2);
110               Print(tr);
111               MultiMerge(tracks,tr,2);
112               tracks->Remove(out);
113               tracks->Remove(in);
114               break;
115             }
116         } 
117     }
118   Int_t nmerged = tracks->GetNTracks()-kNIn; 
119   LOG(AliL3Log::kInformational,"AliL3HoughIntMerger::Merge","Result")
120     <<AliL3Log::kDec<<"Merged Tracks: "<<nmerged<<ENDLOG;
121   
122   //add in tracks
123   //GetOutTracks()->AddTracks(GetInTracks(0)); 
124   
125   return nmerged;
126 }
127
128 void AliL3HoughIntMerger::Print(AliL3Track **tracks)
129 {
130   AliL3HoughTrack *tr1 = (AliL3HoughTrack*)tracks[0];
131   AliL3HoughTrack *tr2 = (AliL3HoughTrack*)tracks[1];
132   Double_t kappadiff = fabs(tr1->GetKappa()-tr2->GetKappa());
133   Double_t phi0diff = fabs(tr1->GetPhi0()-tr2->GetPhi0());
134   cout << "---------Difference in intermerged tracks---------"<<endl;
135   cout << "Kappa: "<<kappadiff<<" Phi0 : "<<phi0diff<<endl;
136   
137 }
138
139 void AliL3HoughIntMerger::SortTracks(AliL3Track **trs, Int_t ntrack)
140 {
141   //Sort the tracks according to their weight
142
143   AliL3HoughTrack **tracks = (AliL3HoughTrack**)trs;
144   AliL3HoughTrack **tmp = new  AliL3HoughTrack*[ntrack];
145   for(Int_t i=0;i<ntrack;i++) tmp[i] = (AliL3HoughTrack*)tracks[i];
146   Int_t *t = new Int_t[ntrack];
147   for(Int_t i=0;i<ntrack;i++) t[i]=-1;
148   
149   for(Int_t j=0;j<ntrack;j++)
150     {
151       Double_t maxw=0;
152       Int_t    maxi=0;
153       for(Int_t i=0;i<ntrack;i++)
154         {
155           if(!tracks[i]) continue;
156           if(tracks[i]->GetWeight() > maxw)
157             {
158               maxw=tracks[i]->GetWeight();
159               maxi=i;
160             }     
161         }
162       t[j]=maxi;  
163       tracks[maxi]=0;
164     }
165   for(Int_t i=0;i<ntrack;i++) tracks[i] = tmp[t[i]];
166   delete[] t;
167   delete[] tmp;
168 }