	SUBROUTINE AF_DRCO  ( report, lenr, iret )
C************************************************************************
C* AF_DRCO								*
C*									*
C* This subroutine decodes a RECCO report.				*
C*									*
C* AF_DRCO  ( REPORT, LENR, IRET )					*
C*									*
C* Input parameters:							*
C*	REPORT		CHAR*		RECCO report 			*
C*	LENR		INTEGER		Length of REPORT 		*
C*									*
C* Output parameters:							*
C*	CIVALS (ICRPID)	CHAR*		RECCO identifier		*
C*	RIVALS (IRDAYW)	REAL		Day of the week			*
C*	RIVALS (IRSLAT)	REAL		Latitude in degrees		*
C*	RIVALS (IRSLON)	REAL		Longitude in degrees		*
C*	RIVALS (IRNTRB)	REAL		Number of turbulence levels	*
C*	RIVALS (IRDGOT)	REAL		Degree of turbulence		*
C*	RIVALS (IRTPOT)	REAL		Type of turbulence		*
C*	RIVALS (IRPSAL)	REAL		Pressure altitude in feet  	*
C*	RIVALS (IRTMPC)	REAL		Temperature in Celsius		*
C*	RIVALS (IRDWPC)	REAL		Dewpoint temperature in Celsius	*
C*	RIVALS (IRNPWX)	REAL		Number of weather levels	*
C*	CIVALS (ICWCOD)	CHAR*		Weather string			*
C*	RIVALS (IRVSBY)	REAL		Visibility in miles		*
C*	RIVALS (IRSSTC)	REAL		Sea surface temp. in Celsius	*
C*	IRET		INTEGER		Return code 			*
C*					  0 = normal return 		*
C*									*
C**									*
C* Log:									*
C* J. Ator/NP12		11/96						*
C* J. Ator/NP12		01/97	Decode sea temperature, surface wind,	*
C*				icing, visibility, and cloud data 	*
C* J. Ator/NP12		08/97	New interface format, style changes	*
C* J. Ator/NCEP		03/98	Decode RPID following 'RMK' indicator	*
C* J. Ator/NCEP		12/98	Initialize rpids, lrpids via DATA stmts,*
C*				don't decode RPID from WMO 2nd hdr line	*
C* D. Kidwell/NCEP	 6/99   Changed turbulence values, added type   *
C* D. Kidwell/NCEP	 7/99   Changed code table for idayw, removed   *
C*			        irptr from call, psal meters to feet    *
C* J. Ator/NCEP		11/99	Declare field variable locally		*
C* J. Ator/NCEP		 4/03	Fix bug in decoding of temps < -50C	*
C* C. Caruso Magee/NCEP  3/04   Fix bug in decoding of dew points < -50C*
C*			        and fix bug in decoding of altitudes    *
C*			        at or above 10000 meters                *
C************************************************************************
	INCLUDE		'GEMPRM.PRM'
	INCLUDE		'afcmn.cmn'
C*
	CHARACTER*(*)	report
C*
	PARAMETER	( NRPIDS = 3 )
C*
	CHARACTER	rpids ( NRPIDS )*5, crid*8, field*(MXLENF)
C*
	INTEGER		lrpids ( NRPIDS )
C*
	DATA		rpids
     +		    / '97779', '92229', '95559' /
	DATA		lrpids
     +		    / 5, 5, 5 /
C-----------------------------------------------------------------------
	iret  = 0
	irptr = 1
C
C*	Set a default value for the RECCO identifier.
C
	civals ( icrpid ) = 'DRP99A'
C
C*	Locate the '97779', '92229', or '95559' indicator which
C*	denotes the start of the report.
C
	CALL ST_NXTS  ( report, irptr, ( irptr + 40 ), rpids, lrpids,
     +			NRPIDS, ipt1, irpid, iernxt )
	IF  ( iernxt .ne. 0 )  THEN
	    RETURN
	END IF
	irptr = irptr + ipt1 + ( lrpids ( irpid ) ) - 1
C
C*	Get the time group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the time (i.e. hour/minute) data.
C
	    CALL AF_HHMM  ( field (1:4), iertim )
C
C*	    Decode the id indicator for later use.
C
	    CALL ST_INTG  ( field (5:5), iidind, ier )
	ELSE
	    iidind = IMISSD
	END IF
C
C*	Get the latitude group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the day of the week data.
C
C*	    Day of the week values are stored in the interface format
C*	    as code figures from WMO code table 4900.
C
	    CALL ST_INTG  ( field (1:1), idayw, ier )
	    IF  ( ier .eq. 0 )  THEN
		rivals ( irdayw ) = FLOAT ( idayw )
	    END IF
