C   01/02/84 805261724  MEMBER NAME  MUANAF   (JADEMUS)     FORTRAN
C
C-----------------------------------------------------------------------
      SUBROUTINE MUANAF
C-----------------------------------------------------------------------
C
C LAST CHANGE 17.30 24/05/88 J. HAGEMANN - SETUP OF YEAR DEP. CONSTANTS
C      CHANGE 18.30 20/05/88 J. HAGEMANN - SETUP OF YEAR DEP. CONSTANTS
C      CHANGE  9.45 21/04/88 J. HAGEMANN - SETUP OF YEAR DEP. CONSTANTS
C      CHANGE 15.25 20/03/86 CHRIS BOWDERY - MOVE NREGS TEST TO LATER ON
C      CHANGE 16.00  9/03/84 CHRIS BOWDERY - CHECK THAT NREGS > 0
C      CHANGE 14.00 26/01/84 TIM GREENSHAW - MU BANKS LOC. WITH CLOC.
C                      VARIABLE JBANK INTRO. TO IMPROVE ERROR OUTPUT.
C      CHANGE 15.30 18/01/84 TIM GREENSHAW - ERROR PRINTOUT ALTERED
C      CHANGE 15.00 30/05/83 HUGH MCCANN - IF EBGEV = 0. , SET IT TO 50.
C                                          ..... FOR COSMIC RUNS.
C      CHANGE 10.00 24/05/82 HUGH MCCANN - TRIVIAL SPELLING MISTAKE.
C                                          EBGEV NOW CHECKED FOR > 50  .
C      CHANGE 23.00 22/02/82 HUGH MCCANN - FOR IMPROVED ERROR OUTPUT.
C      CHANGE 12.00 04/02/82 HUGH MCCANN -- TO MAKE NTPH=4.
C
C-----------------------------------------------------------------------
C
C THIS ROUTINE FOLLOWS INNER DETECTOR TRACKS TO FIND ASSOCIATED MUON
C   CHAMBER HITS AND STORES RESULTS IN 'MUR2' BANKS 0 - 6.
C NOTE THAT IF NO INNER DETECTOR TRACKS ARE PRESENT ONLY BANK 0 IS
C   CREATED.
C
C NOTE ALSO THAT BANK 0 IS CREATED WHETHER OR NOT ANY MUON HITS EXIST.
C
C-----------------------------------------------------------------------
C
      IMPLICIT INTEGER*2 (H)
C
C COMMONS.
C
#include "cmubcs.for"
#include "cmureg.for"
#include "cmucalib.for"
#include "cmufwork.for"
#include "cmustat.for"
#include "cmuffl.for"
C
      COMMON / CMUIEV / IEV,NEV,IRD,KRUN,KREC,
     *                  ISECS,IMINS,IHOURS,IDAY,IMONTH,IYEAR
      COMMON / CMUPTC / XEXTRP,YEXTRP,ZEXTRP,VTZCV,LCOVAR,SIGFXY,SIGFCZ
C
C-----------------------------------------------------------------------
C
C DATA INITIALISATION STATEMENTS.
C
      DATA NWMUR/38/
C NWMUR IS NUMBER OF WORDS PER INNER DETECTOR TRACK IN 'MUR2' BANK 1.
      DATA NTPH/4/
C NTPH IS NUMBER OF TRACKS PER HIT ALLOCATED IN 'MUR2' BANKS 2 AND 3.
      DATA NPL/5/
C NPL IS NUMBER OF POINTS PER EXTRAPOLATED INNER DET. TRACK IN BANK 4.
      DATA IYEARO / -1111 /
C
      LOGICAL FIRST / .TRUE. /
      IF( .NOT. FIRST ) GO TO 4711
        FIRST = .FALSE.
        WRITE(6,9101)
 9101   FORMAT(' +++ MUANAF SPECIAL VERSION OF 24/05/88...')
 4711 CONTINUE
C IYEARO IS YEAR OF LAST EVENT
C
C-----------------------------------------------------------------------
C
C FIND MAGNETIC FIELD FROM HEADER.
      IP=IDATA(IBLN('HEAD'))
      BGAUSS=HDATA(2*IP+30)
