!-----------------------------------------------------------------------
! riter - fortran version of 
! M. Staric, jan-2000, feb-2002
!
!-----------------------------------------------------------------------


      SUBROUTINE RITER(IER)

      INTEGER IER

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

      LOGICAL START /.TRUE./
      LOGICAL RITER_ENABLE /.FALSE./
      SAVE START, RITER_ENABLE
      INTEGER MI

      IER=-2
      IF(START) THEN
         START=.FALSE.
         CALL RITER_INIT(IER)
         IF(IER.NE.0) RETURN
         RITER_ENABLE=.TRUE.
      ENDIF
      IF(.NOT.RITER_ENABLE) RETURN

      CALL DB_READ_REFIND(III)
      CALL RESET_RING_TAB(BIN_SIZE)
      CALL GET_TRACKS(IER)
      IF(IER.NE.0) RETURN
      CALL GET_PHOTONS(IER)
      IF(IER.NE.0) RETURN

      DO ITRA=1,NTRA
         CALL TRACK_IN_RICH(ITRA)
         IF(RECO_FLAG(ITRA).EQ.0) GOTO 1
         CALL SIGNAL_ESTIMATE(ITRA)
         IF(RECO_FLAG(ITRA).EQ.0) GOTO 1
         CALL SET_RICWIN(ITRA)
         IF(RECO_FLAG(ITRA).EQ.0) GOTO 1
         DO IFOT=1,NFOT
            IF(UTEZ(IFOT).EQ.0) GOTO 11
            CALL CALC_THCER(RF(1,IFOT),SIG_F(IFOT),TH_CER,FI_CER,SIG,MI)
            IF(SIG.EQ.0) GOTO 11
            UT=UTEZ(IFOT)
            CALL PUT_RING_TAB(ITRA,IFOT,SIG,TH_CER,FI_CER,UT,MI,IER)
            IF(IER.NE.0) RETURN
 11         CONTINUE
         ENDDO
 1       CONTINUE
      ENDDO

      DO ITER=1,NITER
         CALL RING_ITER(WDIFF)
      ENDDO

      CALL BGR_ESTIMATE(NITER)
      CALL LIKELIHOOD(NITER)
      CALL RITER_TO_RTRA

      RETURN
      END


!-----------------------------------------------------------------------
! riter initialisation
! M. staric, jan-2000  changed: oct-2000, mar-2002
! 
!-----------------------------------------------------------------------

      SUBROUTINE RITER_INIT(IER)

      IMPLICIT NONE
      INTEGER IER

#include "arte/Arte.fi"
#include "auto/EVHD.fi"
#include "riter/RIC_PAR.fi"
      DATA N_ZERO /4.2/
      DATA REF_IND /0./
      DATA REFINDEF /1.00137/
      DATA SIG_M16 /0./
      DATA SIG_M4 /0./
      DATA ETA /1./
      DATA THC_LIM /70.E-3/
      DATA BIN_SIZE /.5E-3/
      DATA NITER /10/
      DATA SWIN_SIZE /6./
      DATA DWIN_SIZE /3./
      DATA HISTO_ON  /0/
      DATA COR_TSIG /.TRUE./
      DATA FAC_TSIG /0.,0./
      DATA OFF_TSIG /0.,0./
      DATA TRA_LKH_ON /.TRUE./
      DATA GLO_AL /.TRUE./
      DATA LOC_AL /.TRUE./
#include "riter/RIC_VES.fi"
      DATA Z_IN /8650./
      DATA Z_OUT /11600./
      DATA ENTW_A /2200./, ENTW_B /1400./
#include "arte/Arte.ff"
#include "auto/EVHD.ff"