C
C*	    Decode the octant of the globe.
C
	    CALL ST_INTG  ( field (2:2), iqoct, ier )
	    IF  ( ier .eq. 0 )  THEN
		IF  ( ( iqoct .eq. 4 ) .or. ( iqoct .eq. 9 ) )  THEN
		    iqoct = IMISSD
		END IF
	    END IF
C
C*	    Decode and store the latitude data.
C
	    CALL ST_INTG  ( field (3:5), islat, ier )
	    IF  ( ier .eq. 0 )  THEN
		slat = FLOAT ( islat ) / 10.0
		IF  ( iqoct .ne. IMISSD )  THEN
		    IF  ( ( iqoct .ge. 5 ) .and. ( slat .gt. 0 ) )  THEN
			slat = slat * (-1)
		    END IF
		    rivals ( irslat ) = slat
		END IF
	    END IF
	ELSE
	    iqoct = IMISSD
	END IF
C
C*	Get the longitude group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the longitude data.
C
	    CALL ST_INTG  ( field (1:3), islon, ier )
	    IF  ( ier .eq. 0 )  THEN
		slon = FLOAT ( islon ) / 10.0
		IF  ( iqoct .ne. IMISSD )  THEN
		    IF  ( ( iqoct .eq. 1 ) .or. ( iqoct .eq. 2 ) .or.
     +			  ( iqoct .eq. 6 ) .or. ( iqoct .eq. 7 ) )  THEN
			IF  ( INT ( slon ) .le. 80 )  THEN
			  slon = slon + 100.0
			END IF
		    END IF
		    IF  ( ( iqoct .eq. 0 ) .or. ( iqoct .eq. 1 ) .or.
     +			  ( iqoct .eq. 5 ) .or. ( iqoct .eq. 6 ) )  THEN
			IF  ( slon .gt. 0 )  THEN
			  slon = slon * (-1)
			END IF
		    END IF
		    rivals ( irslon ) = slon
		END IF
	    END IF
C
C*	    Decode and store the turbulence data.
C
C*	    Degree of turbulence values are stored in the interface
C*	    format using the following code figures -
C* 		NONE     = 0
C*		LIGHT    = 2
C*		MODERATE = 4
C*		SEVERE   = 6
C*	    Type of turbulence values are stored as
C*		In clear air = 1
C*		In cloud     = 4
C
	    CALL ST_INTG  ( field (4:4), ibind, ier )
	    IF  ( ier .eq. 0 )  THEN
		tpot = RMISSD
		IF  ( ibind .eq. 0 )  THEN
		    dgot = 0.0
		ELSE IF ( ibind .eq. 1 )  THEN
		    dgot = 2.0
		ELSE IF ( ( ibind .ge. 2 ) .and. ( ibind .le. 5 ) ) THEN
		    dgot = 4.0
		    IF ( ibind .le. 3 ) THEN
			tpot = 1.0
		      ELSE
			tpot = 4.0
		    END IF
		ELSE IF ( ( ibind .ge. 6 ) .and. ( ibind .le. 9 ) ) THEN
		    dgot = 6.0
		    IF ( ibind .le. 7 ) THEN
			tpot = 1.0
		      ELSE
			tpot = 4.0
		    END IF
		END IF

		rivals ( irntrb ) = 1
		rivals ( irdgot (1) ) = dgot
		rivals ( irtpot (1) ) = tpot
	    END IF
C
C*	    Decode the fc indicator for later use.
C
	    CALL ST_INTG  ( field (5:5), ifcind, ier )
	    IF  ( ( ifcind .ne. 0 ) .and. ( ifcind .ne. 8 ) .and.
     +		  ( ifcind .ne. 9 ) )  THEN
		ifcind = IMISSD
	    END IF
	ELSE
	    ifcind = IMISSD
	END IF
C
C*	Get the pressure altitude group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the pressure altitude data.
C*          Note:  Units for ipsal are decametres, so first convert to
C*          meters, then reconvert to feet for storage into rivals array.
C
	    CALL ST_INTG  ( field (1:3), ipsal, ier )
	    IF  ( ier .eq. 0 )  THEN
		IF ( ( iidind .eq. 1 ) .or. ( iidind .eq. 3 ) .or.
     +               ( iidind .eq. 5 ) .or. ( iidind .eq. 7 ) ) THEN
C
C*                 Altitude is at or above 10000 meters.
C
	           rivals ( irpsal ) = 10000. +
     +                                 ( FLOAT ( ipsal ) * 10.0 )
                ELSE
C
C*                 Altitude is less than 10000 meters.
C
	           rivals ( irpsal ) = FLOAT ( ipsal ) * 10.0
                END IF
		rivals ( irpsal ) = PR_HGMF ( rivals ( irpsal ) )
	    END IF
	END IF
