C...Interface from 4-fermion generator at LEP 2 to Jetset showers and
C...fragmentation. 

C...First comes very stupid main program to test interface below.
      PARAMETER (NMXHEP=2000)
      COMMON/HEPEVT/NEVHEP,NHEP,ISTHEP(NMXHEP),IDHEP(NMXHEP),
     &JMOHEP(2,NMXHEP),JDAHEP(2,NMXHEP),PHEP(5,NMXHEP),VHEP(4,NMXHEP) 
      DOUBLE PRECISION PHEP, VHEP
      COMMON/LUDAT3/MDCY(500,3),MDME(2000,2),BRAT(2000),KFDP(2000,5) 
      
C...Loop over events. Decide how many entries. Reset.
      DO 200 IEV=1,1000
      NEVHEP=IEV
      NHEP=6+INT(4.*RLU(0))
      DO 110 I=1,NHEP
      ISTHEP(I)=1
      IDHEP(I)=22
      JMOHEP(1,I)=0
      JMOHEP(2,I)=0
      JDAHEP(1,I)=0
      JDAHEP(2,I)=0
      DO 100 J=1,5
  100 PHEP(J,I)=0.
      DO 110 J=1,4
  110 VHEP(J,I)=0.    

C...Generate flavours at random.
      ISTHEP(1)=3 
      IDHEP(1)=-11
      ISTHEP(2)=3
      IDHEP(2)=11
      DO 120 IDL=MDCY(24,2),MDCY(24,2)+MDCY(24,3)-1 
      BRSU=BRSU+BRAT(IDL) 
  120 CONTINUE 
      DO 150 I=3,5,2 
  130 RBR=BRSU*RLU(0) 
      IDL=MDCY(24,2)-1 
  140 IDL=IDL+1 
      IDC=IDL 
      RBR=RBR-BRAT(IDL) 
      IF(IDL.LT.MDCY(24,2)+MDCY(24,3)-1.AND.RBR.GT.0.) GOTO 140 
      IF(I.EQ.3) THEN 
        IDHEP(I)=KFDP(IDC,2)
        IDHEP(I+1)=KFDP(IDC,1)
      ELSE
        IDHEP(I)=-KFDP(IDC,1)
        IDHEP(I+1)=-KFDP(IDC,2)
      ENDIF
      PHEP(5,I)=ULMASS(IDHEP(I))
      PHEP(5,I+1)=ULMASS(IDHEP(I+1))
      IF(PHEP(5,I)+PHEP(5,I+1).GT.20.) GOTO 130
  150 CONTINUE

C...Generate momenta at random.
      DO 160 I=3,NHEP        
      PLEN=50.
      IF(I.GE.5) PLEN=5.
      PHEP(1,I)=PLEN*(2.*RLU(0)-1.)          
      PHEP(2,I)=PLEN*(2.*RLU(0)-1.)          
      PHEP(3,I)=PLEN*(2.*RLU(0)-1.) 
  160 PHEP(4,I)=SQRT(PHEP(1,I)**2+PHEP(2,I)**2+PHEP(3,I)**2+
     &PHEP(5,I)**2)

C....Call interface to JETSET. List generated event.
      IERR=0         
      CALL LU4FRM(1.,0.5,0.5,2,0,0,IERR)
      IF(IEV.LE.10) CALL LULIST(1)
  200 CONTINUE

      END

C*******************************************************************

C...The following subroutine illustrates how to write a generic
C...interface between electroweak generators and QCD generators
C...for LEP 2 applications. 
C...
C...In this particular case, an electroweak generator is supposed to 
C...have produced two fermions, two antifermions and an arbitrary 
C...number of photons. These particles are stored in the HEPEVT
C...common block. The allowed order is specified by a standard. 
C...In brief, the final fermions should appear in the order
C...fermion (1) - antifermion (2) - fermion (3) - antifermion (4). 
C...The flavour pairs should be arranged so that, if possible, the
C...first two could come from a W+ and the second two from a W-; 
C...else each pair should have flavours consistent with a Z0. 
C...The subroutine LU4FRM is supposed to read the configuration,
C...and call JETSET to do parton showers and fragmentation.
C...
C...Since the colour flow need not be unique, three real numbers 
C...should be given when LU4FRM is called:
C...ATOTSQ = total squared amplitude for the event, irrespective of
C...    colour flow;
C...A1SQ = squared amplitude for the configuration with 1 + 2 and
C...    3 + 4 as the two colour singlets; and  
C...A2SQ = squared amplitude for the configuration with 1 + 4 and
C...    3 + 2 as the two colour singlets.
C...
C...The choice of strategy is determined by an integer input:
C...ISTRAT = 0 : pick configurations according to A1SQ : A2SQ;
C...       = 1 : assign interference to maximize 1 + 2 and 3 + 4; or
C...       = 2 : assign interference to maximize 1 + 4 and 3 + 2.
C...
C...Final-state QED radiation may be allowed or inhibited:
C...IRAD = 0 : no final-state photon radiation.
C...     = 1 : photon radiation inside each final fermion pair.
C...
C...tau lepton decay may be  handled by QCD generator or not.
C...ITAU = 0 : taus are considered stable by QCD generator.
C...     = 1 : taus are allowed to decay by QCD generator.
C...
C...IERR is an error flag, used both as input and output.
C...At input, 0 means leave routine in case of error, 
C...   nonzero means stop program execution.
C...At output, 0 means acceptable fermion configuration,
C...   nonzero means treatment aborted for some reason.
C...It is up to the writer of the main program to pick error strategy,
C...i.e. to let the program crash or try to fix errors.    