C FIND BEAM ENERGY - IN GEV!!!!!!
      EBGEV=EBEAM(HDATA(2*IP+10))/1000.
C  COSMIC RUNS HAVE ZERO BEAM ENERGY .  SET EBGEV TO 50. IN THAT CASE.
      IF(EBGEV.EQ.0.)EBGEV=50.
C
C  TEST THAT BEAM ENERGY IS BETWEEN 1.0 AND 50.0 GEV .
      IEBGEV=INT(EBGEV)
      IF(EBGEV.LT.1..OR.EBGEV.GT.50.)
     *CALL MUERRY('MUANAF',IEBGEV,' = BEAM ENERGY !!^')
      NTRKS=0
C
C  SETUP OF YEAR DEPENDENT CONSTANTS (DETECTOR STATUS 79-83, 84-86)
C
      IF( IYEAR .EQ. IYEARO ) GOTO 1
C                            SETTINGS FOR MONTE CARLO
         IYEARO = IYEAR
C                                           THE ACTUAL VALUES FOR MC:
C        SIGFCZ = 1.11+-0.04    FOR IYEAR<86
C        SIGFCZ = 1.26+-0.05    FOR IYEAR=86
C                                           BUT USE 1 FOR CLARITY
         SIGFXY = 1.0
         SIGFCZ = 1.0
         REST1  = 3.7
         REST2  =  6.5
         IF( KRUN .LE. 100 ) GOTO 101
C                            SIGFAC FOR DATA 79-84
            SIGFXY = 1.43
            SIGFCZ = 1.57
C                            CHAMBER RESOLUTION FOR DATA BEFORE 1984
            REST1  = 3.0
            REST2  = 6.0
 101     IF( IYEAR .GT. 1983 ) GOTO 102
C                            DETECTOR STATUS BEFORE 1984
            GMBP = 1.77
            ABBP = 0.0176
            RDBP = 0.0741
            DEBP = 2.86E-3
C
            GMJETO = 4.99
            ABJETO = 0.0497
            RDJETO = 0.2076
            DEJETO = 8.08E-3
C
            GOTO 1
 102     CONTINUE
C                            DETECTOR STATUS AFTER 1983
            GMBP = 1.39
            ABBP = 0.0139
            RDBP = 0.0580
            DEBP = 2.25E-3
C
            GMJETO = 6.49
            ABJETO = 0.0646
            RDJETO = 0.2701
            DEJETO = 10.5E-3
C
            IF( KRUN .LE. 100 ) GOTO 1
C                            CHAMBER RESOLUTION FOR DATA 1985
               REST1  = 7.5
               REST2  = 11.5
C                            SIGFAC FOR DATA OF 1985
               SIGFCZ = 1.31
               IF( IYEAR .NE. 1986) GOTO 1
C                            CHAMBER RESOLUTION FOR DATA OF 1986
                  REST1  = 6.7
                  REST2  = 9.5
C                            SIGFAC FOR DATA OF 1986
                  SIGFXY = 1.56
                  SIGFCZ = 1.40
C
C
C-----------------------------------------------------------------------
C
C FIND 'PATR' POINTER.
C
 1    CONTINUE
      IPPATR=IDATA(IBLN('PATR'))
      IF(IPPATR.LE.0)GO TO 3
      NTRKS=IDATA(IPPATR+2)
      NWTRK=IDATA(IPPATR+3)
      ISTART=IDATA(IPPATR+1)+1
C
C-----------------------------------------------------------------------
C
C FIND MUON COORDINATE BANK.
C
 2    CONTINUE
C
C   BANK NUMBERS OF MUR1 BANKS IN JBANK, OF MUR2 BANKS IN IBANK.
C
      JBANK=0
      CALL CLOC(IMUR10,'MUR1',JBANK)
C
C IF MUR1 DOES NOT EXIST ( IN PRINCIPLE NOT POSSIBLE, IF MUANAC IS USED)
C THEN GOT TO 3.
C
      IF(IMUR10.LE.0)GO TO 3
C

C PICK UP NO. OF HITS.
C
      NHITS=IDATA(IMUR10+1)