C
C*	Get the wind group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the wind data.
C
	    CALL AF_WIND  ( field (1:2), field (3:5), ierwnd )
	END IF
C
C*	Get the temperature/dewpoint/weather group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the temperature data.
C
	    CALL ST_INTG  ( field (1:2), itmpc, ier )
	    IF  ( ier .eq. 0 )  THEN
		IF  ( itmpc .gt. 50 )  THEN
C
C*                  Temperature reported is negative but warmer than
C*                  -50 deg C.
C
		    tmpc = FLOAT ( 50 - itmpc )
		ELSE
		    IF ( ( iidind .eq. 2 ) .or. ( iidind .eq. 3 ) .or.
     +			 ( iidind .eq. 6 ) .or. ( iidind .eq. 7 ) ) THEN
C
C*                      Temperature reported is negative but is equal to
C*                      or colder than -50 deg C (i.e. an offset from -50
C*                      deg C was reported, not the actual temperature).
C
			tmpc = FLOAT ( 50 + itmpc ) * (-1.0)
		    ELSE
C
C*                      Temperature reported was equal to or warmer than
C*                      0 deg C.
C
		        tmpc = FLOAT ( itmpc )
		    END IF
		END IF
		rivals ( irtmpc ) = tmpc
	    END IF
C
C*	    Decode and store the dewpoint data.
C
	    CALL ST_INTG  ( field (3:4), idwpc, ier )
	    IF  ( ier .eq. 0 )  THEN
		IF  ( idwpc .gt. 50 )  THEN
C
C*                  Dew point reported is negative but warmer than
C*                  -50 deg C.
C
		    dwpc = FLOAT ( 50 - idwpc )
		ELSE
		    IF ( ( iidind .eq. 6 ) .or. ( iidind .eq. 7 ) ) THEN
C
C*                      Dew point reported is negative but is equal to or
C*                      colder than -50 deg C (i.e. an offset from -50
C*                      deg C was reported, not the actual dew point).
C
			dwpc = FLOAT ( 50 + idwpc ) * (-1.0)
		    ELSE
C
C*                      Dew point reported was equal to or warmer than
C*                      0 deg C.
C
		        dwpc = FLOAT ( idwpc )
                    END IF
		END IF
		rivals ( irdwpc ) = dwpc
	    END IF
C
C*	    Decode and store the present weather data.
C
	    CALL ST_INTG  ( field (5:5), iwxind, ier )
	    IF  ( ier .eq. 0 )  THEN
		IF  ( iwxind .eq. 4 )  THEN
		    civals ( icwcod (1) ) = 'FG'
		ELSE IF  ( iwxind .eq. 5 )  THEN
		    civals ( icwcod (1) ) = 'DZ'
		ELSE IF  ( iwxind .eq. 6 )  THEN
		    civals ( icwcod (1) ) = 'RA'
		ELSE IF  ( iwxind .eq. 7 )  THEN
		    civals ( icwcod (1) ) = 'SN'
		ELSE IF  ( iwxind .eq. 9 )  THEN
		    civals ( icwcod (1) ) = 'TS'
		END IF
		IF  ( civals ( icwcod (1) ) (1:1) .ne. ' ' )  THEN
		    rivals ( irnpwx ) = 1
		END IF
	    END IF
	END IF
C
C*	Get the /JHHH (i.e. pressure/height) group.
C
	CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	IF  ( ier .ne. 0 )  THEN
	    RETURN
	ELSE IF  ( lenf .eq. 5 )  THEN
C
C*	    Decode and store the pressure/height data.
C
	    CALL AF_RPHT  ( field (1:5), ierpht )
	END IF
C
	IF  ( irptr .lt. lenr )  THEN
C
C*	    Look for an 'RMK' indicator in the remainder of the report.
C
	    irmk = INDEX ( report ( irptr : lenr ), 'RMK' )
	    IF  ( irmk .ne. 0 )  THEN
		istart = irptr + irmk + 2
		IF  ( istart .lt. lenr )  THEN
C
C*		    Decode and store the RECCO identifier.
C
		    CALL DC_CRID  ( report ( istart : lenr ),
     +				    crid, lcrid, ierrid )
		    IF  ( ierrid .eq. 0 )  THEN
			civals ( icrpid ) = '        '
			civals ( icrpid ) = crid ( 1 : lcrid )
		    END IF
		END IF
	    END IF
	END IF
C
C*	If the report indicator was '92229', then there
C*	is not an additional section within this report.
C
	IF  ( irpid .eq. 2 )  THEN
	    RETURN
	END IF
