SUBROUTINE FFCHEK
*-- Author : Stephen Burke
      SUBROUTINE FFCHEK(JDIGP,JDIGR,JMAX)
*-----------------------------------------Updates 07/09/93-------                                             
**: FFCHEK 40000 SB. New definition of dead wire flag.                                                        
*-----------------------------------------Updates 03/05/93-------                                             
**: FFCHEK 40000 SB. Allow for hits with no digi.                                                             
**: FFCHEK 40000 SB. Fix bug in efficiency histograms                                                         
*-----------------------------------------Updates 28/01/92-------                                             
**: FFCHEK 30205.SB. Fix bug in efficiency histograms                                                         
*-----------------------------------------Updates 24/01/92-------                                             
**: FFCHEK 30205.SB. Check to see if the drift sign is correct.                                               
**: FFCHEK 30205.SB. Add a check for dead wires                                                               
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Calculate and histogram the efficiency and inefficiency of the     *                                        
* FT pattern recognition, for planars, radials and both together     *                                        
*                                                                    *                                        
* NB Histograms are booked in FFHBK                                  *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      LOGICAL IOK,IOKD                                                  
      DIMENSION MODHIT(12),MODST(12)                                    
      SAVE MODST                                                        
                                                                        
*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,FFGEO.                                                                                                  
      COMMON /FFGEO/ INDG1(2),INDLC(2),IDIGI(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,FKDBG.                                                                                                  
*KEEP,FFDBG.                                                                                                  
      CHARACTER*5 CFDBG                                                 
      CHARACTER*6 CFKDBG                                                
      PARAMETER (CFDBG='FFDBG',CFKDBG='FFKDBG')                         
      PARAMETER (NTRACK=1000)                                           
      COMMON /FFDBG/ ITRTR(2,NTRACK),ITRNF(5,NTRACK),JTRTR              
*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))                           
*KEEP,FTFUNCT.                                                                                                
*     Statement functions for RADIAL Chamber data access.                                                     
*     Using Channel Number J                                                                                  
                                                                        
*     Module, Wedge-pair and Z-plane numbers...                                                               
      IRMOD(J) = J/288                                                  
      IRWDP(J) = (J-IRMOD(J)*288)/12                                    
      IRZPL(J) =  J-IRMOD(J)*288-IRWDP(J)*12                            
*     Statement function for obtaining WEDGE numbers(0-47) of                                                 
*     wires at plus and minus ends of Cell numbers                                                            
      IRWPL(J) = (IRWDP(J)/2)*4 + (MOD(IRWDP(J),2))                     
      IRWMI(J) = MOD(IRWPL(J) + 34,48)                                  
*     Statement function for obtaining IOS wire number (1-36)                                                 
      IRIOSW(J) = IRMOD(J)*12 + IRZPL(J) + 1                            
                                                                        
*     Statement functions for PLANAR Chamber data access.                                                     
*     Using Channel Number J                                                                                  
                                                                        
*     Module, orientation, W-cell and Z-plane numbers...                                                      
      IPMOD(J)  = J/384                                                 
      IPORI(J)  = (J-IPMOD(J)*384)/128                                  
      IPWCL(J)  = (J-IPMOD(J)*384-IPORI(J)*128)/4                       
      IPZPL(J)  = (J-IPMOD(J)*384-IPORI(J)*128-IPWCL(J)*4)              
                                                                        
*     IPSMD in range 0:8 Planar module number.                                                                
      IPSMD(J)  = IPMOD(J)*3 + IPORI(J)                                 
*                                                                                                             
*     IOS wire number (runs from 0 to 36)                                                                     
      IPIOSW(J) = IPSMD(J)*4 + IPZPL(J) + 1                             
                                                                        
* SB plane numbers (1-72) from cell number                                                                    
      IPSBW(J) = 24*IPMOD(J) + 4*IPORI(J) + IPZPL(J) + 1                
      IRSBW(J) = 24*IRMOD(J)              + IRZPL(J) + 13               
                                                                        
* Module, orientation, wire and (typical) cell number from plane                                              
* number in the range 1-72 (planars, radials and combined)                                                    
      IPMSB(J)  = (J - 1)/24                                            
      IPOSB(J)  = (J - 24*IPMSB(J) - 1)/4                               
      IPZSB(J)  = J - 24*IPMSB(J) - 4*IPOSB(J) - 1                      
      IPCLSB(J) = 384*IPMSB(J) + 128*IPOSB(J) + IPZSB(J)                
                                                                        
      IRMSB(J)  = (J - 1)/24                                            
      IRZSB(J)  = J - 24*IRMSB(J) - 13                                  
      IRCLSB(J) = 288*IRMSB(J) + IRZSB(J)                               
                                                                        
      IRADSB(J) = (J - 24*((J-1)/24) - 1)/12                            
      ICELSB(J) = IRADSB(J)*IRCLSB(J) + (1 - IRADSB(J))*IPCLSB(J)       