C
C PICK UP NO. OF WORDS PER HIT.
C
      NWHIT=IDATA(IMUR10+3)
C
C PICK UP COORDINATE BANK, 'MUR1' BANK 1.
C
      JBANK=1
      CALL CLOC(IMUR11,'MUR1',JBANK)
C
C  NO MUR1/1 BANK FOR THIS EVENT IMPLIES THAT MU-FILTER WAS NOT
C  OPERATIONAL, OR SIGNAL-COORDINATE CONVERSION HAS NOT BEEN DONE .
C  OR MAYBE MUON TRACKING WAS NOT DONE IF THIS IS MONTE CARLO.
C  BUT WANT TO MAKE MUR2/0 ANYWAY.
C
      IF(IMUR11.LE.0)GO TO 3
C
C PICK UP HIT STATUS BANK, 'MUR1' BANK 2.
C
      JBANK=2
      CALL CLOC(IMUR12,'MUR1',JBANK)
C
C-----------------------------------------------------------------------
C
C CREATE 'MUR2' RESULTS BANKS.
C
 3    CONTINUE
C
C ADD 'MUR2' TO SPECIAL LIST READY FOR WRITING.
C
      CALL BSAW(1,'MUR2')
      IBANK=0
      CALL CCRE(IMUR20,'MUR2',IBANK,4,IER)
      IF(IER.NE.0)GO TO 96
      IDATA(IMUR20+1) = NTRKS
      IDATA(IMUR20+2) = NWMUR
      IDATA(IMUR20+3) = NTPH
      IDATA(IMUR20+4) = NPL
C
      IF( NREGS .LE. 0 ) GO TO 94
C
      IF(IPPATR.LE.0)GO TO 98
C
      IF(IMUR10.LE.0)GO TO 97
C
      IF(IMUR11.LE.0.OR.IMUR12.LE.0)GO TO 975
C
      IF(NTRKS.LE.0)GO TO 99
C
C MAIN RESULTS BANK.
C
      IBANK=1
      CALL CCRE(IMUR21,'MUR2',IBANK,NTRKS*NWMUR,IER)
      IF(IER.NE.0)GO TO 96
C
C HIT-TRACK CORRELATION BANK.
C
      IBANK=2
      CALL CCRE(IMUR22,'MUR2',IBANK,(NTPH*NHITS+1)/2,IER)
      IF(IER.NE.0)GO TO 96
C
C HIT-TRACK AMBIGUITY BANK.
C
      IBANK=3
      CALL CCRE(IMUR23,'MUR2',IBANK,(NTPH*NHITS+1)/2,IER)
      IF(IER.NE.0)GO TO 96
C
C EXTRAPOLATED TRACK POINTS BANK.
C
      IBANK=4
      CALL CCRE(IMUR24,'MUR2',IBANK,3*NPL*NTRKS,IER)
      IF(IER.NE.0)GO TO 96
