SUBROUTINE FFPCEL
*-- Author : Stephen Burke
      SUBROUTINE FFPCEL(XY,JFT,ICELL,DRIFT)
*-----------------------------------------Updates 13/03/92-------                                             
**: FFPCEL 30205.SB. Only call UGTBNK when run number changes                                                 
*-----------------------------------------Updates 13/02/92-------                                             
**: FFPCEL 30205.SB. ERRLOG error numbers changed.                                                            
*-----------------------------------------Updates 07/02/92-------                                             
**: FFPCEL 30205.SB. Remove unused FKMEAS sequence.                                                           
*-----------------------------------------Updates 24/01/92-------                                             
**: FFPCEL 30205.SB. ERRLOG message format changed.                                                           
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Calculate a planar cell number from a position                     *                                        
*                                                                    *                                        
* Returns ICELL = -1 if JFT is invalid                               *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      DOUBLE PRECISION XY(2),PHI                                        
      DIMENSION FGAP(75)                                                
      SAVE FGAP,IRUN                                                    
                                                                        
*KEEP,BCS.                                                                                                    
      INTEGER      NHROW,NHCOL,NHLEN                                    
      PARAMETER   (NHROW = 2, NHCOL = 1, NHLEN=2)                       
      INTEGER      NBOSIW                                               
      PARAMETER   (NBOSIW=1000000)                                      
      INTEGER      IW(NBOSIW)                                           
      REAL         RW(NBOSIW)                                           
      COMMON /BCS/ IW                                                   
      EQUIVALENCE (RW(1),IW(1))                                         
      SAVE   /BCS/                                                      
*KEEP,BOSMDL.                                                                                                 
C     ------BOSMDL                                                                                            
      LOGICAL       BEGJOB,ENDRUN,BEGRUN,REVENT,ENDJOB,OTHDAT           
      COMMON/BOSMDL/BEGJOB,ENDRUN,BEGRUN,REVENT,ENDJOB,OTHDAT,          
     +              LCCRUN,NCCRUN,NEVENT,                               
     +              IHA,IBS,IDB,IDATEL,LUP,ISN,JSN                      
      SAVE  /BOSMDL/                                                    
C     ------                                                                                                  
*KEEP,FKNPL.                                                                                                  
      CHARACTER*5 CKDBG                                                 
      PARAMETER (CKDBG='FKDBG')                                         
      PARAMETER (NPL=72)                                                
      LOGICAL LTRUE,LFIRST,LTRPL,LTRPLD                                 
      DOUBLE PRECISION TRUE,RTRUE,CHITRU,SPRO,CPRO,SFIL,CFIL            
     &,                SSMT,CSMT,SSMTR,CSMTR,DPRO,CBPRO,QPRO,QGAIN      
     &,                RPRO,CRPRO,RFIL,CRFIL,RSMT,CRSMT,CHIFIL,CHISMT   
*                                                                                                             
* Per-track values can go in H1WORK; note that LTRUE and LFIRST must                                          
* be set at least per event.                                                                                  
*                                                                                                             
* This is about 36k words long; the remaining common blocks are                                               
* about 3.6k in total. Some of this could be in /H1WORK/, but the                                             
* blocks would have to be reorganised.                                                                        
*                                                                                                             
      COMMON /H1WORK/                                                   
* /FKPROJ/                                                                                                    
     &                SPRO(5,NPL),CPRO(5,5,NPL)                         
* /FKFILT/                                                                                                    
     &,               SFIL(5,NPL),CFIL(5,5,NPL)                         
* /FKSMTH/                                                                                                    
     &,               SSMT(5,NPL),CSMT(5,5,NPL)                         
     &,               SSMTR(5,NPL),CSMTR(5,5,NPL)                       
* /FKINT/                                                                                                     
     &,               DPRO(5,5,NPL),CBPRO(5,5,NPL),QPRO(5,5,NPL)        
     &,               QGAIN(5,5,NPL),IAPROX,LFIRST                      
* /FKRSID/                                                                                                    
     &,               RPRO(2,NPL),CRPRO(2,2,NPL),RFIL(2,NPL)            
     &,               CRFIL(2,2,NPL),RSMT(2,NPL),CRSMT(2,2,NPL)         
     &,               CHIFIL(NPL),CHISMT(NPL)                           
* /FKTRUE/                                                                                                    
     &,               TRUE(5,NPL),RTRUE(5,NPL),CHITRU(NPL),LTRUE        
