      subroutine hec_ntuple_book
c
c routine to initialize column-wise ntuples for ntuple package
c
c               name           type  description
c run ntuple:
c               hec_runno        i    run number
c               hec_runpd        i    run period number
c               hec_beame        r    beam energy
c               hec_noevt        i    number of events
c               hec_parttype     i    particle type
c               hec_cryox        r    cryostat position in x
c               hec_tabley       r    table position in y
c               hec_zbeam        r    for MWPCs, z position at which x and y 
c                                     are calculated
c               hec_peakf        i    peak finding method used 
c                                     1 = cubic
c                                     2 = digital filtering
c                                     0 = pure MC
c                                    -1 = spoiled MC with ordinary noise
c                                    -2 = spoiled MC with noise as after 
c                                         digital filtering
c               hec_cal_version  i    version of calibration used
c               hec_dig_version  i    version of digital filtering weights used
c               hec_shower       i    shower model type 
c                                     0 = experimental data
c                                     3 = GHEISHA
c                                     4 = G-FLUKA
c                                     6 = G-CALOR
c               hec_eunit        i    units of energy 
c                                     (1=adc,2=nA,3=GeV,4=MC visible energy)
c               hec_adc_max      i    maximum adc channel number (eg. 144)
c               hec_index        i    index of channel with a given adc number 
c
c               hec_cells_used   i    number of cells used (eg. <= 144)
c               hec_ped_rms      r    run pedestal rms for each channel
c               hec_ieta         i    eta value for each channel
c               hec_iphi         i    phi value for each channel
c               hec_iz           i    z value for each channel
c               hec_ic           i    adc channel number
ccccccccccccccccc
c
c event ntuple:
c        standard event block (hecevt)
c               hec_evtno        i    event number
c               hec_trig         i    trigger flag array 
c                                     (physics,electron,pion,muon,random)
c               hec_signal       r    energy (signal) for each channel
c        MWPC block (hecmwpc)
c               hec_xbeam        r    beam x-coordinate at z=zbeam
c               hec_ybeam        r    beam y-coordinate at z=zbeam
c               hec_xslop        r    trajectory x-slop: at any z  
c                                     x=xbeam+xslop*(z-zbeam)
c               hec_yslop        r    trajectory y-slop: at any z  
c                                     y=ybeam+yslop*(z-zbeam)
c               hec_xbeam_ok     i    =1 if beam x-projection is successfully 
c                                     reconstructed
c               hec_ybeam_ok     i    =1 if beam y-projection is successfully 
c                                     reconstructed
c
ccccccccccccccccc
c
c slow control ntuple: 
c
c               hec_time         i    UNIX binary time data was taken
c               hec_lartemp      r    liquid argon temperature
c               hec_press        r    cryostat pressure
c               hec_adc_used     i    as hec_cells_used in run ntuple 
c               hec_hvcor        r    HV correction factors for each cell:
c                                     1., 4./3., 4./2., 4.  or 0 if
c                                     SC bank missing/unreliable
c
        implicit none
c
c parameter and geometry commons
c
        include'hec_par.inc'        ! shared parameters
        include'hec_datacard.inc'   ! datacard values
        include'hec_geo.inc'        ! geometry correspondance tables
        include'hec_ntuple.inc'     ! ntuple common
        include'hec_ntuple_sys.inc' ! ntuple system common
c
        integer istat, num
	character*512 tags_runh_a,tags_runh_b,tags_runh_c,tags_evt_a
        character*512 tags_evt_b,tags_mwpc,tags_sc
        character*3 cnum
c
c initialize list of column names for run header
c
        num = i_adc_dim
        num = min(999, max(num, 100))
        write(cnum(1:3), '(i3)') num
	tags_runh_a = 'hec_runno:i,hec_runpd:i,hec_beame:r,'//
     +  'hec_noevt:i,'//
     +  'hec_parttype:i,'//
     +  'hec_cryox:r,hec_tabley:r,hec_zbeam:r,'//
     +  'hec_peakf:i,hec_cal_version:i,hec_dig_version:i,'//
     +  'hec_shower:i,hec_eunit:i'
        tags_runh_b = 'hec_adc_max[0,'//cnum//']:i,'//
     +  'hec_index(hec_adc_max):i'
        tags_runh_c = 'hec_cells_used[0,'//cnum//']:i,'//
     +  'hec_ped_rms(hec_cells_used):r,'//
     +  'hec_ieta(hec_cells_used):i,'//
     +  'hec_iphi(hec_cells_used):i,'//
     +  'hec_iz(hec_cells_used):i,'//
     +  'hec_ic(hec_cells_used):i'
c
c names for event ntuple - standard block
c
        tags_evt_a = 'hec_evtno:i,hec_trig(5):i'
        tags_evt_b = 'hec_signal('//cnum//'):r'
c
c names for event ntuple - mwpc block
c
        tags_mwpc = 'hec_xbeam:r,hec_ybeam:r,hec_xslop:r,hec_yslop:r,'//
     +              'hec_xbeam_ok:i,hec_ybeam_ok:i'
c
c names for slow control ntuple
c
        tags_sc = 'hec_time:i,hec_lartemp(8):r,hec_press:r,'//
     +            'hec_adc_used[0,'//cnum//']:i,'//
     +            'hec_hvcor(hec_adc_used):r'
c
c open hbook file for standard ntuple
c
	call hropen(io_ntp,'ntuple','ntuple.hbook','n',1024,istat)
	if (istat .ne. 0) then
	   print*, 'error opening ntuple file'
	   return
	endif
c
c initialize (book) ntuples (disk resident)
c
	call hbnt(id_ntuple_run,'standard run ntuple','D')
	call hbnt(id_ntuple_evt,'standard event ntuple','D')
	call hbnt(id_ntuple_slc,'standard slow control ntuple','D')
c
c define columns for run header ntuple
c
	call hbname(id_ntuple_run,'hecrunh',hec_runno,tags_runh_a)
	call hbname(id_ntuple_run,'hecrunh',hec_adc_max,tags_runh_b)
	call hbname(id_ntuple_run,'hecrunh',hec_cells_used,tags_runh_c)
c
c define columns for event ntuple standard block
c
	call hbname(id_ntuple_evt,'hecevt',hec_evtno,tags_evt_a)
        call hbname(id_ntuple_evt,'hecevt',hec_signal,tags_evt_b)
c
c define columns for event ntuple MWPC block (if requested)
c
	if (mk_beam .ne. 0) 
     +    call hbname(id_ntuple_evt,'hecmwpc',hec_xbeam,tags_mwpc)
c
c define columns for slow control ntuple
c
	call hbname(id_ntuple_slc,'hecsc',hec_time,tags_sc)
c
        return 
        end










