C------------------------------------------------
      REAL FUNCTION RDTOFCAL(calname,cnlen)
C------------------------------------------------

      include 'input_tof.txt'

c----- HBOOK
      INTEGER HMEM
c      REAL RDTOFCAL
      parameter (NWPAWC=8500000)
      common/PAWC/HMEM(NWPAWC)

      parameter (ntp_tofcalib=60)
c------------------------------------------------------------------------
c     local variables
c------------------------------------------------------------------------
      character*250 calname
      character*250 calname1
c      character ch
c      character*2 ch2 
      integer cnlen
c------------------------------------------------------------------

      COMMON/QUEST/IQUEST(100)

C
C------------------------------------------------------------------
C

      INTEGER lrec
      INTEGER istat, ierr, icycle

c      REAL x_coor_lin11(8,2),y_coor_lin12(6,2)
      PARAMETER (lrec=4096)

C-------------------------------------------------------------------------
c
c     HBOOK initialization
c
c------------------------------------------------------------------------

      RDTOFCAL = 0.
      call HLIMIT(NWPAWC)

c------------------------------------------------------------------------
c---------------    open ToF calib rz file 
c------------------------------------------------------------------------

c       do ii=1,250
c         ch=calname(ii:ii)
c         if (ch.eq.'.') goto 100
c       enddo   
c100   continue
c      calname1 = calname(1:ii)//'rz'

c      do ii=1,250
cc         ch=calname(ii:ii)
c         ch2=calname(ii:ii+1)
cc         write(*,*) ii,ch
cc         if (ch.eq.'.') goto 100
c         if (ch2.eq.'rz') goto 100
c       enddo
c100   continue
c      calname1 = calname(1:ii)//'rz'
c       calname1 = calname(1:ii+1)

      calname1 = calname(1:cnlen)



 
       CALL HROPEN(59,'TOFK1131',calname1,'QP',4096,istat)

        if (istat.ne.0) then  ! check if HROPEN was OK
              write(*,*) 'Can''t open correct ToF calibration File !!!'
        stop
        endif

        call HRIN(ntp_tofcalib,9999,0)

        call HBNAME(ntp_tofcalib,' ',0,'$CLEAR')

        call HBNAME(ntp_tofcalib,'TOFK1131',k_s11s31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK1132',k_s11s32,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK1231',k_s12s31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK1232',k_s12s32,'$SET')

        call HBNAME(ntp_tofcalib,'TOFK2131',k_s21s31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK2132',k_s21s32,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK2231',k_s22s31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK2232',k_s22s32,'$SET')

        call HBNAME(ntp_tofcalib,'TOFK1121',k_s11s21,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK1122',k_s11s22,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK1221',k_s12s21,'$SET')
        call HBNAME(ntp_tofcalib,'TOFK1222',k_s12s22,'$SET')

C--------------

        call HBNAME(ntp_tofcalib,'TOFLIN11',y_coor_lin11,'$SET')
        call HBNAME(ntp_tofcalib,'TOFLIN12',x_coor_lin12,'$SET')
c        call HBNAME(ntp_tofcalib,'TOFLIN11',x_coor_lin11,'$SET')
c        call HBNAME(ntp_tofcalib,'TOFLIN12',y_coor_lin12,'$SET')
        call HBNAME(ntp_tofcalib,'TOFLIN21',x_coor_lin21,'$SET')
        call HBNAME(ntp_tofcalib,'TOFLIN22',y_coor_lin22,'$SET')
        call HBNAME(ntp_tofcalib,'TOFLIN31',y_coor_lin31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFLIN32',x_coor_lin32,'$SET')

        call HBNAME(ntp_tofcalib,'TOFTW11',tw11,'$SET')
        call HBNAME(ntp_tofcalib,'TOFTW12',tw12,'$SET')
        call HBNAME(ntp_tofcalib,'TOFTW21',tw21,'$SET')
        call HBNAME(ntp_tofcalib,'TOFTW22',tw22,'$SET')
        call HBNAME(ntp_tofcalib,'TOFTW31',tw31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFTW32',tw32,'$SET')

        call HBNAME(ntp_tofcalib,'TOFADC11',adcx11,'$SET')
        call HBNAME(ntp_tofcalib,'TOFADC12',adcx12,'$SET')
        call HBNAME(ntp_tofcalib,'TOFADC21',adcx21,'$SET')
        call HBNAME(ntp_tofcalib,'TOFADC22',adcx22,'$SET')
        call HBNAME(ntp_tofcalib,'TOFADC31',adcx31,'$SET')
        call HBNAME(ntp_tofcalib,'TOFADC32',adcx32,'$SET')

c        call HPRNTU(ntp_tofcalib)

        call HNOENT(ntp_tofcalib,iemax_cal)

        do iev=1,iemax_cal
           call HGNT(ntp_tofcalib,iev,ierr) !reads an event
c           print *,' ierr ',ierr
        enddo

        call hrout(ntp_tofcalib,icycle,' ')
        call hrend('TOFK1131')
        close(0)

        return
        end

