*-----------------------------------------------------------------------
* Analysing tool for Si aerogel proximity focusing RICH prototype
* v1.2  7-jan-2002
* M. Staric, S Korpar IJS Ljubljana
*
*  --> used with NTUPLE/LOOP
*  --> title of histogram ID=9999 should contain path/name of dat file 
*
* manjka: debele lece
*-----------------------------------------------------------------------

      REAL FUNCTION AERORICH()

      IMPLICIT NONE

      INCLUDE ?
      INCLUDE 'AERO_MAP.fi'
      INCLUDE 'AERO_GEO.fi'
      INCLUDE 'AERO_TRC.fi'
      INCLUDE 'AERO_PAR.fi'

      integer MAXHIT
      PARAMETER (MAXHIT=1024)
      REAL RHIT(3,MAXHIT)
      INTEGER TDC(MAXHIT)
      REAL THC(MAXHIT), FIC(MAXHIT)
      INTEGER NHIT
      REAL R0(3), DIR0(3)
      REAL R(3), DIR(3)
      REAL X,Y, THC_CUT, XC, YC, xx, yy
      LOGICAL OK

      LOGICAL START 
      DATA START /.TRUE./
      SAVE START
      INTEGER I, NN
      CHARACTER*100 DAT_FILE
      INTEGER L

      AERORICH=1.
      IF(START) THEN
         START=.FALSE.
         CALL GET_RUNNAME(DAT_FILE,L) ! title of histogram ID=9999
         CALL READ_DAT(DAT_FILE(1:L))
      ENDIF

      CALL TDC_TO_TRACK(R0,DIR0,OK)
      IF(.NOT.OK) RETURN
      CALL TDC_TO_HITS(RHIT,TDC,MAXHIT,NHIT)

      CALL TO_LOCAL(R0,R,0)
      CALL TO_LOCAL(DIR0,DIR,1)
      CALL HITS_TO_ANGLE(R,DIR,RHIT,NHIT,THC,FIC)

      call hfill(400,r0(1),r0(2),1.)
      call hfill(401,dir0(1),dir0(2),1.)
      call hfill(402,dir(1),dir(2),1.)
      call hfill(300,float(nhit),0.,1.)
      nn=0
c      thc_cut=thc_mean-3.*sig_thc
      thc_cut=0.1
      
      xc=r(1)+dir(1)/dir(3)*(-r(3))
      yc=r(2)+dir(2)/dir(3)*(-r(3))
      do i=1,nhit
         call hfill(100,rhit(1,i),rhit(2,i),1.)
         call hfill(200,float(tdc(i)),0.,1.)
         call hfill(500,thc(i),0.,1.)
         if(thc(i).ge.0) then
            x=thc(i)*cos(fic(i))
            y=thc(i)*sin(fic(i))
            call hfill(501,x,y,1.)
            call hfill(502,fic(i),thc(i),1.)
            xx=rhit(1,i)-xc
            yy=rhit(2,i)-yc
            call hfill(503,xx,yy,1.)
         endif
         if(thc(i).gt.thc_cut) nn=nn+1
      enddo
      call hfill(301,float(nn),0.,1.)


      END





*----- decoding --------------------------------------------------------

      SUBROUTINE TDC_TO_TRACK(R0,DIR,OK)

      IMPLICIT NONE
      REAL R0(3), DIR(3)        ! track in global system [out]
      LOGICAL OK                ! if T, track's been reconstructed 

      INCLUDE ?
      INCLUDE 'AERO_TRC.fi'
      INCLUDE 'AERO_PAR.fi'

      INTEGER I, EDGE, TDCDAT, TDCCH, K, II
      INTEGER INDTDC(20)
      INTEGER TRDAT(16,0:1)
      INTEGER ISUM(6), IDIF(6)
      LOGICAL LSUM(6)
      REAL X1, Y1, X2, Y2, R(3), S

c     indeks v arrayu v TDCjih  

      OK=.FALSE.
      IF(NTDC.EQ.4) THEN
         DO I=1,4
            INDTDC(I)=ITELE(20+I)
         ENDDO
      ELSE
         DO I=1,7
            INDTDC(I)=ITELE(20+I)+1
         ENDDO
      ENDIF