*KEND.                                                                                                        
                                                                        
      DATA MODST/0,6,12,18,34,40,46,52,66,72,78,84/                     
                                                                        
**********************************************************************                                        
                                                                        
* Put this track in the list of true tracks                                                                   
      JTR = 0                                                           
 100  CONTINUE                                                          
         JTR = JTR + 1                                                  
         ITR = ITRTR(1,JTR)                                             
      IF (ITR.NE.JMAX .AND. ITR.GT.0 .AND. JTR.LT.NTRACK) GOTO 100      
                                                                        
      IF (ITR.EQ.0) THEN                                                
         ITRTR(1,JTR) = JMAX                                            
         ITRTR(2,JTR) = 1                                               
      ELSEIF (ITR.EQ.JMAX) THEN                                         
         ITRTR(2,JTR) = ITRTR(2,JTR) + 1                                
      ENDIF                                                             
                                                                        
* Current true track                                                                                          
      JTRTR = JMAX                                                      
                                                                        
      CALL VZERO(MODHIT,12)                                                                            
      NPP    = 0                                                        
      NDIGP  = 0                                                        
      NGOODP = 0                                                        
                                                                        
      IF (JDIGP.GT.0) THEN                                              
         NHIT = IW(INDRSX(1)+2)                                         
         JDIG = JDIGP                                                   
 200     CONTINUE                                                       
            NDIGP = NDIGP + 1                                           
            IOK   = .FALSE.                                             
            IOKD  = .FALSE.                                             
            DO 300 JHIT=1,NHIT                                          
               IFLAG = IBTAB(INDRSX(1),1,JHIT)                          
               JSTR  = IBTAB(INDRSX(1),2,JHIT)                          
               IDIG  = IBTAB(INDRSX(1),3,JHIT)                          
               ICELL = IBTAB(INDLC(1),1,IDIG)                           
* Check for dead wire                                                                                         
               IF (IBTAB(INDG1(1),1,ICELL+1).EQ.1) GOTO 300             
               IF (JSTR.EQ.JMAX .AND. IFLAG.LT.512 .AND.                
     &             IDIG.GT.0) THEN                                      
                  IF (IDIG.EQ.JDIG)  THEN                               
                     IOK = .TRUE.                                       
                     ISGN = MOD(IBTAB(INDX(1),2,JDIG),2)                
                     IF (MOD(IFLAG,2).EQ.ISGN) IOKD = .TRUE.            
                  ENDIF                                                 
                  IF (JDIG.EQ.JDIGP) THEN                               
                     NPP   = NPP + 1                                    
                     KMOD  = ICELL/384                                  
                     KORI  = (ICELL - KMOD*384)/128                     
                     JMOD  = 4*KMOD + KORI + 1                          
                     MODHIT(JMOD) = MODHIT(JMOD) + 1                    
                  ENDIF                                                 
               ENDIF                                                    
 300        CONTINUE                                                    
            ICELL = IBTAB(INDLC(1),1,JDIG)                              
            JPL   = JFTPL(IPSBW(ICELL))                                 
            IF (IOK .AND. IOKD) THEN                                    
               NGOODP      = NGOODP + 1                                 
               LTRPL(JPL)  = .TRUE.                                     
               LTRPLD(JPL) = .TRUE.                                     
            ELSEIF (IOK) THEN                                           
C               NGOODP     = NGOODP + 1                                                                       
               LTRPL(JPL)  = .TRUE.                                     
               LTRPLD(JPL) = .FALSE.                                    
            ELSE                                                        
               LTRPL(JPL)  = .FALSE.                                    
               LTRPLD(JPL) = .FALSE.                                    
            ENDIF                                                       
            JDIG = IBTAB(INDX(1),1,JDIG)                                
         IF (JDIG.NE.JDIGP) GOTO 200                                    
      ENDIF                                                             
                                                                        
      NRP    = 0                                                        
      NDIGR  = 0                                                        
      NGOODR = 0                                                        
                                                                        
      IF (JDIGR.GT.0) THEN                                              
         NHIT = IW(INDRSX(2)+2)                                         
         JDIG = JDIGR                                                   
 400     CONTINUE                                                       
            NDIGR = NDIGR + 1                                           
            IOK   = .FALSE.                                             
            IOKD  = .FALSE.                                             
            DO 500 JHIT=1,NHIT                                          
               IFLAG = IBTAB(INDRSX(2),1,JHIT)                          
               JSTR  = IBTAB(INDRSX(2),2,JHIT)                          
               IDIG  = IBTAB(INDRSX(2),3,JHIT)                          
               IWEDG = JBIT(IFLAG,2)                                    
               ICELL = IBTAB(INDLC(2),1,IDIG)                           
               IDEAD = IBTAB(INDG1(2),1,ICELL+1)                        
