Coding violation fixes.
[u/mrichter/AliRoot.git] / HLT / src / AliL3TrackMerger.cxx
1 // @(#) $Id$
2
3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright &copy ALICE HLT Group
5
6 /** \class AliL3TrackMerger
7 <pre>
8 //_____________________________________________________________
9 // AliL3TrackMerger
10 //
11 // The L3 track segment merger
12 //
13 </pre
14 */
15
16 #include "AliL3StandardIncludes.h"
17
18 #include "AliL3Logging.h"
19 #include "AliL3TrackMerger.h"
20 #include "AliL3Track.h"
21 #include "AliL3TrackSegmentData.h"
22 #include "AliL3Transform.h"
23 #include "AliL3TrackArray.h"
24
25 ClassImp(AliL3TrackMerger)
26
27 AliL3TrackMerger::AliL3TrackMerger(){
28   //Default constructor
29   Is2Global(kFALSE);
30   fSlow = kFALSE;
31   SetParameter();
32   fRowMin = 0;
33   fRowMax = 0;
34 }
35
36
37 AliL3TrackMerger::AliL3TrackMerger(Int_t nsubsectors) : AliL3Merger()
38 {
39   //Constructor.
40   InitMerger(nsubsectors);
41   fNSubSector = nsubsectors;
42   Is2Global(kFALSE);
43   fSlow = kFALSE;
44   SetParameter();
45   fRowMin = new Int_t[nsubsectors];
46   fRowMax = new Int_t[nsubsectors];
47   
48 }
49
50 AliL3TrackMerger::~AliL3TrackMerger(){
51   //Destructor
52 }
53
54 void AliL3TrackMerger::SetRows(Int_t *row){
55   //Set the indeces of the first and last
56   //TPC padrows
57   //
58   for(Int_t i=0;i<fNSubSector;i++){
59     fRowMin[i]=*(row+(2*i));
60     fRowMax[i]=*(row+(2*i+1));
61   }
62 }
63
64 void AliL3TrackMerger::InitSector(Int_t slice,Int_t subsector){
65   // 
66   // Select Sector and subsector. The following FillTracks call will 
67   // fill this subsector
68   //
69   fSlice = slice;
70   fSubSector = subsector;
71   fCurrentTracks = fSubSector;
72 }
73
74 void AliL3TrackMerger::SlowMerge(AliL3TrackArray *mergedtrack,AliL3TrackArray *tracksin,AliL3TrackArray *tracksout,Double_t xval){
75   // 
76   // Slow merging of two AliL3TrackArrays
77   // at reference plane x=xval
78   //
79   void *ntuple=GetNtuple();
80   const Int_t  kNOut=tracksout->GetNTracks();
81   const Int_t  kNIn =tracksin->GetNTracks();
82   const Int_t  kNMerged =mergedtrack->GetNTracks();
83   AliL3Track *tracks[2];
84   Bool_t merge = kTRUE;
85   while(merge){
86     Int_t inmin=-1,outmin=-1;
87     Double_t min=10;
88     for(Int_t out=0;out<kNOut;out++){
89     AliL3Track *outertrack=tracksout->GetCheckedTrack(out);
90     if(!outertrack) continue;
91       for(Int_t in=0;in<kNIn;in++){
92         AliL3Track *innertrack=tracksin->GetCheckedTrack(in);
93         if(!innertrack) continue;
94         Double_t diff = TrackDiff(innertrack,outertrack);
95         if(diff>=0&&diff<min){
96           min=diff;
97           inmin=in;
98           outmin=out; 
99         }
100       } 
101     }
102     if(inmin>=0&&outmin>=0){
103       AliL3Track *outertrack=tracksout->GetTrack(outmin);
104       AliL3Track *innertrack=tracksin->GetTrack(inmin);
105       tracks[0]=innertrack;
106       tracks[1]=outertrack;
107       SortTracks(tracks,2);
108       MultiMerge(mergedtrack,tracks,2);
109       outertrack->CalculatePoint(xval);
110       innertrack->CalculatePoint(xval);
111       PrintDiff(innertrack,outertrack);
112       //FillNtuple(ntuple,innertrack,outertrack);
113       tracksout->Remove(outmin);
114       tracksin->Remove(inmin);
115 //      tracksout->Compress();
116 //      tracksin->Compress(); 
117     }
118     else merge = kFALSE;
119   }
120   LOG(AliL3Log::kInformational,"AliL3TrackMerger::SlowMerge","Result")
121   <<AliL3Log::kDec<<"Merged Tracks: "
122   <<mergedtrack->GetNTracks()-kNMerged<<ENDLOG;
123   char name[256] = "ntuple_t.root";
124   for(Int_t i=0;i<4;i++)
125     if(tracksin==GetInTracks(i))
126       sprintf(name,"ntuple_t_%d.root",i);
127   WriteNtuple(name,ntuple);
128 }
129
130 void AliL3TrackMerger::SlowMerge(){
131   fSlow = kTRUE;
132   Merge();
133 }
134
135 void AliL3TrackMerger::InterMerge(){
136   // 
137   // Merging of the tracks
138   // between readout patches
139   //
140   for(Int_t patch=0;patch< GetNIn();patch++){
141     AliL3TrackArray * tracks = GetInTracks(patch);
142     Double_t xval = AliL3Transform::Row2X((fRowMax[patch]+fRowMin[patch])/2);
143     Int_t nrow= fRowMax[patch]-fRowMin[patch]+1;
144     const Int_t  kNIn =tracks->GetNTracks();
145     AliL3Track *tr[2];
146     for(Int_t in=0;in<kNIn;in++){
147       AliL3Track *t = tracks->GetCheckedTrack(in);
148       if(t){
149         t->CalculateHelix();
150         t->CalculatePoint(xval);
151       }
152     }
153     for(Int_t out=0;out<kNIn;out++){
154     AliL3Track *outertrack=tracks->GetCheckedTrack(out);
155     if(!outertrack) continue;
156       for(Int_t in=0;in<kNIn;in++){
157         if(in==out) continue;
158         AliL3Track *innertrack=tracks->GetCheckedTrack(in);
159         if(!innertrack) continue;
160         if(outertrack->GetNHits()+innertrack->GetNHits()>nrow) continue;
161
162         if(IsTrack(innertrack,outertrack)){
163           tr[0]=innertrack;
164           tr[1]=outertrack;
165           SortTracks(tr,2);
166           if(tr[0]->GetLastPointX()<tr[1]->GetFirstPointX()){
167             MultiMerge(tracks,tr,2);
168             tracks->Remove(out);
169             tracks->Remove(in);
170             break;
171           }
172         }
173       }
174     }
175     Int_t nmerged = tracks->GetNTracks()-kNIn;
176     LOG(AliL3Log::kInformational,"AliL3TrackMerger::InterMerge","Result")
177     <<AliL3Log::kDec<<"Merged Tracks: "<<nmerged<<ENDLOG;
178   }
179 }
180
181 void AliL3TrackMerger::Merge(){
182   //Loop over tracks and pass them to the track merger.
183   Double_t edge0 = AliL3Transform::Pi()/18;
184   Double_t edge1 = AliL3Transform::TwoPi() - edge0;
185   AliL3TrackArray *ttt = GetOutTracks();
186   if(fNSubSector==1) {
187     GetOutTracks()->AddTracks(GetInTracks(0)); 
188     LOG(AliL3Log::kInformational,"AliL3TrackMerger::Merge","Result")
189     <<AliL3Log::kDec<<"Total Copied Tracks: "<<GetOutTracks()->GetNPresent()
190     <<ENDLOG;
191     return;
192   }
193   Int_t subsec = fNSubSector -2; 
194   for(Int_t i=subsec;i>=0;i--){
195     AliL3TrackArray *tout = GetOutTracks();
196     if(i==subsec) tout = GetInTracks(subsec+1);
197     AliL3TrackArray *tin = GetInTracks(i);
198     Double_t xval = AliL3Transform::Row2X(fRowMax[i]);
199     Double_t xmax = AliL3Transform::Row2X(fRowMax[i+1]);
200     Double_t ymax = xval*tan(edge0);
201     for(Int_t out=0;out<tout->GetNTracks();out++){
202       AliL3Track *outtrack=tout->GetCheckedTrack(out);
203       if(!outtrack) continue;
204       outtrack->CalculateHelix();
205       outtrack->CalculatePoint(xval);
206       if(outtrack->IsPoint()&&fabs(outtrack->GetPointY())>ymax){
207         if(outtrack->GetNHits()<10)
208           tout->Remove(out);
209       }
210     }
211 //    tout->Compress();
212     for(Int_t in=0;in<tin->GetNTracks();in++){
213       AliL3Track *intrack=(AliL3Track*)tin->GetTrack(in);
214       intrack->CalculateHelix();
215       intrack->CalculatePoint(xval);
216     }
217     tin->QSort();
218     tout->QSort();
219
220     if(fSlow) SlowMerge(ttt,tin,tout,xval);
221     else Merge(ttt,tin,tout);
222     for(Int_t in=0;in<tin->GetNTracks();in++){
223       AliL3Track *intrack=(AliL3Track*)tin->GetCheckedTrack(in);
224       if(!intrack) continue;
225       if(intrack->CalculateEdgePoint(edge0)){
226         if(intrack->GetPointX()<xmax ){
227           AddTrack(ttt,intrack);
228           tin->Remove(in);
229         }
230       } 
231       else if(intrack->CalculateEdgePoint(edge1)){
232         if(intrack->GetPointX()<xmax ){
233           AddTrack(ttt,intrack);
234           tin->Remove(in);
235         }
236       }
237     }
238 /*
239     for(Int_t in=0;in<tin->GetNTracks();in++){
240       AliL3Track *intrack=(AliL3Track*)tin->GetCheckedTrack(in);
241       if(!intrack) continue;
242       if(intrack->GetNHits()<10) continue;
243       AddTrack(ttt,intrack);
244       tin->Remove(in);
245     }
246 */
247   } // end subsector loop
248   LOG(AliL3Log::kInformational,"AliL3TrackMerger::Merge","Result")
249   <<AliL3Log::kDec<<"Total Merged Tracks: "<<GetOutTracks()->GetNPresent()
250   <<ENDLOG;
251 }
252
253 Int_t AliL3TrackMerger::Merge(AliL3TrackArray* mergedtrack,AliL3TrackArray *tracksin,AliL3TrackArray *tracksout){
254   //Loop over tracks and pass them to the track merger.
255   AliL3Track *tracks[2];
256
257   const Int_t  kNOut=tracksout->GetNTracks();
258   const Int_t  kNIn =tracksin->GetNTracks();
259   const Int_t  kNMerged =mergedtrack->GetNTracks();
260
261   Bool_t *ismatchedin  = new Bool_t[kNIn];
262   for(Int_t in =0;in<kNIn;in++)
263     ismatchedin[in]=kFALSE;
264   Bool_t *ismatchedout = new Bool_t[kNOut];
265   for(Int_t out =0;out<kNOut;out++)
266     ismatchedout[out] = kFALSE;
267   for(Int_t out =0;out<kNOut;out++){
268     AliL3Track *outertrack=(AliL3Track*)tracksout->GetCheckedTrack(out);
269     if(!outertrack) continue;
270     for(Int_t in =0;in<kNIn;in++){
271       if(ismatchedin[in]) continue;
272       AliL3Track *innertrack=(AliL3Track*)tracksin->GetCheckedTrack(in);
273       if(!innertrack) continue;
274       if(outertrack==innertrack) continue;
275       if(outertrack->GetCharge()!=innertrack->GetCharge()) continue;
276       if(IsTrack(innertrack,outertrack)){
277         tracks[0]=innertrack; tracks[1]=outertrack; 
278         SortTracks(tracks,2);  
279         if(tracks[0]->GetLastPointX()<tracks[1]->GetFirstPointX()){
280           MultiMerge(mergedtrack,tracks,2);
281           tracksout->Remove(out);
282           tracksin->Remove(in);
283           ismatchedin[in]=kTRUE;
284           ismatchedout[out]=kTRUE;
285           break;
286         }
287       }
288     }
289   }
290
291   Int_t nmerged = mergedtrack->GetNTracks()-kNMerged;
292   LOG(AliL3Log::kInformational,"AliL3TrackMerger::Merge","Result")
293   <<AliL3Log::kDec<<"Merged Tracks: "<<nmerged<<ENDLOG;
294   delete[] ismatchedin;
295   delete[] ismatchedout;
296   return nmerged;
297 }
298
299