C
C*	Decode and store any additional section data within this report.
C
C*	There is no standard format for the additional section
C*	(i.e. many types of data are possible, each of which can be
C*	reported or omitted in accordance with local requirements, and
C*	the types which are reported can appear in any order with
C*	respect to each other!); thus, to prevent mistakes, stop the
C*	decoding of additional section data altogether if a format
C*	error is detected during decoding of any one of the individual
C*	types.
C
	DO WHILE ( .true. )
	    ipt1 = irptr
	    CALL AF_GFLD  ( report, lenr, irptr, field, lenf, ier )
	    IF  ( ( ier .ne. 0 ) .or. ( lenf .ne. 5 ) ) THEN
		RETURN
	    END IF
	    IF  ( field (1:1) .eq. '1' )  THEN
C
C*		Decode the cloud data.
C
		irptr = ipt1
		CALL AF_RCLD  ( report, lenr, irptr, iercld )
		IF  ( iercld .ne. 0 )  THEN
		    RETURN
		END IF
	    ELSE IF  ( field (1:1) .eq. '4' )  THEN
C
C*		Decode and store the surface wind data.
C
		CALL AF_RSWD  ( field (2:5), ierswd )
	    ELSE IF  ( field (1:1) .eq. '6' )  THEN
	    ELSE IF  ( field (1:1) .eq. '7' )  THEN
C
C*		Decode and store the icing data.
C
		irptr = ipt1
		CALL AF_RICG  ( report, lenr, irptr, iericg )
		IF  ( iericg .ne. 0 )  THEN
		    RETURN
		END IF
	    ELSE IF  ( field (1:1) .eq. '8' )  THEN
	    ELSE IF  ( field (1:1) .eq. '9' )  THEN
C
C*		Decode and store the flight visibility data.
C
		CALL ST_INTG  ( field (2:2), ivi, ier )
		IF  ( ( ivi .ge. 1 ) .and. ( ivi .le. 3 ) )  THEN
		    rivals ( irvsby ) = FLOAT ( ivi )
		END IF
C
C*		Decode and store the sea-surface temperature data.
C
		CALL ST_INTG  ( field (3:5), isstc, ier )
		IF  ( ier .eq. 0 )  THEN
		    rivals ( irsstc ) = FLOAT ( isstc ) / 10.0
		END IF
	    ELSE
		RETURN
	    END IF
	END DO
C*
	RETURN
	END


      logical function gencuts_input(pjet,njets)
************************************************************************
*   Author: J.M. Campbell, 5th December 2001                           *
*                                                                      *
*   This routine imposes a generic set of cuts that can be applied     *
*   to all processes in process.DAT, using the parton momenta in pjet  *
*   which have already passed through the jet clustering algorithm     *
*                                                                      *
*   Only a basic set of variables is tested:                           *
*     pt(lepton) > leptpt, eta(lepton) < leptrap, missing Et > misspt  *
*                                                                      *
*   If leptpt2 or leptrap2 is not zero, then any leptons beyond the    *
*   leading-pt one must instead satisfy:                               *
*     pt(lepton) > leptpt2, eta(lepton) < leptrap2                     *
*                                                                      *
*   For processes where one would like to apply jet-like cuts, but     *
*   no clustering has been performed, additional cuts apply:           *
*     pt(jet) > jetpt, eta(jet) < jetrap, R(jet1,jet2) > Rcut          *
*                                                                      *
*   Finally, if further process-specific cuts are necessary,           *
*   an appropriate second routine may be called                        *
*                                                                      *
*   Return TRUE if this point FAILS the cuts                           *
*                                                                      *
************************************************************************
      implicit none
      include 'bbproc.f'
      include 'constants.f'
      include 'jetcuts.f'
      include 'leptcuts.f'
      include 'process.f'
      include 'plabel.f'
      include 'masses.f'
      include 'jetlabel.f'
      include 'notag.f'
      logical first,passedlept,is_lepton,is_photon,is_neutrino,
     & is_hadronic,failed
      integer njets,j,k,countb,bindex(mxpart),jindex,kindex,ib1,ib2
      integer countlept,leptindex(mxpart),countnu,
     & countjet,jetindex(mxpart),pntr,nuindex(mxpart)
      double precision pjet(mxpart,4),etvec(4),pZj(4),mZj
      double precision pt,etarap,etmiss,evtmisset,R,Rcut,etaj,etak,
     & etalept,mll,jetpt,jetrap,mnul,etabuffer
      logical hwwjetcuts
      double precision ht,qeta,mlbnu,merecon,reconcorr
      double precision dphi_ll,m_ll,mtrans,scut1,scut2
      double precision phill,phillcut,etajet2cut,mllcut,mnulcut
      double precision mZjcut,ptcheck,aetacheck
      integer ilep,igam,inu,countljet, countbjet,reconstr_top
      double precision pljet(mxpart,4),pbjet(mxpart,4)
      common/stopvars/ht,qeta,mlbnu,merecon,reconcorr
      common/hwwvars/dphi_ll,m_ll,mtrans,scut1,scut2
      common/rcut/Rcut
