
      program tstpyt
C
C      test program for stdhep/pythia interface
C      compare event list
C      write events and cross-section
C
#include "stdlun.inc"

      integer istr,mstat
      integer nevt,i,nprnt,lok
      CHARACTER*10 CHNEV
      CHARACTER*72 LONGSTRING
C
      CALL GETENV('NUMBER_OF_EVENTS',CHNEV)
      CALL GETENV('PYTHIA_CARD',LONGSTRING)
      READ(CHNEV,'(I10)') NEVT
      nprnt=10
      call upyinit(nevt,istr,LONGSTRING)

C...
C     call mcfio_FPrintDictionary(lnhout)
c     call cntcode(1)
c     CALL hptrlstj
C          Fill Stdhep common block 1 (only valid for Pythia)
      call stdflpyxsec(nevt)
C          Write Stdhep begin-run record   
      call stdxwrt(100,istr,lok)
      if(lok.ne.0) write(lnhout,*)
     1        ' Problem writing stdhep begin run record'
c
      do 100 i=1,nevt
        call pyevnt
        call lunhep(1)
        if(i.le.nprnt)then
          call pylist(3)
          call heplst(1)
        endif
c       call tstflem(20)
ccall cntcode(2)
        call stdxwrt(1,istr,lok)
C          Fill Stdhep common block 1 (only valid for Pythia)
        call stdflpyxsec(nevt)
C       call tstflxsec(20)
 100  continue
C
C          Fill Stdhep common block 1
      call stdflpyxsec(nevt)
C          Write end-of-run record  
      call stdxwrt(200,istr,lok)
      if(lok.ne.0) write(lnhout,*) ' Problem writing end run record'

C...   Print end of run statistics.
      mstat=1
      call pystat(mstat)
      call pylist(12)

c...close event file
      call stdxend(istr)
c     call cntcode(3)
C...print histograms
c     call houtpu(lnhout)
c     call histdo
c...close output file
      close(unit=lnhout)
      stop
      end

      subroutine upyinit(nevt,istr,LONGSTRING)
C
C   Pythia initialization
C
#include "pydat1.inc"
#include "stdlun.inc"

C...   PYTHIA initialization variables.
      character*4 frame
      character*5 beam,target
      integer nevt,istr,iloc
      double precision win
      CHARACTER*72 SIN
      CHARACTER*72 LONGSTRING
      CHARACTER*72 TMPSTRING
C
C initialize HEP logical units
      lnhwrt=23
      lnhrd=0
      lnhdcy=0
      lnhout=22
      lnhpdf=0
      lnhdmp=0
      mstu(11)=lnhout
      iloc=index(LONGSTRING,".")
      TMPSTRING=LONGSTRING(1:iloc)//'lpt'
      open(unit=lnhout,file=TMPSTRING,status='new')
      TMPSTRING=LONGSTRING(1:iloc)//'stdhep'
      call stdxwinit(TMPSTRING,LONGSTRING(1:iloc),nevt,istr,lok)
C
C     call tstbkem(20)
C
C...   Initialize PYTHIA
      frame  = 'cms'
      beam   = 'mu+'
      target = 'mu-'
      win    = 3000.0d0
      OPEN(UNIT=77,FILE=LONGSTRING,status='unknown')
      DO I=1,1000
       READ(77,'(A)',ERR=100,END=100) SIN
       CALL PYGIVE(SIN)
      ENDDO
100   CONTINUE
      CLOSE(77)
      call pyinit(frame,beam,target,win)

      return
      end

      subroutine cntcode(ii)
c
c  make a table of isthep values
c
#include "stdhep.inc"
#include "stdlun.inc"
      integer ii,i,icnt(200)
      save icnt

      if(ii.eq.1)then
         do i=1,200
	   icnt(i)=0
	 enddo
      elseif(ii.eq.2)then
         do i=1,nhep
	   ist = isthep(i)
	   if(ist.le.200 .and. ist.gt.0) icnt(ist) = icnt(ist) + 1
	 enddo
      elseif(ii.eq.3)then
         write(lnhout,1001) 
	 do i=1,200
            write(lnhout,1002) i,icnt(i)
	 enddo
      else
      endif

      return
 1001 format(10x,'List of used isthep values')
 1002 format(' isthep ',i3,2x,i10)
      end