c     read in tracker data

      CALL VZERO(TRDAT,32)
      IF (NTDC.EQ.4) THEN
         DO I=25,INDTDC(1)-1
            EDGE=IBITS(ITELE(I),0,1)
            TDCDAT=IBITS(ITELE(I),16,16)
            TDCCH=IBITS(ITELE(I),1,5)
            IF (TDCCH.LT.16) THEN
               K=MAPTDCC(TDCCH)
               IF(K.GT.0) THEN
                  TRDAT(K,EDGE)=TDCDAT
               ENDIF
            ENDIF
         ENDDO
      ELSE
         DO I=INDTDC(1),INDTDC(2)-1
            EDGE=IBITS(IHITS(I),0,1)
            TDCDAT=IBITS(IHITS(I),16,16)
            TDCCH=IBITS(IHITS(I),1,5)
            IF (TDCCH.LT.16) THEN
               K=MAPTDCC(TDCCH)
               IF(K.GT.0) THEN
                  TRDAT(K,EDGE)=TDCDAT
               ENDIF
            ENDIF
         ENDDO
      ENDIF

c     sum, dif, check sum

      OK=.TRUE.
      EDGE=1
      DO I=1,4
         II=2*I-1
         ISUM(I)=TRDAT(II+1,EDGE)+TRDAT(II,EDGE)-2*TRDAT(9+(I-1)/2,EDGE)
         IDIF(I)=TRDAT(II+1,EDGE)-TRDAT(II,EDGE)
         LSUM(I)=ISUM(I).GT.LL(I).AND.ISUM(I).LT.UL(I)
         OK=OK.AND.LSUM(I)
      ENDDO
      IF(.NOT.OK) RETURN

c     track

      Y1=DLY_SLOPE(1)*FLOAT(IDIF(1))+DLY_CONST(1)
      X1=DLY_SLOPE(2)*FLOAT(IDIF(2))+DLY_CONST(2)
      Y2=DLY_SLOPE(3)*FLOAT(IDIF(3))+DLY_CONST(3)
      X2=DLY_SLOPE(4)*FLOAT(IDIF(4))+DLY_CONST(4)

      R0(1)=X1+R_TRCH(1,1)
      R0(2)=Y1+R_TRCH(2,1)
      R0(3)=R_TRCH(3,1)

      R(1)=X2+R_TRCH(1,2)
      R(2)=Y2+R_TRCH(2,2)
      R(3)=R_TRCH(3,2)

      S=0.
      IF(R(3).GT.R0(3)) THEN
         DO I=1,3
            DIR(I)=R(I)-R0(I)
            S=S+DIR(I)**2
         ENDDO
      ELSE
         DO I=1,3
            DIR(I)=R0(I)-R(I)
            S=S+DIR(I)**2
         ENDDO
      ENDIF
      OK=S.GT.0
      IF(.NOT.OK) RETURN
      S=SQRT(S)
      DO I=1,3
         DIR(I)=DIR(I)/S
      ENDDO

      RETURN
      END



      SUBROUTINE TDC_TO_HITS(RHIT,TDC,MAXHIT,NHIT)

      IMPLICIT NONE
      REAL RHIT(3,*)
      INTEGER TDC(*)
      INTEGER MAXHIT
      INTEGER NHIT

      INCLUDE ?
      INCLUDE 'AERO_MAP.fi'
      INCLUDE 'AERO_PAR.fi'

      INTEGER I, ICR, INDEND, EDGE, TDCDAT, TDCCH, ICH, NCH
      INTEGER INDTDC(20)


      NHIT=0

c     indeks v arrayu v TDCjih  

      IF(NTDC.EQ.4) THEN
         DO I=1,4
            INDTDC(I)=ITELE(20+I)
         ENDDO
      ELSE
         DO I=1,7
            INDTDC(I)=ITELE(20+I)+1
         ENDDO
      ENDIF

