SUBROUTINE FFEND
*-- Author : Stephen Burke  15/02/94
      SUBROUTINE FFEND
*-----------------------------------------Updates 15/02/94-------                                             
**: FFEND.......SB.  New routine for end-of-run printout                                                      
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Print statistics at end of run                                     *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
*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,FKCNTL.                                                                                                 
      COMMON /FKCNTL/ LUN,IPR,ITR,IPL,JSTART,JSTOP,JLAST,JSTEP          
*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,FKDBG.                                                                                                  
*KEND.                                                                                                        
      COMMON /SUMARY/ LSUMA                                             
                                                                        
      DIMENSION SEGFR(0:3,0:3),SEGFRP(0:3),SEGFRR(0:3)                  
      DIMENSION S23FR(0:3,0:3),S23FRP(0:3),S23FRR(0:3)                  
                                                                        
*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,BOSMDL.                                                                                                 
C     ------BOSMDL                                                                                            
      LOGICAL       BEGJOB,ENDRUN,BEGRUN,REVENT,ENDJOB,OTHDAT           
      COMMON/BOSMDL/BEGJOB,ENDRUN,BEGRUN,REVENT,ENDJOB,OTHDAT,          
     +              LCCRUN,NCCRUN,NEVENT,                               
     +              IHA,IBS,IDB,IDATEL,LUP,ISN,JSN                      
      SAVE  /BOSMDL/                                                    
