ROOT logo
#ifndef ALI_TPC_CORRECTION_H
#define ALI_TPC_CORRECTION_H

/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 * See cxx source for full Copyright notice                               */


////////////////////////////////////////////////////////////////////////////////
// AliTPCCorrection class                                                     //
////////////////////////////////////////////////////////////////////////////////


#include <TNamed.h>
#include "TMatrixD.h"
#include "TMatrixF.h"
class TH2F;
class TTimeStamp;
class TCollection;
class TTreeSRedirector;
class AliExternalTrackParam;
class TTree;
class THnSparse;
class AliESDVertex;


class AliTPCCorrection : public TNamed {
public:
  enum CompositionType {kParallel,kQueue};

  AliTPCCorrection();
  AliTPCCorrection(const char *name,const char *title);
  virtual ~AliTPCCorrection();
  virtual Bool_t AddCorrectionCompact(AliTPCCorrection* corr, Double_t weight);

  // functions to correct a space point
          void CorrectPoint (      Float_t x[], Short_t roc);
          void CorrectPointLocal(Float_t x[], Short_t roc);
          void CorrectPoint (const Float_t x[], Short_t roc,Float_t xp[]);
	  virtual void GetCorrection(const Float_t x[], Short_t roc,Float_t dx[]);

  virtual void GetCorrectionDz(const Float_t x[], Short_t roc,Float_t dx[], Float_t delta);
  virtual void GetCorrectionIntegralDz(const Float_t x[], Short_t roc,Float_t dx[], Float_t delta);
  
  // functions to distort a space point
          void DistortPoint (      Float_t x[], Short_t roc);
          void DistortPointLocal(Float_t x[], Short_t roc);
          void DistortPoint (const Float_t x[], Short_t roc,Float_t xp[]);
  virtual void GetDistortion(const Float_t x[], Short_t roc,Float_t dx[]);

  virtual void GetDistortionDz(const Float_t x[], Short_t roc,Float_t dx[], Float_t delta);
  virtual void GetDistortionIntegralDz(const Float_t x[], Short_t roc,Float_t dx[], Float_t delta);
  
  // initialization and update functions
  virtual void Init();
  virtual void Update(const TTimeStamp &timeStamp);

  // map scaling
  virtual void    SetCorrScaleFactor(Float_t /*val*/) { ; }
  virtual Float_t GetCorrScaleFactor() const { return 1.; }
  
  // convenience functions
  virtual void Print(Option_t* option="") const;
 
  TH2F* CreateHistoDRinXY   (Float_t z=10.,Int_t nx=100,Int_t ny=100);
  TH2F* CreateHistoDRPhiinXY(Float_t z=10.,Int_t nx=100,Int_t nphi=100);
  TH2F* CreateHistoDZinXY   (Float_t z=10.,Int_t nx=100,Int_t ny=100);

  TH2F* CreateHistoDRinZR   (Float_t phi=0.,Int_t nZ=100,Int_t nR=100);
  TH2F* CreateHistoDRPhiinZR(Float_t phi=0.,Int_t nZ=100,Int_t nR=100);
  TH2F* CreateHistoDZinZR   (Float_t phi=0.,Int_t nZ=100,Int_t nR=100);


  TTree* CreateDistortionTree(Double_t step=5);
  static void  MakeDistortionMap(THnSparse * his0, TTreeSRedirector *pcstream, const char* hname, Int_t run,  Float_t refX, Int_t type, Int_t integ=1);
  static void  MakeDistortionMapCosmic(THnSparse * his0, TTreeSRedirector *pcstream, const char* hname, Int_t run,  Float_t refX, Int_t type);
  static void  MakeDistortionMapSector(THnSparse * his0, TTreeSRedirector *pcstream, const char* hname, Int_t run, Int_t type);
  // normally called directly in the correction classes which inherit from this class
  virtual void SetOmegaTauT1T2(Float_t omegaTau,Float_t t1,Float_t t2);
  AliExternalTrackParam * FitDistortedTrack(AliExternalTrackParam & trackIn, Double_t refX, Int_t dir,TTreeSRedirector *pcstream);
  void StoreInOCDB(Int_t startRun, Int_t endRun, const char *comment=0);
  static void MakeTrackDistortionTree(TTree *tinput, Int_t dtype, Int_t ptype, const TObjArray * corrArray, Int_t step=1, Int_t offset=0, Bool_t debug=0);
  static void MakeSectorDistortionTree(TTree *tinput, Int_t dtype, Int_t ptype, const TObjArray * corrArray, Int_t step=1, Int_t offset=0, Bool_t debug=0);
  static void MakeLaserDistortionTreeOld(TTree* tree, TObjArray *corrArray, Int_t itype);
  static void MakeLaserDistortionTree(TTree* tree, TObjArray *corrArray, Int_t itype);

