C   22/02/82 206281614  MEMBER NAME  ONMUCOOR (JADEMUS)     FORTRAN
C   17/02/82 202171518  MEMBER NAME  ERMUCOOR (S)           FORTRAN
C   04/02/82 202041502  MEMBER NAME  MUCOOR   (JADEMUS)     FORTRAN
C   18/11/81 202041444  MEMBER NAME  MUCOOR   (S)           FORTRAN
C=======================================================================
C MUCOOR CONVERTS THE RAW DATA INTO CARTESIAN COORDINATES
C=======================================================================
C DON'T FORGET TO CHANGE MUANAC TO STORE DATE OF THIS VERSION IN BANK 0.
C=======================================================================
C LAST CHANGE 16.15  28/06/82   HUGH MCCANN  - DISREGARD CHAMBER STATUS.
C      CHANGE 12.30  31/03/82   HUGH MCCANN  - MODIFY BAD CHAMBER BODGE.
C      CHANGE 13.30  22/02/82   HUGH MCCANN  - FOR IMPROVED ERROR OUTPUT
C      CHANGE 14.30  04/02/82   HUGH MCCANN  - BODGE FOR BAD CHAMBERS
C      CHANGE 12.30  19/10/81   HUGH MCCANN  -CORRECT CRATE6 BAD GAS BUG
C      CHANGE 14.58  04/09/81   CHRIS BOWDERY- CORRECT HANG BUG
C      CHANGE 07.30  10/04/81   HUGH MCCANN  - JADEMUS UPDATE.
C      CHANGE 10.19  26/03/81   JOHN ALLISON - TO ADD ABOVE COMMENT
C                                           ABOUT MUANAC, DATE & BANK 0.
C      CHANGE 14.45  24/03/81   HUGH MCCANN -- ALLOW FOR CRATE 6 DURING
C                                              THE BAD GAS PERIOD.
C      CHANGE 15.00  19/02/81   HUGH MCCANN -- MORE TIDYING & DEBUGGING
C MAJOR TIDYING UP   10/09/80   IAN DUERDOTH
C      CHANGE 21.20  12/05/80   JOHN ALLISON - TO COMMENT OUT CMUDAT,ETC
C      CHANGE 15.00  21/04/80   HUGH MCCANN.
C
C
      SUBROUTINE MUCOOR(HMU,NHITS,HC,NHITMX,LMU,HLUN,NWHIT)
      IMPLICIT INTEGER*2 (H)
C
C COMMONS
CMACRO CMUSER
#include "cmucalib.for"
#include "cmustat.for"
CMACRO CMUHIST
#include "cmubcs.for"
      COMMON /CMUIEV/IEV,NEV,IRD,KRUN,KREC,
     *ISECS,IMINS,IHOURS,IDAY,IMONTH,IYEAR
      COMMON /CMUPRN/IPRIN,MUHPR
C     COMMON /CMUDAT/HMUX(634),HMUY(634),HMUZ(634),
C    1  HLEN(634),HLST(634)
      LOGICAL FIRST/.TRUE./,ENOUGH
      LOGICAL PFG/.TRUE./
      LOGICAL TIMREF,  GOOD
      LOGICAL ADJCHM,SAMCHM,HIT12
      INTEGER  KHM/0/, NOUT/0/
      DIMENSION HMU(1),HC(1),HLUN(1)
      DIMENSION IDRIFT(4)
      DATA NER/0/, KMESS/0/
      DATA HB2/4/,HB3/8/,HB4/16/,HB5/32/,HB6/64/,HB7/128/
C
C
C
C
C     IEV IS THE EVENT NO.,NEV IS NO. OF EVENTS PROCESSED SO FAR...
C     KRUN IS THE RUN NO........
C     HSECT GIVES THE CRATE FOR EACH CHAMBER
C
C
C
C-----------------------------------------------------------------------
C
C     IDBLE IS THE 'DOUBLE PULSE' CUT. IF THE DRIFT TIME DIFFERENCE
C     OF TWO PULSES (WITHIN A GIVEN CHAMBER) IS LESS THAN IDBLE,THEN
C     ONE ASSUMES THAT THE LATER PULSE IS JUST THE 'ECHO' OF THE FIRST.
C     IN THIS CASE ONLY THE EARLIER PULSE IS ACCEPTED AS A 'GENUINE' HIT
      DATA IDBLE/15/
C
C     IMTDLO AND IMTDHI ARE RESPECTIVELY THE SMALLEST AND LARGEST
C     ACCEPTABLE VALUES OF THE DRIFT COORDINATE IN MM
      DATA IMTDLO/-5/,IMTDHI/160/
C
C     MAXMES IS THE MAXIMUM NUMBER OF ERROR MESSAGES TO BE PRINTED/JOB
C     THE NO. OF ERROR MESSAGES IN THE JOB IS COUNTED BY NMESS.
      DATA MAXMES/100/
C
C     MAXMUM IS THE MAXIMUM NUMBER OF SINGLES MESSAGES TO BE PRINTED/JOB
      DATA MAXMUM/100/
C
C     ICONST IS THE CONSTANT SUBTRACTED FROM THE DRIFT TIME OF HIT2
C     TO ESTIMATE THE DRIFT TIME OF HIT1 IN THE EVENT THAT THE DRIFT
C     INFORMATION OF HIT1 HAS BEEN LOST
      DATA ICONST/7/
C
C     MAXREC IS THE MAXIMUM NUMBER OF ATTEMPTS AT DATA RECOVERY
      DATA MAXREC/10/
C
C     IZEXTN  IS ALLOWED EXTENTION PAST THE ENDS OF THE CHAMBER IN
C     WHICH WE PULL THE HIT BACK TO THE PHYSICAL END OF THE CHAMBER
      DATA  IZEXTN/500/