c     read in photon data, into array HITS

      CALL VZERO(HITS,MAXCHA)
      NCH=0
      IF(NTDC.EQ.4) THEN
         DO ICR=1,4
            IF(ICR.LT.4) THEN
               INDEND=INDTDC(ICR+1)-1
            ELSE
               INDEND=NB
            ENDIF
            DO I=INDTDC(ICR),INDEND
               EDGE=IBITS(ITELE(I),0,1)
               TDCDAT=IBITS(ITELE(I),16,16)
               TDCCH=IBITS(ITELE(I),1,5)
               ICH=32*(ICR-1)+TDCCH+1
               IF(TDCDAT.GT.TLL(ICH).AND.TDCDAT.LT.TUL(ICH)) THEN
                  NCH=MAX(NCH,ICH)
                  IF (TDCDAT.GT.HITS(ICH)) HITS(ICH)=TDCDAT
               ENDIF
            ENDDO
         ENDDO
      ELSE
         DO ICR=1,6
            IF(ICR.LT.6) THEN
               INDEND=INDTDC(ICR+2)-1
            ELSE
               INDEND=NF
            ENDIF
            DO I=INDTDC(ICR+1),INDEND
               EDGE=IBITS(IHITS(I),0,1)
               TDCDAT=IBITS(IHITS(I),16,16)
               TDCCH=IBITS(IHITS(I),1,5)
               ICH=32*(ICR-1)+TDCCH+1
               IF(TDCDAT.GT.TLL(ICH).AND.TDCDAT.LT.TUL(ICH)) THEN
                  IF (TDCDAT.GT.HITS(ICH).AND.EDGE.EQ.1) THEN
                     HITS(ICH)=TDCDAT
                     NCH=MAX(NCH,ICH)
                  ENDIF
               ENDIF
            ENDDO 
         ENDDO
      ENDIF
      
c     photon hits
      
      DO ICH=1,NCH
         IF(ON_CHAN(ICH).AND.HITS(ICH).GT.0) THEN
            IF(NHIT.GE.MAXHIT) RETURN
            NHIT=NHIT+1
            RHIT(1,NHIT)=X_CHAN(ICH)
            RHIT(2,NHIT)=Y_CHAN(ICH)
            RHIT(3,NHIT)=0.
            TDC(NHIT)=HITS(ICH)
         ENDIF
      ENDDO

      RETURN
      END


*----- reconstruction --------------------------------------------------

      SUBROUTINE HITS_TO_ANGLE(R0,DIR,RHIT,NH,THC,FIC)

