      subroutine hec_trig_evt
c
c process trigger bits from pattern units and produce useful
c trigger words to be found in hec_trig.inc
c
c thanks to Leonid Kourtchaninov
c ML  99/08/26  improved with DAQ trigger words and
c               tdc wrap around flags
c ML  99/09/16  fix for bad lpat_1(13)
c
      implicit none
c
      include'hec_stats_trig.inc' !daq and wac trigger stats
c
      integer i
      logical vm, m1, m2, lpat_1_13_soft
c
c user datacard values common
c
      include'hec_par.inc'      !shared parameters
      include'hec_datacard.inc' !datacard values
      include'hec_evth.inc'     !event header
      include'hec_runh.inc'     !run header 
      include'hec_trig.inc'     !event trigger
c
c *****
c triggers constructed from pattern unit bits
c
c
c define a random trigger
c software random trigger, uncorrelated with particle
c before may97 run 6503, this is in lpat_4(3)
c from   may97 run 6503, this is in lpat_1(14)
c 
      if (irunpd .le. 5 .and. run_no .lt. 6503) then
        trig_random = lpat_4(3)
      else
        trig_random = lpat_1(14)
      endif
c
c hardware random trigger
c trigger set 20ms after some particle
c
      trig_random_hard = lpat_1(4)
c
c define a halo trigger
c - hits in the veto wall before the calorimeter
c - hits on the up and down counters with a hole that are in front
c   of the f1 and f2 finger counters
c - early pileup hits
c - late pileup hits
c note the the hardware definition of the halo does not 
c included the late pileup lpat_4(1)
c L.K. agrees that the following definition is better
c
      vm = .false.
      do i = 5, 16
        vm = vm .or. lpat_2(i)
      enddo
      trig_halo = vm .or. lpat_1(2) .or. lpat_1(11)
      trig_halo = trig_halo .or. lpat_1(5) .or. lpat_4(1)
c
c define a muon counter trigger
c - hits from the muon counter wall after the cryostat
c - hits from the muon counter wall after the shielding
c before may97, lpat_3(1) was pi/e/mu
c from   may97, lpat_3(1) is m2/1.
c
      m1 = .false.
      do i = 1, 4
       m1 = m1 .or. lpat_2(i)
      enddo
      do i = 11, 16
       m1 = m1 .or. lpat_3(i)
      enddo
      m2 = .false.
      if (irunpd .le. 4) then
        do i = 2, 10
          m2 = m2 .or. lpat_3(i)
        enddo
      else
        do i = 1, 10
          m2 = m2 .or. lpat_3(i)
        enddo
      endif
      trig_mucounters = m1 .and. m2
c
c get the pretrigger
c before may97,          pretrigger is in lpat_1(14)
c from   may97 run 6503, pretrigger is in lpat_1(3)
c set it true otherwise...
c
      trig_pretrigger = .true.
      if (irunpd .le. 4) then
        trig_pretrigger = lpat_1(14)
      elseif (irunpd .ge. 5 .and. run_no .ge. 6503) then
        trig_pretrigger = lpat_1(3)
      endif
c
c pretrigger bit was not set at the beginning of the june 2000
c test beam period (runpd = 12, run_no <= 9411 )
c
      if(irunpd .eq. 12. and. run_no .le. 9411 )then
         if(i_event.lt.2)then
            print*,' '
            print*,' ************ WARNING: ****'
            print*,' *** pretrigger bit manually switched ON'  
            print*,' *** lpat_1 = ',lpat_1 
         endif 
         trig_pretrigger = .true.
c
c reset pretrigger for random events
c
         if (trig_random .or. trig_random_hard) then
            trig_pretrigger = .false.
         endif
      endif