C
C
C
C-----------------------------------------------------------------------
C          ________________
C          WHAT MUCOOR DOES
C          ----------------
C
C CONVERTS MU-CHAMBER SIGNALS TO COORDINATES USING FULL SET OF
C    CALIBRATION DATA.
C
C                              IF THE COORDINATE IS BAD THEN IT IS SET
C                              TO THE CENTRE OF THE CHAMBER,AND HLUN IS
C                              UPDATED APPROPRIATELY
C-----------------------------------------------------------------------
C                       INPUT VARIABLES
C
C HMU         THE RAW MU DATA.
C NHITMX      THE MAXIMUM NO OF HITS THAT CAN BE STORED IN OUTPUT
C               ARRAY HC.  THE DIMENSION OF HC IN THE CALLING ROUTINE
C               MUST BE AT LEAST NWHIT*NHITMX.
C LMU         THE LENGTH OF THE RAW DATA BANK.
C NWHIT       THE NUMBER OF WORDS PER HIT IN THE OUTPUT ARRAY HC
C
C-----------------------------------------------------------------------
C                       OUTPUT VARIABLES
C
C NHITS       THE NUMBER OF HITS,  OUTPUT TO THE ARRAY HC.
C HC          THE OUTPUT ARRAY, EACH HIT IS NWHIT WORDS.
C               WORD  1     -  4*CHAMBER NUMBER + (HIT NUMBER -1).
C               WORD  2     -  10*LAYER NUMBER + ORIENTATION PARAMATER.
C               WORDS 3-8   -  X,Y,Z(LEFT),X,Y,Z(RIGHT) (MM).
C               WORD  9     -  POINTER TO RAW DATA
C HLUN          STATUS BITS FOR EACH HIT IN HC
C
C-----------------------------------------------------------------------
C             BIT...15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
C STRUCTURE OF HLUN  0  0  0  0  0  0  0  A  B  C  D  X  Y  Z  T  L
C               ......THIS BIT NUMBERING RUNS OPPOSITE TO IBM CONVENTION
C                      I.E. MACHINE SEES 'L' FLAG IN BIT NO. 15.
C
C        (BIT) (TRUTH VALUE)
C HLUN  0  = 0(1)   IF LONGITUDINAL COORDINATE IS INVALID(VALID)
C
C HLUN  1  = 0(1)   IF TRANSVERSE   COORDINATE IS INVALID(VALID)
C        I.E. MOD(HLUN,4)= 0 ---> BOTH  COORDS. BAD
C                        = 1 ---> LONG. COORD OK ( TRANS. COORD. BAD)
C                        = 2 ---> TRANS COORD OK ( LONG.  COORD. BAD)
C                        = 3 ---> ALL OK
C
C HLUN  2  = 0(1)   IF Z  COORDINATE IS INVALID(VALID)
C
C HLUN  3  = 0(1)   IF Y  COORDINATE IS INVALID(VALID)
C
C HLUN  4  = 0(1)   IF X  COORDINATE IS INVALID(VALID)
C
C HLUN  5  = 0(1)   IF Z  COORDINATE IS NORMAL(DRIFT OR LONG)
C
C HLUN  6  = 0(1)   IF Y  COORDINATE IS NORMAL(DRIFT OR LONG)
C
C HLUN  7  = 0(1)   IF X  COORDINATE IS NORMAL(DRIFT OR LONG)
C
C HLUN  8  = 0(1)   IF HIT1 LOST BY HIT4 & RECOVERED FROM HIT2(ALL OK)
C
C NOTE:  THE 1'ST SET BIT(OF 5,6,7) IS ALWAYS THAT FOR  LONG. COORD.
C
C=======================================================================
C=======================================================================
C
C                START OF EXECUTABLE PROGRAM.
C
C***********  RE  MONTE-CARLO :  THIS VERSION OF MUCOOR TREATS ALL
C             INPUT MUEV BANKS IDENTICALLY, REGARDLESS OF WHETHER
C             THE INPUT IS MONTE-CARLO , UNDER THE ASSUMPTION THAT
C             M.C. MUEV BANKS ARE IDENTICAL TO REAL DATA. SO , IF
C             THIS IS A MUEV BANK OUTPUT FROM THE OLD M.C. , NO
C             PROCESSING IS ATTEMPTED.
C
          IF( HMU(1).EQ.0 ) RETURN
C********************************************************************
C
C     COUNT NUMBER OF CALLS TO MUCOOR
          NMU(1)=NMU(1)+1
C
C     OPTIONAL OUTPUT
      PFG = IPRIN.NE.0
      IF(FIRST) GOTO 861
      GOTO 862
C     ENTER HERE ON FIRST CALL.
  861 FIRST = .FALSE.
      NMESS=0
      IF(.NOT.PFG)GO TO 862
