C   23/06/78 701061213  MEMBER NAME  CERNKV66 (S)           FORTRAN
      SUBROUTINE CERNKV(DEPTH,DIR,EE,T)
C
C     S.YAMADA    12-11-77    XX:XX
C     LAST MOD.   28-04-81    SIDE WALL REFLECTION.
C     MOD TO SF6  27-09-85    J.OLSSON (S.YAMADA)
C     ERROR CORR   2-01-86    J.OLSSON
C     N=1.8047     5-01-87    J.OLSSON
C
C     THIS VERSION IS A FIRST ATTEMPT TO USE CORRECT REFRACTIVE INDEX
C     FOR SF6. THE PREVIOUS VERSION FOR SF6, CERNKV6, USES THE SAME
C     VALUE FOR N AS THE OLD SF5 ONE, NAMELY: 1.6725, ACC. TO COMMENT.
C     THE CALCULATED VALUE USED IN ANL, 592.23 CORRESPONDS HOWEVER TO
C     N=1.764.   THIS POINT SHOULD BE CLARIFIED...
C     FURTHERMORE, SOME OTHER VARIABLES MAY HAVE TO BE CHANGED, E.G.
C     ANGLE OF TOTAL REFLECTION...
C
C---- TRACE CERENKOV LIGHT TO THE BACK SURFACE OF THE LEAD GLASS
C     COUNTER AND EXAMINE PHOTOELECTRIC CONVERSION
C---- DEPTH = INITIAL POINT DEPTH OF THE SOURCE  ELECTRON IN RAD LNG.
C---- DIR = DIRECTION COSIGNS OF THE  SOURCE ELECTRON.
C---- EE = SOURCE ELECTON ENERGY.(USED  FOR HISTOGRAM BINNING ONLY)
C---- T  =  PATH LENGTH OF  THE SOURCE ELECTRON IN RAD LENGTH.
C
      DIMENSION  DIR(3)
C
      COMMON /CONSTB/ CONVMX, BK7, TOTL
C---- CONVMX=MAX CONVERSION EFFICIENCY OF R594.
C---- BK7=LENGTH OF THE BK7 LIGHT GUIDE
C-        NOTE:  SF6 WITH NO LIGHTGUIDES
C
      COMMON /CCERSR/ SOURCE(20,25),CRNKSC(20,25), ISPCT(20,40),
     1                 NEP,NACP
C
C---  NEP   NR OF EMITTED PHOTONS
C---  NACP  NR OF ACCEPTED PHOTONS (GENERATING PHOTOELECTRON)
C---- SOURCE(IDEPTH,IEE)   INPUT ELECTRON SPECTRUM FOR EACH DEPTH.
C---- CRNKSC(IDEPTH,IEE)   CERENKOV CONTRIBUTING ELECTRON SPECTRUM.
C---- ISPCT(IDEPTH,LAMDA)    CERENKOV  LIGHT SPECTRUM SEEN AT
C                          THE BACK SURFACE.IDEPTH=EMMISION DEPTH
C SF5 PARAMETERS, N=1.6725
C     DATA RAD/2.23/,  COST/0.5979/,SINT/0.8016/
C
C PARAMETERS FOR SF6, N=1.8047
      DATA RAD/1.53/,  COST/0.55411/,SINT/0.83244/
C
C---- DEFAULT VALUE FOR C,S
      DATA C/1.0/,  S/0.0/
      DATA ICALL6/0/
C
C---- CALCULATE TOTAL CERENKOV QUANTA NUMBER.
C     NL=(2*3.14/137.)/C*(1-1/N**2)*(DELTA  NEU)*T(IN CM)
C     DELTA NEU =  FREQUENCY RANGE,HERE BETWEEN  429000 AND 1000000
C                  GHZ,DNEU=571000GHZ.
C     N =  REFLACTIVE INDEX (=1.8047 IS USED)   SF6 (KOBAYASHI, 10.2.86)
C     (1-1/N**2)=0.6425      SF5   (N=1.6725)
C     (1-1/N**2)=0.69296     SF6   (N=1.8047)
C     C=LIGHT VELOCITY(=30CM/NSEC)
C
      ICALL6 = ICALL6 + 1
C
      TCM =  RAD*T
C
C     ANL = 592.23*TCM     SF5 VALUE, NOTE, WRONG, EQUIV. N=1.76
C     ANL = 560.86*TCM     SF5 VALUE CORRESPONDING TO N=1.6725
C
      ANL = 604.90*TCM
C
      IF(ICALL6.EQ.1) WRITE(6,772) ANL
772   FORMAT(' CERNKV66, CORRECTED VERSION FROM 5.1.1987, ANL=',E14.5)
      NL = ANL
      ANL1 = NL
      NL = NL+1
      FRCANL = ANL-ANL1