c     calculation of Cerenkov angles of photon hits
c     THC < 0, if solution doesn't exist
c     all in the local system (RICH)

      IMPLICIT NONE
      REAL R0(3), DIR(3)        ! track [in]
      REAL RHIT(3,*)            ! photon hits [in]
      INTEGER NH                ! number of hits [in]
      REAL THC(*), FIC(*)       ! Cerenkov theta and phi [out]

      INCLUDE 'AERO_GEO.fi'

      REAL R(3), DIRF(3)
      REAL X(3), Y(3), Z(3)     ! axes of local track system (z()=dir())
      REAL DIRCH(3)             ! photon in local track system
      INTEGER I
      REAL DD, DLA, DMEAN
      LOGICAL OK

      IF(NH.LE.0) RETURN
      IF(DIR(3).EQ.0) THEN
         DO I=1,NH
            THC(I)=-2.
         ENDDO
         RETURN
      ENDIF
      R(1)=R0(1)+DIR(1)/DIR(3)*(Z_AERO-R0(3)) ! aerogel exit
      R(2)=R0(2)+DIR(2)/DIR(3)*(Z_AERO-R0(3))
      R(3)=Z_AERO
      DD=D_AERO/DIR(3)
      DLA=DD/ATL_AERO
      IF(DLA.GT.(.01)) THEN
         DMEAN=ATL_AERO/(1.-EXP(-DLA))*(1.-EXP(-DLA)*(1.+DLA))
      ELSE
         DMEAN=DD/2.
      ENDIF 
      DO I=1,3
         R(I)=R(I)-DMEAN*DIR(I) ! mean emission point
      ENDDO
      CALL TRACK_SYSTEM(DIR,X,Y,Z)
      DO I=1,NH
         CALL CER_ANGLE(R,DIR,RHIT(1,I),DIRF,OK)
         IF(OK) THEN
            DIRCH(1)=DIRF(1)*X(1)+DIRF(2)*X(2)+DIRF(3)*X(3)
            DIRCH(2)=DIRF(1)*Y(1)+DIRF(2)*Y(2)+DIRF(3)*Y(3)
            DIRCH(3)=DIRF(1)*Z(1)+DIRF(2)*Z(2)+DIRF(3)*Z(3)
            THC(I)=ACOS(DIRCH(3))
         ELSE
            THC(I)=-1.
         ENDIF
         FIC(I)=0.
         IF(THC(I).GT.0) FIC(I)=ATAN2(DIRCH(2),DIRCH(1))
      ENDDO

      RETURN
      END


      SUBROUTINE CER_ANGLE(R,DIR,RH,DIRF,OK)

      IMPLICIT NONE
      REAL R(3), DIR(3)         ! emission point, track direction [in]
      REAL RH(3)                ! photon hit (at PMT) [in]
      REAL DIRF(3)              ! recon. photon direction inside aerogel [out]
      LOGICAL OK

      INCLUDE 'AERO_GEO.fi'

      REAL RF(3), RHIT(3), THC_OLD, THC
      INTEGER I, MITER
      PARAMETER (MITER=10)
      REAL THCMIN
      PARAMETER (THCMIN=.1E-3)

      RF(1)=R(1)+DIR(1)/DIR(3)*(Z_AERO-R(3))
      RF(2)=R(2)+DIR(2)/DIR(3)*(Z_AERO-R(3))
      RF(3)=Z_AERO
      IF(NO_LENS) THEN
         CALL CER_PHOTON(R,RH,RF,DIRF)
         OK=.TRUE.
      ELSE
         THC_OLD=-1.
         DO I=1,MITER
            CALL LENSES(RF,RH,RHIT,OK)
            CALL CER_PHOTON(R,RHIT,RF,DIRF)
            THC=ACOS(DIRF(3))
            IF(ABS(THC-THC_OLD).LT.THCMIN) GOTO 1
            THC_OLD=THC
         ENDDO
 1       CONTINUE
         DO I=1,3
            RH(I)=RHIT(I)
         ENDDO
      ENDIF

      RETURN
      END



      SUBROUTINE CER_PHOTON(R,RH,RF,DIRF)

      IMPLICIT NONE
      REAL R(3)                 ! photon emission point [in]
      REAL RH(3)                ! photon hit (at lenses) [in]
      REAL RF(3)                ! photon exit point [in/out]
      REAL DIRF(3)              ! photon direction in aerogel [out]

      INCLUDE 'AERO_GEO.fi'

      INTEGER I, ITER, NITER
      PARAMETER (NITER=100)     ! max. number of iterations
      REAL DMIN
      PARAMETER (DMIN=.1)
      REAL DIRL(3), X, Y, S

      DO ITER=1,NITER
         S=0.
         DO I=1,3
            DIRL(I)=RH(I)-RF(I)
            S=S+DIRL(I)**2
         ENDDO
         S=SQRT(S)
         DO I=1,3
            DIRL(I)=DIRL(I)/S
         ENDDO
         DIRF(1)=DIRL(1)/REF_IND
         DIRF(2)=DIRL(2)/REF_IND
         DIRF(3)=SQRT(1-DIRF(1)**2-DIRF(2)**2)
         X=RF(1)
         Y=RF(2)
         RF(1)=R(1)+DIRF(1)/DIRF(3)*(Z_AERO-R(3))
         RF(2)=R(2)+DIRF(2)/DIRF(3)*(Z_AERO-R(3))
         IF((RF(1)-X)**2+(RF(2)-Y)**2.LT.DMIN) RETURN
      ENDDO

      RETURN
      END



      SUBROUTINE LENSES(RF,RH,RHIT,OK)

      IMPLICIT NONE
      REAL RF(3)                ! exit of photon from aerogel [in]
      REAL RH(3)                ! photon hit on PMT [in]
      REAL RHIT(3)              ! photon hit projected to lens surface [out]
      LOGICAL OK                ! T, if inside lens surface

      INCLUDE 'AERO_GEO.fi'

      REAL R0(3), R1(3), R2(3), RX(3)
      INTEGER I

      R0(1)=(IFIX(RH(1)/D_LENS)+.5)*D_LENS
      R0(2)=(IFIX(RH(2)/D_LENS)+.5)*D_LENS
      R0(3)=Z_LENS
      DO I=1,3
         R1(I)=RF(I)-R0(I)
         R2(I)=RH(I)-R0(I)
      ENDDO

      CALL THIN_LENS(R1,R2,F_LENS,RX)
      DO I=1,3
         RHIT(I)=RX(I)+R0(I)
      ENDDO
      OK=ABS(RX(1)).LT.D_LENS/2.AND.ABS(RX(2)).LT.D_LENS/2

      RETURN
      END



      SUBROUTINE THIN_LENS(R1,R2,F,RX)

      REAL R1(3), R2(3)         ! points before/after lens (internal coo.) [in]
      REAL F                    ! focal length
      REAL RX(3)                ! point on the lens surface [out]

      ZZ=R2(3)-R1(3)+R1(3)*R2(3)/F
      RX(1)=(R2(3)*R1(1)-R2(1)*R1(3))/ZZ
      RX(2)=(R2(3)*R1(2)-R2(2)*R1(3))/ZZ
      RX(3)=0.

      RETURN
      END



      SUBROUTINE TO_LOCAL(R0,R,KAJ)

      IMPLICIT NONE
      REAL R0(3)                ! [in]
      REAL R(3)                 ! [out]
      INTEGER KAJ               ! 0=space point, 1=direction vector

      INCLUDE 'AERO_GEO.fi'
      INTEGER I
      REAL Z, X


      IF(KAJ.EQ.0) THEN         ! transformation of space point
         DO I=1,3
            R(I)=R0(I)-R_ROT(I)
         ENDDO
         Z= R(3)*COS_ALFA+R(1)*SIN_ALFA
         X=-R(3)*SIN_ALFA+R(1)*COS_ALFA
         R(1)=X
         R(3)=Z
         DO I=1,3
            R(I)=R(I)+R_RICH(I)
         ENDDO
      ELSE                      ! transformation of direction vector
         R(1)=-R0(3)*SIN_ALFA+R0(1)*COS_ALFA
         R(2)=R0(2)
         R(3)= R0(3)*COS_ALFA+R0(1)*SIN_ALFA
      ENDIF

      RETURN
      END



      SUBROUTINE TRACK_SYSTEM(DIR,X,Y,Z)

      REAL DIR(3)
      REAL X(3), Y(3), Z(3)

      SS=1.-DIR(2)**2
      IF(SS.GT.0) THEN
         SS=SQRT(SS)
         Z(1)=DIR(1)
         Z(2)=DIR(2)
         Z(3)=DIR(3)
         X(1)=Z(3)/SS
         X(2)=0.
         X(3)=-Z(1)/SS
         Y(1)=Z(2)*X(3)
         Y(2)=Z(3)*X(1)-Z(1)*X(3)
         Y(3)=-Z(2)*X(1)
      ELSE
         Z(1)=0.
         Z(2)=1.
         Z(3)=0.
         X(1)=0.
         X(2)=0.
         X(3)=1.
         Y(1)=1.
         Y(2)=0.
         Y(3)=0.
      ENDIF
      RETURN
      END


