]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCTrackMerger.cxx
memory leak elimiated, minor format of comments
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCTrackMerger.cxx
1 // $Id$
2 // Original: AliHLTTrackMerger.cxx,v 1.12 2005/06/14 10:55:21 cvetan 
3
4 //**************************************************************************
5 //* This file is property of and copyright by the ALICE HLT Project        * 
6 //* ALICE Experiment at CERN, All rights reserved.                         *
7 //*                                                                        *
8 //* Primary Authors: Uli Frankenfeld, maintained by                        *
9 //*                  Matthias Richter <Matthias.Richter@ift.uib.no>        *
10 //*                  for The ALICE HLT Project.                            *
11 //*                                                                        *
12 //* Permission to use, copy, modify and distribute this software and its   *
13 //* documentation strictly for non-commercial purposes is hereby granted   *
14 //* without fee, provided that the above copyright notice appears in all   *
15 //* copies and that both the copyright notice and this permission notice   *
16 //* appear in the supporting documentation. The authors make no claims     *
17 //* about the suitability of this software for any purpose. It is          *
18 //* provided "as is" without express or implied warranty.                  *
19 //**************************************************************************
20
21 /// @file   AliHLTTPCTrackMerger.cxx
22 /// @author Uli Frankenfeld, maintained by Matthias Richter
23 /// @date   
24 /// @brief  The HLT TPC track segment merger
25 ///
26
27 #include "AliHLTTPCLogging.h"
28 #include "AliHLTTPCTrackMerger.h"
29 #include "AliHLTTPCTrack.h"
30 #include "AliHLTTPCTrackSegmentData.h"
31 #include "AliHLTTPCTransform.h"
32 #include "AliHLTTPCTrackArray.h"
33
34 #if __GNUC__ >= 3
35 using namespace std;
36 #endif
37
38 ClassImp(AliHLTTPCTrackMerger)
39
40 AliHLTTPCTrackMerger::AliHLTTPCTrackMerger() 
41   :
42   AliHLTTPCMerger(),
43   fSubSector(0),
44   fNSubSector(0),
45   fRowMin(NULL),
46   fRowMax(NULL),
47   fSlow(0)
48 {
49   //Default constructor
50   Is2Global(kFALSE);
51   SetParameter();
52 }
53
54
55 AliHLTTPCTrackMerger::AliHLTTPCTrackMerger(Int_t nsubsectors) 
56   :
57   AliHLTTPCMerger(),
58   fSubSector(0),
59   fNSubSector(nsubsectors),
60   fRowMin(new Int_t[nsubsectors]),
61   fRowMax(new Int_t[nsubsectors]),
62   fSlow(0)
63 {
64   //Constructor.
65   InitMerger(nsubsectors);
66   Is2Global(kFALSE);
67   fSlow = kFALSE;
68   SetParameter();
69 }
70
71 AliHLTTPCTrackMerger::~AliHLTTPCTrackMerger()
72 {
73   //Destructor
74   if (fRowMin) delete[] fRowMin;
75   if (fRowMax) delete[] fRowMax;
76 }
77
78 void AliHLTTPCTrackMerger::SetRows(Int_t *row){
79   //Set the indeces of the first and last
80   //TPC padrows
81   //
82   for(Int_t i=0;i<fNSubSector;i++){
83     fRowMin[i]=*(row+(2*i));
84     fRowMax[i]=*(row+(2*i+1));
85   }
86 }
87
88 void AliHLTTPCTrackMerger::InitSector(Int_t slice,Int_t subsector){
89   // 
90   // Select Sector and subsector. The following FillTracks call will 
91   // fill this subsector
92   //
93   fSlice = slice;
94   fSubSector = subsector;
95   fCurrentTracks = fSubSector;
96 }
97
98 void AliHLTTPCTrackMerger::SlowMerge(AliHLTTPCTrackArray *mergedtrack,AliHLTTPCTrackArray *tracksin,AliHLTTPCTrackArray *tracksout,Double_t xval){
99   // 
100   // Slow merging of two AliHLTTPCTrackArrays
101   // at reference plane x=xval
102   //
103   void *ntuple=GetNtuple();
104   const Int_t  kNOut=tracksout->GetNTracks();
105   const Int_t  kNIn =tracksin->GetNTracks();
106   const Int_t  kNMerged =mergedtrack->GetNTracks();
107   AliHLTTPCTrack *tracks[2];
108   Bool_t merge = kTRUE;
109   while(merge){
110     Int_t inmin=-1,outmin=-1;
111     Double_t min=10;
112     for(Int_t out=0;out<kNOut;out++){
113     AliHLTTPCTrack *outertrack=tracksout->GetCheckedTrack(out);
114     if(!outertrack) continue;
115       for(Int_t in=0;in<kNIn;in++){
116         AliHLTTPCTrack *innertrack=tracksin->GetCheckedTrack(in);
117         if(!innertrack) continue;
118         Double_t diff = TrackDiff(innertrack,outertrack);
119         if(diff>=0&&diff<min){
120           min=diff;
121           inmin=in;
122           outmin=out; 
123         }
124       } 
125     }
126     if(inmin>=0&&outmin>=0){
127       AliHLTTPCTrack *outertrack=tracksout->GetTrack(outmin);
128       AliHLTTPCTrack *innertrack=tracksin->GetTrack(inmin);
129       tracks[0]=innertrack;
130       tracks[1]=outertrack;
131       SortTracks(tracks,2);
132       MultiMerge(mergedtrack,tracks,2);
133       outertrack->CalculatePoint(xval);
134       innertrack->CalculatePoint(xval);
135       PrintDiff(innertrack,outertrack);
136       //FillNtuple(ntuple,innertrack,outertrack);
137       tracksout->Remove(outmin);
138       tracksin->Remove(inmin);
139 //      tracksout->Compress();
140 //      tracksin->Compress(); 
141     }
142     else merge = kFALSE;
143   }
144   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCTrackMerger::SlowMerge","Result")
145   <<AliHLTTPCLog::kDec<<"Merged Tracks: "
146   <<mergedtrack->GetNTracks()-kNMerged<<ENDLOG;
147   char name[256] = "ntuple_t.root";
148   for(Int_t i=0;i<4;i++)
149     if(tracksin==GetInTracks(i))
150       sprintf(name,"ntuple_t_%d.root",i);
151   WriteNtuple(name,ntuple);
152 }
153
154 void AliHLTTPCTrackMerger::SlowMerge(){
155   fSlow = kTRUE;
156   Merge();
157 }
158
159 void AliHLTTPCTrackMerger::InterMerge(){
160   // 
161   // Merging of the tracks
162   // between readout patches
163   //
164   for(Int_t patch=0;patch< GetNIn();patch++){
165     AliHLTTPCTrackArray * tracks = GetInTracks(patch);
166     Double_t xval = AliHLTTPCTransform::Row2X((fRowMax[patch]+fRowMin[patch])/2);
167     Int_t nrow= fRowMax[patch]-fRowMin[patch]+1;
168     const Int_t  kNIn =tracks->GetNTracks();
169     AliHLTTPCTrack *tr[2];
170     for(Int_t in=0;in<kNIn;in++){
171       AliHLTTPCTrack *t = tracks->GetCheckedTrack(in);
172       if(t){
173         t->CalculateHelix();
174         t->CalculatePoint(xval);
175       }
176     }
177     for(Int_t out=0;out<kNIn;out++){
178     AliHLTTPCTrack *outertrack=tracks->GetCheckedTrack(out);
179     if(!outertrack) continue;
180       for(Int_t in=0;in<kNIn;in++){
181         if(in==out) continue;
182         AliHLTTPCTrack *innertrack=tracks->GetCheckedTrack(in);
183         if(!innertrack) continue;
184         if(outertrack->GetNHits()+innertrack->GetNHits()>nrow) continue;
185
186         if(IsTrack(innertrack,outertrack)){
187           tr[0]=innertrack;
188           tr[1]=outertrack;
189           SortTracks(tr,2);
190           if(tr[0]->GetLastPointX()<tr[1]->GetFirstPointX()){
191             MultiMerge(tracks,tr,2);
192             tracks->Remove(out);
193             tracks->Remove(in);
194             break;
195           }
196         }
197       }
198     }
199     Int_t nmerged = tracks->GetNTracks()-kNIn;
200     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCTrackMerger::InterMerge","Result")
201     <<AliHLTTPCLog::kDec<<"Merged Tracks: "<<nmerged<<ENDLOG;
202   }
203 }
204
205 void AliHLTTPCTrackMerger::Merge(){
206   //Loop over tracks and pass them to the track merger.
207   Double_t edge0 = AliHLTTPCTransform::Pi()/18;
208   Double_t edge1 = AliHLTTPCTransform::TwoPi() - edge0;
209   AliHLTTPCTrackArray *ttt = GetOutTracks();
210   if(fNSubSector==1) {
211     GetOutTracks()->AddTracks(GetInTracks(0)); 
212     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCTrackMerger::Merge","Result")
213     <<AliHLTTPCLog::kDec<<"Total Copied Tracks: "<<GetOutTracks()->GetNPresent()
214     <<ENDLOG;
215     return;
216   }
217   Int_t subsec = fNSubSector -2; 
218   for(Int_t i=subsec;i>=0;i--){
219     AliHLTTPCTrackArray *tout = GetOutTracks();
220     if(i==subsec) tout = GetInTracks(subsec+1);
221     AliHLTTPCTrackArray *tin = GetInTracks(i);
222     Double_t xval = AliHLTTPCTransform::Row2X(fRowMax[i]);
223     Double_t xmax = AliHLTTPCTransform::Row2X(fRowMax[i+1]);
224     Double_t ymax = xval*tan(edge0);
225     for(Int_t out=0;out<tout->GetNTracks();out++){
226       AliHLTTPCTrack *outtrack=tout->GetCheckedTrack(out);
227       if(!outtrack) continue;
228       outtrack->CalculateHelix();
229       outtrack->CalculatePoint(xval);
230       if(outtrack->IsPoint()&&fabs(outtrack->GetPointY())>ymax){
231         if(outtrack->GetNHits()<10)
232           tout->Remove(out);
233       }
234     }
235 //    tout->Compress();
236     for(Int_t in=0;in<tin->GetNTracks();in++){
237       AliHLTTPCTrack *intrack=(AliHLTTPCTrack*)tin->GetTrack(in);
238       intrack->CalculateHelix();
239       intrack->CalculatePoint(xval);
240     }
241     tin->QSort();
242     tout->QSort();
243
244     if(fSlow) SlowMerge(ttt,tin,tout,xval);
245     else Merge(ttt,tin,tout);
246     for(Int_t in=0;in<tin->GetNTracks();in++){
247       AliHLTTPCTrack *intrack=(AliHLTTPCTrack*)tin->GetCheckedTrack(in);
248       if(!intrack) continue;
249       if(intrack->CalculateEdgePoint(edge0)){
250         if(intrack->GetPointX()<xmax ){
251           AddTrack(ttt,intrack);
252           tin->Remove(in);
253         }
254       } 
255       else if(intrack->CalculateEdgePoint(edge1)){
256         if(intrack->GetPointX()<xmax ){
257           AddTrack(ttt,intrack);
258           tin->Remove(in);
259         }
260       }
261     }
262 /*
263     for(Int_t in=0;in<tin->GetNTracks();in++){
264       AliHLTTPCTrack *intrack=(AliHLTTPCTrack*)tin->GetCheckedTrack(in);
265       if(!intrack) continue;
266       if(intrack->GetNHits()<10) continue;
267       AddTrack(ttt,intrack);
268       tin->Remove(in);
269     }
270 */
271   } // end subsector loop
272   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCTrackMerger::Merge","Result")
273   <<AliHLTTPCLog::kDec<<"Total Merged Tracks: "<<GetOutTracks()->GetNPresent()
274   <<ENDLOG;
275 }
276
277 Int_t AliHLTTPCTrackMerger::Merge(AliHLTTPCTrackArray* mergedtrack,AliHLTTPCTrackArray *tracksin,AliHLTTPCTrackArray *tracksout){
278   //Loop over tracks and pass them to the track merger.
279   AliHLTTPCTrack *tracks[2];
280
281   const Int_t  kNOut=tracksout->GetNTracks();
282   const Int_t  kNIn =tracksin->GetNTracks();
283   const Int_t  kNMerged =mergedtrack->GetNTracks();
284
285   Bool_t *ismatchedin  = new Bool_t[kNIn];
286   for(Int_t in =0;in<kNIn;in++)
287     ismatchedin[in]=kFALSE;
288   Bool_t *ismatchedout = new Bool_t[kNOut];
289   for(Int_t out =0;out<kNOut;out++)
290     ismatchedout[out] = kFALSE;
291   for(Int_t out =0;out<kNOut;out++){
292     AliHLTTPCTrack *outertrack=(AliHLTTPCTrack*)tracksout->GetCheckedTrack(out);
293     if(!outertrack) continue;
294     for(Int_t in =0;in<kNIn;in++){
295       if(ismatchedin[in]) continue;
296       AliHLTTPCTrack *innertrack=(AliHLTTPCTrack*)tracksin->GetCheckedTrack(in);
297       if(!innertrack) continue;
298       if(outertrack==innertrack) continue;
299       if(outertrack->GetCharge()!=innertrack->GetCharge()) continue;
300       if(IsTrack(innertrack,outertrack)){
301         tracks[0]=innertrack; tracks[1]=outertrack; 
302         SortTracks(tracks,2);  
303         if(tracks[0]->GetLastPointX()<tracks[1]->GetFirstPointX()){
304           MultiMerge(mergedtrack,tracks,2);
305           tracksout->Remove(out);
306           tracksin->Remove(in);
307           ismatchedin[in]=kTRUE;
308           ismatchedout[out]=kTRUE;
309           break;
310         }
311       }
312     }
313   }
314
315   Int_t nmerged = mergedtrack->GetNTracks()-kNMerged;
316   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCTrackMerger::Merge","Result")
317   <<AliHLTTPCLog::kDec<<"Merged Tracks: "<<nmerged<<ENDLOG;
318   delete[] ismatchedin;
319   delete[] ismatchedout;
320   return nmerged;
321 }
322
323