SUBROUTINE FKANAL
*-- Author : Stephen Burke
      SUBROUTINE FKANAL(JPL,RES,CHISQ,NPS,NRS)
*-----------------------------------------Updates 06/08/92-------                                             
**: FKANAL.......SB. Minor bug fixed.                                                                         
*-----------------------------------------Updates 24/01/92-------                                             
**: FKANAL 30205.SB. Digi with wrong drift sign counts as a bad hit.                                          
**: FKANAL 30205.SB. Phi histograms added.                                                                    
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Analyse the KF output                                              *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      DOUBLE PRECISION RES(2),CHISQ,FKDPHI
                                                                        
*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,FKCNTL.                                                                                                 
      COMMON /FKCNTL/ LUN,IPR,ITR,IPL,JSTART,JSTOP,JLAST,JSTEP          
*KEEP,FKMEAS.                                                                                                 
      DOUBLE PRECISION WMES,CMES,HMES                                   
      COMMON /FKMEAS/ WMES(2,NPL),CMES(2,2,NPL),HMES(2,2,NPL),MES(NPL)  
*KEEP,FKTRUE.                                                                                                 
*KEEP,FKPROJ.                                                                                                 
*KEEP,FKSMTH.                                                                                                 
*KEEP,FKRSID.                                                                                                 
*KEEP,FKFLAG.                                                                                                 
      LOGICAL LPRO,LFIL,LSMT,LMES,LRAD,LRPRO,LRFIL,LRSMT,LPOINT,LBLOCK  
      COMMON /FKFLAG/ LPRO(NPL),LFIL(NPL),LSMT(NPL),LMES(NPL)           
     &,               LRAD(NPL),LRPRO,LRFIL,LRSMT,LPOINT,LBLOCK         
*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,FKHIST.                                                                                                 
      DIMENSION IPLANE(12)                                              
      SAVE IPLANE                                                       
      DATA IPLANE/1,12,13,24,25,36,37,48,49,60,61,72/                   