************************************************************************
*     Set-up the jet-like cut parameters here                          *
      parameter (jetpt=15d0,jetrap=2d0)
************************************************************************
      parameter(phillcut=1.2d0,etajet2cut=2.5d0,mllcut=15d0)
      parameter(mZjcut=40d0,mnulcut=30d0)
      data first/.true./
      save first,countlept,countnu,countb,leptindex,nuindex,bindex

      gencuts_input=.false.

      hwwjetcuts=.false.

      if (first) then
      first=.false.
c--- initialize counters and arrays that will be used to perform cuts
      countlept=0
      countnu=0
c--- lepton pt and rapidity cuts
      do j=3,mxpart
        if (is_lepton(j)) then
          countlept=countlept+1
          leptindex(countlept)=j
        endif
        if (is_neutrino(j)) then
           countnu=countnu+1
           nuindex(countnu)=j
        endif
      enddo
c--- Look for particles that should be treated as jets,
c--- so far only b decays from Z-bosons and
c--- hadronic decay of the W in diboson processes
c--- If these are present, we will do additional cuts
      countb=0
      do j=3,mxpart
         if ((plabel(j) .eq. 'qb') .or. (plabel(j) .eq. 'ab')
     .  .or. (plabel(j) .eq. 'qq') .or. (plabel(j) .eq. 'qa')) then
           countb=countb+1
           bindex(countb)=j
         endif
      enddo
c--- write-out the cuts we are using
      write(6,*)
      write(6,*)  '****************** Generic cuts ********************'
      write(6,*)  '*                                                  *'
      write(6,99) '*        pt(lepton)      >   ',leptpt,
     .                ' GeV            *'
      write(6,99) '*      |eta(lepton)|     <   ',leptrap,
     .                '                *'
      write(6,99) '*       pt(missing)      >   ',misspt,
     .                ' GeV            *'
      if ((leptpt2 .ne. 0d0) .or. (leptrap2 .ne. 0d0)) then
      write(6,99) '*     pt(2nd+ lepton)    >   ',leptpt2,
     .                ' GeV            *'
      write(6,99) '*   |eta(2nd+ lepton)|   <   ',leptrap2,
     .                '                *'
      endif
      if     (case.eq.'Wgamma') then
      if (mtrans34cut .lt. 0d0) then
      write(6,99) '*   (3,4,5) trans. mass  >   ',abs(mtrans34cut),
     .                ' GeV            *'
      else
      write(6,99) '* (e-gam,nu) trans. mass >   ',mtrans34cut,
     .                ' GeV            *'
      endif
      elseif (case.eq.'Zgamma') then
      write(6,99) '*    (3,4,5) inv. mass   >   ',mtrans34cut,
     .                ' GeV            *'
      elseif ( case .eq. 'Z_tjet') then
         write(6,99) '*      R(jet,lepton)     >   ',Rjlmin,
     .        '                *'
         write(6,99) '*    |mll-mz| <   ',mllcut,
     .                ' GeV            *'
         write(6,99) '*    |mZj-mt| <   ',mZjcut,
     .                ' GeV            *'

      else
      write(6,99) '*  (3,4) transverse mass >   ',mtrans34cut,
     .                ' GeV            *'
      endif
      write(6,99) '*      R(jet,lepton)     >   ',Rjlmin,
     .                '                *'
      write(6,99) '*     R(lepton,lepton)   >   ',Rllmin,
     .                '                *'
      write(6,99) '* |eta(jet1)-eta(jet2)|  >   ',delyjjmin,
     .                '                *'
      if (jetsopphem) then
      write(6,*) '*           eta(jet1) . eta(jet2)  <  0            *'
      endif
      if     (lbjscheme .eq. 1) then
      write(6,*) '*        eta(jet1)  <  eta(lept)  <  eta(jet2)     *'
      elseif (lbjscheme .ge. 2) then
      write(6,*) '*  eta(jet1)+Rcut  <  eta(lept)  <  eta(jet2)-Rcut *'
      lbjscheme=2
      endif
      if (countb .gt. 0) then
      write(6,*)  '*                                                  *'
      write(6,99) '*      pt(jet)       >   ',jetpt,
     .                ' GeV                *'
      write(6,99) '*    |eta(jet)|      <   ',jetrap,
     .                '                    *'
      write(6,99) '*   R(jet1,jet2)     >   ',Rcut,
     .                '                    *'
      endif
      if (hwwjetcuts) then
      write(6,99) '*   phi(lepton,lepton)   <   ',phillcut,
     .                '                *'
      write(6,99) '*  |eta(2nd jet)|        >   ',etajet2cut,
     .                '                *'
      write(6,99) '*  m(lepton,lepton)      <   ',mllcut,
     .                '                *'
      endif
      write(6,*)  '****************************************************'
      endif