*----- reading mappings, geometry etc. ---------------------------------

      SUBROUTINE READ_DAT(FILE_NAME)

      IMPLICIT NONE
      CHARACTER*(*) FILE_NAME
      INCLUDE 'AERO_MAP.fi'
      INCLUDE 'AERO_GEO.fi'
      INCLUDE 'AERO_TRC.fi'
      INCLUDE 'AERO_PAR.fi'

      INTEGER NGEO, II, I, K, ITLL, ITUL, IO, KK, L
      integer MAXGEO
      PARAMETER (MAXGEO=128)
      REAL XX, GEO(MAXGEO), PI, DZ_AERO, DEB_LECE
      CHARACTER*80 TXT, CC
      CHARACTER*100 MAPFILE, HOTFILE, TDCFILE, PATH
      INTEGER LMF, LHF, LTF, LPA
      
      geo(45)=6
      NGEO=0
      LMF=0
      LHF=0
      LPA=INDEX(FILE_NAME,'/run')
      IF(LPA.GT.0) PATH=FILE_NAME(1:LPA)

      PRINT*,FILE_NAME
      OPEN(1,FILE=FILE_NAME,STATUS='OLD')

 1    CONTINUE
      READ(1,'(A)',ERR=1,END=99) TXT
      IF(INDEX(TXT,'map=').NE.0) THEN
         II=INDEX(TXT,'=')+1
         CC=TXT(II:)
         CALL STRIP_OFF(CC,L)
         IF(LPA.GT.0) THEN
            MAPFILE=PATH(1:LPA)//CC(1:L)
         ELSE
            MAPFILE=CC(1:L)
         ENDIF
         LMF=INDEX(MAPFILE,'.map')+3
         GOTO 1
      ELSE IF(INDEX(TXT,'tdc=').NE.0) THEN
         II=INDEX(TXT,'=')+1
         CC=TXT(II:)
         CALL STRIP_OFF(CC,L)
         IF(LPA.GT.0) THEN
            TDCFILE=PATH(1:LPA)//CC(1:L)
         ELSE
            TDCFILE=CC(1:L)
         ENDIF
         LTF=INDEX(TDCFILE,'.map')+3
         GOTO 1
      ELSE IF(INDEX(TXT,'hot=').NE.0) THEN
         II=INDEX(TXT,'=')+1
         CC=TXT(II:)
         CALL STRIP_OFF(CC,L)
         IF(LPA.GT.0) THEN
            HOTFILE=PATH(1:LPA)//CC(1:L)
         ELSE
            HOTFILE=CC(1:L)
         ENDIF
         LHF=INDEX(HOTFILE,'.hot')+3
         GOTO 1
      ENDIF
      READ(TXT,*,ERR=2,END=2) II,XX
      GOTO 3
 2    READ(TXT,*,ERR=1,END=1) II,KK
      XX=KK
 3    CONTINUE
      IF(II.GT.0.AND.II.LE.MAXGEO) THEN
         NGEO=NGEO+1
         GEO(II)=XX
      ELSE
         PRINT*,'dat file: too many parameters, i=',ii
      ENDIF
      GOTO 1

 99   CONTINUE
      CLOSE(1)
      PRINT*,'--> number of parameters read: ',NGEO

      PI=4.*ATAN(1.)

