Bugfix in destructor etc.
[u/mrichter/AliRoot.git] / HLT / hough / AliL3Hough.cxx
CommitLineData
f80b98cb 1//Author: Anders Strand Vestbo
2//Last Modified: 28.6.01
3
f000f8a5 4#include <string.h>
f80b98cb 5#include <TCanvas.h>
6#include <TFile.h>
f000f8a5 7
a6008206 8#include "AliL3Logging.h"
f80b98cb 9#include "AliL3Histogram.h"
f000f8a5 10#include "AliL3Hough.h"
11#include "AliL3HoughTransformer.h"
12#include "AliL3HoughMaxFinder.h"
f80b98cb 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"
f000f8a5 20
21ClassImp(AliL3Hough)
22
23AliL3Hough::AliL3Hough()
24{
a6008206 25 fBinary = kFALSE;
26 fNEtaSegments = 0;
27 fAddHistograms = kFALSE;
28 fRemoveFoundTracks = kFALSE;
29 fWriteDigits=kFALSE;
f000f8a5 30}
31
32
4fc9a6a4 33AliL3Hough::AliL3Hough(Char_t *path,Bool_t binary,Int_t n_eta_segments)
f000f8a5 34{
4fc9a6a4 35 fBinary = binary;
36 strcpy(fPath,path);
f80b98cb 37 fNEtaSegments = n_eta_segments;
a6008206 38 fAddHistograms = kFALSE;
39 fRemoveFoundTracks = kFALSE;
40 fWriteDigits = kFALSE;
4fc9a6a4 41 Init();
f000f8a5 42}
43
44
45AliL3Hough::~AliL3Hough()
46{
f80b98cb 47 if(fMemHandler)
4fc9a6a4 48 DeleteMemory();
49 if(fHoughTransformer)
50 DeleteTransformers();
a6008206 51 if(fEval)
52 DeleteEval();
53 if(fPeakFinder)
54 delete fPeakFinder;
55 if(fTracks)
56 delete fTracks;
4fc9a6a4 57 if(fRootFile)
58 {
59 fRootFile->Close();
60 delete fRootFile;
61 }
f000f8a5 62}
63
a6008206 64void 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
4fc9a6a4 74void AliL3Hough::DeleteTransformers()
f000f8a5 75{
4fc9a6a4 76 for(Int_t i=0; i<NPatches; i++)
f80b98cb 77 {
4fc9a6a4 78 if(!fHoughTransformer[i]) continue;
a6008206 79 delete fHoughTransformer[i];
f80b98cb 80 }
4fc9a6a4 81 delete [] fHoughTransformer;
f000f8a5 82}
83
4fc9a6a4 84void AliL3Hough::DeleteMemory()
f000f8a5 85{
4fc9a6a4 86 for(Int_t i=0; i<NPatches; i++)
87 {
88 if(!fMemHandler[i]) continue;
89 delete fMemHandler[i];
90 }
91 delete [] fMemHandler;
f000f8a5 92}
93
4fc9a6a4 94void AliL3Hough::Init()
f000f8a5 95{
4fc9a6a4 96 fHoughTransformer = new AliL3HoughTransformer*[NPatches];
97 fMemHandler = new AliL3FileHandler*[NPatches];
98 for(Int_t i=0; i<NPatches; i++)
f80b98cb 99 {
4fc9a6a4 100 fHoughTransformer[i] = new AliL3HoughTransformer(1,i,fNEtaSegments);
a6008206 101 fHoughTransformer[i]->CreateHistograms(64,-0.003,0.003,64,-0.26,0.26);
102 fHoughTransformer[i]->SetThreshold(3);
4fc9a6a4 103 fMemHandler[i] = new AliL3FileHandler();
a6008206 104 if(!fBinary)
105 fMemHandler[i]->SetAliInput(fPath);
106 }
107 fPeakFinder = new AliL3HoughMaxFinder("KappaPhi");
108}
109
110void 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();
f80b98cb 123 }
4fc9a6a4 124}
f80b98cb 125
4fc9a6a4 126void AliL3Hough::TransformSlice(Int_t slice)
127{
f80b98cb 128
4fc9a6a4 129 for(Int_t i=0; i<NPatches; i++)
f80b98cb 130 {
a6008206 131 //Reset memories
132 fHoughTransformer[i]->Reset();
4fc9a6a4 133 fMemHandler[i]->Free();
134 UInt_t ndigits=0;
135 AliL3DigitRowData *digits =0;
136 Char_t name[256];
a6008206 137 if(fBinary)//take input data from binary files
4fc9a6a4 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 {
4fc9a6a4 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();
f80b98cb 151 }
4fc9a6a4 152
153}
154
a6008206 155AliL3Histogram *AliL3Hough::AddHistograms(Int_t eta_index)
4fc9a6a4 156{
a6008206 157
158 AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(eta_index);
4fc9a6a4 159 for(Int_t i=1; i<NPatches; i++)
f80b98cb 160 {
a6008206 161 AliL3Histogram *hist = fHoughTransformer[i]->GetHistogram(eta_index);
4fc9a6a4 162 hist0->Add(hist);
f80b98cb 163 }
f80b98cb 164
4fc9a6a4 165 return hist0;
166}
167
a6008206 168void AliL3Hough::AddAllHistograms()
4fc9a6a4 169{
a6008206 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
186void 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");
4fc9a6a4 197
a6008206 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
226void 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];
4fc9a6a4 246 for(Int_t i=0; i<NPatches; i++)
f80b98cb 247 {
a6008206 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();
f80b98cb 264 }
f000f8a5 265
a6008206 266}
f80b98cb 267
a6008206 268void 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
4fc9a6a4 278}