Merge branch 'TPCdev' of https://git.cern.ch/reps/AliRoot into TPCdev
[u/mrichter/AliRoot.git] / TPC / Base / AliTPCTransform.cxx
CommitLineData
022ee144 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
66954e3f 23// Transformation
022ee144 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//
003b43ed 31// Time of flight correction -
32// - Depends on the vertex position
33// - by default
34//
022ee144 35// Usage:
829455ad 36// AliTPCclusterer::AddCluster
37// AliTPCtracker::Transform
022ee144 38//
39//-------------------------------------------------------
c1bdda91 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
022ee144 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"
cf5b0aa0 62#include "AliTPCCorrection.h"
66954e3f 63#include "TGeoMatrix.h"
9430b11a 64#include "AliTPCRecoParam.h"
65#include "AliTPCCalibVdrift.h"
022ee144 66#include "AliTPCTransform.h"
0b736a46 67#include "AliMagF.h"
68#include "TGeoGlobalMagField.h"
69#include "AliTracker.h"
1e6337b1 70#include <AliCTPTimeParams.h>
022ee144 71
66954e3f 72ClassImp(AliTPCTransform)
022ee144 73
74
9430b11a 75AliTPCTransform::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}
93AliTPCTransform::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
003b43ed 98{
24db6af7 99 //
c1bdda91 100 // Speed it up a bit!
24db6af7 101 //
c1bdda91 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 }
003b43ed 107 fPrimVtx[0]=0;
108 fPrimVtx[1]=0;
109 fPrimVtx[2]=0;
c1bdda91 110}
111
112AliTPCTransform::~AliTPCTransform() {
24db6af7 113 //
114 // Destructor
115 //
c1bdda91 116}
117
003b43ed 118void 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
24db6af7 128void AliTPCTransform::Transform(Double_t *x,Int_t *i,UInt_t /*time*/,
003b43ed 129 Int_t /*coordinateType*/) {
24db6af7 130 // input: x[0] - pad row
131 // x[1] - pad
c1bdda91 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
ecc5dd8f 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 //
768e46f1 143 if (!fCurrentRecoParam) {
144 return;
145 }
24db6af7 146 Int_t row=TMath::Nint(x[0]);
147 Int_t pad=TMath::Nint(x[1]);
c1bdda91 148 Int_t sector=i[0];
9430b11a 149 AliTPCcalibDB* calib=AliTPCcalibDB::Instance();
24db6af7 150 //
151 AliTPCCalPad * time0TPC = calib->GetPadTime0();
2293155b 152 AliTPCCalPad * distortionMapY = calib->GetDistortionMap(0);
153 AliTPCCalPad * distortionMapZ = calib->GetDistortionMap(1);
4486a91f 154 AliTPCCalPad * distortionMapR = calib->GetDistortionMap(2);
24db6af7 155 AliTPCParam * param = calib->GetParameters();
0b736a46 156 AliTPCCorrection * correction = calib->GetTPCComposedCorrection(); // first user defined correction // if does not exist try to get it from calibDB array
157 if (!correction) correction = calib->GetTPCComposedCorrection(AliTracker::GetBz());
24db6af7 158 if (!time0TPC){
159 AliFatal("Time unisochronity missing");
768e46f1 160 return ; // make coverity happy
24db6af7 161 }
3f3549a3 162 AliTPCCorrection * correctionDelta = calib->GetTPCComposedCorrectionDelta();
c1bdda91 163
24db6af7 164 if (!param){
165 AliFatal("Parameters missing");
768e46f1 166 return; // make coverity happy
24db6af7 167 }
c1bdda91 168
24db6af7 169 Double_t xx[3];
170 // Apply Time0 correction - Pad by pad fluctuation
3f3549a3 171 //
172 if (!calib->HasAlignmentOCDB()) x[2]-=time0TPC->GetCalROC(sector)->GetValue(row,pad);
24db6af7 173 //
174 // Tranform from pad - time coordinate system to the rotated global (tracking) system
175 //
176 Local2RotatedGlobal(sector,x);
177 //
178 //
179 //
c1bdda91 180 // Alignment
181 //TODO: calib->GetParameters()->GetClusterMatrix(sector)->LocalToMaster(x,xx);
c1bdda91 182 RotatedGlobal2Global(sector,x);
cf5b0aa0 183
24db6af7 184 //
cf5b0aa0 185 // old ExB correction
24db6af7 186 //
768e46f1 187 if(fCurrentRecoParam->GetUseExBCorrection()) {
3ce45991 188
189 calib->GetExB()->Correct(x,xx);
190
191 } else {
192
193 xx[0] = x[0];
194 xx[1] = x[1];
195 xx[2] = x[2];
196 }
197
ecc5dd8f 198 //
cf5b0aa0 199 // new composed correction - will replace soon ExB correction
200 //
768e46f1 201 if(fCurrentRecoParam->GetUseComposedCorrection()&&correction) {
2942f542 202 Float_t distPoint[3]={static_cast<Float_t>(xx[0]),static_cast<Float_t>(xx[1]),static_cast<Float_t>(xx[2])};
cf5b0aa0 203 correction->CorrectPoint(distPoint, sector);
204 xx[0]=distPoint[0];
205 xx[1]=distPoint[1];
206 xx[2]=distPoint[2];
3f3549a3 207 if (correctionDelta&&fCurrentRecoParam->GetUseAlignmentTime()){ // appply time dependent correction if available and enabled
2942f542 208 Float_t distPointDelta[3]={static_cast<Float_t>(xx[0]),static_cast<Float_t>(xx[1]),static_cast<Float_t>(xx[2])};
5c8d0ec4 209 correctionDelta->CorrectPoint(distPointDelta, sector);
3f3549a3 210 xx[0]=distPointDelta[0];
211 xx[1]=distPointDelta[1];
212 xx[2]=distPointDelta[2];
213 }
cf5b0aa0 214 }
215
216
217 //
ecc5dd8f 218 // Time of flight correction
003b43ed 219 //
768e46f1 220 if (fCurrentRecoParam->GetUseTOFCorrection()){
9430b11a 221 const Int_t kNIS=param->GetNInnerSector(), kNOS=param->GetNOuterSector();
222 Float_t sign=1;
223 if (sector < kNIS) {
224 sign = (sector < kNIS/2) ? 1 : -1;
225 } else {
226 sign = ((sector-kNIS) < kNOS/2) ? 1 : -1;
227 }
228 Float_t deltaDr =0;
229 Float_t dist=0;
230 dist+=(fPrimVtx[0]-x[0])*(fPrimVtx[0]-x[0]);
231 dist+=(fPrimVtx[1]-x[1])*(fPrimVtx[1]-x[1]);
232 dist+=(fPrimVtx[2]-x[2])*(fPrimVtx[2]-x[2]);
233 dist = TMath::Sqrt(dist);
234 // drift length correction because of TOF
235 // the drift velocity is in cm/s therefore multiplication by 0.01
236 deltaDr = (dist*(0.01*param->GetDriftV()))/TMath::C();
237 xx[2]+=sign*deltaDr;
ecc5dd8f 238 }
9430b11a 239 //
240 //
241 //
242
ecc5dd8f 243 //
c1bdda91 244 Global2RotatedGlobal(sector,xx);
2293155b 245
246 //
247 // Apply non linear distortion correction
248 //
249 if (distortionMapY ){
0b736a46 250 // wt - to get it form the OCDB
251 // ignore T1 and T2
252 AliMagF* magF= (AliMagF*)TGeoGlobalMagField::Instance()->GetField();
253 Double_t bzField = magF->SolenoidField()/10.; //field in T
254 Double_t vdrift = param->GetDriftV()/1000000.; // [cm/us] // From dataBase: to be updated: per second (ideally)
255 Double_t ezField = 400; // [V/cm] // to be updated: never (hopefully)
256 if (sector%36<18) ezField*=-1;
257 Double_t wt = -10.0 * (bzField*10) * vdrift / ezField ;
258 Double_t c0=1./(1.+wt*wt);
259 Double_t c1=wt/c0;
260
2293155b 261 //can be switch on for each dimension separatelly
4486a91f 262 if (fCurrentRecoParam->GetUseFieldCorrection()&0x2)
0b736a46 263 if (distortionMapY){
264 xx[1]-= c0*distortionMapY->GetCalROC(sector)->GetValue(row,pad);
265 xx[0]-= c1*distortionMapY->GetCalROC(sector)->GetValue(row,pad);
266 }
2293155b 267 if (fCurrentRecoParam->GetUseFieldCorrection()&0x4)
4486a91f 268 if (distortionMapZ)
269 xx[2]-=distortionMapZ->GetCalROC(sector)->GetValue(row,pad);
270 if (fCurrentRecoParam->GetUseFieldCorrection()&0x8)
0b736a46 271 if (distortionMapR){
272 xx[0]-= c0*distortionMapR->GetCalROC(sector)->GetValue(row,pad);
273 xx[1]-=-c1*distortionMapR->GetCalROC(sector)->GetValue(row,pad)*wt;
274 }
275
2293155b 276 }
4486a91f 277 //
2293155b 278
003b43ed 279 //
c1bdda91 280 x[0]=xx[0];x[1]=xx[1];x[2]=xx[2];
281}
282
24db6af7 283void AliTPCTransform::Local2RotatedGlobal(Int_t sector, Double_t *x) const {
284 //
285 //
022ee144 286 // Tranform coordinate from
287 // row, pad, time to x,y,z
24db6af7 288 //
022ee144 289 // Drift Velocity
290 // Current implementation - common drift velocity - for full chamber
24db6af7 291 // TODO: use a map or parametrisation!
292 //
293 //
294 //
768e46f1 295 if (!fCurrentRecoParam) return;
9430b11a 296 const Int_t kMax =60; // cache for 60 seconds
297 static Int_t lastStamp=-1; //cached values
298 static Double_t lastCorr = 1;
299 //
66954e3f 300 AliTPCcalibDB* calib=AliTPCcalibDB::Instance();
24db6af7 301 AliTPCParam * param = calib->GetParameters();
9430b11a 302 AliTPCCalibVdrift *driftCalib = AliTPCcalibDB::Instance()->GetVdrift(fCurrentRun);
303 Double_t driftCorr = 1.;
304 if (driftCalib){
305 //
306 // caching drift correction - temp. fix
307 // Extremally slow procedure
308 if ( TMath::Abs((lastStamp)-Int_t(fCurrentTimeStamp))<kMax){
309 driftCorr = lastCorr;
310 }else{
311 driftCorr = 1.+(driftCalib->GetPTRelative(fCurrentTimeStamp,0)+ driftCalib->GetPTRelative(fCurrentTimeStamp,1))*0.5;
312 lastCorr=driftCorr;
313 lastStamp=fCurrentTimeStamp;
314
315 }
316 }
43a74775 317 //
a8f8b6a1 318 // simple caching non thread save
319 static Double_t vdcorrectionTime=1;
5647625c 320 static Double_t vdcorrectionTimeGY=0;
a8f8b6a1 321 static Double_t time0corrTime=0;
c3fea858 322 static Double_t deltaZcorrTime=0;
a8f8b6a1 323 static Int_t lastStampT=-1;
324 //
817766d5 325 if (lastStampT!=(Int_t)fCurrentTimeStamp){
a8f8b6a1 326 lastStampT=fCurrentTimeStamp;
768e46f1 327 if(fCurrentRecoParam->GetUseDriftCorrectionTime()>0) {
a8f8b6a1 328 vdcorrectionTime = (1+AliTPCcalibDB::Instance()->
329 GetVDriftCorrectionTime(fCurrentTimeStamp,
fefec90f 330 fCurrentRun,
a8f8b6a1 331 sector%36>=18,
332 fCurrentRecoParam->GetUseDriftCorrectionTime()));
333 time0corrTime= AliTPCcalibDB::Instance()->
334 GetTime0CorrectionTime(fCurrentTimeStamp,
fefec90f 335 fCurrentRun,
a8f8b6a1 336 sector%36>=18,
337 fCurrentRecoParam->GetUseDriftCorrectionTime());
c3fea858 338 //
339 deltaZcorrTime= AliTPCcalibDB::Instance()->
340 GetVDriftCorrectionDeltaZ(fCurrentTimeStamp,
341 fCurrentRun,
342 sector%36>=18,
343 0);
344
a8f8b6a1 345 }
346 //
768e46f1 347 if(fCurrentRecoParam->GetUseDriftCorrectionGY()>0) {
a8f8b6a1 348
5647625c 349 Double_t corrGy= AliTPCcalibDB::Instance()->
a8f8b6a1 350 GetVDriftCorrectionGy(fCurrentTimeStamp,
351 AliTPCcalibDB::Instance()->GetRun(),
352 sector%36>=18,
5647625c 353 fCurrentRecoParam->GetUseDriftCorrectionGY());
354 vdcorrectionTimeGY = corrGy;
a8f8b6a1 355 }
43a74775 356 }
9430b11a 357
358
24db6af7 359 if (!param){
360 AliFatal("Parameters missing");
768e46f1 361 return; // make coverity happy
24db6af7 362 }
363 Int_t row=TMath::Nint(x[0]);
9389f9a4 364 // Int_t pad=TMath::Nint(x[1]);
24db6af7 365 //
366 const Int_t kNIS=param->GetNInnerSector(), kNOS=param->GetNOuterSector();
367 Double_t sign = 1.;
502d13b5 368 Double_t zwidth = param->GetZWidth()*driftCorr;
5647625c 369 Float_t xyzPad[3];
370 AliTPCROC::Instance()->GetPositionGlobal(sector, TMath::Nint(x[0]) ,TMath::Nint(x[1]), xyzPad);
371 if (AliTPCRecoParam:: GetUseTimeCalibration()) zwidth*=vdcorrectionTime*(1+xyzPad[1]*vdcorrectionTimeGY);
24db6af7 372 Double_t padWidth = 0;
373 Double_t padLength = 0;
374 Double_t maxPad = 0;
375 //
376 if (sector < kNIS) {
377 maxPad = param->GetNPadsLow(row);
378 sign = (sector < kNIS/2) ? 1 : -1;
379 padLength = param->GetPadPitchLength(sector,row);
380 padWidth = param->GetPadPitchWidth(sector);
381 } else {
382 maxPad = param->GetNPadsUp(row);
383 sign = ((sector-kNIS) < kNOS/2) ? 1 : -1;
384 padLength = param->GetPadPitchLength(sector,row);
385 padWidth = param->GetPadPitchWidth(sector);
386 }
387 //
388 // X coordinate
022ee144 389 x[0] = param->GetPadRowRadii(sector,row); // padrow X position - ideal
24db6af7 390 //
391 // Y coordinate
392 //
393 x[1]=(x[1]-0.5*maxPad)*padWidth;
afbaa016 394 // pads are mirrorred on C-side
395 if (sector%36>17){
396 x[1]*=-1;
397 }
398
399 //
400
24db6af7 401 //
402 // Z coordinate
403 //
1e6337b1 404 if (AliTPCcalibDB::Instance()->IsTrgL0()){
405 // by defualt we assume L1 trigger is used - make a correction in case of L0
406 AliCTPTimeParams* ctp = AliTPCcalibDB::Instance()->GetCTPTimeParams();
3d1b41c1 407 if (ctp){
408 //for TPC standalone runs no ctp info
409 Double_t delay = ctp->GetDelayL1L0()*0.000000025;
410 x[2]-=delay/param->GetTSample();
411 }
1e6337b1 412 }
413 x[2]-= param->GetNTBinsL1();
24db6af7 414 x[2]*= zwidth; // tranform time bin to the distance to the ROC
1e6337b1 415 x[2]-= 3.*param->GetZSigma() + time0corrTime;
24db6af7 416 // subtract the time offsets
417 x[2] = sign*( param->GetZLength(sector) - x[2]);
c3fea858 418 x[2]-=deltaZcorrTime; // subtrack time dependent z shift (calibrated together with the drift velocity and T0)
c1bdda91 419}
420
c2da420d 421void AliTPCTransform::RotatedGlobal2Global(Int_t sector,Double_t *x) const {
24db6af7 422 //
423 // transform possition rotated global to the global
424 //
c1bdda91 425 Double_t cos,sin;
426 GetCosAndSin(sector,cos,sin);
427 Double_t tmp=x[0];
e81544f7 428 x[0]= cos*tmp-sin*x[1];
429 x[1]=+sin*tmp+cos*x[1];
c1bdda91 430}
431
c2da420d 432void AliTPCTransform::Global2RotatedGlobal(Int_t sector,Double_t *x) const {
24db6af7 433 //
434 // tranform possition Global2RotatedGlobal
435 //
c1bdda91 436 Double_t cos,sin;
437 GetCosAndSin(sector,cos,sin);
438 Double_t tmp=x[0];
e81544f7 439 x[0]= cos*tmp+sin*x[1];
440 x[1]= -sin*tmp+cos*x[1];
c1bdda91 441}
442
c2da420d 443void AliTPCTransform::GetCosAndSin(Int_t sector,Double_t &cos,
c1bdda91 444 Double_t &sin) const {
445 cos=fCoss[sector%18];
446 sin=fSins[sector%18];
447}
448
c1bdda91 449
43a74775 450void AliTPCTransform::ApplyTransformations(Double_t */*xyz*/, Int_t /*volID*/){
451 //
452 // Modify global position
453 // xyz - global xyz position
454 // volID - volID of detector (sector number)
455 //
456 //
457
458}