      FUNCTION L15CT_PROV_EVENT ()
C----------------------------------------------------------------------
C-
C-   Purpose and Methods :  Verification program for L1.5 Cal Trig input
C-                          energies
C-                          
C-
C-   Inputs  : TRGR bank (implicit)
C-   Outputs :
C-   Controls:
C-
C-   Created   8-NOV-1994   Steve Gross   MSU L1/L1.5 Triggers
C-            10-NOV-1994  -added output file handling
C-             1-DEC-1994   log file now called L15CT_PROV_xxxxx.LOG
C-                          rather than just L15CT_PROV.LOG, input
C-                          file name is included in L15CT_PROV_xxxx.LOG
C----------------------------------------------------------------------
      IMPLICIT NONE

      INCLUDE 'D0$PARAMS:LEVEL1_LOOKUP.PARAMS'
      INCLUDE 'D0$PARAMS:L1SIM_CONTROL.PARAMS'
      INCLUDE 'D0$INC:ZEBCOM.INC'

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 'L15CT_PROV_SUMMARY.INC'

      LOGICAL  STATUS, MFP_EVENT_FLAG

      LOGICAL  L15CT_PROV_EVENT

      LOGICAL  FIRST

      DATA     FIRST/.TRUE./

      INTEGER  IER, N

      INTEGER  GT_USER

      PARAMETER ( GT_USER = 523 )   ! Default User Unit Number

      INTEGER  RUN_NUM, EVENT_NUM, ERROR_COUNT

      CHARACTER*60  INPUT_FILE, LOG_FILE_NAME

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

      L15CT_PROV_EVENT = .FALSE.

      MFP_EVENT_FLAG   = .FALSE.

C----------------------------------------------------------------------
C- Perform initialization tasks on first pass (could hang from separate
C- PBD "hook" instead) 
C-
      IF (FIRST) THEN

        CALL  INPUT_FILE_NAME ( INPUT_FILE )

        CALL  EVNTID ( RUN_NUM, EVENT_NUM )

        CALL  INRCP (L1SIM_RCPFILE, IER)

        CALL  L1_FW_AND_CT_RCP

        CALL  L1C_INIT_LSM

        CALL  INTMSG ( ' LSM Initialization complete ' )

C- Allow all error messages to be displayed on the screen

        CALL  ERRMAX ( ' ', -1, -1 )

C- Allocate a logical unit for the log file

        CALL  GTUNIT ( GT_USER, LOG_FILE_LUN, IER )

        IF ( IER .NE. 0 ) THEN

          CALL ERRMSG ( ' LUN ','GTUNIT',
     &  'Could not allocate logical unit for log file ', 'F' )

          GOTO 999

        ENDIF

        CALL  STRINT ( 'L15CT_PROV_', RUN_NUM, LOG_FILE_NAME, N )

        CALL  D0OPEN ( LOG_FILE_LUN, LOG_FILE_NAME(1:N)//'.LOG', 
     &                 'O', STATUS )

        IF ( STATUS .EQV. .FALSE. ) THEN

          CALL ERRMSG ( ' FILE OPEN', 'D0OPEN',
     &   'Could not open log file ', 'F' )

          GOTO 999

        ENDIF

        WRITE ( LOG_FILE_LUN, *) ' '
        WRITE ( LOG_FILE_LUN, *) ' ===================='
        WRITE ( LOG_FILE_LUN, *) ' L15CT_PROV Log file '
        WRITE ( LOG_FILE_LUN, *) ' ===================='
        WRITE ( LOG_FILE_LUN, *) ' '

        WRITE ( LOG_FILE_LUN, 90 ) INPUT_FILE

        WRITE ( LOG_FILE_LUN, *) ' '

   90 FORMAT ( ' Input file: ', A )

        FIRST = .FALSE.

      ENDIF

C----------------------------------------------------------------------
C- Perform "every event" processing
C-

C- Get Run Number and Event Number

      CALL  EVNTID ( RUN_NUM, EVENT_NUM )

C- Extract L1 Cal Trig ADC Counts from L1 Data Block


      CALL  L1_CALTRIG_TOWERS ( STATUS )

      IF ( STATUS .EQV. .FALSE. ) THEN

        CALL ERRMSG ( ' NO L1CT ADC','L15CT_PROV_EVENT',
     &    ' No Level 1 Cal Trig ADC counts found, skipping event.','W')

        WRITE ( LOG_FILE_LUN, 100 ) RUN_NUM, EVENT_NUM

  100 FORMAT ( ' Run ', I6, '    Event ', I8, 
     &         ' has invalid L1CT data, skipped' )

        GOTO 999

      ENDIF


C- Extract "raw" L1.5 Cal Trig Debug Section

      CALL  L15CT_PROV_EXTRACT_DEBUG ( 1, STATUS, MFP_EVENT_FLAG )

      IF ( STATUS .EQV. .FALSE. ) THEN

        CALL ERRMSG ( ' L15CT DEBUG DATA ERROR', 'L15CT_PROV_EVENT',
     &    ' L1.5 Cal Trig Debug Block has error, skipping event', 
     &    'W' )

        WRITE ( LOG_FILE_LUN, 200 ) RUN_NUM, EVENT_NUM

  200 FORMAT ( ' Run ', I6, '    Event ', I8, 
     &  ' has invalid L15CT DeBug data, skipped' )

        GOTO 999

      ENDIF


C- Verify that this is an Mark and Force Pass Event        
                                     
      IF ( .NOT. MFP_EVENT_FLAG ) THEN

        CALL ERRMSG ( ' NOT MFP EVENT', 'L15CT_PROV_EVENT',
     &    ' Not a L1.5 Cal Trig MFP Event, skipping event ', 'W' )

        WRITE ( LOG_FILE_LUN, 300 ) RUN_NUM, EVENT_NUM

  300 FORMAT ( ' Run ', I6, '    Event ', I8, 
     &         ' not MFP, skipped ')

        GOTO 999

      ENDIF


C- Calculate expected L15 Cal Trig "raw" input energies

      CALL  L15C_GET_ENERGIES


C- Build array of actual L1.5 Cal Trig energies

      CALL  L15CT_EXTRACT_INPUT_ENERGIES ( STATUS )

      IF ( STATUS .EQV. .FALSE. ) THEN

        CALL ERRMSG ( ' INVALID L15CT DEBUG DATA','L15CT_PROV_EVENT',
     &    'Bad Level 1.5 Cal Trig Debug Data found, skipping event.',
     &    'W' )
   
        WRITE ( LOG_FILE_LUN, 200 ) RUN_NUM, EVENT_NUM

        GOTO 999

      ENDIF

C- Search for mismatches
                                           
      CALL  L15CT_COMPARE_INPUT ( STATUS )

      IF ( STATUS .EQV. .TRUE. ) THEN 

        ERROR_COUNT = ERROR_COUNT + 1

      ELSE

        CALL INTMSG ( ' Event processed, no errors found ' ) 

      END IF

C- End of event processing

      L15CT_PROV_EVENT = .TRUE.

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