SUBROUTINE FKRST
*-- Author : S.Burke / J.V. Morris
      SUBROUTINE FKRST(JPL,IERR)
**********************************************************************                                        
*                                                                    *                                        
* Calculate residuals and chi-squared between the smoothed and true  *                                        
* vectors at plane JPL                                               *                                        
*                                                                    *                                        
* ERROR CONDITIONS;                                                  *                                        
*       IERR =   0  ; normal termination                             *                                        
*   ->  IERR = 111  ; smoothed covariance not positive definite      *                                        
*                                                                    *                                        
*   ->  Fatal error                                                  *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                               
                                                                        
      PARAMETER (IUTIL=1,IROUT=5)                                       
*KEEP,FKECODE.                                                                                                
      PARAMETER (IWARN=0,IFATAL=1,IFPRO=2,IFFLT=3,IFSMO=4,IFPRS=5,      
     &           IFPAS=6,IFPAF=7)                                       
      PARAMETER (IINF1=1,IINF2=2,IINF3=3,IINV=4,IDONE=5)                
      PARAMETER (IICV=6,IMCV=7,IOCV=11,IRCV=12,IOVCV=13,                
     &           ITHGP2=16,ITHG1=17)                                    
      PARAMETER (IFREE=20,IFREE1=30,IFREE2=40,IFREE3=50)                
*KEND.                                                                                                        
                                                                        
*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,FKTRUE.                                                                                                 
*KEEP,FKSMTH.                                                                                                 
*KEND.                                                                                                        
                                                                        
      DIMENSION CT(5,5)                                                 
                                                                        
**********************************************************************                                        
                                                                        
      IERR = 0                                                          
                                                                        
*                                                                                                             
* Calculate the residuals ...                                                                                 
*                                                                                                             
      CALL FKDIFF(TRUE(1,JPL),SSMT(1,JPL),RTRUE(1,JPL))
                                                                        
*                                                                                                             
* ... and the chi-squared                                                                                     
*                                                                                                             
      CALL UCOPY(CSMT(1,1,JPL),CT,50)                                                                  
      CALL DSINV(5,CT,5,IFAIL)                                                                         
      CHITRU(JPL) = 0.D0                                                
      IF (IFAIL.NE.0) THEN                                              
         CALL FKERR(IUTIL,IROUT,IFATAL,IOCV,IERR)
      ELSE                                                              
         DO 200 I1=1,5                                                  
         DO 200 I2=1,5                                                  
            CHITRU(JPL) = CHITRU(JPL)                                   
     &                    + RTRUE(I1,JPL)*CT(I1,I2)*RTRUE(I2,JPL)       
 200     CONTINUE                                                       
      ENDIF                                                             
                                                                        
      RETURN                                                            
      END                                                               
*