StarDriftV     =  1e-6*thedb->DriftVelocity() ;        
  fOmegaTau  =  StDetectorDbTpcOmegaTau::instance();
  TensorV1   =  fOmegaTau->getOmegaTauTensorV1();
  TensorV2   =  fOmegaTau->getOmegaTauTensorV2();

  StarMagE   =  TMath::Abs((CathodeV-GG)/TPC_Z0) ;         // STAR Electric Field (V/cm) Magnitude
  OmegaTau   =  -10.0 * B[2] * StarDriftV / StarMagE ;     // B in kGauss, note the sign of B is important 

  Const_0    =  1. / ( 1. +  TensorV2*TensorV2*OmegaTau*OmegaTau ) ;
  Const_1    =  TensorV1*OmegaTau / ( 1. + TensorV1*TensorV1*OmegaTau*OmegaTau ) ;
  Const_2    =  TensorV2*TensorV2*OmegaTau*OmegaTau / ( 1. + TensorV2*TensorV2*OmegaTau*OmegaTau ) ;


UndoDistortion 
________________________________________________________________________________
	       FastUndoBDistortion
________________________________________________________________________________
/// 2D - faster - B field distortions (Table) - calculate the distortions due to the shape of the B field
/*! 
    Distortions are calculated and then stored in a table.  This calculation uses a 2D
    magnetic field which is phi symmetric.  The real 3D field has a slight twist that
    may be important for high precision work.  I recommend using this faster version (JT) 
    if you don't care about distortions smaller than 200 microns. 
    This method requires about 10 seconds of CPU time to generate the table but it is 
    very fast after the table has been created. Use it when you have a large number 
    of points ( > 10,000 ).
*/
________________________________________________________________________________
			UndoBDistortion	
________________________________________________________________________________
/// B field distortions in 3D ( no Table ) - calculate the distortions due to the shape of the B field
/*! 
    Distortions are calculated point by point and integrated in real time.
    This avoids the time required to set up a table of distorted values but
    is slow for a very large number of points ( > 10,000 ).
*/
	  Xprime[0] +=  index*(ah/3)*( Const_2*B[0] - Const_1*B[1] ) / B[2] ;
	  Xprime[1] +=  index*(ah/3)*( Const_2*B[1] + Const_1*B[0] ) / B[2] ;

________________________________________________________________________________
	       FastUndo2DBDistortion
			Undo2DBDistortion
________________________________________________________________________________
/*! 
    Distortions are calculated point by point and integrated in real time.
    This avoids the time required to set up a table of distorted values but
    is slow for a very large number of points ( > 10,000 ).
*/
	  Xprime[0] +=  index*(ah/3)*( Const_2*B[0] - Const_1*B[1] ) / B[2] ;
	  Xprime[1] +=  index*(ah/3)*( Const_2*B[1] + Const_1*B[0] ) / B[2] ;
________________________________________________________________________________
	       UndoPad13Distortion
________________________________________________________________________________
/// Pad row 13 distortion
/*!
    Remove the effect of the mechanical imperfections between the inner sectors
    and the outer sectors.  There is a gap between the sectors that allow E field
    lines to leak out of the anode and gated grid region.  HHWieman has modelled this
    effect and his solution is used to remove the distortions.
 */
      phi =  phi - ( Const_1*(-1*sum)*TMath::Cos(phi0-phi) + Const_0*sum*TMath::Sin(phi0-phi) ) / r ;      
      r   =  r   - ( Const_0*sum*TMath::Cos(phi0-phi) - Const_1*(-1*sum)*TMath::Sin(phi0-phi) ) ;  
________________________________________________________________________________

	       UndoTwistDistortion
________________________________________________________________________________
/// Twist distortion
/*!
    Remove the effects of a simple "twist" of the TPC in the magnet.  If there is
    an angle between the E and B fields, there will be a distortion in the recorded
    tracks.  This routine takes out that distortion.
 */
