C   26/09/79            MEMBER NAME  MUEVPR   (S)           FORTRAN
C   20/09/79 C9092101   MEMBER NAME  MUEVPR   (S)           FORTRAN
      SUBROUTINE MUEVPR(KEV,KRUN,KREC)
C
C-----------------------------------------------------------------------
C PRINTS OUT THE BANK MUEV IN AN INTELLIGIBLE FORM.
C
C INPUT VARIABLES...
C HMU/HDATA   THE RAW MU DATA.                  / THESE ARE OBTAINED BY
C LMU         THE LENGTH OF THE RAW DATA BANK.  / A CALL TO BOS.
C    THE NUMBERS KEV,KRUN,KREC ARE PRINTED AS PART OF THE TITLE.
C    THERE IS ONE OUTPUT WORD FOR EACH INPUT WORD.
C    IF THE STRING CANNOT BE RECOGNIZED THE TEN WORDS SPANNING THE
C    BAD WORD ARE PRINTED AND THE BANK IS DUMPED USING HPRS.
C        WARNING THIS CODE FREQUENTLY USES '+' FOR CARRIAGE CONTROL
C        I.E. NO ADVANCE,  SO THE NUMBER OF LINES SENT TO THE LINE
C        PRINTER(LINES LIMITED) IS LARGER THAN THE NUMBER SEEN ON
C        ON THE PRINTED OUTPUT.
C
C-----------------------------------------------------------------------
C
      IMPLICIT INTEGER*2 (H)
#include "cmubcs.for"
      DIMENSION HMU(1)
      EQUIVALENCE (HDATA(1),HMU(1))
      LOGICAL*1 CRCNTL, PLUS/'+'/, SPACE/' '/
      DATA IEOD/3840/
C
C START OF EXECUTABLE PROGRAM.
C SET UP LOOP TO FIND MU HIT DATA. (SKIP BANK DESCRIPTOR WORD)
      A=A
      IPMU = IDATA(IBLN('MUEV'))
      IF(IPMU.EQ.0) GOTO 91
      LMU = IDATA(IPMU)
      IP = IPMU*2 + 2
C  IP POINTS TO HALF WORD BEFORE FIRST MUON DATA WORD
C THERE ARE 2*LMU DATA WORDS, MUON DATA STARTS IN WORD 3
      IPLAST=2*LMU + 2*IPMU
      JCHAM = 0
C
C  WRITE OUT TITLE AND HEADINGS
      WRITE(6,130)KEV,KRUN,KREC
  130 FORMAT('0MUEVPR - EVENT',I4,'  RUN',I5,' REC',I5,'  ',10('****')
     1 / ' CRT  TREF',4('  CHM/H  TDR  TLG')    )
C
      GOTO 1001
C
C-----------------------------------------------------------------------
C START OF MAIN LOOP ---------------------------------------------------
C
C
 2000 CONTINUE
C  RETURN HERE AFTER PRINTING LAST CHAM/HIT TD TL
C  THE NEXT WORD COULD BE 'END OF DATA' OR 'NEXT CRATE'
C  OR 'NEXT CHAM/HIT'
      IP=IP+1
      IF(IP.GE.IPLAST) GOTO 200
      NW = HMU(IP)
C  TEST FOR END OF DATA(0F00)
      IF(NW.EQ.3840) GOTO 1003
C  TEST FOR NEXT CHAM/HIT WORD
      IF(NW.GE.4 .AND. NW.LE.2539 ) GOTO 1013
C  SO IT MUST BE THE NEXT CRATE
      GOTO 1002
C
C  OUTPUT THE END OF DATA WORD AFTER CHAMBER DATA
 1003 IF(NH.EQ.1)WRITE(6,121)IEOD
      IF(NH.EQ.2)WRITE(6,122)IEOD
      IF(NH.EQ.3)WRITE(6,123)IEOD
      IF(NH.EQ.4)WRITE(6,124)IEOD
  121 FORMAT('+',28X,I4,':')
  122 FORMAT('+',45X,I4,':')
  123 FORMAT('+',62X,I4,':')
  124 FORMAT('+',79X,I4,':')
      GOTO 1001
C
C  OUTPUT THE END OF DATA WORD  WHEN NO HITS IN THE CRATE
 1004 WRITE(6,105)IEOD
  105 FORMAT('+', 3X,I6,':')
      GOTO 1001
C
C  THIS IS THE ENTRY POINT, FIRST WORD SHOULD BE A CRATE WORD
 1001 IP=IP+1
      IF(IP.GE.IPLAST) GOTO 200
      NW = HMU(IP)
