Moving to static AliL3Transform.
[u/mrichter/AliRoot.git] / HLT / comp / AliL3ModelTrack.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright &copy ASV
5
6 #include <stream.h>
7 #include <string.h>
8 #include <math.h>
9
10 #include "AliL3ModelTrack.h"
11 #include "AliL3Transform.h"
12
13 //_____________________________________________________________
14 // AliL3ModelTrack
15 //
16 // 
17
18 ClassImp(AliL3ModelTrack)
19
20 AliL3ModelTrack::AliL3ModelTrack()
21 {
22   fNClusters = 0;
23   fClusters = 0;
24   fOverlap = 0;
25   fPad=0;
26   fTime=0;
27   fClusterCharge=0;
28   fTrackModel=0;
29 }
30
31
32 AliL3ModelTrack::~AliL3ModelTrack()
33 {
34   if(fClusters)
35     delete [] fClusters;
36   if(fPad)
37     delete [] fPad;
38   if(fTime)
39     delete [] fTime;
40   if(fOverlap)
41     delete [] fOverlap;
42   if(fTrackModel)
43     delete fTrackModel;
44 }
45
46 void AliL3ModelTrack::Init(Int_t slice,Int_t patch)
47 {
48   fNClusters = 0;
49   fSlice=slice;
50   fPatch=patch;
51   Int_t nrows = AliL3Transform::GetNRows(fPatch);
52   fClusters = new AliL3ClusterModel[nrows];
53   fPad = new Float_t[nrows];
54   fTime = new Float_t[nrows];
55   fTrackModel = new AliL3TrackModel;
56   fOverlap = new Int_t[nrows];
57   
58   memset(fClusters,0,nrows*sizeof(AliL3ClusterModel));
59   memset(fPad,0,nrows*sizeof(Float_t));
60   memset(fTime,0,nrows*sizeof(Float_t));
61   memset(fTrackModel,0,sizeof(AliL3TrackModel));
62   for(Int_t i=0; i<nrows; i++)
63     fOverlap[i]=-1;
64
65   fClusterCharge = 100;
66   
67   // 100 micrometers:
68   fXYResidualQ = 0.01/AliL3Transform::GetPadPitchWidth(patch);
69   fZResidualQ = 0.01/AliL3Transform::GetPadPitchWidth(patch);
70   
71   fXYWidthQ = 0.005/AliL3Transform::GetPadPitchWidth(patch);
72   fZWidthQ = 0.005/AliL3Transform::GetPadPitchWidth(patch);
73 }
74
75
76 void AliL3ModelTrack::SetCluster(Int_t row,Float_t fpad,Float_t ftime,Float_t charge,Float_t sigmaY2,Float_t sigmaZ2,Int_t npads)
77 {
78   Int_t index = row - AliL3Transform::GetFirstRow(fPatch);
79   if(index != fNClusters)
80     cout<<"AliL3ModelTrack::SetCluster() : Mismatch ; index: "<<index<<" nclusters "<<fNClusters<<endl;
81   
82   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
83     {
84       cerr<<"AliL3ModelTrack::SetCluster() : Wrong index: "<<index<<" row "<<row<<endl;
85       return;
86     }
87   AliL3ClusterModel *cl = GetClusterModel(row);
88   if(!charge)
89     cl->fPresent = kFALSE;
90   else
91     {
92       cl->fPresent = kTRUE;
93       cl->fDTime = (ftime - GetTimeHit(row))/fXYResidualQ;
94       cl->fDPad = (fpad - GetPadHit(row))/fZResidualQ;
95       cl->fDCharge = charge - fClusterCharge;
96       cl->fDSigmaY2 = (sigmaY2 - GetParSigmaY2(row))/fXYWidthQ;
97       cl->fDSigmaZ2 = (sigmaZ2 - GetParSigmaZ2(row))/fZWidthQ;
98       cl->fNPads = npads;
99     }
100   
101   fNClusters++;
102 }
103
104 Int_t AliL3ModelTrack::CheckClustersQuality(UInt_t npads=3)
105 {
106
107   //Check the quality of clusters,- remove clusters with less than
108   //npads. 
109   //Returns the number of good clusters left.
110
111   Int_t count=0;
112
113   for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
114     {
115       AliL3ClusterModel *cl = GetClusterModel(i);
116       if(cl->fNPads < npads)
117         cl->fPresent = kFALSE;
118       if(cl->fPresent)
119         count++;
120     }
121   
122   return count;
123 }
124
125 void AliL3ModelTrack::FillModel()
126 {
127   //Fill the track structure
128   
129   if(!fTrackModel)
130     {
131       cerr<<"AliL3ModelTrack::FillModel() : No trackmodel "<<endl;
132       return;
133     }
134   fTrackModel->fKappa = GetKappa();
135   fTrackModel->fFirstPointX = GetFirstPointX();
136   fTrackModel->fFirstPointY = GetFirstPointY();
137   fTrackModel->fFirstPointZ = GetFirstPointZ();
138   fTrackModel->fTgl = GetTgl();
139   fTrackModel->fPsi = GetPsi();
140   fTrackModel->fLength = (Short_t)GetLength();
141   fTrackModel->fClusterCharge = fClusterCharge;
142   fTrackModel->fNClusters = fNClusters;
143
144 }
145
146 void AliL3ModelTrack::FillTrack()
147 {
148   //Fill the track parameters from the structure.
149   
150   if(!fTrackModel)
151     {
152       cerr<<"AliL3ModelTrack::FillTrack() : No data!!"<<endl;
153       return;
154     }
155   SetKappa(fTrackModel->fKappa);
156   SetCharge((-1*(Int_t)copysign(1.,GetKappa())));
157   SetFirstPoint(fTrackModel->fFirstPointX,fTrackModel->fFirstPointY,fTrackModel->fFirstPointZ);
158   SetTgl(fTrackModel->fTgl);
159   SetPsi(fTrackModel->fPsi);
160   SetLength(fTrackModel->fLength);
161   fClusterCharge=fTrackModel->fClusterCharge;
162   fNClusters = fTrackModel->fNClusters;
163   SetPt((BFACT*AliL3Transform::GetBField())/fabs(GetKappa()));
164   
165   CalculateHelix();
166   
167   Float_t hit[3];
168   Int_t sector,row;
169   for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
170     {
171       AliL3ClusterModel *cl = GetClusterModel(i);
172       if(!cl) continue;
173       GetCrossingPoint(i,hit);
174       AliL3Transform::Slice2Sector(fSlice,i,sector,row);
175       AliL3Transform::Local2Raw(hit,sector,row);
176       SetPadHit(i,hit[1]);
177       SetTimeHit(i,hit[2]);
178     }
179 }
180
181 void AliL3ModelTrack::SetPadHit(Int_t row,Float_t pad)
182 {
183   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
184   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
185     {
186       cerr<<"AliL3ModelTrack::SetPadHit() : Wrong index: "<<index<<endl;
187       return;
188     }
189   fPad[index]=pad;
190   
191 }
192
193 void AliL3ModelTrack::SetTimeHit(Int_t row,Float_t time)
194 {
195   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
196   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
197     {
198       cerr<<"AliL3ModelTrack::SetTimeHit() : Wrong index: "<<index<<endl;
199       return;
200     }
201   fTime[index]=time;
202 }
203
204 void AliL3ModelTrack::SetOverlap(Int_t row,Int_t id)
205 {
206   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
207   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
208     {
209       cerr<<"AliL3ModelTrack::SetOverlap() : Wrong index: "<<index<<endl;
210       return;
211     }
212   fOverlap[index]=id;
213 }
214
215 Int_t AliL3ModelTrack::GetNPads(Int_t row)
216 {
217   AliL3ClusterModel *cl = GetClusterModel(row);
218   return cl->fNPads;
219 }
220
221 Bool_t AliL3ModelTrack::GetPad(Int_t row,Float_t &pad)
222 {
223   //(ftime - GetTimeHit(fNClusters))/fXYResidualQ;
224   //(fpad - GetPadHit(fNClusters))/fZResidualQ;
225
226   AliL3ClusterModel *cl = GetClusterModel(row);
227   pad = cl->fDPad*fXYResidualQ + GetPadHit(row);
228
229   return (Bool_t)cl->fPresent;
230 }
231
232 Bool_t AliL3ModelTrack::GetTime(Int_t row,Float_t &time)
233 {
234   AliL3ClusterModel *cl = GetClusterModel(row);
235   time = cl->fDTime*fZResidualQ + GetTimeHit(row);
236
237   return (Bool_t)cl->fPresent;
238 }
239
240 Bool_t AliL3ModelTrack::GetClusterCharge(Int_t row,Int_t &charge)
241 {
242   AliL3ClusterModel *cl = GetClusterModel(row);
243   charge = (Int_t)cl->fDCharge + fClusterCharge;
244   
245   return (Bool_t)cl->fPresent;
246 }
247
248 Bool_t AliL3ModelTrack::GetXYWidth(Int_t row,Float_t &width)
249 {
250   AliL3ClusterModel *cl = GetClusterModel(row);
251   width = cl->fDSigmaY2*fXYWidthQ + GetParSigmaY2(row);
252   
253   return (Bool_t)cl->fPresent;
254 }
255
256 Bool_t AliL3ModelTrack::GetZWidth(Int_t row,Float_t &width)
257 {
258   AliL3ClusterModel *cl = GetClusterModel(row);
259   width = cl->fDSigmaZ2*fZWidthQ + GetParSigmaZ2(row);
260   
261   return (Bool_t)cl->fPresent;
262 }
263
264 Bool_t AliL3ModelTrack::GetPadResidual(Int_t row,Float_t &res)
265 {
266   AliL3ClusterModel *cl = GetClusterModel(row);
267   res = cl->fDPad;
268   return cl->fPresent;
269 }
270
271 Bool_t AliL3ModelTrack::GetTimeResidual(Int_t row,Float_t &res)
272 {
273   AliL3ClusterModel *cl = GetClusterModel(row);
274   res = cl->fDTime;
275   return cl->fPresent;
276 }
277
278 Bool_t AliL3ModelTrack::GetXYWidthResidual(Int_t row,Float_t &res)
279 {
280   AliL3ClusterModel *cl = GetClusterModel(row);
281   res = cl->fDSigmaY2;
282   return cl->fPresent;
283 }
284
285 Bool_t AliL3ModelTrack::GetZWidthResidual(Int_t row,Float_t &res)
286 {
287   AliL3ClusterModel *cl = GetClusterModel(row);
288   res = cl->fDSigmaZ2;
289   return cl->fPresent;
290 }
291
292 Float_t AliL3ModelTrack::GetPadHit(Int_t row)
293 {
294   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
295   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
296     {
297       cerr<<"AliL3ModelTrack::GetPadHit() : Wrong index: "<<index<<" row "<<row<<endl;
298       return 0;
299     }
300   return fPad[index];
301 }
302
303 Float_t AliL3ModelTrack::GetTimeHit(Int_t row)
304 {
305   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
306   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
307     {
308       cerr<<"AliL3ModelTrack::GetTimeHit() : Wrong index: "<<index<<" row "<<row<<endl;
309       return 0;
310     }
311   return fTime[index];
312 }
313
314 Int_t AliL3ModelTrack::GetOverlap(Int_t row)
315 {
316   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
317   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
318     {
319       cerr<<"AliL3ModelTrack::GetOverlap() : Wrong index: "<<index<<endl;
320       return 0;
321     }
322   return fOverlap[index];
323 }
324
325 AliL3ClusterModel *AliL3ModelTrack::GetClusterModel(Int_t row)
326 {
327   if(!fClusters) return 0; 
328   Int_t index = row-AliL3Transform::GetFirstRow(fPatch);
329   if(index < 0 || index > AliL3Transform::GetNRows(fPatch))
330     {
331       cerr<<"AliL3ModelTrack::GetClusterModel() : Wrong index: "<<index<<endl;
332       return 0;
333     }
334   return &fClusters[index];
335 }
336
337 void AliL3ModelTrack::Print()
338 {
339   //Print info
340
341   cout<<"----Slice "<<fSlice<<" Patch "<<fPatch<<"----"<<endl;
342   cout<<"First point "<<GetFirstPointX()<<" "<<GetFirstPointY()<<" "<<GetFirstPointZ()<<endl;
343   cout<<"Last point "<<GetLastPointX()<<" "<<GetLastPointY()<<" "<<GetLastPointZ()<<endl;
344   cout<<"Pt "<<GetPt()<<" kappa "<<GetKappa()<<" tgl "<<GetTgl()<<" psi "<<GetPsi()<<" charge "<<GetCharge()<<endl;
345   cout<<"Center "<<GetCenterX()<<" "<<GetCenterY()<<endl<<endl;
346   cout<<"NHits "<<GetNClusters()<<endl;
347   cout<<"Clusters:"<<endl;
348
349   for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
350     {
351       AliL3ClusterModel *cl = GetClusterModel(i);
352       
353       if(!cl->fPresent)
354         cout<<i<<" Empty"<<" Padcrossing "<<GetPadHit(i)<<" Timecrossing "<<GetTimeHit(i)<<" ";
355       else
356         {
357           cout<<i<<" Dpad "<<cl->fDPad<<" Dtime "<<cl->fDTime<<" Dcharge "<<cl->fDCharge;
358           cout<<" DsigmaY2 "<<cl->fDSigmaY2<<" DsigmaZ2 "<<cl->fDSigmaZ2;
359           cout<<" Padcrossing "<<GetPadHit(i)<<" Timecrossing "<<GetTimeHit(i)<<" ";
360           cout<<"Number of pads "<<GetNPads(i)<<endl;
361         }
362       cout<<"Overlapping index "<<GetOverlap(i)<<endl;
363     }
364 }
365
366 Double_t AliL3ModelTrack::GetParSigmaY2(Int_t row)
367 {
368   //Calculate the expected cluster width, based on the track parameters and drift distance.
369
370   Float_t pad,time;
371   if(!GetTime(row,time) || !GetPad(row,pad))
372     return -1;
373   
374   Float_t xyz[3];
375   Int_t sector,padrow;
376   AliL3Transform::Slice2Sector(fSlice,row,sector,padrow);
377   AliL3Transform::Raw2Local(xyz,sector,padrow,pad,time);
378   
379   //Calculate the drift length:
380   Double_t drift;
381   if(xyz[2] > 0)
382     drift = AliL3Transform::GetZLength() - xyz[2];
383   else
384     drift = AliL3Transform::GetZLength() + xyz[2];
385   
386   Double_t prf = AliL3Transform::GetPRFSigma(fPatch);
387   Double_t diffT = AliL3Transform::GetDiffT();
388   Double_t padlength = AliL3Transform::GetPadLength(fPatch);
389   Double_t anode = AliL3Transform::GetAnodeWireSpacing();
390   Double_t beta = GetCrossingAngle(row);
391   
392   Double_t sigmaY2 = prf*prf + diffT*diffT*drift + padlength*padlength*tan(beta)*tan(beta)/12 + anode*anode*pow( tan(beta)-0.15, 2)/12;
393   
394   //Convert back to raw coordinates.
395   sigmaY2 = sigmaY2/pow(AliL3Transform::GetPadPitchWidth(fPatch),2);
396   return sigmaY2;
397 }
398
399 Double_t AliL3ModelTrack::GetParSigmaZ2(Int_t row)
400 {
401   //Calculate the expected cluster width, based on the track parameters and drift distance.
402   
403   Float_t pad,time;
404   if(!GetTime(row,time) || !GetPad(row,pad))
405     return -1;
406   
407   Float_t xyz[3];
408   Int_t sector,padrow;
409   AliL3Transform::Slice2Sector(fSlice,row,sector,padrow);
410   AliL3Transform::Raw2Local(xyz,sector,padrow,pad,time);
411   
412   //Calculate the drift length:
413   Double_t drift;
414   if(xyz[2] > 0)
415     drift = AliL3Transform::GetZLength() - xyz[2];
416   else
417     drift = AliL3Transform::GetZLength() + xyz[2];
418   
419   Double_t sigma0 = AliL3Transform::GetTimeSigma();
420   Double_t diffL = AliL3Transform::GetDiffL();
421   Double_t padlength = AliL3Transform::GetPadLength(fPatch);
422   Double_t tanl = GetTgl();
423   
424   Double_t sigmaZ2 = sigma0*sigma0 + diffL*diffL*drift + padlength*padlength * tanl*tanl/12;
425   
426   //Convert back to raw coodinates:
427   sigmaZ2 = sigmaZ2/pow(AliL3Transform::GetZWidth(),2);
428   return sigmaZ2;
429   
430 }