      PROGRAM TUNE_ZERESP
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : help tuning lookup zeresp
C-
C-   Inputs  : LSM file name
C-   Outputs : TUNE_ZERESP.LOG
C-             TUNE_ZERESP.PIK
C-
C-   Created   3-DEC-1990   MICHIGAN STATE UNIVERSITY, TRIGGER CONTROL SOFTWARE
C-   Updated  19-JUL-1991   output LSM section
C-                          constraints: only complete slices in magn_eta are
C-                          allowed (i.e. both signs and all 32 phis)
C-
C----------------------------------------------------------------------
      IMPLICIT NONE
C----------------------------------------------------------------------
C
C       Global declarations
C
      INCLUDE 'D0$LEVEL1:LEVEL1_LOOKUP.PARAMS'
      INCLUDE 'D0$LEVEL1:LEVEL1_LOOKUP.INC'
C
C       Local variables
C
      INTEGER   STATUS
      CHARACTER*80 FILE_NAME
      INTEGER    SIGN_ETA, MAGN_ETA, PHI
      INTEGER    EM_ADC_BYTE, HD_ADC_BYTE
      INTEGER    NOT_MAPPED, EM, HD, PX, PY
      PARAMETER (NOT_MAPPED = 0, EM = 1, HD = 2, PX = 3, PY = 4 )
      INTEGER    LUQ_BYTE_0 ( EM:PY , PAGE_INDEX_MIN:PAGE_INDEX_MAX )
      INTEGER    LUQ_BYTE_1 ( EM:PY , PAGE_INDEX_MIN:PAGE_INDEX_MAX )
      INTEGER    MIN_0 ( EM:PY ), MAX_0 ( EM:PY ), MIN_1 ( EM:PY )
      INTEGER    PICK ( POS_ETA:NEG_ETA, 
     &                  ETA_MIN:ETA_MAX,
     &                  PHI_MIN:PHI_MAX, 
     &                  EM:PY ) 
      INTEGER    INDEX
      INTEGER    PROM_TYPE
      INTEGER    FIRST_ETA, LAST_ETA

      WRITE ( 6, 1000 )
 1000 FORMAT ( ' LSM FILE NAME: ', $ )
      READ ( 5, 1010 ) FILE_NAME
 1010 FORMAT ( A )

      CALL LOAD_COMMON_BLOCK ( 10, FILE_NAME, STATUS )

      WRITE ( 6, 1050 ) STATUS
 1050 FORMAT ( ' STATUS : ', I5 )

      IF ( STATUS .NE. 0 ) GOTO 999

      OPEN ( UNIT = 20, FILE = 'TUNE_ZERESP.LOG', STATUS = 'NEW' )
      OPEN ( UNIT = 30, FILE = 'TUNE_ZERESP.PIK', STATUS = 'NEW' )

      DO SIGN_ETA = POS_ETA, NEG_ETA
        DO MAGN_ETA = ETA_MIN, ETA_MAX
          DO PHI = PHI_MIN, PHI_MAX

            IF ( ( DAC_BYTE(SIGN_ETA,MAGN_ETA,PHI,EM_TOWER) .EQ. 0 )
     &      .AND. ( DAC_BYTE(SIGN_ETA,MAGN_ETA,PHI,HD_TOWER) .EQ. 0 ) )
     &        GOTO 100

            CALL PROM_RESPONSE_ALL_INDICES (SIGN_ETA,MAGN_ETA,PHI,
     &                                      0, 0, LUQ_BYTE_0 )

            CALL PROM_RESPONSE_ALL_INDICES (SIGN_ETA,MAGN_ETA,PHI,
     &                                      1, 1, LUQ_BYTE_1 )

            WRITE ( 20, 2000 ) SIGN_ETA, MAGN_ETA, PHI

            IF ( DAC_BYTE(SIGN_ETA,MAGN_ETA,PHI,EM_TOWER) .NE. 0 ) THEN

              MIN_0 ( EM ) = MIN( LUQ_BYTE_0(EM,1),
     &                            LUQ_BYTE_0(EM,2),
     &                            LUQ_BYTE_0(EM,3),
     &                            LUQ_BYTE_0(EM,4),
     &                            LUQ_BYTE_0(EM,5),
     &                            LUQ_BYTE_0(EM,6),
     &                            LUQ_BYTE_0(EM,7) ) 

              MAX_0 ( EM ) = MAX( LUQ_BYTE_0(EM,1),
     &                            LUQ_BYTE_0(EM,2),
     &                            LUQ_BYTE_0(EM,3),
     &                            LUQ_BYTE_0(EM,4),
     &                            LUQ_BYTE_0(EM,5),
     &                            LUQ_BYTE_0(EM,6),
     &                            LUQ_BYTE_0(EM,7) ) 

              MIN_1 ( EM ) = MIN( LUQ_BYTE_1(EM,1),
     &                            LUQ_BYTE_1(EM,2),
     &                            LUQ_BYTE_1(EM,3),
     &                            LUQ_BYTE_1(EM,4),
     &                            LUQ_BYTE_1(EM,5),
     &                            LUQ_BYTE_1(EM,6),
     &                            LUQ_BYTE_1(EM,7) ) 

              PICK ( SIGN_ETA, MAGN_ETA, PHI, EM ) = 
     &          LOOKUP_ZERESP (SIGN_ETA,MAGN_ETA,PHI,EM_ET_QUANT)
     &        - MIN_0( EM ) 

              WRITE ( 20, 2010 ) (LUQ_BYTE_0(EM,INDEX),INDEX=1,8),
     &                           (LUQ_BYTE_1(EM,INDEX),INDEX=1,8),
     &                           PICK ( SIGN_ETA, MAGN_ETA, PHI, EM ) 

              IF ( MAX_0 ( EM ) .EQ. 0 ) THEN 
                WRITE (  6, 5000 ) 
                WRITE ( 20, 5000 ) 
 5000           FORMAT ( ' INCREASE LOOKUP_ZERESP AND RE-RUN' ) 
              END IF

              IF ( MIN_1 ( EM ) .EQ. 0 ) THEN 
                WRITE (  6, 5010 ) 
                WRITE ( 20, 5010 ) 
 5010           FORMAT ( ' LOOKUP_ZERESP SET TOO LOW' ) 
              END IF

            END IF

            IF ( DAC_BYTE(SIGN_ETA,MAGN_ETA,PHI,HD_TOWER) .NE. 0 ) THEN

              MIN_0 ( HD ) = MIN( LUQ_BYTE_0(HD,1),
     &                            LUQ_BYTE_0(HD,2),
     &                            LUQ_BYTE_0(HD,3),
     &                            LUQ_BYTE_0(HD,4),
     &                            LUQ_BYTE_0(HD,5),
     &                            LUQ_BYTE_0(HD,6),
     &                            LUQ_BYTE_0(HD,7) ) 

              MAX_0 ( HD ) = MAX( LUQ_BYTE_0(HD,1),
     &                            LUQ_BYTE_0(HD,2),
     &                            LUQ_BYTE_0(HD,3),
     &                            LUQ_BYTE_0(HD,4),
     &                            LUQ_BYTE_0(HD,5),
     &                            LUQ_BYTE_0(HD,6),
     &                            LUQ_BYTE_0(HD,7) ) 

              MIN_1 ( HD ) = MIN( LUQ_BYTE_1(HD,1),
     &                            LUQ_BYTE_1(HD,2),
     &                            LUQ_BYTE_1(HD,3),
     &                            LUQ_BYTE_1(HD,4),
     &                            LUQ_BYTE_1(HD,5),
     &                            LUQ_BYTE_1(HD,6),
     &                            LUQ_BYTE_1(HD,7) ) 

              PICK ( SIGN_ETA, MAGN_ETA, PHI, HD ) = 
     &          LOOKUP_ZERESP (SIGN_ETA,MAGN_ETA,PHI,HD_ET_QUANT)
     &        - MIN_0 ( HD ) 

              WRITE ( 20, 2020 ) (LUQ_BYTE_0(HD,INDEX),INDEX=1,8),
     &                           (LUQ_BYTE_1(HD,INDEX),INDEX=1,8),
     &                           PICK ( SIGN_ETA, MAGN_ETA, PHI, HD )

              IF ( MAX_0 ( HD ) .EQ. 0 ) THEN 
                WRITE (  6, 5000 ) 
                WRITE ( 20, 5000 ) 
              END IF

              IF ( MIN_1 ( HD ) .EQ. 0 ) THEN 
                WRITE (  6, 5010 ) 
                WRITE ( 20, 5010 ) 
              END IF


              MIN_0 ( PX ) = MIN( LUQ_BYTE_0(PX,1),
     &                            LUQ_BYTE_0(PX,2),
     &                            LUQ_BYTE_0(PX,3),
     &                            LUQ_BYTE_0(PX,4),
     &                            LUQ_BYTE_0(PX,5),
     &                            LUQ_BYTE_0(PX,6),
     &                            LUQ_BYTE_0(PX,7) ) 

              MAX_0 ( PX ) = MAX( LUQ_BYTE_0(PX,1),
     &                            LUQ_BYTE_0(PX,2),
     &                            LUQ_BYTE_0(PX,3),
     &                            LUQ_BYTE_0(PX,4),
     &                            LUQ_BYTE_0(PX,5),
     &                            LUQ_BYTE_0(PX,6),
     &                            LUQ_BYTE_0(PX,7) ) 

              PICK ( SIGN_ETA, MAGN_ETA, PHI, PX ) = 
     &          LOOKUP_ZERESP (SIGN_ETA,MAGN_ETA,PHI,PX_QUANT)
     &        - MIN_0 ( PX ) 

                WRITE ( 20, 2030 ) (LUQ_BYTE_0(PX,INDEX),INDEX=1,8),
     &                             (LUQ_BYTE_1(PX,INDEX),INDEX=1,8),
     &                             PICK ( SIGN_ETA, MAGN_ETA, PHI, PX ) 

              MIN_0 ( PY ) = MIN( LUQ_BYTE_0(PY,1),
     &                            LUQ_BYTE_0(PY,2),
     &                            LUQ_BYTE_0(PY,3),
     &                            LUQ_BYTE_0(PY,4),
     &                            LUQ_BYTE_0(PY,5),
     &                            LUQ_BYTE_0(PY,6),
     &                            LUQ_BYTE_0(PY,7) ) 

              MAX_0 ( PY ) = MAX( LUQ_BYTE_0(PY,1),
     &                            LUQ_BYTE_0(PY,2),
     &                            LUQ_BYTE_0(PY,3),
     &                            LUQ_BYTE_0(PY,4),
     &                            LUQ_BYTE_0(PY,5),
     &                            LUQ_BYTE_0(PY,6),
     &                            LUQ_BYTE_0(PY,7) ) 

              PICK ( SIGN_ETA, MAGN_ETA, PHI, PY ) = 
     &          LOOKUP_ZERESP (SIGN_ETA,MAGN_ETA,PHI,PY_QUANT)
     &        - MIN_0 ( PY ) 

                WRITE ( 20, 2040 ) (LUQ_BYTE_0(PY,INDEX),INDEX=1,8),
     &                             (LUQ_BYTE_1(PY,INDEX),INDEX=1,8),
     &                             PICK ( SIGN_ETA, MAGN_ETA, PHI, PY )

            END IF

  100       CONTINUE

          END DO
        END DO
      END DO



      WRITE ( 30, 3000 ) 
      WRITE ( 30, * ) 

      DO MAGN_ETA = ETA_MIN, ETA_MAX
        IF ( DAC_BYTE(POS_ETA,MAGN_ETA,1,EM_TOWER) .NE. 0 ) THEN
          FIRST_ETA = MAGN_ETA
          GOTO 200
        END IF 
      END DO
  200 CONTINUE

      DO MAGN_ETA = ETA_MAX, ETA_MIN, -1
        IF ( DAC_BYTE(POS_ETA,MAGN_ETA,1,EM_TOWER) .NE. 0 ) THEN
          LAST_ETA = MAGN_ETA
          GOTO 210
        END IF 
      END DO
  210 CONTINUE


      WRITE ( 30, 3010 ) 'EM'
      WRITE ( 30, * ) 
      WRITE ( 30, 3020 ) 'ET'
      WRITE ( 30, * ) 
      WRITE ( 30, 3030 ) 'PLUS'
      WRITE ( 30, 3040 ) FIRST_ETA, LAST_ETA
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( POS_ETA, MAGN_ETA, PHI, EM ),
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3030 ) 'MINUS'
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( NEG_ETA, MAGN_ETA, PHI, EM ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3070 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3100 ) ADC_ZERESP ( POS_ETA, FIRST_ETA, 1, EM )
      WRITE ( 30, * ) 
      WRITE ( 30, 3080 ) 
      WRITE ( 30, * ) 

      DO MAGN_ETA = ETA_MIN, ETA_MAX
        IF ( DAC_BYTE(POS_ETA,MAGN_ETA,1,HD_TOWER) .NE. 0 ) THEN
          FIRST_ETA = MAGN_ETA
          GOTO 300
        END IF 
      END DO
  300 CONTINUE

      DO MAGN_ETA = ETA_MAX, ETA_MIN, -1
        IF ( DAC_BYTE(POS_ETA,MAGN_ETA,1,HD_TOWER) .NE. 0 ) THEN
          LAST_ETA = MAGN_ETA
          GOTO 310
        END IF 
      END DO
  310 CONTINUE


      WRITE ( 30, 3010 ) 'HD'
      WRITE ( 30, * ) 
      WRITE ( 30, 3020 ) 'ET'
      WRITE ( 30, * ) 
      WRITE ( 30, 3030 ) 'PLUS' 
      WRITE ( 30, 3040 ) FIRST_ETA, LAST_ETA
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( POS_ETA, MAGN_ETA, PHI, HD ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3030 ) 'MINUS'
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( NEG_ETA, MAGN_ETA, PHI, HD ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3070 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3100 ) ADC_ZERESP ( POS_ETA, FIRST_ETA, 1, HD )
      WRITE ( 30, * ) 
      WRITE ( 30, 3080 ) 
      WRITE ( 30, * ) 



      DO MAGN_ETA = ETA_MIN, ETA_MAX
        IF ( ( DAC_BYTE(POS_ETA,MAGN_ETA,1,EM_TOWER) .NE. 0 ) 
     &  .OR. ( DAC_BYTE(POS_ETA,MAGN_ETA,1,HD_TOWER) .NE. 0 ) ) THEN
          FIRST_ETA = MAGN_ETA
          GOTO 400
        END IF 
      END DO
  400 CONTINUE

      DO MAGN_ETA = ETA_MAX, ETA_MIN, -1
        IF ( ( DAC_BYTE(POS_ETA,MAGN_ETA,1,EM_TOWER) .NE. 0 ) 
     &  .OR. ( DAC_BYTE(POS_ETA,MAGN_ETA,1,HD_TOWER) .NE. 0 ) ) THEN
          LAST_ETA = MAGN_ETA
          GOTO 410
        END IF 
      END DO
  410 CONTINUE


      WRITE ( 30, 3010 ) 'TOT'
      WRITE ( 30, * ) 
      WRITE ( 30, 3020 ) 'PX'
      WRITE ( 30, * ) 
      WRITE ( 30, 3030 ) 'PLUS' 
      WRITE ( 30, 3040 ) FIRST_ETA, LAST_ETA
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( POS_ETA, MAGN_ETA, PHI, PX ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3030 ) 'MINUS'
      WRITE ( 30, 3040 ) FIRST_ETA, LAST_ETA
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( NEG_ETA, MAGN_ETA, PHI, PX ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3070 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3020 ) 'PY'
      WRITE ( 30, * ) 
      WRITE ( 30, 3030 ) 'PLUS'
      WRITE ( 30, 3040 ) FIRST_ETA, LAST_ETA
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( POS_ETA, MAGN_ETA, PHI, PY ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3030 ) 'MINUS'
      WRITE ( 30, 3040 ) FIRST_ETA, LAST_ETA
      DO MAGN_ETA = FIRST_ETA, LAST_ETA
        WRITE ( 30, '(32I3)' ) ( PICK ( NEG_ETA, MAGN_ETA, PHI, PY ), 
     &                       PHI = 1,32 )
      END DO
      WRITE ( 30, 3050 ) 
      WRITE ( 30, 3060 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3070 ) 
      WRITE ( 30, * ) 

      WRITE ( 30, 3080 ) 
      WRITE ( 30, * ) 



      WRITE ( 30, 3090 ) 

 2000 FORMAT ( ' SIGN', I5, ' ETA', I5, ' PHI', I5 )
 2010 FORMAT ( ' EM' / 8I5 /  8I5 / I5 )
 2020 FORMAT ( ' HD' / 8I5 /  8I5 / I5 )
 2030 FORMAT ( ' PX' / 8I5 /  8I5 / I5 )
 2040 FORMAT ( ' PY' / 8I5 /  8I5 / I5 )

 3000 FORMAT ( ' section LOOKUP_ZERESP' ) 
 3010 FORMAT ( '   with CHANNEL ', A ) 
 3020 FORMAT ( '     with LOOKUP ', A ) 
 3030 FORMAT ( '       with SIGN_ETA ', A ) 
 3040 FORMAT ( '         list MAGN_ETA ', I2, ' to ', I2, 
     &                          ' PHI 1 to 32' )
 3050 FORMAT ( '         end_list' ) 
 3060 FORMAT ( '       end_with' ) 
 3070 FORMAT ( '     end_with' ) 
 3080 FORMAT ( '   end_with' ) 
 3090 FORMAT ( ' end_section' ) 
 3100 FORMAT ( '     with LOOKUP L2'/
     &         '       with SIGN_ETA PLUS and MINUS'/
     &         '         with MAGN_ETA 1 to 20'/
     &         '           with PHI 1 to 32'/
     &         '             assign ', I4 /
     &         '           end_with'/
     &         '         end_with'/
     &         '       end_with'/
     &         '     end_with' ) 

      CLOSE ( UNIT = 20 )
      CLOSE ( UNIT = 30 )

C----------------------------------------------------------------------
  999 CONTINUE
      END