mysql> select beginTime,flavor,deactive,LocalxShift,LocalyShift,LocalzShift,PhiXY,PhiXZ,PhiYZ,PhiXY_geom,PhiXZ_geom,PhiYZ_geom from tpcGlobalPosition where beginTime='2004-11-29 00:00:00' order by beginTime;
+---------------------+------------------+----------+-------------+-------------+-------------+------------+-------------+-------------+------------+-------------+-------------+ 
| beginTime           | flavor           | deactive | LocalxShift | LocalyShift | LocalzShift | PhiXY      | PhiXZ       | PhiYZ       | PhiXY_geom | PhiXZ_geom  | PhiYZ_geom  | gamma
+---------------------+------------------+----------+-------------+-------------+-------------+------------+-------------+-------------+------------+-------------+-------------+
| 2004-11-29 00:00:00 | FullMagFNegative |        0 | -0.22200000 | -0.14300001 | -0.20900001 | 0.00000000 | -0.00041200 | -0.00032600 | 0.00004000 | -0.00044100 | -0.00013200 | -2
| 2004-11-29 00:00:00 | FullMagFPositive |        0 | -0.22200000 | -0.14300001 | -0.20900001 | 0.00000000 | -0.00029000 | -0.00008500 | 0.00004000 | -0.00044100 | -0.00013200 | +1
| 2004-11-29 00:00:00 | ZeroMagF         |        0 | -0.22200000 | -0.14300001 | -0.20900001 | 0.00000000 | -0.00035100 | -0.00020500 | 0.00004000 | -0.00044100 | -0.00013200 | +3
+---------------------+------------------+----------+-------------+-------------+-------------+------------+-------------+-------------+------------+-------------+-------------+
                                                                                                             ^^^^^^^^^^    ^^^^^^^^^^^   

      XTWIST      =   -0.165 ;      // X Displacement of West end of TPC wrt magnet (mRad)
      YTWIST      =    0.219 ;      // Y Displacement of West end of TPC wrt magnet (mRad)
  XTWIST         =  1e3*thedb->GlobalPosition()->TpcEFieldRotationY() ;  // PhiXZ
  YTWIST         =  -1e3*thedb->GlobalPosition()->TpcEFieldRotationX() ; // PhiYZ         
  fOmegaTau  =  StDetectorDbTpcOmegaTau::instance();
  TensorV1   =  fOmegaTau->getOmegaTauTensorV1();
  TensorV2   =  fOmegaTau->getOmegaTauTensorV2();
      TensorV1    =  1.35 ;  // Drift velocity tensor term: in the ExB direction
      TensorV2    =  1.10 ;  // Drift velocity tensor term: direction perpendicular to Z and ExB
  StarMagE   =  TMath::Abs((CathodeV-GG)/TPC_Z0) ;         // STAR Electric Field (V/cm) Magnitude
  OmegaTau   =  -10.0 * B[2] * StarDriftV / StarMagE ;     // B in kGauss, note the sign of B is important 
  Const_0    =  1. / ( 1. +  TensorV2*TensorV2*OmegaTau*OmegaTau ) ;
  Const_1    =  TensorV1*OmegaTau / ( 1. + TensorV1*TensorV1*OmegaTau*OmegaTau ) ;
  Const_2    =  TensorV2*TensorV2*OmegaTau*OmegaTau / ( 1. + TensorV2*TensorV2*OmegaTau*OmegaTau ) ;
  if ( x[2] >= 0.0 ) sign =  1 ;                       // (TPC West)
  else               sign = -1 ;                       // (TPC East)  

  Zdrift = sign * ( TPC_Z0 - TMath::Abs(x[2]) ) ;
  Xprime[0] = x[0] - (     Const_1 * YTWIST - Const_2 * XTWIST ) * Zdrift/1000 ;
  Xprime[1] = x[1] - ( -1* Const_1 * XTWIST - Const_2 * YTWIST ) * Zdrift/1000 ;
  Xprime[2] = x[2] ;                                   // Subtract to undo the distortion 
________________________________________________________________________________
			
	       UndoClockDistortion
________________________________________________________________________________
/// Clock distortion
/*!
    The East endwheel of the TPC and the West endwheel of the TPC are not perfectly aligned.
    They were inserted separately into the TPC field cage tube.  They are aligned at the outer
    diameter (4 meters) to within about 1 mm.  This causes a slight misalingment of the relative
    coordinate systems.  By convention, we assume that one end is perfect and attribute all of 
    the error to a rotation of the other end ... however, the method (and the DB) allow you to
    input a rotation angle for each end, if you wish.  Note: this is a coordinate transformation
    and not a distortion correction.  It is here for historical reasons.
 */
      EASTCLOCKERROR =   0.0 ;      // Phi rotation of East end of TPC in milli-radians
      WESTCLOCKERROR = -0.43 ;      // Phi rotation of West end of TPC in milli-radians
  EASTCLOCKERROR =  1e3*thedb->FieldCage()->EastClockError();
  WESTCLOCKERROR =  1e3*thedb->FieldCage()->WestClockError();
  r      =  TMath::Sqrt( x[0]*x[0] + x[1]*x[1] ) ;
  phi    =  TMath::ATan2(x[1],x[0]) ;

  if ( x[2] < 0 )  phi += EASTCLOCKERROR/1000. ;    // Phi rotation error in milli-radians
  if ( x[2] >= 0 ) phi += WESTCLOCKERROR/1000. ;    // Phi rotation error in milli-radians

  Xprime[0] = r * TMath::Cos(phi) ;
  Xprime[1] = r * TMath::Sin(phi) ;
  Xprime[2] = x[2] ;
