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