!--------------------------------------------------------------------
! write result of riter to RTRA
! M. Staric, feb-2000
!
!--------------------------------------------------------------------


      SUBROUTINE RITER_TO_RTRA

      IMPLICIT NONE

#include "riter/RIC_TRA.fi"
      REAL HRI(MAXTRA)
#include "arte/Arte.fi"
#include "auto/RTRA.fi"

      INTEGER ITRA, IROW, I, IDP
      REAL PROB(0:NHYP-1)
      REAL LRME

#include "arte/Arte.ff"
#include "auto/RTRA.ff"
      
      CALL RITER_ESTIMATE_HRI(HRI)
      DO ITRA=1,NTRA
         IROW = RTRA_ROW(ITRA)
         IF(IROW.LT.1.OR.IROW.GT.NRTRA()) GOTO 1
         IQ(IQRTRA_HRI(IROW)) = NINT(HRI(ITRA))
         IQ(IQRTRA_FLR(IROW)) = RECO_FLAG(ITRA)
         IF(RECO_FLAG(ITRA).NE.0) THEN
            DO I=0,NHYP-1
               PROB(I)=PLKH(I,ITRA)
            ENDDO
            IF(NHYP.GT.6) CALL NORMALIZE_LKH(PROB,NHYP,6,IDP)
            Q( QRTRA_LRE(IROW))  = EXP( PROB(1) )
            Q( QRTRA_LRMU(IROW)) = EXP( PROB(2) )
            Q( QRTRA_LRPI(IROW)) = EXP( PROB(3) )
            Q( QRTRA_LRK( IROW)) = EXP( PROB(4) )
            Q( QRTRA_LRP( IROW)) = EXP( PROB(5) )
            LRME=0.
            DO I=6,NHYP-1
               LRME=LRME+EXP(MIN(PROB(I),70.))
            ENDDO
            Q( QRTRA_LRME( IROW)) = LRME
         ELSE
            Q( QRTRA_LRE(IROW))  = 0.
            Q( QRTRA_LRMU(IROW)) = 0.
            Q( QRTRA_LRPI(IROW)) = 0.
            Q( QRTRA_LRK( IROW)) = 0.
            Q( QRTRA_LRP( IROW)) = 0.
            Q( QRTRA_LRME( IROW)) = 0.
         ENDIF
 1       CONTINUE
      ENDDO
     
      RETURN
      END



      SUBROUTINE RITER_ESTIMATE_HRI(HRI)

      IMPLICIT NONE
      REAL HRI(*)

#include "riter/RIC_TRA.fi"
#include "riter/RIC_PAR.fi"

      REAL WIN(MAXTRA)
      INTEGER ITRA
      INTEGER K, NT, IFOT
      REAL SIG, THCER, FICER, WT, THC0
      REAL X1, X2, BGR

c--   reset, signal window
      DO ITRA=1,NTRA
         SIG=SQRT(SIG_M16**2+(T_SIG(1,ITRA)**2+T_SIG(2,ITRA)**2)/2.) 
         WIN(ITRA)=SWIN_SIZE*SIG
         HRI(ITRA)=0.
      ENDDO

c--   count photons inside the window for max. probable hypothesis
      CALL GET_RING_TAB_ROWS(NT)
      DO K=1,NT
         CALL GET_RING_TAB(K,ITRA,IFOT,SIG,THCER,FICER,WT)
         IF(RECO_FLAG(ITRA).NE.0) THEN
            THC0=THC(PID(ITRA),ITRA)
            IF(THC0.GT.0) THEN
               IF(ABS(THCER-THC0).LT.WIN(ITRA)) HRI(ITRA)=HRI(ITRA)+WT
            ENDIF
         ENDIF
      ENDDO

c--   subtract estimated background
      DO ITRA=1,NTRA
         IF(RECO_FLAG(ITRA).NE.0) THEN
            THC0=THC(PID(ITRA),ITRA)
            IF(THC0.GT.0) THEN
               X1=MAX(THC0-WIN(ITRA),0.)
               X2=MIN(THC0+WIN(ITRA),THC_LIM)
               IF(X2.GT.X1) THEN
                  IF(NITER.EQ.0) THEN
                     BGR=STFB(ITRA)*AX1(ITRA)/2.*(X2**2-X1**2)
                  ELSE
                     BGR=STFB(ITRA)*AX3(ITRA)/4.*(X2**4-X1**4)
                  ENDIF
                  HRI(ITRA)=MAX(HRI(ITRA)-BGR,0.)
               ENDIF
            ENDIF
         ENDIF
      ENDDO

      RETURN
      END



