      subroutine hec_his_fit_output 
c
      implicit none
c 
      include'hec_par.inc'         
      include'hec_runh.inc'
      include'hec_his_fit.inc'
c
c local variables
c     
      integer flag
      parameter (flag=0)
      logical hexist
      external hexist
c
c
c open output files in append mode if they exists, create
c them otherwise
c
      open(101,file='zcluster_adc.data',status='OLD',
     +     access='APPEND',err=1001)
      write(101,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +     z_clus_noent(1),z_clus_mean(1),z_clus_sig(1),
     +     z_clus_mean_err(1),z_clus_sig_err(1),
     +     z_clus_chi2(1),flag
c      
      go to 10
c
 1001 open(101,file='zcluster_adc.data',status='NEW',
     +     access='APPEND',err=2001)
      write(101,*)' '
      write(101,880)
      write(101,881)
      write(101,*)' '
      write(101,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +     z_clus_noent(1),z_clus_mean(1),z_clus_sig(1),
     +     z_clus_mean_err(1),z_clus_sig_err(1),z_clus_chi2(1),
     +     flag
c
 10   open(102,file='zcluster_cal.data',status='OLD',
     +     access='APPEND',err=1002)
      write(102,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +     z_clus_noent(2),z_clus_mean(2),z_clus_sig(2),
     +     z_clus_mean_err(2),z_clus_sig_err(2),
     +     z_clus_chi2(2),flag
c      
      go to 20
c
 1002 open(102,file='zcluster_cal.data',status='NEW',
     +     access='APPEND',err=2001)
      write(102,*)' '
      write(102,880)
      write(102,881)
      write(102,*)' '
      write(102,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +     z_clus_noent(2),z_clus_mean(2),z_clus_sig(2),
     +     z_clus_mean_err(2),z_clus_sig_err(2),z_clus_chi2(2),
     +     flag
c
 20   open(103,file='zcluster_hvcor.data',status='OLD',
     +     access='APPEND',err=1003)
      write(103,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +     z_clus_noent(3),z_clus_mean(3),z_clus_sig(3),
     +     z_clus_mean_err(3),z_clus_sig_err(3),
     +     z_clus_chi2(3),flag
c      
      go to 30
c
 1003 open(103,file='zcluster_hvcor.data',status='NEW',
     +     access='APPEND',err=2001)
      write(103,*)' '
      write(103,880)
      write(103,881)
      write(103,*)' '
      write(103,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +     z_clus_noent(3),z_clus_mean(3),z_clus_sig(3),
     +     z_clus_mean_err(3),z_clus_sig_err(3),z_clus_chi2(3),
     +     flag
c
 30   if(hexist(9091))then
          open(104,file='impactcluster_adc.data',status='OLD',
     +        access='APPEND',err=1004)
          write(104,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +         imp_clus_noent(1),imp_clus_mean(1),imp_clus_sig(1),
     +         imp_clus_mean_err(1),imp_clus_sig_err(1),
     +         imp_clus_chi2(1),flag     
          go to 40
 1004     open(104,file='impactcluster_adc.data',status='NEW',
     +         access='APPEND',err=2001)
          write(104,*)' '
          write(104,880)
          write(104,881)
          write(104,*)' '
          write(104,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +         imp_clus_noent(1),imp_clus_mean(1),imp_clus_sig(1),
     +         imp_clus_mean_err(1),imp_clus_sig_err(1),
     +         imp_clus_chi2(1),flag
      endif

 40   if(hexist(9092))then
          open(105,file='impactcluster_cal.data',status='OLD',
     +        access='APPEND',err=1005)
          write(105,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +         imp_clus_noent(2),imp_clus_mean(2),imp_clus_sig(2),
     +         imp_clus_mean_err(2),imp_clus_sig_err(2),
     +         imp_clus_chi2(2),flag     
          go to 50
 1005     open(105,file='impactcluster_cal.data',status='NEW',
     +         access='APPEND',err=2001)
          write(105,*)' '
          write(105,880)
          write(105,881)
          write(105,*)' '
          write(105,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +         imp_clus_noent(2),imp_clus_mean(2),imp_clus_sig(2),
     +         imp_clus_mean_err(2),imp_clus_sig_err(2),
     +         imp_clus_chi2(2),flag
      endif

 50   if(hexist(9093))then
          open(106,file='impactcluster_hvcor.data',status='OLD',
     +        access='APPEND',err=1006)
          write(106,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +         imp_clus_noent(3),imp_clus_mean(3),imp_clus_sig(3),
     +         imp_clus_mean_err(3),imp_clus_sig_err(3),
     +         imp_clus_chi2(3),flag
          go to 60
 1006     open(106,file='impactcluster_hvcor.data',status='NEW',
     +         access='APPEND',err=2001)
          write(106,*)' '
          write(106,880)
          write(106,881)
          write(106,*)' '
          write(106,999)run_no,e_beam,i_part,x_cryo,y_tabl,
     +         imp_clus_noent(3),imp_clus_mean(3),imp_clus_sig(3),
     +         imp_clus_mean_err(3),imp_clus_sig_err(3),
     +         imp_clus_chi2(3),flag
      endif
 60   return
c
 2001 print*,'Error opening output file. Hec_his_fit_output Exiting'
c
 880  Format(5x,'Run',4x,'Beam',4x,'Particle',2x,'Impact_Point',
     +     4x,'N',16x,'Gaussian_fit_results')
 881  Format(4x,'Number',1x,'Energy',5x,'Type',6x,'X',
     +     6x,'Y',4x,'events',3x,'Mean',3x,'Sigma',2x,'Mean_err',
     +     2x,'Sigma_err',2x,'chi2',2x,'Flag')
 999  Format(3x,i6,2x,f6.1,6x,i2,2x,f6.1,1x,f6.1,4x,i5,
     +     3x,2(f6.2,1x),2x,f5.2,6x,f5.2,3x,f5.2,5x,i1)
c
      end





