      REAL FUNCTION qatest(FLAG)
      IMPLICIT NONE
      include ?
      INTEGER FLAG
*--   FLAG=1 GET specific PMT info
*--   FLAG=0 statistics
*--   Enter user code here
*
      vector ax,ay,run
      REAL YVAL(25)
      REAL YBGR(25)
      REAL XVAL(25)
      REAL YSUM(25)

      REAL HV,HVDET,HVRATE,YHV
      REAL HVC(20) ,HVCX

      REAL FNORM

      INTEGER ITIP,IDX
      INTEGER J,I

      LOGICAL START
      DATA START /.TRUE./
      SAVE START
      CHARACTER*100 GETSN
      INTEGER LEN


      IF(START) THEN
         START=.FALSE.
         IF (FLAG.EQ.1) THEN
            CALL GET_SERIALNO(GETSN,LEN) ! title of histogram ID=999 
            PRINT *,'Serial Number ',LEN,' ',GETSN(1:LEN)
         ENDIF
      ENDIF

      qatest = 0.

      IF (FLAG.EQ.1) THEN
         IF(SN(1:6).NE.GETSN(1:LEN)) RETURN
         IF (TYPE.EQ.4) NORM=NORM/6.4

         PRINT *,'Serial Number ',GETSN(1:LEN),' Norm=',norm
      ENDIF


      qatest = 1.
      IF (TYPE.EQ.16) THEN
         ITIP=1
      ELSE
         ITIP=0
      ENDIF
      IF (HVSET.EQ.0) return
      HVCX=0
      DO I=1,TYPE+2
         DO J=1,N1
            IF (I.EQ.1) THEN
               XVAL(J)=X1(J)+HVSHIFT
               YSUM(J)=0
               AX(J)= XVAL(J)
            ENDIF

            IDX=(J-1)*(TYPE+2)+I
            if (I.LE.TYPE) THEN
               FNORM=UNORM(I)*NORM/1000*TIM1(J)
            else
               FNORM=NORM/1000*TIM1(J)
            endif   
            YVAL(J)=SC1(IDX)/FNORM
            YBGR(J)=SC3(IDX)/TIM3(J)/UNORM(I)
            YSUM(J)=YSUM(J)+YVAL(J)
            AY((I-1)*25+J)=YVAL(J)
         ENDDO        
         CALL HVOPT(N1,XVAL,YVAL,HVC(I))
         if (I.LE.TYPE) THEN
            HVCX= HVCX + HVC(I)
            CALL GETRATE(N1,XVAL,YBGR,FLOAT(HVSET),HVRATE)
            CALL HF1(112+ITIP,HVRATE,1.)
         endif   
      ENDDO
      HVCX=HVCX/TYPE

      DO J=1,N1
         YSUM(J)=YSUM(J)/TYPE
         CALL HF2(100+ITIP,XVAL(J),YSUM(J),1.)      
      ENDDO


      CALL HVOPT(N1,XVAL,YSUM,HV)
      CALL GETRATE(N1,XVAL,YSUM,HV,YHV)
c      PRINT *,HV,YHV

      HVDET=HVSET+(1-ITIP)*50

      CALL HF1(102+ITIP,HV,1.)
      CALL HF1(104+ITIP,YHV,1.)
      CALL HF2(106+ITIP,HV,YHV,1.)
      CALL HF2(108+ITIP,HV,HVDET,1.)
      CALL GETRATE(N1,XVAL,YSUM,HVDET,HVRATE)
      if (TYPE.EQ.4) then
         HVRATE=HVRATE*6.4
      endif   
      CALL HF1(110+ITIP,HVRATE,1.)

c     position on the detector


      IF (SM.GT.6) THEN
         SM=13-SM
         NC=11-NC

         NR=NR+32
      ELSE
         NR=31-NR
      ENDIF
      NC=NC+SM*12

   
      CALL HF2(201,FLOAT(NC),FLOAT(NR),HVDET)
      CALL HF2(200,FLOAT(NC),FLOAT(NR),HVRATE)

c     runinfo to pass to kumac
      RUN(1)=TYPE
      RUN(2)=J-1
      RUN(3)=HV
      RUN(4)=HVSET
      RUN(5)=EBB
      RUN(6)=HVCX

      END


      
      SUBROUTINE GETRATE(N,X,Y,HV,YOUT)