  void FastSimDistortedVertex(Double_t orgVertex[3], Int_t nTracks, AliESDVertex &aV, AliESDVertex &avOrg, AliESDVertex &cV, AliESDVertex &cvOrg, TTreeSRedirector * const pcstream, Double_t etaCuts);

  static void AddVisualCorrection(AliTPCCorrection* corr, Int_t position);
  static AliTPCCorrection*  GetVisualCorrection(Int_t position);
  static Double_t GetCorrSector(Double_t sector, Double_t r, Double_t kZ, Int_t axisType, Int_t corrType=0);
  static Double_t GetCorrXYZ(Double_t gx, Double_t gy, Double_t gz, Int_t axisType, Int_t corrType=0);
  //
  static Double_t GetCorrXYZDz(Double_t gx, Double_t gy, Double_t gz, Int_t axisType, Int_t corrType=0,Double_t delta=5);
  static Double_t GetCorrXYZIntegrateZ(Double_t gx, Double_t gy, Double_t gz, Int_t axisType, Int_t corrType=0, Double_t delta=5);

  static Double_t GetDistXYZ(Double_t gx, Double_t gy, Double_t gz, Int_t axisType, Int_t corrType=0);
  //
  static Double_t GetDistXYZDz(Double_t gx, Double_t gy, Double_t gz, Int_t axisType, Int_t corrType=0,Double_t delta=5);
  static Double_t GetDistXYZIntegrateZ(Double_t gx, Double_t gy, Double_t gz, Int_t axisType, Int_t corrType=0, Double_t delta=5);
  

protected:
  TH2F* CreateTH2F(const char *name,const char *title,
		   const char *xlabel,const char *ylabel,const char *zlabel,
		   Int_t nbinsx,Double_t xlow,Double_t xup,
		   Int_t nbinsy,Double_t ylow,Double_t yup);
 
  static const Double_t fgkTPCZ0;       // nominal gating grid position 
  static const Double_t fgkIFCRadius;   // Mean Radius of the Inner Field Cage ( 82.43 min,  83.70 max) (cm)
  static const Double_t fgkOFCRadius;   // Mean Radius of the Outer Field Cage (252.55 min, 256.45 max) (cm)
  static const Double_t fgkZOffSet;     // Offset from CE: calculate all distortions closer to CE as if at this point
  static const Double_t fgkCathodeV;    // Cathode Voltage (volts)
  static const Double_t fgkGG;          // Gating Grid voltage (volts)
  static const Double_t fgkdvdE;        // [cm/V] drift velocity dependency on the E field (from Magboltz for NeCO2N2 at standard environment)
  static const Double_t fgkEM;          // charge/mass in [C/kg]
  static const Double_t fgke0;          // vacuum permittivity [A·s/(V·m)]


  enum {kNR=   72  };     // Number of R points in the table for interpolating distortion data
  enum {kNPhi= 18*10+1};  // Number of Phi points in the table for interpolating distortion data ( plus one extra for 360 == 0 ) 
  enum {kNZ=   166};      // Number of Z points in the table for interpolating distortion data

  Double_t fgkRList[kNR]; // points in the radial direction (for the lookup table)
  Double_t fgkPhiList[kNPhi]; // points in the phi direction (for the lookup table)
  Double_t fgkZList[kNZ]; // points in the z direction (for the lookup table)

  // Simple Interpolation functions: e.g. with tricubic interpolation (not yet in TH3)
  Int_t fILow, fJLow, fKLow;          // variable to help in the interpolation 
  // Double_t versions
  void Interpolate2DEdistortion( Int_t order, Double_t r, Double_t z, 
				 const Double_t er[kNZ][kNR], Double_t &erValue );
  void Interpolate3DEdistortion( Int_t order, Double_t r, Float_t phi, Double_t z, 
				 const Double_t er[kNZ][kNPhi][kNR], const Double_t ephi[kNZ][kNPhi][kNR], 
				 const Double_t ez[kNZ][kNPhi][kNR],
				 Double_t &erValue, Double_t &ephiValue, Double_t &ezValue);
  // TMatrixD versions (for e.g. Poisson relaxation)
  Double_t Interpolate2DTable( Int_t order, Double_t x, Double_t y, 
			      Int_t nx,  Int_t ny, const Double_t xv[], const Double_t yv[], 
			      const TMatrixD &array );
  Double_t Interpolate3DTable( Int_t order, Double_t x,   Double_t y,   Double_t z,
			      Int_t  nx,    Int_t  ny,    Int_t  nz,
			      const Double_t xv[], const Double_t yv[], const Double_t zv[],
			      TMatrixD **arrayofArrays );
  Double_t Interpolate( const Double_t xArray[], const Double_t yArray[], 
			Int_t order, Double_t x );
  void Search( Int_t n, const Double_t xArray[], Double_t x, Int_t &low );
 