C
      TCMZ = TCM*ABS(DIR(3))
      DEPTCM = DEPTH*RAD
C----   SET INDECES  FOR HISTOGRAMS
      IDEP  = DEPTH+1.
      IF(IDEP.GT.20) IDEP =  20
      IEE =  5.*ALOG10(EE)+3.
      IF(IEE.LT.1) IEE =  1
      IF(IEE.GT.25) IEE =25
C
      SOURCE(IDEP,IEE) = SOURCE(IDEP,IEE)+T
C
C---- REPEAT FOR NL-PHOTONS
        DO 1 I=1,NL
C----   CORRECT FRACTIONAL TRIAL NUMBERS.(RANDOMLY CORRECTED.)
        IF(I.EQ.NL.AND.RN(DUM).GT.FRCANL) GO TO 1
C
        NEP = NEP+1
C       CALCULATE  PHOTON DIRECTION COSINE Z-COMPONENT.
        CALL RANAZI(C,S)
        ANN = DIR(3)*COST+(DIR(1)*C+DIR(2)*S)*SINT
C----    CHECK TOTAL REFLECTION CONDITION BETWEEN NORMAL GLASS(N=1.52)
        ABSANN = ABS(ANN)
C-----  TOTAL REFLECTION CHECK IS CHANGED. 02-12-77
C       DATA MESS/0/
C       IF(MESS.EQ.0) WRITE(6,600)
C 600   FORMAT('0******** TRANSPARENCY CHECK AT THE SF5/BK7 BOUNDARY',
C 600   FORMAT('0******** TRANSPARENCY CHECK AT THE SF6/PM BOUNDARY',
C    1  /' ',20X,'CHANGED 02-12-77   ********')
C       MESS = 1
        IF(ABSANN.LT.0.4172) GO TO 1
        TRNS = 1.-0.0065/(ABSANN-0.410)
        IF(RN(DUM).GT.TRNS) GO TO 1
C
C        SET  FREQUENCY
        FREQ = 429000.+RN(DUM)*571000.
C----    ALAMDA=WAVE LENGTH IN NANO-METER.
        ALAMDA = 3.E8/FREQ
C
C----    EMMITTED POSITION Z-CORDINATE IN CM.
        ZS = DEPTCM+RN(DUM)*TCMZ
        IF(ANN.LT.0.) ZS = -ZS
C        ABSORPTION LENGTH TO THE BACK SURFACE AND THROUGH LIGHT GUIDE
C       ABSL = ((TOTL-ZS)/AVABS(ALAMDA)+BK7/BK7ABS(ALAMDA))/ABSANN
        ABSL = ((TOTL-ZS)/AVABS(ALAMDA))/ABSANN
        IF(ABSL.LT.0.) GO TO 1
        IF(ABSL.GT.20.) GO TO  1
C        ABSORPTION  PROBABILITY  CHECK
        IF(RN(DUM).GT.EXP(-ABSL)) GO TO 1
C
C----   REFLECTION AT SIDE WALLS
C  ERROR CORRECTED 2.1.1986   J.O.
C       IF( ABSANN.GE. 0.5979) GO TO 2
        IF( ABSANN.GE. COST )  GO TO 2
C       NOT TOTALLY REFLECTED.
C       COUNT NUMBER OF REFLECTION.
        XY = (TOTL-ZS)*SQRT(1.0-ABSANN**2)/ABSANN
        NREF = XY*0.2
        IF( NREF.LT.1 ) GO TO 2
C       REFLECTION EFFICIENCY IS TAKEN TO BE 0.8.
C  NOTE: OTHER VERSIONS OF YAMADA USE 0.9 AS REFLECTION EFFICIENCY
        IF( RN(DUM).GT.0.8**NREF ) GO TO 1
    2   CONTINUE
C
C         PHOTON REACHES THE BACK SURFACE
C       CHECK CONVERSION
C----   TO KEEP MONTE CARLO EFFICIENCY CONVMX IS MULTIPLIED TO THE RANDU
         IF(CONVMX*RN(DUM).GT.CONVEF(ALAMDA)) GO TO 1
C       PHOTON  IS DETECTED.
         LAMD10 = ALAMDA*0.1-30.
         IF(LAMD10.LT.1) LAMD10 = 1
         IF(LAMD10.GT.40) LAMD10 = 40
         CRNKSC(IDEP,IEE) = CRNKSC(IDEP,IEE)+1.
         ISPCT(IDEP,LAMD10) = ISPCT(IDEP,LAMD10)+1
         NACP = NACP+1
    1    CONTINUE
      RETURN
      END
