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