SUBROUTINE FFPHNT
*-- Author : Stephen Burke
      SUBROUTINE FFPHNT(ICELL,DRIFT,JMIN)
*-----------------------------------------Updates 07/09/93-------                                             
**: FFPHNT 40000 SB. New definition of dead wire flag.                                                        
*-----------------------------------------Updates 27/07/93-------                                             
**: FFPHNT 30907 RP. Farm changes.                                                                            
*-----------------------------------------Updates 06/08/92-------                                             
**: FFPHNT 30907 SB. Add check on bad hit flag.                                                               
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Look for a planar digi near a specified point                      *                                        
*                                                                    *                                        
* NB The FRPE bank must have been unpacked into FPLC and FPHC, with  *                                        
* a pointer to FPLC in INDLC(1) in /FFGEO/                           *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
*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,FFGEO.                                                                                                  
      COMMON /FFGEO/ INDG1(2),INDLC(2),IDIGI(NPL)                       
*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,CNSTBF.                                                                                                 
      INTEGER   LW(NBOSIW)                                              
      REAL      SW(NBOSIW)                                              
      EQUIVALENCE (RW(1),IW(1),LW(1),SW(1))                             
*KEEP,STFUNCT.                                                                                                
*     index of element before row number IROW                                                                 
      INDR(IND,IROW)=IND+2+IW(IND+1)*(IROW-1)                           
*     index of L'th element  of row number IROW                                                               
      INDCR(IND,L,IROW)=INDR(IND,IROW) + L                              
*     L'th integer element of the IROW'th row of bank with index IND                                          
      IBTAB(IND,L,IROW)=IW(INDCR(IND,L,IROW))                           
*     L'th real element of the IROW'th row of bank with index IND                                             
      RBTAB(IND,L,IROW)=RW(INDCR(IND,L,IROW))                           
*KEEP,STFCLW.                                                                                                 
* statement functions acting on the BOS COMMON LW                                                             
*     index of element before row number LWROW                                                                
      LNDR(LND,LWROW) = LND + 2 + LW(LND+1)*(LWROW-1)                   
*     index of L-th element of row number LWROW                                                               
      LNDCR(LND,L,LWROW)=LNDR(LND,LWROW)+L                              
*     L-th integer element of the LWROW'th row in bank with index LND                                         
      LBTAB(LND,L,LWROW)=LW(LNDCR(LND,L,LWROW))                         
*     L-th real element of the LWROW'th row in bank with index LND                                            
      SBTAB(LND,L,LWROW)=SW(LNDCR(LND,L,LWROW))                         
*                                                                                                             
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      JMIN = 0                                                          
                                                                        
      INDHC = NLINK('FPHC',0)
      IF (INDHC.LE.0 .OR. INDG1(1).LE.0 .OR. INDLC(1).LE.0) RETURN      
                                                                        
* Check for dead wire                                                                                         
      IF (LBTAB(INDG1(1),1,ICELL+1).EQ.1) RETURN                        
                                                                        
      NHIT = IBTAB(INDHC,1,ICELL+1)                                     
      J1   = IBTAB(INDHC,2,ICELL+1)                                     
                                                                        
*                                                                                                             
* Find the hit with the minimum difference between its drift and                                              
* the target drift. Since the resolution is 150 um and the two-track                                          
* resolution is 2 mm there is never any ambiguity.                                                            
*                                                                                                             
                                                                        
      DDMIN = 1.E10                                                     
      DO 100 JDIG=J1,J1+NHIT-1                                          
         DD  = RBTAB(INDLC(1),2,JDIG)                                   
         DDD = ABS(ABS(DD) - ABS(DRIFT))                                
         IF (DDD.LT.DDMIN) THEN                                         
            DDMIN = DDD                                                 
            JMIN  = JDIG                                                
         ENDIF                                                          
 100  CONTINUE                                                          
                                                                        
* Check the bad hit flag (if it exists)                                                                       
      IF (IW(INDLC(1)+1).LT.4) RETURN                                   
      IBAD = IBTAB(INDLC(1),4,JMIN)                                     
      IF (JBIT(IBAD,8).EQ.1) JMIN = 0                                   
                                                                        
      RETURN                                                            
      END                                                               
*