Bugfix in AliL3Hough::FindTrackCandidates; when track->SetEta, one has to
[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 "AliL3HoughTransformerVhdl.h"
17 #include "AliL3HoughMaxFinder.h"
18 #ifdef use_aliroot
19 #include "AliL3FileHandler.h"
20 #else
21 #include "AliL3MemHandler.h"
22 #endif
23 #include "AliL3DataHandler.h"
24 #include "AliL3DigitData.h"
25 #include "AliL3HoughEval.h"
26 #include "AliL3Transform.h"
27 #include "AliL3TrackArray.h"
28 #include "AliL3HoughTrack.h"
29
30
31 //_____________________________________________________________
32 // AliL3Hough
33 //
34 // Interface class for the Hough transform
35 //
36 // Example how to use:
37 //
38 // AliL3Hough *hough = new AliL3Hough(path,kTRUE,NumberOfEtaSegments);
39 // hough->ReadData(slice);
40 // hough->Transform();
41 // hough->FindTrackCandidates();
42 // 
43 // AliL3TrackArray *tracks = hough->GetTracks(patch);
44
45 ClassImp(AliL3Hough)
46
47 AliL3Hough::AliL3Hough()
48 {
49   //Constructor
50   
51   fBinary = kFALSE;
52   fNEtaSegments = 0;
53   fAddHistograms = kFALSE;
54   fDoIterative = kFALSE; 
55   fWriteDigits=kFALSE;
56   fNPatches=0;
57   fMemHandler = 0;
58   fHoughTransformer = 0;
59   fEval = 0;
60   fPeakFinder = 0;
61   fTracks = 0;
62   fMerger = 0;
63   fInterMerger = 0;
64   fGlobalMerger = 0;
65   fversion = 0;
66 }
67
68 AliL3Hough::AliL3Hough(Char_t *path,Bool_t binary,Int_t n_eta_segments,Int_t tversion=0)
69 {
70   //Default ctor.
71
72   fBinary = binary;
73   strcpy(fPath,path);
74   fNEtaSegments = n_eta_segments;
75   fAddHistograms = kFALSE;
76   fDoIterative = kFALSE; 
77   fWriteDigits = kFALSE;
78   fversion = tversion;
79 }
80
81 AliL3Hough::~AliL3Hough()
82 {
83   //dtor
84
85   CleanUp();
86   if(fMerger)
87     delete fMerger;
88   if(fInterMerger)
89     delete fInterMerger;
90   if(fPeakFinder)
91     delete fPeakFinder;
92   if(fGlobalMerger)
93     delete fGlobalMerger;
94 }
95
96 void AliL3Hough::CleanUp()
97 {
98   //Cleanup memory
99   
100   for(Int_t i=0; i<fNPatches; i++)
101     {
102       if(fTracks[i]) delete fTracks[i];
103       if(fEval[i]) delete fEval[i];
104       if(fHoughTransformer[i]) delete fHoughTransformer[i];
105       if(fMemHandler[i]) delete fMemHandler[i];
106     }
107   
108   /*
109   if(fTracks) delete [] fTracks;
110   if(fEval) delete [] fEval;
111   if(fHoughTransformer) delete [] fHoughTransformer;
112   if(fMemHandler) delete [] fMemHandler;
113   */
114 }
115
116 void AliL3Hough::Init(Char_t *path,Bool_t binary,Int_t n_eta_segments,Bool_t bit8=kFALSE)
117 {
118   fBinary = binary;
119   strcpy(fPath,path);
120   fNEtaSegments = n_eta_segments;
121   fAddHistograms = kFALSE;
122   fDoIterative = kFALSE; 
123   fWriteDigits = kFALSE;
124   fUse8bits = bit8;
125   
126   AliL3Transform::Init(fPath);
127
128   fNPatches = AliL3Transform::GetNPatches();
129   fHoughTransformer = new AliL3HoughBaseTransformer*[fNPatches];
130   fMemHandler = new AliL3MemHandler*[fNPatches];
131   fTracks = new AliL3TrackArray*[fNPatches];
132   fEval = new AliL3HoughEval*[fNPatches];
133   for(Int_t i=0; i<fNPatches; i++)
134     {
135       switch (fversion){
136       case 1: 
137         fHoughTransformer[i] = new AliL3HoughTransformerVhdl(1,i,fNEtaSegments);
138         fHoughTransformer[i]->CreateHistograms(180,0.1,180,-90,90);
139         fHoughTransformer[i]->SetLowerThreshold(3);
140         break;
141       default:
142         fHoughTransformer[i] = new AliL3HoughTransformer(1,i,fNEtaSegments);
143         fHoughTransformer[i]->CreateHistograms(64,0.1,64,-30,30);
144         fHoughTransformer[i]->SetLowerThreshold(3);
145       }
146
147       fEval[i] = new AliL3HoughEval();
148       fTracks[i] = new AliL3TrackArray("AliL3HoughTrack");
149       if(fUse8bits)
150         fMemHandler[i] = new AliL3DataHandler();
151       else
152 #ifdef use_aliroot
153         {
154           fMemHandler[i] = new AliL3FileHandler();
155           if(!fBinary)
156             {
157               Char_t filename[100];
158               sprintf(filename,"%s/digitfile",fPath);
159               fMemHandler[i]->SetAliInput(filename);
160             }
161         }
162 #else
163       fMemHandler[i] = new AliL3MemHandler();
164 #endif
165     }
166   fPeakFinder = new AliL3HoughMaxFinder("KappaPhi",100);
167   fMerger = new AliL3HoughMerger(fNPatches);
168   fInterMerger = new AliL3HoughIntMerger();
169   fGlobalMerger = 0;
170 }
171
172 void AliL3Hough::Process(Int_t minslice,Int_t maxslice)
173 {
174   //Process all slices [minslice,maxslice].
175   fGlobalMerger = new AliL3HoughGlobalMerger(minslice,maxslice);
176   
177   for(Int_t i=minslice; i<=maxslice; i++)
178     {
179       ReadData(i);
180       Transform();
181       if(fAddHistograms)
182         AddAllHistograms();
183       FindTrackCandidates();
184       Evaluate();
185       fGlobalMerger->FillTracks(fTracks[0],i);
186     }
187 }
188
189 void AliL3Hough::ReadData(Int_t slice,Int_t eventnr=0)
190 {
191   //Read data from files, binary or root.
192   
193   fCurrentSlice = slice;
194   for(Int_t i=0; i<fNPatches; i++)
195     {
196       fMemHandler[i]->Free();
197       UInt_t ndigits=0;
198       AliL3DigitRowData *digits =0;
199       Char_t name[256];
200       fMemHandler[i]->Init(slice,i);
201       if(fBinary)//take input data from binary files
202         {
203           if(fUse8bits)
204             sprintf(name,"%sdigits_c8_%d_%d.raw",fPath,slice,i);
205           else
206             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,i);
207           fMemHandler[i]->SetBinaryInput(name);
208           digits = (AliL3DigitRowData *)fMemHandler[i]->CompBinary2Memory(ndigits);
209           fMemHandler[i]->CloseBinaryInput();
210         }
211       else //read data from root file
212         {
213 #ifdef use_aliroot
214           digits=(AliL3DigitRowData *)fMemHandler[i]->AliDigits2Memory(ndigits,eventnr);
215           fMemHandler[i]->FreeDigitsTree();
216 #else
217           cerr<<"You cannot read from rootfile now"<<endl;
218 #endif
219         }
220       fHoughTransformer[i]->SetInputData(ndigits,digits);
221     }
222 }
223
224 void AliL3Hough::Transform(Int_t row_range=-1)
225 {
226   //Transform all data given to the transformer within the given slice
227   //(after ReadData(slice))
228
229   for(Int_t i=0; i<fNPatches; i++)
230     {
231       fHoughTransformer[i]->Reset();//Reset the histograms
232       if(row_range < 0)
233         fHoughTransformer[i]->TransformCircle();
234       else
235         fHoughTransformer[i]->TransformCircleC(row_range);
236     }
237 }
238
239 void AliL3Hough::MergePatches()
240 {
241   if(fAddHistograms) //Nothing to merge here
242     return;
243   fMerger->MergePatches(kTRUE);
244 }
245
246 void AliL3Hough::MergeInternally()
247 {
248   if(fAddHistograms)
249     fInterMerger->FillTracks(fTracks[0]);
250   else
251     fInterMerger->FillTracks(fMerger->GetOutTracks());
252   
253   fInterMerger->MMerge();
254 }
255
256 void AliL3Hough::ProcessSliceIter()
257 {
258   //Process current slice (after ReadData(slice)) iteratively.
259   
260   for(Int_t i=0; i<fNPatches; i++)
261     {
262       ProcessPatchIter(i);
263       fMerger->FillTracks(fTracks[i],i); //Copy tracks to merger
264     }
265   
266 }
267
268 void AliL3Hough::ProcessPatchIter(Int_t patch)
269 {
270   //Process patch in a iterative way. 
271   //transform + peakfinding + evaluation + transform +...
272
273   Int_t num_of_tries = 10;
274   AliL3HoughBaseTransformer *tr = fHoughTransformer[patch];
275   AliL3TrackArray *tracks = fTracks[patch];
276   tracks->Reset();
277   AliL3HoughEval *ev = fEval[patch];
278   ev->InitTransformer(tr);
279   ev->RemoveFoundTracks();
280   ev->SetNumOfRowsToMiss(2);
281   ev->SetNumOfPadsToLook(2);
282   AliL3Histogram *hist;
283   for(Int_t t=0; t<num_of_tries; t++)
284     {
285       tr->Reset();
286       tr->TransformCircle();
287       for(Int_t i=0; i<fNEtaSegments; i++)
288         {
289           hist = tr->GetHistogram(i);
290           if(hist->GetNEntries()==0) continue;
291           fPeakFinder->SetHistogram(hist);
292           //Int_t n=1;
293           Float_t x,y;
294           //fPeakFinder->FindAbsMaxima(*x,*y);
295           fPeakFinder->FindPeak(3,0.95,5,x,y);
296           AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->NextTrack();
297           track->SetTrackParameters(x,y,1);
298           if(!ev->LookInsideRoad(track,i))
299             {   
300               tracks->Remove(tracks->GetNTracks()-1);
301               tracks->Compress();
302             }
303         }
304     }
305   LOG(AliL3Log::kInformational,"AliL3Hough::ProcessPatch","NTracks")
306     <<AliL3Log::kDec<<"Found "<<tracks->GetNTracks()<<" tracks in patch "<<patch<<ENDLOG;
307 }
308
309
310 void AliL3Hough::AddAllHistograms()
311 {
312   //Add the histograms within one etaslice.
313   //Resulting histogram are in patch=0.
314
315   for(Int_t i=0; i<fNEtaSegments; i++)
316     {
317       AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(i);
318       for(Int_t j=1; j<fNPatches; j++)
319         {
320           AliL3Histogram *hist = fHoughTransformer[j]->GetHistogram(i);
321           hist0->Add(hist);
322         }
323     }
324   fAddHistograms = kTRUE;
325 }
326
327 void AliL3Hough::FindTrackCandidates()
328 {
329   //Look for peaks in histograms, and find the track candidates
330   
331   Int_t n_patches;
332   if(fAddHistograms)
333     n_patches = 1; //Histograms have been added.
334   else
335     n_patches = fNPatches;
336
337   
338   for(Int_t i=0; i<n_patches; i++)
339     {
340       AliL3HoughBaseTransformer *tr = fHoughTransformer[i];
341       Double_t eta_slice = (tr->GetEtaMax()-tr->GetEtaMin())/tr->GetNEtaSegments();
342       fTracks[i]->Reset();
343       for(Int_t j=0; j<fNEtaSegments; j++)
344         {
345           AliL3Histogram *hist = tr->GetHistogram(j);
346           if(hist->GetNEntries()==0) continue;
347           fPeakFinder->Reset();
348           fPeakFinder->SetHistogram(hist);
349           fPeakFinder->FindMaxima(0,0); //Simple maxima finder
350           
351           //fPeakFinder->FindAbsMaxima();
352           for(Int_t k=0; k<fPeakFinder->GetEntries(); k++)
353             {
354               if(fPeakFinder->GetWeight(k) == 0) continue;
355               AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
356               track->SetTrackParameters(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetWeight(k));
357               track->SetEtaIndex(j);
358               Double_t eta = (Double_t)((j+0.5)*eta_slice);
359               if(fCurrentSlice > 17) eta*=-1;
360               track->SetEta(eta);
361               track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));
362             }
363         }
364       fTracks[i]->QSort();
365     }
366 }
367
368 void AliL3Hough::InitEvaluate()
369 {
370   //Pass the transformer objects to the AliL3HoughEval objects:
371   //This will provide the evaluation objects with all the necessary
372   //data and parameters it needs.
373   
374   for(Int_t i=0; i<fNPatches; i++) 
375     fEval[i]->InitTransformer(fHoughTransformer[i]);
376 }
377
378 void AliL3Hough::Evaluate(Int_t road_width)
379 {
380   //Evaluate the tracks, by looking along the road in the raw data.
381   //If track does not cross all padrows - rows2miss, it is removed from the arrray.
382   
383   if(!fTracks[0])
384     {
385       LOG(AliL3Log::kError,"AliL3Hough::Evaluate","Track Array")
386         <<"No tracks to work with..."<<ENDLOG;
387       return;
388     }
389   
390   InitEvaluate();
391   
392   AliL3TrackArray *tracks;
393   for(Int_t i=0; i<fNPatches; i++)
394     {
395       fEval[i]->InitTransformer(fHoughTransformer[i]);
396       fEval[i]->SetNumOfRowsToMiss(2);
397       fEval[i]->SetNumOfPadsToLook(road_width);
398       if(fAddHistograms)
399         tracks = fTracks[0];
400       else
401         tracks = fTracks[i];
402       for(Int_t j=0; j<tracks->GetNTracks(); j++)
403         {
404           AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
405           if(!track)
406             {
407               LOG(AliL3Log::kWarning,"AliL3Hough::Evaluate","Track array")
408                 <<"Track object missing!"<<ENDLOG;
409               continue;
410             }
411           
412           if(!fEval[i]->LookInsideRoad(track,track->GetEtaIndex()))
413             tracks->Remove(j);
414           if(fAddHistograms)
415             track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));//All rows included
416         }
417       tracks->Compress();
418       tracks->QSort(); //Sort the tracks according to weight
419       
420       if(!fAddHistograms)
421         fMerger->FillTracks(tracks,i); //Copy tracks to the track merger
422     }
423   
424 }
425
426 void AliL3Hough::EvaluateWithEta()
427 {
428
429   
430   if(!fTracks[0])
431     {
432       printf("AliL3Hough::EvaluateWithEta: NO TRACKS\n");
433       return;
434     }
435   printf("Number of tracks before evaluation %d\n",fTracks[0]->GetNTracks());
436  
437   for(Int_t i=0; i<fNPatches; i++)
438     {
439       fEval[i]->InitTransformer(fHoughTransformer[i]);
440       fEval[i]->FindEta(fTracks[0]);
441     }
442   fMerger->FillTracks(fTracks[0],0);
443 }
444
445 void AliL3Hough::WriteTracks(Int_t slice,Char_t *path)
446 {
447   //Write the tracks in slice
448   
449   AliL3MemHandler *mem = new AliL3MemHandler();
450   Char_t fname[100];
451   if(fAddHistograms)
452     {
453       sprintf(fname,"%s/tracks_ho_%d.raw",path,slice);
454       mem->SetBinaryOutput(fname);
455       mem->TrackArray2Binary(fTracks[0]);
456       mem->CloseBinaryOutput();
457     }
458   else 
459     {
460       for(Int_t i=0; i<fNPatches; i++)
461         {
462           sprintf(fname,"%s/tracks_ho_%d_%d.raw",path,slice,i);
463           mem->SetBinaryOutput(fname);
464           mem->TrackArray2Binary(fTracks[i]);
465           mem->CloseBinaryOutput();
466         }
467     }
468   delete mem;
469   
470 }
471
472 void AliL3Hough::WriteDigits(Char_t *outfile)
473 {
474 #ifdef use_aliroot  
475   //Write the current data to a new rootfile.
476
477   for(Int_t i=0; i<fNPatches; i++)
478     {
479       AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fHoughTransformer[i]->GetDataPointer();
480       fMemHandler[i]->AliDigits2RootFile(tempPt,outfile);
481     }
482 #else
483   cerr<<"AliL3Hough::WriteDigits : You need to compile with AliROOT!"<<endl;
484   return;
485 #endif  
486 }
487