3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.h"
8 #include "AliL3Logging.h"
9 #include "AliL3HoughTransformer.h"
10 #include "AliL3MemHandler.h"
11 #include "AliL3Transform.h"
12 #include "AliL3DigitData.h"
13 #include "AliL3HistogramAdaptive.h"
19 //_____________________________________________________________
20 // AliL3HoughTransformer
22 // Hough transformation class
25 ClassImp(AliL3HoughTransformer)
27 AliL3HoughTransformer::AliL3HoughTransformer()
38 AliL3HoughTransformer::AliL3HoughTransformer(Int_t slice,Int_t patch,Int_t n_eta_segments,Bool_t DoEtaOverlap,Bool_t DoMC) : AliL3HoughBaseTransformer(slice,patch,n_eta_segments)
43 fEtaOverlap = DoEtaOverlap;
57 AliL3HoughTransformer::~AliL3HoughTransformer()
63 for(Int_t i=0; i<GetNEtaSegments(); i++)
65 if(!fTrackID[i]) continue;
73 void AliL3HoughTransformer::DeleteHistograms()
77 for(Int_t i=0; i<GetNEtaSegments(); i++)
79 if(!fParamSpace[i]) continue;
80 delete fParamSpace[i];
82 delete [] fParamSpace;
85 void AliL3HoughTransformer::CreateHistograms(Int_t nxbin,Double_t pt_min,
86 Int_t nybin,Double_t phimin,Double_t phimax)
88 //Create the histograms (parameter space).
89 //These are 2D histograms, span by kappa (curvature of track) and phi0 (emission angle with x-axis).
90 //The arguments give the range and binning;
91 //nxbin = #bins in kappa
92 //nybin = #bins in phi0
93 //pt_min = mimium Pt of track (corresponding to maximum kappa)
94 //phi_min = mimimum phi0 (degrees)
95 //phi_max = maximum phi0 (degrees)
97 Double_t x = AliL3Transform::GetBFact()*AliL3Transform::GetBField()/pt_min;
98 Double_t torad = AliL3Transform::Pi()/180;
99 CreateHistograms(nxbin,-1.*x,x,nybin,phimin*torad,phimax*torad);
102 void AliL3HoughTransformer::CreateHistograms(Int_t nxbin,Double_t xmin,Double_t xmax,
103 Int_t nybin,Double_t ymin,Double_t ymax)
106 fParamSpace = new AliL3Histogram*[GetNEtaSegments()];
108 Char_t histname[256];
109 for(Int_t i=0; i<GetNEtaSegments(); i++)
111 sprintf(histname,"paramspace_%d",i);
112 //fParamSpace[i] = new AliL3HistogramAdaptive(histname,0.1,1.5,0.05,nybin,ymin,ymax);
113 fParamSpace[i] = new AliL3Histogram(histname,"",nxbin,xmin,xmax,nybin,ymin,ymax);
119 AliL3Histogram *hist = fParamSpace[0];
120 Int_t ncells = (hist->GetNbinsX()+2)*(hist->GetNbinsY()+2);
121 cout<<"Transformer: Allocating "<<GetNEtaSegments()*ncells*sizeof(TrackIndex)<<" bytes to fTrackID"<<endl;
122 fTrackID = new TrackIndex*[GetNEtaSegments()];
123 for(Int_t i=0; i<GetNEtaSegments(); i++)
124 fTrackID[i] = new TrackIndex[ncells];
129 void AliL3HoughTransformer::Reset()
131 //Reset all the histograms
135 LOG(AliL3Log::kWarning,"AliL3HoughTransformer::Reset","Histograms")
136 <<"No histograms to reset"<<ENDLOG;
140 for(Int_t i=0; i<GetNEtaSegments(); i++)
141 fParamSpace[i]->Reset();
145 AliL3Histogram *hist = fParamSpace[0];
146 Int_t ncells = (hist->GetNbinsX()+2)*(hist->GetNbinsY()+2);
147 for(Int_t i=0; i<GetNEtaSegments(); i++)
148 memset(fTrackID[i],0,ncells*sizeof(TrackIndex));
153 Int_t AliL3HoughTransformer::GetEtaIndex(Double_t eta)
155 //Return the histogram index of the corresponding eta.
157 Double_t etaslice = (GetEtaMax() - GetEtaMin())/GetNEtaSegments();
158 Double_t index = (eta-GetEtaMin())/etaslice;
162 void AliL3HoughTransformer::GetEtaIndexes(Double_t eta,Int_t *indexes)
164 //Return histogram indexes in case of overlapping etaslices.
166 Double_t etaslice = (GetEtaMax() - GetEtaMin())/GetNEtaSegments();
167 Int_t index = (Int_t)((eta-GetEtaMin())/etaslice);
171 indexes[1] = index - 1;
175 indexes[0] = index - 1;
180 inline AliL3Histogram *AliL3HoughTransformer::GetHistogram(Int_t eta_index)
182 if(!fParamSpace || eta_index >= GetNEtaSegments() || eta_index < 0)
184 if(!fParamSpace[eta_index])
186 return fParamSpace[eta_index];
189 Double_t AliL3HoughTransformer::GetEta(Int_t eta_index,Int_t slice)
191 Double_t eta_slice = (GetEtaMax()-GetEtaMin())/GetNEtaSegments();
195 Int_t index = eta_index + 1;
196 eta=(Double_t)((index)*eta_slice);
199 eta=(Double_t)((eta_index+0.5)*eta_slice);
204 void AliL3HoughTransformer::TransformCircle()
206 //Transform the input data with a circle HT.
207 //The function loops over all the data, and transforms each pixel with the equations:
209 //kappa = 2/R*sin(phi - phi0)
211 //where R = sqrt(x*x +y*y), and phi = arctan(y/x)
213 //Each pixel then transforms into a curve in the (kappa,phi0)-space. In order to find
214 //which histogram in which the pixel should be transformed, the eta-value is calculated
215 //and the proper histogram index is found by GetEtaIndex(eta).
218 AliL3DigitRowData *tempPt = GetDataPointer();
221 LOG(AliL3Log::kError,"AliL3HoughTransformer::TransformCircle","Data")
222 <<"No input data "<<ENDLOG;
226 //Loop over the padrows:
227 for(Int_t i=AliL3Transform::GetFirstRow(GetPatch()); i<=AliL3Transform::GetLastRow(GetPatch()); i++)
229 //Get the data on this padrow:
230 AliL3DigitData *digPt = tempPt->fDigitData;
231 if(i != (Int_t)tempPt->fRow)
233 cerr<<"AliL3HoughTransform::TransformCircle : Mismatching padrow numbering "<<i<<" "<<(Int_t)tempPt->fRow<<endl;
237 //Loop over the data on this padrow:
238 for(UInt_t j=0; j<tempPt->fNDigit; j++)
240 UShort_t charge = digPt[j].fCharge;
241 UChar_t pad = digPt[j].fPad;
242 UShort_t time = digPt[j].fTime;
243 if((Int_t)charge <= GetLowerThreshold() || (Int_t)charge > GetUpperThreshold())
249 //Transform data to local cartesian coordinates:
250 AliL3Transform::Slice2Sector(GetSlice(),i,sector,row);
251 AliL3Transform::Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
254 Double_t eta = AliL3Transform::GetEta(xyz);
256 //Get the corresponding index, which determines which histogram to fill:
257 Int_t eta_index = GetEtaIndex(eta);
259 if(eta_index < 0 || eta_index >= GetNEtaSegments())
262 //Get the correct histogrampointer:
263 AliL3Histogram *hist = fParamSpace[eta_index];
266 cerr<<"AliL3HoughTransformer::TransformCircle : Error getting histogram in index "<<eta_index<<endl;
270 //Do the transformation:
271 Float_t R = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1]);
272 Float_t phi = AliL3Transform::GetPhi(xyz);
274 //Fill the histogram along the phirange
275 for(Int_t b=hist->GetFirstYbin(); b<=hist->GetLastYbin(); b++)
277 Float_t phi0 = hist->GetBinCenterY(b);
278 Float_t kappa = 2*sin(phi - phi0)/R;
279 hist->Fill(kappa,phi0,charge);
280 //hist->Fill(kappa,phi0,1);
284 Int_t bin = hist->FindBin(kappa,phi0);
285 for(Int_t t=0; t<3; t++)
287 Int_t label = digPt[j].fTrackID[t];
290 for(c=0; c<MaxTrack; c++)
291 if(fTrackID[eta_index][bin].fLabel[c] == label || fTrackID[eta_index][bin].fNHits[c] == 0)
293 if(c == MaxTrack-1) cerr<<"AliL3HoughTransformer::TransformCircle : Array reached maximum!! "<<c<<endl;
294 fTrackID[eta_index][bin].fLabel[c] = label;
295 fTrackID[eta_index][bin].fNHits[c]++;
302 //Move the data pointer to the next padrow:
303 AliL3MemHandler::UpdateRowPointer(tempPt);
315 struct EtaContainer {
319 void AliL3HoughTransformer::TransformCircleC(Int_t *row_range,Int_t every)
321 //Circle transform, using combinations of every 2 points lying
322 //on different padrows and within the same etaslice.
324 AliL3DigitRowData *tempPt = GetDataPointer();
326 LOG(AliL3Log::kError,"AliL3HoughTransformer::TransformCircleC","Data")
327 <<"No input data "<<ENDLOG;
329 Int_t minrow = AliL3Transform::GetFirstRow(GetPatch());
330 Int_t maxrow = AliL3Transform::GetLastRow(GetPatch());
333 minrow = row_range[0];
334 maxrow = row_range[1];
335 if(minrow < AliL3Transform::GetFirstRow(GetPatch()) || minrow >= AliL3Transform::GetLastRow(GetPatch()))
336 minrow = AliL3Transform::GetFirstRow(GetPatch());
337 if(maxrow < AliL3Transform::GetFirstRow(GetPatch()) || maxrow >= AliL3Transform::GetLastRow(GetPatch()))
338 maxrow = AliL3Transform::GetLastRow(GetPatch());
339 if(minrow > maxrow || maxrow==minrow)
341 cerr<<"AliL3HoughTransformer::TransformCircleC : Bad row range "<<minrow<<" "<<maxrow<<endl;
347 minrow = AliL3Transform::GetFirstRow(GetPatch());
348 maxrow = AliL3Transform::GetLastRow(GetPatch());
352 for(Int_t i=AliL3Transform::GetFirstRow(GetPatch()); i<=AliL3Transform::GetLastRow(GetPatch()); i++)
354 counter += tempPt->fNDigit;
355 AliL3MemHandler::UpdateRowPointer(tempPt);
358 Int_t bound = (GetNEtaSegments()+1)*(AliL3Transform::GetNRows(GetPatch())+1);
359 EtaContainer *etaPt = new EtaContainer[bound];
360 memset(etaPt,0,bound*sizeof(EtaContainer));
362 Digit *digits = new Digit[counter];
363 cout<<"Allocating "<<counter*sizeof(Digit)<<" bytes to digitsarray"<<endl;
364 memset(digits,0,counter*sizeof(Digit));
366 Int_t sector,row,tot_charge,pad,time,charge;
367 Double_t r1,r2,phi1,phi2,eta,kappa,phi_0;
371 tempPt = GetDataPointer();
373 cout<<"Calculating digits in patch "<<GetPatch()<<endl;
374 for(Int_t i=AliL3Transform::GetFirstRow(GetPatch()); i<=AliL3Transform::GetLastRow(GetPatch()); i++)
376 AliL3DigitData *digPt = tempPt->fDigitData;
377 for(UInt_t di=0; di<tempPt->fNDigit; di++)
379 charge = digPt[di].fCharge;
380 pad = digPt[di].fPad;
381 time = digPt[di].fTime;
382 AliL3Transform::Slice2Sector(GetSlice(),i,sector,row);
383 AliL3Transform::Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
384 eta = AliL3Transform::GetEta(xyz);
386 digits[counter].row = i;
387 digits[counter].r = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1]);
388 digits[counter].phi = atan2(xyz[1],xyz[0]);
389 digits[counter].charge = charge;
393 Int_t eta_index = GetEtaIndex(eta);
395 Int_t index = (GetNEtaSegments()+1)*(i-AliL3Transform::GetFirstRow(GetPatch())) + eta_index;
397 if(index > 0 && index < bound)
399 if(etaPt[index].first == 0)
400 etaPt[index].first = &digits[counter];
402 (etaPt[index].last)->next = &digits[counter];
403 etaPt[index].last = &digits[counter];
409 GetEtaIndexes(eta,eta_index);
411 index[0] = (GetNEtaSegments()+1)*(i-AliL3Transform::GetFirstRow(GetPatch())) + eta_index[0];
412 index[1] = (GetNEtaSegments()+1)*(i-AliL3Transform::GetFirstRow(GetPatch())) + eta_index[1];
413 if(index[0] == index[1])
415 cerr<<"Same etaindexes "<<index[0]<<" "<<index[1]<<endl;
419 Int_t ind = index[0];
420 if(ind > 0 && ind < bound)
422 if(etaPt[ind].first == 0)
423 etaPt[ind].first = &digits[counter];
425 (etaPt[ind].last)->next = &digits[counter];
426 etaPt[ind].last = &digits[counter];
430 if(ind > 0 && ind < bound)
432 if(etaPt[ind].first == 0)
433 etaPt[ind].first = &digits[counter];
435 (etaPt[ind].last)->next = &digits[counter];
436 etaPt[ind].last = &digits[counter];
442 AliL3MemHandler::UpdateRowPointer(tempPt);
445 cout<<"Doing the combinatorics"<<endl;
449 for(Int_t e=0; e<GetNEtaSegments(); e++)
451 for(Int_t i=minrow; i<=maxrow; i+=every)
453 Int_t index1 = (GetNEtaSegments()+1)*(i-AliL3Transform::GetFirstRow(GetPatch())) + e;
455 for(dPt1 = (Digit*)etaPt[index1].first; dPt1 != 0; dPt1 = (Digit*)dPt1->next)
457 for(Int_t j=i+every; j<=maxrow; j+=every)
459 Int_t index2 = (GetNEtaSegments()+1)*(j-AliL3Transform::GetFirstRow(GetPatch())) + e;
461 for(dPt2 = (Digit*)etaPt[index2].first; dPt2 != 0; dPt2 = (Digit*)dPt2->next)
463 if(dPt1->row == dPt2->row)
465 cerr<<"same row; indexes "<<index1<<" "<<index2<<endl;
469 //Get the correct histogrampointer:
470 AliL3Histogram *hist = fParamSpace[e];
473 printf("AliL3HoughTransformer::TransformCircleC() : No histogram at index %d\n",i);
482 phi_0 = atan( (r2*sin(phi1)-r1*sin(phi2))/(r2*cos(phi1)-r1*cos(phi2)) );
483 kappa = 2*sin(phi2-phi_0)/r2;
484 tot_charge = dPt1->charge + dPt2->charge;
485 hist->Fill(kappa,phi_0,tot_charge);
499 void AliL3HoughTransformer::TransformLine(Int_t *row_range,Float_t *phirange)
501 //Do a line transform on the data.
504 AliL3DigitRowData *tempPt = GetDataPointer();
507 LOG(AliL3Log::kError,"AliL3HoughTransformer::TransformLine","Data")
508 <<"No input data "<<ENDLOG;
512 Int_t minrow = AliL3Transform::GetFirstRow(GetPatch());
513 Int_t maxrow = AliL3Transform::GetLastRow(GetPatch());
516 minrow = row_range[0];
517 maxrow = row_range[1];
518 if(minrow < AliL3Transform::GetFirstRow(GetPatch()) || minrow >= AliL3Transform::GetLastRow(GetPatch()))
519 minrow = AliL3Transform::GetFirstRow(GetPatch());
520 if(maxrow < AliL3Transform::GetFirstRow(GetPatch()) || maxrow >= AliL3Transform::GetLastRow(GetPatch()))
521 maxrow = AliL3Transform::GetLastRow(GetPatch());
522 if(minrow > maxrow || maxrow==minrow)
524 cerr<<"AliL3HoughTransformer::TransformCircleC : Bad row range "<<minrow<<" "<<maxrow<<endl;
529 for(Int_t i=minrow; i<=maxrow; i++)
531 AliL3DigitData *digPt = tempPt->fDigitData;
532 if(i != (Int_t)tempPt->fRow)
534 printf("AliL3HoughTransform::TransformLine : Mismatching padrow numbering\n");
537 for(UInt_t j=0; j<tempPt->fNDigit; j++)
539 UShort_t charge = digPt[j].fCharge;
540 UChar_t pad = digPt[j].fPad;
541 UShort_t time = digPt[j].fTime;
542 if(charge < GetLowerThreshold())
546 AliL3Transform::Slice2Sector(GetSlice(),i,sector,row);
547 AliL3Transform::Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
551 Float_t phi = AliL3Transform::GetPhi(xyz);
552 if(phi < phirange[0] || phi > phirange[1])
555 Float_t eta = AliL3Transform::GetEta(xyz);
556 Int_t eta_index = GetEtaIndex(eta);//(Int_t)(eta/etaslice);
557 if(eta_index < 0 || eta_index >= GetNEtaSegments())
560 xyz[0] = xyz[0] - AliL3Transform::Row2X(minrow);
562 //Get the correct histogram:
563 AliL3Histogram *hist = fParamSpace[eta_index];
566 printf("AliL3HoughTransformer::TransformLine : Error getting histogram in index %d\n",eta_index);
569 for(Int_t xbin=hist->GetFirstXbin(); xbin<hist->GetLastXbin(); xbin++)
571 Double_t theta = hist->GetBinCenterX(xbin);
572 Double_t rho = xyz[0]*cos(theta) + xyz[1]*sin(theta);
573 hist->Fill(theta,rho,charge);
576 AliL3MemHandler::UpdateRowPointer(tempPt);
587 struct LEtaContainer {
591 void AliL3HoughTransformer::TransformLineC(Int_t *rowrange,Float_t *phirange)
593 AliL3DigitRowData *tempPt = GetDataPointer();
595 LOG(AliL3Log::kError,"AliL3HoughTransformer::TransformCircleC","Data")
596 <<"No input data "<<ENDLOG;
600 for(Int_t i=AliL3Transform::GetFirstRow(GetPatch()); i<=AliL3Transform::GetLastRow(GetPatch()); i++)
602 counter += tempPt->fNDigit;
603 AliL3MemHandler::UpdateRowPointer(tempPt);
606 Int_t bound = (GetNEtaSegments()+1)*(AliL3Transform::GetNRows(GetPatch())+1);
607 LEtaContainer *etaPt = new LEtaContainer[bound];
608 memset(etaPt,0,bound*sizeof(LEtaContainer));
610 LDigit *digits = new LDigit[counter];
611 cout<<"Allocating "<<counter*sizeof(LDigit)<<" bytes to digitsarray"<<endl;
612 memset(digits,0,counter*sizeof(LDigit));
618 tempPt = GetDataPointer();
620 cout<<"Calculating digits in patch "<<GetPatch()<<endl;
621 for(Int_t i=AliL3Transform::GetFirstRow(GetPatch()); i<=AliL3Transform::GetLastRow(GetPatch()); i++)
623 AliL3DigitData *digPt = tempPt->fDigitData;
624 for(UInt_t di=0; di<tempPt->fNDigit; di++)
626 Int_t charge = digPt[di].fCharge;
627 Int_t pad = digPt[di].fPad;
628 Int_t time = digPt[di].fTime;
629 AliL3Transform::Slice2Sector(GetSlice(),i,sector,row);
630 AliL3Transform::Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
631 Double_t eta = AliL3Transform::GetEta(xyz);
633 Float_t phi = atan2(xyz[1],xyz[0]);
634 if(phi < phirange[0] || phi > phirange[1]) continue;
636 digits[counter].row = i;
637 digits[counter].y = xyz[1];
638 digits[counter].charge = charge;
640 Int_t eta_index = GetEtaIndex(eta);
641 Int_t index = (GetNEtaSegments()+1)*(i-AliL3Transform::GetFirstRow(GetPatch())) + eta_index;
643 if(index > 0 && index < bound)
645 if(etaPt[index].first == 0)
646 etaPt[index].first = &digits[counter];
648 (etaPt[index].last)->next = &digits[counter];
649 etaPt[index].last = &digits[counter];
653 AliL3MemHandler::UpdateRowPointer(tempPt);
656 cout<<"Doing the combinatorics"<<endl;
660 for(Int_t e=0; e<GetNEtaSegments(); e++)
662 for(Int_t i=rowrange[0]; i<=rowrange[1]; i++)
664 Int_t index1 = (GetNEtaSegments()+1)*(i-AliL3Transform::GetFirstRow(GetPatch())) + e;
666 for(dPt1 = (LDigit*)etaPt[index1].first; dPt1 != 0; dPt1 = (LDigit*)dPt1->next)
668 for(Int_t j=i+1; j<=rowrange[1]; j++)
670 Int_t index2 = (GetNEtaSegments()+1)*(j-AliL3Transform::GetFirstRow(GetPatch())) + e;
672 for(dPt2 = (LDigit*)etaPt[index2].first; dPt2 != 0; dPt2 = (LDigit*)dPt2->next)
674 if(dPt1->row == dPt2->row)
676 cerr<<"same row; indexes "<<index1<<" "<<index2<<endl;
680 //Get the correct histogrampointer:
681 AliL3Histogram *hist = fParamSpace[e];
684 printf("AliL3HoughTransformer::TransformCircleC() : No histogram at index %d\n",i);
689 float x1 = AliL3Transform::Row2X(dPt1->row) - AliL3Transform::Row2X(rowrange[0]);
690 float x2 = AliL3Transform::Row2X(dPt2->row) - AliL3Transform::Row2X(rowrange[0]);
693 float theta = atan2(x2-x1,y1-y2);
694 float rho = x1*cos(theta)+y1*sin(theta);
695 hist->Fill(theta,rho,1);//dPt1->charge+dPt2->charge);
707 Int_t AliL3HoughTransformer::GetTrackID(Int_t eta_index,Double_t kappa,Double_t psi)
711 cerr<<"AliL3HoughTransformer::GetTrackID : Flag switched off"<<endl;
716 if(eta_index < 0 || eta_index > GetNEtaSegments())
718 cerr<<"AliL3HoughTransformer::GetTrackID : Wrong etaindex "<<eta_index<<endl;
721 AliL3Histogram *hist = fParamSpace[eta_index];
722 Int_t bin = hist->FindBin(kappa,psi);
725 for(UInt_t i=0; i<MaxTrack; i++)
727 Int_t nhits=fTrackID[eta_index][bin].fNHits[i];
728 if(nhits == 0) break;
732 label = fTrackID[eta_index][bin].fLabel[i];
738 cout<<"AliL3HoughTransformer::GetTrackID : Compile with do_mc flag!"<<endl;