      SUBROUTINE L15CT_COMPARE_INPUT ( ERROR )
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : Compare L1.5 Cal Trig energies as extracted from
C-                         DeBug Section of Data Block to expected input as
C-                         calculated from L1 ADC Counts.
C-
C-   Inputs  : L15CT_TT_DEBUG   array of L1.5 Cal Trig "raw" input ADC
C-                              counts extracted from DeBug Section
C-                              (PASSED IN COMMON BLOCK)
C-
C-             LOG_FILE_LUN     logical unit number of log file (passed 
C-                              in common block)
C-
C-   Outputs : ERROR  flag indicating whether an error was found
C-
C-   Controls:
C-
C-   Created   8-NOV-1994   Steve Gross  MSU L1/L1.5 Triggers
C-            10-NOV-1994   added output file reporting
C-            15-NOV-1994   modified DO loop structure
C-            16-NOV-1994   added building of summary information
C-
C----------------------------------------------------------------------
      IMPLICIT NONE

      INCLUDE 'D0$PARAMS:LEVEL1_LOOKUP.PARAMS'

C- L15_FRAMEWORK.PARAMS must be included because that's where NUM_DSPS
C- is (for no known reason), and L15_LOCAL_DSP.PARAMS requires NUM_DSPS.
      INCLUDE 'D0$PARAMS:L15_FRAMEWORK.PARAMS'
      INCLUDE 'D0$PARAMS:L15_LOCAL_DSP.PARAMS'
      INCLUDE 'D0$INC:L15_LOCAL_DSP.INC'

      INCLUDE 'L15C_EVENT.INC'

      INCLUDE 'L15CT_PROV_SUMMARY.INC'

      INCLUDE 'D0$INC:ZEBCOM.INC'

      INTEGER  TOWER_TYPE, SETA, PHI, RACK, RUN, EVENT

      INTEGER  ERROR_COUNT, BIT_POS, BIT, RAW_BYTE

      CHARACTER*79  MESSAGE

      LOGICAL  ERROR

      CHARACTER*5 TOWER_TYPE_CHAR ( L15C_EM_TWR:L15C_TOT_TWR )

      DATA  TOWER_TYPE_CHAR/ '   EM', 'Total'/

      CHARACTER*2 L15CT_LDSP_ID ( -ETA_MAX:ETA_MAX, RACK_1:RACK_2 )

      DATA  L15CT_LDSP_ID/ 4*'A3', 4*'A4', 4*'A1', 4*'B3', 4*'B4',
     +                     '!!',
     +                     4*'B1', 4*'C3', 4*'C4', 4*'C1', 4*'C2',
     +                     4*'A2', 4*'A3', 4*'A4', 4*'A1', 4*'B3',
     +                     '!!',
     +                     4*'B4', 4*'B1', 4*'C3', 4*'C4', 4*'C1'/

C----------------------------------------------------------------------

      ERROR = .FALSE.

      ERROR_COUNT = 0

      CALL  EVNTID ( RUN, EVENT )

      DO TOWER_TYPE = L15C_EM_TWR, L15C_TOT_TWR

C- Examine all ETA, all PHI, both RACKs

        DO SETA = -ETA_MAX, ETA_MAX

C- Skip ETA = 0

          IF ( SETA .EQ. 0 ) GOTO 998

          DO PHI = PHI_MIN, PHI_MAX

            DO RACK = RACK_1, RACK_2
              
C- Look for a mismatch between the RAW and the DEBUG counts

              RAW_BYTE = L15CT_TT_RAW ( SETA, PHI, TOWER_TYPE )

              IF ( RAW_BYTE .NE.
     &             L15CT_TT_DEBUG  ( SETA, PHI, TOWER_TYPE, RACK ) )
     &        THEN

                ENCODE  ( 80, 1999, MESSAGE )
     &          SETA, PHI, L15CT_LDSP_ID ( SETA, RACK ),
     &          TOWER_TYPE_CHAR ( TOWER_TYPE ), RACK,
     &          RAW_BYTE, 
     &          L15CT_TT_DEBUG  ( SETA, PHI, TOWER_TYPE, RACK )

                CALL ERRMSG ( ' DATA MISMATCH', 'L15CT_PROV_EVENT',
     &                        MESSAGE, 'W' )

                IF ( .NOT. ERROR ) THEN

                  WRITE ( LOG_FILE_LUN, 1998 ) RUN, EVENT

                  ERROR = .TRUE.

                ENDIF

                WRITE ( LOG_FILE_LUN, * ) MESSAGE

                ERROR_COUNT = ERROR_COUNT + 1

              END IF

C- Look for bits low and bits high in RAW input

              DO BIT_POS = 0, 7

                BIT = IAND ( RAW_BYTE, 2**BIT_POS )

                IF ( BIT .EQ. 0 ) THEN

                  L15CT_TT_RAW_BIT_LOW ( SETA, PHI, TOWER_TYPE ) = 
     &            IOR ( L15CT_TT_RAW_BIT_LOW ( SETA, PHI, TOWER_TYPE ),
     &                  2**BIT_POS )

                ELSE

                  L15CT_TT_RAW_BIT_HIGH ( SETA, PHI, TOWER_TYPE ) = 
     &            IOR ( L15CT_TT_RAW_BIT_HIGH ( SETA, PHI, TOWER_TYPE),
     &                  2**BIT_POS )
                 
                END IF

              END DO

C- Look for maximum value in RAW input

              IF ( RAW_BYTE .GT. 
     &           L15CT_TT_RAW_MAX ( SETA, PHI, TOWER_TYPE ) ) THEN
            
                L15CT_TT_RAW_MAX ( SETA, PHI, TOWER_TYPE ) = RAW_BYTE

              END IF

            END DO
          
          END DO

  998     CONTINUE

        END DO

      END DO

      IF ( ERROR ) THEN

        WRITE ( LOG_FILE_LUN, 2001 ) RUN, EVENT, ERROR_COUNT

      ELSE

        WRITE ( LOG_FILE_LUN, 2000 ) RUN, EVENT

      END IF

C----------------------------------------------------------------------
  999 RETURN

 1998 FORMAT ( ' Run ', I6, '    Event ', I8, ': Data Mismatch Error ')

 1999 FORMAT ( '   Eta/Phi ', SPI3, '/', SSI2, '   LDSP ',
     &         A2, '  ', A5, ' Et  Rack ', SSI1,
     &         '   Expected $', Z2, '   Found $', Z2 )

 2000 FORMAT ( ' Run ', I6, '    Event ', I8, ': OK' )

 2001 FORMAT ( ' Run ', I6, '    Event ', I8, ': ', I4, 
     & ' Errors found ')

      END
