      subroutine hec_online_pass1
c
c pass over data set to fill online histograms
c
      implicit none
c
      include'hec_par.inc'       ! shared parameters
      include'hec_adc.inc'       ! adc values common
      include'hec_datacard.inc'  ! datacard values
      include'hec_dig.inc'       ! digital filtering common
      include'hec_dig_sys.inc'   ! digital filtering system common
      include'hec_evth.inc'      ! event header common
      include'hec_geo.inc'       ! geometry correspondance tables
      include'hec_max.inc'       ! max adc values
      include'hec_online.inc'    ! online common
      include'hec_ped.inc'       ! pedestal values
      include'hec_trig.inc'      ! trigger common
c
c local variable declaration
c
      logical hec_nextevt
      external hec_nextevt
      logical again
c
      logical goflag
c
      print *, ' '
      print *, ' *** starting pass for online histogram analysis'
c
c initialise data io for pass
c
      call hec_io_pass_ini
c
c loop over events
c
      i_event = 1
      i_proc = 0
      again = .true.

      do while (i_event .le. n_evtmax .and. again)
c
c check on end of epio file and/or errors
c
         again = hec_nextevt()
c
         if (again) then
 
            if (mod(i_event, 1000) .eq. 0 .or. i_event .eq. 1) then
               print *, ' *** starting event number ', i_event
            endif
c
c process triggers for this event 
c
            call hec_trig_evt
c
c count physics triggers in run 
c
            if (trig_physics) phys_trig_count = phys_trig_count + 1
c
c only process events corresponding to user's request
c
            goflag = .false.
            if (trigreq .eq. 1) goflag = .true.
            if (trigreq .eq. 2 .and. trig_physics) goflag = .true.
            if (trigreq .eq. 3 .and. trig_e ) goflag = .true.
            if (trigreq .eq. 4 .and. trig_pi) goflag = .true.
            if (trigreq .eq. 5 .and. trig_mu) goflag = .true.
c     
c UGLY FIX, REWRITE BEAM CHAMBER CODE INTERFACE!!
c
            if (goflag .or. i_event .eq. 1) then
               call hec_online_mwpc
           endif
c
            if (goflag) then
c
c calculate pedestals and tdcs for this event (needed for max finding)
c
               call hec_ped_evt1
               call hec_ped_fill
               call hec_tdc_evt
c
c find signal max and fill preferred array
c
               if (m_pref .eq. 1) then
                  call hec_cub_evt
               endif
               if (m_pref .ge. 2) then
                  call hec_dig_evt
               endif
               call hec_max_fill
c
c online histogram analysis for a given event
c
               call hec_online_evt1
c 
c count events that are processed
c
               i_proc = i_proc + 1
c
            endif
c
c handle end of event data io
c
            call hec_io_pass_evt
c     
            i_event = i_event + 1
c
         endif
c     
      enddo                     ! enddo over events
c
      i_event = i_event - 1
c
      print *, ' *** events processed      ', i_event
c
c book histograms that are filled at analysis stage
c
      call hec_his_book_ana
c
c system histogram analysis at end of event loop
c
      call hec_online_ana1
c
c gaussian fit to Z clusters and impact point clusters
c
      call hec_his_fit
c
c output fit results to file
c
      call hec_his_fit_output
c
c handle data io at pass end
c
      call hec_io_pass_ter
c
      print *, ' *** pass completed for online histogram filling'
c
      print *, ' ***********************************************'
      print *, ' *** ',i_proc,' events processed by your trigger'
     +          , ' selection'
      print *, ' ***********************************************'
c
      end

 
              
            
          