C     Basic pt and rapidity cuts for lepton
      if     (countlept .eq. 1) then
          ptcheck=pt(leptindex(1),pjet)
          aetacheck=abs(etarap(leptindex(1),pjet))
          if ((ptcheck .lt. leptpt) .or. (aetacheck .gt. leptrap)) then
            gencuts_input=.true.
            return
          endif
          if ((aetacheck .gt. leptveto1min) .and.
     &        (aetacheck .lt. leptveto1max)) then
            gencuts_input=.true.
            return
          endif
      elseif (countlept .gt. 1) then
c--- loop over all the lepton possibilities for lepton 1 (j)
          j=0
  77      continue
          j=j+1
          passedlept=.true.
          ptcheck=pt(leptindex(j),pjet)
          aetacheck=abs(etarap(leptindex(j),pjet))
          if ((ptcheck .lt. leptpt) .or. (aetacheck .gt. leptrap)) then
            passedlept=.false.
            goto 78
          endif
          if ((aetacheck .gt. leptveto1min) .and.
     &        (aetacheck .lt. leptveto1max)) then
            passedlept=.false.
            goto 78
          endif
          do k=1,countlept
            if (k .ne. j) then
             ptcheck=pt(leptindex(k),pjet)
             aetacheck=abs(etarap(leptindex(k),pjet))
              if ((ptcheck.lt. leptpt2).or.(aetacheck.gt. leptrap2))then
                passedlept=.false.
              endif
              if ((aetacheck .gt. leptveto2min) .and.
     &            (aetacheck .lt. leptveto2max)) then
                passedlept=.false.
              endif
            endif
          enddo
  78      continue
c--- return to beginning if we failed and there are more leptons to try
          if ((passedlept .eqv. .false.).and.(j .lt. countlept)) goto 77
          gencuts_input=.not.(passedlept)
          if (gencuts_input) return
      endif

c--- missing energy cut
      evtmisset=etmiss(pjet,etvec)
      if ((evtmisset .lt. misspt) .and. (evtmisset .ne. 0d0)) then
        gencuts_input=.true.
        return
      endif

c--- mtrans34cut is used for three roles:
c---  1) Wgamma    --> mtrans34cut<0: transverse mass cut on (3,4,5) system
c---               --> mtrans34cut<0: transverse mass cut on (e-gam,nu) system
c---  2) Zgamma    --> invariant mass cut on (3,4,5) system
c---  3) otherwise --> transverse mass cut on (3,4) system
c---
      if (case.eq.'Wgamma') then
c--- cut on transverse mass of (3,4,5) system for Wgamma
        if (mtrans34cut .lt. 0d0) then
        mtrans=0d0
        do j=3,5
           mtrans=mtrans+dsqrt(pjet(j,1)**2+pjet(j,2)**2)
        enddo
        mtrans=mtrans**2
        do j=1,2
           mtrans=mtrans-(pjet(3,j)+pjet(4,j)+pjet(5,j))**2
        enddo
        mtrans=dsqrt(max(mtrans,0d0))
        if (mtrans .lt. abs(mtrans34cut)) then
           gencuts_input=.true.
           return
        endif
        else
c--- cut on (e-gam,nu) transverse mass for Wgamma
        if (is_neutrino(3)) then
          inu=3
          ilep=4
        else
          inu=4
          ilep=3
        endif
        igam=5
        mtrans=(pjet(ilep,4)+pjet(igam,4))**2
     &        -(pjet(ilep,1)+pjet(igam,1))**2
     &        -(pjet(ilep,2)+pjet(igam,2))**2
     &        -(pjet(ilep,3)+pjet(igam,3))**2
        mtrans=mtrans+(pjet(ilep,1)+pjet(igam,1))**2
     &               +(pjet(ilep,2)+pjet(igam,2))**2
        mtrans=dsqrt(max(mtrans,0d0))
     &        +dsqrt(pjet(inu,1)**2+pjet(inu,2)**2)
        mtrans=mtrans**2
        do j=1,2
           mtrans=mtrans-(pjet(3,j)+pjet(4,j)+pjet(5,j))**2
        enddo
        mtrans=dsqrt(max(mtrans,0d0))
        if (mtrans .lt. mtrans34cut) then
           gencuts_input=.true.
           return
        endif
        endif
