FKLFIT COMMENTS
*-- Author : S.Burke / J.V. Morris
      SUBROUTINE FKLFIT(IERR)
**********************************************************************                                        
*                                                                    *                                        
* KALMAN Filter + Smoother applied to the full Forward Tracker       *                                        
*                                                                    *                                        
* ERROR CONDITIONS;                                                  *                                        
*                                                                    *                                        
*       IERR =        0 ; normal termination                         *                                        
*   ->  IERR =      101 ; no starting point was provided             *                                        
*   ->  IERR =      102 ; not enough measurements to fit             *                                        
*   ->  IERR =      103 ; invalid value in MES array                 *                                        
*   ->  IERR =      104 ; invalid value of JSTART, JSTOP or JLAST    *                                        
*       IERR =  20 +  n ; 1 < n < 10 iterations in point rejection   *                                        
*   ->  IERR =      130 ; 10 iterations in point rejection           *                                        
*   ->  IERR = 200 + ee ; fatal error ee from FKLPRO                 *                                        
*   ->  IERR = 300 + ee ; fatal error ee from FKLFIL                 *                                        
*   ->  IERR = 400 + ee ; fatal error ee from FKLSMO                 *                                        
*   ->  IERR = 500 + ee ; fatal error ee from FKLPRS                 *                                        
*   ->  IERR = 600 + ee ; fatal error ee from FKLPAS                 *                                        
*   ->  IERR = 600 + ee ; fatal error ee from FKLPAF                 *                                        
*                                                                    *                                        
*   ->  Fatal errors                                                 *                                        
*                                                                    *                                        
**********************************************************************                                        
*KEEP,FKECODE.                                                                                                
*KEND.                                                                                                        
*                                                                                                             
* Common block definitions                                                                                    
*                                                                                                             
*KEEP,FKNPL.                                                                                                  
*                                                                                                             
* 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.                                                                        
*                                                                                                             
* /FKPROJ/                                                                                                    
* /FKFILT/                                                                                                    
* /FKSMTH/                                                                                                    
* /FKINT/                                                                                                     
* /FKRSID/                                                                                                    
* /FKTRUE/                                                                                                    
* /FKDBG/                                                                                                     
*KEEP,FKCNTL.                                                                                                 
*KEEP,FKCONS.                                                                                                 
*KEEP,FKMEAS.                                                                                                 
*KEEP,FKFLAG.                                                                                                 
*KEEP,FKPROJ.                                                                                                 
*KEEP,FKFILT.                                                                                                 
*KEEP,FKSMTH.                                                                                                 
*KEEP,FKTRUE.                                                                                                 
*KEEP,FKRSID.                                                                                                 
*KEEP,FKRJCT.                                                                                                 
*KEEP,FKINT.                                                                                                  
*KEND.                                                                                                        
**********************************************************************                                        
*                                                                                                             
* Initialisation and checks .........                                                                         
*                                                                                                             
*                                                                                                             
* Check that the start and stop positions are sensible                                                        
*                                                                                                             
         CALL FKERR(IUTIL,IROUT,IFATAL,IINV,IERR)
* Starting point does not exist                                                                               
         CALL FKERR(IUTIL,IROUT,IFATAL,IINF1,IERR)
*                                                                                                             
* Set the steps between planes according to the direction                                                     
*                                                                                                             
*                                                                                                             
* Are there enough measurements (ignoring the starting point which has                                        
* zero weight) to do the 5 parameter fit?                                                                     
*                                                                                                             
* Can only deal with 1 or 2 measurements per plane                                                            
               CALL FKERR(IUTIL,IROUT,IFATAL,IINF3,IERR)
         CALL FKERR(IUTIL,IROUT,IFATAL,IINF2,IERR)
*                                                                                                             
* Calculate plane spacing according to direction.                                                             
* Be careful about edge effects!                                                                              
*                                                                                                             
* If point rejection is on, the smoothed residuals must be calculated                                         
* Re-entry point                                                                                              
         CALL FKERR(IUTIL,IROUT,IFATAL,IFREE1,IERR)
*                                                                                                             
* Reset all flags ...                                                                                         
*                                                                                                             
*                                                                                                             
* End of initialisation and checks.                                                                           
*                                                                                                             
**********************************************************************                                        
*                                                                                                             
* If we're past the last measurement, look for a new one                                                      
            CALL FKLPAF(JPL,IFAIL)
               CALL FKERR(IUTIL,IROUT,IFPAF,IFAIL,IERR)
* Filter at plane JPL                                                                                         
         CALL FKLFLT(JPL,IFAIL)
            CALL FKERR(IUTIL,IROUT,IFFLT,IFAIL,IERR)
* Project to plane JPL+JSTEP (except if we're at the end)                                                     
            CALL FKLPRO(JPL,IFAIL)
               CALL FKERR(IUTIL,IROUT,IFPRO,IFAIL,IERR)
**********************************************************************                                        
* Smooth from plane JPL+JSTEP to plane JPL ...                                                                
         CALL FKLSMO(JPL,IFAIL)
            CALL FKERR(IUTIL,IROUT,IFSMO,IFAIL,IERR)
* Set the rejection flag to 0 on the first pass                                                               
* Reject bad points (do this on all planes, as they may be in a block)                                        
            CALL FKLPRS(JPL,NDROP,IFAIL)
               CALL FKERR(IUTIL,IROUT,IFPRS,IFAIL,IERR)
*                                                                                                             
* If there isn't a measurement, look for one.                                                                 
* FKLPAS can be called even if LMES is .TRUE., but whether                                                    
* it's useful depends on FKHUNT, which hasn't been written yet.                                               
* If JPL is above the high water mark FKLPAF will already have                                                
* looked for a new point, so there's no point in doing it again.                                              
*                                                                                                             
               CALL FKLPAS(JPL,NDROP,IFAIL)
                  CALL FKERR(IUTIL,IROUT,IFPAS,IFAIL,IERR)
* If we've changed a point on this pass, we must smooth back to JSTOP                                         
*                                                                                                             
* Have any planes been rejected during smoothing?                                                             
* If so, re-filter outwards from plane NDROP, and smooth back to NDROP+1                                      
*                                                                                                             
      IF (NPASS.GT.2) CALL FKERR(IUTIL,IROUT,IWARN,IFREE+NPASS,IERR)
*