C     ------                                                                                                  
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      IF (IW(6).LE.0) RETURN                                            
      IF (.NOT.ENDJOB .AND. LSUMA.NE.1) RETURN                          
                                                                        
      IF (LUN.LE.0) LUN = 6                                             
                                                                        
      WRITE(LUN,*)                                                      
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) '             *** Kalman Filter track summary ***'   
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Tracks found:               ',NTRIN                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'BOS array full:             ',NWFAIL                
      WRITE(LUN,*) 'Bank structure corrupt:     ',NBFAIL                
      WRITE(LUN,*) 'Bad track parameters:       ',NTFAIL                
      WRITE(LUN,*) 'Too few planar points:      ',NPFAIL                
      WRITE(LUN,*) 'Too few radial points:      ',NRFAIL                
      WRITE(LUN,*) 'Momentum below cut:         ',NMFAIL                
      WRITE(LUN,*) 'Fatal error in fit:         ',NFFAIL                
      WRITE(LUN,*) 'Bad chi-squared prob:       ',NXFAIL                
      WRITE(LUN,*) 'Momentum too small:         ',NQFAIL                
      WRITE(LUN,*) 'Theta too large:            ',NAFAIL                
      WRITE(LUN,*) 'Track outside tracker:      ',NOFAIL                
      WRITE(LUN,*) 'Start and end inconsistent: ',NIFAIL                
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Tracks fitted:              ',NTROUT                
      WRITE(LUN,*)                                                      
      WRITE(LUN,*)                                                      
                                                                        
      IF (LTRUTH) THEN                                                  
         WRITE(LUN,*) 'Tracks split:               ',NNSPLT             
         WRITE(LUN,*) 'Tracks missed:              ',NNMISS             
         WRITE(LUN,*) 'Tracks missed (primary):    ',NNMISP             
         WRITE(LUN,*)                                                   
         WRITE(LUN,*)                                                   
      ENDIF                                                             
                                                                        
      IF (NTROUT.LE.0 .OR. .NOT.ENDJOB) RETURN                          
                                                                        
      CALL VZERO(SEGFR,16)                                                                             
      CALL VZERO(SEGFRP,4)                                                                             
      CALL VZERO(SEGFRR,4)                                                                             
      SEGSUM = 0.                                                       
      CALL VZERO(S23FR,16)                                                                             
      CALL VZERO(S23FRP,4)                                                                             
      CALL VZERO(S23FRR,4)                                                                             
      S23SUM = 0.                                                       
      DO 200 JPS=0,7                                                    
         NPS = JBIT(JPS,3) + JBIT(JPS,2) + JBIT(JPS,1)                  
         DO 100 JRS=0,7                                                 
            NRS  = JBIT(JRS,3) + JBIT(JRS,2) + JBIT(JRS,1)              
            SEGF = 100.*FLOAT(NPRSEG(JPS,JRS))/FLOAT(NTROUT)            
            SEGFR(NPS,NRS) = SEGFR(NPS,NRS) + SEGF                      
            SEGFRP(NPS)    = SEGFRP(NPS)    + SEGF                      
            SEGFRR(NRS)    = SEGFRR(NRS)    + SEGF                      
            SEGSUM         = SEGSUM         + SEGF                      
            S23F = 100.*FLOAT(N23SEG(JPS,JRS))/FLOAT(NTROUT)            
            S23FR(NPS,NRS) = S23FR(NPS,NRS) + S23F                      
            S23FRP(NPS)    = S23FRP(NPS)    + S23F                      
            S23FRR(NRS)    = S23FRR(NRS)    + S23F                      
            S23SUM         = S23SUM         + S23F                      
 100     CONTINUE                                                       
 200  CONTINUE                                                          
                                                                        
      WRITE(LUN,1000)                                                   
      WRITE(LUN,*)                                                      
      WRITE(LUN,1001) (SEGFR(0,JRS),JRS=0,3),SEGFRP(0)                  
      WRITE(LUN,1002) (SEGFR(1,JRS),JRS=0,3),SEGFRP(1)                  
      WRITE(LUN,1003) (SEGFR(2,JRS),JRS=0,3),SEGFRP(2)                  
      WRITE(LUN,1004) (SEGFR(3,JRS),JRS=0,3),SEGFRP(3)                  
      WRITE(LUN,*)                                                      
      WRITE(LUN,1005) (SEGFRR(JPS),JPS=0,3),SEGSUM                      
      WRITE(LUN,*)                                                      
      WRITE(LUN,*)                                                      
                                                                        
 1000 FORMAT(14X,'0R',8X,'1R',8X,'2R',8X,'3R',12X,'XR')                 
 1001 FORMAT('     0P:   ',4(F6.2,4X),4X,F6.2)                          
 1002 FORMAT('     1P:   ',4(F6.2,4X),4X,F6.2)                          
 1003 FORMAT('     2P:   ',4(F6.2,4X),4X,F6.2)                          
 1004 FORMAT('     3P:   ',4(F6.2,4X),4X,F6.2)                          
 1005 FORMAT('     XP:   ',4(F6.2,4X),4X,F6.2)                          
                                                                        
      WRITE(LUN,1010)                                                   
      WRITE(LUN,*)                                                      
      WRITE(LUN,1011) (S23FR(0,JRS),JRS=0,3),S23FRP(0)                  
      WRITE(LUN,1012) (S23FR(1,JRS),JRS=0,3),S23FRP(1)                  
      WRITE(LUN,1013) (S23FR(2,JRS),JRS=0,3),S23FRP(2)                  
      WRITE(LUN,1014) (S23FR(3,JRS),JRS=0,3),S23FRP(3)                  
      WRITE(LUN,*)                                                      
      WRITE(LUN,1015) (S23FRR(JPS),JPS=0,3),S23SUM                      
      WRITE(LUN,*)                                                      
      WRITE(LUN,*)                                                      
                                                                        
 1010 FORMAT(13X,'0P3',7X,'1P3',7X,'2P3',7X,'3P3',11X,'XP3')            
 1011 FORMAT('     0P2:  ',4(F6.2,4X),4X,F6.2)                          
 1012 FORMAT('     1P2:  ',4(F6.2,4X),4X,F6.2)                          
 1013 FORMAT('     2P2:  ',4(F6.2,4X),4X,F6.2)                          
 1014 FORMAT('     3P2:  ',4(F6.2,4X),4X,F6.2)                          
 1015 FORMAT('     XP2:  ',4(F6.2,4X),4X,F6.2)                          
                                                                        
      P1P2   = 0.                                                       
      P0P1P2 = 0.                                                       
      R0R1   = 0.                                                       
      P0R0R1 = 0.                                                       
      R0P1   = 0.                                                       
      P0R0P1 = 0.                                                       
      P0P2   = 0.                                                       
      R0P1R1 = 0.                                                       
      R1R2   = 0.                                                       
      P0P1   = 0.                                                       
      P1R1P2 = 0.                                                       
      R0R2   = 0.                                                       
      R0R1R2 = 0.                                                       
      P1P2R2 = 0.                                                       
      R1P2   = 0.                                                       
      R1P2R2 = 0.                                                       
                                                                        