C     PRINT MEANING OF MUCOOR ERROR MESSAGES.
      WRITE(6,888)
  888 FORMAT(//' MEANING OF MUCOOR MESSAGE CODES'/
     *         ' ==============================='//
     *' (9)16...CHAMBER IN WRRONG CRATE'/
     *' (9)15...DRIFT TIME OF HIT ONE HAS BEEN LOST'/
     *' (9)13...SINGLES DATA WITH HIT NOT 1'/
     *' (9)12...LAST WORD IN HMU NOT 0 OR 3840'/
     *' (9)11...LENGTH ERROR IN HMU'/
     *' (9)10...OUTPUT ARRAY FULL'/
     *' (9)09...SINGLES COUNT.LT.0 OR COUNT TIME.LE.0'/
     *' (9)07...HIT NUMBER NOT DECREASED BY ONE'/
     *' (9)06...CHAMBER NUMBER OUT OF SEQUENCE'/
     *' (9)05...NEW CHAMBER WITH JHIT.NE.1'/
     *' (9)04...CHAMBER NUMBER OUTSIDE RANGE 1 TO 634'/
     *' (9)03...TIME REFERENCE WORD MISSING'/
     *' (9)02...CRATE NUMBER OUT OF ORDER'/
     *' (9)01...CRATE NUMBER OUTSIDE RANGE 1 TO 14'/
     *'  70+N...CRATE N MISSING'//)
  862 CONTINUE
C
C-----------------------------------------------------------------------
C         INITIALISE COUNTERS ETC
C
C         SET THE DRIFT TIME OUT-OF-SEQUENCE FLAG TO ZERO.
          ITDOS=0
C     CLEAR HIT COUNTER
      NHITS=0
      NDIG = 0
C     CLEAR COUNTER FOR THE NUMBER OF ATTEMPTS AT DATA RECOVERY
      NRECOV=0
C  HAS THE MAXIMUM NO. OF ERROR MESSAGES BEEN PRINTED IN THIS JOB?
      IF(NMESS.GE.MAXMES)PFG=.FALSE.
C
C     SET UP LOOP TO FIND MU HIT DATA
C     SET IP=2 TO POINT TO FIRST DATA WORD-1.(IE SKIP BANK DESCRIPTOR)
      IP=2
      IPLAST=2*LMU
      J2 = -NWHIT
      JHIT = 1
      JCHAM = 0
      JCRATE = 0
      IER = 0
      JER=0
      ITDH4=0
C     GET TYPE OF DATA
         GOTO 1001
C
C=======================================================================
C=======================================================================
C
C              START OF MAIN LOOP
C
C        ALL JUMPS IN THIS PROGRAM ARE FORWARDS, EXCEPT TWO:
C          GO TO 2000     &     GO TO 2200
C-----------------------------------------------------------------------
C
C         RETURN HERE AFTER CONVERTING LAST HIT TO COORDS.
C         AND AFTER A RECOVERABLE ERROR
 2000  CONTINUE
       IF(IER.NE.0) GOTO 2201
C
C         DECIDE WHERE TO RE-ENTER THE MAIN SEARCH PROGRAM
C         THE NEXT WORD COULD BE - END OF DATA
C                                - NEXT CRATE
C                                - NEXT CHAM/HIT
C         IF LAST HIT NUMBER WAS NOT 1, THERE MUST BE ANOTHER HIT
          IF(IHIT.NE.1) GOTO                1012
C         GET NEXT WORD
          IP=IP+1
          IF(IP.GE.IPLAST) GOTO                  200
          NW = HMU(IP)
C         TEST FOR END OF DATA(0F00)
          IF(NW.EQ.3840) GOTO               1001
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, BUT THIS IS NOT THE CORRECT
C         WAY TO COME UPON THE NEXT CRATE.
          IP=IP-1
          GOTO                              1001
C
C-----------------------------------------------------------------------
C
 2201 CONTINUE
         JER=IER
         JHIT=1
         JCHAM=0
C  TOLERATE AN ABITRARY NO. OF E.O.C. MARKERS.
              IF(IER.GT.1)GO TO 1990
              IF(ICRATE.NE.0)GO TO 1990
              IER=0
              GO TO 1001
 1990 CONTINUE
      IER = 0
      IF ( NRECOV.GT.MAXREC )  GOTO               201
      NRECOV = NRECOV + 1
 2200 CONTINUE
C  ERROR RECOVERY,  SEARCH FOR NEXT END-OF-CRATE OR CRATE-MARKER.
      NW = HMU(IP)
      IF(NW.EQ.3840)GO TO              2203
      IF(NW.GE.3841.AND.NW.LE.3854.AND.JER.NE.2) GOTO 2204
C IF JER=2 AND PRESENT CRATE HAS NO E.O.C.,NEED THE FOLLOWING
C STATEMENT TO START NEXT CRATE :
      IF(NW.GE.3841.AND.NW.LE.3854.AND.JER.EQ.2
     *   .AND.NW.GT.ICRATE+3840)GO TO 2204
      IP=IP+1
      IF(IP.GE.IPLAST)GO TO                      201
      GO TO 2200
 2203 NMU(22) = NMU(22) + 1
      GOTO 1001
 2204 NMU(23) = NMU(23) + 1
      IP=IP-1
      GOTO 1001
C
C-----------------------------------------------------------------------
C
C                  GET NEXT CRATE
 1001 CONTINUE
C     THIS IS THE INITIAL ENTRY POINT
C     NEXT WORD SHOULD BE A CRATE WORD
C     THE PROCESSING OF EVERY CRATE MUST START HERE AND , IN
C     NORMAL CIRCUMSTANCES, MUST END HERE---EXCEPT CRATE 14,
C     WHICH CAN GO TO 201 AT THE END WITHOUT COMING HERE.
C     AT THIS POINT, NOTE THOSE CRATES WHICH HAD A MISSING
C     E.O.C. MARKER.
          IF(JCRATE.GT.0.AND.HMU(IP).NE.3840)
     *         NMU(84+JCRATE)=NMU(84+JCRATE) + 1
          IP=IP+1
          IF(IP.GE.IPLAST) GOTO                  200
            NW = HMU(IP)
C           TEST FOR VALID CRATE NUMBER
            ICRATE = NW - 3840
            IF(ICRATE.LT.1.OR.ICRATE.GT.14) GOTO 901
C           CHECK THAT CRATE NUMBER HAS INCREASED
            IF( ICRATE.LE.JCRATE ) GOTO          902
            IF(ICRATE.EQ.JCRATE+1)GO TO 1003
                  JCR1=JCRATE+1
                  ICR1=ICRATE-1
CCC               INCREMENT 'CRATE MISSING' COUNTERS.
                  DO 1502 JJ=JCR1,ICR1
                  IER=70+JJ
                  MER=100*IP+IER
                  CALL MUERRY('MUCOOR',MER,' ERROR CODE.^')
 1502             NMU(70+JJ)=NMU(70+JJ)+1
                  NMESS=NMESS+1
 1003      CONTINUE
            JCRATE = ICRATE
            JCHAM = 0
            KF910 = 0
C
C-----------------------------------------------------------------------
C
C         THE NEXT WORD CAN BE,
C          EITHER 1)  END-OF-CRATE WORD, 3840 (HEX 0F00)
C              OR 2)  TIME REF FOR THE CRATE, IF THERE ARE HITS
C              OR 3)  CHAM/HIT WORD,IF THERE ARE NO HITS & ONLY SINGLES
             IP=IP+1
             NW = HMU(IP)
             IF(NW.EQ.3840) GOTO           1001
