C   23/06/78 704172139  MEMBER NAME  CERFUNH  (S)           FORTRAN
      FUNCTION FUNCTN(X,I,A)
C
C---- S.YAMADA         06-12-77
C---- FIT FUNCTION FOR CERENKOV LIGHT DETECTION EFFICIENCY
C
C   CALCULATES EXPLICITLY PART1-3 IN MODE 1, FOR PLOTTING
C      PARAMETER 4 IN MODE 1 IS NOW      A(4) * EXP(...)
C      I.E. WITH 4 PARAMETERS NO DEPENDENCE ON CC1 ANY MORE..
C      AUTO CHANGE FOR NTERM=2,3,4
C
      DIMENSION X(1),A(1),DELTAA(1),DERIV(2)
      COMMON /COSTB/ COST, MODE, AESS, BASI,CC,CC1,AEXPA,A4,A5,A6,A7
     $ ,PART1,PART2,PART3
C     DATA AESS/1.00/
C
      GO TO (1,2,3,3),MODE
C
C---- MODE=1
    1 CONTINUE
C---- FUNCTN=A(1)+CC(COST)*EXP(0.55*X(I))+A(2)*X(I)
C---- FUNCTN=A(1)+CC(COST)*EXP(A(3)*X(I))+A(2)*X(I)
C
C     CC1 = ((0.8751*COST-0.8362)*COST+0.3924)*COST-2.0
      CC1 = ((A4*COST+A5)*COST+A6)*COST+A7
C     CC = 10.**CC1
      CC = BASI**CC1
      IF(NTERM.GT.3) CC = A(4)
C     IF(COST.LT.-.3) CC = 0.
C---- FOR ENDCAP CC=1.5*CC
C     CC = 1.5*CC
C     EXPARG = EXP(0.55*X(I))
      ARGEXP = AEXPA*(X(I)**AESS)
      IF(NTERM.GT.2) ARGEXP = A(3)*(X(I)**AESS)
      EXPARG = EXP(ARGEXP)
C     FUNCTN = A(1)+CC*EXPARG+A(2)*X(I)
      PART1 = A(1)
      PART2 = A(2)*X(I)
      PART3 = CC*EXPARG
      FUNCTN = PART1 + PART2 + PART3
      RETURN
C
C---- MODE=2
    2 Z = X(I)**2
      GO TO 4
C---- MODE=3,4
    3 Z = X(I)
    4 FF = ((A(8)*Z+A(7))*Z+A(6))*Z
      FUNCTN = ((((FF+A(5))*Z+A(4))*Z+A(3))*Z+A(2))*Z+A(1)
      RETURN
C
C
C **************************************
C
      ENTRY FDERIV(X,I,A,DELTAA,NTERMS,DERIV)
C
C    S.YAMADA     06-12-77
C     MODIFIED  07-12-77
C----CALCULATE DERIVATIVES OF THE FIT FUNCTION FOR CEERENKOV LIGHT
C    DETECTION EFFICIENCY SIMULATION DATA.
C----DEIFFERENCIATION IS DONE FOR EACH PARAMETER.
C--        BUG IN OVERWRITING INDEX I CORRECTED 7.10.85  J.OLSSON
C
      GO TO (10,20,30,30),MODE
   10 CONTINUE
C----- J=1
      DERIV(1) = 1.
C----- J=2
      DERIV(2) = X(I)
      IF(NTERMS.LT.3) RETURN
      EXCC = EXP(A(3)*(X(I)**AESS))
C----- J=3
      DERIV(3) = CC*(X(I)**AESS)*EXCC
C     DERIV(3) = EXP(0.55*X(I))
      IF(NTERMS.LT.4) RETURN
C----- J=4
      DERIV(4) = EXCC
      RETURN
C
C---- MODE=2
   20 Z = X(I)**2
      GO TO 40
C---- MODE=3
   30 Z = X(I)
   40 DERIV(1) = 1.0
C       DO 50 I=1,7
C  50   DERIV(I+1) = DERIV(I)*Z
C   BUG BUG BUG BUG BUG BUG  !! !! !! !! !!
        DO 50 JJ=1,7
   50   DERIV(JJ+1) = DERIV(JJ)*Z
      RETURN
      END