* Order is planar, radial, mixed, P?, R?                                                                      
      DO 400 JPS=0,7                                                    
         DO 300 JRS=0,7                                                 
            MASK = JPS*8 + JRS                                          
            NSEG = NPRSEG(JPS,JRS)                                      
* P0: planar, radial, mixed                                                                                   
            IF (IAND(MASK,32+16).EQ.48)   P1P2   = P1P2   + NSEG        
            IF (IAND(MASK,32+16+8).EQ.56) P0P1P2 = P0P1P2 + NSEG        
            IF (IAND(MASK,2+1).EQ.3)      R0R1   = R0R1   + NSEG        
            IF (IAND(MASK,8+2+1).EQ.11)   P0R0R1 = P0R0R1 + NSEG        
            IF (IAND(MASK,16+1).EQ.17)    R0P1   = R0P1   + NSEG        
            IF (IAND(MASK,16+8+1).EQ.25)  P0R0P1 = P0R0P1 + NSEG        
* P1: planar, radial                                                                                          
            IF (IAND(MASK,32+8).EQ.40)    P0P2   = P0P2   + NSEG        
            IF (IAND(MASK,16+2+1).EQ.19)  R0P1R1 = R0P1R1 + NSEG        
* P2: planar, radial                                                                                          
            IF (IAND(MASK,4+2).EQ.6)      R1R2   = R1R2   + NSEG        
* R0: planar                                                                                                  
            IF (IAND(MASK,16+8).EQ.24)    P0P1   = P0P1   + NSEG        
* R1: planar, radial                                                                                          
            IF (IAND(MASK,32+16+2).EQ.50) P1R1P2 = P1R1P2 + NSEG        
            IF (IAND(MASK,4+1).EQ.5)      R0R2   = R0R2   + NSEG        
            IF (IAND(MASK,4+2+1).EQ.7)    R0R1R2 = R0R1R2 + NSEG        
* R2: planar, mixed                                                                                           
            IF (IAND(MASK,32+16+4).EQ.52) P1P2R2 = P1P2R2 + NSEG        
            IF (IAND(MASK,32+2).EQ.34)    R1P2   = R1P2   + NSEG        
            IF (IAND(MASK,32+4+2).EQ.38)  R1P2R2 = R1P2R2 + NSEG        
 300     CONTINUE                                                       
 400  CONTINUE                                                          
                                                                        
      EFFP0P = -1.                                                      
      EFFP0R = -1.                                                      
      EFFP0M = -1.                                                      
      EFFP1P = -1.                                                      
      EFFP1R = -1.                                                      
      EFFP2P = -1.                                                      
      EFFP2R = -1.                                                      
      EFFR0P = -1.                                                      
      EFFR1P = -1.                                                      
      EFFR1R = -1.                                                      
      EFFR2P = -1.                                                      
      EFFR2M = -1.                                                      
                                                                        
      IF (P1P2.GT.0.) EFFP0P = P0P1P2/P1P2                              
      IF (R0R1.GT.0.) EFFP0R = P0R0R1/R0R1                              
      IF (R0P1.GT.0.) EFFP0M = P0R0P1/R0P1                              
      IF (P0P2.GT.0.) EFFP1P = P0P1P2/P0P2                              
      IF (R0R1.GT.0.) EFFP1R = R0P1R1/R0R1                              
      IF (P0P1.GT.0.) EFFP2P = P0P1P2/P0P1                              
      IF (R1R2.GT.0.) EFFP2R = R1P2R2/R1R2                              
      IF (P0P1.GT.0.) EFFR0P = P0R0P1/P0P1                              
      IF (P1P2.GT.0.) EFFR1P = P1R1P2/P1P2                              
      IF (R0R2.GT.0.) EFFR1R = R0R1R2/R0R2                              
      IF (P1P2.GT.0.) EFFR2P = P1P2R2/P1P2                              
      IF (R1P2.GT.0.) EFFR2M = R1P2R2/R1P2                              
                                                                        
      WRITE(LUN,*) 'Efficiency for planar 0 from planars = ',EFFP0P     
     &,            '   (',INT(P1P2),')'                                 
      WRITE(LUN,*) 'Efficiency for planar 0 from radials = ',EFFP0R     
     &,            '   (',INT(R0R1),')'                                 
      WRITE(LUN,*) 'Efficiency for planar 0 from both    = ',EFFP0M     
     &,            '   (',INT(R0P1),')'                                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Efficiency for planar 1 from planars = ',EFFP1P     
     &,            '   (',INT(P0P2),')'                                 
      WRITE(LUN,*) 'Efficiency for planar 1 from radials = ',EFFP1R     
     &,            '   (',INT(R0R1),')'                                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Efficiency for planar 2 from planars = ',EFFP2P     
     &,            '   (',INT(P0P1),')'                                 
      WRITE(LUN,*) 'Efficiency for planar 2 from radials = ',EFFP2R     
     &,            '   (',INT(R1R2),')'                                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Efficiency for radial 0 from planars = ',EFFR0P     
     &,            '   (',INT(P0P1),')'                                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Efficiency for radial 1 from planars = ',EFFR1P     
     &,            '   (',INT(P1P2),')'                                 
      WRITE(LUN,*) 'Efficiency for radial 1 from radials = ',EFFR1R     
     &,            '   (',INT(R0R2),')'                                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*) 'Efficiency for radial 2 from planars = ',EFFR2P     
     &,            '   (',INT(P1P2),')'                                 
      WRITE(LUN,*) 'Efficiency for radial 2 from both    = ',EFFR2M     
     &,            '   (',INT(R1P2),')'                                 
      WRITE(LUN,*)                                                      
      WRITE(LUN,*)                                                      
                                                                        
      IF (IDIAG.GT.0) THEN                                              
