Changes ClassDef version from 2 to 3.
[u/mrichter/AliRoot.git] / HLT / hough / AliL3HoughTransformer.cxx
CommitLineData
b5a207b4 1//$Id$
2
b1886074 3// Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4//*-- Copyright &copy ASV
4cafa5fc 5
48f3c46f 6#include "AliL3MemHandler.h"
e26acabd 7#include "AliL3Logging.h"
99e7186b 8#include "AliL3HoughTransformer.h"
4de874d1 9#include "AliL3Defs.h"
10#include "AliL3Transform.h"
4cafa5fc 11#include "AliL3DigitData.h"
99e7186b 12#include "AliL3Histogram.h"
4de874d1 13
b1886074 14//_____________________________________________________________
15// AliL3HoughTransformer
16//
17// Hough transformation class
3ef466c5 18//
b1886074 19
4de874d1 20ClassImp(AliL3HoughTransformer)
21
22AliL3HoughTransformer::AliL3HoughTransformer()
23{
24 //Default constructor
4cafa5fc 25
4de874d1 26}
27
99e7186b 28AliL3HoughTransformer::AliL3HoughTransformer(Int_t slice,Int_t patch,Int_t n_eta_segments)
52a2a604 29{
3ef466c5 30 //Normal constructor
31
52a2a604 32 fSlice = slice;
33 fPatch = patch;
99e7186b 34 fNEtaSegments = n_eta_segments;
35 fEtaMin = 0;
36 fEtaMax = fSlice < 18 ? 0.9 : -0.9;
37 fTransform = new AliL3Transform();
38 fThreshold = 0;
48f3c46f 39 fNDigitRowData=0;
40 fDigitRowData=0;
52a2a604 41}
42
4de874d1 43AliL3HoughTransformer::~AliL3HoughTransformer()
44{
4de874d1 45 if(fTransform)
46 delete fTransform;
99e7186b 47 DeleteHistograms();
4cafa5fc 48}
49
99e7186b 50void AliL3HoughTransformer::DeleteHistograms()
4cafa5fc 51{
99e7186b 52 if(!fParamSpace)
53 return;
54 for(Int_t i=0; i<fNEtaSegments; i++)
4fc9a6a4 55 {
56 if(!fParamSpace[i]) continue;
57 delete fParamSpace[i];
58 }
99e7186b 59 delete [] fParamSpace;
4cafa5fc 60}
61
e26acabd 62void AliL3HoughTransformer::CreateHistograms(Int_t nxbin,Double_t pt_min,
63 Int_t nybin,Double_t phimin,Double_t phimax)
64{
3ef466c5 65 //Create the histograms (parameter space).
66 //These are 2D histograms, span by kappa (curvature of track) and phi0 (emission angle with x-axis).
67 //The arguments give the range and binning;
68 //nxbin = #bins in kappa
69 //nybin = #bins in phi0
70 //pt_min = mimium Pt of track (corresponding to maximum kappa)
71 //phi_min = mimimum phi0 (degrees)
72 //phi_max = maximum phi0 (degrees)
73
e26acabd 74 Double_t bfact = 0.0029980;
75 Double_t bfield = 0.2;
76 Double_t x = bfact*bfield/pt_min;
b5a207b4 77 CreateHistograms(nxbin,-1.*x,x,nybin,phimin*ToRad,phimax*ToRad);
e26acabd 78}
79
99e7186b 80void AliL3HoughTransformer::CreateHistograms(Int_t nxbin,Double_t xmin,Double_t xmax,
81 Int_t nybin,Double_t ymin,Double_t ymax)
4cafa5fc 82{
4fc9a6a4 83
99e7186b 84 fParamSpace = new AliL3Histogram*[fNEtaSegments];
4cafa5fc 85
99e7186b 86 Char_t histname[256];
87 for(Int_t i=0; i<fNEtaSegments; i++)
4de874d1 88 {
99e7186b 89 sprintf(histname,"paramspace_%d",i);
90 fParamSpace[i] = new AliL3Histogram(histname,"",nxbin,xmin,xmax,nybin,ymin,ymax);
4de874d1 91 }
4cafa5fc 92}
93
e26acabd 94void AliL3HoughTransformer::Reset()
95{
48f3c46f 96 //Reset all the histograms
e26acabd 97
98 if(!fParamSpace)
99 {
100 LOG(AliL3Log::kWarning,"AliL3HoughTransformer::Reset","Histograms")
101 <<"No histograms to reset"<<ENDLOG;
102 return;
103 }
104
105 for(Int_t i=0; i<fNEtaSegments; i++)
106 fParamSpace[i]->Reset();
e26acabd 107}
108
99e7186b 109void AliL3HoughTransformer::SetInputData(UInt_t ndigits,AliL3DigitRowData *ptr)
9f33a1db 110{
3ef466c5 111 //Give the pointer to the data.
112
99e7186b 113 fNDigitRowData = ndigits;
114 fDigitRowData = ptr;
4de874d1 115}
116
b5a207b4 117Int_t AliL3HoughTransformer::GetEtaIndex(Double_t eta)
118{
3ef466c5 119 //Return the histogram index of the corresponding eta.
120
b5a207b4 121 Double_t etaslice = (fEtaMax - fEtaMin)/fNEtaSegments;
122 Double_t index = (eta-fEtaMin)/etaslice;
123 return (Int_t)index;
124}
125
4fc9a6a4 126void AliL3HoughTransformer::TransformCircle()
4de874d1 127{
99e7186b 128 //Transform the input data with a circle HT.
3ef466c5 129 //The function loops over all the data, and transforms each pixel with the equations:
130 //
131 //kappa = 2/R*sin(phi - phi0)
132 //
133 //where R = sqrt(x*x +y*y), and phi = arctan(y/x)
134 //
135 //Each pixel then transforms into a curve in the (kappa,phi0)-space. In order to find
136 //which histogram in which the pixel should be transformed, the eta-value is calcluated
137 //and the proper histogram index is found by GetEtaIndex(eta).
138
52a2a604 139
99e7186b 140 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fDigitRowData;
141 if(!tempPt || fNDigitRowData==0)
4de874d1 142 {
4fc9a6a4 143 printf("\nAliL3HoughTransformer::TransformCircle : No input data!!!\n\n");
99e7186b 144 return;
4de874d1 145 }
99e7186b 146
3ef466c5 147 //Loop over the padrows:
4cafa5fc 148 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
149 {
3ef466c5 150 //Get the data on this padrow:
99e7186b 151 AliL3DigitData *digPt = tempPt->fDigitData;
152 if(i != (Int_t)tempPt->fRow)
4cafa5fc 153 {
4fc9a6a4 154 printf("AliL3HoughTransform::TransformCircle : Mismatching padrow numbering\n");
99e7186b 155 continue;
156 }
3ef466c5 157
158 //Loop over the data on this padrow:
99e7186b 159 for(UInt_t j=0; j<tempPt->fNDigit; j++)
160 {
161 UShort_t charge = digPt[j].fCharge;
162 UChar_t pad = digPt[j].fPad;
163 UShort_t time = digPt[j].fTime;
48f3c46f 164 if(charge <= fThreshold)
99e7186b 165 continue;
4cafa5fc 166 Int_t sector,row;
99e7186b 167 Float_t xyz[3];
3ef466c5 168
169 //Transform data to local cartesian coordinates:
4cafa5fc 170 fTransform->Slice2Sector(fSlice,i,sector,row);
99e7186b 171 fTransform->Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
3ef466c5 172
173 //Calculate the eta:
4fc9a6a4 174 Double_t eta = fTransform->GetEta(xyz);
3ef466c5 175
176 //Get the corresponding index, which determines which histogram to fill:
b5a207b4 177 Int_t eta_index = GetEtaIndex(eta);//(Int_t)((eta-fEtaMin)/etaslice);
99e7186b 178 if(eta_index < 0 || eta_index >= fNEtaSegments)
179 continue;
4cafa5fc 180
e26acabd 181 //Get the correct histogrampointer:
99e7186b 182 AliL3Histogram *hist = fParamSpace[eta_index];
183 if(!hist)
4cafa5fc 184 {
4fc9a6a4 185 printf("AliL3HoughTransformer::TransformCircle : Error getting histogram in index %d\n",eta_index);
99e7186b 186 continue;
4cafa5fc 187 }
4cafa5fc 188
3ef466c5 189 //Do the transformation:
190 Float_t R = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1]);
99e7186b 191 Float_t phi = fTransform->GetPhi(xyz);
4de874d1 192
99e7186b 193 //Fill the histogram along the phirange
194 for(Int_t b=hist->GetFirstYbin(); b<=hist->GetLastYbin(); b++)
4de874d1 195 {
99e7186b 196 Float_t phi0 = hist->GetBinCenterY(b);
197 Float_t kappa = 2*sin(phi - phi0)/R;
198 hist->Fill(kappa,phi0,charge);
4de874d1 199 }
4de874d1 200 }
3ef466c5 201
202 //Move the data pointer to the next padrow:
48f3c46f 203 AliL3MemHandler::UpdateRowPointer(tempPt);
4de874d1 204 }
4de874d1 205}
206
3ef466c5 207void AliL3HoughTransformer::TransformCircleC(Int_t row_range)
b1886074 208{
209 //Circle transform, using combinations of every 2 points lying
210 //on different padrows and within the same etaslice.
211
b1886074 212 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fDigitRowData;
213 if(!tempPt)
214 printf("\nAliL3HoughTransformer::TransformCircleC() : Zero data pointer\n");
215
3ef466c5 216 Int_t counter=0;
b1886074 217 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
218 {
3ef466c5 219 counter += tempPt->fNDigit;
b1886074 220 AliL3MemHandler::UpdateRowPointer(tempPt);
221 }
3ef466c5 222
223 struct Digit {
224 Int_t row;
225 Double_t r;
226 Double_t phi;
227 Int_t eta_index;
228 Int_t charge;
229 };
230
231 Digit *digits = new Digit[counter];
232 cout<<"Allocating "<<counter*sizeof(Digit)<<" bytes to digitsarray"<<endl;
233
234 Int_t total_digits=counter;
235 Int_t sector,row,tot_charge,pad,time,charge;
b1886074 236 Double_t r1,r2,phi1,phi2,eta,kappa,phi_0;
b1886074 237 Float_t xyz[3];
3ef466c5 238
239 counter=0;
240 tempPt = (AliL3DigitRowData*)fDigitRowData;
241
242 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
b1886074 243 {
3ef466c5 244 AliL3DigitData *digPt = tempPt->fDigitData;
245 for(UInt_t di=0; di<tempPt->fNDigit; di++)
b1886074 246 {
3ef466c5 247 charge = digPt[di].fCharge;
b1886074 248 pad = digPt[di].fPad;
249 time = digPt[di].fTime;
b1886074 250 fTransform->Slice2Sector(fSlice,i,sector,row);
251 fTransform->Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
252 eta = fTransform->GetEta(xyz);
3ef466c5 253 digits[counter].row = i;
254 digits[counter].r = sqrt(xyz[0]*xyz[0] + xyz[1]*xyz[1]);
255 digits[counter].phi = atan2(xyz[1],xyz[0]);
256 digits[counter].eta_index = GetEtaIndex(eta);
257 digits[counter].charge = charge;
258 counter++;
259 }
260 AliL3MemHandler::UpdateRowPointer(tempPt);
261 }
262
263 for(Int_t i=0; i<total_digits; i++)
264 {
265 if(digits[i].eta_index < 0 || digits[i].eta_index >= fNEtaSegments) continue;
266 Int_t ind = digits[i].eta_index;
267
268 for(Int_t j=i+1; j<total_digits; j++)
269 {
270 if(digits[i].row == digits[j].row) continue;
271 if(digits[i].eta_index != digits[j].eta_index) continue;
272 if(digits[i].row + row_range < digits[j].row) break;
b1886074 273
274 //Get the correct histogrampointer:
3ef466c5 275 AliL3Histogram *hist = fParamSpace[ind];
b1886074 276 if(!hist)
277 {
3ef466c5 278 printf("AliL3HoughTransformer::TransformCircleC() : No histogram at index %d\n",ind);
b1886074 279 continue;
280 }
3ef466c5 281
282 r1 = digits[i].r;
283 phi1 = digits[i].phi;
284 r2 = digits[j].r;
285 phi2 = digits[j].phi;
286 phi_0 = atan( (r2*sin(phi1)-r1*sin(phi2))/(r2*cos(phi1)-r1*cos(phi2)) );
287 kappa = 2*sin(phi2-phi_0)/r2;
288 tot_charge = digits[i].charge + digits[j].charge;
289 hist->Fill(kappa,phi_0,tot_charge);
b1886074 290 }
291 }
3ef466c5 292 delete [] digits;
b1886074 293}
294
4fc9a6a4 295void AliL3HoughTransformer::TransformLine()
296{
297 //Do a line transform on the data.
298
299
300 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fDigitRowData;
301 if(!tempPt || fNDigitRowData==0)
302 {
303 printf("\nAliL3HoughTransformer::TransformLine : No input data!!!\n\n");
304 return;
305 }
306
b5a207b4 307 //Double_t etaslice = (fEtaMax - fEtaMin)/fNEtaSegments;
4fc9a6a4 308 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
309 {
310 AliL3DigitData *digPt = tempPt->fDigitData;
311 if(i != (Int_t)tempPt->fRow)
312 {
313 printf("AliL3HoughTransform::TransformLine : Mismatching padrow numbering\n");
314 continue;
315 }
316 for(UInt_t j=0; j<tempPt->fNDigit; j++)
317 {
318 UShort_t charge = digPt[j].fCharge;
319 UChar_t pad = digPt[j].fPad;
320 UShort_t time = digPt[j].fTime;
321 if(charge < fThreshold)
322 continue;
323 Int_t sector,row;
324 Float_t xyz[3];
325 fTransform->Slice2Sector(fSlice,i,sector,row);
326 fTransform->Raw2Local(xyz,sector,row,(Int_t)pad,(Int_t)time);
327 Float_t eta = fTransform->GetEta(xyz);
b5a207b4 328 Int_t eta_index = GetEtaIndex(eta);//(Int_t)(eta/etaslice);
4fc9a6a4 329 if(eta_index < 0 || eta_index >= fNEtaSegments)
330 continue;
331
332 //Get the correct histogram:
333 AliL3Histogram *hist = fParamSpace[eta_index];
334 if(!hist)
335 {
336 printf("AliL3HoughTransformer::TransformLine : Error getting histogram in index %d\n",eta_index);
337 continue;
338 }
339 for(Int_t xbin=hist->GetFirstXbin(); xbin<hist->GetLastXbin(); xbin++)
340 {
341 Double_t theta = hist->GetBinCenterX(xbin);
342 Double_t rho = xyz[0]*cos(theta) + xyz[1]*sin(theta);
343 hist->Fill(theta,rho,charge);
344 }
345 }
48f3c46f 346 AliL3MemHandler::UpdateRowPointer(tempPt);
4fc9a6a4 347 }
348
349}