!----------------------------------------------------------------------
! get photons from HITB
! M. Staric, mar-2000
!
!----------------------------------------------------------------------


      SUBROUTINE GET_PHOTONS(IER)

      IMPLICIT NONE
      INTEGER IER

#include "riter/RIC_PHOT.fi"

#include "arte/Arte.fi"
#include "auto/GEDE.fi"
#include "auto/HITB.fi"
#include "auto/DIGB.fi"

      INTEGER IR, IR1, IR2, INDX, IRI, ID
      LOGICAL OK
      REAL DX
      INTEGER IER1, IER2
      SAVE IER1, IER2

#include "arte/Arte.ff"
#include "auto/GEDE.ff"
#include "auto/HITB.ff"
#include "auto/DIGB.ff"

      IR1=1
      IR2=0
      DO IR=1,NGEDE()
         IF(IGEDE_CMP(IR).EQ.5) THEN
            IR1=IGEDE_HIT1(IR)
            IR2=IGEDE_HITL(IR)
            IR2=MIN(IR2,NHITB())
            IF(IHITB_GEDE(IR2).NE.5) THEN
               IR2=IR2-1
            ENDIF
            GOTO 1
         ENDIF
      ENDDO
 1    CONTINUE

      NFOT=0
      DO IR=IR1,IR2
         INDX=PHITB_DIGB(IR)
         IF(INDX.GT.0) THEN
            IRI=MHITB_DIGB(INDX)
            ID=IDIGB_ID(IRI)
            CALL ADDTO_RIC_PHOT(ID,OK)
            IF(.NOT.OK) CALL MEPRIC(IER2,10,
     &           'RITER: addto_ric_phot_: status=FALSE')
         ELSE
            CALL MEPRIC(IER1,10,'RITER: get_photons_ - no DIGB')
         ENDIF
      ENDDO
      IER=MAX(0,NFOT-MAXFOT)
      NFOT=MIN(NFOT,MAXFOT)
 
      RETURN
      END


      SUBROUTINE ADDTO_RIC_PHOT(ID,OK)

      INTEGER ID                ! coded channel (ID from DIGB)
      LOGICAL OK

#include "riter/RIC_PHOT.fi"
#include "riter/RIC_PAR.fi"
#include "riter/RIC_CHA.fi"
#include "riter/RIC_FED.fi"

      IPCH=MOD(ID,100)
      ID=ID/100
      ICOL=MOD(ID,100)
      ID=ID/100
      IROW=MOD(ID,100)
      ISM=ID/100
      CALL PREVERI(IR,IPCH,ICOL,IROW,ISM,OK)
      IF(.NOT.OK) RETURN
      OK=.FALSE.
      IF(ISM.GT.6) THEN
         ISM=20-ISM
         ICOL=11-ICOL
         IROW=31-IROW
      ENDIF
      ITIP=PMTYPE(IROW/2,ICOL/2,ISM)
      IF(ITIP.EQ.0) THEN
         IF(ISM.LT.7) IPCH=15-IPCH
         IX=MOD(IPCH,4)
         IY=IPCH/4
         SIG2=SIG_M16
      ELSE IF(ITIP.EQ.1) THEN
         IF(ISM.LT.7) IPCH=3-IPCH
         IX=MOD(IPCH,2)*2
         IY=(IPCH/2)*2
         SIG2=SIG_M4
      ELSE
cc         print*,'ITIP:',itip,' at ',irow/2,icol/2,ism
         RETURN
      ENDIF
      ICOL=ICOL*4+IX
      IROW=IROW*4+IY
      ICH=CH_DET(IROW,ICOL,ISM)
      IF(ICH.LT.0) THEN
cc         print*,'ICH:',ich
         RETURN
      ENDIF
      NFOT=NFOT+1
      IF(NFOT.GT.MAXFOT) RETURN
      DX=(ITIP+1)/2.
      ALFA=(ICOL+DX)/48.
      BETA=(IROW+DX)/128.
      CALL DET_RHIT(ALFA,BETA,ISM,RF(1,NFOT))
      SIG_F(NFOT)=SIG2
      UTEZ(NFOT)=1.
      FOT_CH(NFOT)=ICH
      OK=.TRUE.

      RETURN
      END


      SUBROUTINE PREVERI(IR,IPCH,ICOL,IROW,ISM,OK)

      LOGICAL OK, DATAERR

      OK=.NOT.DATAERR(IPCH,0,15,IR,'IPCH')
      OK=OK.AND..NOT.DATAERR(ICOL,0,11,IR,'ICOL')
      OK=OK.AND..NOT.DATAERR(IROW,0,31,IR,'IROW')
      OK=OK.AND..NOT.DATAERR(ISM,0,13,IR,'ISM')
      RETURN
      END




