!-----------------------------------------------------------------------
! iterative ring search
! M. Staric, avg-1999, rev. jan-2000, jan-2002
! 
! RESET_RING_TAB(BIN_SIZE)
! PUT_RING_TAB(ITRA,IFOT,SIG,THC,FIC,UTEZ,IMIR,IER)
! CORRECT_RING_TAB(K,THC,SIG)
! RING_ITER(WDIFF)
! GET_RING_TAB_ROWS(NT)
! GET_RING_TAB(K,ITRA,IFOT,SIG,THC,FIC,WT)
! GET_RING_TAB_IMIR(K,IMIR)
! GET_RING_TAB_MP(K,ISPH,IPLM,OK)
!-----------------------------------------------------------------------

      SUBROUTINE RING_ITER(WDIFF)

! iteracijska procedura

      REAL WDIFF                ! max(abs(w_old-w_new))

      INTEGER ITRA		! stevilka delca
      INTEGER IFOT		! stevilka fotona
      REAL THC, FIC             ! cerenkov radij fotona, fi cerenkov
      REAL SIG                  ! celotna sigma (foton+track)
      REAL UTEZ                 ! utez fotona (povprecno stevilo, effi...)
      INTEGER IMIR              ! mirror pairs encoded
      REAL BIN_SIZE             ! bin size
      INTEGER NT                ! stevec tab. radijev
      INTEGER IER               ! 0=ok
      REAL WT                   ! rezultat iteracije
      INTEGER IPLM, ISPH
      LOGICAL OK

! tabela radijev in utezi 
      PARAMETER (NTB=100000)
      INTEGER IRICH(8,NTB)      ! (IR,ITRA,IFOT,SIG,UTEZ,THC,FIC,W)
      REAL RICH(8,NTB)          !   1   2   3    4    5    6   7  8
      EQUIVALENCE (RICH,IRICH)
      SAVE RICH
      INTEGER MIRRORS(NTB)
      SAVE MIRRORS
! stevci
      INTEGER N /0/, NH /0/, NF /0/
      SAVE N, NH, NF
! bin size
      REAL BIN /.5E-3/
      SAVE BIN
! histogrami
      PARAMETER (NBIN=256)
      PARAMETER (MAXTRA=1024)
      PARAMETER (MAXFOT=32000)
      REAL PF(0:NBIN-1,MAXTRA)  ! histogram (radij,delec)
      REAL SPF(MAXFOT)		! vsota verjetnosti po delcih za i-ti foton
! ostalo
      INTEGER*4 IMI
      INTEGER*2 JMI(2)
      EQUIVALENCE (IMI,JMI)

! filanje PF
      CALL VZERO(PF,NBIN*NH)
      DO I=1,N
         IR=IRICH(1,I)
         ITRACK=IRICH(2,I)
         PF(IR,ITRACK)=PF(IR,ITRACK)+RICH(8,I)
      ENDDO
! filanje SPF
      CALL VZERO(SPF,NF)
      DO I=1,N
         IR=IRICH(1,I)
         ITRACK=IRICH(2,I)
         IFOTON=IRICH(3,I)
         SPF(IFOTON)=SPF(IFOTON)+PF(IR,ITRACK)
      ENDDO
! nove utezi
      WDIFF=0.
      DO I=1,N
         IR=IRICH(1,I)
         ITRACK=IRICH(2,I)
         IFOTON=IRICH(3,I)
         UT=RICH(5,I)
         W_OLD=RICH(8,I)
         IF(SPF(IFOTON).EQ.0) THEN
            W=0.
         ELSE
            W=PF(IR,ITRACK)/SPF(IFOTON)*UT
         ENDIF
         WDIFF=MAX(WDIFF,ABS(W-W_OLD))
         RICH(8,I)=W
      ENDDO
      RETURN


      ENTRY RESET_RING_TAB(BIN_SIZE)

      BIN=BIN_SIZE
      N=0
      NH=0
      NF=0
      RETURN


      ENTRY PUT_RING_TAB(ITRA,IFOT,SIG,THC,FIC,UTEZ,IMIR,IER)

! ob vsakem klicu vneses nov par delec-foton v tabelo
! tabelo resetiras z reset_ring_tab

      IER=0
      IF(UTEZ.LE.0) RETURN
      IR=THC/BIN
      IF(IR.GT.NBIN-1) RETURN
      N=N+1	
      IF(N.GT.NTB) THEN
         IER=1
         RETURN
      ENDIF
      IF(ITRA.GT.MAXTRA) THEN
         IER=2
         RETURN
      ENDIF
      NH=MAX(NH,ITRA)
      IF(IFOT.GT.MAXFOT) THEN
         IER=3
         RETURN
      ENDIF
      NF=MAX(NF,IFOT)
      IRICH(1,N)=IR
      IRICH(2,N)=ITRA
      IRICH(3,N)=IFOT
      RICH(4,N)=SIG
      RICH(5,N)=UTEZ
      RICH(6,N)=THC
      RICH(7,N)=FIC
      RICH(8,N)=1.
      MIRRORS(N)=IMIR
      RETURN


      ENTRY CORRECT_RING_TAB(K,THC,SIG)

! write corrected cerenkov angle and sigma to table row K

      IR=THC/BIN
      IF(IR.GT.NBIN-1) RETURN
      IRICH(1,K)=IR
      RICH(4,K)=SIG
      RICH(6,K)=THC
      RETURN


      ENTRY GET_RING_TAB_ROWS(NT)

      NT=MIN(N,NTB)
      RETURN


      ENTRY GET_RING_TAB(K,ITRA,IFOT,SIG,THC,FIC,WT)

      ITRA=IRICH(2,K)
      IFOT=IRICH(3,K)
      SIG=RICH(4,K)
      THC=RICH(6,K)
      FIC=RICH(7,K)
      WT=RICH(8,K)
      RETURN


      ENTRY GET_RING_TAB_IMIR(K,IMIR)

      IMIR=MIRRORS(K)
      RETURN

      ENTRY GET_RING_TAB_MP(K,ISPH,IPLM,OK)

      IMI=MIRRORS(K)
      OK=JMI(1).EQ.JMI(2)
      IPLM=IAND(IMI,'FF'x)-1
      ISPH=IAND(ISHFT(IMI,-8),'FF'x)-1
      OK=OK.AND.IPLM.GE.0.AND.ISFM.GE.0

      RETURN

      END