C
C              TEST FOR VALID TIME REF(WHICH IS ALWAYS ZERO)
               TIMREF = NW.EQ.3584
               IF(.NOT.TIMREF)IP=IP-1
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 FOR A VALID CHAMBER NUMBER
               IF(ICHAM.LT.1.OR.ICHAM.GT.634) GOTO 904
C              CHECK IF CHAMBER IS IN THE CRATE
               IF(ICRATE.NE.HSECT(HFR(ICHAM)))
     *         GO TO                             916
               IHIT = NW - 4*ICHAM + 1
C              IHIT IS BY DEFINITION IN THE RANGE 1 THROUGH 4
C
C-----------------------------------------------------------------------
C
C     TEST IF CHAMBER NUMBER AND HIT NUMBER ARE CONSISTENT WITH THE LAST
             IF( JHIT.EQ.1 ) GOTO           1010
               IF( IHIT.NE.JHIT-1 ) GOTO         907
               IF( ICHAM.NE.JCHAM ) GOTO         905
               GOTO                    3906
 1010        IF(ICHAM.LE.JCHAM) GOTO 1906
               GOTO  3906
C              DEAL WITH CRATE 9 & 10 WHERE CHAMBERS ARE NOT IN SEQUENCE
 1906        IF(.NOT.(ICRATE.EQ.9.OR.ICRATE.EQ.10)) GOTO 906
             IF(KF910.EQ.1) GOTO              906
             IF( (ICRATE.EQ.9.AND.ICHAM.LE.286.AND.JCHAM.GE.323).OR.
     *       (ICRATE.EQ.10.AND.ICHAM.LE.298.AND.JCHAM.GE.335))
     *       GOTO 2906
C              WE DO HAVE AN ERROR
               GOTO 906
C              NOT AN ERROR BUT IT CAN ONLY HAPPEN ONCE PER CRATE
 2906     KF910 = 1
 3906 CONTINUE
C
C-----------------------------------------------------------------------
C
 1011 CONTINUE
          ADJCHM = ICHAM.EQ.JCHAM+1
          SAMCHM = ICHAM.EQ.JCHAM
          HIT12 = SAMCHM.AND.JHIT.EQ.2
          IF(.NOT.SAMCHM) ITDH4 = 0
C         SET ITDH4 TO 1 IF THERE ARE 4 HITS IN THIS CHAMBER
          IF(.NOT.SAMCHM.AND.IHIT.EQ.4) ITDH4 = 1
C         NEXT WORD IS A DRIFT TIME
          IP=IP+1
          IITD = HMU(IP)
          KKTD = IITD
C         AND THE NEXT IS A LONGITUDINAL TIME DIFFERENCE
          IP=IP+1
          IITL = HMU(IP)
          KKTL = IITL
C         IF NO TIME REF IN THIS CRATE THEN IT MUST BE SINGLES
          IF( IITD.LT.0 .OR. IITD.GT.255 .OR.
     *    IITL.LT.0 .OR. IITL.GT.255 ) GOTO 1200
C           ERROR IF STILL HERE AND THERE WAS NO TIME REF
             IF(.NOT.TIMREF) GOTO 903
             GOTO 1202
C
C-----------------------------------------------------------------------
C
 1200 CONTINUE
C     THIS SECTION HANDLES SINGLES RATES DATA
          NCNT =  IITD - 2048
          NTIM =  IITL - 2048
          IF(IHIT.NE.1) GOTO 913
          IF(NCNT.LT.0 .OR. NTIM.LE.0 ) GOTO 909
          RATE = NCNT
          RATE = 2.0*RATE/NTIM
CP    IF( ENOUGH ) GOTO 2004
CP    KMESS = KMESS + 1
CP    WRITE(6,80)ICHAM,NCNT,NTIM,RATE
CP 80 FORMAT(' MUCOOR SINGLES RATES. CHAM=',I3,'   COUNTS=',I3,
CP   *    '   TIME=',I3,'   CNTS/SEC=',F6.1 )
CP    ENOUGH = KMESS.GE.MAXMUM
 2004 CONTINUE
      GOTO 2002
C
C-----------------------------------------------------------------------
C
C
 1202  CONTINUE
C
C     TEST IF DRIFT TIMES ARE IN SEQUENCE
      IF(.NOT.SAMCHM)GOTO 1014
C     IF DRIFT TIMES ARE OUT OF SEQUENCE, SET ITDOS
C     AND FLAG ALL HITS IN THIS CHAMBER ASBAD.
C     DO THIS IS DONE AFTER PROCESSING HIT1.
          IF(IITD.LT.JITD)GO TO 1014
          ITDOS=1
          NMU(25) = NMU(25) + 1
CC        N.B. NORMALLY, WE DON'T WANT PRINTING FOR THIS ERROR
 1014 CONTINUE
