FUNCTION FKCHPR
*-- Author : S.Burke / J.V. Morris
      DOUBLE PRECISION FUNCTION FKCHPR(NPROB,NFREE,IERR)
**********************************************************************                                        
*                                                                    *                                        
*  Inverse chi-sq probability                                        *                                        
*                                                                    *                                        
* ERROR CONDITIONS;                                                  *                                        
*       IERR =   0  ; normal termination                             *                                        
*   ->  IERR = 101  ; invalid probability cut                        *                                        
*   ->  IERR = 104  ; invalid value of NPROB or NFREE                *                                        
*                                                                    *                                        
*   ->  Fatal error                                                  *                                        
*                                                                    *                                        
* The function returns -1 for error 4, 0 or 10**10 for error 1       *                                        
*                                                                    *                                        
**********************************************************************                                        
                                                                        
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                               
                                                                        
      PARAMETER (IUTIL=1,IROUT=9)                                       
*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,FKRJCT.                                                                                                 
      DOUBLE PRECISION CHITOT,X2PCUT,X2CUTB,X2CUTA,X2CUTN               
     &,                       X2PCTI,X2CTBI,X2CTAI,X2CTNI               
      LOGICAL LWIRE,LPRINI                                              
      COMMON /FKRJCT/ X2PCUT,X2CUTB,X2CUTA,X2CUTN                       
     &,               X2PCTI,X2CTBI,X2CTAI,X2CTNI                       
     &,               CHITOT(NPL),NDF(NPL)                              
     &,               NBLOCK(NPL),NBADP(NPL),NBADB(NPL)                 
     &,               NFAILP(NPL),NFAILB(NPL),NNEWP(NPL)                
     &,               NUNRJP(NPL),NUNRJB(NPL),NRERJP(NPL)               
     &,               NCPRS,NBPRS,NCPAS,NPASS,IRJCT(NPL)                
     &,               LWIRE(NPL),LPRINI                                 
*KEND.                                                                                                        
                                                                        
**********************************************************************                                        
                                                                        
      REAL PROB                                                         
      DIMENSION PCUT(8),CHISQ(4,2*NPL)                                  
      EQUIVALENCE(PCUT,X2PCUT)                                          
                                                                        
      SAVE CHISQ                                                        
                                                                        
      PARAMETER (NDIM=8*NPL)                                            
      DATA CHISQ/NDIM*-1.D0/                                            
                                                                        
**********************************************************************                                        
                                                                        
      IERR = 0                                                          
                                                                        
      IF (NPROB.LE.0 .OR. NPROB.GT.4 .OR. NFREE.LE.0) THEN              
         CALL FKERR(IUTIL,IROUT,IFATAL,IINV,IERR)
         FKCHPR = -1.D0                                                 
         RETURN                                                         
      ENDIF                                                             
                                                                        
      IF (NFREE.LE.2*NPL .AND. CHISQ(NPROB,NFREE).GE.0.D0) THEN         
         FKCHPR = CHISQ(NPROB,NFREE)                                    
         RETURN                                                         
      ENDIF                                                             
                                                                        
      PROB = 1.D0 - PCUT(NPROB)                                         
      IF (PROB.LT.0.D0 .OR. PROB.GT.1.D0)                               
     &   CALL FKERR(IUTIL,IROUT,IFATAL,IINF1,IERR)
                                                                        
      IF (PROB.LE.0.D0) THEN                                            
         FKCHPR = 0.D0                                                  
      ELSEIF (PROB.GE.1.D0) THEN                                        
         FKCHPR = 1.D10                                                 
      ELSE                                                              
         FKCHPR = CHISIN(PROB,NFREE)                                    
      ENDIF                                                             
                                                                        
      IF (NFREE.LE.2*NPL) CHISQ(NPROB,NFREE) = FKCHPR                   
                                                                        
      RETURN                                                            
      END                                                               
*