      SUBROUTINE QUWT(WVIS,PTMAX,WEIGHT)
 
      INCLUDE '/aleph/phy/qcde.inc'
      REAL QUTEF
      LOGICAL DEBUG
      EXTERNAL QUTEF
      INCLUDE '/aleph/phy/qhac.inc'
      COMMON/SELDAT/IREAD,IKEPT,INOVX,INOVX2,IBASIC,ZVTX,RVTX
       COMMON/QUTRIG/SCE2,SNEL,TKC2,SNMU,SNEC,SCALED,PLU1,
     1MASK(32),L3ON,WRITE_FLAG
       LOGICAL WRITE_FLAG
      PARAMETER(ZCUT=7.0)
      CHARACTER*80 TEXT
 
 
      LOGICAL FIRST/.TRUE./,USEMC/.TRUE./
 
      INCLUDE '/aleph/phy/qmacro.inc'
 
C
C CALCULATE EVENTS WEIGHT HERE
C
 
      IF(FIRST)THEN
            BGWT = 1.0                  
            KBGWT = IW(NAMIND('BGWT',0))
            IF(KBGWT.NE.0)THEN
                  BGWT = RW(KBGWT+1)
            ENDIF
            KMCWT = IW(NAMIND('MCWT',0))
            IF(KMCWT.NE.0)THEN
                  USEMC = .FALSE.
            ENDIF
 
            WRITE(6,*)' EVENT WEIGHTING SUMMARY :'
            WRITE(6,*)' '
            IF(USEMC)THEN
             WRITE(6,*)' MC WEIGHTS FROM GENERATOR. '
             WRITE(6,*)' '
            ENDIF
            WRITE(6,*)' REAL EVENTS WEIGHTED BY LEVEL3 REJECT '
            WRITE(6,*)'  AND TRIGGER EFFICIENCY '
            WRITE(6,*)' '
            WRITE(TEXT,*)' BEAM GAS WEIGHT SET TO ',BGWT
            CALL  QUWR(TEXT)
            WRITE(TEXT,*)' FOR EVENTS WITH |ZVTX| > ',ZCUT
            CALL  QUWR(TEXT)
 
            FIRST = .FALSE.
      ENDIF
 
       WEIGHT = 1.0
       WRITE_FLAG = .FALSE.
C
C
       IF(XMCEV)THEN
C
C MC EVENTS:  USE MC WEIGHTS
C
          IF(USEMC)WEIGHT=QKEVWT
       ELSE
C
C REAL DATA: FIRST WEIGHT DUE TO LEVEL 2 VETO
C
          CALL QUL2WT(AL2WT,.FALSE.)
C
C REAL DATA: SECOND WEIGHT DUE TO LEVEL 3 VETO
C
          CALL QUL3WT(AL3WT,.FALSE.)
          WEIGHT = AL3WT*AL2WT

c
c     
         IF(WEIGHT.GT.10)THEN
C              WRITE(6,*)' TOTAL WEIGHT > 10.0 IN ',KRUN,KEVT
C              WRITE(6,*)' LEVEL 2 WEIGHT ',AL2WT
              CALL QUL2WT(AL2WT,.TRUE.)
C              WRITE(6,*)' LEVEL 3 WEIGHT ',AL3WT
              CALL QUL3WT(AL3WT,.TRUE.)
              WRITE_FLAG = .TRUE.
         ENDIF
          IF(WEIGHT.EQ.0)RETURN
C
C           THEN APPLY TRIGGER EFFICIENCY CORRECTION FOR SNG_C_E2
C
          TEMP = QUTEF(WVIS,PTMAX)
          WEIGHT = WEIGHT/TEMP
       ENDIF
C ALL EVENTS:
C THE APPLY BEAM GAS CORRECTION
C
       IF(ABS(ZVTX).GT.ZCUT) WEIGHT = -1.0*BGWT
 
       RETURN
       END
       SUBROUTINE QUL3WT (AL3WT,DEBUG)