C         INITIALISE BIT 8 OF HIT STATUS OK (I.E. HIT ONE NOT KILLED )
C         THIS FLAG IS ONLY MEANINGFUL FOR HIT1.
          HLUN3 = 2**(8)
      IF(.NOT.SAMCHM)GO TO 1015
C
C         SINCE THE 4'TH HIT DESTROYS THE DRIFT INFORMATION OF THE 1'ST
C        TRY TO RECOVER THIS INFORMATION BY SUBTRACTING A FIXED QUANTITY
C       FROM THE DRIFT TIME OF HIT 2.IF THERE WERE 4 HITS NOTE THE DRIFT
C         TIME FOR THE 2'ND HIT
C         RECORD TEMPORARILY THE DRIFT TIME FOR EACH HIT
          IDRIFT(IHIT) = IITD
C-----------------------------------------------------------------------

C     DO SOMETHING ABOUT HIT 4 KILLING HIT 1
      IF(IHIT.GT.1) GOTO 1015
        IF(ITDH4.EQ.1.AND.IITD.EQ.0.AND.IHIT.EQ.1) GOTO 915
          GOTO 1015
  915     NMU(65)=NMU(65)+1
CC        N.B. NORMALLY, WE DON'T WANT PRINTING FOR THIS ERROR
C         WE HAVE LOST HIT ONE,  RECOVER IT
          KKTD = IDRIFT(2) - ICONST
C         SET STATUS BANK THAT THIS HIT IS BODGED..( BIT 8,= 0)
          HLUN3 = 0
 1015 CONTINUE
C
C-----------------------------------------------------------------------
C     NOTE: KKTD,MAY AT THIS STAGE,BE DIFFERENT TO IITD IF HIT1 HAS BEEN
C     KILLED BY HIT4
          ITD = 0
          IF(KKTD.GE.5.AND.KKTD.LT.256) ITD = KKTD
C
          ITL = 0
          IF(KKTL.GE.4.AND.KKTL.LE.255 ) ITL = KKTL
C
C         COUNT NUMBER OF DIGITISINGS FOUND
          NDIG = NDIG + 1
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C  BODGE FOR 1980 SUSPECT CHAMBERS :
C     PRE-AUTUMN 1980     ---   NO NEED TO SWITCH ANY CHAMBERS OFF ;
C     AUTUMN 1980 TILL 6/81 --- MUST SWITCH ALL 15 OFF , FROM RUN 4900.
C     ( AUTUMN 1980 = 1/10/80 )
C     USE TIME TO IMPLEMENT THESE BODGES, RATHER RUN NO. SO THAT
C     MONTE-CARLO WILL PICK UP CORRECT CHAMBER STATUS.
          IF(IYEAR.LT.1980)GO TO 4901
          IF(IYEAR.GT.1981)GO TO 4902
          IF(IYEAR.EQ.1980.AND.IMONTH.LT.10)GO TO 4901
          IF(IYEAR.EQ.1981.AND.IMONTH.GT.5)GO TO 4901
          HMCSTA(6)  =1
          HMCSTA(22) =1
          HMCSTA(26) =1
          HMCSTA(111)=1
          HMCSTA(120)=1
          HMCSTA(168)=1
          HMCSTA(170)=1
          HMCSTA(178)=1
          HMCSTA(217)=1
          HMCSTA(232)=1
          HMCSTA(246)=1
          HMCSTA(253)=1
          HMCSTA(256)=1
          HMCSTA(257)=1
          HMCSTA(258)=1
 4901 CONTINUE
C
C  ALL DATA UP TILL END OF 1981 , SWITCH 210-213 OFF ALWAYS (DIGITISER):
          HMCSTA(210)=1
          HMCSTA(211)=1
          HMCSTA(212)=1
          HMCSTA(213)=1
 4902     CONTINUE
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C         GET THE STATUS OF THE CHAMBER
CCCCCCCC  GOOD = HMCSTA(ICHAM).EQ.0       CCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCC     CONSIDER ALL CHAMBERS ON. CCCCCCCCCCCCCCCCC
          GOOD = .TRUE.
C         IGNORE HITS FROM BAD CHAMBERS
          IF( GOOD )GO TO              1017
C           SIGNAL FROM CHAMBER WITH STATUS 'NOT OK'.
            NMU(12)=NMU(12)+1
            GO TO                                2001
C             SIGNAL FROM CHAMBER WITH STATUS 'OK'.
 1017 CONTINUE
C
C----------------------------------------------------------------------
C
C        REMOVE HITS FROM SECOND BEAM CROSSING.
C   NORMALLY , THIS MEANS A FIXED CUT OF 80 CLOCK PULSES (4.0 MICROSECS)
C   BUT , IF DRIFT VELOCITY DECREASES , NEED LARGER CUTOFF.
C   ---- THIS HAPPENED BETWEEN RUNS 4220 & 4482.
C   THE NORMAL DRIFT VELOCITY IS 2150 MICRONS PER CLOCK PULSE.
C   --- TO ABOUT 5 %, THAT IS. SO PUT ITMCUT=80*2260/NEW VDRIFT  .
C
      ITMCUT=80
      IF(KRUN.GE.4220.AND.KRUN.LE.4482)ITMCUT=180800/HVDRFT(ICHAM)
      IF ( ITD.GE.ITMCUT ) GOTO 2001
C
C-----------------------------------------------------------------------
C
C        DOUBLE PULSE CUT.
C        IF THE PREVIOUS HIT WAS IN THE SAME CHAMBER AND IF IT WAS
C        CLOSER THAN THE CUT, IDBLE (SET TO 15), THEN DELETE THE
C        PREVIOUSLY ENTERED HIT BY REDUCING NHITS BY ONE AND RESETTING
C        THE POINTER J2,  THEN PROCEDE TO PROCESS THE PRESENT HIT.
         IF(.NOT.SAMCHM) GOTO               1016
         IF(JITD-KKTD.GT.IDBLE.OR.JITD-KKTD.LE.0) GOTO 1016