c--   set some values according to RealData or MC: 
      IF(IEVHD_EXP(1).GT.0) THEN
         IF(SIG_M16.EQ.0) SIG_M16=0.65e-3
         IF(SIG_M4.EQ.0) SIG_M4=1.05e-3
         IF(FAC_TSIG(1).EQ.0) THEN
            FAC_TSIG(1)=2.5
            FAC_TSIG(2)=3.2
            OFF_TSIG(1)=0.
            OFF_TSIG(2)=0.
         ENDIF
      ELSE
         IF(SIG_M16.EQ.0) SIG_M16=0.62e-3
         IF(SIG_M4.EQ.0) SIG_M4=1.04e-3
         IF(FAC_TSIG(1).EQ.0) THEN
            FAC_TSIG(1)=1.7
            FAC_TSIG(2)=1.7
            OFF_TSIG(1)=0.
            OFF_TSIG(2)=0.
         ENDIF
         LOC_AL=.FALSE.
      ENDIF

c--   get what is also needed
      CALL LOAD_GEO(IER)
      IF(IER.NE.0) RETURN
      CALL TO_VIRT_POS

c--   print messages
      IF(GLO_AL) THEN
         CALL MEALLP('-- Global_to_RICH transformation ENABLED')
      ELSE
         CALL MEALLP('-- Global_to_RICH transformation DISABLED')
      ENDIF
      IF(LOC_AL) THEN
         CALL MEALLP('-- Local alignment corrections ENABLED')
      ELSE
         CALL MEALLP('-- Local alignment corrections DISABLED')
      ENDIF
      IF(COR_TSIG) THEN
         CALL MEALLP('-- Track angular sigma corrections ENABLED')
      ELSE
         CALL MEALLP('-- Track angular sigma corrections DISABLED')
      ENDIF
      IF(TRA_LKH_ON) THEN
         CALL MEALLP('-- track likelihood calculation ENABLED')
      ELSE
         CALL MEALLP('-- track likelihood calculation DISABLED')
      ENDIF
      CALL MEALLP('RITER: initialisation finished')
      CALL MEALLP('-------------------------------------------')
      RETURN
      END


