* PROGRAM RDEVDT
      CHARACTER*80 RUNTIT, FILNAM
      CHARACTER*32 RUNTIM
      DIMENSION ISCORE(4), ENDIST(12), REGSCO(5000,4)


      WRITE(*,*) 'Name of the EVENTDAT binary file?'
      READ(*,'(A)') FILNAM
      IB = INDEX(FILNAM,' ')

      OPEN(UNIT = 7, FORM = 'UNFORMATTED', FILE = FILNAM(1:IB-1), 
     &     STATUS = 'OLD')
      OPEN(UNIT = 8, FORM = 'FORMATTED', FILE = FILNAM(1:IB-1)//'.txt', 
     &     STATUS = 'NEW')
      
*     Once, at the beginning of the run:
      READ(7) RUNTIT, RUNTIM, NREGS, NSCO, (ISCORE(IS), IS = 1, NSCO)
      WRITE(8,'(A80)') RUNTIT
      WRITE(8,'(A32)') RUNTIM
      WRITE(8,'(A,I6,5X,A,I4)') 'Number of regions: ', NREGS,
     &     ' Number of scored quantities: ', NSCO
      WRITE(8,'(A,4I6)') 'The scored quantities are: ',
     &     (ISCORE(IS), IS = 1, NSCO)
      
      
*     Loop on each primary particle:
 100  CONTINUE
      WRITE(8,*)
      READ(7,END=300) NCASE, WEIPRU, ENETOT
      WRITE(8,'(A,I10,1P,2G12.4)') 'NCASE, WEIPRU, ENETOT: ',
     &     NCASE, WEIPRU, ENETOT
      READ(7) (ENDIST(IE), IE = 1, 12)
      WRITE(8,'(A)') 'ENDIST: '
      DO 400 IE = 1, 12, 2
         WRITE(8,'(2(I5,5X,1P,G12.4))') IE,ENDIST(IE),IE+1,ENDIST(IE+1)
 400  CONTINUE
      DO 200 ISC = 1, NSCO
         READ(7) IISC, ISCORE(ISC)
         
*     IISC is redundant, must be equal to ISC
         IF(IISC .NE. ISC) STOP 'Wrong sequence'
         WRITE(8,'(A,I2,A,I3,A)')
     &        'Quantity n. ',ISC, ' (',ISCORE(ISC),'):'
         
         READ(7) (REGSCO(IR,ISC), IR = 1, NREGS)
         WRITE(8,*) 'Scoring per region:'
         DO 500 IR = 1, NREGS
            WRITE(8,'(I7,3X,1P,G12.4)') IR, REGSCO(IR,ISC)
 500     CONTINUE
 200  CONTINUE
      
      READ(7) NDUM, DUM1, DUM2
      IF (DUM1 .LT. 0.) THEN
*        DUM1 < 0 is used to signal that seeds follow
         READ(7) ISEED1, ISEED2, SEED1, SEED2, SOPP1, SOPP2
         WRITE(8,*) ISEED1, ISEED2, SEED1, SEED2, SOPP1, SOPP2
      ELSE
         BACKSPACE 7
      END IF
*     This event is finished, start again with the next one
      GO TO 100
      
 300  CONTINUE
      WRITE(8,*) "End of a run of ", NCASE, " particles"
      CLOSE (UNIT = 7)
      CLOSE (UNIT = 8)
      END