C...Program changed 20 December 1996: add QED radiation off
C...lepton-neutrino pair for IRAD=1; previously IRAD=1 only
C...affected photon radiation off quark-antiquark. 
C...Program changed 6 August 1998: tau's that had radiated photons
C...were not correctly picked up and forbidden to decay in the
C...ITAU=0 option.

      SUBROUTINE LU4FRM(ATOTSQ,A1SQ,A2SQ,ISTRAT,IRAD,ITAU,IERR)

      COMMON/LUJETS/N,K(4000,5),P(4000,5),V(4000,5)
      COMMON/LUDAT1/MSTU(200),PARU(200),MSTJ(200),PARJ(200)
      DIMENSION IJOIN(2),INTAU(4)

C...Call LUHEPC to convert from HEPEVT to LUJETS common.
      INERR=0
      MSTU(28)=0
      CALL LUHEPC(2)
      IF(MSTU(28).EQ.8) INERR=1

C...Loop through entries and pick up all final fermions/antifermions.
      I1=0
      I2=0
      I3=0
      I4=0
      DO 100 I=1,N
      IF(K(I,1).LE.0.OR.K(I,1).GT.10) GOTO 100
      KFA=IABS(K(I,2))
      IF((KFA.GE.1.AND.KFA.LE.6).OR.(KFA.GE.11.AND.KFA.LE.16)) THEN
        IF(K(I,2).GT.0) THEN
          IF(I1.EQ.0) THEN
            I1=I
          ELSEIF(I3.EQ.0) THEN
            I3=I
          ELSE
            INERR=2
            CALL LUERRM(6,'(LU4FRM:) more than two fermions')
          ENDIF
        ELSE
          IF(I2.EQ.0) THEN
            I2=I
          ELSEIF(I4.EQ.0) THEN
            I4=I
          ELSE 
            INERR=3
            CALL LUERRM(6,'(LU4FRM:) more than two antifermions')
          ENDIF
        ENDIF
      ENDIF
  100 CONTINUE

C...Check that event is arranged according to conventions. 
      IF(I1.EQ.0.OR.I2.EQ.0.OR.I3.EQ.0.OR.I4.EQ.0) THEN
        INERR=4
        CALL LUERRM(6,'(LU4FRM:) event contains too few fermions')
      ENDIF
      IF(I2.LT.I1.OR.I3.LT.I2.OR.I4.LT.I3) THEN
        INERR=5
        CALL LUERRM(6,'(LU4FRM:) fermions arranged in wrong order')
      ENDIF

C...Check which fermion pairs are quarks and which leptons.
      IF(IABS(K(I1,2)).LT.10.AND.IABS(K(I2,2)).LT.10) THEN
        IQL12=1
      ELSEIF(IABS(K(I1,2)).GT.10.AND.IABS(K(I2,2)).GT.10) THEN
        IQL12=2
      ELSE
        INERR=6
        CALL LUERRM(6,'(LU4FRM:) first fermion pair inconsistent') 
      ENDIF
      IF(IABS(K(I3,2)).LT.10.AND.IABS(K(I4,2)).LT.10) THEN
        IQL34=1
      ELSEIF(IABS(K(I3,2)).GT.10.AND.IABS(K(I4,2)).GT.10) THEN
        IQL34=2
      ELSE
        INERR=7
        CALL LUERRM(6,'(LU4FRM:) second fermion pair inconsistent') 
      ENDIF