*KEEP,FKDBG.                                                                                                  
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      CALL HCDIR('//PAWC/'//CKDBG,' ')                                                                 
                                                                        
      IF (JPL.GT.0) THEN                                                
         IF (LTRPL(JPL) .AND. LTRPLD(JPL)) THEN                         
            IBASE = 315                                                 
         ELSE                                                           
            IBASE = 325                                                 
         ENDIF                                                          
         CALL HFILL(IBASE+1+5*IRP(JPL),SNGL(RES(1)),0.,1.)                                             
         IF (MES(JPL).EQ.2) CALL HFILL(IBASE+12,SNGL(RES(2)),0.,1.)                                    
         CALL HFILL(IBASE+5+5*IRP(JPL),SNGL(CHISQ),0.,1.)                                              
         RETURN                                                         
      ENDIF                                                             
                                                                        
* Decode steering                                                                                             
      IDIFF = MOD(IHFK,10)                                              
      IRES  = MOD(IHFK/10,10)                                           
      IPLN  = MOD(IHFK/100,10)                                          
                                                                        
      IF (LTRUE .AND. IDIFF.GT.0) THEN                                  
         IF (CSMT(1,1,JSTART).GT.0.0D0)                                 
     &       CALL HFILL(101,SNGL((SSMT(1,JSTART)-TRUE(1,JSTART))/                                      
     &                       DSQRT(CSMT(1,1,JSTART))),0.,1.)            
         IF (CSMT(2,2,JSTART).GT.0.0D0)                                 
     &       CALL HFILL(102,SNGL((SSMT(2,JSTART)-TRUE(2,JSTART))/                                      
     &                       DSQRT(CSMT(2,2,JSTART))),0.,1.)            
         IF (CSMT(3,3,JSTART).GT.0.0D0)                                 
     &       CALL HFILL(103,SNGL((SSMT(3,JSTART)-TRUE(3,JSTART))/                                      
     &                       DSQRT(CSMT(3,3,JSTART))),0.,1.)            
         IF (CSMT(4,4,JSTART).GT.0.0D0)                                 
     &       CALL HFILL(104,SNGL((SSMT(4,JSTART)-TRUE(4,JSTART))/                                      
     &                       DSQRT(CSMT(4,4,JSTART))),0.,1.)            
         IF (CSMT(5,5,JSTART).GT.0.0D0)                                 
     &       CALL HFILL(105,SNGL((SSMT(5,JSTART)-TRUE(5,JSTART))/                                      
     &                       DSQRT(CSMT(5,5,JSTART))),0.,1.)            
         IF (TRUE(3,JSTART).EQ.0.0D0) TRUE(3,JSTART) = 1.0D-20          
         PLOG   = -DLOG10(DABS(TRUE(3,JSTART)))                         
         THETA  = DATAN(TRUE(4,JSTART))                                 
         PHI    = TRUE(5,JSTART)                                        
C         D1OP   = DABS(SSMT(3,JSTART)) - DABS(TRUE(3,JSTART))                                                
         D1OP   = SSMT(3,JSTART) - TRUE(3,JSTART)                       
         DTHETA = DATAN(SSMT(4,JSTART)) - THETA                         
         DPHI   = FKDPHI(SSMT(5,JSTART),TRUE(5,JSTART))
         IF (PLOG.LT.0. .AND. PLOG.GT.-0.3) THEN                        
            CALL HFILL(230,FLOAT(NPS+NRS),D1OP,1.)                                                     
         ELSEIF (PLOG.LT.0.5) THEN                                      
            CALL HFILL(231,FLOAT(NPS+NRS),D1OP,1.)                                                     
         ELSEIF (PLOG.GE.0.5) THEN                                      
            CALL HFILL(232,FLOAT(NPS+NRS),D1OP,1.)                                                     
         ENDIF                                                          
         CALL HFILL(233,PLOG,D1OP,1.)                                                                  
         CALL HFILL(234,PLOG,DTHETA,1.)                                                                
         CALL HFILL(235,PLOG,DPHI,1.)                                                                  
         CALL HFILL(243,THETA,D1OP,1.)                                                                 
         CALL HFILL(244,THETA,DTHETA,1.)                                                               
         CALL HFILL(245,THETA,DPHI,1.)                                                                 
         CALL HFILL(253,PHI,D1OP,1.)                                                                   
         CALL HFILL(254,PHI,DTHETA,1.)                                                                 
         CALL HFILL(255,PHI,DPHI,1.)                                                                   
      ENDIF                                                             
                                                                        
      IF (IRES.GT.0) THEN                                               
         DO 100 KPL=1,JPLMAX                                            
            IF (LMES(KPL)) THEN                                         
               IF (LTRPL(KPL) .AND. LTRPLD(KPL)) THEN                   
                  IBASE = 295                                           
               ELSE                                                     
                  IBASE = 305                                           
               ENDIF                                                    
               CALL HFILL(IBASE+1+5*IRP(KPL),SNGL(RSMT(1,KPL)),0.,1.)                                  
               IF (MES(KPL).EQ.2)                                       
     &            CALL HFILL(IBASE+12,SNGL(RSMT(2,KPL)),0.,1.)                                         
               CALL HFILL(IBASE+5+5*IRP(KPL),SNGL(CHISMT(KPL)),0.,1.)                                  
            ENDIF                                                       
 100     CONTINUE                                                       
      ENDIF                                                             
                                                                        
      IF (IPLN.GT.0) THEN                                               
         DO 200 I=1,12                                                  
            KPL = JFTPL(IPLANE(I))                                      
            IF (LMES(KPL) .AND. KPL.GT.0) THEN                          
               CHPROB=PROB(SNGL(CHIFIL(KPL)),MES(KPL))                  
               CALL HFILL(KPL+500,CHPROB,0.,1.)                                                        
               CHPROB=PROB(SNGL(CHISMT(KPL)),MES(KPL))                  
               CALL HFILL(KPL+600,CHPROB,0.,1.)                                                        
               IF (LTRUE) THEN                                          
                  CHPROB=PROB(SNGL(CHITRU(KPL)),5)                      
                  CALL HFILL(KPL+700,CHPROB,0.,1.)                                                     
               ENDIF                                                    
               CALL HFILL(KPL+800,SNGL(RSMT(1,KPL)),0.,1.)                                             
               IF (MES(KPL).EQ.2)                                       
     &            CALL HFILL(KPL+900,SNGL(RSMT(2,KPL)),0.,1.)                                          
            ENDIF                                                       
 200     CONTINUE                                                       
      ENDIF                                                             
                                                                        
      RETURN                                                            
      END                                                               
*