c--- cut on invariant mass of (3,4,5) system for Zgamma
      elseif (case.eq.'Zgamma') then
        mtrans=(pjet(3,4)+pjet(4,4)+pjet(5,4))**2
        do j=1,3
          mtrans=mtrans-(pjet(3,j)+pjet(4,j)+pjet(5,j))**2
        enddo
        mtrans=dsqrt(max(mtrans,0d0))
        if(mtrans.lt.mtrans34cut) then
           gencuts_input=.true.
           return
        endif
      else
c--- cut on transverse mass of (3,4) pair otherwise
        mtrans=
     &   (pjet(3,1)*pjet(4,1)+pjet(3,2)*pjet(4,2))
     &   /dsqrt((pjet(3,1)**2+pjet(3,2)**2)
     &         *(pjet(4,1)**2+pjet(4,2)**2))
        mtrans=2d0*dsqrt(pjet(3,1)**2+pjet(3,2)**2)
     &   *dsqrt(pjet(4,1)**2+pjet(4,2)**2)*(1d0-mtrans)
        mtrans=dsqrt(max(mtrans,0d0))
        if (mtrans .lt. mtrans34cut) then
          gencuts_input=.true.
        return
        endif
      endif

c--- lepton-lepton separation (if there are 2 or more leptons)
      if ((countlept .gt. 1)) then
        do j=1,countlept
        do k=j+1,countlept
          if (R(pjet,leptindex(j),leptindex(k)) .lt. Rllmin) then
            gencuts_input=.true.
            return
          endif
        enddo
        enddo
c--- extra cut on phi(lept,lept) for H(->WW)+jet search
        if (hwwjetcuts) then
          phill=
     .       (pjet(leptindex(1),1)*pjet(leptindex(2),1)
     .       +pjet(leptindex(1),2)*pjet(leptindex(2),2))
     .       /dsqrt((pjet(leptindex(1),1)**2+pjet(leptindex(1),2)**2)
     .             *(pjet(leptindex(2),1)**2+pjet(leptindex(2),2)**2))
          if (phill .lt. -0.999999999D0) phill=-1d0
          phill=dacos(phill)
          if (phill .gt. phillcut) then
            gencuts_input=.true.
            return
          endif
        endif
c--- extra cut on m(lept,lept) for H(->WW)+jet search
        if (hwwjetcuts) then
          mll=dsqrt(2d0*(
     .       +pjet(leptindex(1),4)*pjet(leptindex(2),4)
     .       -pjet(leptindex(1),1)*pjet(leptindex(2),1)
     .       -pjet(leptindex(1),2)*pjet(leptindex(2),2)
     .       -pjet(leptindex(1),3)*pjet(leptindex(2),3)))
          if (mll .gt. mllcut) then
            gencuts_input=.true.
            return
          endif
        endif
      endif

c -- mll lepton cut for Ztjet production
      if (case .eq. 'Z_tjet' .or. case .eq. 'Z_tdkj') then
         mll=dsqrt(2d0*(
     .        +pjet(leptindex(1),4)*pjet(leptindex(2),4)
     .        -pjet(leptindex(1),1)*pjet(leptindex(2),1)
     .        -pjet(leptindex(1),2)*pjet(leptindex(2),2)
     .        -pjet(leptindex(1),3)*pjet(leptindex(2),3)))
c         if ( abs(mll-zmass) .ge. mllcut) then
c            gencuts_input=.true.
c            return
c         endif
      endif
      if (case .eq. 'Z_tdkj') then
         mnul=dsqrt(2d0*(
     .        +pjet(nuindex(1),4)*pjet(leptindex(3),4)
     .        -pjet(nuindex(1),1)*pjet(leptindex(3),1)
     .        -pjet(nuindex(1),2)*pjet(leptindex(3),2)
     .        -pjet(nuindex(1),3)*pjet(leptindex(3),3)))
c         if ( abs(mnul-wmass) .ge. mnulcut) then
c            gencuts_input=.true.
c            return
c         endif
      endif

c--- if there are no cuts on the jets - or no jets - we are done
      if ((Rjlmin .le. 0d0) .and. (delyjjmin .le. 0d0)) return
      if ((njets .eq. 0) .and. (countb .eq. 0)) return

c--- identify the jets
      countjet=0
      do j=3,mxpart
        if (is_hadronic(j)) then
          countjet=countjet+1
          jetindex(countjet)=j
        endif
      enddo

c--- countjet will pick up the extra 'pp' needed for the real piece,
c--- therefore we should subtract 1 from this number
      if (countjet .gt. njets) countjet=countjet-1

      if ((njets .ne. countjet) .and. (notag .eq. 0)) then
        write(6,*) 'Something is wrong in gencuts_input.f -'
        write(6,*) 'countjet = ',countjet,' BUT njets = ',njets
        stop
      endif