C
C-----------------------------------------------------------------------
C
C TRY IT. (HERE FOLLOWS THE INTRODUCTION TO MUFFLE FOR REFERENCE.)
C     SUBROUTINE MUFFLE(IPATR,APATR,NTRKS,NWTRK,
C    * BGAUSS,
C    * HC,NHITS,NWHIT,
C    * HLUN,
C    * IMUR,AMUR,NWMUR,
C    * HTC,HAMB,NTPH,
C    * EXTRA,NPL,EBEAM)
C
C     IMPLICIT INTEGER*2 (H)
C
C     DIMENSION IPATR(1),APATR(1),
C    * HC(1),
C    * HLUN(1),
C    * IMUR(1),AMUR(1),
C    * HTC(1),HAMB(1),
C    * EXTRA(1)
C
C-----------------------------------------------------------------------
C
C IPATR,APATR IS PATT. REC. DATA (SEE JADE COMP. NOTE 12, 23/2/79.)
C NTRKS IS NUMBER OF TRACKS.
C NWTRK IS NUMBER OF WORDS PER TRACK.
C BGAUSS IS MAGNETIC FIELD IN GAUSS.
C HC CONTAINS MUON HIT COORDINATES.
C NHITS IS THE NUMBER OF MUON HITS.
C NWHIT IS NUMBER OF WORDS PER HIT IN HIT COORDINATE ARRAY.
C HLUN IS HIT STATUS ARRAY.
C IMUR,AMUR IS MU-RESULTS 'MUR2' BANK 1 - FILLED BY MUFFLE.
C NWMUR IS NUMBER OF WORDS PER INNER DTECTOR TRACK IN 'MUR2' BANK 1.
C HTC IS HIT-TRACK CORELLATION ARRAY - FILLED BY MUFFLE.
C HAMB IS AMBIGUITY FLAG FOR EACH ENTRY IN HTC.
C NTPH IS NUMBER OF TRACKS PER HIT ALLOCATED IN HTC.
C EXTRA STORES THE POINTS ON THE EXTRAPOLATED TRACK.
C NPL IS THE NUMBER OF POINTS PER EXTRAPOLATED TRACK.
C EBEAM IS THE BEAM ENERGY IN GEV.
C
      CALL MUFFLE(IDATA(IPPATR+ISTART),IDATA(IPPATR+ISTART),
     * NTRKS,NWTRK,
     * BGAUSS,
     * IDATA(IMUR11+1),NHITS,NWHIT,
     * IDATA(IMUR12+1),
     * IDATA(IMUR21+1),IDATA(IMUR21+1),NWMUR,
     * IDATA(IMUR22+1),IDATA(IMUR23+1),NTPH,
     * IDATA(IMUR24+1),NPL,EBGEV)
C
C-----------------------------------------------------------------------
C
C NOW ADD BANK 5, THE MULTIPLE SCATTERING ELIPSES.
C
      IBANK=5
      CALL CCRE(IMUR25,'MUR2',IBANK,NELIPS,IER)
      IF(IER.NE.0)GO TO 96
      CALL UCOPY(ELIPSE,IDATA(IMUR25+1),NELIPS)
C
C-----------------------------------------------------------------------
C
C NOW ADD BANK 6, THE BAD CHAMBER LIST.
C
      NBADCH=0
      DO 6 I=1,NCHAMS
      IF(HMCSTA(I).EQ.0)GO TO 6
      NBADCH=NBADCH+1
      HBADCH(NBADCH)=I
 6    CONTINUE
      IF(MOD(NBADCH,2).NE.0)HBADCH(NBADCH+1)=0
      IBANK=6
      NBADC2=(NBADCH+1)/2
      CALL CCRE(IMUR26,'MUR2',IBANK,NBADC2,IER)
      IF(IER.NE.0)GO TO 96
      CALL UCOPY(HBADCH,IDATA(IMUR26+1),NBADC2)
C
C-----------------------------------------------------------------------
C
      GO TO 99
C
C-----------------------------------------------------------------------
C
C ERROR CONDITIONS.
C
 94   CALL MUERRY('MUANAF',0,'MU ANALYSIS ABORTED - NO MU REGIONS FOUND.
     + NO CALIBRATION?^')
      GO TO 99
C
 95   CALL MUERRY('MUANAF',IBANK,' = I, NOT ENOUGH SPACE FOR ''MUR2/I''.
     +^')
      GO TO 99
C
 96   IF(IER.GT.1)GO TO 95
      CALL MUERRY('MUANAF',IBANK,' = I, ''MUR2/I'' ALREADY PRESENT.^')
      GO TO 99
C
 97   NMU(10)=NMU(10)+1
      CALL MUERRY('MUANAF',JBANK,' = I, ''MUR1/I'' NOT LOCATED. HAS MUAN
     +AC BEEN CALLED?^')
      GO TO 99
C
 975  IF (NHITS .LE. 0) GO TO 99
      NMU(4) = NMU(4) + 1
      CALL MUERRY('MUANAF',JBANK,' = I, ''MUR1/I'' EXPECTED BUT MISSING,
     + SERIOUS ERROR.^')
      GO TO 99
C
 98   CALL MUERRY('MUANAF',0,'''PATR'' NOT LOCATED.^')
      NTRKS=0
      GO TO 3
C
C-----------------------------------------------------------------------
C
 99   CONTINUE
      RETURN
      END