!-----------------------------------------------------------------------
! Modification of parameters in /RIC_PAR/ with kuip interface
! M. Staric, jul-2000  changed: oct-2000
!-----------------------------------------------------------------------


      SUBROUTINE RITER_SET_NZERO

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(N_ZERO)
      PRINT*,'RITER: N0 - fig. of merit for RICH [mm^-1] = ',N_ZERO
      RETURN
      END


      SUBROUTINE RITER_SET_REFIND

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(REFINDEF)
      PRINT*,'RITER: default refractive index = ',REFINDEF
      RETURN
      END


      SUBROUTINE RITER_SET_SIGM16

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(SIG_M16)
      PRINT*,'RITER: angular sigma for M16 region [rad] = ',SIG_M16
      RETURN
      END


      SUBROUTINE RITER_SET_SIGM4

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(SIG_M4)
      PRINT*,'RITER: angular sigma for M4 region [rad] = ',SIG_M4
      RETURN
      END


      SUBROUTINE RITER_SET_ETA

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(ETA)
      PRINT*,'RITER: signal normalisation factor = ',ETA
      RETURN
      END


      SUBROUTINE RITER_SET_THCLIM

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(THC_LIM)
      PRINT*,'RITER: limit for Cerenkov angle [rad] = ',THC_LIM
      RETURN
      END


      SUBROUTINE RITER_SET_BIN

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(BIN_SIZE)
      PRINT*,'RITER: bin size used for ring_iter [rad] = ',BIN_SIZE
      RETURN
      END


      SUBROUTINE RITER_SET_NITER

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETI(NITER)
      PRINT*,'RITER: number of iterations = ',NITER
      RETURN
      END


      SUBROUTINE RITER_SET_SWIN

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(SWIN_SIZE)
      PRINT*,'RITER: signal window size in num. of SIGMAs = ',SWIN_SIZE
      RETURN
      END


      SUBROUTINE RITER_SET_DWIN

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(DWIN_SIZE)
      PRINT*,'RITER: wide window size in number of SWINs = ',DWIN_SIZE
      RETURN
      END


      SUBROUTINE RITER_SET_TCORR

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"
      CHARACTER*80 CC
      INTEGER L

      CALL KUGETC(CC,L)
      IF(L.GT.0) THEN
         CALL CUTOL(CC)
         COR_TSIG=CC(1:L).EQ.'on'
      ENDIF      
      IF(COR_TSIG) THEN
         PRINT*,'RITER: corrections for track angular sigma ENABLED'
      ELSE
         PRINT*,'RITER: corrections for track angular sigma DISABLED'
      ENDIF
      RETURN
      END


      SUBROUTINE RITER_SET_TCPAR

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      CALL KUGETR(FAC_TSIG(1))
      CALL KUGETR(FAC_TSIG(2))
      CALL KUGETR(OFF_TSIG(1))
      CALL KUGETR(OFF_TSIG(2))
      PRINT*,'RITER: correction parameters for track angular sigma = ',
     &     FAC_TSIG, OFF_TSIG
      RETURN
      END


      SUBROUTINE RITER_SET_TRALKH

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"
      CHARACTER*80 CC
      INTEGER L

      CALL KUGETC(CC,L)
      IF(L.GT.0) THEN
         CALL CUTOL(CC)
         TRA_LKH_ON=CC(1:L).EQ.'on'
      ENDIF      
      IF(TRA_LKH_ON) THEN
         CALL MEALLP('RITER: track likelihood calculation ENABLED')
      ELSE
         CALL MEALLP('RITER: track likelihood calculation DISBLED')
      ENDIF
      RETURN
      END


      SUBROUTINE RITER_SET_GLO_AL

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"
      CHARACTER*80 CC
      INTEGER L

      CALL KUGETC(CC,L)
      IF(L.GT.0) THEN
         CALL CUTOL(CC)
         GLO_AL=CC(1:L).EQ.'on'
      ENDIF      
      IF(GLO_AL) THEN
         PRINT*,'RITER: Global_to_RICH transformation ENABLED'
      ELSE
         PRINT*,'RITER: Global_to_RICH transformation DISABLED'
      ENDIF
      RETURN
      END


      SUBROUTINE RITER_SET_LOC_AL

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"
      CHARACTER*80 CC
      INTEGER L

      CALL KUGETC(CC,L)
      IF(L.GT.0) THEN
         CALL CUTOL(CC)
         LOC_AL=CC(1:L).EQ.'on'
      ENDIF      
      IF(LOC_AL) THEN
         PRINT*,'RITER: Local alignment corrections ENABLED'
      ELSE
         PRINT*,'RITER: Local alignment corrections DISABLED'
      ENDIF
      RETURN
      END


      SUBROUTINE RITER_SHOWPAR

      IMPLICIT NONE
#include "riter/RIC_PAR.fi"

      PRINT*,'RITER: Current settings of parameters in /RIC_PAR/:'
      PRINT*
      PRINT*,'  NZERO     ',N_ZERO
      PRINT*,'  REFIND    ',REF_IND,' (current)'
      PRINT*,'  REFIND    ',REFINDEF,' (default)'
      PRINT*,'  SIGM16    ',SIG_M16
      PRINT*,'  SIGM4     ',SIG_M4
      PRINT*,'  ETA       ',ETA
      PRINT*,'  THCLIM    ',THC_LIM
      PRINT*,'  BIN       ',BIN_SIZE
      PRINT*,'  NITER     ',NITER
      PRINT*,'  SWIN      ',SWIN_SIZE
      PRINT*,'  DWIN      ',DWIN_SIZE
      IF(GLO_AL) THEN
         PRINT*,'  GLOBAL  on'
      ELSE
         PRINT*,'  GLOBAL  off'
      ENDIF
      IF(LOC_AL) THEN
         PRINT*,'  LOCAL  on'
      ELSE
         PRINT*,'  LOCAL  off'
      ENDIF
      IF(COR_TSIG) THEN
         PRINT*,'  TCORR  on'
      ELSE
         PRINT*,'  TCORR  off'
      ENDIF
      PRINT*,'  TCPAR (scaling) ',FAC_TSIG
      PRINT*,'  TCPAR (offset)  ',OFF_TSIG
      IF(TRA_LKH_ON) THEN
         PRINT*,'  TRALKH  on'
      ELSE
         PRINT*,'  TRALKH  off'
      ENDIF
      PRINT*

      RETURN
      END