* Output LOOK histograms                                                                                      
         CALL SAREA('FTREC',0)                                                                         
         CALL FWLOOK('FFLOOKOUTPUT',IERR)                                                              
         IF (IERR.NE.0) THEN                                            
            WRITE(6,'('' FWLOOK Error'',I10)') IERR                     
         ELSE                                                           
            WRITE(6,'('' FFKAL Look output to file FFLOOKOUTPUT'')')    
         ENDIF                                                          
      ENDIF                                                             
                                                                        
*Check print flag                                                                                             
      IF (IW(6).GT.0) THEN                                              
         CALL FKEND
         CALL HPDIR('//PAWC/'//CFDBG,' ')                                                              
         CALL HPDIR('//PAWC/'//CFKDBG,' ')                                                             
         IF (IHFK.GT.0) CALL FKHPR
      ENDIF                                                             
                                                                        
                                                                        
      IF (IDIAG.LT.100 .OR. LUNHB.LE.0) RETURN                          
                                                                        
      OPEN(UNIT=LUNHB,ACCESS='DIRECT',FORM='UNFORMATTED',               
     &     RECL=1024,STATUS='NEW',IOSTAT=IOS)                           
      IF (IOS.NE.0) THEN                                                
         CALL ERRLOG(311,'W:FFKAL:  HBOOK file open failed')                                           
         RETURN                                                         
      ENDIF                                                             
                                                                        
      CALL HRFILE(LUNHB,'FFKAL','N')                                                                   
      CALL HCDIR('//PAWC/'//CFDBG,' ')                                                                 
      CALL HCDIR('//FFKAL',' ')                                                                        
      CALL HROUT(0,ICYC,' ')                                                                           
      CALL HCDIR('//PAWC/'//CFKDBG,' ')                                                                
      CALL HCDIR('//FFKAL',' ')                                                                        
      CALL HROUT(0,ICYC,' ')                                                                           
      CALL HCDIR('//PAWC/'//CKDBG,' ')                                                                 
      CALL HCDIR('//FFKAL',' ')                                                                        
      CALL HROUT(0,ICYC,' ')                                                                           
      CALL HCDIR('//PAWC/FPT0',' ')                                                                    
      CALL HCDIR('//FFKAL',' ')                                                                        
      CALL HROUT(0,ICYC,' ')                                                                           
      CALL HCDIR('//PAWC',' ')                                                                         
      CALL HREND('FFKAL')                                                                              
                                                                        
      CLOSE(LUNHB)                                                      
                                                                        
      RETURN                                                            
      END                                                               
*