FUNCTION FPLT2D
*-- Author :    J. V. Morris          17/04/93
      FUNCTION FPLT2D(TICK,SGN,IWCH)
**********************************************************************                                        
*                                                                    *                                        
* Planar Time-to-Distance model.                                     *                                        
* TICK is drift time in ticks with Tzero already subtracted          *                                        
* SGN is +1.0 for "near" wires -1.0 for "far" wires                  *                                        
* if SGN = 0.0, then then no near/far asymmetry adjustment is made   *                                        
* IWCH is the planar channel number (0 to 1151)                      *                                        
* FPLT2D   is the computed drift distance in cm.                     *                                        
*                                                                    *                                        
* The function uses microns and nsecs internally                     *                                        
* JVM 04/01/93                                                       *                                        
*                                                                    *                                        
* Include 6% velocity asymmetry beta                                 *                                        
* JVM 21/01/93                                                       *                                        
*                                                                    *                                        
* The velocity asymmetry is disabled until it can be tested!         *                                        
* Include different T-2-D for "inner" and "outer" wires.             *                                        
* JVM 08/04/93                                                       *                                        
*                                                                    *                                        
* Change interface to calibration constants.                         *                                        
* Add SGN = 0.0 option.                                              *                                        
* SJM 17/04/93                                                       *                                        
**********************************************************************                                        
                                                                        
                                                                        
*>>>>>>>>>>>>> interface to calibration constants                                                             
*     Common for time-to-distance relation                                                                    
      COMMON/T2DISP/ VEL(24), XPT(18)                                   
                                                                        
*SJM  VEL(1:4)   = Vout. VEL(5:8)   = Vin. far wires                                                          
*SJM  VEL(9:12)  = Vout. VEL(13:16) = Vin. symmetric case                                                     
*SJM  VEL(17:20) = Vout. VEL(21:24) = Vin. near wires                                                         
*SJM  XPT(1:3)   = Xout. XPT(4:6)   = Xin. far wires                                                          
*SJM  XPT(7:9)   = Xout. XPT(10:12) = Xin. symmetric case                                                     
*SJM  XPT(13:15) = Xout. XPT(16:18) = Xin. near wires                                                         
                                                                        
                                                                        
*JVM  VIN  = 44.93, 32.93, 33.12, 28.92        for run 34469                                                  
*JVM  VOUT = 46.55, 31.80, 32.99, 29.17        for run 34469                                                  
*JVM  X0IN = 5025., 16360., 28100.             for all runs                                                   
*JVM  X0OUT= 5025., 17080., 28100.             for all runs                                                   
*>>>>>>>>>>>>> end of calibration constants                                                                   
                                                                        
                                                                        
* The parameters V(1) to V(4) (local drift velocities in micron/nsec),                                        
* X(1) and X(2) have been determined by a fit to the drift time                                               
* distribution from run 34469 with the constraint that the resulting                                          
* drift distance distribution be flat. X(3) is the nominal Planar cell                                        
* depth in microns.                                                                                           
                                                                        
*     channel  outer, inner => IOFF = 0, 1                                                                    
      IWW = MOD(IWCH,4)                                                 
      IF( IWW.EQ.0 .OR. IWW.EQ.3 )THEN ! outer                          
         IOFF  = 0                                                      
      ELSE                             ! inner                          
         IOFF  = 1                                                      
      ENDIF                                                             
*     Far, symmetric, near => ISGN = -1,0,+1                                                                  
      IF(SGN .EQ. 0.0) THEN                                             
        ISGN = 0                                                        
      ELSE                                                              
        ISGN = INT(SIGN(1.1,SGN))                                       
      ENDIF                                                             
                                                                        
      IV = (ISGN + 1)*8 + IOFF*4                                        
      IX = (ISGN + 1)*6 + IOFF*3                                        
                                                                        
*     Write(6,'('' Vel'',4F10.6)')(VEL(IV+LL), LL = 1,4)                                                      
*     Write(6,'('' Xpt'',4F10.2)')(XPT(IX+LL), LL = 1,3)                                                      
                                                                        
*     T in nsecs                                                                                              
      T      = TICK * 0.1923077                                         
      FPLT2D = 0.0                                                      
                                                                        
                                                                        
      IF( T.LE.0.) THEN                                                 
         FPLT2D = VEL(IV+1)*T                                           
         GOTO 1000                                                      
      ENDIF                                                             
                                                                        
*     Drift time to X(1)                                                                                      
      TL1 = XPT(IX+1)*ALOG(VEL(IV+2)/VEL(IV+1))/(VEL(IV+2)-VEL(IV+1))   
                                                                        
      IF( T.LE.TL1 ) THEN                                               
         D = XPT(IX+1)                                                  
         DV = VEL(IV+2)-VEL(IV+1)                                       
         FPLT2D = D*VEL(IV+1)*( EXP(DV*T/D)-1.0 )/DV                    
         GOTO 1000                                                      
      ENDIF                                                             
                                                                        
      D = XPT(IX+2)-XPT(IX+1)                                           
      DV= VEL(IV+3)-VEL(IV+2)                                           
*     Drift time to X(2)                                                                                      
      TL2 = TL1 + D*ALOG(VEL(IV+3)/VEL(IV+2))/DV                        
                                                                        
      IF( T.LE.TL2 ) THEN                                               
         FPLT2D = XPT(IX+1)+D*VEL(IV+2)*( EXP(DV*(T-TL1)/D)-1.0 )/DV    
         GOTO 1000                                                      
      ENDIF                                                             
                                                                        
      D = XPT(IX+3)-XPT(IX+2)                                           
      DV= VEL(IV+4)-VEL(IV+3)                                           
*     Drift time to X(3)                                                                                      
      TL3 = TL2 + D*ALOG(VEL(IV+4)/VEL(IV+3))/DV                        
                                                                        
      IF( T.LE.TL3 ) THEN                                               
         FPLT2D = XPT(IX+2)+D*VEL(IV+3)*( EXP(DV*(T-TL2)/D)-1.0 )/DV    
         GOTO 1000                                                      
      ENDIF                                                             
                                                                        
      FPLT2D = XPT(IX+3) + VEL(IV+4)*(T-TL3)                            
                                                                        
*     Microns to cm                                                                                           
 1000 FPLT2D = FPLT2D * 1.0E-4                                          
                                                                        
      RETURN                                                            
      END                                                               
*