C   23/08/85 511061955  MEMBER NAME  MCTAGE   (S)           FORTRAN
C--------------------------------------------------------------------
C   09/03/84 403110312  MEMBER NAME  MCTAGE   (S)           FORTRAN
C
C
C ROUTINE TO PERFORM MONTE CARLO INTEGRATION OF ENERGY DEPOSITED
C IN LEAD GLASS / LEAD SCINTILLATOR OF TAGGING SYSTEM (JADE)
C
      SUBROUTINE MCTAGE(MARKMC,X,Y,Z,ARRAY,EHIT)
C
C INPUT
C======
C
C    MARKMC  - WHICH MARKMC OF TAGGER
C    X,Y   - POSITION OF HIT (ALL POSITIONS ARE CONSIDERED IN PLANE
C            OF TAGGER
C    ARRAY - ARRAY TO BE DIMENSIONED TO AT LEAST THE MAXIMUM ADDRESS
C            OF BLOCKS  RETURNED BY MCTAGB
C
C OUTPUT
C=======
C
C  THE ENERGIES DEPOSITED ARE STORED IN ARRAY
C
C  DONT FORGET TO CLEAR ARRAY ONCE PER EVENT
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C A.J.FINCH 8/3/84
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C DUE TO FORTRAN NAMING CONVENTION MUST MAKE MCTAGF REAL
C
      REAL MCTAGF
C
C IEND IS THE MAXIMUM DISTANCE FROM THE HIT AT WHICH ENERGY IS CALCULATE
C SO INTEGRAL MUST BE TENDING TO ZERO AT THIS POINT
C
      DATA RMAX/200./
C
C A FEW WELL KNOWN CONSTANTS
C
      DATA TWOPI/6.2832/
C
C ILIMIT CONTROLS THE NUMBER OF TRIES AT THE INTEGRATIONPER STEP
C
      DATA ILIMIT/500/
C
C
      DIMENSION ARRAY(1)
      DIMENSION NUMBER(200)
C
C
C SOME INITIALISATION
C
      NCOUNT = 0
      SUM = 0
      IBMAX=0
      NTRIES = 0

      CALL VZERO(NUMBER,200)

C
C FIRST CHECK ORIGINAL HIT IS IN A BLOCK
C
      CALL MCTAGB(MARKMC,X,Y,Z,IBLOCK,&999)
C
        AMAX = MCTAGF(0.0,MARKMC)
        AMIN = MCTAGF(RMAX,MARKMC)
C
        IF(AMAX.GE.AMIN)GOTO 101
         TEMP = AMIN
         AMIN = AMAX
         AMAX = TEMP
101     CONTINUE
C ----------------------------------------------------------------------
C
C START INTEGRATION LOOP >>>>>>
C
C ----------------------------------------------------------------------
C
  1     CONTINUE
C
C CHOOSE AN 'R' - DISTANCE FROM HIT OF INTEGRATION
C
        R =   RN(DUMMY) * RMAX
C
C
C DECIDE WHETHER TO USE THIS R ?
C
C CHOOSE AN MCTAGF(R)
C
       AN = RN(DUMMY)*(AMAX-AMIN)+AMIN
       W = MCTAGF(R,MARKMC)
       IF(AN.GT.W) GOTO 1
C
C WE ARE USING THIS R SO CALCULATE ANSWER
C
CCCCC CALL HFILL(2,R,0.0,1.0)
C
C
C CHOOSE A FI
C
         FI = TWOPI * RN(DUMMY)
         XX = X+R*COS(FI)
         YY = Y+R*SIN(FI)
         NCOUNT = NCOUNT + 1
C
C IN A BLOCK ?
C
      CALL MCTAGB(MARKMC,XX,YY,Z,IBLOCK,&10)
      IF(IBLOCK.LE.0)GOTO10
C
C YES - ADD RESULT TO ARRAY
C
C
      NUMBER(IBLOCK) = NUMBER(IBLOCK) + 1
      NTRIES = NTRIES + 1
10    IF (NTRIES.LT.ILIMIT)GOTO 1
C ----------------------------------------------------------------------
C
C  END OF INTEGRATION LOOP
C
C ----------------------------------------------------------------------
100   CONTINUE
C ----------------------------------------------------------------------
C
C  END OF MAIN LOOP
C
C ----------------------------------------------------------------------
C
C
C
 900  CONTINUE
C
      IF(NTRIES.LE.0)GOTO 1000
C
       DO 950 I = 1,200
        IF(NUMBER(I).LE.0)GOTO 950
        ARRAY(I)=EHIT*NUMBER(I)*1000/NCOUNT
 950   CONTINUE
C
C THATS IT ALL DONE
 1000 CONTINUE
      RETURN
 999  CONTINUE
      RETURN
      END