c -- identify b-jets
      call idjet(pjet,jetindex,countljet,countbjet,
     &     pljet,pbjet)

c--- extra cut on eta(2nd jet) for H(->WW)+jet search
      if ((hwwjetcuts) .and. (countjet .ge. 2)) then
        if (pt(jetindex(1),pjet) .gt. pt(jetindex(2),pjet)) then
          if (abs(etarap(jetindex(2),pjet)) .lt. etajet2cut) then
            gencuts_input=.true.
            return
          endif
        else
          if (abs(etarap(jetindex(1),pjet)) .lt. etajet2cut) then
            gencuts_input=.true.
            return
          endif
        endif
      endif
c--- jet-lepton separation (if there are 1 or more jets and leptons)
c -- applies to all jets
      if ((njets .gt. 0) .and. (countlept .gt. 0)) then
        do j=1,countlept
        do k=1,njets
           if (R(pjet,leptindex(j),jetindex(k)) .lt. Rjlmin) then
            gencuts_input=.true.
            return
          endif
        enddo
        enddo
      endif

c -- cut on Zj mass -- use light jet only
c -- only one light jet needs to be able to construct the top mass
      if (case .eq. 'Z_tjet' .or. case .eq. 'Z_tdkj') then
         reconstr_top=0
         do j=1,countljet
            pZj(:)=pljet(j,:)+pjet(leptindex(1),:)
     &           +pjet(leptindex(2),:)
            mZj=dsqrt(pZj(4)**2-pZj(1)**2-pZj(2)**2-pZj(3)**2)

            if ( abs(mZj-mt) .le. mZjcut) then
               reconstr_top=reconstr_top+1
            endif
         enddo
         if (reconstr_top .eq. 0) then
            gencuts_input=.true.
            return
         endif
      endif

c--- WBF-style cuts (if there are 2 or more jets)
      if ((njets .gt. 1)) then
c--- jet-jet rapidity separation
c--- j and k point to the two highest pt ('tagging') jets
        j=1
        k=2
        if (njets .eq. 3) then
          if     ( pt(jetindex(1),pjet) .lt.
     .      min(pt(jetindex(2),pjet),pt(jetindex(3),pjet)) ) then
            j=2
            k=3
          elseif ( pt(jetindex(2),pjet) .lt.
     .      min(pt(jetindex(1),pjet),pt(jetindex(3),pjet)) ) then
            j=1
            k=3
          endif
        endif
        if (abs(etarap(jetindex(j),pjet)-etarap(jetindex(k),pjet))
     .         .lt. delyjjmin) then
          gencuts_input=.true.
          return
        endif

c--- Requirement that jets be in opposite hemispheres
        if (jetsopphem) then
          if(etarap(jetindex(j),pjet)*etarap(jetindex(k),pjet) .gt. 0d0)
     .       then
            gencuts_input=.true.
            return
          endif
        endif

        if (lbjscheme .gt. 0) then
c--- Cut to require lepton to be between jets
          etabuffer=dble(lbjscheme-1)*Rcut
          etaj=etarap(jetindex(j),pjet)
          etak=etarap(jetindex(k),pjet)
          do pntr=1,countlept
            etalept=etarap(leptindex(pntr),pjet)
            if ( (etalept .lt. min(etaj,etak)+etabuffer) .or.
     .           (etalept .gt. max(etaj,etak)-etabuffer) ) then
              gencuts_input=.true.
              return
            endif
          enddo
        endif

      endif

c-- cuts on b-quarks
      if (bbproc) then
        call getbs(pjet,ib1,ib2)
        if ( (abs(etarap(ib1,pjet)) .gt. etabjetmax)
     .  .or. (pt(ib1,pjet) .lt. ptbjetmin) ) gencuts_input=.true.
        if ( (abs(etarap(ib2,pjet)) .gt. etabjetmax)
     .  .or. (pt(ib2,pjet) .lt. ptbjetmin) ) gencuts_input=.true.
      endif

c--- completed basic cuts
C--- if there are jet-like particles (see above), do more cuts
      if (countb .gt. 0) then
        do j=1,countb
          jindex=bindex(j)
          if (          (pt(jindex,pjet) .lt. jetpt) .or.
     .           (abs(etarap(jindex,pjet)) .gt. jetrap)) then
            gencuts_input=.true.
            return
          endif
          do k=j+1,countb
            kindex=bindex(k)
            if ((r(pjet,jindex,kindex) .lt. rcut)) then
              gencuts_input=.true.
              return
            endif
          enddo
        enddo
      endif

      return

   99 format(1x,a29,f6.2,a17)

      end