C              CHECK IF PREVIOUS HIT WAS KEPT. IF NOT , DON'T DELETE
C              A HIT, JUST PROCESS THIS ONE.
               IF(JITD.GE.ITMCUT)GO TO 1016
C              SO ITS A CLOSE PULSE AND WE HAVE TO DELETE ONE
               NHITS = NHITS - 1
               J2 = J2 - NWHIT
               NMU(21) = NMU(21) + 1
 1016    CONTINUE
C
C-----------------------------------------------------------------------
C
C      WE NOW HAVE A SET OF VALID NUMBERS (ICRATE,ICHAM,IHIT,ITD,ITL)
C      WHICH HAVE SATISFIED ALL THE DATA CONSISTENCY REQUIREMENTS.
C      THESE WILL NOW BE CONVERTED INTO COORDINATES.
C      THEY CAN NO LONGER BE REJECTED, BUT ONLY FLAGGED.
C      ITD, ITL  MAY BE ZERO, INDICATING A HIT BUT NO POSITIONAL
C      INFORMATION.
C
C-----------------------------------------------------------------------
C
C         GET THE GLOBAL PARAMETERS
C         FRAME NUMBER.
          IFRAME=HFR(ICHAM)
C         ORIENTATION PARAMETER.
          IOR=HNORM(IFRAME)
C         CALCULATE THE NORMAL COORDINATE.
          ICNORM=HDIST(IFRAME)+HD1(ICHAM)
C
C         PROCESS THE LONGITUDINAL DATA
          IMMTL  = 0
          IF(ITL.NE.0)IMMTL=(HLSF(IHIT,ICHAM)*(ITL-HLTP(ICHAM)))/100
C         SET HLUN1 TO 1 (=0001)
          HLUN1 = 1
          LONG  = 1
C         CALCULATE THE LONGITUDINAL CENTRE OF THE CHAMBER, THEN
C         CALCULATE THE LONGITUDINAL COORDINATE, FLAG IF BAD.
          ICLCEN = (HCLLO(IFRAME)+HCLHI(IFRAME))*0.5
          ICLONG = ICLCEN + IMMTL
C         DATA IS GOOD IF POSITION IS WITHIN IZEXTN OF END OF CHAMBER
          IF( ITL.NE.0. .AND.
     *    ICLONG.GE.(HCLLO(IFRAME)-IZEXTN)  .AND.
     *    ICLONG.LE.(HCLHI(IFRAME)+IZEXTN)  ) GOTO  7
C            THE DATA IS BAD.
C            SET HLUN1 TO 0 (=0000)
             HLUN1 = 0
             LONG  = 0
C            SET ICLONG TO COORDINATE OF CENTRE OF CHAMBER
             ICLONG = ICLCEN
C            INCREMENT BAD LONGITUDINAL COUNT.
             NMU(6)=NMU(6)+1
    7     CONTINUE
          IF(ICLONG.LT.HCLLO(IFRAME).OR.ICLONG.GT.HCLHI(IFRAME))
     *    NMU(27) = NMU(27) + 1
C         IF POSITION IS OUTSIDE CHAMBER, PULL IT IN.
          IF(ICLONG.LT.HCLLO(IFRAME)) ICLONG = HCLLO(IFRAME)
          IF(ICLONG.GT.HCLHI(IFRAME)) ICLONG = HCLHI(IFRAME)
C   FOR THE HANG CORRECTION WE NEED THE CORRECTED IMMTL
          IMMTL = ICLONG - ICLCEN
C
C         ICLONG, HLUN1, LONG ARE NOW SET.
C
C         PROCESS THE DRIFT TIME DATA
          IMMTD = 0
          IF(ITD.NE.0)IMMTD=(HVDRFT(ICHAM)*(ITD-HDTP(ICHAM)))/1000
C   FOR CRATE 6, HAVE TO CORRECT THE SPECIAL UPDATES OF DRIFTVELOCITY
C   PERTAINING TO THE BAD GAS PERIOD (FOR THE CRATE 6 CLOCK PULSE.)
          IF(KRUN.GE.4220.AND.KRUN.LE.4482.AND.ICRATE.EQ.6)
     *    IMMTD=1.258*IMMTD
C         CALCULATE THE WIRE COORDINATE.
          ICWIRE=HCTW(ICHAM)
C         SET HLUN2 TO 2 (=0010)
          HLUN2 = 2
          NDRIF = 1
C       CALCULATE THE TWO (L/R AMBIGUOUS) TRANSVERSE COORDINATES.
             IF(IMMTD.LT.IMTDLO .OR. IMMTD.GT.IMTDHI) GOTO 85
C            DATA IS GOOD, BUT PULL IN IF JUST OUTSIDE PHYSICAL RANGE
             IF(IMMTD.LT.0.OR.IMMTD.GT.145) NMU(29)=NMU(29)+1
             IF(IMMTD.LT.0) IMMTD = 0
             IF(IMMTD.GT.145)IMMTD = 145
             IANG=(HANG(IFRAME)*IMMTL)/10000.
             ICT1=ICWIRE-IMMTD+IANG
             ICT2=ICWIRE+IMMTD+IANG
             GOTO 8
   85        CONTINUE
C            THE DATA IS BAD, SO SET HLUN2 TO 0 (=00000)
             HLUN2 = 0
             NDRIF = 0
C            SET IC1 & IC2 TO COORDINATE OF CENTRE OF CHAMBER
             ICT1 = ICWIRE
             ICT2 = ICWIRE
C            INCREMENT BAD DRIFT TIME COUNT.
             NMU(5)=NMU(5)+1
    8     CONTINUE