  // TMatrixF versions (smaller size, e.g. for final look up table)
  Float_t Interpolate2DTable( Int_t order, Double_t x, Double_t y, 
			      Int_t nx,  Int_t ny, const Double_t xv[], const Double_t yv[], 
			      const TMatrixF &array );
  Float_t Interpolate3DTable( Int_t order, Double_t x,   Double_t y,   Double_t z,
			      Int_t  nx,    Int_t  ny,    Int_t  nz,
			      const Double_t xv[], const Double_t yv[], const Double_t zv[],
			       TMatrixF **arrayofArrays ); 
  Float_t Interpolate( const Double_t xArray[], const Float_t yArray[], 
			Int_t order, Double_t x );

  virtual Int_t IsPowerOfTwo ( Int_t i ) const  ;

  
 
  // Algorithms to solve the laplace or poisson equation 
  void PoissonRelaxation2D(TMatrixD &arrayV, TMatrixD &chargeDensity, 
			   TMatrixD &arrayErOverEz, TMatrixD &arrayDeltaEz,
			   Int_t rows, Int_t columns, Int_t iterations,
			   Bool_t rocDisplacement = kTRUE);

  void PoissonRelaxation3D( TMatrixD **arrayofArrayV, TMatrixD **arrayofChargeDensities, 
			    TMatrixD **arrayofEroverEz, TMatrixD **arrayofEPhioverEz, TMatrixD **arrayofEz,
			    Int_t rows, Int_t columns,  Int_t phislices, 
			    Float_t deltaphi, Int_t iterations, Int_t summetry,
			    Bool_t rocDisplacement = kTRUE); 
  void   SetIsLocal(Bool_t isLocal){fIsLocal=isLocal;}
  Bool_t IsLocal() const  { return fIsLocal;}
protected:
  Double_t fT1;         // tensor term of wt - T1
  Double_t fT2;         // tensor term of wt - T2
  Bool_t fIsLocal;      // switch to indicate that the distortion is a local vector drphi/dz, dr/dz
  static TObjArray *fgVisualCorrection;  // array of orrection for visualization
private:
  AliTPCCorrection(const AliTPCCorrection &);               // not implemented
  AliTPCCorrection &operator=(const AliTPCCorrection &);    // not implemented

  void InitLookUpfulcrums();   // to initialize the grid of the look up table

  ClassDef(AliTPCCorrection,5);
};

