Bugfix in destructor etc.
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Hough.cxx
1 //Author:        Anders Strand Vestbo
2 //Last Modified: 28.6.01
3
4 #include <string.h>
5 #include <TCanvas.h>
6 #include <TFile.h>
7
8 #include "AliL3Logging.h"
9 #include "AliL3Histogram.h"
10 #include "AliL3Hough.h"
11 #include "AliL3HoughTransformer.h"
12 #include "AliL3HoughMaxFinder.h"
13 #include "AliL3FileHandler.h"
14 #include "AliL3DigitData.h"
15 #include "AliL3HoughEval.h"
16 #include "AliL3Transform.h"
17 #include "AliL3Defs.h"
18 #include "AliL3TrackArray.h"
19 #include "AliL3HoughTrack.h"
20
21 ClassImp(AliL3Hough)
22
23 AliL3Hough::AliL3Hough()
24 {
25   fBinary = kFALSE;
26   fNEtaSegments = 0;
27   fAddHistograms = kFALSE;
28   fRemoveFoundTracks = kFALSE; 
29   fWriteDigits=kFALSE;
30 }
31
32
33 AliL3Hough::AliL3Hough(Char_t *path,Bool_t binary,Int_t n_eta_segments)
34 {
35   fBinary = binary;
36   strcpy(fPath,path);
37   fNEtaSegments = n_eta_segments;
38   fAddHistograms = kFALSE;
39   fRemoveFoundTracks = kFALSE; 
40   fWriteDigits = kFALSE;
41   Init();
42 }
43
44
45 AliL3Hough::~AliL3Hough()
46 {
47   if(fMemHandler)
48     DeleteMemory();
49   if(fHoughTransformer)
50     DeleteTransformers();
51   if(fEval)
52     DeleteEval();
53   if(fPeakFinder)
54     delete fPeakFinder;
55   if(fTracks)
56     delete fTracks;
57   if(fRootFile)
58     {
59       fRootFile->Close();
60       delete fRootFile;
61     }
62 }
63
64 void AliL3Hough::DeleteEval()
65 {
66   for(Int_t i=0; i<NPatches; i++)
67     {
68       if(!fEval[i]) continue;
69       delete fEval[i];
70     }
71   delete [] fEval;
72 }
73
74 void AliL3Hough::DeleteTransformers()
75 {
76   for(Int_t i=0; i<NPatches; i++)
77     {
78       if(!fHoughTransformer[i]) continue;
79       delete fHoughTransformer[i];
80     }
81   delete [] fHoughTransformer;
82 }
83
84 void AliL3Hough::DeleteMemory()
85 {
86   for(Int_t i=0; i<NPatches; i++)
87     {
88       if(!fMemHandler[i]) continue;
89       delete fMemHandler[i];
90     }
91   delete [] fMemHandler;
92 }
93
94 void AliL3Hough::Init()
95 {
96   fHoughTransformer = new AliL3HoughTransformer*[NPatches];
97   fMemHandler = new AliL3FileHandler*[NPatches];
98   for(Int_t i=0; i<NPatches; i++)
99     {
100       fHoughTransformer[i] = new AliL3HoughTransformer(1,i,fNEtaSegments);
101       fHoughTransformer[i]->CreateHistograms(64,-0.003,0.003,64,-0.26,0.26);
102       fHoughTransformer[i]->SetThreshold(3);
103       fMemHandler[i] = new AliL3FileHandler();
104       if(!fBinary)
105         fMemHandler[i]->SetAliInput(fPath);
106     }
107   fPeakFinder = new AliL3HoughMaxFinder("KappaPhi");
108 }
109
110 void AliL3Hough::Process(Int_t minslice,Int_t maxslice)
111 {
112   //Process all slices [minslice,maxslice].
113
114   for(Int_t i=minslice; i<=maxslice; i++)
115     {
116       TransformSlice(i);
117       if(fAddHistograms)
118         AddAllHistograms();
119       FindTrackCandidates();
120       Evaluate(fRemoveFoundTracks);
121       if(fWriteDigits)
122         WriteDigits();
123     }
124 }
125
126 void AliL3Hough::TransformSlice(Int_t slice)
127 {
128   
129   for(Int_t i=0; i<NPatches; i++)
130     {
131       //Reset memories
132       fHoughTransformer[i]->Reset();      
133       fMemHandler[i]->Free();
134       UInt_t ndigits=0;
135       AliL3DigitRowData *digits =0;
136       Char_t name[256];
137       if(fBinary)//take input data from binary files
138         {
139           sprintf(name,"%sdigits_%d_%d.raw",fPath,slice,i);
140           fMemHandler[i]->SetBinaryInput(name);
141           digits = (AliL3DigitRowData *)fMemHandler[i]->CompBinary2Memory(ndigits);
142           fMemHandler[i]->CloseBinaryInput();
143         }
144       else //read data from root file
145         {
146           fMemHandler[i]->Init(slice,i,NRows[i]);
147           digits=(AliL3DigitRowData *)fMemHandler[i]->AliDigits2Memory(ndigits); 
148         }
149       fHoughTransformer[i]->SetInputData(ndigits,digits);
150       fHoughTransformer[i]->TransformCircle();
151     }
152   
153 }
154
155 AliL3Histogram *AliL3Hough::AddHistograms(Int_t eta_index)
156 {
157
158   AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(eta_index);
159   for(Int_t i=1; i<NPatches; i++)
160     {
161       AliL3Histogram *hist = fHoughTransformer[i]->GetHistogram(eta_index);
162       hist0->Add(hist);
163     }
164   
165   return hist0;
166 }
167
168 void AliL3Hough::AddAllHistograms()
169 {
170   //Add the histograms within one etaslice.
171   //Resulting histogram are in patch=0.
172   
173   LOG(AliL3Log::kDebug,"AliL3Hough::AddAllHistograms","Progress")
174     <<"Adding all histograms"<<ENDLOG;
175   for(Int_t i=0; i<fNEtaSegments; i++)
176     {
177       AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(i);
178       for(Int_t j=1; j<NPatches; j++)
179         {
180           AliL3Histogram *hist = fHoughTransformer[j]->GetHistogram(i);
181           hist0->Add(hist);
182         }
183     }
184 }
185
186 void AliL3Hough::FindTrackCandidates()
187 {
188   //Look for peaks in histograms, and find the track candidates
189   
190   if(fTracks)
191   {
192     LOG(AliL3Log::kDebug,"AliL3Hough::FindTrackCandidates","Track array")
193       <<"Deleting old track array"<<ENDLOG;
194     delete fTracks;
195   }
196   fTracks = new AliL3TrackArray("AliL3HoughTrack");
197   
198   Int_t n_patches;
199   if(fAddHistograms)
200     n_patches = 1; //Histograms has been added.
201   else
202     n_patches = NPatches;
203
204   for(Int_t i=0; i<n_patches; i++)
205     {
206       AliL3HoughTransformer *tr = fHoughTransformer[i];
207       for(Int_t j=0; j<fNEtaSegments; j++)
208         {
209           AliL3Histogram *hist = tr->GetHistogram(j);
210           fPeakFinder->SetHistogram(hist);
211           Int_t n=10;
212           Float_t x[10];
213           Float_t y[10];
214           fPeakFinder->FindPeak1(x,y,n);
215           for(Int_t k=0; k<n; k++)
216             {
217               AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks->NextTrack();
218               track->SetTrackParameters(x[k],y[k],1);
219               track->SetEtaIndex(j);
220             }
221         }
222     }
223   
224 }
225
226 void AliL3Hough::Evaluate(Bool_t remove)
227 {
228   //Evaluate the tracks, by looking along the road in the raw data.
229   //You may choose to remove the found tracks from the image.
230   
231   if(!fTracks)
232     {
233       LOG(AliL3Log::kError,"AliL3Hough::Evaluate","Track array")
234         <<AliL3Log::kHex<<"No tracks to work on "<<(Int_t)fTracks<<ENDLOG;
235       return;
236     }
237   
238   if(fEval)
239     {
240       LOG(AliL3Log::kDebug,"AliL3Hough::Evaluate","Evaluate object")
241         <<"Deleting old AliL3HoughEval objects"<<ENDLOG;
242       DeleteEval();
243     }
244   
245   fEval = new AliL3HoughEval*[NPatches];
246   for(Int_t i=0; i<NPatches; i++)
247     {
248       fEval[i] = new AliL3HoughEval(fHoughTransformer[i]);
249       if(remove)
250         fEval[i]->RemoveFoundTracks();
251       for(Int_t j=0; j<fTracks->GetNTracks(); j++)
252         {
253           AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks->GetCheckedTrack(j);
254           if(!track)
255             {
256               printf("AliL3Hough::Evaluate : Missing track object...\n");
257               continue;
258             }
259           //fEval[i]->LookInsideRoad(track,track->GetEtaIndex());
260           if(!fEval[i]->LookInsideRoad(track,track->GetEtaIndex()))
261             fTracks->Remove(j);
262         }
263       fTracks->Compress();
264     }
265   
266 }
267
268 void AliL3Hough::WriteDigits(Char_t *outfile)
269 {
270   //Write the current data to a new rootfile.
271
272   for(Int_t i=0; i<NPatches; i++)
273     {
274       AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fHoughTransformer[i]->GetDataPointer();
275       fMemHandler[i]->AliDigits2RootFile(tempPt,outfile);
276     }
277   
278 }