c     tracker chambers

      NTRCHAM=2
      DO I=1,4
         DLY_SLOPE(I)=GEO(I)
         DLY_CONST(I)=GEO(I+4)
         LL(I)=GEO(I+8)
         UL(I)=GEO(I+12)
      ENDDO
      R_TRCH(1,1)=0.
      R_TRCH(2,1)=0.
      R_TRCH(3,1)=GEO(17)
      R_TRCH(1,2)=0.
      R_TRCH(2,2)=0.
      R_TRCH(3,2)=GEO(18)

c     rich cut levels

      DO I=0,5
         ITLL=GEO(I+29)
         ITUL=GEO(I+35)
         DO K=1,32
            II=I*32+K
            TLL(II)=ITLL
            TUL(II)=ITUL
         ENDDO
      ENDDO

c     rich geo

      R_ROT(1)=GEO(22)
      R_ROT(2)=GEO(23)
      R_ROT(3)=GEO(20)
      COS_ALFA=COS(GEO(24)*PI/180.)
      SIN_ALFA=SIN(GEO(24)*PI/180.)
      R_RICH(1)=90.
      R_RICH(2)=90.
      R_RICH(3)=0.

      REF_IND=GEO(25)
      ATL_AERO=GEO(41)
      D_AERO=GEO(42)
      DZ_AERO=GEO(43)
      Z_AERO=(GEO(19)-GEO(20))*COS_ALFA+DZ_AERO
      Z_MEP=Z_AERO-D_AERO/2.    ! not used

      NO_LENS=GEO(26).EQ.0
      Z_LENS=-GEO(44)
      F_LENS=50.
      D_LENS=30.
      RIND_LENS=1.5
      DEB_LECE=12.
      RC_LENS(1)=(RIND_LENS-1.)*2.*F_LENS
      RC_LENS(2)=RC_LENS(1)
      ZC_LENS(1)=Z_LENS-(RC_LENS(1)-DEB_LECE/2.)
      ZC_LENS(2)=Z_LENS+(RC_LENS(2)-DEB_LECE/2.)
      R_LENS=SQRT(RC_LENS(1)**2-(RC_LENS(1)-DEB_LECE/2.)**2)

c     other parameters

      NTDC=GEO(45)
      THC_MEAN=GEO(27)
      SIG_THC=GEO(28)