c     calculates value of y at x=hv
c     yout=y(x=hv)
      IMPLICIT NONE
      INTEGER N
      REAL X(*),Y(*)
      REAL HV,YOUT

      INTEGER I,IMAX
      REAL DX,DY,FMAX


      FMAX=0
      IMAX=1
      DO I=1,N
         IF (X(I).LE.HV) THEN            
            IMAX=I
         ENDIF
      ENDDO
      IMAX=MIN(IMAX,N-1)
      IF (X(IMAX).EQ.HV) THEN
         YOUT=Y(IMAX)
      ELSE
         DX=X(IMAX+1)-X(IMAX)
         DY=Y(IMAX+1)-Y(IMAX)
         IF (DY.EQ.0) THEN
c            PRINT*,'DY ERROR'
            YOUT=Y(IMAX)
         ELSE
            YOUT=Y(IMAX)+ (HV-X(IMAX))*DY/DX
         ENDIF
      ENDIF
         
      RETURN
      END



      SUBROUTINE HVOPT2(N,X,Y,HV)
*     y'=max
      IMPLICIT NONE
      INTEGER N,I
      REAL X(*),Y(*)
      REAL HV

      REAL Y1(25),Y2(25),FMAX
      INTEGER IMAX
      LOGICAL OK

      DO I=1,N-1
           Y1(I)=Y(I+1)-Y(I)
      ENDDO

      IMAX=0
      FMAX=Y1(2)-Y1(1)
      OK=.TRUE.
      DO I=1,N-2
         Y2(I)=Y1(I+1)-Y1(I)
c         PRINT *,X(I),Y1(I)
         IF (Y2(I).GE.0.AND.X(I).LT.800.AND.OK) THEN
            IMAX=I
         ELSE
           OK=.FALSE.
         ENDIF
      ENDDO
      IF (IMAX.EQ.0) THEN
       PRINT*,'ERROR IMAX ',IMAX
       IMAX=10
      ENDIF
      IMAX=MIN(N,IMAX+2)
      HV  =X(IMAX) +100
      
      PRINT*,'HV=', HV  
      RETURN
      END




      SUBROUTINE HVOPT(N,X,Y,HV)
*     y.gt.threshold

      IMPLICIT NONE
      include ?
      INTEGER N,I,IMAX
      REAL X(*),Y(*)
      REAL HV,THR,DX,DY,SHIFT
      
      IF (TYPE.EQ.16) THEN
         THR=400
         SHIFT=50
      ELSE
         THR=400
         SHIFT=0
      ENDIF

      I=1
      DO WHILE (I.LE.N.AND.Y(I).LT.THR)
         I=I+1
      ENDDO
      IMAX=MIN(N,I-1)


      DX = X(IMAX+1)-X(IMAX)
      DY = Y(IMAX+1)-Y(IMAX)
c      PRINT *,IMAX,DY,Y(IMAX+1)     
      HV = X(IMAX)+ (THR-Y(IMAX))*DX/DY + 100 +SHIFT

      RETURN
      END
*----- misc. -----------------------------------------------------------


      SUBROUTINE STRIP_OFF(TT,LENG)

      CHARACTER*(*) TT
      INTEGER LENG
      LOGICAL Q
      INTEGER LL, ICHR

      LL=LEN(TT)
      DO I=1,LL
         IF(TT(1:1).EQ.' ') THEN
            TT=TT(2:LL)
         ENDIF
      ENDDO

C     LENG=INDEX(TT,CHAR(13))-1 ! interpreter ne pozna funkcije CHAR
C     IF(LENG.GE.0) RETURN

      LENG=LL
      DO WHILE(LENG.GT.0.AND.TT(LENG:LENG).EQ.' ')
         LENG=LENG-1
      ENDDO

      RETURN
      END


      SUBROUTINE GET_SERIALNO(SN,L)

      CHARACTER*(*) SN
      INTEGER L

      CALL HGIVE(999,SN,NX,X1,X2,NY,Y1,Y2,NWT,LOC)
      CALL STRIP_OFF(SN,L)

      RETURN
      END