* /FKDBG/                                                                                                     
     &,               LTRPL(NPL),LTRPLD(NPL)                            
*KEEP,FFSTEE.                                                                                                 
      PARAMETER (NFT=72)                                                
      LOGICAL LRISV,LWMAP,LGRAPH,LTRUTH                                 
      REAL DSX,DSY,DSQOP,DSTTH,DSPHI,PMCUT,PCUT,CHPCUT                  
     &,    QOPMAX,THEMAX,RFTMIN,RFTMAX,CEMAX                            
      COMMON /FFSTEE/ DSX,DSY,DSQOP,DSTTH,DSPHI                         
     &,               PMCUT,IDIAG,JPLRSV,LRISV,LWMAP(NFT),JPLMAX        
     &,               IRP(NPL),JPLFT(NPL),JFTPL(NFT)                    
     &,               LUNGKS,IWKGKS,IDGKS,LUNHB,LUNGKM,IWKGKM,IDGKM     
     &,               LGRAPH,LTRUTH,IHFF,IHFK,ISRJCT,PCUT,CHPCUT        
     &,               QOPMAX,THEMAX,RFTMIN,RFTMAX,CEMAX                 
*KEEP,FKPIDP.                                                                                                 
      DOUBLE PRECISION PI,TWOPI,PIBY2                                   
      PARAMETER (PI=3.141592653589793238)                               
      PARAMETER (TWOPI=PI*2.0D0,PIBY2=PI/2.0D0)                         
*KEND.                                                                                                        
                                                                        
      DATA IRUN/-999999/                                                
                                                                        
**********************************************************************                                        
                                                                        
      ICELL = -1                                                        
                                                                        
      IF (IRUN.NE.NCCRUN) THEN                                          
* Get geometry bank                                                                                           
         CALL UGTBNK('FGAP',INFGAP)
         IF (INFGAP.LE.0) THEN                                          
            CALL ERRLOG(341,'S:FFPCEL: Bank FGAP not found')                                           
            RETURN                                                      
         ENDIF                                                          
         IRUN = NCCRUN                                                  
         CALL UCOPY(RW(INFGAP+1),FGAP,75)                                                              
      ENDIF                                                             
                                                                        
      IF (JFT.GE.1 .AND. JFT.LE.12) THEN                                
         KMOD = 0                                                       
      ELSEIF (JFT.GE.25 .AND. JFT.LE.36) THEN                           
         KMOD = 1                                                       
      ELSEIF (JFT.GE.49 .AND. JFT.LE.60) THEN                           
         KMOD = 2                                                       
      ELSE                                                              
         RETURN                                                         
      ENDIF                                                             
                                                                        
* Orientation and wire number                                                                                 
      KORI  = MOD(JFT-1,12)/4                                           
      KWIRE = MOD(JFT-1,4)                                              
                                                                        
* NB The format of FGAP is hard-coded here                                                                    
      W0     = FGAP(7)                                                  
      DW     = FGAP(8)                                                  
      INDMOD = 31 + 5*(3*KMOD + KORI)                                   
      PHI    = FGAP(INDMOD+3) - PIBY2                                   
      CPHI   = DCOS(PHI)                                                
      SPHI   = DSIN(PHI)                                                
      STAGG  = FGAP(INDMOD+4)                                           
                                                                        
* Cell number and drift (NB check stagger!)                                                                   
      W     = CPHI*XY(2) - SPHI*XY(1)                                   
      WCELL = W - W0 + DW/2. - STAGG*(-1)**KWIRE                        
      KCELL = WCELL/DW                                                  
      IF (KCELL.LT.0 .OR. KCELL.GT.25) RETURN                           
      DRIFT = WCELL - KCELL*DW - DW/2.                                  
                                                                        
* Adjust cell number for split cells                                                                          
      IF (KCELL.GE.10 .AND. KCELL.LE.15) THEN                           
         R = CPHI*XY(1) + SPHI*XY(2)                                    
         IF (R.LT.0.) KCELL = KCELL + 6                                 
      ELSEIF (KCELL.GT.15) THEN                                         
         KCELL = KCELL + 6                                              
      ENDIF                                                             
                                                                        
* Cell number                                                                                                 
      ICELL = 384*KMOD + 128*KORI + 4*KCELL + KWIRE                     
                                                                        
      RETURN                                                            
      END                                                               
*