c
c define a minimum bias physics trigger
c - beam signal
c - pretrigger (both f1 and f2 fingers (and maybe b1))
c - no halo
c note that this corresponds to the e/pi/mu trigger bit,
c except that here late pileup (lpat_4(1)) are included
c in the definition of halo. 
c before may97, e/pi/mu is in lpat_3(1)
c from   may97 run 6503, e/pi/mu is in lpat_1(10)
c
      trig_physics = trig_pretrigger .and. (.not. trig_halo)
c
c define a muon trigger
c
      trig_mu = trig_physics .and. trig_mucounters
c
c define a electron/pion trigger
c
      trig_epi = trig_physics .and. (.not. trig_mucounters)
c
c define a pion trigger
c - uses the cerenkov counter info
c
      trig_pi = trig_epi .and. (.not. lpat_1(6))
c
c define an electron trigger
c
      trig_e = trig_epi .and. lpat_1(6)
c
c *****
c actual triggers used in DAQ
c there are possibly six daq trigger streams
c lpat_1(4)   harware random
c lpat_1(12)  MWPC (currently not connected)
c lpat_1(13)  mu
c lpat_1(14)  software random  (out of spill)
c lpat_1(15)  epi
c lpat_1(16)  e
c
c also get rid of late pileups from lpat_4(1)
c
c define the daq pi/e*c trigger
c
      trig_daq_e =   lpat_1(16) .and. (.not. lpat_4(1))
c
c define the daq pi/e trigger
c
      trig_daq_epi = lpat_1(15) .and. (.not. lpat_4(1))
c
c define the daq mu trigger
c broken lpat_1(13) during run period 9 and 10
c definition of trig_daq_mu in the case of broken lpat_1(13)
c prescaled mu-trigger lpat_1(13) can appear only if trig_mu is true. 
c no late pileup in physics:
c
      if (irunpd .eq. 9 .or. irunpd .eq. 10) then
        lpat_1_13_soft = .false.
        if (trig_mu) then 
          lpat_1_13_soft = .not. 
     +    (lpat_1(4) .or. lpat_1(12) .or. 
     +     lpat_1(14) .or. lpat_1(15) .or. lpat_1(16))
        endif
        trig_daq_mu = lpat_1_13_soft .and. (.not. lpat_4(1)) 
      else
        trig_daq_mu =  lpat_1(13) .and. (.not. lpat_4(1))
      endif
c
c status
c
      trig_bad = .false.
      if (trig_random .and. trig_random_hard) trig_bad = .true.
      if ((trig_random .or. trig_random_hard) .and.
     +    (trig_daq_e .or. trig_daq_epi .or. trig_daq_mu)) 
     +    trig_bad = .true.
      if ((trig_daq_e .or. trig_daq_epi) .and. trig_daq_mu) 
     +    trig_bad = .true.
c
c flags for tdc wrap around constants
c in principle, one should use a different tdc wrap around constant
c depending on which one of the following flags is set (at most only one
c of these flags is set in one event)
c
      trig_wac_c   = .false.
      trig_wac_epi = .false.
      trig_wac_mu  = .false.
c
      if (.not.trig_bad) then
        if (trig_daq_epi) then
          trig_wac_epi = .true.
        elseif (trig_daq_e) then
          trig_wac_c = .true.
        elseif (trig_daq_mu) then
          trig_wac_mu = .true.
        endif
      endif
c
c keep track of daq and wac trigger stats
c
      if (trig_daq_e)       stat_daq_e       = stat_daq_e       + 1
      if (trig_daq_epi)     stat_daq_epi     = stat_daq_epi     + 1
      if (trig_daq_mu)      stat_daq_mu      = stat_daq_mu      + 1
      if (trig_random)      stat_random      = stat_random      + 1
      if (trig_random_hard) stat_random_hard = stat_random_hard + 1
      if (trig_wac_c)       stat_wac_c       = stat_wac_c       + 1
      if (trig_wac_epi)     stat_wac_epi     = stat_wac_epi     + 1
      if (trig_wac_mu)      stat_wac_mu      = stat_wac_mu      + 1
c
      end


