      PROGRAM READBIN

      PARAMETER ( MXUSBN = 100 )
      PARAMETER ( MXDUMM = 4500000 )
      PARAMETER ( PI = 3.141592653589793D0 )
      LOGICAL LUSBIN, LEVTBN, LNTZER, LUSEVT, LUSTKB, LTRKBN, LSNGBN
      CHARACTER*80 FILE1,RUNTIT
      CHARACTER*32 RUNTIM
      CHARACTER*10 TITUSB(MXUSBN)
      COMMON /UUSRBN/ XLOW  (MXUSBN), XHIGH (MXUSBN), YLOW  (MXUSBN),
     &                YHIGH (MXUSBN), ZLOW  (MXUSBN), ZHIGH (MXUSBN),
     &                DXUSBN(MXUSBN), DYUSBN(MXUSBN), DZUSBN(MXUSBN),
     &                TCUSBN(MXUSBN), BKUSBN(MXUSBN), B2USBN(MXUSBN),
     &                XAXUSB(MXUSBN), YAXUSB(MXUSBN),
     &                NXBIN (MXUSBN), NYBIN (MXUSBN), NZBIN (MXUSBN),
     &                ITUSBN(MXUSBN), IDUSBN(MXUSBN), KBUSBN(MXUSBN),
     &                IPUSBN(MXUSBN), KRTNBN(MXUSBN), LEVTBN(MXUSBN),
     &                LNTZER(MXUSBN), LSNGBN(MXUSBN), LTRKBN(MXUSBN),
     &                NUSRBN, LUSBIN, LUSEVT, LUSTKB
      INTEGER*4 NHITS, IHELP(MXDUMM),IREG(MXDUMM),ICELL(MXDUMM)
     $     ,IETA(MXDUMM) 
      REAL*4 GMHELP(MXDUMM)
      REAL*8 XFF,YFF,EDEP1,EDEP2,EDEP3
**
      INTEGER*4 NDIM (3), IDIM (3)
**
      CALL treeinit();
*
      iev = 0
      call histini(iev)
      IEVRE = 0
 1000 CONTINUE
      WRITE(*,*)' Type the input file:'
      FILE1=' '
      READ (*,'(A)')FILE1
      IF ( FILE1 .EQ. ' ' ) GO TO 2000
      OPEN (UNIT=1,FILE=FILE1,STATUS='OLD',FORM='UNFORMATTED',
     &      ERR= 200)
      GO TO 300
 200  CONTINUE
      WRITE(*,*)' Error opening primary/secondary file!'
      GO TO 2000
 300  CONTINUE

      IREAD=0
      READ(1)RUNTIT,RUNTIM
      WRITE(*,*) RUNTIT
      WRITE(*,*) RUNTIM
      IREAD=IREAD+1
      IB0=0
      NBTOT=0
      DO 1201 IB=IB0+1,1000
         READ (1,ERR=1205,END=1500) NB, TITUSB(IB), ITUSBN(IB),
     &                   IDUSBN(IB),
     &                   XLOW(IB),XHIGH(IB),NXBIN(IB),
     &                   DXUSBN(IB),YLOW(IB),
     &                   YHIGH(IB),NYBIN(IB),DYUSBN(IB),
     &                   ZLOW(IB),ZHIGH(IB),NZBIN(IB),
     &                   DZUSBN(IB),LNTZER(IB),BKUSBN(IB),
     &                   B2USBN(IB),TCUSBN(IB)
         WRITE(*,*)  NB, TITUSB(IB), ITUSBN(IB),
     &                   IDUSBN(IB),
     &                   XLOW(IB),XHIGH(IB),NXBIN(IB),
     &                   DXUSBN(IB),YLOW(IB),
     &                   YHIGH(IB),NYBIN(IB),DYUSBN(IB),
     &                   ZLOW(IB),ZHIGH(IB),NZBIN(IB),
     &                   DZUSBN(IB),LNTZER(IB),BKUSBN(IB),
     &                   B2USBN(IB),TCUSBN(IB)
         IREAD=IREAD+1
         NBOLD=NB
         NBTOT=IB
 1201 CONTINUE
 1205 CONTINUE
      REWIND 1
      DO I=1,IREAD
        READ(1)
      ENDDO
      DO IEV=1,100000000
C         WRITE(*,*) "Now IEV = ",IEV
         ETOT   = 0.0
         DO IB=1,NBTOT
            READ(1,END=1500)MB,IEVD,WEI
            NB = IB
            IF ( LNTZER(NB) ) THEN
               READ (1, END=1500) NHITS,(IHELP(J),GMHELP(J),J=1,NHITS)
            ELSE
               READ (1, END=1500)
     &              (GMHELP(J),J=1,NXBIN(NB)*NYBIN(NB)*NZBIN(NB))
               NHITS = NXBIN(NB)*NYBIN(NB)*NZBIN(NB)
C               WRITE(*,*) 'NHITS = ',NHITS
               DO J = 1, NHITS
                  IHELP(J) = J
               END DO
            END IF

C            write(*,*) ( GMHELP(K), K=1,NHITS)
            DO J=1,NHITS
               NDIM (1) = NXBIN (NB)
               NDIM (2) = NYBIN (NB)
               NDIM (3) = NZBIN (NB)
               CALL ARRNDX ( IHELP(J), 3, NDIM, IDIM )
               IF (GMHELP(J).GT.0.) THEN
                  ETOT = ETOT + gmhelp(j)
               ENDIF
            END DO
            EDEP1 = gmhelp(1)
            EDEP2 = gmhelp(2)
            EDEP3 = gmhelp(3)
            CALL treefill(EDEP1,EDEP2,EDEP3)
C            call histdel()
         ENDDO
         IEVRE = IEVRE + 1
C         WRITE (*,*) 'EV, ETOT: ',IEVRE, ETOT
      END DO
 1500 CONTINUE
      WRITE (*,*)' IEVD:',IEVD
      CLOSE(1)
      GOTO 1000
 2000 CONTINUE
 2200 CONTINUE
      WRITE(*,*)
      WRITE(*,*)' IEVRE :',IEVRE
      CALL fileclose()
      STOP
      END

