C----------------------------------------------------------------------
      SUBROUTINE HWABEG
C     USER'S ROUTINE FOR INITIALIZATION
C----------------------------------------------------------------------
c        1         2         3         4         5         6         7
      INCLUDE 'HERWIG65.INC'

      integer istr_stdhep,iev,lok
      common /stdhep_io/ istr_stdhep,iev

c --- OPEN FILE AND RECORD ASSIGNED ISTREAM :
c --- =======================================
      write(6,*) 'mcatnlo_to_stdhep : HWABEG'
      write(6,*) 'mcatnlo_to_stdhep : expecting ',maxev,' events'
      write(6,*) 'mcatnlo_to_stdhep : opening file mcatnlo.stdhep'
      call stdxwinit('mcatnlo.stdhep','mcatnlo',maxev,istr_stdhep,lok)
      if (lok.NE.0) then
         write(6,*) 'mcatnlo_to_stdhep : ERROR in call to stdxwinit'
      endif

      iev=0

      END
C----------------------------------------------------------------------
      SUBROUTINE HWAEND
C     USER'S ROUTINE FOR TERMINAL CALCULATIONS, HISTOGRAM OUTPUT, ETC
C----------------------------------------------------------------------
      INCLUDE 'HERWIG65.INC'

      integer istr_stdhep,iev
      common /stdhep_io/ istr_stdhep,iev

c --- CLOSE STDHEP FILE :
c --- ===================
      write(6,*) 'mcatnlo_to_stdhep : HWAEND'
      write(6,*) 'mcatnlo_to_stdhep : ',iev,' events written'
      write(6,*) 'mcatnlo_to_stdhep : closing file mcatnlo.stdhep'
      call stdxend(istr_stdhep)

      END

C----------------------------------------------------------------------
      SUBROUTINE HWANAL
C     USER'S ROUTINE TO ANALYSE DATA FROM EVENT
C----------------------------------------------------------------------
      INCLUDE 'HERWIG65.INC'
      DOUBLE PRECISION HWVDOT,PSUM(4)
      INTEGER ICHSUM,ICHINI,IHEP
      LOGICAL DIDSOF
      REAL*8 WWW0

c --- STDHEP common block that contains event weight information.
c --- This should be brought in with a relevant "INCLUDE", but for
c --- now put it in by hand :
      double precision eventweightlh, scalelh
      double precision alphaqedlh, alphaqcdlh, spinlh
      integer          icolorflowlh, idruplh
c
      common/hepev4/eventweightlh, alphaqedlh, alphaqcdlh, scalelh(10),
     1              spinlh(3,NMXHEP), icolorflowlh(2,NMXHEP), idruplh
      save /hepev4/

      integer istr_stdhep,iev,lok
      common /stdhep_io/ istr_stdhep,iev

c --- Return if a Herwig error :
      IF (IERROR.NE.0) RETURN

c --- Debug print-out to make sure HEPEVT common correctly filled :
c     write(6,*) 'HWANAL : nhep, evwgt = ',nhep,' , ',evwgt

c --- TREATMENT OF NEGATIVE WEIGHTS :
c --- ===============================
c --- Fill the eventweightlh entry of the HEPEV4 common block of
c --- STDHEP. 
c --- However, since HEPG does not have an event weight entry, we
c --- also adopt the following scheme : if this is a negative weight 
c --- event, add 0.1 GeV to the energy of the first particle in the 
c --- event record. Note that MC@NLO should always generate weights 
c --- of uniform magnitude, so only the sign needs to be recorded.
c --- Apparently this doesn't interfere with momentum conservation 
c --- checking below.
      eventweightlh=evwgt
      if (evwgt<0.0) then
c        write(6,*) 'NEGATIVE WEIGHT EVENT'
         PHEP(4,1)=PHEP(4,1)+0.1
      endif

      iev=iev+1
      if (mod(iev,500).EQ.0) then
         write(6,*) 'mcatnlo_to_stdhep : writing event ',iev
      endif
      call stdxwrt(1,istr_stdhep,lok)
      if (lok.NE.0) then
         write(6,*) 'mcatnlo_to_stdhep : ERROR in call to stdxwrt'
      endif

c --- HERWIG CODE TO CHECK MOMENTUM AND CHARGE CONSERVATION :
c --- =======================================================
      WWW0=EVWGT
      CALL HWVSUM(4,PHEP(1,1),PHEP(1,2),PSUM)
      CALL HWVSCA(4,-1D0,PSUM,PSUM)
      ICHSUM=0
      ICHINI=ICHRG(IDHW(1))+ICHRG(IDHW(2))
      DIDSOF=.FALSE.
      DO 100 IHEP=1,NHEP
C     UNCOMMENT THE FOLLOWING WHEN REMOVING THE CHECK ON MOMENTUM 
C     IF(IWP*IWM.EQ.1) GOTO 11
         IF (IDHW(IHEP).EQ.16) DIDSOF=.TRUE.
         IF (ISTHEP(IHEP).EQ.1) THEN
            CALL HWVSUM(4,PHEP(1,IHEP),PSUM,PSUM)
            ICHSUM=ICHSUM+ICHRG(IDHW(IHEP))
         ENDIF
 100  CONTINUE      
C     CHECK MOMENTUM CONSERVATION
cc    write(6,*) 'HWVDOT(3,PSUM,PSUM) = ',HWVDOT(3,PSUM,PSUM)
cc    write(6,*) 'PHEP(4,1) = ',PHEP(4,1)
      IF (HWVDOT(3,PSUM,PSUM).GT.1.E-4*PHEP(4,1)**2) THEN
         CALL HWWARN('HWANAL',1,*101)
 101     PRINT 102, PSUM
 102     FORMAT(' Bad Momentum Sum = ',4F9.3/)
         CALL HWUEPR
         STOP
      ENDIF
      IF (ICHSUM.NE.ICHINI) THEN
         CALL HWWARN('HWANAL',2,*201)
 201     PRINT 202, ICHSUM
 202     FORMAT(' Bad Charge Sum = ',I5/)
         CALL HWUEPR
         STOP
      ENDIF
 999  END
      
      
