      SUBROUTINE SWW4F(generator)
C Introduce in your program the common ww4f
C-----------------------------------------------------------------------
C Routine to give the 4 fermions produced
C Igami : MC track number from ISR photons (max 10)
C IWW4F : MC track number of 4 produced fermions
C IGAMF : MC track number of FSR fermions (max 5)
C-----------------------------------------------------------------------
#include "qcde.h"
#include "qdecl.h"
      INTEGER GET_GENERATOR,GENERATOR
      INTEGER IGAMI,IWW4F,IGAMF
      COMMON/WW4F/IGAMI(10),IWW4F(4),IGAMF(5)
#include "qmacro.h"
*
* Search for generator number
*
      GENERATOR    = GET_GENERATOR()
      generator=mod(generator,10000)
*
      CALL VZERO(IGAMI,10)
      CALL VZERO(IWW4F,4)
      CALL VZERO(IGAMF,5)
*
      IF (generator.eq.5034.or.generator.eq.5035.
     & or.generator.eq.5038) then
       call std_part
      elseif(generator.eq.5028) then
       call exca_part
      else
       print *,'This generator is not a WW/ZZ one',generator
      endif
*     
      RETURN
      END

c-------------------------------------------------------------------------------
c author : S. Jezequel
c! outputs : kingal generator number
c-------------------------------------------------------------------------------
      integer function get_generator()
      external namind
      integer jkrun
#include "qdecl.h"
#include "qhac.h"
#include "qcde.h"
#include "qmacro.h"
      jkrun=iw(namind('KRUN'))
      if (jkrun.eq.0) then
        call qwmese('_GET_GENERATOR : no KRUN bank - return')
        get_generator = 0
        go to 999
      endif
      get_generator = itabl(jkrun,1,1)
  999 return
      end

      SUBROUTINE STD_PART
C-----------------------------------------------------------------------
C  Process one event
C-----------------------------------------------------------------------

#include "qdecl.h"
#include "qcde.h"
      DIMENSION MC_W(2)
      COMMON/WW4F/IGAMI(10),IWW4F(4),IGAMF(5)
      INTEGER MC_INT
#include "qmacro.h"
*
*       DO IU=KFMCT,KFMCT+15
*        print *,'iu',iu,ktpcod(iu),qch(iu),knmoth(iu),kndau(iu)
*        if(ktpcod(iu).eq.44) print *,(ktpcod(kdau(iu,i)),i=1,kndau(iu))
*       ENDDO
* Find ISR
       NGAMI=0
       MC_G=KPDIR('GAMMA',KMONTE)
        IF (MC_G.GT.0) THEN
          DO WHILE (MC_G.GT.0)
            IF(KNMOTH(MC_G).EQ.0) THEN
             NGAMI=NGAMI+1
             IF(NGAMI.GT.10) print *,'too much ISR'
             IGAMI(NGAMI)=MC_G
            ENDIF
             MC_G=KFOLLO(MC_G)
           ENDDO
        ENDIF
C FIND THE W'S
         MC_INT = KPDIR('W-',KMONTE)
         DO WHILE (MC_INT.GT.0)
           IF(KNMOTH(MC_INT).EQ.0) MC_W(1)=MC_INT
           MC_INT=KFOLLO(MC_INT)
         ENDDO
         MC_INT = KPDIR('W+',KMONTE)
         DO WHILE (MC_INT.GT.0)
           IF(KNMOTH(MC_INT).EQ.0) MC_W(2)=MC_INT
           MC_INT=KFOLLO(MC_INT)
         ENDDO
C FIND THE Z0'S
         IF(KPDIR('Z0',KMONTE).NE.0) THEN
          MC_W(1)=KPDIR('Z0',KMONTE)
          DO WHILE(KFOLLO(MC_W(1)).GT.0)
           MC_W(2)=MC_W(1) 
           MC_W(1)=KFOLLO(MC_W(2))
          ENDDO
         ENDIF
C Global initialization
         INUMF=0
C J.B. Hansen return if no Z or W found
         IF(MC_W(1).EQ.0.OR.MC_W(2).EQ.0) THEN
C          if(mod(kevt,1000).eq.1) then
C          print *,'no double Z or W found'
C          print *,'this program runs only with them'
CC          endif
          RETURN
         ENDIF
C TO GET DAUGTHERS OF THE W- or Z
         IDU=0
         DO INUM=1,KNDAU(MC_W(1))
          IWM = KDAU(MC_W(1),INUM)
          IF(KTPCOD(IWM).NE.KPART('gamma').AND.
     &       KTPCOD(IWM).NE.KTPCOD(MC_W(1))) THEN
           IDU=IDU+1
           IWW4F(IDU)=IWM
          ELSEIF(KTPCOD(IWM).EQ.KPART('GAMMA')) THEN
           INUMF=INUMF+1
           IGAMF(INUMF)=IWM
          ENDIF
         ENDDO
C TO GET DAUGTHERS OF THE W+ or Z
         DO INUM=1,KNDAU(MC_W(2))
          IWM = KDAU(MC_W(2),INUM)   
          IF(KTPCOD(IWM).NE.KPART('gamma').AND.
     &       KTPCOD(IWM).NE.KTPCOD(MC_W(2))) THEN
           IDU=IDU+1
           IWW4F(IDU)=IWM
          ELSEIF(KTPCOD(IWM).EQ.KPART('GAMMA')) THEN
           INUMF=INUMF+1
           IGAMF(INUMF)=IWM
          ENDIF
         ENDDO
      END

      SUBROUTINE EXCA_PART
C-----------------------------------------------------------------------
C  Process one event  of excalibur generator
C-----------------------------------------------------------------------

#include "qdecl.h"
#include "qcde.h"
      COMMON/WW4F/IGAMI(10),IWW4F(4),IGAMF(5)
#include "qmacro.h"
* Find ISR
       NGAMI=0
       NGAMF=0
       MC_G=KPDIR('GAMMA',KMONTE)
        IF (MC_G.GT.0) THEN
          DO WHILE (MC_G.GT.0)
            IF(KNMOTH(MC_G).EQ.0) THEN
             IF(MC_G-KFMCT.LE.2) THEN
              NGAMI=NGAMI+1
              IF(NGAMI.GT.10) print *,'too much ISR'
              IGAMI(NGAMI)=MC_G
             ELSE
              NGAMF=NGAMF+1
              IGAMF(NGAMF)=MC_G
             ENDIF
            ENDIF
             MC_G=KFOLLO(MC_G)
           ENDDO
        ENDIF
C
         IWW4F(3)=KFMCT
         do while (ktpcod(IWW4F(3)).eq.1)
           IWW4F(3)=IWW4F(3)+1
         enddo
         IWW4F(4)=IWW4F(3)+1
c watch out for FSR
         IWW4F(1)=IWW4F(4)+1
         do while (ktpcod(IWW4F(1)).eq.1)
           IWW4F(1)=IWW4F(1)+1
         enddo
         IWW4F(2)=IWW4F(1)+1
      END





