      SUBROUTINE L2_DO_CD_HITFIND(CAN_DROP)
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : control cd hitfinding in L2
C-
C-   Inputs  :
C-   Outputs : CAN_DROP(1:4)  = .TRUE. if a) no request to hitfind or
C-                                          b) hitfind successful
C-                                    AND
C-                                        c) dropping not forbidden
C-                                          (see entry L2_DONT_DROP_RAW)
C-             CD hit banks under FILT
C-   Controls: RCP banks CD_HITFIND_RCP (From STP) and L2SETUP (from script)
C-
C-   Created  17-JUL-1993   James T. Linnemann
C-   Updated   8-SEP-1994   D. Claes: Protection against bad LUMSET#.
C----------------------------------------------------------------------
      IMPLICIT NONE
      INCLUDE 'D0$INC:L2SET.INC'                ! sets max L2 lumsets
      INCLUDE 'D0$INC:ZEBCOM.INC'
      LOGICAL CAN_DROP(4),DO_HITFIND,KEEP_RAW(4),OK
      CHARACTER*24 CD_TO_HITFIND
      INTEGER IER,ISET,JSET,TRULEN,I,NCHR
      CHARACTER*4  BANK(4),SUB
      CHARACTER*80 MSG,STRING,STRING_IN
      LOGICAL STRING_HAS,SET_OK
      SAVE BANK,KEEP_RAW
      DATA BANK/'CDD1','CDD2','CDD3','CDD4'/
      DATA KEEP_RAW/4*.FALSE./
C----------------------------------------------------------------------
C...statement function returns true if substring SUB found in STRING
      STRING_HAS(SUB)= INDEX(STRING,SUB(1:TRULEN(SUB))).NE.0
C----------------------------------------------------------------------
C
C...get from L2SETUP (if it's there) whether to do hitfinding at all
      CALL GET_L2SETUP_SET_NUMBER(JSET)      !which instance to use
      CALL EZPICK_NOMSG('L2SETUP',IER)       ! Select bank
      OK = IER.EQ.0   !was it found?
      IF (IER.EQ.0) CALL EZGETA('L2_HITFIND',JSET,JSET,1,DO_HITFIND,IER)
      IF (OK) CALL EZRSET
      IF (IER.NE.0) THEN      ! Error reading RCP
        DO_HITFIND = .FALSE.    ! Default is to do no hitfinding
      ENDIF
      IF ( DO_HITFIND ) THEN
        DO I = 1,4
          CAN_DROP(I) = .FALSE.  !won't drop unless hitfind successfully
        ENDDO
      ELSE
        DO I = 1,4
          CAN_DROP(I) = .TRUE.  !no hitfind requested; will drop if asked to
        ENDDO
      ENDIF
C
C...get (luminosity dependent) parameter set number to search for hitfind list
      CALL GET_LUMINOSITY_PARAMETER(ISET)
C
C This is called from frame code...and seen by all runs...special runs, CALIBs
C and even  TEST  TRIGGERs.  Do not complain if L2SET# is ZERO!  During GLOBAL
C running several other pieces ( L2_PRESCLAE, L2_VERT ) also check this number
C at the FILTERING level, and will issue warnings if L2SET# = 0. 
C
      IF(ISET.LT.0 .OR. ISET.GT.NL2SETS) THEN                 ! Note: Less Than
        MSG = 'Illegal L2SET#.  Will not try L2 hitfinding.'
C        IF (ISET.EQ.0) MSG =                                 ! Forgive L2SET#
C     &    'No L2SET#. Will not try L2 hitfinding.'           ! of ZERO here.
        CALL ERRMSG('BAD_LUM_SET','L2_DO_CD_HITFIND',MSG,'W')
        SET_OK = .FALSE.
      ELSE
        SET_OK = .TRUE.
      ENDIF
C
C...fetch from the STP download the list of banks to hitfind at this luminosity
      IF (DO_HITFIND.AND.SET_OK) THEN
        CALL EZPICK_NOMSG('CD_HITFIND_RCP',IER)       ! Select bank
        OK = IER.EQ.0   !was it found?
        IF (IER .EQ. 0) CALL EZGETS('CD_TO_HITFIND',ISET,CD_TO_HITFIND,
     &    NCHR,IER)
        IF (OK) CALL EZRSET
        IF (IER .NE. 0) THEN      ! Error reading RCP
          WRITE (MSG,100)ISET,IER
  100     FORMAT(' ISET =',I5,' IER =',I3,
     &        '. No CD_TO_HITFIND in CD_HITFIND_RCP')
          CALL ERRMSG('BAD_CDCOMP','L2_CD_HITFIND',MSG,'W')
        ELSE
C
C...now have list of banks to try hitfinding on
          STRING = CD_TO_HITFIND
          DO I = 1,4
            IF (STRING_HAS(BANK(I))) THEN
                CALL L2_CD_HITFIND(I,CAN_DROP(I)) !returns true if hitfinding ok
                IF(.NOT.CAN_DROP(I)) CALL ERRMSG(
     &            'FAIL_HITFIND','L2_DO_CD_HITFIND',BANK(I),'W')
            ENDIF
          ENDDO
        ENDIF
      ENDIF
C
C...final result: allow dropping if all ok and no-one forbade dropping
      DO I = 1,4
        CAN_DROP(I) = CAN_DROP(I).AND.(.NOT.KEEP_RAW(I))
        KEEP_RAW(I) = .FALSE.  !ready for next event
      ENDDO
  999 RETURN
C#######################################################################
      ENTRY L2_DONT_DROP_RAW(STRING_IN)
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : forbid dropping of detectors given in STRING
C-      eg CDD1_CDD4 for CDD1 and CDD4 bank
C-      the notion is that this can be called on a per-script basis from a
C-      special tool (ie during the filtering)
C-
C-   Inputs  : STRING_IN   CHARACTER*80
C-   Outputs : change internal state of L2_CD_HITFIND
C-   Controls: STRING_IN
C-
C-   Created  15-OCT-1993   James T. Linnemann
C-
C----------------------------------------------------------------------
      STRING = STRING_IN
      DO I = 1,4
        KEEP_RAW(I) = STRING_HAS(BANK(I))
      ENDDO
      RETURN
      END
