]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/hough/AliL3HoughTransformerLUT.cxx
Moved to the latest version of the HLT code in Bergen.
[u/mrichter/AliRoot.git] / HLT / hough / AliL3HoughTransformerLUT.cxx
CommitLineData
3e87ef69 1// @(#) $Id$
d5d754e5 2
3// Author: Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
3e87ef69 4//*-- Copyright &copy ALICE HLT Group
d5d754e5 5
6#include "AliL3StandardIncludes.h"
7
8#include "AliL3Logging.h"
9#include "AliL3HoughTransformerLUT.h"
10#include "AliL3Transform.h"
11#include "AliL3MemHandler.h"
12#include "AliL3DigitData.h"
13#include "AliL3Histogram.h"
1f1942b8 14#include "AliL3HistogramAdaptive.h"
d5d754e5 15
16#if GCCVERSION == 3
17using namespace std;
18#endif
19
1f1942b8 20/** /class AliL3HoughTransformerLUT
21//<pre>
d5d754e5 22//_____________________________________________________________
23// AliL3HoughTransformerLUT
24//
25// Hough transformation class using Look-UP-Tables
26//
1f1942b8 27//</pre>
28*/
d5d754e5 29
30ClassImp(AliL3HoughTransformerLUT)
31
32AliL3HoughTransformerLUT::AliL3HoughTransformerLUT() : AliL3HoughBaseTransformer()
33{
1f1942b8 34 fParamSpace=0;
d5d754e5 35 fMinRow=0;
36 fMaxRow=0;
37
38 fNRows=0;
39 fNEtas=0;
40 fNPhi0=0;
41 fSector=0;
3bcf971b 42 fSlice=0;
d5d754e5 43 fSectorRow=0;
44 fZSign=0;
45 fZLengthPlusOff=0;
46 fTimeWidth=0;
47 fPadPitch=0;
48 fEtaSlice=0;
49
50 fLUTX=0;
51 fLUTY=0;
52 fLUTEta=0;
3e87ef69 53 fLUTEtaReal=0;
d5d754e5 54 fLUTphi0=0;
55 fLUT2sinphi0=0;
56 fLUT2cosphi0=0;
18659a6b 57 fLUTKappa=0;
700db259 58
18659a6b 59 fLastPad=0;
60 fLastIndex=0;
3e87ef69 61 fAccCharge=0;
1f1942b8 62 fX=fY=0.;
d5d754e5 63}
64
65AliL3HoughTransformerLUT::AliL3HoughTransformerLUT(Int_t slice,Int_t patch,Int_t n_eta_segments) : AliL3HoughBaseTransformer(slice,patch,n_eta_segments)
66{
1f1942b8 67 fParamSpace=0;
3bcf971b 68 fMinRow=0;
69 fMaxRow=0;
70
71 fNRows=0;
72 fNEtas=0;
73 fNPhi0=0;
74 fSector=0;
75 fSectorRow=0;
76 fZSign=0;
77 fZLengthPlusOff=0;
78 fTimeWidth=0;
79 fPadPitch=0;
80 fEtaSlice=0;
81
82 fLUTX=0;
83 fLUTY=0;
84 fLUTEta=0;
3e87ef69 85 fLUTEtaReal=0;
3bcf971b 86 fLUTphi0=0;
87 fLUT2sinphi0=0;
88 fLUT2cosphi0=0;
18659a6b 89 fLUTKappa=0;
90
91 fLastPad=0;
92 fLastIndex=0;
3e87ef69 93 fAccCharge=0;
1f1942b8 94 fX=fY=0.;
d5d754e5 95
96 Init(slice,patch,n_eta_segments);
97}
98
99AliL3HoughTransformerLUT::~AliL3HoughTransformerLUT()
100{
101 DeleteHistograms();
102
d5d754e5 103 if(fNRows){
104 delete[] fLUTX;
105 delete[] fLUTY;
106 fNRows=0;
107 }
3bcf971b 108
d5d754e5 109 if(fNEtas){
110 delete[] fLUTEta;
3e87ef69 111 delete[] fLUTEtaReal;
d5d754e5 112 fNEtas=0;
113 }
114}
115
116void AliL3HoughTransformerLUT::DeleteHistograms()
117{
118 if(fNPhi0){
119 delete[] fLUT2sinphi0;
120 delete[] fLUT2cosphi0;
121 delete[] fLUTphi0;
18659a6b 122 delete[] fLUTKappa;
d5d754e5 123 fNPhi0=0;
124 fLUTphi0=0;
125 fLUT2sinphi0=0;
126 fLUT2cosphi0=0;
18659a6b 127 fLUTKappa=0;
d5d754e5 128 }
129
130 if(!fParamSpace){
3bcf971b 131 for(Int_t i=0; i<fNEtas; i++)
d5d754e5 132 {
133 if(!fParamSpace[i]) continue;
134 delete fParamSpace[i];
135 }
136 delete [] fParamSpace;
137 }
138}
139
3e87ef69 140void AliL3HoughTransformerLUT::Init(Int_t slice,Int_t patch,Int_t n_eta_segments,Int_t n_seqs)
d5d754e5 141{
142 AliL3HoughBaseTransformer::Init(slice,patch,n_eta_segments);
143
144 //delete old LUT tables
145 if(fNRows){
146 fNRows=0;
147 delete[] fLUTX;
148 delete[] fLUTY;
149 }
150 if(fNEtas){
151 delete[] fLUTEta;
3e87ef69 152 delete[] fLUTEtaReal;
d5d754e5 153 fNEtas=0;
154 }
155
156 //member values
157 fMinRow=AliL3Transform::GetFirstRow(patch);;
158 fMaxRow=AliL3Transform::GetLastRow(patch);;
159 fNRows=AliL3Transform::GetNRows(patch);;
160 fNEtas=n_eta_segments;
3bcf971b 161 if(fNEtas!=GetNEtaSegments()) {
162 cerr << "AliL3HoughTransformerLUT::Init -> Error: Number of Etas must be equal!" << endl;
163 exit(1);
164 }
d5d754e5 165
166 AliL3Transform::Slice2Sector(slice,fMinRow,fSector,fSectorRow);
1f1942b8 167 fZSign = slice < 18 ? 1:-1; //see AliL3Transformer
3bcf971b 168 fSlice = slice;
d5d754e5 169 fZLengthPlusOff=AliL3Transform::GetZLength()+AliL3Transform::GetZOffset();
170 fTimeWidth=AliL3Transform::GetZWidth();
171
172 fPadPitch=0;
173 if(fSector<AliL3Transform::GetNSectorLow())
174 fPadPitch=AliL3Transform::GetPadPitchWidthLow();
175 else
176 fPadPitch=AliL3Transform::GetPadPitchWidthUp();
177
178 Float_t etamax_=GetEtaMax();
179 Float_t etamin_=GetEtaMin();
180 fEtaSlice=(etamax_-etamin_)/n_eta_segments;
181
182 //lookup tables for X and Y
183 fLUTX=new Float_t[fNRows];
184 fLUTY=new Float_t[fNRows];
185 for(Int_t rr=0;rr<fNRows;rr++){
186 fLUTX[rr]=Float_t(AliL3Transform::Row2X(rr+fMinRow));
187 fLUTY[rr]=Float_t(0.5*(AliL3Transform::GetNPads(rr+fMinRow)-1)*fPadPitch);
188 //cout << rr << ": " << (Float_t)fLUTX[rr] << " " << (Float_t)fLUTY[rr] << endl;
189 }
190
191 //lookup tables for rz2s <=> etas
1f1942b8 192 //will only be used ifdef FULLLUT
d5d754e5 193 fLUTEta=new Float_t[fNEtas];
3e87ef69 194 fLUTEtaReal=new Float_t[fNEtas];
d5d754e5 195 for(Int_t rr=0;rr<fNEtas;rr++){
3bcf971b 196 Float_t eta=etamin_+(rr+1)*fEtaSlice;
197 fLUTEta[rr]=CalcRoverZ2(eta);
3e87ef69 198 fLUTEtaReal[rr]=eta-0.5*fEtaSlice;
199 //cout << rr << ": " << eta << " " << fLUTEtaReal[rr] << " " << GetEta(rr,fSlice) << " - " << fLUTEta[rr] << endl;
d5d754e5 200 }
201}
202
1f1942b8 203void AliL3HoughTransformerLUT::CreateHistograms(Float_t ptmin,Float_t ptmax,Float_t ptres,
204 Int_t nybin,Float_t psi)
205{
206 //Create histograms.
207 //_Only_ to be used in case of the adaptive histograms!
208 //phimax is given in radians!!
209
210 if(ptmin > ptmax)
211 {
212 cerr<<"AliL3HoughTransformerLUT::CreateHistograms: Error in ptrange "<<ptmin<<" "<<ptmax<<endl;
213 return;
214 }
215 if(psi < 0)
216 {
217 cerr<<"AliL3HoughTransformerLUT::CreateHistograms: Wrong psi-angle "<<psi<<endl;
218 return;
219 }
220
221 fParamSpace = new AliL3Histogram*[fNEtas];
222 Char_t histname[256];
223 for(Int_t i=0; i<GetNEtaSegments(); i++)
224 {
225 sprintf(histname,"paramspace_%d",i);
226 fParamSpace[i] = new AliL3HistogramAdaptive(histname,ptmin,ptmax,ptres,nybin,-psi,psi);
227 }
228
229 //create lookup table for sin and cos
230 fNPhi0=nybin;
231 fLUTphi0=new Float_t[fNPhi0];
232 fLUT2sinphi0=new Float_t[fNPhi0];
233 fLUT2cosphi0=new Float_t[fNPhi0];
234 fLUTKappa=new Float_t[fNPhi0];
235 AliL3Histogram *hist=fParamSpace[0];
236 Int_t i=0;
237 for(Int_t b=hist->GetFirstYbin(); b<=hist->GetLastYbin(); b++)
238 {
239 Float_t phi0 = hist->GetBinCenterY(b);
240 fLUTphi0[i]=phi0;
241 fLUT2sinphi0[i]=2.*sin(phi0);
242 fLUT2cosphi0[i]=2.*cos(phi0);
243 fLUTKappa[i]=0.;
244 i++;
245 //cout << i << ": " << fLUTphi0[i] << " " << fLUT2sinphi0[i] << " " << fLUT2cosphi0[i] << endl;
246 }
247}
248
045549b7 249void AliL3HoughTransformerLUT::CreateHistograms(Int_t nxbin,Float_t pt_min,Int_t nybin,Float_t phimin,Float_t phimax)
d5d754e5 250{
251 //Create the histograms (parameter space).
252 //These are 2D histograms, span by kappa (curvature of track) and phi0 (emission angle with x-axis).
253 //The arguments give the range and binning;
254 //nxbin = #bins in kappa
255 //nybin = #bins in phi0
256 //pt_min = mimium Pt of track (corresponding to maximum kappa)
257 //phi_min = mimimum phi0 (degrees)
258 //phi_max = maximum phi0 (degrees)
259
260 Double_t x = AliL3Transform::GetBFact()*AliL3Transform::GetBField()/pt_min;
1f1942b8 261 //Double_t torad = AliL3Transform::Pi()/180;
262 CreateHistograms(nxbin,-1.*x,x,nybin,phimin/**torad*/,phimax/**torad*/);
d5d754e5 263}
264
045549b7 265void AliL3HoughTransformerLUT::CreateHistograms(Int_t nxbin,Float_t xmin,Float_t xmax,Int_t nybin,Float_t ymin,Float_t ymax)
d5d754e5 266{
267 fParamSpace = new AliL3Histogram*[fNEtas];
268
269 Char_t histname[256];
270 for(Int_t i=0; i<fNEtas; i++)
271 {
272 sprintf(histname,"paramspace_%d",i);
273 fParamSpace[i] = new AliL3Histogram(histname,"",nxbin,xmin,xmax,nybin,ymin,ymax);
274 }
275
d5d754e5 276 //create lookup table for sin and cos
1f1942b8 277 fNPhi0=nybin;
d5d754e5 278 fLUTphi0=new Float_t[fNPhi0];
279 fLUT2sinphi0=new Float_t[fNPhi0];
280 fLUT2cosphi0=new Float_t[fNPhi0];
18659a6b 281 fLUTKappa=new Float_t[fNPhi0];
1f1942b8 282
283 AliL3Histogram *hist=fParamSpace[0];
284 Int_t i=0;
285 for(Int_t b=hist->GetFirstYbin(); b<=hist->GetLastYbin(); b++)
286 {
287 Float_t phi0 = hist->GetBinCenterY(b);
288 fLUTphi0[i]=phi0;
289 fLUT2sinphi0[i]=2.*sin(phi0);
290 fLUT2cosphi0[i]=2.*cos(phi0);
291 fLUTKappa[i]=0.;
292 //cout << i << ": " << fLUTphi0[i] << " " << fLUT2sinphi0[i] << " " << fLUT2cosphi0[i] << endl;
293 i++;
294 }
d5d754e5 295}
296
297void AliL3HoughTransformerLUT::Reset()
298{
299 //Reset all the histograms
300
301 if(!fParamSpace)
302 {
303 LOG(AliL3Log::kWarning,"AliL3HoughTransformer::Reset","Histograms")
304 <<"No histograms to reset"<<ENDLOG;
305 return;
306 }
307
3bcf971b 308 for(Int_t i=0; i<fNEtas; i++)
d5d754e5 309 fParamSpace[i]->Reset();
d5d754e5 310}
311
d5d754e5 312Int_t AliL3HoughTransformerLUT::GetEtaIndex(Double_t eta)
313{
314 //Return the histogram index of the corresponding eta.
3bcf971b 315
3e87ef69 316#ifdef FULLLUT
317 /* try to imitate a circuit -> should
318 go into the VHDL implementation of transformer */
3bcf971b 319 Float_t rz2=CalcRoverZ2(eta);
320 return FindIndex(rz2);
3e87ef69 321#else /* optimize for speed on the computer */
322 Double_t index = (eta-GetEtaMin())/fEtaSlice;
323 return (Int_t)index;
324#endif
3bcf971b 325}
326
1f1942b8 327AliL3Histogram *AliL3HoughTransformerLUT::GetHistogram(Int_t eta_index)
d5d754e5 328{
3bcf971b 329 if(!fParamSpace || eta_index >= fNEtas || eta_index < 0)
d5d754e5 330 return 0;
331 if(!fParamSpace[eta_index])
332 return 0;
333 return fParamSpace[eta_index];
334}
335
d5d754e5 336Double_t AliL3HoughTransformerLUT::GetEta(Int_t eta_index,Int_t slice)
337{
3bcf971b 338 if(eta_index >= fNEtas || eta_index < 0){
1f1942b8 339 LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::GetEta","Index") << "Index out of range."<<ENDLOG;
3bcf971b 340 return 0.;
341 }
342 if(slice != fSlice){
1f1942b8 343 LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::GetEta","Index") << "Given slice does not match internal slice."<<ENDLOG;
3bcf971b 344 return 0.;
345 }
346
3e87ef69 347 return(fLUTEtaReal[eta_index]);
348}
349
d5d754e5 350void AliL3HoughTransformerLUT::TransformCircle()
351{
d5d754e5 352 //Transform the input data with a circle HT.
3bcf971b 353 //The function loops over all the data, and transforms each pixel with the equation:
d5d754e5 354 //
3bcf971b 355 //kappa = 2/R^2 * (y*cos(phi0) - x*sin(phi0) )
d5d754e5 356 //
3bcf971b 357 //where R^2 = x^2 + y^2
d5d754e5 358 //
1f1942b8 359 //After a day of testing it is proven that this h
d5d754e5 360
d5d754e5 361 AliL3DigitRowData *tempPt = GetDataPointer();
362 if(!tempPt)
363 {
3bcf971b 364 LOG(AliL3Log::kError,"AliL3HoughTransformerLUT::TransformCircle","Data")
d5d754e5 365 <<"No input data "<<ENDLOG;
366 return;
367 }
1f1942b8 368
369 Int_t lowch=GetLowerThreshold();
370 Int_t highch=GetUpperThreshold();
371
d5d754e5 372 //Loop over the padrows:
3bcf971b 373 for(Int_t i=fMinRow, row=0; i<=fMaxRow; i++, row++)
d5d754e5 374 {
375 //Get the data on this padrow:
376 AliL3DigitData *digPt = tempPt->fDigitData;
377 if(i != (Int_t)tempPt->fRow)
378 {
3bcf971b 379 LOG(AliL3Log::kError,"AliL3HoughTransformerLUT::TransformCircle","Data")
b490510d 380 <<"AliL3HoughTransformerLUT::TransformCircle : Mismatching padrow numbering "<<i<<" != "<<(Int_t)tempPt->fRow<<ENDLOG;
d5d754e5 381 continue;
382 }
383
efb8e06f 384
385 //start a new row
18659a6b 386 fLastPad=-1;
1f1942b8 387 //store x for this row
388 fX = CalcX(row);
389 //accumulate charge per etaslice
3e87ef69 390 fAccCharge=0;
1f1942b8 391 //last histogram
392 fLastIndex=-1;
3e87ef69 393
d5d754e5 394 //Loop over the data on this padrow:
395 for(UInt_t j=0; j<tempPt->fNDigit; j++)
396 {
397 UShort_t charge = digPt[j].fCharge;
3bcf971b 398
399 //check threshold
1f1942b8 400 if((Int_t)charge <= lowch)
d5d754e5 401 continue;
1f1942b8 402 if ((Int_t)charge > highch)
403 charge=highch;
3bcf971b 404
405 UChar_t pad = digPt[j].fPad;
406 UShort_t time = digPt[j].fTime;
407
18659a6b 408 if(fLastPad!=pad){ //only update if necessary
3e87ef69 409
1f1942b8 410 //if there is accumalated charge,
411 //put it in the old histogram
412 //using the old X and Y values
413 if(fAccCharge>0) AddCurveToHistogram(-1); //fLastIndex will be set to -1
3e87ef69 414
415#ifdef FULLLUT
416 fLastIndex=fNEtas-1;
417#endif
418
1f1942b8 419 //calculate Y for the new pad
420 fY = CalcY(pad,row);
421 //remember new pad
18659a6b 422 fLastPad=pad;
423 }
424
1f1942b8 425#ifdef FULLLUT
3bcf971b 426 //find eta slice
efb8e06f 427 Float_t z = CalcZ(time);
3e87ef69 428
1f1942b8 429 Float_t z2=z*z;
efb8e06f 430 Float_t rz2 = 1 + r2/z2;
18659a6b 431 Int_t eta_index = FindIndex(rz2,fLastIndex);
3e87ef69 432#else
1f1942b8 433 Float_t z = CalcZ(time);
434 Double_t r = sqrt(fX*fX+fY*fY+z*z);
435 Double_t eta = 0.5 * log((r+z)/(r-z));
436 Int_t eta_index = GetEtaIndex(eta);
3e87ef69 437#endif
3bcf971b 438 if(eta_index < 0 || eta_index >= fNEtas){
1f1942b8 439 LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::TransformCircle","Histograms")<<"No histograms corresponding to eta index value of "<<eta_index<<"."<<ENDLOG;
d5d754e5 440 continue;
3bcf971b 441 }
3e87ef69 442
443#ifndef FULLLUT
444 if(fLastIndex==-1) fLastIndex=eta_index;
d5d754e5 445#endif
3e87ef69 446
1f1942b8 447 if(fLastIndex!=eta_index){ //enter old values first
3e87ef69 448 AddCurveToHistogram(eta_index);
3bcf971b 449 }
3e87ef69 450
451 fAccCharge+=charge;
452 //fAccCharge+=1;
d5d754e5 453 }
3e87ef69 454
455 //in case there is charge, store it before moving to the next row
456 if(fAccCharge>0) AddCurveToHistogram(-1);
efb8e06f 457
d5d754e5 458 //Move the data pointer to the next padrow:
459 AliL3MemHandler::UpdateRowPointer(tempPt);
460 }
d5d754e5 461}
462
463void AliL3HoughTransformerLUT::Print()
464{
465 cout << "fSlice: " << GetSlice() << endl;
466 cout << "fPatch: " << GetPatch() << endl;
467 cout << "fSector: " << fSector << endl;
468 cout << "fSectorRow: " << fSectorRow << endl;
469 cout << "fMinRow: " << fMinRow << endl;
470 cout << "fMaxRow: " << fMaxRow << endl;
471 cout << "fNRows: " << fNRows << endl;
472 cout << "fNEtas: " << fNEtas << endl;
473 cout << "fNPhi0: " << fNPhi0 << endl;
474 cout << "fZSign: " << fZSign << endl;
475 cout << "fZLengthPlusOff: " << fZLengthPlusOff << endl;
476 cout << "fPadPitch: " << fPadPitch << endl;
477 cout << "fTimeWidth: " << fTimeWidth << endl;
478
479 if(!fNRows) return;
480 cout << "fLUTX " << fNRows << endl;
481 for(Int_t i=0;i<fNRows;i++) cout << "fLUTX[" << i << "]=" << (Float_t)fLUTX[i] << endl;
482 cout << "fLUTY " << fNRows << endl;
483 for(Int_t i=0;i<fNRows;i++) cout << "fLUTY[" << i << "]=" << fLUTY[i] << endl;
484 if(!fNEtas) return;
485 cout << "fLUTEta " << fNEtas << endl;
486 for(Int_t i=0;i<fNEtas;i++) cout << "fLUTEta[" << i << "]=" << fLUTEta[i] << endl;
3e87ef69 487 cout << "fLUTEtaReal " << fNEtas << endl;
488 for(Int_t i=0;i<fNEtas;i++) cout << "fLUTEtaReal[" << i << "]=" << fLUTEtaReal[i] << endl;
d5d754e5 489 if(!fNPhi0) return;
490 cout << "fLUTphi0 " << fNPhi0 << endl;
491 for(Int_t i=0;i<fNPhi0;i++) cout << "fLUTPhi0[" << i << "]=" << fLUTphi0[i] << endl;
492 cout << "fLUT2sinphi0 " << fNPhi0 << endl;
493 for(Int_t i=0;i<fNPhi0;i++) cout << "fLUT2sinphi0[" << i << "]=" << fLUT2sinphi0[i] << endl;
494 cout << "fLUT2cosphi0 " << fNPhi0 << endl;
495 for(Int_t i=0;i<fNPhi0;i++) cout << "fLUT2cosphi0[" << i << "]=" << fLUT2cosphi0[i] << endl;
496}