SUBROUTINE FFHUNT
*-- Author : S.Burke
      SUBROUTINE FFHUNT(JDIGP,JDIGR,JMAX)
**********************************************************************                                        
*                                                                    *                                        
* Hunt for the best candidate for the 'true' track, given a list     *                                        
* of digis.                                                          *                                        
*                                                                    *                                        
* JDIGP and JDIGR are pointers to the first digis on the track.      *                                        
* JMAX is returned as the STR index of the 'true' track.             *                                        
*                                                                    *                                        
* Note that the FRPX, FRRX, FPUX and FRUX banks are assumed to have  *                                        
* been copied to work banks, with pointers in /FFWBI/.               *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      DIMENSION ITRUE(2,1000)                                           
                                                                        
*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,FFWBI.                                                                                                  
* Work bank indices (note that INDKTR is *NOT* a work bank index!)                                            
      PARAMETER (NWBI=10)                                               
      COMMON /FFWBI/ INDRSX(2),INDX(2),INDKX(2),INDPUR                  
     &,              INDKTR,INDKTX,INDTPR                               
*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,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))                           
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      JMAX = 0                                                          
                                                                        
* Allow for hit banks to be nonexistant                                                                       
      IF (JDIGP.GT.0 .AND. INDRSX(1).LE.0) RETURN                       
      IF (JDIGR.GT.0 .AND. INDRSX(2).LE.0) RETURN                       
                                                                        
      CALL VZERO(ITRUE,2000)                                                                           
                                                                        
      IF (JDIGP.GT.0) THEN                                              
* Loop over planar digis looking for a corresponding hit ...                                                  
         NHIT = IW(INDRSX(1)+2)                                         
         JDIG = JDIGP                                                   
 100     CONTINUE                                                       
            DO 200 JHIT=1,NHIT                                          
               IDIG = IBTAB(INDRSX(1),3,JHIT)                           
               IF (IDIG.EQ.JDIG) CALL FFNUM(1,JHIT,ITRUE)
 200        CONTINUE                                                    
            JDIG = IBTAB(INDX(1),1,JDIG)                                
         IF (JDIG.NE.JDIGP) GOTO 100                                    
      ENDIF                                                             
                                                                        
      IF (JDIGR.GT.0) THEN                                              
* ... and the same for radials                                                                                
         NHIT = IW(INDRSX(2)+2)                                         
         JDIG = JDIGR                                                   
 300     CONTINUE                                                       
            DO 400 JHIT=1,NHIT                                          
               IDIG = IBTAB(INDRSX(2),3,JHIT)                           
               IF (IDIG.EQ.JDIG) CALL FFNUM(2,JHIT,ITRUE)
 400        CONTINUE                                                    
            JDIG = IBTAB(INDX(2),1,JDIG)                                
         IF (JDIG.NE.JDIGR) GOTO 300                                    
      ENDIF                                                             
                                                                        
      MAX  = 0                                                          
      JSTR = 0                                                          
 500  CONTINUE                                                          
         JSTR = JSTR + 1                                                
* ITRUE now contains the number of hits for each track ...                                                    
         IF (ITRUE(2,JSTR).GT.MAX) THEN                                 
            MAX  = ITRUE(2,JSTR)                                        
            JMAX = ITRUE(1,JSTR)                                        
         ENDIF                                                          
      IF (ITRUE(1,JSTR).GT.0 .AND. JSTR.LT.1000) GOTO 500               
                                                                        
      RETURN                                                            
      END                                                               
*