C...Return or stop program in case of problems.
      IF(INERR.EQ.0) THEN
        IERR=0
      ELSEIF(IERR.EQ.0) THEN
        IERR=INERR
        RETURN
      ELSE
        WRITE(6,*) ' ERROR: listing of faulty event follows:'
        CALL LULIST(2)
        WRITE(6,*) ' Fermions found in lines ',I1,I2,I3,I4
        WRITE(6,*) ' Error type in event above is ',INERR
        WRITE(6,*) ' Program execution will be stopped now'
        WRITE(6,*) ' since main program does not correct errors!'
        STOP
      ENDIF   

C...Decide whether to allow or not photon radiation in showers.
      MSTJ(41)=2
      IF(IRAD.EQ.0) MSTJ(41)=1 

C...Decide on colour pairing.
      IF(IQL12.EQ.2.AND.IQL34.EQ.2) THEN
        NPAIR=0
      ELSEIF(IQL12.EQ.1.AND.IQL34.EQ.2) THEN
        NPAIR=1
        IP1=I1
        IP2=I2
      ELSEIF(IQL12.EQ.2.AND.IQL34.EQ.1) THEN
        NPAIR=1
        IP1=I3
        IP2=I4
      ELSE
        NPAIR=2
        IP1=I1
        IP3=I3
        R1SQ=A1SQ
        R2SQ=A2SQ
        DELTA=ATOTSQ-A1SQ-A2SQ
        IF(ISTRAT.EQ.1) THEN
          IF(DELTA.GT.0.) R1SQ=R1SQ+DELTA
          IF(DELTA.LT.0.) R2SQ=MAX(0.,R2SQ+DELTA)
        ELSEIF(ISTRAT.EQ.2) THEN
          IF(DELTA.GT.0.) R2SQ=R2SQ+DELTA
          IF(DELTA.LT.0.) R1SQ=MAX(0.,R1SQ+DELTA)
        ENDIF   
        IF(R2SQ.LT.RLU(0)*(R1SQ+R2SQ)) THEN
          IP2=I2
          IP4=I4
        ELSE
          IP2=I4
          IP4=I2
        ENDIF
      ENDIF         
 
C...Do colour joining and parton showers.
      IF(NPAIR.GE.1) THEN
        IJOIN(1)=IP1
        IJOIN(2)=IP2
        CALL LUJOIN(2,IJOIN)
        PM12S=(P(IP1,4)+P(IP2,4))**2-(P(IP1,1)+P(IP2,1))**2-
     &  (P(IP1,2)+P(IP2,2))**2-(P(IP1,3)+P(IP2,3))**2
        CALL LUSHOW(IP1,IP2,SQRT(MAX(0.,PM12S)))
      ENDIF
      IF(NPAIR.EQ.2) THEN 
        IJOIN(1)=IP3
        IJOIN(2)=IP4
        CALL LUJOIN(2,IJOIN)
        PM34S=(P(IP3,4)+P(IP4,4))**2-(P(IP3,1)+P(IP4,1))**2-
     &  (P(IP3,2)+P(IP4,2))**2-(P(IP3,3)+P(IP4,3))**2
        CALL LUSHOW(IP3,IP4,SQRT(MAX(0.,PM34S)))
      ENDIF

C...Do optional QED showers for lepton/neutrino pairs.
C...(Inserted 20 December 1996.)
      IF(IRAD.EQ.1) THEN
        IF(IQL12.EQ.2) THEN
          PM12S=(P(I1,4)+P(I2,4))**2-(P(I1,1)+P(I2,1))**2-
     &    (P(I1,2)+P(I2,2))**2-(P(I1,3)+P(I2,3))**2
          CALL LUSHOW(I1,I2,SQRT(MAX(0.,PM12S)))
        ENDIF
        IF(IQL34.EQ.2) THEN
          PM34S=(P(I3,4)+P(I4,4))**2-(P(I3,1)+P(I4,1))**2-
     &    (P(I3,2)+P(I4,2))**2-(P(I3,3)+P(I4,3))**2
          CALL LUSHOW(I3,I4,SQRT(MAX(0.,PM34S)))
        ENDIF
      ENDIF

C...Do fragmentation and decays. Possibly except tau decay.
C...(Modified 6 August 1998.)
      IF(ITAU.EQ.0) THEN
        NTAU=0
        DO 110 I=1,N
        IF(IABS(K(I,2)).EQ.15.AND.K(I,1).EQ.1) THEN
          NTAU=NTAU+1
          INTAU(NTAU)=I
          K(I,1)=11
        ENDIF
  110   CONTINUE
      ENDIF       
      CALL LUEXEC
      IF(ITAU.EQ.0) THEN
        DO 120 I=1,NTAU
        K(INTAU(I),1)=1
  120   CONTINUE
      ENDIF       
 
      END       
  












      
          
       

      
          
       