c     other configure files

      IF(LMF.GT.0) THEN
         CALL READ_MAP(MAPFILE(1:LMF))
      ELSE
         PRINT*,'=== No map file in dat file: channel mapping ??!!'
      ENDIF
      IF(LTF.GT.0) THEN
         CALL READ_TDCMAP(TDCFILE(1:LTF))
      ELSE
         PRINT*,'=== No tdcmap file in dat file: channel mapping ??!!'
      ENDIF
      IF(LHF.GT.0) CALL READ_HOT(HOTFILE(1:LHF))

      RETURN
      END



      SUBROUTINE READ_MAP(FILE_NAME)

      IMPLICIT NONE
      CHARACTER*(*) FILE_NAME
      INCLUDE 'AERO_MAP.fi'
      INTEGER I
      REAL X,Y

      DO I=1,MAXCHA
         ON_CHAN(I)=.FALSE.
      ENDDO
      NCHAN=0

      PRINT*,FILE_NAME
      OPEN(1,FILE=FILE_NAME,STATUS='OLD')
 1    CONTINUE
      READ(1,*,ERR=1,END=99) X,Y
      IF(X.GT.0.AND.Y.GT.0) THEN
         IF(NCHAN.GE.MAXCHA) GOTO 99
         NCHAN=NCHAN+1
         X_CHAN(NCHAN)=X
         Y_CHAN(NCHAN)=Y
         ON_CHAN(NCHAN)=.TRUE.
      ENDIF
      GOTO 1

 99   CONTINUE
      CLOSE(1)
      PRINT*,'--> number of connected channels: ',NCHAN

      RETURN
      END



      SUBROUTINE READ_TDCMAP(FILE_NAME)

      IMPLICIT NONE
      CHARACTER*(*) FILE_NAME
      INCLUDE 'AERO_TRC.fi'
      INTEGER I, K, NCH

      NCH=0
      CALL VZERO(MAPTDCC,32)
      PRINT*,FILE_NAME
      OPEN(1,FILE=FILE_NAME,STATUS='OLD')
 1    CONTINUE
      READ(1,*,ERR=1,END=99) I,K
      IF(I.GE.0.AND.I.LT.32) THEN
         MAPTDCC(I)=K
         NCH=NCH+1
      ENDIF
      GOTO 1

 99   CONTINUE
      CLOSE(1)
      PRINT*,'--> number of mapped TDC: ',NCH

      RETURN
      END



      SUBROUTINE READ_HOT(FILE_NAME)
      
      IMPLICIT NONE
      CHARACTER*(*) FILE_NAME
      INCLUDE 'AERO_MAP.fi'
      INTEGER NHOT, ICH

      NHOT=0
      PRINT*,FILE_NAME
      OPEN(1,FILE=FILE_NAME,STATUS='OLD')
 1    CONTINUE
      READ(1,*,ERR=1,END=99) ICH
      IF(ICH.LT.1.OR.ICH.GT.NCHAN) GOTO 1
      NHOT=NHOT+1
      ON_CHAN(ICH)=.FALSE.
      GOTO 1

 99   CONTINUE
      CLOSE(1)
      PRINT*,'--> number of channels disabled: ',NHOT

      RETURN
      END



*----- misc. -----------------------------------------------------------


      SUBROUTINE STRIP_OFF(TT,LENG)
 
      CHARACTER*(*) TT
      INTEGER LENG
      LOGICAL Q
      INTEGER LL, ICHR

      LL=LEN(TT)
      DO I=1,LL
         IF(TT(1:1).EQ.' ') THEN
            TT=TT(2:LL)
         ENDIF
      ENDDO
 
C     LENG=INDEX(TT,CHAR(13))-1 ! interpreter ne pozna funkcije CHAR
C     IF(LENG.GE.0) RETURN

      LENG=LL
      DO WHILE(LENG.GT.0.AND.TT(LENG:LENG).EQ.' ')
         LENG=LENG-1
      ENDDO

      RETURN
      END



      SUBROUTINE GET_RUNNAME(RUNNAME,L)

      CHARACTER*(*) RUNNAME
      INTEGER L

      CALL HGIVE(9999,RUNNAME,NX,X1,X2,NY,Y1,Y2,NWT,LOC)
      CALL STRIP_OFF(RUNNAME,L)

      RETURN
      END