C
C
C RETURN CORRECT WEIGHTING TO ALLOW FOR LEVEL 3 VETO
C
       INCLUDE '/aleph/phy/qcde.inc'
       INCLUDE '/aleph/phy/qhac.inc'

       LOGICAL DEBUG,level3
       COMMON/QUTRIG/SCE2,SNEL,TKC2,SNMU,SNEC,SCALED,PLU1,
     1MASK(32),L3ON
       
 
       INTEGER     CRUN
       PARAMETER   (NBITS=5)
       INTEGER      SNGNEL,SNGCE2,TRKC2
       PARAMETER   (SNGNEL=1)
       PARAMETER   (SNGCE2=2)
       PARAMETER   (TRKCN2=3)
       PARAMETER   (SNGNEC=4)
       PARAMETER   (DBLCE2=5)
       INTEGER     BIT(NBITS),L3BIT(NBITS),TBIT(NBITS)
       INTEGER     AGETDB
       PARAMETER   (NSET=5)
       INTEGER      KTRIG,ICALL(NSET)
       PARAMETER   (NL3OFF=33)
       INTEGER     L3OFRN(2*NL3OFF)
       CHARACTER*4 CHAINT
       CHARACTER*8 NAME(NBITS),NAM32(32),TNAME
       REAL        SCFAC(NBITS)
       REAL        FACTOR(NBITS,NSET)
       LOGICAL     FLAG,ENABLE(32),L3ON
       LOGICAL     FIRST,SCE2,SNEL,TKC2,DCE2,SCALED,SNEC,SNMU,PLU1

       EXTERNAL    CHAINT,AGETDB
 
       INCLUDE '/aleph/phy/qmacro.inc'
 
       DATA ICOUNT/0/
       DATA FIRST/.TRUE./
       DATA CRUN/0/
       DATA BIT/NBITS*-1/
       DATA SCFAC/NBITS*1.0/
       DATA NAME(SNGNEL)/'SNG_N_EL'/
       DATA NAME(SNGCE2)/'SNG_C_E2'/
       DATA NAME(TRKCN2)/'TRK_CNT2'/
       DATA NAME(SNGNEC)/'SNG_N_EC'/
       DATA NAME(DBLCE2)/'DBL_C_E2'/
       DATA ENABLE/32*.FALSE./
       DATA ICALL/NSET*0/

C       IF(     (KRUN.GT.7496 .AND.KRUN.LT.10174)
C     1     .OR.(KRUN.GT.10756.AND.KRUN.LT.20000)
C     1     .OR.(KRUN.GT.20594.AND.KRUN.LT.21034)
C     1     .OR.(KRUN.GT.21168.AND.KRUN.LT.23548)
C     1     .OR.(KRUN.GT.25000))

       DATA L3OFRN/    1, 7495,10175,10755,20001,20593,21035,21167
     1,23549,24999,25139,26337,26350,26350,26564,26564,26578,26650
     1,26929,26942,27216,27216,27602,27602,27667,27669,27734,27735 
     1,27748,27748,28242,28300,28347,28347,28360,28360,28376,28376 
     1,28585,28595,28627,28627,28745,28766,28901,28902,29015,29015 
     1,29083,29093,29100,29100,29119,29119,29460,29460,29785,29946 
     1,29972,30062,30240,30256,30364,30365,30373,30381/
C
C REJECT FACTORS IN LEVEL 3 LOGIC
C IN THE ORDER
C (SNGNEL=1),(SNGCE2=2),(TRKCN2=3),(SNGNEC=4),(DBLCE2=5)
C ONE SET FOR EACH PERIOD OF LEVEL 3 ACTIVITY
C
       DATA FACTOR/0.05, 0.1, 1.0, 1.0, 1.0,
     1             0.05, 0.1, 0.1, 1.0, 1.0,
     1             0.03, 0.1, 0.1, 0.02,1.0,
     1             0.05, 0.2, 0.1, 1.0, 1.0,
     1             0.05, 0.2, 0.1, 1.0, 0.05/
