acd660224a89ccf0d9d7efe531b66e73a0c7237e
[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]->SetThreshold(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]->SetThreshold(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             fMemHandler[i]->SetAliInput(fPath);
157         }
158 #else
159       fMemHandler[i] = new AliL3MemHandler();
160 #endif
161     }
162   fPeakFinder = new AliL3HoughMaxFinder("KappaPhi",100);
163   fMerger = new AliL3HoughMerger(fNPatches);
164   fInterMerger = new AliL3HoughIntMerger();
165   fGlobalMerger = 0;
166 }
167
168 void AliL3Hough::Process(Int_t minslice,Int_t maxslice)
169 {
170   //Process all slices [minslice,maxslice].
171   fGlobalMerger = new AliL3HoughGlobalMerger(minslice,maxslice);
172   
173   for(Int_t i=minslice; i<=maxslice; i++)
174     {
175       ReadData(i);
176       Transform();
177       if(fAddHistograms)
178         AddAllHistograms();
179       FindTrackCandidates();
180       Evaluate();
181       fGlobalMerger->FillTracks(fTracks[0],i);
182     }
183 }
184
185 void AliL3Hough::ReadData(Int_t slice,Int_t eventnr=0)
186 {
187   //Read data from files, binary or root.
188
189   for(Int_t i=0; i<fNPatches; i++)
190     {
191       fMemHandler[i]->Free();
192       UInt_t ndigits=0;
193       AliL3DigitRowData *digits =0;
194       Char_t name[256];
195       fMemHandler[i]->Init(slice,i);
196       if(fBinary)//take input data from binary files
197         {
198           if(fUse8bits)
199             sprintf(name,"%sdigits_c8_%d_%d.raw",fPath,slice,i);
200           else
201             sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,i);
202           fMemHandler[i]->SetBinaryInput(name);
203           digits = (AliL3DigitRowData *)fMemHandler[i]->CompBinary2Memory(ndigits);
204           fMemHandler[i]->CloseBinaryInput();
205         }
206       else //read data from root file
207         {
208 #ifdef use_aliroot
209           digits=(AliL3DigitRowData *)fMemHandler[i]->AliDigits2Memory(ndigits,eventnr); 
210           fMemHandler[i]->FreeDigitsTree();
211 #else
212           cerr<<"You cannot read from rootfile now"<<endl;
213 #endif
214         }
215       fHoughTransformer[i]->SetInputData(ndigits,digits);
216     }
217 }
218
219 void AliL3Hough::Transform(Int_t row_range=-1)
220 {
221   //Transform all data given to the transformer within the given slice
222   //(after ReadData(slice))
223
224   for(Int_t i=0; i<fNPatches; i++)
225     {
226       fHoughTransformer[i]->Reset();//Reset the histograms
227       if(row_range < 0)
228         fHoughTransformer[i]->TransformCircle();
229       else
230         fHoughTransformer[i]->TransformCircleC(row_range);
231     }
232 }
233
234 void AliL3Hough::MergePatches()
235 {
236   if(fAddHistograms) //Nothing to merge here
237     return;
238   fMerger->MergePatches(kTRUE);
239 }
240
241 void AliL3Hough::MergeInternally()
242 {
243   if(fAddHistograms)
244     fInterMerger->FillTracks(fTracks[0]);
245   else
246     fInterMerger->FillTracks(fMerger->GetOutTracks());
247   
248   fInterMerger->MMerge();
249 }
250
251 void AliL3Hough::ProcessSliceIter()
252 {
253   //Process current slice (after ReadData(slice)) iteratively.
254   
255   for(Int_t i=0; i<fNPatches; i++)
256     {
257       ProcessPatchIter(i);
258       fMerger->FillTracks(fTracks[i],i); //Copy tracks to merger
259     }
260   
261 }
262
263 void AliL3Hough::ProcessPatchIter(Int_t patch)
264 {
265   //Process patch in a iterative way. 
266   //transform + peakfinding + evaluation + transform +...
267
268   Int_t num_of_tries = 10;
269   AliL3HoughBaseTransformer *tr = fHoughTransformer[patch];
270   AliL3TrackArray *tracks = fTracks[patch];
271   tracks->Reset();
272   AliL3HoughEval *ev = fEval[patch];
273   ev->InitTransformer(tr);
274   ev->RemoveFoundTracks();
275   ev->SetNumOfRowsToMiss(2);
276   ev->SetNumOfPadsToLook(2);
277   AliL3Histogram *hist;
278   for(Int_t t=0; t<num_of_tries; t++)
279     {
280       tr->Reset();
281       tr->TransformCircle();
282       for(Int_t i=0; i<fNEtaSegments; i++)
283         {
284           hist = tr->GetHistogram(i);
285           if(hist->GetNEntries()==0) continue;
286           fPeakFinder->SetHistogram(hist);
287           //Int_t n=1;
288           Float_t x,y;
289           //fPeakFinder->FindAbsMaxima(*x,*y);
290           fPeakFinder->FindPeak(3,0.95,5,x,y);
291           AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->NextTrack();
292           track->SetTrackParameters(x,y,1);
293           if(!ev->LookInsideRoad(track,i))
294             {   
295               tracks->Remove(tracks->GetNTracks()-1);
296               tracks->Compress();
297             }
298         }
299     }
300   LOG(AliL3Log::kInformational,"AliL3Hough::ProcessPatch","NTracks")
301     <<AliL3Log::kDec<<"Found "<<tracks->GetNTracks()<<" tracks in patch "<<patch<<ENDLOG;
302 }
303
304
305 void AliL3Hough::AddAllHistograms()
306 {
307   //Add the histograms within one etaslice.
308   //Resulting histogram are in patch=0.
309
310   for(Int_t i=0; i<fNEtaSegments; i++)
311     {
312       AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(i);
313       for(Int_t j=1; j<fNPatches; j++)
314         {
315           AliL3Histogram *hist = fHoughTransformer[j]->GetHistogram(i);
316           hist0->Add(hist);
317         }
318     }
319   fAddHistograms = kTRUE;
320 }
321
322 void AliL3Hough::FindTrackCandidates()
323 {
324   //Look for peaks in histograms, and find the track candidates
325   
326   Int_t n_patches;
327   if(fAddHistograms)
328     n_patches = 1; //Histograms have been added.
329   else
330     n_patches = fNPatches;
331
332   
333   for(Int_t i=0; i<n_patches; i++)
334     {
335       AliL3HoughBaseTransformer *tr = fHoughTransformer[i];
336       Double_t eta_slice = (tr->GetEtaMax()-tr->GetEtaMin())/tr->GetNEtaSegments();
337       fTracks[i]->Reset();
338       for(Int_t j=0; j<fNEtaSegments; j++)
339         {
340           AliL3Histogram *hist = tr->GetHistogram(j);
341           if(hist->GetNEntries()==0) continue;
342           fPeakFinder->Reset();
343           fPeakFinder->SetHistogram(hist);
344           fPeakFinder->FindMaxima(0,0); //Simple maxima finder
345           cout<<"Found "<<fPeakFinder->GetEntries()<<endl;
346
347           for(Int_t k=0; k<fPeakFinder->GetEntries(); k++)
348             {
349               if(fPeakFinder->GetWeight(k) == 0) continue;
350               AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
351               track->SetTrackParameters(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetWeight(k));
352               track->SetEtaIndex(j);
353               track->SetEta((Double_t)((j+0.5)*eta_slice));
354               track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));
355             }
356         }
357       fTracks[i]->QSort();
358     }
359 }
360
361 void AliL3Hough::InitEvaluate()
362 {
363   //Pass the transformer objects to the AliL3HoughEval objects:
364   //This will provide the evaluation objects with all the necessary
365   //data and parameters it needs.
366   
367   for(Int_t i=0; i<fNPatches; i++) 
368     fEval[i]->InitTransformer(fHoughTransformer[i]);
369 }
370
371 void AliL3Hough::Evaluate(Int_t road_width)
372 {
373   //Evaluate the tracks, by looking along the road in the raw data.
374   //If track does not cross all padrows - rows2miss, it is removed from the arrray.
375   
376   if(!fTracks[0])
377     {
378       LOG(AliL3Log::kError,"AliL3Hough::Evaluate","Track Array")
379         <<"No tracks to work with..."<<ENDLOG;
380       return;
381     }
382   
383   InitEvaluate();
384   
385   AliL3TrackArray *tracks;
386   for(Int_t i=0; i<fNPatches; i++)
387     {
388       fEval[i]->InitTransformer(fHoughTransformer[i]);
389       fEval[i]->SetNumOfRowsToMiss(2);
390       fEval[i]->SetNumOfPadsToLook(road_width);
391       if(fAddHistograms)
392         tracks = fTracks[0];
393       else
394         tracks = fTracks[i];
395       for(Int_t j=0; j<tracks->GetNTracks(); j++)
396         {
397           AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
398           if(!track)
399             {
400               LOG(AliL3Log::kWarning,"AliL3Hough::Evaluate","Track array")
401                 <<"Track object missing!"<<ENDLOG;
402               continue;
403             }
404           
405           if(!fEval[i]->LookInsideRoad(track,track->GetEtaIndex()))
406             tracks->Remove(j);
407           if(fAddHistograms)
408             track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));//All rows included
409         }
410       tracks->Compress();
411       tracks->QSort(); //Sort the tracks according to weight
412       
413       if(!fAddHistograms)
414         fMerger->FillTracks(tracks,i); //Copy tracks to the track merger
415     }
416   
417 }
418
419 void AliL3Hough::EvaluateWithEta()
420 {
421
422   
423   if(!fTracks[0])
424     {
425       printf("AliL3Hough::EvaluateWithEta: NO TRACKS\n");
426       return;
427     }
428   printf("Number of tracks before evaluation %d\n",fTracks[0]->GetNTracks());
429  
430   for(Int_t i=0; i<fNPatches; i++)
431     {
432       fEval[i]->InitTransformer(fHoughTransformer[i]);
433       fEval[i]->FindEta(fTracks[0]);
434     }
435   fMerger->FillTracks(fTracks[0],0);
436 }
437
438 void AliL3Hough::WriteTracks(Int_t slice,Char_t *path)
439 {
440   //Write the tracks in slice
441   
442   AliL3MemHandler *mem = new AliL3MemHandler();
443   Char_t fname[100];
444   if(fAddHistograms)
445     {
446       sprintf(fname,"%s/tracks_ho_%d.raw",path,slice);
447       mem->SetBinaryOutput(fname);
448       mem->TrackArray2Binary(fTracks[0]);
449       mem->CloseBinaryOutput();
450     }
451   else 
452     {
453       for(Int_t i=0; i<fNPatches; i++)
454         {
455           sprintf(fname,"%s/tracks_ho_%d_%d.raw",path,slice,i);
456           mem->SetBinaryOutput(fname);
457           mem->TrackArray2Binary(fTracks[i]);
458           mem->CloseBinaryOutput();
459         }
460     }
461   delete mem;
462   
463 }
464
465 void AliL3Hough::WriteDigits(Char_t *outfile)
466 {
467 #ifdef use_aliroot  
468   //Write the current data to a new rootfile.
469
470   for(Int_t i=0; i<fNPatches; i++)
471     {
472       AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fHoughTransformer[i]->GetDataPointer();
473       fMemHandler[i]->AliDigits2RootFile(tempPt,outfile);
474     }
475 #else
476   cerr<<"AliL3Hough::WriteDigits : You need to compile with AliROOT!"<<endl;
477   return;
478 #endif  
479 }
480