C         ICT1, ICT2, HLUN2, NDRIF ARE NOW SET.
C
C-----------------------------------------------------------------------
C
C     STORE THE RESULTING COORDINATES IN THE OUTPUT ARRAY HC.
      IF(NHITS.GE.NHITMX) GOTO 910
          NHITS = NHITS + 1
C         REMEMBER J2 WAS SET TO -NWHIT AT BEGINNING OF EVENT.
          J2 = J2 + NWHIT
C         FILL OUTPUT ARRAY HC.
C         4*(CHAMBER NUMBER) + (HIT NUMBER -1)
          HC(J2+1)=HMU(IP-2)
C         10*LAYER NUMBER + ORIENTATION PARAMETER (HOR).
          HC(J2+2)=10*HLAYER(IFRAME)+IOR
C         OVERALL X DISPLACEMENT.
          IX=HOVALL(HUNIT(IFRAME))
C         X,Y,Z COORDINATES
          GO TO (11,12,13),IOR
   11        CONTINUE
               HC(J2+3)=ICNORM+IX
               HC(J2+4)=ICT1
               HC(J2+5)=ICLONG
               HC(J2+6)=ICNORM+IX
               HC(J2+7)=ICT2
               HC(J2+8)=ICLONG
               HLUNY = HB6 + HB5 + HB4 + NDRIF*HB3 + LONG*HB2
             GO TO 20
   12        CONTINUE
               HC(J2+3)=ICT1+IX
               HC(J2+4)=ICNORM
               HC(J2+5)=ICLONG
               HC(J2+6)=ICT2+IX
               HC(J2+7)=ICNORM
               HC(J2+8)=ICLONG
               HLUNY = HB7 + HB5 + NDRIF*HB4 + HB3 + LONG*HB2
             GO TO 20
   13        CONTINUE
               HC(J2+3)=ICT1+IX
               HC(J2+4)=ICLONG
               HC(J2+5)=ICNORM
               HC(J2+6)=ICT2+IX
               HC(J2+7)=ICLONG
               HC(J2+8)=ICNORM
               HLUNY = HB7 + HB6 + NDRIF*HB4 + LONG*HB3 + HB2
   20     CONTINUE
C         POINTER TO CHAM/HIT DATA WORD IN RAW DATA
          HC(J2+9)=IP-2
C         PACK STATUS INFORM'N FOR THIS HIT IN THE FIRST 8 BITS OF HLUN
          HLUN(NHITS) = HLUN1 + HLUN2 + HLUN3 + HLUNY
C
C-----------------------------------------------------------------------
C
C       RECORD CURRENT PARAMETERS, (TO CHECK NEXT DIGITISING)
 2001 CONTINUE
          JITD = IITD
          JITL = IITL
          JTD = ITD
          JTL = ITL
          JHIT = IHIT
          JCHAM = ICHAM
C
C-----------------------------------------------------------------------
C
      IF(IHIT.GT.1)GO TO 2002
      IF(ITDOS.EQ.0)GO TO 2002
C           DRIFT TIMES ARE OUT OF SEQUENCE FOR THIS CHAMBER.
C         PROCEDURE FOR DRIFT TIMES OUT OF SEQUENCE :
C            LOOK BACK THRO' THE ARRAY HC AND COUNT THE NO. OF
C         HITS WHICH WERE KEPT FROM THIS CHAMBER. THEN CORRECT HLUN
C         ENTRY FOR THESE HITS BY MAKING BOTH DRIFT AND LONGITUDINAL
C         COORDINATES 'BAD'.
          IKEPT=0
          J1=J2-3*NWHIT
      DO 3010 JJ=J1,J2,NWHIT
          JHCHAM=HC(JJ+1)/4
          IF(JHCHAM.EQ.ICHAM)IKEPT=IKEPT+1
 3010 CONTINUE
          HLUN1=0
          HLUN2=0
          LONG=0
          NDRIF=0
      DO 3025 I=1,IKEPT
      GO TO (3011,3012,3013),IOR
 3011     HLUNY=HB6+HB5+HB4+NDRIF*HB3+LONG*HB2
      GO TO 3020
 3012     HLUNY=HB7+HB5+NDRIF*HB4+HB3+LONG*HB2
      GO TO 3020
 3013 HLUNY=HB7+HB6+NDRIF*HB4+LONG*HB3+HB2
 3020 HLUN(NHITS-IKEPT+I) = HLUN1 + HLUN2 + HLUN3 + HLUNY
 3025 CONTINUE
C
C      BEFORE GOING ON TO NEXT CHAMBER , SET ITDOS TO ZERO AND
C      INCREMENT NMU(64).
          ITDOS=0
          NMU(64)=NMU(64)+1
          GOTO 2002
C
C-----------------------------------------------------------------------
C
C       ERROR COUNTING
C
C       901 IS INVALID CRATE NUMBER
  901 NMU(51) = NMU(51) + 1
        IER = 1
        GOTO 930
C
C       902 IS CRATE NUMBER OUT OF ORDER
  902 NMU(52)=NMU(52)+1
        IER = 2
        GOTO 930
C
C       903 IS MISSING TIME REF WORD
  903 NMU(53) = NMU(53) + 1
        IER = 3
        GOTO 930
C
C       904 IS INVALID CHAMBER NUMBER
  904 NMU(54) = NMU(54) + 1
        IER = 4
        GOTO 930
C
C       905 IS NEW CHAMBER WITH JHIT = 1
  905 NMU(55) = NMU(55) + 1
        IER = 5
        GOTO 930
C
C       906 IS CHAMBER NO OUT OF SEQUENCEE
  906   NMU(56) = NMU(56) + 1
        IER = 6
        GOTO 930