C TEST FOR VALID CRATE NUMBER
 1002 ICRATE = NW - 3840
      IF( ICRATE.LT.1 .OR. ICRATE.GT.14 )  GOTO 901
      WRITE(6,101) ICRATE
  101 FORMAT(' ',I3)
      CRCNTL = PLUS
C
C  NEXT WORD MUST BE END OF DATA OR THE TIME REF FOR THE CRATE
C  BUT THE TIME REF COULD BE ABSENT IF THERE ARE NO HITS
      IP=IP+1
      NW = HMU(IP)
      IF(NW.EQ.3840) GOTO 1004
      IREF = NW - 3584
C TEST FOR VALID TIME REF
      LOGICAL TIMREF
      TIMREF = IREF.GE.0 .AND. IREF.LE.255
      IF(.NOT.TIMREF)IP=IP-1
      IF(TIMREF)WRITE(6,102)IREF
  102 FORMAT('+',3X,I6)
C
C  NEXT WORD MUST BE A CHAMBER/HIT WORD
 1012 IP=IP+1
      IF(IP.GE.IPLAST) GOTO 200
      NW = HMU(IP)
 1013 ICHAM = NW/4
C CHECK IS A VALID CHAMBER NUMBER
      IF( ICHAM.LT.1 .OR. ICHAM.GT.634 ) GOTO 904
      IHIT = NW - 4*ICHAM + 1
C IHIT IS BY DEFINITION IN THE RANGE 1 - 4
C TEST IF SAME OR NEW CHAMBER
      LOGICAL SAMCHM
      SAMCHM = ICHAM.EQ.JCHAM
      IF(.NOT.SAMCHM) NH = 0
      JCHAM = ICHAM
C
C  NEXT TWO WORDS ARE TD & TL  OR  SCALER COUNTS AND TIME
      NH = NH + 1
      IP=IP+1
      IITD = HMU(IP)
      IP=IP+1
      IITL = HMU(IP)
      IF(NH.EQ.1)WRITE(6,111)CRCNTL,ICHAM,IHIT,IITD,IITL
      IF(NH.EQ.2)WRITE(6,112)ICHAM,IHIT,IITD,IITL
      IF(NH.EQ.3)WRITE(6,113)ICHAM,IHIT,IITD,IITL
      IF(NH.EQ.4)WRITE(6,114)ICHAM,IHIT,IITD,IITL
      IF(NH.LT.1.OR.NH.GT.4) GOTO 916
      CRCNTL = SPACE
  111 FORMAT( A1,  9X, I5,'/',I1,I5,I5 )
  112 FORMAT('+', 26X, I5,'/',I1,I5,I5 )
  113 FORMAT('+', 43X, I5,'/',I1,I5,I5 )
  114 FORMAT('+', 60X, I5,'/',I1,I5,I5 )
      GOTO 2000
C
C END OF MAIN LOOP -----------------------------------------------------
C-----------------------------------------------------------------------
C
C CHECK THE POSITION OF THE POINTER WHEN EXIT FROM THE LOOP
  200 IF(IP.EQ.IPLAST+1) GOTO 201
      IF(IP.NE.IPLAST) GOTO 911
      IF( HMU(IP).EQ.0 ) GOTO 201
      IF( HMU(IP).EQ.3840 ) GOTO 1003
      GOTO 911
C
C ERROR, HAVE TO GIVE UP AND CALL HPRS.
C  901 CRATE OUT OF RANGE
C  904 CHAMBER OUT OF RANGE
C  911 LENGTH ERROR
C  916 INVALID COUNT OF HITS
  916 CONTINUE
  901 CONTINUE
  904 CONTINUE
  911 CONTINUE
      I1 = IP - 5
      I2 = I1 + 9
      IP = IP - 2*IPMU - 2
      WRITE(6,120)IP, (HMU(I),I=I1,I2)
  120 FORMAT(' MUEVPR - ERROR AT WORD',I3,'       ****',
     *   /' ',10X, 10I6 , '  GIVE UP AND CALL HPRS' )
      CALL HPRS('MUEV',0)
      GO TO 201
C
C  MUEV BANK NOT PRESENT
   91 WRITE(6,160) KEV, KRUN, KREC
  160 FORMAT(' MUEVPR - EVNT',I4,'  RUN',I4,'  REC',I4,
     1   '  ----MUEV BANK MISSING----' )
      GOTO 201
C
  201 RETURN
      END
