FUNCTION FFKILL
*-- Author : Stephen Burke  30/11/92
      LOGICAL FUNCTION FFKILL(J1,J2)
*-----------------------------------------Updates 27/07/93-------                                             
**: FFKILL 30907 SB. Changes to monitoring histograms.                                                        
**: FFKILL 30907 RP. Farm changes.                                                                            
*-----------------------------------------Updates 03/03/93-------                                             
**: FFKILL 30907 SB. Cuts can be turned off with negative values.                                             
*-----------------------------------------Updates 30/11/92-------                                             
**: FFKILL 30907 SB. New deck to remove bad tracks.                                                           
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Decide whether to reject a track                                   *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      DOUBLE PRECISION S1(5),C1(5,5),S2(5),C2(5,5),DTRAN(5,5),QMS(5,5)  
     &,                DZ,RADLEN,CHI12,TEMP                             
                                                                        
*KEEP,FTHIST.                                                                                                 
* indices of filter farm histos                                                                               
      COMMON/FTHIST/IHP(100)                                            
*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,FFSCAL.                                                                                                 
* Counters                                                                                                    
      PARAMETER (NSCAL=145)                                             
      COMMON /FFSCAL/ NPFAIL,NRFAIL,NXFAIL,NFFAIL,NMFAIL,NBFAIL,NTFAIL  
     &,               NWFAIL,NNSPLT,NNMISS,NNMISP                       
     &,               NQFAIL,NAFAIL,NOFAIL,NIFAIL                       
     &,               NTRIN,NTROUT,NPRSEG(0:7,0:7),N23SEG(0:7,0:7)      
*KEEP,FKCONS.                                                                                                 
      DOUBLE PRECISION ZPL,DZPL,RADL                                    
      COMMON /FKCONS/ ZPL(NPL),DZPL(NPL),RADL(NPL)                      
*KEEP,FKSMTH.                                                                                                 
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      FFKILL = .TRUE.                                                   
                                                                        
* Momentum too small?                                                                                         
      IF (QOPMAX.GE.0. .AND. ABS(SSMT(3,J1)).GT.QOPMAX) THEN            
         NQFAIL = NQFAIL + 1                                            
         RETURN                                                         
      ENDIF                                                             
                                                                        
* Theta too big?                                                                                              
      IF (THEMAX.GE.0.) THEN                                            
         TTMX = TAN(THEMAX)                                             
      ELSE                                                              
         TTMX = 1.0E35                                                  
      ENDIF                                                             
      IF (SSMT(4,J1).GT.TTMX .OR. SSMT(4,J2).GT.TTMX) THEN              
         NAFAIL = NAFAIL + 1                                            
         RETURN                                                         
      ENDIF                                                             
                                                                        
* Start/end outside tracker?                                                                                  
      R1   = SQRT(SSMT(1,J1)**2 + SSMT(2,J1)**2)                        
      R2   = SQRT(SSMT(1,J2)**2 + SSMT(2,J2)**2)                        
      RMIN = MIN(R1,R2)                                                 
      RMAX = MAX(R1,R2)                                                 
      IF (RMIN.LT.RFTMIN .OR. RMAX.GT.RFTMAX) THEN                      
         NOFAIL = NOFAIL + 1                                            
         RETURN                                                         
      ENDIF                                                             
                                                                        
      FFKILL = .FALSE.                                                  
      IF (CEMAX.LT.0.) RETURN                                           
                                                                        
* Swim from start to end ...                                                                                  
      DZ = ZPL(J2) - ZPL(J1)                                            
      CALL FKTRAN(DZ,ZPL(J1),SSMT(1,J1),S1,DTRAN)
      CALL FKMUL(CSMT(1,1,J1),DTRAN,C1)
                                                                        
* ... allow for multiple scattering - assume av. rad. length is 10 cm (!                                      
      RADLEN = 10.0D0                                                   
      CALL FKSCAT(DZ,SSMT(1,J1),RADLEN,DTRAN,QMS)
      CALL FKQADD(C1,QMS)
                                                                        
* ... find difference between end parameters and extrapolation ...                                            
      CALL FKDIFF(SSMT(1,J2),S1,S2)
      CALL FKADD(CSMT(1,1,J2),C1,C2)
      CALL DSINV(5,C2,5,IFAIL)                                                                         
                                                                        
* ... and calculate chi-squared                                                                               
      IF (IFAIL.NE.0) RETURN                                            
                                                                        
      CHI12 = 0.0D0                                                     
      DO 200 I1=1,5                                                     
         TEMP = 0.0D0                                                   
         DO 100 I2=1,5                                                  
            TEMP = TEMP + C2(I1,I2)*S2(I2)                              
 100     CONTINUE                                                       
         CHI12 = CHI12 + S2(I1)*TEMP                                    
 200  CONTINUE                                                          
                                                                        
C      CALL SHS(20,0,SNGL(CHI12))                                                                             
      IF (CHI12.LE.CEMAX) RETURN                                        
                                                                        
      NIFAIL = NIFAIL + 1                                               
      FFKILL = .TRUE.                                                   
                                                                        
      RETURN                                                            
      END                                                               
*