0f419bdf0610166e765a7fc7ca9a106a3d386db4
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Hough.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright &copy ASV 
5
6
7 #include <string.h>
8
9 #include "AliL3HoughMerger.h"
10 #include "AliL3HoughIntMerger.h"
11 #include "AliL3HoughGlobalMerger.h"
12 #include "AliL3Logging.h"
13 #include "AliL3Histogram.h"
14 #include "AliL3Hough.h"
15 #include "AliL3HoughTransformer.h"
16 #include "AliL3HoughMaxFinder.h"
17 #ifdef use_aliroot
18 #include "AliL3FileHandler.h"
19 #else
20 #include "AliL3MemHandler.h"
21 #endif
22 #include "AliL3DigitData.h"
23 #include "AliL3HoughEval.h"
24 #include "AliL3Transform.h"
25 #include "AliL3Defs.h"
26 #include "AliL3TrackArray.h"
27 #include "AliL3HoughTrack.h"
28
29
30 //_____________________________________________________________
31 // AliL3Hough
32 //
33 // Base class for the Hough transform
34 //
35
36
37 ClassImp(AliL3Hough)
38
39 AliL3Hough::AliL3Hough()
40 {
41   //Constructor
42   
43   fBinary = kFALSE;
44   fNEtaSegments = 0;
45   fAddHistograms = kFALSE;
46   fDoIterative = kFALSE; 
47   fWriteDigits=kFALSE;
48   fNPatches=0;
49 }
50
51
52 AliL3Hough::AliL3Hough(Char_t *path,Bool_t binary,Int_t n_eta_segments)
53 {
54   fBinary = binary;
55   strcpy(fPath,path);
56   fNEtaSegments = n_eta_segments;
57   fAddHistograms = kFALSE;
58   fDoIterative = kFALSE; 
59   fWriteDigits = kFALSE;
60   Init();
61 }
62
63
64 AliL3Hough::~AliL3Hough()
65 {
66   CleanUp();
67   if(fMerger)
68     delete fMerger;
69   if(fInterMerger)
70     delete fInterMerger;
71   if(fPeakFinder)
72     delete fPeakFinder;
73   if(fGlobalMerger)
74     delete fGlobalMerger;
75 }
76
77 void AliL3Hough::CleanUp()
78 {
79   //Cleanup memory
80   
81   for(Int_t i=0; i<fNPatches; i++)
82     {
83       if(fTracks[i]) delete fTracks[i];
84       if(fEval[i]) delete fEval[i];
85       if(fHoughTransformer[i]) delete fHoughTransformer[i];
86       if(fMemHandler[i]) delete fMemHandler[i];
87     }
88   
89   /*    
90         if(fTracks) delete [] fTracks;
91         if(fEval) delete [] fEval;
92         if(fHoughTransformer) delete [] fHoughTransformer;
93         if(fMemHandler) delete [] fMemHandler;
94   */
95 }
96
97 void AliL3Hough::Init()
98 {
99   fNPatches = NPatches;
100   fHoughTransformer = new AliL3HoughTransformer*[fNPatches];
101 #ifdef use_aliroot
102   fMemHandler = new AliL3FileHandler*[fNPatches];
103 #else
104   fMemHandler = new AliL3MemHandler*[fNPatches];
105 #endif
106   fTracks = new AliL3TrackArray*[fNPatches];
107   fEval = new AliL3HoughEval*[fNPatches];
108   for(Int_t i=0; i<fNPatches; i++)
109     {
110       fHoughTransformer[i] = new AliL3HoughTransformer(1,i,fNEtaSegments);
111       fHoughTransformer[i]->CreateHistograms(64,-0.003,0.003,64,-0.26,0.26);
112       fHoughTransformer[i]->SetThreshold(3);
113       fEval[i] = new AliL3HoughEval();
114       fTracks[i] = new AliL3TrackArray("AliL3HoughTrack");
115 #ifdef use_aliroot
116       fMemHandler[i] = new AliL3FileHandler();
117       if(!fBinary)
118         fMemHandler[i]->SetAliInput(fPath);
119 #else
120       fMemHandler[i] = new AliL3MemHandler();
121 #endif
122       
123     }
124   fPeakFinder = new AliL3HoughMaxFinder("KappaPhi");
125   fMerger = new AliL3HoughMerger(fNPatches);
126   fInterMerger = new AliL3HoughIntMerger();
127 }
128
129 void AliL3Hough::Process(Int_t minslice,Int_t maxslice)
130 {
131   //Process all slices [minslice,maxslice].
132   fGlobalMerger = new AliL3HoughGlobalMerger(minslice,maxslice);
133   
134   for(Int_t i=minslice; i<=maxslice; i++)
135     {
136       ReadData(i);
137       Transform();
138       if(fAddHistograms)
139         AddAllHistograms();
140       FindTrackCandidates();
141       Evaluate();
142       fGlobalMerger->FillTracks(fTracks[0],i);
143     }
144   
145   
146 }
147
148 void AliL3Hough::ReadData(Int_t slice)
149 {
150   //Read data from files, binary or root.
151
152   for(Int_t i=0; i<fNPatches; i++)
153     {
154       fMemHandler[i]->Free();
155       UInt_t ndigits=0;
156       AliL3DigitRowData *digits =0;
157       Char_t name[256];
158       if(fBinary)//take input data from binary files
159         {
160           sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,i);
161           fMemHandler[i]->SetBinaryInput(name);
162           digits = (AliL3DigitRowData *)fMemHandler[i]->CompBinary2Memory(ndigits);
163           fMemHandler[i]->CloseBinaryInput();
164         }
165       else //read data from root file
166         {
167 #ifdef use_aliroot
168           fMemHandler[i]->Init(slice,i,NRows[i]);
169           digits=(AliL3DigitRowData *)fMemHandler[i]->AliDigits2Memory(ndigits); 
170 #else
171           cerr<<"You cannot read from rootfile now"<<endl;
172 #endif
173         }
174       fHoughTransformer[i]->SetInputData(ndigits,digits);
175     }
176 }
177
178 void AliL3Hough::Transform()
179 {
180   //Transform all data given to the transformer within the given slice
181   //(after ReadData(slice))
182
183   for(Int_t i=0; i<fNPatches; i++)
184     {
185       fHoughTransformer[i]->Reset();//Reset the histograms
186       fHoughTransformer[i]->TransformCircle();
187     }
188 }
189
190 void AliL3Hough::MergePatches()
191 {
192   if(fAddHistograms) //Nothing to merge here
193     return;
194   AliL3Transform *tr = new AliL3Transform();
195   fMerger->SetTransformer(tr);
196   fMerger->MergePatches(kTRUE);
197   delete tr;
198 }
199
200 void AliL3Hough::MergeInternally()
201 {
202   if(fAddHistograms)
203     fInterMerger->FillTracks(fTracks[0]);
204   else
205     fInterMerger->FillTracks(fMerger->GetOutTracks());
206   
207   fInterMerger->MMerge();
208 }
209
210 void AliL3Hough::ProcessSliceIter()
211 {
212   //Process current slice (after ReadData(slice)) iteratively.
213   
214   for(Int_t i=0; i<fNPatches; i++)
215     {
216       ProcessPatchIter(i);
217       fMerger->FillTracks(fTracks[i],i); //Copy tracks to merger
218     }
219   
220 }
221
222 void AliL3Hough::ProcessPatchIter(Int_t patch)
223 {
224   //Process patch in a iterative way. 
225   //transform + peakfinding + evaluation + transform +...
226
227   Int_t num_of_tries = 10;
228   AliL3HoughTransformer *tr = fHoughTransformer[patch];
229   AliL3TrackArray *tracks = fTracks[patch];
230   tracks->Reset();
231   AliL3HoughEval *ev = fEval[patch];
232   ev->InitTransformer(tr);
233   ev->RemoveFoundTracks();
234   ev->SetNumOfRowsToMiss(2);
235   ev->SetNumOfPadsToLook(2);
236   AliL3Histogram *hist;
237   for(Int_t t=0; t<num_of_tries; t++)
238     {
239       tr->Reset();
240       tr->TransformCircle();
241       for(Int_t i=0; i<fNEtaSegments; i++)
242         {
243           hist = tr->GetHistogram(i);
244           if(hist->GetNEntries()==0) continue;
245           fPeakFinder->SetHistogram(hist);
246           Int_t n=1;
247           Float_t x,y;
248           //fPeakFinder->FindAbsMaxima(*x,*y);
249           fPeakFinder->FindPeak(3,0.95,5,x,y);
250           AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->NextTrack();
251           track->SetTrackParameters(x,y,1);
252           if(!ev->LookInsideRoad(track,i))
253             {   
254               tracks->Remove(tracks->GetNTracks()-1);
255               tracks->Compress();
256             }
257         }
258     }
259   LOG(AliL3Log::kInformational,"AliL3Hough::ProcessPatch","NTracks")
260     <<AliL3Log::kDec<<"Found "<<tracks->GetNTracks()<<" tracks in patch "<<patch<<ENDLOG;
261 }
262
263
264 void AliL3Hough::AddAllHistograms()
265 {
266   //Add the histograms within one etaslice.
267   //Resulting histogram are in patch=0.
268
269   for(Int_t i=0; i<fNEtaSegments; i++)
270     {
271       AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(i);
272       for(Int_t j=1; j<fNPatches; j++)
273         {
274           AliL3Histogram *hist = fHoughTransformer[j]->GetHistogram(i);
275           hist0->Add(hist);
276         }
277     }
278   fAddHistograms = kTRUE;
279 }
280
281 void AliL3Hough::FindTrackCandidates()
282 {
283   //Look for peaks in histograms, and find the track candidates
284   
285   Int_t n_patches;
286   if(fAddHistograms)
287     n_patches = 1; //Histograms has been added.
288   else
289     n_patches = fNPatches;
290   
291   for(Int_t i=0; i<n_patches; i++)
292     {
293       AliL3HoughTransformer *tr = fHoughTransformer[i];
294       fTracks[i]->Reset();
295       for(Int_t j=0; j<fNEtaSegments; j++)
296         {
297           AliL3Histogram *hist = tr->GetHistogram(j);
298           if(hist->GetNEntries()==0) continue;
299           fPeakFinder->SetHistogram(hist);
300           Int_t n=10;
301           Float_t x[10];
302           Float_t y[10];
303           Int_t weight[10];
304           fPeakFinder->FindPeak1(x,y,weight,n,1);
305           for(Int_t k=0; k<n; k++)
306             {
307               if(weight[k] == 0) continue;
308               AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
309               track->SetTrackParameters(x[k],y[k],weight[k]);
310               track->SetEtaIndex(j);
311               track->SetEta((Double_t)(j*tr->GetEtaSlice()));
312               track->SetRowRange(NRows[0][0],NRows[5][1]);
313             }
314         }
315       fTracks[i]->QSort();
316     }
317 }
318
319 void AliL3Hough::Evaluate(Int_t road_width)
320 {
321   //Evaluate the tracks, by looking along the road in the raw data.
322   
323
324   if(!fTracks[0])
325     {
326       LOG(AliL3Log::kError,"AliL3Hough::Evaluate","Track Array")
327         <<"No tracks to work with..."<<ENDLOG;
328       return;
329     }
330   
331   printf("Number of tracks before evaluation %d\n",fTracks[0]->GetNTracks());
332   AliL3TrackArray *tracks;
333   for(Int_t i=0; i<fNPatches; i++)
334     {
335       fEval[i]->InitTransformer(fHoughTransformer[i]);
336       fEval[i]->SetNumOfRowsToMiss(2);
337       fEval[i]->SetNumOfPadsToLook(road_width);
338       if(fAddHistograms)
339         tracks = fTracks[0];
340       else
341         tracks = fTracks[i];
342       for(Int_t j=0; j<tracks->GetNTracks(); j++)
343         {
344           AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
345           if(!track)
346             {
347               LOG(AliL3Log::kWarning,"AliL3Hough::Evaluate","Track array")
348                 <<"Track object missing!"<<ENDLOG;
349               continue;
350             }
351           
352           if(!fEval[i]->LookInsideRoad(track,track->GetEtaIndex()))
353             tracks->Remove(j);
354           if(fAddHistograms)
355             track->SetRowRange(NRows[0][0],NRows[5][1]);//All rows included
356         }
357       tracks->Compress();
358       tracks->QSort(); //Sort the tracks according to weight
359       
360       if(!fAddHistograms)
361         fMerger->FillTracks(tracks,i); //Copy tracks to the track merger
362     }
363   
364 }
365
366 void AliL3Hough::EvaluateWithEta()
367 {
368   if(!fTracks[0])
369     {
370       printf("AliL3Hough::EvaluateWithEta: NO TRACKS\n");
371       return;
372     }
373   printf("Number of tracks before evaluation %d\n",fTracks[0]->GetNTracks());
374  
375   for(Int_t i=0; i<fNPatches; i++)
376     {
377       fEval[i]->InitTransformer(fHoughTransformer[i]);
378       fEval[i]->FindEta(fTracks[0]);
379     }
380   fMerger->FillTracks(fTracks[0],0);
381 }
382
383 void AliL3Hough::WriteTracks(Char_t *path)
384 {
385   AliL3MemHandler *mem = new AliL3MemHandler();
386   Char_t fname[100];
387   if(fAddHistograms)
388     {
389       sprintf(fname,"%s/tracks.raw",path);
390       mem->SetBinaryOutput(fname);
391       mem->TrackArray2Binary(fTracks[0]);
392       mem->CloseBinaryOutput();
393     }
394   else 
395     {
396       for(Int_t i=0; i<fNPatches; i++)
397         {
398           sprintf(fname,"%s/tracks_%d.raw",path,i);
399           mem->SetBinaryOutput(fname);
400           mem->TrackArray2Binary(fTracks[i]);
401           mem->CloseBinaryOutput();
402         }
403     }
404   delete mem;
405   
406 }
407 #ifdef use_aliroot
408 void AliL3Hough::WriteDigits(Char_t *outfile)
409 {
410   //Write the current data to a new rootfile.
411
412   for(Int_t i=0; i<fNPatches; i++)
413     {
414       AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fHoughTransformer[i]->GetDataPointer();
415       fMemHandler[i]->AliDigits2RootFile(tempPt,outfile);
416     }
417   
418 }
419 #endif