C
C       907 IS HIT NUMBER NOT DECREASED BY ONE
  907   NMU(57) = NMU(57) + 1
        IER = 7
        GOTO 930
C
C       909 IS NEGATIVE COUNTS OR TIME IN SINGLES DATA
  909   NMU(59) = NMU(59) + 1
        IER = 9
        GOTO 930
C
C       913 IS SINGLES DATA WITH HIT NOT 1
  913 NMU(63) = NMU(63) + 1
        IER = 13
        GOTO 930
C
C       916 IS CHAMBER IN WRONG CRATE
  916 NMU(66) = NMU(66) + 1
        IER = 16
C
  930 CONTINUE
      NMESS=NMESS+1
      MER=100*IP+IER
      CALL MUERRY('MUCOOR',MER,' ERROR CODE.^')
C
C       AFTER ERROR JUMP BACK TO TRY TO START AGAIN
C-----------------------------------------------------------------------
C
C         JUMP BACK TO BEGINNING OF THE MAIN LOOP
 2002 CONTINUE
      GOTO  2000
C----------------------------------------------------------------------
C
C                  END OF MAIN LOOP
C=======================================================================
C=======================================================================
C
C         END OF INPUT DATA REACHED
C         CHECK THE POSITION OF THE POINTER
  200     IF(IP.EQ.IPLAST+1) GOTO 201
            IF(IP.NE.IPLAST) GOTO 911
            IF( HMU(IP).EQ.0 .OR. HMU(IP).EQ.3840 ) GOTO 201
              GOTO 912
  201  CONTINUE
C             NOTE WHETHER CRATE 14 HAD AN E.O.C. MARKER.
              IF(ICRATE.EQ.14.AND.(  HMU(IP).EQ.3840.OR.HMU(IP-1)
     *        .EQ.3840 )  ) GO TO 202
              NMU(98)=NMU(98)+1
  202 CONTINUE
C             INCREMENT NUMBER OF HITS AND 'NO DATA' COUNT
              NMU(4)=NMU(4)+NHITS
              IF(NHITS.LE.0)NMU(3)=NMU(3)+1
CP    WRITE(6,444)IEV,NHITS
CP444  FORMAT(1X,/,'++++ EVENT ',I3,' ++++ NUMBER OF HITS ',I3/)
      RETURN
C
C=======================================================================
C=======================================================================
C
C     PROCESS THE VARIOUS ERROR  CONDITIONS
C
C     910 IS OVERFLOW OF OUTPUT ARRAY
  910 NMU(60) = NMU(60) + 1
      IER = 10
      GOTO 998
C
C     911 IS LENGTH ERROR IN ARRAY HMU
  911 NMU(61) = NMU(61) + 1
      IER = 11
      GOTO 998
C
C     912 IS LAST WORD NOT 0 OR 3840 IN HMU
  912 NMU(62) = NMU(62) + 1
      IER = 12
      GOTO 998
C
  998 CONTINUE
      NMESS=NMESS+1
      MER=100*IP+IER
      CALL MUERRY('MUCOOR',MER,' ERROR CODE.^')
  999 CONTINUE
C     999 IS THE EXIT FOR ERRORS 10, 11, 12.
C     CAN PUT TYPE OUT HERE
      RETURN
C
C=======================================================================
C=======================================================================
C=======================================================================
C=======================================================================
C
C     WHAT IS SCALED IN THE COUNTERS NMU
C
C     1     CALLS TO MUCOOR
C     3     EVENTS WITH NO HITS
C     4     SUM(OVER EVENTS) OF NUMBER OF HITS
C     5     DRIFT COORD FLAGGED AS BAD
C     6     LONGITUDINAL COORD FLAGGED AS BAD
C    12     HIT FROM CHAMBER WITH STATUS NOT GOOD(1017)
C    21     HIT DELETED BY DOUBLE PULSE CUT(1016)
C    22     END-OF-CRATE FOUND IN RECOVER SEARCH (2203)
C    23     CRATE MARKER FOUND IN RECOVER SEARCH (2204)
C    25     IITD.GE.JITD
C    27     LONGL HIT PULLED IN TO END OF CHAMBER (7)
C    29     DRIFT COORD PULLED IN
C    51     INVALID CRATE NUMBER (901)
C    52     CRATE NUMBER OUT OF ORDER
C    53     TIME REFERENCE WORD NOT PRESENT
C    54     INVALID CHAMBER NUMBER
C    55     NEW CHAMBER, BUT LAST HIT WAS NOT HIT1
C    56     CHAMBER OUTOF SEQUENCE
C    57     HIT NUMBER NOT DECREASED BY ONE
C    59     IN SINGLES DATA, COUNTS OR TIME IS NEGATIVE
C    60     OVERFLOW OF OUTPUT ARRAY, I.E. MORE THAN 200 HITS
C    61     LENGTH ERROR IN HMU
C    62     LAST WORD OF HMU IS NOT 0 NOR 3840
C    63     SINGLES DATA, BUT NOT HIT1
C    64     OUTPUT HITS FLAGGED DUE TO OUT OF SEQUENCE DRIFT TIMES
C           N.B. #64 IS INCREMENTED ONLY ONCE PER CHAMBER IN ANY ONE
C                EVENT, WHEREAS #25 IS INCREMENTED FOR EVERY INSTANCE
C                WHERE WE HAVE  (SAMCHM).AND.IITD.GE.JITD  , WHICH
C                MORE THAN ONCE IN ANY ONE CHAMBER.
C    65     RECOVER OVERWRITTEN HIT1 WITH (HIT2-ICONST)
C    66     CHAMBER IN WRONG CRATE
C
C    70 + NN   COUNT OF NUMBER OF EVENTS WHERE CRATE NN WAS MISSING
C    84 + NN    "    "   "     "    "      "     "   "  HAD NO E.O.C.
      END