________________________________________________________________________________
	       UndoMembraneDistortion
________________________________________________________________________________
      phi =  phi - ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
      r   =  r   - ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________
	       UndoEndcapDistortion
________________________________________________________________________________
      phi =  phi - ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
      r   =  r   - ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________
	       UndoIFCShiftDistortion
________________________________________________________________________________
/*! 
    The Inner field cage of the TPC is not perfectly aligned with the outer field cage 
    of the TPC.  They are shifted along the Z axis by about 1 mm.  This causes a tilting 
    of the equi-potential lines inside the TPC and therefore a DCA error at the vertex.  
    The distortion is anti- symmetric in Z. 
    Electrostatic equations solved in Rectangular Coodinates by Jim Thomas
    Updated to work in cylindrical coordinates by Jamie Dunlop  11/01/2001
*/
________________________________________________________________________________
	       UndoSpaceChargeDistortion
________________________________________________________________________________
     phi =  phi - SpaceCharge * ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
     r   =  r   - SpaceCharge * ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________
	       UndoSpaceChargeR2Distortion
________________________________________________________________________________
/// 1/R**2 SpaceCharge Distortion
/*!
  Space Charge distortion using space charge from a real event.  Any charge distribution can 
  be simulated by this method.  However, the best charge distribution is Howard's fit to 
  HiJet events.  It is approximately independent of Z due to the Bjorken Plateau a mid-rapidity.  The 
  radial distribution is approximately 1/R**2, however we use a better parameterization in the code.
  Many different charge distributions are hidden in the comments of the code.  All candidate distributions
  have been integrated over Z to simulate the linear increase of space charge in Z due to the slow 
  drift velocity of the ions.  Electrostatic equations solved by relaxtion.  
  Original work by H. H. Wieman, N. Smirnov, and J. Thomas 
*/
      phi =  phi - SpaceChargeR2 * ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
      r   =  r   - SpaceChargeR2 * ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________
	       UndoShortedRingDistortion
________________________________________________________________________________
/// Shorted Ring Distortion
/*!
    This code assumes that information about shorted rings in the TPC field cage will come from the DB.  
    The DB returns rows from a table and the columns have the following meaning:
  
    Side(E=0/W=1)  Cage(IFC=0/OFC=1)  Ring(# from CM)  MissingResistance(MOhm)  ExtraResistance(MOhm)
    
    0  0  169.5  2.0  2.0
    0  0  115.5  2.0  0.0
    1  1  150.5  1.0  1.0
    0  0    0.0  0.0  0.0
    etc.

    The table indicates that there are two shorted rings on the IFC of the East end of the TPC.  
    One of these shorts has a 2.0 MegOhm compensating resistor installed in the external resistor chain.
    There is also a short on the OFC of the West end of the TPC.  It is a non-standard short of 1 MOhm. 
    It has an external compensating resistor of 1.0 MOhm.  The row of zeros indicates that there are 
    no more shorts in the TPC. Counting of the rings starts at the Central Membrane; the CM is ring zero, 
    and 150.5 means the short is between rings 150 and 151.

    Electrostatic Equations from SN0253 by Howard Wieman.
    Note that we use Howard's funny coordinate system where Z==0 at the GG.
*/
      phi =  phi - ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
      r   =  r   - ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________
	       UndoGridLeakDistortion
/// Grid Leakage Calculation
/*!
  Calculate the distortions due to charge leaking out of the gap between the inner and outer sectors
  as well as the gap between the IFC and the innersector, as well as outersector and OFC.
  Original work by Gene VanBuren, and J. Thomas 
*/
      phi =  phi - SpaceChargeR2 * ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
      r   =  r   - SpaceChargeR2 * ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________
	       Undo3DGridLeakDistortion
/// 3D GridLeak Distortion Calculation
/*!
  Calculate the 3D distortions due to charge leaking out of the gap between the inner and outer sectors.
  Original work by Gene VanBuren, and J. Thomas 
*/
      phi =  phi - SpaceChargeR2 * ( Const_0*Ephi_integral - Const_1*Er_integral ) / r ;      
      r   =  r   - SpaceChargeR2 * ( Const_0*Er_integral   + Const_1*Ephi_integral ) ;  
________________________________________________________________________________