#endif
 AliTPCCorrection.h:1
 AliTPCCorrection.h:2
 AliTPCCorrection.h:3
 AliTPCCorrection.h:4
 AliTPCCorrection.h:5
 AliTPCCorrection.h:6
 AliTPCCorrection.h:7
 AliTPCCorrection.h:8
 AliTPCCorrection.h:9
 AliTPCCorrection.h:10
 AliTPCCorrection.h:11
 AliTPCCorrection.h:12
 AliTPCCorrection.h:13
 AliTPCCorrection.h:14
 AliTPCCorrection.h:15
 AliTPCCorrection.h:16
 AliTPCCorrection.h:17
 AliTPCCorrection.h:18
 AliTPCCorrection.h:19
 AliTPCCorrection.h:20
 AliTPCCorrection.h:21
 AliTPCCorrection.h:22
 AliTPCCorrection.h:23
 AliTPCCorrection.h:24
 AliTPCCorrection.h:25
 AliTPCCorrection.h:26
 AliTPCCorrection.h:27
 AliTPCCorrection.h:28
 AliTPCCorrection.h:29
 AliTPCCorrection.h:30
 AliTPCCorrection.h:31
 AliTPCCorrection.h:32
 AliTPCCorrection.h:33
 AliTPCCorrection.h:34
 AliTPCCorrection.h:35
 AliTPCCorrection.h:36
 AliTPCCorrection.h:37
 AliTPCCorrection.h:38
 AliTPCCorrection.h:39
 AliTPCCorrection.h:40
 AliTPCCorrection.h:41
 AliTPCCorrection.h:42
 AliTPCCorrection.h:43
 AliTPCCorrection.h:44
 AliTPCCorrection.h:45
 AliTPCCorrection.h:46
 AliTPCCorrection.h:47
 AliTPCCorrection.h:48
 AliTPCCorrection.h:49
 AliTPCCorrection.h:50
 AliTPCCorrection.h:51
 AliTPCCorrection.h:52
 AliTPCCorrection.h:53
 AliTPCCorrection.h:54
 AliTPCCorrection.h:55
 AliTPCCorrection.h:56
 AliTPCCorrection.h:57
 AliTPCCorrection.h:58
 AliTPCCorrection.h:59
 AliTPCCorrection.h:60
 AliTPCCorrection.h:61
 AliTPCCorrection.h:62
 AliTPCCorrection.h:63
 AliTPCCorrection.h:64
 AliTPCCorrection.h:65
 AliTPCCorrection.h:66
 AliTPCCorrection.h:67
 AliTPCCorrection.h:68
 AliTPCCorrection.h:69
 AliTPCCorrection.h:70
 AliTPCCorrection.h:71
 AliTPCCorrection.h:72
 AliTPCCorrection.h:73
 AliTPCCorrection.h:74
 AliTPCCorrection.h:75
 AliTPCCorrection.h:76
 AliTPCCorrection.h:77
 AliTPCCorrection.h:78
 AliTPCCorrection.h:79
 AliTPCCorrection.h:80
 AliTPCCorrection.h:81
 AliTPCCorrection.h:82
 AliTPCCorrection.h:83
 AliTPCCorrection.h:84
 AliTPCCorrection.h:85
 AliTPCCorrection.h:86
 AliTPCCorrection.h:87
 AliTPCCorrection.h:88
 AliTPCCorrection.h:89
 AliTPCCorrection.h:90
 AliTPCCorrection.h:91
 AliTPCCorrection.h:92
 AliTPCCorrection.h:93
 AliTPCCorrection.h:94
 AliTPCCorrection.h:95
 AliTPCCorrection.h:96
 AliTPCCorrection.h:97
 AliTPCCorrection.h:98
 AliTPCCorrection.h:99
 AliTPCCorrection.h:100
 AliTPCCorrection.h:101
 AliTPCCorrection.h:102
 AliTPCCorrection.h:103
 AliTPCCorrection.h:104
 AliTPCCorrection.h:105
 AliTPCCorrection.h:106
 AliTPCCorrection.h:107
 AliTPCCorrection.h:108
 AliTPCCorrection.h:109
 AliTPCCorrection.h:110
 AliTPCCorrection.h:111
 AliTPCCorrection.h:112
 AliTPCCorrection.h:113
 AliTPCCorrection.h:114
 AliTPCCorrection.h:115
 AliTPCCorrection.h:116
 AliTPCCorrection.h:117
 AliTPCCorrection.h:118
 AliTPCCorrection.h:119
 AliTPCCorrection.h:120
 AliTPCCorrection.h:121
 AliTPCCorrection.h:122
 AliTPCCorrection.h:123
 AliTPCCorrection.h:124
 AliTPCCorrection.h:125
 AliTPCCorrection.h:126
 AliTPCCorrection.h:127
 AliTPCCorrection.h:128
 AliTPCCorrection.h:129
 AliTPCCorrection.h:130
 AliTPCCorrection.h:131
 AliTPCCorrection.h:132
 AliTPCCorrection.h:133
 AliTPCCorrection.h:134
 AliTPCCorrection.h:135
 AliTPCCorrection.h:136
 AliTPCCorrection.h:137
 AliTPCCorrection.h:138
 AliTPCCorrection.h:139
 AliTPCCorrection.h:140
 AliTPCCorrection.h:141
 AliTPCCorrection.h:142
 AliTPCCorrection.h:143
 AliTPCCorrection.h:144
 AliTPCCorrection.h:145
 AliTPCCorrection.h:146
 AliTPCCorrection.h:147
 AliTPCCorrection.h:148
 AliTPCCorrection.h:149
 AliTPCCorrection.h:150
 AliTPCCorrection.h:151
 AliTPCCorrection.h:152
 AliTPCCorrection.h:153
 AliTPCCorrection.h:154
 AliTPCCorrection.h:155
 AliTPCCorrection.h:156
 AliTPCCorrection.h:157
 AliTPCCorrection.h:158
 AliTPCCorrection.h:159
 AliTPCCorrection.h:160
 AliTPCCorrection.h:161
 AliTPCCorrection.h:162
 AliTPCCorrection.h:163
 AliTPCCorrection.h:164
 AliTPCCorrection.h:165
 AliTPCCorrection.h:166
 AliTPCCorrection.h:167
 AliTPCCorrection.h:168
 AliTPCCorrection.h:169
 AliTPCCorrection.h:170
 AliTPCCorrection.h:171
 AliTPCCorrection.h:172
 AliTPCCorrection.h:173
 AliTPCCorrection.h:174
 AliTPCCorrection.h:175
 AliTPCCorrection.h:176
 AliTPCCorrection.h:177
 AliTPCCorrection.h:178
 AliTPCCorrection.h:179
 AliTPCCorrection.h:180
 AliTPCCorrection.h:181
 AliTPCCorrection.h:182
 AliTPCCorrection.h:183
 AliTPCCorrection.h:184
 AliTPCCorrection.h:185
 AliTPCCorrection.h:186
 AliTPCCorrection.h:187
 AliTPCCorrection.h:188
 AliTPCCorrection.h:189
 AliTPCCorrection.h:190