      subroutine hec_his_book_ana
c
c book online histograms after event loop
c
      implicit none
c
      include'hec_par.inc'      !shared parameters
      include'hec_datacard.inc' !datacard values
      include'hec_geo.inc'      !geometry correspondence tables
      include'hec_his_sys.inc'  !histogram package
      include'hec_online.inc'   !online common
      include'hec_ped.inc'      !pedestal ids
      include'hec_runh.inc'     !run header common
      include'hec_ped_sys.inc'  !pedestal computation common
      include'hec_hvpar.inc'
      include'hec_slow_pc.inc'  !pc slow control common block
c
c local variables
c
      integer ic, i_z, i
      character*70 ct
      character*4 c4
      character*1 cd
      integer nbin, nbin_eta, nbin_phi
      integer id
c
c histo type 1 -- ped/noise histos
c
      if (histo_switch(1) .eq. 1) then
c
c adc pedestal values
c
        id = id_ped
        call hbook1(id, 'pedestals', 100, 500., 1500., 0.)
c
        id = id_ped_ic
        call hbook1(id, 'pedestals vs channel', i_adc_used, 
     +    0.5, i_adc_used + 0.5, 0.)
c      
        if(n_evtmax.le.100)then
           nbin=i_ped_evt1
        else if(n_evtmax.le.1000)then
           nbin=i_ped_evt1/10
        else if(n_evtmax.le.10000)then
           nbin=i_ped_evt1/100
        else 
           nbin=i_ped_evt1/200
        endif               
        if(nbin.gt.100)nbin=100

        call hbook1(id_pedevt+1, 'pedestal vs event for ch 17',
     +       nbin, 0., float(i_ped_evt1), 0.)
        call hbook1(id_pedevt+2, 'pedestal vs event for ch 37',
     +       nbin, 0., float(i_ped_evt1), 0.)
        call hbook1(id_pedevt+3, 'pedestal vs event for ch 62',
     +       nbin, 0., float(i_ped_evt1), 0.)
        call hbook1(id_pedevt+4, 'pedestal vs event for ch 112',
     +       nbin, 0., float(i_ped_evt1), 0.)
c
c adc pedestal rms values 
c
        nbin = 100
        id = id_rms
        call hbook1(id, 'pedestals rms', nbin, 0., 50., 0.)
c
        id = id_rms_ic
        call hbook1(id, 'pedestals rms vs channel', i_adc_used, 
     +    0.5, i_adc_used + 0.5, 0.)
      endif
c
c histo type 2 -- geometrical energy distribution
c
      if (histo_switch(2) .eq. 1) then
c
c maximum versus channel number
c
        nbin = i_adc_used
        ct = 'ave. adc(max) vs channel '
        id = id_max_ic
        call hbook1(id,ct,nbin,0.5,i_adc_used+0.5,0.)
c
c book lego map to find beam in each layer 
c
        nbin_eta = 6
        nbin_phi = 6
        id = id_map - 1
        do i_z = 1, i_z_used
          id = id + 1
          write(cd(1:1), '(i1)') i_z
          ct = 'Depth '//cd
          call hbook2(id, ct, 
     +      nbin_phi, 0.5, real(nbin_phi)+0.5, 
     +      nbin_eta, real(i_z)-0.5, real(nbin_eta + i_z)-0.5, 10000.)
        enddo
c
c book histos which show depth profile for each module
c
        nbin = i_z_used
        ct = 'energy - pedestal depth profile for module 1 '
        id = id_adc_z + i_z_used + 1
        call hbook1(id, ct, nbin, 0., float(nbin), 0.)
        ct = 'energy - pedestal depth profile for module 2 '
        id = id_adc_z + i_z_used + 2
        call hbook1(id, ct, nbin, 0., float(nbin), 0.)
        ct = 'energy - pedestal depth profile for module 3 '
        id = id_adc_z + i_z_used + 3
        call hbook1(id, ct, nbin, 0., float(nbin), 0.)
c
      endif
c
c histo type 4 -- tdc corrected
c
      if (histo_switch(4) .eq. 1) then
c
c time profile in ns (including tdc) for each cell.
c number of bins is a parameter in hec_his_sys.inc
c
        do ic = 1, i_adc_used
          if(ic_connected(ic))then
             write(c4, '(i4)') ic
             ct = 'time + tdc (ns) for channel'//c4
             id = id_timns_ic + ic
             if(i_tim_used.le.64)then
                call hbook1(id, ct, i_tim_used*25, 0.,
     +            real(i_tim_used*25), 0.)
             else
                call hbook1(id, ct, i_tim_used*10, 0.,
     +            real(i_tim_used*25), 0.)                
             endif
          endif
        enddo
      endif
c
c histo type 3 -- no tdc, no calibration correction
c
      if (histo_switch(3) .eq. 1) then
c
c time profile for each channel in 1997
c
        if (irunpd .ge. 4) then
          do ic = 1, i_adc_used
            if(ic_connected(ic))then
               write(c4, '(i4)') ic
               ct = 'time profile for channel'//c4
               id = id_tprof_ic + ic
               call hbook1(id,ct,i_tim_used,0.5,
     +              real(i_tim_used)+0.5,0.)
            endif
          enddo
        endif
c
      endif
c
c slow control histos
c
      if(irunpd.gt.7 .and. histo_switch(7).eq.1)then
         do i = 0, nmodhv_cal_max - 1
            write(cd, '(i1)') i
            ct = 'High Voltage - Cal module '//cd
            call hbook1(6600+i,ct,nchhv_cal_max,0.5,
     +           float(nchhv_cal_max)+0.5,0.)
            ct = 'Current - Cal module '//cd
            call hbook1(6610+i,ct,nchhv_cal_max,0.5,
     +           float(nchhv_cal_max)+0.5,0.)
         enddo
         do i = 0, nmodhv_pm_max - 1
            write(cd, '(i1)') i
            ct = 'High Voltage - PM module '//cd
            call hbook1(6620+i,ct,nchhv_pm_max,0.5,
     +           float(nchhv_pm_max)+0.5,0.)
            ct = 'Current - PM module '//cd
            call hbook1(6630+i,ct,nchhv_pm_max,0.5,
     +           float(nchhv_pm_max)+0.5,0.)
         enddo
         do i = 0, nmodhv_mwpc_max - 1
            write(cd, '(i1)') i
            ct = 'HV - MWPC module '//cd
            call hbook1(6640+i,ct,nchhv_mwpc_max,0.5,
     +           float(nchhv_mwpc_max)+0.5,0.)
            ct = 'Current - MWPC module '//cd
            call hbook1(6650+i,ct,nchhv_mwpc_max,0.5,
     +           float(nchhv_mwpc_max)+0.5,0.)
         enddo
         ct = 'Temperature in Cryostat (K)'
         call hbook1(6660,ct,nval_temp_max,0.,
     +        float(nval_temp_max),0.)
         ct = 'Lar vapor pressure (mbar)'
         call hbook1(6661,ct,1,1.,-1.,0.)
c
      endif
c
      return
      end