* Check for dead wire                                                                                         
               IF (IDEAD.EQ.1 .OR. IDEAD.EQ.IWEDG+2) GOTO 500           
               IF (JSTR.EQ.JMAX .AND. IFLAG.LT.512 .AND.                
     &             IDIG.GT.0) THEN                                      
                  IF (IDIG.EQ.JDIG)  THEN                               
                     IOK = .TRUE.                                       
                     ISGN = MOD(IBTAB(INDX(2),2,JDIG),2)                
                     IF (MOD(IFLAG,2).EQ.ISGN) IOKD = .TRUE.            
                  ENDIF                                                 
                  IF (JDIG.EQ.JDIGR) THEN                               
                     NRP   = NRP + 1                                    
                     KMOD  = ICELL/288                                  
                     JMOD  = 4*KMOD + 4                                 
                     MODHIT(JMOD) = MODHIT(JMOD) + 1                    
                  ENDIF                                                 
               ENDIF                                                    
 500        CONTINUE                                                    
            ICELL = IBTAB(INDLC(2),1,JDIG)                              
            JPL   = JFTPL(IRSBW(ICELL))                                 
            IF (IOK .AND. IOKD) THEN                                    
               NGOODR      = NGOODR + 1                                 
               LTRPL(JPL)  = .TRUE.                                     
               LTRPLD(JPL) = .TRUE.                                     
            ELSEIF (IOK) THEN                                           
C               NGOODR     = NGOODR + 1                                                                       
               LTRPL(JPL)  = .TRUE.                                     
               LTRPLD(JPL) = .FALSE.                                    
            ELSE                                                        
               LTRPL(JPL)  = .FALSE.                                    
               LTRPLD(JPL) = .FALSE.                                    
            ENDIF                                                       
            JDIG = IBTAB(INDX(2),1,JDIG)                                
         IF (JDIG.NE.JDIGR) GOTO 400                                    
      ENDIF                                                             
                                                                        
      IF (NPP.GT.0) THEN                                                
         EFF1P = ABS(FLOAT(NGOODP)/FLOAT(NPP) - 0.001)                  
         CALL HFILL(311,EFF1P,0.,1.)                                                                   
      ELSEIF (NGOODP.EQ.0) THEN                                         
         CALL HFILL(311,-1.,0.,1.)                                                                     
      ENDIF                                                             
      IF (NDIGP.GT.0) THEN                                              
         EFF2P = ABS(FLOAT(NDIGP - NGOODP)/FLOAT(NDIGP) - 0.001)        
         CALL HFILL(314,EFF2P,0.,1.)                                                                   
      ENDIF                                                             
      CALL HFILL(321,FLOAT(NDIGP),0.,1.)                                                               
                                                                        
      IF (NRP.GT.0) THEN                                                
         EFF1R = ABS(FLOAT(NGOODR)/FLOAT(NRP) - 0.001)                  
         CALL HFILL(312,EFF1R,0.,1.)                                                                   
      ELSEIF (NGOODR.EQ.0) THEN                                         
         CALL HFILL(312,-1.,0.,1.)                                                                     
      ENDIF                                                             
      IF (NDIGR.GT.0) THEN                                              
         EFF2R = ABS(FLOAT(NDIGR - NGOODR)/FLOAT(NDIGR) - 0.001)        
         CALL HFILL(315,EFF2R,0.,1.)                                                                   
      ENDIF                                                             
      CALL HFILL(322,FLOAT(NDIGR),0.,1.)                                                               
                                                                        
      IF (NPP.GT.0 .OR. NRP.GT.0) THEN                                  
         EFF1 = ABS(FLOAT(NGOODP+NGOODR)/FLOAT(NPP+NRP) - 0.001)        
         CALL HFILL(313,EFF1,0.,1.)                                                                    
      ELSEIF (NGOODP.EQ.0 .AND. NGOODR.EQ.0) THEN                       
         CALL HFILL(313,-1.,0.,1.)                                                                     
      ENDIF                                                             
      IF (NDIGP.GT.0 .OR. NDIGR.GT.0) THEN                              
         EFF2 = ABS(FLOAT(NDIGP+NDIGR-NGOODP-NGOODR)/FLOAT(NDIGP+NDIGR) 
     &        - 0.001)                                                  
         CALL HFILL(316,EFF2,0.,1.)                                                                    
      ENDIF                                                             
                                                                        
      DO 600 JMOD = 1,12                                                
         NUM = MODST(JMOD) + MODHIT(JMOD)                               
         CALL HFILL(323,FLOAT(NUM),0.,1.)                                                              
 600  CONTINUE                                                          
                                                                        
      RETURN                                                            
      END                                                               
*