]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCTransform.cxx
Secure codin - overruns corrected.
[u/mrichter/AliRoot.git] / TPC / AliTPCTransform.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 //-------------------------------------------------------
17 //          Implementation of the TPC transformation class
18 //
19 //   Origin: Marian Ivanov   Marian.Ivanov@cern.ch
20 //           Magnus Mager
21 //
22 //   Class for tranformation of the coordinate frame
23 //   Transformation  
24 //    local coordinate frame (sector, padrow, pad, timebine) ==>
25 //    rotated global (tracking) cooridnate frame (sector, lx,ly,lz)
26 //
27 //    Unisochronity  - (substract time0 - pad by pad)
28 //    Drift velocity - Currently common drift velocity - functionality of AliTPCParam
29 //    ExB effect     - 
30 //
31 //    Time of flight correction -
32 //                   - Depends on the vertex position
33 //                   - by default 
34 //                           
35 //    Usage:
36 //          AliTPCclustererMI::AddCluster
37 //          AliTPCtrackerMI::Transform
38 //    
39 //-------------------------------------------------------
40
41 /* To test it:
42    cdb=AliCDBManager::Instance()
43    cdb->SetDefaultStorage("local:///u/mmager/mycalib1")
44    c=AliTPCcalibDB::Instance()
45    c->SetRun(0)
46    Double_t x[]={1.0,2.0,3.0}
47    Int_t i[]={4}
48    AliTPCTransform trafo
49    trafo.Transform(x,i,0,1)
50  */
51
52 /* $Id$ */
53
54 #include "AliTPCROC.h"
55 #include "AliTPCCalPad.h"
56 #include "AliTPCCalROC.h"
57 #include "AliTPCcalibDB.h"
58 #include "AliTPCParam.h"
59 #include "TMath.h"
60 #include "AliLog.h"
61 #include "AliTPCExB.h"
62 #include "AliTPCCorrection.h"
63 #include "TGeoMatrix.h"
64 #include "AliTPCRecoParam.h"
65 #include "AliTPCCalibVdrift.h"
66 #include "AliTPCTransform.h"
67 #include "AliMagF.h"
68 #include "TGeoGlobalMagField.h"
69 #include "AliTracker.h"
70 #include <AliCTPTimeParams.h>
71
72 ClassImp(AliTPCTransform)
73
74
75 AliTPCTransform::AliTPCTransform():
76   AliTransform(),
77   fCurrentRecoParam(0),       //! current reconstruction parameters
78   fCurrentRun(0),             //! current run
79   fCurrentTimeStamp(0)        //! current time stamp   
80 {
81   //
82   // Speed it up a bit!
83   //
84   for (Int_t i=0;i<18;++i) {
85     Double_t alpha=TMath::DegToRad()*(10.+20.*(i%18));
86     fSins[i]=TMath::Sin(alpha);
87     fCoss[i]=TMath::Cos(alpha);
88   }
89   fPrimVtx[0]=0;
90   fPrimVtx[1]=0;
91   fPrimVtx[2]=0;
92 }
93 AliTPCTransform::AliTPCTransform(const AliTPCTransform& transform):
94   AliTransform(transform),
95   fCurrentRecoParam(transform.fCurrentRecoParam),       //! current reconstruction parameters
96   fCurrentRun(transform.fCurrentRun),             //! current run
97   fCurrentTimeStamp(transform.fCurrentTimeStamp)        //! current time stamp   
98 {
99   //
100   // Speed it up a bit!
101   //
102   for (Int_t i=0;i<18;++i) {
103     Double_t alpha=TMath::DegToRad()*(10.+20.*(i%18));
104     fSins[i]=TMath::Sin(alpha);
105     fCoss[i]=TMath::Cos(alpha);
106   }
107   fPrimVtx[0]=0;
108   fPrimVtx[1]=0;
109   fPrimVtx[2]=0;
110 }
111
112 AliTPCTransform::~AliTPCTransform() {
113   //
114   // Destructor
115   //
116 }
117
118 void AliTPCTransform::SetPrimVertex(Double_t *vtx){
119   //
120   //
121   //
122   fPrimVtx[0]=vtx[0];
123   fPrimVtx[1]=vtx[1];
124   fPrimVtx[2]=vtx[2];
125 }
126
127
128 void AliTPCTransform::Transform(Double_t *x,Int_t *i,UInt_t /*time*/,
129                                 Int_t /*coordinateType*/) {
130   // input: x[0] - pad row
131   //        x[1] - pad 
132   //        x[2] - time in us
133   //        i[0] - sector
134   // output: x[0] - x (all in the rotated global coordinate frame)
135   //         x[1] - y
136   //         x[2] - z
137   //
138   //  primvtx     - position of the primary vertex
139   //                used for the TOF correction
140   //                TOF of particle calculated assuming the speed-of-light and 
141   //                line approximation  
142   //
143   
144   Int_t row=TMath::Nint(x[0]);
145   Int_t pad=TMath::Nint(x[1]);
146   Int_t sector=i[0];
147   AliTPCcalibDB*  calib=AliTPCcalibDB::Instance();  
148   //
149   AliTPCCalPad * time0TPC = calib->GetPadTime0(); 
150   AliTPCCalPad * distortionMapY = calib->GetDistortionMap(0); 
151   AliTPCCalPad * distortionMapZ = calib->GetDistortionMap(1); 
152   AliTPCCalPad * distortionMapR = calib->GetDistortionMap(2); 
153   AliTPCParam  * param    = calib->GetParameters(); 
154   AliTPCCorrection * correction = calib->GetTPCComposedCorrection();   // first user defined correction  // if does not exist  try to get it from calibDB array
155   if (!correction) correction = calib->GetTPCComposedCorrection(AliTracker::GetBz());
156   if (!time0TPC){
157     AliFatal("Time unisochronity missing");
158   }
159   AliTPCCorrection * correctionDelta = calib->GetTPCComposedCorrectionDelta(); 
160
161   if (!param){
162     AliFatal("Parameters missing");
163   }
164
165   Double_t xx[3];
166   //  Apply Time0 correction - Pad by pad fluctuation
167   //  
168   if (!calib->HasAlignmentOCDB()) x[2]-=time0TPC->GetCalROC(sector)->GetValue(row,pad);
169   //
170   // Tranform from pad - time coordinate system to the rotated global (tracking) system
171   //
172   Local2RotatedGlobal(sector,x);
173   //
174   //
175   //
176   // Alignment
177   //TODO:  calib->GetParameters()->GetClusterMatrix(sector)->LocalToMaster(x,xx);
178   RotatedGlobal2Global(sector,x);
179   
180   //
181   // old ExB correction 
182   //
183   if(fCurrentRecoParam&&fCurrentRecoParam->GetUseExBCorrection()) {
184
185     calib->GetExB()->Correct(x,xx);
186
187   } else {
188
189     xx[0] = x[0];
190     xx[1] = x[1];
191     xx[2] = x[2];
192   }
193
194   //
195   // new composed  correction  - will replace soon ExB correction
196   //
197   if(fCurrentRecoParam&&fCurrentRecoParam->GetUseComposedCorrection()&&correction) {
198     Float_t distPoint[3]={xx[0],xx[1],xx[2]};
199     correction->CorrectPoint(distPoint, sector);
200     xx[0]=distPoint[0];
201     xx[1]=distPoint[1];
202     xx[2]=distPoint[2];
203     if (correctionDelta&&fCurrentRecoParam->GetUseAlignmentTime()){  // appply time dependent correction if available and enabled
204       Float_t distPointDelta[3]={xx[0],xx[1],xx[2]};
205       correctionDelta->CorrectPoint(distPointDelta, sector);
206       xx[0]=distPointDelta[0];
207       xx[1]=distPointDelta[1];
208       xx[2]=distPointDelta[2];
209     }
210   } 
211
212
213   //
214   // Time of flight correction
215   // 
216   if (fCurrentRecoParam&&fCurrentRecoParam->GetUseTOFCorrection()){
217     const Int_t kNIS=param->GetNInnerSector(), kNOS=param->GetNOuterSector(); 
218     Float_t sign=1;
219     if (sector < kNIS) {
220       sign = (sector < kNIS/2) ? 1 : -1;
221     } else {
222       sign = ((sector-kNIS) < kNOS/2) ? 1 : -1;
223     }
224     Float_t deltaDr =0;
225     Float_t dist=0;
226     dist+=(fPrimVtx[0]-x[0])*(fPrimVtx[0]-x[0]);
227     dist+=(fPrimVtx[1]-x[1])*(fPrimVtx[1]-x[1]);
228     dist+=(fPrimVtx[2]-x[2])*(fPrimVtx[2]-x[2]);
229     dist = TMath::Sqrt(dist);
230     // drift length correction because of TOF
231     // the drift velocity is in cm/s therefore multiplication by 0.01
232     deltaDr = (dist*(0.01*param->GetDriftV()))/TMath::C(); 
233     xx[2]+=sign*deltaDr;
234   }
235   //
236   //
237   //
238
239   //
240   Global2RotatedGlobal(sector,xx);
241
242   //
243   // Apply non linear distortion correction  
244   //
245   if (distortionMapY ){
246     // wt - to get it form the OCDB
247     // ignore T1 and T2
248     AliMagF* magF= (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
249     Double_t bzField = magF->SolenoidField()/10.; //field in T
250     Double_t vdrift = param->GetDriftV()/1000000.; // [cm/us]   // From dataBase: to be updated: per second (ideally)
251     Double_t ezField = 400; // [V/cm]   // to be updated: never (hopefully)
252     if (sector%36<18) ezField*=-1;
253     Double_t wt = -10.0 * (bzField*10) * vdrift / ezField ;
254     Double_t c0=1./(1.+wt*wt);
255     Double_t c1=wt/c0;
256     
257     //can be switch on for each dimension separatelly
258     if (fCurrentRecoParam->GetUseFieldCorrection()&0x2)
259       if (distortionMapY){
260         xx[1]-= c0*distortionMapY->GetCalROC(sector)->GetValue(row,pad);
261         xx[0]-= c1*distortionMapY->GetCalROC(sector)->GetValue(row,pad);
262       }
263     if (fCurrentRecoParam->GetUseFieldCorrection()&0x4) 
264       if (distortionMapZ)
265         xx[2]-=distortionMapZ->GetCalROC(sector)->GetValue(row,pad);
266     if (fCurrentRecoParam->GetUseFieldCorrection()&0x8) 
267       if (distortionMapR){
268         xx[0]-= c0*distortionMapR->GetCalROC(sector)->GetValue(row,pad);
269         xx[1]-=-c1*distortionMapR->GetCalROC(sector)->GetValue(row,pad)*wt;
270       }
271     
272   }
273   //
274
275   //
276   x[0]=xx[0];x[1]=xx[1];x[2]=xx[2];
277 }
278
279 void AliTPCTransform::Local2RotatedGlobal(Int_t sector, Double_t *x) const {
280   //
281   //  
282   // Tranform coordinate from  
283   // row, pad, time to x,y,z
284   //
285   // Drift Velocity 
286   // Current implementation - common drift velocity - for full chamber
287   // TODO: use a map or parametrisation!
288   //
289   //  
290   //
291   const  Int_t kMax =60;  // cache for 60 seconds
292   static Int_t lastStamp=-1;  //cached values
293   static Double_t lastCorr = 1;
294   //
295   AliTPCcalibDB*  calib=AliTPCcalibDB::Instance();
296   AliTPCParam  * param    = calib->GetParameters(); 
297   AliTPCCalibVdrift *driftCalib = AliTPCcalibDB::Instance()->GetVdrift(fCurrentRun);
298   Double_t driftCorr = 1.;
299   if (driftCalib){
300     //
301     // caching drift correction - temp. fix
302     // Extremally slow procedure
303     if ( TMath::Abs((lastStamp)-Int_t(fCurrentTimeStamp))<kMax){
304       driftCorr = lastCorr;
305     }else{
306       driftCorr = 1.+(driftCalib->GetPTRelative(fCurrentTimeStamp,0)+ driftCalib->GetPTRelative(fCurrentTimeStamp,1))*0.5;
307       lastCorr=driftCorr;
308       lastStamp=fCurrentTimeStamp;
309       
310     }
311   }
312   //
313   // simple caching non thread save
314   static Double_t vdcorrectionTime=1;
315   static Double_t vdcorrectionTimeGY=0;
316   static Double_t time0corrTime=0;
317   static Int_t    lastStampT=-1;
318   //
319   if (lastStampT!=(Int_t)fCurrentTimeStamp){
320     lastStampT=fCurrentTimeStamp;
321     if(fCurrentRecoParam&&fCurrentRecoParam->GetUseDriftCorrectionTime()>0) {
322       vdcorrectionTime = (1+AliTPCcalibDB::Instance()->
323                           GetVDriftCorrectionTime(fCurrentTimeStamp, 
324                                                   fCurrentRun,
325                                                   sector%36>=18,
326                                                   fCurrentRecoParam->GetUseDriftCorrectionTime()));                       
327       time0corrTime= AliTPCcalibDB::Instance()->
328         GetTime0CorrectionTime(fCurrentTimeStamp, 
329                                fCurrentRun,
330                                sector%36>=18,
331                                fCurrentRecoParam->GetUseDriftCorrectionTime()); 
332     }
333     //
334     if(fCurrentRecoParam&&fCurrentRecoParam->GetUseDriftCorrectionGY()>0) {
335       
336       Double_t corrGy= AliTPCcalibDB::Instance()->
337                         GetVDriftCorrectionGy(fCurrentTimeStamp, 
338                                               AliTPCcalibDB::Instance()->GetRun(),
339                                               sector%36>=18,
340                                               fCurrentRecoParam->GetUseDriftCorrectionGY());
341       vdcorrectionTimeGY = corrGy;
342     }
343   }
344
345
346   if (!param){
347     AliFatal("Parameters missing");
348   }
349   Int_t row=TMath::Nint(x[0]);
350   //  Int_t pad=TMath::Nint(x[1]);
351   //
352   const Int_t kNIS=param->GetNInnerSector(), kNOS=param->GetNOuterSector();
353   Double_t sign = 1.;
354   Double_t zwidth    = param->GetZWidth()*driftCorr;
355   Float_t xyzPad[3];
356   AliTPCROC::Instance()->GetPositionGlobal(sector, TMath::Nint(x[0]) ,TMath::Nint(x[1]), xyzPad);
357   if (AliTPCRecoParam:: GetUseTimeCalibration()) zwidth*=vdcorrectionTime*(1+xyzPad[1]*vdcorrectionTimeGY);
358   Double_t padWidth  = 0;
359   Double_t padLength = 0;
360   Double_t    maxPad    = 0;
361   //
362   if (sector < kNIS) {
363     maxPad = param->GetNPadsLow(row);
364     sign = (sector < kNIS/2) ? 1 : -1;
365     padLength = param->GetPadPitchLength(sector,row);
366     padWidth = param->GetPadPitchWidth(sector);
367   } else {
368     maxPad = param->GetNPadsUp(row);
369     sign = ((sector-kNIS) < kNOS/2) ? 1 : -1;
370     padLength = param->GetPadPitchLength(sector,row);
371     padWidth  = param->GetPadPitchWidth(sector);
372   }
373   //
374   // X coordinate
375   x[0] = param->GetPadRowRadii(sector,row);  // padrow X position - ideal
376   //
377   // Y coordinate
378   //
379   x[1]=(x[1]-0.5*maxPad)*padWidth;
380   // pads are mirrorred on C-side
381   if (sector%36>17){
382     x[1]*=-1;
383   }
384   
385   //
386   
387   //
388   // Z coordinate
389   //
390   if (AliTPCcalibDB::Instance()->IsTrgL0()){
391     // by defualt we assume L1 trigger is used - make a correction in case of  L0
392     AliCTPTimeParams* ctp = AliTPCcalibDB::Instance()->GetCTPTimeParams();
393     if (ctp){
394       //for TPC standalone runs no ctp info
395       Double_t delay = ctp->GetDelayL1L0()*0.000000025;
396       x[2]-=delay/param->GetTSample();
397     }
398   }
399   x[2]-= param->GetNTBinsL1();
400   x[2]*= zwidth;  // tranform time bin to the distance to the ROC
401   x[2]-= 3.*param->GetZSigma() + time0corrTime;
402   // subtract the time offsets
403   x[2] = sign*( param->GetZLength(sector) - x[2]);
404 }
405
406 void AliTPCTransform::RotatedGlobal2Global(Int_t sector,Double_t *x) const {
407   //
408   // transform possition rotated global to the global
409   //
410   Double_t cos,sin;
411   GetCosAndSin(sector,cos,sin);
412   Double_t tmp=x[0];
413   x[0]= cos*tmp-sin*x[1];
414   x[1]=+sin*tmp+cos*x[1];
415 }
416
417 void AliTPCTransform::Global2RotatedGlobal(Int_t sector,Double_t *x) const {
418   //
419   // tranform possition Global2RotatedGlobal
420   //
421   Double_t cos,sin;
422   GetCosAndSin(sector,cos,sin);
423   Double_t tmp=x[0];
424   x[0]= cos*tmp+sin*x[1];
425   x[1]= -sin*tmp+cos*x[1];
426 }
427
428 void AliTPCTransform::GetCosAndSin(Int_t sector,Double_t &cos,
429                                           Double_t &sin) const {
430   cos=fCoss[sector%18];
431   sin=fSins[sector%18];
432 }
433
434
435 void AliTPCTransform::ApplyTransformations(Double_t */*xyz*/, Int_t /*volID*/){
436   //
437   // Modify global position
438   // xyz    - global xyz position
439   // volID  - volID of detector (sector number)
440   //
441   //
442   
443 }