      SUBROUTINE RITER_EVNT

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CHARACTER*80 TXT
      INTEGER IER
      INTEGER ERR1,ERR2,ERR3,ERR4
      SAVE ERR1,ERR2,ERR3,ERR4

      CALL RITER(IER)

      HISTO_ON=0
      IF(IER.EQ.0) THEN
         HISTO_ON=1
      ELSE IF(IER.EQ.-1) THEN
         CALL MEPRIC(ERR1,10,'RITER error: load_geo_ - missing data') 
      ELSE IF(IER.EQ.-2) THEN
         CALL MEPRIC(ERR2,10,'RITER error: - riter NOT enabled') 
      ELSE IF(IER.GT.0) THEN
         CALL MEPRIC(ERR3,100,
     &        'RITER warning: - giving up, occupancy too large') 
      ELSE
         CALL MEPRIC(ERR4,10,'RITER error: unknown')
      ENDIF

      IF(TRA_LKH_ON) THEN
         CALL TRACK_LIKEHOOD
      ENDIF

      RETURN
      END



      SUBROUTINE RITER_HFILL

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"
#include "riter/RIC_TRA.fi"
      LOGICAL GOOD(MAXTRA)
#include "arte/Arte.fi"
#include "auto/RTRA.fi"
#include "auto/RTRE.fi"
#include "arte/RSEG_cmp.fi" 

      INTEGER ITRA_OLD, NT, K, ITRA, IFOT, NDF, IROW, ID, IDP, CMP
      REAL SIG, THCER, FICER, WT, CHI, PPI, PKA, PPR, PGH, XX
      LOGICAL OK

#include "arte/Arte.ff"
#include "auto/RTRA.ff"
#include "auto/RTRE.ff"

      IF(HISTO_ON.EQ.0) RETURN 

      DO ITRA=1,NTRA
         GOOD(ITRA)=.NOT.KLON(ITRA).AND.T_MOM(ITRA).GT.5
         IF(GOOD(ITRA)) THEN
            GOOD(ITRA)=.FALSE.
            IROW=RTRA_ROW(ITRA)
            CMP=BRTRA_CMP(IROW)
            IF(IAND(CMP,RSEGC_PATT).EQ.0) GOTO 1
            IF(IAND(CMP,RSEGC_TRIT).EQ.0) GOTO 1
            NDF=IRTRA_NDOF(IROW)
            IF(NDF.LE.0) THEN
               NDF=IRTRA_HSI(IROW)+IRTRA_HIN(IROW)+IRTRA_HOU(IROW)-5
               NDF=MAX(NDF,1)
            ENDIF            
            CHI=FRTRA_CHI(IROW)/FLOAT(NDF)
            IF(CHI.GT.5) GOTO 1
            IF(IRTRA_FLR(IROW).LT.25) GOTO 1
            IF(IROW.LE.NRTRE()) THEN
               IF(IRTRE_FLMG(IROW).GT.0.AND.
     &              FRTRE_LMGT(IROW).LT.(0.1)) GOTO 1
            ENDIF
            IF(T_SIG(3,ITRA)**2+T_SIG(4,ITRA)**2.GT.(1.2E-3)**2) GOTO 1
            GOOD(ITRA)=.TRUE.
         ENDIF
 1       CONTINUE
      ENDDO

      ITRA_OLD=-1
      OK=.FALSE.
      IDP=3
      CALL GET_RING_TAB_ROWS(NT)
      DO K=1,NT
         CALL GET_RING_TAB(K,ITRA,IFOT,SIG,THCER,FICER,WT)
         IF(ITRA.NE.ITRA_OLD) THEN
            ITRA_OLD=ITRA
            OK=.FALSE.
            IF(GOOD(ITRA)) THEN
               IF(T_MOM(ITRA).LT.10) THEN
                  ID=1000
               ELSE IF(T_MOM(ITRA).LT.20) THEN
                  ID=1004
               ELSE IF(T_MOM(ITRA).LT.40) THEN
                  ID=1008
               ELSE
                  ID=1012
               ENDIF
               IROW=RTRA_ROW(ITRA)
               PPI=FRTRA_LRE(IROW)+FRTRA_LRMU(IROW)+FRTRA_LRPI(IROW)
               PKA=FRTRA_LRK(IROW)
               PPR=FRTRA_LRP(IROW)
               PGH=1.-PPI-PKA-PPR
               CALL RHFILL(ID+1,0.,0.,PGH)
               CALL RHFILL(ID+1,3.,0.,PPI)
               CALL RHFILL(ID+1,4.,0.,PKA)
               CALL RHFILL(ID+1,5.,0.,PPR)
               OK=PPI.GT.(0.05)
               IF(OK) CALL RHFILL(ID+4,STFS(3,ITRA),0.,1.)
               IDP=3
               IF(FRTRA_LMMU(IROW).GT.0) IDP=2
            ENDIF
         ENDIF
         IF(OK) THEN
            XX=THCER-THC(IDP,ITRA)
            SIG=ABS(SIG)
            CALL RHFILL(ID+2,XX*1000.,0.,WT)
            IF(SIG.NE.0) CALL RHFILL(ID+3,XX/SIG,0.,WT)
         ENDIF
      ENDDO
      HISTO_ON=0

      RETURN
      END
 