C
c      DATA TRNREJ(SNG_N_EL_NR) /.05/
c      DATA TRNREJ(SNG_C_E2_NR) /.2/
c      DATA TRNREJ(TRK_CNT2_NR)/.1/
c      DATA TRNREJ(DBL_C_E2_NR) /.05/
C
C LEVEL 3 HISTORY:
C  7/4/90 
C         LEVEL 3 REJECTION ENABLED ON SNG_CH_E2 ( 10%)
C                                  AND SNG_N_EL  ( 5%)
C  RUN 11337                       
C                                 TRK_CNT2 ADDED ( 10%)
C  RUN 20000 ( START '93)
C                            SNG_N_EL SPLIT INTO
C                            SNG_N_EL ( BARREL ) (3%)
C                   AND      SNG_N_EC (END CAP ) (2%)
C  RUN 20839 SNG_CH_E2 PUT ONTO BIT 2, AND DOWNSCALED BY FACTOR 2 (AT LEVEL 1)
C                            SNG_CH_E2 FACTOR CHANGED TO 20%
C                            SNG_N_EL AND SNG_N_EC 
C                              RECOMBINED        (5%)
C EARLY 1994 SNG_CH_E2 LEVEL 1 DOWNSCALE IS 10
C 7/7/94 A.finch talks at echenevex meeting, dble_ch_e2 is
C         approved
C         run  27057 is next run after this meeting
C 23/9/94 DBL_CH_E2 INTRODUCED WITH 5% FACTOR IN LEVEL 3 (run no. 29106)
C
C 1994  Run  25000 to Run  30999 
C 1995  Run  31000 to Run  37853           
C 1996  NO CHANGE FROM 1995
c	Here are the retain numbers:
c
c      DATA TRNREJ(SNG_N_EL_NR) /.05/
c      DATA TRNREJ(SNG_C_E2_NR) /.2/
c      DATA TRNREJ(DBL_C_E2_NR) /.05/
c      DATA TRNREJ(TRK_CNT2_NR)/.1/
c 
c	Chgeers, Gunther.
C
C FIRST TIME - LOOK FOR CONTROL CARDS
C
      IF(FIRST)THEN
           FIRST = .FALSE.
           CALL QUXNAM
           INL3W = IW(NAMIND('NL3W',0))
           IF(INL3W.NE.0)THEN
            CALL QUWR(' ')
            CALL QUWR(' ')
            CALL QUWR(
     1' *** WARNING - LEVEL 3 WEIGHTING SUPPRESSED *** ')
            CALL QUWR(
     1' ***      BY NL3W CARD                      *** ')
            CALL QUWR(' ')
            CALL QUWR(' ')
           ENDIF
           INL2W = IW(NAMIND('NL2W',0))
           IF(INL2W.NE.0)THEN
            CALL QUWR(' ')
            CALL QUWR(' ')
            CALL QUWR(
     1' *** WARNING - LEVEL 2 WEIGHTING SUPPRESSED *** ')
            CALL QUWR(
     1' ***      BY NL2W CARD                      *** ')
            CALL QUWR(' ')
            CALL QUWR(' ')
           ENDIF
C           WRITE(6,*)' ACCEPTANCE FACTORS FOR BITS...'
C           WRITE(6,600)(NAME(I),I=1,NBITS)
600        FORMAT(4(A8,' '))
C           DO J = 1,NSET
C                WRITE(6,*)(FACTOR(I,J),I=1,NBITS)
C           ENDDO
       ENDIF
       AL3WT = 1.0
       IF(XMCEV)RETURN


C
C CHECK TRIGGER CONFIGURATION EVERY NEW RUN
C
      IF(CRUN.NE.KRUN)THEN
       CRUN = KRUN
C
C
C CHOOSE WHICH SET TO USE
C
        IF     (KRUN.LT.11337)THEN
                        ISET=1
        ELSEIF(KRUN.LT.20000)THEN
                        ISET=2
        ELSEIF(KRUN.LT.20839)THEN
                        ISET=3
        ELSEIF(KRUN.LT.29106)THEN
                        ISET=4
        ELSE
                        ISET=5
        ENDIF
C
C            try and find XTBN ( from the data stream, otherwise look on
C      database
C
       JXTBN = IW(NAMIND('XTBN'))                                        
       IF(JXTBN.EQ.0)THEN
        ISTAT = AGETDB('XTBN',KRUN)                                       
        IF(ISTAT.EQ.0)THEN                                                
          CALL ALTELL('+++QUL3WT+++ Bank XTBN not found',0,'RETURN')     
          GOTO 999                                                       
        ENDIF                                                             
        JXTBN = IW(NAMIND('XTBN'))                                        
       ENDIF
C
C SET THE TEMPORARY ARRAY POINTERS TO `UNSET' (-1)
C
       DO I =1,NBITS
           TBIT(I) = -1
       ENDDO
       IF(JXTBN.NE.0)THEN                        
C
C LOOK FOR THE BITS NUMBERS AND MASK SETTING FOR BITS CORRESPONDING TO
C  MNEMONICS...

        IF(KRUN.LT.20000.OR.KRUN.GE.20839)BIT(SNGNEC)=-1

        DO 50 K=1,LROWS(JXTBN)                                          
C
C NOTE I SWITCH HERE FROM COUNTING FROM 0 TO COUNTING FROM 1
C  BY ADDING 1
C
         IBIT  = ITABL(JXTBN,K,JXTBTB) + 1
         IFLAG = ITABL(JXTBN,K,JXTBTN)
         FLAG  = .TRUE.
         IF(IFLAG.EQ.0)FLAG = .FALSE.
C
C NOTIFY DISABLING OF BITS, BEFORE NAME IS OVERWRITTEN
C
C         IF(FLAG.NE.ENABLE(IBIT))THEN
C           WRITE(6,*)' FLAG FOR BIT ',IBIT,' ',NAM32(IBIT),
C     1     ' CHANGED FROM ',ENABLE(IBIT),' TO ',FLAG
C         ENDIF
         ENABLE(IBIT) = FLAG

         TNAME = CHAINT(ITABL(JXTBN,K,JXTBBM)) //
     1               CHAINT(ITABL(JXTBN,K,JXTBBM+1))
C         IF(TNAME.NE.NAM32(IBIT))THEN
C          WRITE(6,*)' NAME OF BIT ',IBIT,' CHANGED FROM ',NAM32(IBIT),
C     1    ' TO ',TNAME
C         ENDIF
         NAM32(IBIT) = CHAINT(ITABL(JXTBN,K,JXTBBM)) //
     1               CHAINT(ITABL(JXTBN,K,JXTBBM+1))
C
C FOR THE RELEVANT BITS FOR LEVEL 3 VETO SAVE BIT NUMBER IN TEMPORARY ARRAY
C
         DO 49 I = 1,NBITS
           IF(NAM32(IBIT).EQ.NAME(I))THEN
              TBIT(I) = IBIT
           ENDIF
   49   CONTINUE
C
C SPECIAL FOR SINGLE MUON
C
        IF(NAM32(IBIT).EQ.'SNG_MUON')ISNGMU=IBIT

   50   CONTINUE                                                        
C
C END OF LOOP OVER 32 BITS 
C
       ENDIF
C
C LOOP OVER LEVEL 3 BITS TO SEE IF THEY CHANGED POSTION
C                                                             
       DO I = 1,NBITS
              IPOINT = TBIT(I)
              IF(BIT(I).NE.IPOINT)THEN
C                 IF(IPOINT.GT.-1)THEN
C                  WRITE(6,*)'  RUN: ',KRUN
C                  WRITE(6,*)' TRIGGER ',NAME(I),' IS NOW ON BIT ',
C     1            IPOINT
C                 ELSE
C                  WRITE(6,*)'  RUN: ',KRUN
C                  WRITE(6,*)' TRIGGER ',NAME(I),' IS ON NO BIT NOW '
C                 ENDIF
              ENDIF
              BIT(I) = IPOINT
       ENDDO
      ENDIF                                                             

C
C END OF ONCE PER RUN SECTION
C
C
C ONCE PER EVENT STARTS...
C 
C
C ANALYSE SET TRIGGER BITS
C
      KTRIG=KXTEL2
601   FORMAT(5(1X,Z8))
C
C NOTE THE INVERSION  ! MASK COUNTS FROM LEFT
C BITS ARE COUNTED FROM RIGHT IN WORDS
C
       DO I = 1,NBITS
        L3BIT(I) = 0
       ENDDO
       IMSUM = 0
C
C
       DO 90 IBIT  = 1,32
             MASK(IBIT)=JBIT(KTRIG,IBIT)
C
C REMEMBER IF A LEVEL 3 BIT GOT SET
C
             IF(MASK(IBIT).EQ.1)THEN
               DO I = 1,NBITS
                IF(BIT(I).NE.-1.AND.IBIT.EQ.BIT(I))L3BIT(I)=1
               ENDDO
             ENDIF
             IMSUM = IMSUM + MASK(IBIT)
90     CONTINUE

C
C SET THE FLAGS FOR INTERESTING BITS (NOT USED HERE FOR CALCULATIONS)
C
        SNEL = .FALSE.
        SCE2 = .FALSE.
        TKC2 = .FALSE.
        SNEC = .FALSE.
        DCE2 = .FALSE.
 

        IF(MASK(BIT(SNGNEL)).GT.0)SNEL = .TRUE.
        IF(MASK(BIT(SNGCE2)).GT.0)SCE2 = .TRUE.
        IF(MASK(BIT(TRKCN2)).GT.0)TKC2 = .TRUE.
C
C THERE ARE RUN PERIODS WHERE THESE TRIGGERS DON'T EXIST 
C HENCE TWO TESTS
C
        IF(BIT(SNGNEC).GT.0.AND.
     1    MASK(BIT(SNGNEC)).GT.0)SNEC = .TRUE.
        IF(BIT(DBLCE2).GT.0.AND.
     1    MASK(BIT(DBLCE2)).GT.0)DCE2 = .TRUE.
C.................................................................
C SINGLE MUON IS A SPECIAL CASE - JUST FOR TRIGGER
C EFFICIENCY CALCULATIONS IN OTHER ROUTINES 
C
        SNMU = .FALSE.
        IF(MASK(ISNGMU).GT.0)SNMU = .TRUE.
C................................................................
C
C IF THERE ARE NO TRIGGERS IN A REAL EVENT WE MUST REJECT THE EVENT !
C
      IF(.NOT.XMCEV.AND.IMSUM.EQ.0)THEN
           AL3WT = 0.0
           GOTO 999
      ENDIF
C
C IF USER HAS REQUESTED THAT NO LEVEL 3 CORRECTION SHOULD BE PERFORMED
C - JUMP OUT HERE, HAVING FILLED COMMON VARIABLES
C
       IF(INL3W.NE.0)GOTO 2000
C
C CHECK FOR PERIODS WHEN LEVEL 3 WAS DISABLED
C THIS INORMATION IS CURRENTLY NOT STORED IN THE RUN HEADER !
C
C        L3ON = .FALSE.
C       IF(     (KRUN.GT.7496 .AND.KRUN.LT.10174)
C    1     .OR.(KRUN.GT.10756.AND.KRUN.LT.20000)
C    1     .OR.(KRUN.GT.20594.AND.KRUN.LT.21034)
C    1     .OR.(KRUN.GT.21168.AND.KRUN.LT.23548)
C    1     .OR.(KRUN.GT.25000))
C    1      L3ON = .TRUE.
C
C LOOP OVER ALL THE PERIODS WHEN LEVEL3 WAS OFF, IF WE ARE IN 
C  ON THEN RETURN 
C
      L3ON = .TRUE.
      DO I = 1,NL3OFF,2
        IF(KRUN.GE.L3OFRN(I).AND.KRUN.LE.L3OFRN(I+1))THEN
           L3ON = .FALSE.
           RETURN
        ENDIF
      ENDDO

C
C LOOK AT LEVEL 3 BIT, IF LEVEL3 IS FALSE EVENT SHOULD
C HAVE BEEN REJECTED BY LEVEL 3 BUT WAS KEPT ANY WAY
C AS IT WAS SAVED BY ONE OF THE QUOTAS
C
C
      CALL QL3REJ(LEVEL3)
c  
      IF(.NOT.LEVEL3)THEN
C
C FIRST CHECK IF ANY BIT OTHER THAN THOSE THAT THAT ARE VETOED IS
C PRESENT
C
        DO  I = 1,32
          IF(MASK(I).EQ.1)THEN
           IF(.NOT.ENABLE(I))THEN
C             WRITE(6,*)' NON ENABLED BIT SET ',I,
C     1       NAM32(I),' IN EVENT ',KRUN,' ',KEVT
             GOTO 10
           ELSE
C
C IF THIS IS A BIT WITH A LEVEL 3 FACTOR < 1.0 - THEN CARRY ON
C OTHERWISE EXIT
C
            DO J = 1,NBITS
             IF(I.EQ.BIT(J).AND.FACTOR(J,ISET).LT.1.0)GOTO 10
            ENDDO
C
C IF WE GET HERE THIS IS A NON LEVEL 3 BIT SET - SO EVENT HAS NO L3 WEIGHT 
C  CORRECTION WE CAN SKIP IT
C
            GOTO 2000
           ENDIF
          ENDIF
10        CONTINUE
        ENDDO      
C
C THIS IS A VALID LEVEL 3 REJECT EVENT - 
C  LOOK FOR THE DIFFERENT COMBINATIONS OF POSSIBLE TRIGGER BITS
C
C
C CALCULATE THE ACCEPTED FRACTION WITH THIS 
C COMBINATION OF TRIGGER BITS
C
        REJECT=1.0
        ACCEPT=0.0
C        
C
C
        DO I = 1,NBITS
                IF(L3BIT(I).EQ.1)THEN
                        ACCEPT=ACCEPT+FACTOR(I,ISET)*REJECT
                        REJECT=REJECT*(1-FACTOR(I,ISET))
                ENDIF
        ENDDO
C
        ICALL(ISET) = ICALL(ISET) + 1
C        IF(DEBUG.OR.ICALL(ISET).LT.20)THEN
C                WRITE(6,*)'SNEL,SCE2,TKC2,SNEC,DCE2',
C     1                     SNEL,SCE2,TKC2,SNEC,DCE2
C                WRITE(6,*)' ISET = ',ISET,
C     1                    ' ACCEPTED FRACTION ',ACCEPT
C                WRITE(6,*)L3BIT
C        ENDIF
        IF(ACCEPT.GT.0)THEN
         IF(ACCEPT.LE.1.0)THEN
          AL3WT = 1/ACCEPT
C          WRITE(6,*)' IN RUN ',KRUN,KEVT,' LEVEL 3 FACTOR = ',
C     1    AL3WT
         ELSE
          AL3WT = 1.0
         ENDIF
        ELSE
C         WRITE(6,*)' ERROR - ACCEPT = 0.0 !',KRUN,' ',KEVT
C                WRITE(6,*)'SNEL,SCE2,TKC2,SNEC,DCE2',
C     1                     SNEL,SCE2,TKC2,SNEC,DCE2
         AL3WT = 1.0
        ENDIF
      ENDIF
C
2000  CONTINUE
999   RETURN
      END
       SUBROUTINE QUL2WT (AL2WT,DEBUG)
C
C
C RETURN CORRECT WEIGHTING TO ALLOW FOR LEVEL 2 VETO
C
       INCLUDE '/aleph/phy/qcde.inc'
       INCLUDE '/aleph/phy/qhac.inc'
       LOGICAL PRINTIT,DEBUG
       PARAMETER   (NBITS=8,NSET=3)
       REAL        DWNSC(NBITS,NSET),FACTOR(NBITS),OLDFAC(NBITS)
C 
       INCLUDE '/aleph/phy/qmacro.inc'
C
C LEVEL 2 REJECT FACTORS FOR BITS 1 TO 8
C
C 1994 SET...
C
       DATA (DWNSC(I,1),I=1,NBITS)/65000., 2.,3.,2500.,4., 2.,10., 10./

c    0 65000 RNDM_TST  Random Test Trigger (PLU-Level)                    
c *  1     2 SNG_C_E2  Single Charged ElMag AND ITC_RPZ(No TPC)  EWT2  0.18/0.22(Ec) 0.14(Ba)         4(Ec)  4(Ba)
c *  2     3 SNG_N_EL  Single Neutral ElMag Energy Barrel        EWT3  2.18/2.41(Ec) 0.95(Ba)        55(Ec) 29(Ba)
c *  3  2500 SiCAL_LO  SiCAL  A_low or B_low - Thrs=Low    Sarm        9.3
c *  4     4 SiCAL_ME  SiCAl  A_med or B_med - Thrs=Medium Coinc       24.2(A)*12.1(B) + vice versa
c *  5     2 SiCAL_HI  SiCAL  A_hig or B_hig - Thrs=High   Sarm        36.3
c *  6    10 LW_ET_HI  LCW Total Energy (High Threshold)         ETT2  3.3(A)* 6.2(B) * 28.7(Sum_AB)  8* 8 *42
c *  7    10 LW_A+BVH  LCW_A_hig or LCW_B_hig AND AntiSpark      ETT3 23.7(A)+25.8(B) (*6.83 in 1/4) 37+37 * 8

       DATA (DWNSC(I,2),I=1,NBITS)/65000.,10.,1.,2500.,1., 20.,10., 
     1                                                          100./

c    0 65000 RNDM_TST  Random Test Trigger (PLU-Level)                    
c *  1    10 SNG_C_E2  Single Charged ElMag AND ITC_RPZ(No TPC)  EWT2  0.18/0.22(Ec) 0.14(Ba)         4(Ec)  4(Ba)
c *  2     1 SNG_N_EL  Single Neutral ElMag Energy Barrel        EWT3  2.18/2.41(Ec) 0.95(Ba)        55(Ec) 29(Ba)
c *  3  2500 SiCAL_LO  SiCAL  A_low or B_low - Thrs=Low    Sarm        9.3
c *  4     1 SiCAL_ME  SiCAl  A_med or B_med - Thrs=Medium Coinc       24.2(A)*12.1(B) + vice versa
c *  5    20 SiCAL_HI  SiCAL  A_hig or B_hig - Thrs=High   Sarm        36.3
c *  6    10 LW_ET_HI  LCW Total Energy (High Threshold)         ETT2  3.3(A)* 6.2(B) * 28.7(Sum_AB)  8* 8 *42
c *  7   100 LW_A+BVH  LCW_A_hig or LCW_B_hig AND AntiSpark      ETT3 23.7(A)+25.8(B) (*6.83 in 1/4) 37+37 * 8

       DATA (DWNSC(I,3),I=1,NBITS)/65000.,20.,1.,1.,5., 1.,20., 1./

c
c    0 65000 RNDM_TST  Debug Random Test Trigger                    
c *  1    10 SNG_C_E2  Single Charged ElMag (No TPC)             EWT2   0.18/0.21(Ec) 0.15(Ba)        5(Ec)  5(Ba)
c *  2     1 SNG_N_EL  Single Neutral ElMag Energy               EWT3   2.09/2.30(Ec) 0.95(Ba)       66(Ec) 35(Ba)
c    3     1 --------  ---------------------------------------       
c *  4     5 SiCAL_ME  SiCAl  High*Low  Coinc --Thrs=H,L               45.0(A)*30.0(B) + vice versa
c *  5     1 LW_LO_HI  LCW_A_LOW*B_hig + A_hig*B_low             ETT1/ETT2  30.0*40.0 GeV              53*69+51*71   
c *  6    20 LW_A+BLO  LCW_A_low or LCW_B_low                    ETT1       30.0 GeV                      53+51
c *  7     1 LW_A+BVH  LCW_A_hig or LCW_B_hig                    ETT3       50.0 GeV                      88+87
C
C ***************************************************************************************************************
C Date: 25-JUL-1996         RUN_161         Mask: E8BE03F6
C ---------------------------------------------------------------------------------------------------------------
C  Bit DwnSc Mnemonic  Description                             Signal     Effect. ThrsHold[GeV]      DACsetting
C ---------------------------------------------------------------------------------------------------------------
C    0 65000 RNDM_TST  Debug Random Test Trigger                    
C *  1    20 SNG_C_E2  Single Charged ElMag AND ITC_RPZ (No TPC) EWT2   0.18/0.21(Ec) 0.15(Ba)        5(Ec)  5(Ba)
C *  2     1 SNG_N_EL  Single Neutral ElMag Energy               EWT3   2.09/2.30(Ec) 0.95(Ba)       66(Ec) 35(Ba)
C    3     1 --------  ---------------------------------------       
C *  4     5 SiCAL_ME  SiCAl  High*Low  Coinc --Thrs=H,L               45.0(A)*30.0(B) + vice versa
C *  5     1 LW_LO_HI  LCW_A_LOW*B_hig + A_hig*B_low             ETT1/ETT2  32.0*48.0 GeV              54*81+80*53   
C*  6    20 LW_A+BLO  LCW_A_low or LCW_B_low                    ETT3       40.0 GeV                      67+67
C *  7     1 LW_A+BVH  LCW_A_hig or LCW_B_hig                    ETT2       48.0 GeV                      81+80 



c      IF(FIRST)THEN
c           FIRST = .FALSE.
c      ENDIF
      IF(KRUN.NE.CRUN)THEN
        CRUN = KRUN
        ISET = 0
        IF(KRUN.GE.25000.AND.KRUN.LE.30999)ISET=1
        IF(KRUN.GE.31000.AND.KRUN.LE.40599)ISET=2
        IF(KRUN.GE.40600.AND.KRUN.LE.99998)ISET=3
        IF(ISET.EQ.0)RETURN
        DO I = 1,NBITS
         FACTOR(I) = 1/DWNSC(I,ISET)
        ENDDO

        IF(ISET.EQ.1)THEN
C
C 1994 CHANGES...
C
C recalculate scale factors based on following data
C                       Bit                       Bit               Bit
c *** Run 25439 SiCAL_LO 4 2500->5000   SiCAL_HI 6  2->20
c *** Run 25456 LW_A+BVH 8   10-> 100   SNG_C_E2 2  2->10
c *** Run 25796 SNG_N_EL 3    1->   3   SiCAL_ME 5  1-> 4
c *** Run 25838 SiCAL_ME 5    4->   6   SiCAL_HI 6 20->30
c *** Run 25932 SiCAL_HI 6   30->  80
c *** Run 26473 SiCAL_LO 4 5000->2500   SiCAL_ME 5  6-> 1    SiCAL_HI 6 80->20
c *** Run 26497 SiCAL_HI 6   20->   4
c *** Run 26640 SNG_N_EL 3    3->   2                                      also add EndCaps at higher Threshold)
c *** Run 26754 Sical_Hi 6    4->  20
c *** Run 26774 Sng_C_E2 2   10->   5
c *** Run 27223 SNG_C_E2 2    5->  10  SNG_N_EL  3 2-> 1
        IF(KRUN.GE.25439)FACTOR(4)= 1./ 5000.
        IF(KRUN.GE.25439)FACTOR(6)= 1./   20.
        IF(KRUN.GE.25456)FACTOR(8)= 1./  100.
        IF(KRUN.GE.25456)FACTOR(2)= 1./   10.
        IF(KRUN.GE.25796)FACTOR(3)= 1./    3.
        IF(KRUN.GE.25796)FACTOR(5)= 1./    4.
        IF(KRUN.GE.25838)FACTOR(5)= 1./    6.
        IF(KRUN.GE.25838)FACTOR(6)= 1./   30.
        IF(KRUN.GE.25932)FACTOR(6)= 1./   80.
        IF(KRUN.GE.26473)FACTOR(4)= 1./ 2500.
        IF(KRUN.GE.26473)FACTOR(6)= 1./    1.
        IF(KRUN.GE.26473)FACTOR(6)= 1./   20.
        IF(KRUN.GE.26497)FACTOR(6)= 1./    4.
        IF(KRUN.GE.26640)FACTOR(3)= 1./    2.
        IF(KRUN.GE.26754)FACTOR(6)= 1./   20.
        IF(KRUN.GE.26774)FACTOR(2)= 1./    5.
        IF(KRUN.GE.27223)FACTOR(2)= 1./   10.
        IF(KRUN.GE.27223)FACTOR(3)= 1./    1.

       ELSEIF(ISET.EQ.2)THEN
c ***********************************************************************************
c  1995 changes
c ***************************************************************************************************************
c Run_35305     ITC_RPZ-bit set to static_1.
c Run_35387     ECW_TEST Trigger (Bit 11) added.
c Run_35823     Ecal thresholds adjusted. Back to values of 1994.
c Run_36072     Lcal thresholds adjusted. Back to values of 1994.
c Run_36092  *  Set downscale factor for LW_ET_HI back to the old value of 10. It was 1 since last November. 
c Run_36140     Treshold ETT1 Barrel adjusted. DAC 112-->105.
c Run_32226     Bit_13 SiCAL_MX enabled.
c Run_36491     Bit_23 TPC_LASR enabled. 
c Run_36769     Vernier_Scan information added (Bit_30).
c Run_36769  *  Downscale factor for Sng_C_E2 set from 10 to 100.
c Run_36897     Bit_01 SNG_C_E2 disabled.
c Run_37370     FINCHtest and TAG_2GAM removed
c Run_37389     SNG_MUX2 and SNG_EMX2 added (L2-update SNG_MUON and SNG_C_EM)
c Run_37445     LW_LO_HI added (Bit_00) enabled RUN 37456
c Run_40100     SiCAL_LO, SiCAL_HI, SiCAL_MX removed
c Run_40100     LW_LO_HI moved from Bit_00 to Bit_05; LW_A+BLO added on Bit_06
c Run_40100     Vernier_Scan (Bit_30) removed
c Run_40200     COS_HCBA (Bit_30) added and enabled
c Run_40300  *  SiCAL_ME dwnsca(1-->4)  LW_A+BLO dwnsca(1-->10)
c Run_40394  *  SICAL_ME dwnsca(4-->5)  LW_A+BLO dwnsca(10-->20)
c
         IF(KRUN.GE.36092)FACTOR(7) = 1./  10.
         IF(KRUN.GE.36769)FACTOR(2) = 1./ 100.
         IF(KRUN.GE.40300)FACTOR(5) = 1./ 4.  
         IF(KRUN.GE.40300)FACTOR(7) = 1./ 10. 
         IF(KRUN.GE.40394)FACTOR(5) = 1./ 5.  
         IF(KRUN.GE.40394)FACTOR(7) = 1./ 20. 
       ELSEIF(ISET.EQ.3)THEN
c ***********************************************************************************
c  1996 changes
c ***************************************************************************************************************
C Run_41408      LCW_ETT thresholds adapted for 161 GeV
C Run_41418      LCW_ETT threshold 1 changed from 36 GeV to 32 GeV
C Run_41485      LCW_ETT single arm low 40 Gev, single arm high 48 Gev
C Run_41565      TRK_CNT2 level2 definition changed: inner or outer track count >2
C Run_41612      DBL_CTST bit 24 defined: two ECW modules,ITC_RPZ, ITC/TPC trk_cnt .ge. 2 
C Run_41706      DBL_CTST enabled
       ENDIF
       PRINTIT=.FALSE.
       DO I = 1,NBITS
        IF(OLDFAC(I).NE.FACTOR(I))PRINTIT=.TRUE.
       ENDDO
       IF(PRINTIT)THEN
C        WRITE(6,*)' _QUL2WT_ factors changed at run ',
C     1  KRUN
C        DO I = 1,NBITS
C             WRITE(6,*)' LEVEL 2 FACTOR ',I,' = ',FACTOR(I)
C        ENDDO
       ENDIF
       DO I = 1,NBITS
        OLDFAC(I) = FACTOR(I)
       ENDDO
      ENDIF
       AL2WT = 1.0
       IF(ISET.EQ.0)RETURN
       IF(XMCEV)RETURN
C 
C
C ANALYSE SET TRIGGER BITS
C
      KTRIG=KXTEL2
C
C CALCULATE THE ACCEPTED FRACTION WITH THIS 
C COMBINATION OF TRIGGER BITS
C
C look at the last 24 bits in the mask
C IF ONE IS SET - GET OUT
C
       DO  IBIT  = 9,32
             ITEST=JBIT(KTRIG,IBIT)
             IF(ITEST.EQ.1)RETURN
       ENDDO
      REJECT=1.0
      ACCEPT=0.0
C
C LOOK AT DOWNSCALED TRIGGERS
C
C      WRITE(6,*)' WARNING EVENT TRIGGERED BY BIT NO. < 8 ONLY'
       DO  IBIT  = 1,8
             ITEST=JBIT(KTRIG,IBIT)
             IF(ITEST.EQ.1)THEN
C                        WRITE(6,*)' TRIGGER BIT IS ',IBIT
                        ACCEPT=ACCEPT+FACTOR(IBIT)*REJECT
                        REJECT=REJECT*(1-FACTOR(IBIT))
             ENDIF
       ENDDO
C
        IF(ACCEPT.GT.0)THEN
        IF(ACCEPT.LE.1.0)THEN
          AL2WT = 1/ACCEPT
        ELSE
          AL2WT = 1.0
        ENDIF
        ENDIF
C        WRITE(6,*)' LEVEL 2 SCALE FACTOR = ',AL2WT
      RETURN
      END
C-----------------------------------------------------------------------
      SUBROUTINE QL3REJ(LEVEL3)
C--------------------------------------------------------------------
C
C ANALYSE LEVEL 3 REJECTION
C
C--------------------------------------------------------------------
      INCLUDE '/aleph/phy/qcde.inc'
      INCLUDE '/aleph/phy/qhac.inc'
C
      LOGICAL FIRST
      LOGICAL LEVEL3
 
      INCLUDE '/aleph/phy/qmacro.inc'
 
      DATA    FIRST/.TRUE./
 
      IF(FIRST)THEN
            NAEVEH= NAMIND('EVEH')
            FIRST=.FALSE.
      ENDIF
 
      LEVEL3 = .TRUE.
 
      KEVEH= IW(NAEVEH)
      IF(KEVEH.EQ.0)GOTO 999
C
C LOOK AT EVEH WORD 8 BITS 16 - 24
C  IF 21 ( HEX ) , 33 ( DEC ) THEN THIS EVENT IS A LEVEL 3 REJECT
C
      IWORD = IW(KEVEH+8)
C
C FIND BITS 16 - 24
C
      JTRIG = JBYT(IWORD,17,8)
 
      IF(JTRIG.EQ.33)LEVEL3 = .FALSE.
999   RETURN
      END

