3 // Author: Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright & copy CL
6 #include "AliL3StandardIncludes.h"
8 #include "AliL3Logging.h"
9 #include "AliL3HoughTransformerLUT.h"
10 #include "AliL3Transform.h"
11 #include "AliL3MemHandler.h"
12 #include "AliL3DigitData.h"
13 #include "AliL3Histogram.h"
19 //_____________________________________________________________
20 // AliL3HoughTransformerLUT
22 // Hough transformation class using Look-UP-Tables
25 ClassImp(AliL3HoughTransformerLUT)
27 AliL3HoughTransformerLUT::AliL3HoughTransformerLUT() : AliL3HoughBaseTransformer()
56 AliL3HoughTransformerLUT::AliL3HoughTransformerLUT(Int_t slice,Int_t patch,Int_t n_eta_segments) : AliL3HoughBaseTransformer(slice,patch,n_eta_segments)
83 Init(slice,patch,n_eta_segments);
86 AliL3HoughTransformerLUT::~AliL3HoughTransformerLUT()
94 for(Int_t i=0; i<fNEtas; i++)
96 if(!fTrackID[i]) continue;
116 void AliL3HoughTransformerLUT::DeleteHistograms()
119 delete[] fLUT2sinphi0;
120 delete[] fLUT2cosphi0;
131 for(Int_t i=0; i<fNEtas; i++)
133 if(!fParamSpace[i]) continue;
134 delete fParamSpace[i];
136 delete [] fParamSpace;
140 void AliL3HoughTransformerLUT::Init(Int_t slice,Int_t patch,Int_t n_eta_segments)
142 AliL3HoughBaseTransformer::Init(slice,patch,n_eta_segments);
144 //delete old LUT tables
156 fMinRow=AliL3Transform::GetFirstRow(patch);;
157 fMaxRow=AliL3Transform::GetLastRow(patch);;
158 fNRows=AliL3Transform::GetNRows(patch);;
159 fNEtas=n_eta_segments;
160 if(fNEtas!=GetNEtaSegments()) {
161 cerr << "AliL3HoughTransformerLUT::Init -> Error: Number of Etas must be equal!" << endl;
165 AliL3Transform::Slice2Sector(slice,fMinRow,fSector,fSectorRow);
166 fZSign = slice < 18 ? 1:-1;
168 fZLengthPlusOff=AliL3Transform::GetZLength()+AliL3Transform::GetZOffset();
169 fTimeWidth=AliL3Transform::GetZWidth();
172 if(fSector<AliL3Transform::GetNSectorLow())
173 fPadPitch=AliL3Transform::GetPadPitchWidthLow();
175 fPadPitch=AliL3Transform::GetPadPitchWidthUp();
177 Float_t etamax_=GetEtaMax();
178 Float_t etamin_=GetEtaMin();
179 fEtaSlice=(etamax_-etamin_)/n_eta_segments;
181 //lookup tables for X and Y
182 fLUTX=new Float_t[fNRows];
183 fLUTY=new Float_t[fNRows];
184 for(Int_t rr=0;rr<fNRows;rr++){
185 fLUTX[rr]=Float_t(AliL3Transform::Row2X(rr+fMinRow));
186 fLUTY[rr]=Float_t(0.5*(AliL3Transform::GetNPads(rr+fMinRow)-1)*fPadPitch);
187 //cout << rr << ": " << (Float_t)fLUTX[rr] << " " << (Float_t)fLUTY[rr] << endl;
190 //lookup tables for rz2s <=> etas
191 fLUTEta=new Float_t[fNEtas];
192 for(Int_t rr=0;rr<fNEtas;rr++){
193 Float_t eta=etamin_+(rr+1)*fEtaSlice;
194 fLUTEta[rr]=CalcRoverZ2(eta);
195 //cout << rr << ": " << eta << " " << fLUTEta[rr] << " " << GetEta(rr,fSlice) << endl;
199 void AliL3HoughTransformerLUT::CreateHistograms(Int_t nxbin,Double_t pt_min,Int_t nybin,Double_t phimin,Double_t phimax)
201 //Create the histograms (parameter space).
202 //These are 2D histograms, span by kappa (curvature of track) and phi0 (emission angle with x-axis).
203 //The arguments give the range and binning;
204 //nxbin = #bins in kappa
205 //nybin = #bins in phi0
206 //pt_min = mimium Pt of track (corresponding to maximum kappa)
207 //phi_min = mimimum phi0 (degrees)
208 //phi_max = maximum phi0 (degrees)
210 Double_t x = AliL3Transform::GetBFact()*AliL3Transform::GetBField()/pt_min;
211 Double_t torad = AliL3Transform::Pi()/180;
212 CreateHistograms(nxbin,-1.*x,x,nybin,phimin*torad,phimax*torad);
215 void AliL3HoughTransformerLUT::CreateHistograms(Int_t nxbin,Double_t xmin,Double_t xmax,Int_t nybin,Double_t ymin,Double_t ymax)
217 fParamSpace = new AliL3Histogram*[fNEtas];
219 Char_t histname[256];
220 for(Int_t i=0; i<fNEtas; i++)
222 sprintf(histname,"paramspace_%d",i);
223 fParamSpace[i] = new AliL3Histogram(histname,"",nxbin,xmin,xmax,nybin,ymin,ymax);
229 cerr << "Did not program changes in AliL3TransformerLUT for DO_MC!" << endl;
232 AliL3Histogram *hist = fParamSpace[0];
233 Int_t ncells = (hist->GetNbinsX()+2)*(hist->GetNbinsY()+2);
234 cout<<"Allocating "<<fNEtas*ncells*sizeof(TrackIndex)<<" bytes to fTrackID"<<endl;
235 fTrackID = new TrackIndex*[fNEtas];
236 for(Int_t i=0; i<fNEtas; i++)
237 fTrackID[i] = new TrackIndex[ncells];
242 //create lookup table for sin and cos
245 fLUTphi0=new Float_t[fNPhi0];
246 fLUT2sinphi0=new Float_t[fNPhi0];
247 fLUT2cosphi0=new Float_t[fNPhi0];
248 fLUTKappa=new Float_t[fNPhi0];
249 Float_t diff=(ymax-ymin)/nybin;
250 Float_t phi0=ymin-0.5*diff;
251 for(Int_t i=0; i<fNPhi0; i++){
254 fLUT2sinphi0[i]=2.*sin(phi0);
255 fLUT2cosphi0[i]=2.*cos(phi0);
257 //cout << i << ": " << fLUTphi0[i] << " " << fLUT2sinphi0[i] << " " << fLUT2cosphi0[i] << endl;
261 void AliL3HoughTransformerLUT::Reset()
263 //Reset all the histograms
267 LOG(AliL3Log::kWarning,"AliL3HoughTransformer::Reset","Histograms")
268 <<"No histograms to reset"<<ENDLOG;
272 for(Int_t i=0; i<fNEtas; i++)
273 fParamSpace[i]->Reset();
277 cerr << "Did not program changes in AliL3TransformerLUT for DO_MC!" << endl;
280 AliL3Histogram *hist = fParamSpace[0];
281 Int_t ncells = (hist->GetNbinsX()+2)*(hist->GetNbinsY()+2);
282 for(Int_t i=0; i<fNEtas; i++)
283 memset(fTrackID[i],0,ncells*sizeof(TrackIndex));
289 Int_t AliL3HoughTransformerLUT::GetEtaIndex(Double_t eta)
291 //Return the histogram index of the corresponding eta.
293 Float_t rz2=CalcRoverZ2(eta);
294 return FindIndex(rz2);
297 Int_t AliL3HoughTransformerLUT::FindIndex(Float_t rz2, Int_t start)
299 //could improve search through devide and conquere strategy
304 while((index<fNEtas)&&(rz2<=fLUTEta[index])){
308 while((index>=0)&&(rz2>fLUTEta[index])){
313 //cout << start << " - " << index << ": " << rz2 << " " << fLUTEta[index] << endl;
317 inline AliL3Histogram *AliL3HoughTransformerLUT::GetHistogram(Int_t eta_index)
319 if(!fParamSpace || eta_index >= fNEtas || eta_index < 0)
321 if(!fParamSpace[eta_index])
323 return fParamSpace[eta_index];
326 Float_t AliL3HoughTransformerLUT::CalcRoverZ2(Float_t eta)
328 Float_t e=exp(2*eta);
329 Float_t ret=(e+1)/(e-1);
334 Float_t AliL3HoughTransformerLUT::CalcEta(Float_t roverz2)
336 Float_t rz=sqrt(roverz2);
338 Float_t ret=(1+rz)/(rz-1);
343 Float_t AliL3HoughTransformerLUT::CalcX(Int_t row)
348 Float_t AliL3HoughTransformerLUT::CalcY(Int_t pad,Int_t row)
350 return pad*fPadPitch-fLUTY[row];
353 Float_t AliL3HoughTransformerLUT::CalcZ(Int_t time)
355 Float_t ret=time*fTimeWidth;
356 if(fZSign>0) ret=fZLengthPlusOff-ret;
357 else ret=ret-fZLengthPlusOff;
361 Double_t AliL3HoughTransformerLUT::GetEta(Int_t eta_index,Int_t slice)
363 if(eta_index >= fNEtas || eta_index < 0){
364 //LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::GetEta","Index") << "Index out of range."<<ENDLOG;
368 //LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::GetEta","Index") << "Given slice does not match internal slice."<<ENDLOG;
372 return (CalcEta(fLUTEta[eta_index])-0.5*fEtaSlice);
375 void AliL3HoughTransformerLUT::TransformCircle()
377 //Transform the input data with a circle HT.
378 //The function loops over all the data, and transforms each pixel with the equation:
380 //kappa = 2/R^2 * (y*cos(phi0) - x*sin(phi0) )
382 //where R^2 = x^2 + y^2
384 //Each pixel then transforms into a curve in the (kappa,phi0)-space. In order to find
385 //which histogram in which the pixel should be transformed, the eta-value is calcluated
386 //and the proper histogram index is found by GetEtaIndex(eta).
388 AliL3DigitRowData *tempPt = GetDataPointer();
391 LOG(AliL3Log::kError,"AliL3HoughTransformerLUT::TransformCircle","Data")
392 <<"No input data "<<ENDLOG;
396 //Loop over the padrows:
397 for(Int_t i=fMinRow, row=0; i<=fMaxRow; i++, row++)
399 //Get the data on this padrow:
400 AliL3DigitData *digPt = tempPt->fDigitData;
401 if(i != (Int_t)tempPt->fRow)
403 LOG(AliL3Log::kError,"AliL3HoughTransformerLUT::TransformCircle","Data")
404 <<"AliL3HoughTransformerLUT::TransformCircle : Mismatching padrow numbering "<<i<<" != "<<(Int_t)tempPt->fRow<<ENDLOG;
408 //calculate x for this row
409 Float_t x = CalcX(row);
416 //Loop over the data on this padrow:
417 for(UInt_t j=0; j<tempPt->fNDigit; j++)
419 UShort_t charge = digPt[j].fCharge;
422 if((Int_t)charge <= GetLowerThreshold() || (Int_t)charge > GetUpperThreshold())
425 UChar_t pad = digPt[j].fPad;
426 UShort_t time = digPt[j].fTime;
428 if(fLastPad!=pad){ //only update if necessary
431 //calculate hough for this pad
432 Float_t y = CalcY(pad,row);
435 Float_t R2 = 1. / r2;
436 for(Int_t b=0; b<fNPhi0; b++)
437 fLUTKappa[b]=R2*(y*fLUT2cosphi0[b]-x*fLUT2sinphi0[b]);
443 Float_t z = CalcZ(time);
445 Float_t rz2 = 1 + r2/z2;
446 Int_t eta_index = FindIndex(rz2,fLastIndex);
447 //cout << row << " " << (int)pad << " " << (int)time << " " << eta_index <<" " <<fLastIndex<< endl;
448 if(eta_index < 0 || eta_index >= fNEtas){
449 //LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::TransformCircle","Histograms")<<"No histograms corresponding to eta index value of "<<eta_index<<"."<<ENDLOG;
452 //Get the correct histogrampointer:
453 AliL3Histogram *hist = fParamSpace[eta_index];
455 //LOG(AliL3Log::kWarning,"AliL3HoughTransformerLUT::TransformCircle","Histograms")<<"Error getting histogram in index "<<eta_index<<"."<<ENDLOG;
458 fLastIndex=eta_index;
460 //Fill the histogram along the phirange
461 for(Int_t b=0; b<fNPhi0; b++){
462 hist->Fill(fLUTKappa[b],fLUTphi0[b],charge);
463 //hist->Fill(fLUTKappa[b],fLUTphi0[b],1);
464 //cout << kappa << " " << fLUTphi0[b] << " " << charge << endl;
469 cerr << "Did not program changes in AliL3TransformerLUT for DO_MC!" << endl;
472 Int_t bin = hist->FindBin(kappa,phi0);
473 for(Int_t t=0; t<3; t++)
475 Int_t label = digPt[j].fTrackID[t];
478 for(c=0; c<MaxTrack; c++)
479 if(fTrackID[eta_index][bin].fLabel[c] == label || fTrackID[eta_index][bin].fNHits[c] == 0)
481 if(c == MaxTrack-1) LOG(AliL3Log::kError,"AliL3HoughTransformerLUT::TransformCircle","MCData") <<"Array reached maximum!! "<<c<<endl;
482 fTrackID[eta_index][bin].fLabel[c] = label;
483 fTrackID[eta_index][bin].fNHits[c]++;
491 //Move the data pointer to the next padrow:
492 AliL3MemHandler::UpdateRowPointer(tempPt);
496 Int_t AliL3HoughTransformerLUT::GetTrackID(Int_t eta_index,Double_t kappa,Double_t psi,Int_t &nhits)
500 cerr<<"AliL3HoughTransformer::GetTrackID : Flag switched off"<<endl;
505 cerr << "Did not program changes in AliL3TransformerLUT for DO_MC!" << endl;
508 if(eta_index < 0 || eta_index > fNEtas)
510 cerr<<"AliL3HoughTransformer::GetTrackID : Wrong etaindex "<<eta_index<<endl;
513 AliL3Histogram *hist = fParamSpace[eta_index];
514 Int_t bin = hist->FindBin(kappa,psi);
517 for(UInt_t i=0; i<MaxTrack; i++)
519 Int_t nhits=fTrackID[eta_index][bin].fNHits[i];
520 if(nhits == 0) break;
524 label = fTrackID[eta_index][bin].fLabel[i];
530 cout<<"AliL3HoughTransformer::GetTrackID : Compile with do_mc flag!"<<endl;
534 void AliL3HoughTransformerLUT::Print()
536 cout << "fSlice: " << GetSlice() << endl;
537 cout << "fPatch: " << GetPatch() << endl;
538 cout << "fSector: " << fSector << endl;
539 cout << "fSectorRow: " << fSectorRow << endl;
540 cout << "fMinRow: " << fMinRow << endl;
541 cout << "fMaxRow: " << fMaxRow << endl;
542 cout << "fNRows: " << fNRows << endl;
543 cout << "fNEtas: " << fNEtas << endl;
544 cout << "fNPhi0: " << fNPhi0 << endl;
545 cout << "fZSign: " << fZSign << endl;
546 cout << "fZLengthPlusOff: " << fZLengthPlusOff << endl;
547 cout << "fPadPitch: " << fPadPitch << endl;
548 cout << "fTimeWidth: " << fTimeWidth << endl;
551 cout << "fLUTX " << fNRows << endl;
552 for(Int_t i=0;i<fNRows;i++) cout << "fLUTX[" << i << "]=" << (Float_t)fLUTX[i] << endl;
553 cout << "fLUTY " << fNRows << endl;
554 for(Int_t i=0;i<fNRows;i++) cout << "fLUTY[" << i << "]=" << fLUTY[i] << endl;
556 cout << "fLUTEta " << fNEtas << endl;
557 for(Int_t i=0;i<fNEtas;i++) cout << "fLUTEta[" << i << "]=" << fLUTEta[i] << endl;
559 cout << "fLUTphi0 " << fNPhi0 << endl;
560 for(Int_t i=0;i<fNPhi0;i++) cout << "fLUTPhi0[" << i << "]=" << fLUTphi0[i] << endl;
561 cout << "fLUT2sinphi0 " << fNPhi0 << endl;
562 for(Int_t i=0;i<fNPhi0;i++) cout << "fLUT2sinphi0[" << i << "]=" << fLUT2sinphi0[i] << endl;
563 cout << "fLUT2cosphi0 " << fNPhi0 << endl;
564 for(Int_t i=0;i<fNPhi0;i++) cout << "fLUT2cosphi0[" << i << "]=" << fLUT2cosphi0[i] << endl;