      SUBROUTINE QUTRWT(IDEB,AL2WT,AL3WT,LTRIG)
C---------------------------------------------------------------
C!
C! ANALYSE TRIGGER, RETURN LEVEL 2 WEIGHT, LEVEL 3 WEIGHT AND
C! SET TRIGGER BITS
C!
C - A.FINCH 26/8/97
C   modified 24/8/98 due to corrupt MINI data
C - Input :   IDEB INTEGER DEBUG LEVEL REQUESTED
C                          0 = NONE
C                          1 = WARNINGS
C                          2 = INFORMATION
C - Output arguments : AL2WT  REAL  LEVEL 2 WEIGHT FOR THIS EVENT
C                      AL3WT  REAL  LEVEL 3 WEIGHT FOR THIS EVENT
C                      LTRIG(6) INTEGER ARRAY CONTAINING ONES AND ZEROS
C                               1 = BIT WAS SET
C                               0 = BIT WAS NOT SET

C                              BITS ARE: SNG_N_EL  1
C                                        SNG_C_E2  2
C                                        TRK_CNT2  3
C                                        SNG_N_EC  4
C                                        DBL_C_E2  5
C                                        SNG_MUON  6
C
C  CONTROL CARDS            NL3W DISABLES LEVEL 3 WEIGHT CALCULATION
C                           NL2W DISABLES LEVEL 2 WEIGHT CALCULATION
C CALLS                  QUL2WT
C                        QUL3WT
C---------------------------------------------------------------
      INTEGER IDEB,LTRIG(*)
      REAL AL2WT,AL3WT      
      CALL  QUL3WT (IDEB,AL3WT,LTRIG)
      CALL  QUL2WT (IDEB,AL2WT)
      RETURN
      END
C
C
       SUBROUTINE QUL2WT (IDEB,AL2WT)
C---------------------------------------------------------------
C!
C! ANALYSE LEVEL 2 TRIGGER WEIGHTS
C - A.FINCH 26/8/97
C - Input :   IDEB INTEGER DEBUG LEVEL REQUESTED
C                          0 = NONE
C                          1 = WARNINGS
C                          2 = INFORMATION
C - Output arguments : AL2WT  REAL  LEVEL 2 WEIGHT FOR THIS EVENT
C  CONTROL CARDS        NL2W DISABLES LEVEL 2 WEIGHT CALCULATION
C CALLED BY                GUTWT
C CALLS                  QUWR
C---------------------------------------------------------------

C
C
C RETURN CORRECT WEIGHTING TO ALLOW FOR LEVEL 2 VETO
C
#include "qcde.h"
#include "qhac.h"
       LOGICAL PRINTIT,FIRST
       PARAMETER   (NBITS=8,NSET=3)
       REAL        DWNSC(NBITS,NSET),FACTOR(NBITS),OLDFAC(NBITS)
C 
       CHARACTER*8 NAM32(32)
       LOGICAL     ENABLE(32)
       COMMON    /CGGTRG/ NAM32,ENABLE

#include "qmacro.h"
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.,10.,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 

      DATA FIRST/.TRUE./



      IF(FIRST)THEN
           FIRST = .FALSE.
           INL2W = 0
           INL2W = IW(NAMIND('NL2W',0))
           IF(INL2W.NE.0)THEN
            IF(IDEB.GE.1)THEN
             CALL QUWR(' ')
             CALL QUWR(' ')
             CALL QUWR(
     1' *** WARNING - LEVEL 2 WEIGHTING SUPPRESSED *** ')
             CALL QUWR(
     1' ***      BY NL2W CARD                      *** ')
             CALL QUWR(' ')
             CALL QUWR(' ')
            ENDIF
           ENDIF
           NAX1RG = NAMIND('X1RG')                                        
      ENDIF
      AL2WT = 1.0
      IF(INL2W.NE.0)RETURN
      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
        IF(IDEB.GE.2)WRITE(6,*)' _QUL2WT_ factors changed at run ',
     1  KRUN
        DO I = 1,NBITS
             IF(IDEB.GE.2)
     1       WRITE(6,*)' LEVEL 2 FACTOR ',I,' = ',FACTOR(I)
        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 SPECIAL CHECK TO SOLVE A PROBLEM WITH CERTAIN
C MINI FILES WHERE THE X1RG BANKS COLUNN1 1 IS CORRUPT
C
      IF(KXTEL2.EQ.16.AND.KXTET2.EQ.4.AND.KXTET1.EQ.66)THEN             
        IF(IDEB.GE.2)THEN                                                
           WRITE(6,*)'  '                                               
           WRITE(6,*)'  '                                               
           WRITE(6,*)'  '                                               
           WRITE(6,*)' PROBLEM WITH TRIGGER INFORMATION '                                      
           WRITE(6,*)' RUN EVENT KXTET1...',                              
     1     KRUN,KEVT,KXTET1,KXTET2,KXTEL2                                 
           CALL PRTABL('X1RG',0)                                          
         ENDIF
C                                                                       
C FIND THE LEVEL2 TRIGGER INFORMATION FROM X1RG  ROW 3                       
C                                                
        IF(IDEB.GE.2)
     >  WRITE(6,*)' TAKING INFO. FROM ROW 3 OF X1RG '
        IX1RG = IW(NAX1RG)                                              
        IF(IX1RG.GT.0)THEN                                              
C         C1 = CHAINT(ITABL(IX1RG,3,1))                                  
C         I1 = ITABL(IX1RG,3,2)                                          
C         I2 = ITABL(IX1RG,3,3)                                          
         I3 = ITABL(IX1RG,3,4)                                          
        ENDIF                                                           
        KTRIG = I3
        IF(IDEB.GE.2)WRITE(6,*)' KTRIG SET TO ',KTRIG
       ENDIF                                                           

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
      IF(IDEB.GE.2)then
        WRITE(6,*)' WARNING EVENT TRIGGERED BY BIT NO. < 8 ONLY'
        WRITE(6,60)KTRIG
 60     format(' Trigger mask: ',Z4)
        WRITE(6,*)' Triggers:'
        DO  IBIT  = 1,32
             ITEST=JBIT(KTRIG,IBIT)
             if(ENABLE(IBIT))THEN
              write(6,*)
     >        '  ',ibit,' ',ITEST,' ',nam32(ibit),' enabled '
             else
              write(6,*)
     >        '  ',ibit,' ',ITEST,' ',nam32(ibit),' disabled '
             endif
        enddo
      endif
       DO  IBIT  = 1,8
             ITEST=JBIT(KTRIG,IBIT)
             IF(ITEST.EQ.1)THEN
                        IF(IDEB.GE.2)
     1                  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
        IF(IDEB.GE.2)
     >  WRITE(6,*)' LEVEL 2 SCALE FACTOR = ',AL2WT
      RETURN
      END

       SUBROUTINE QUL3WT(IDEB,AL3WT,L3BIT) 
C---------------------------------------------------------------
C!
C!RETURN CORRECT WEIGHTING TO ALLOW FOR LEVEL 3 VETO
C!
C - A.FINCH 26/8/97
C - Input :   IDEB INTEGER DEBUG LEVEL REQUESTED
C                          0 = NONE
C                          1 = WARNINGS
C                          2 = INFORMATION
C - Output arguments : AL3WT  REAL  LEVEL 3 WEIGHT FOR THIS EVENT
C                      LTRIG(6) INTEGER ARRAY CONTAINING ONES AND ZEROS
C                               1 = BIT WAS SET
C                               0 = BIT WAS NOT SET
C                              BITS ARE: SNGNEL=1
C                                        SNGCE2=2
C                                        TRKCN2=3
C                                        SNGNEC=4
C                                        DBLCE2=5
C                                        SNGMUON=6
C  CONTROL CARDS            NL3W DISABLES LEVEL 3 WEIGHT CALCULATION
C CALLS                  QUWR
C                        QUXNAM
C                        QL3REJ
C CALLED BY              QUTRWT
C---------------------------------------------------------------
C
C
#include "qcde.h"
#include "qhac.h"
       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+1),TBIT(NBITS)
       INTEGER     AGETDB

       INTEGER     MASK(32)
       REAL        FTEMP(32),FX3(32)
       PARAMETER   (NSET=5)
       INTEGER      KTRIG,ICALL(NSET)

       PARAMETER   (NL3OFF=33)
       INTEGER     L3OFRN(2*NL3OFF)

       CHARACTER*4 CHAINT
       CHARACTER*8 NAME(NBITS),TNAME

       REAL        FACTOR(NBITS,NSET)
       LOGICAL     FLAG,L3ON
       LOGICAL     FIRST,SCE2,SNEL,TKC2,DCE2,SNEC,SNMU

       CHARACTER*8 NAM32(32)
       LOGICAL     ENABLE(32)
       COMMON    /CGGTRG/ NAM32,ENABLE

       EXTERNAL    CHAINT,AGETDB
 
#include "qmacro.h"
 
       DATA ICOUNT/0/
       DATA FIRST/.TRUE./
       DATA CRUN/0/
       DATA BIT/NBITS*-1/

       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/


       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
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
C FIRST TIME - LOOK FOR CONTROL CARDS
C
      IF(FIRST)THEN
           FIRST = .FALSE.
           IF(IDEB.GE.2)CALL QUXNAM
           INL3W = IW(NAMIND('NL3W',0))
           IF(INL3W.NE.0)THEN
            IF(IDEB.GE.1)THEN
             CALL QUWR(' ')
             CALL QUWR(' ')
             CALL QUWR(
     1' *** WARNING - LEVEL 3 WEIGHTING SUPPRESSED *** ')
             CALL QUWR(
     1' ***      BY NL3W CARD                      *** ')
             CALL QUWR(' ')
             CALL QUWR(' ')
            ENDIF
           ENDIF
           IF(IDEB.GE.1)THEN
             WRITE(6,*)' ACCEPTANCE FACTORS FOR BITS...'
             WRITE(6,600)(NAME(I),I=1,NBITS)
600         FORMAT(4(A8,' '))
            DO J = 1,NSET
                 WRITE(6,*)(FACTOR(I,J),I=1,NBITS)
            ENDDO
           ENDIF
       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                                                
          IF(IDEB.GE.1)
     >    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
         IF(FLAG.NE.ENABLE(IBIT))THEN
          IF(IDEB.GE.2)THEN
           WRITE(6,*)' FLAG FOR BIT ',IBIT,' ',NAM32(IBIT),
     1     ' CHANGED FROM ',ENABLE(IBIT),' TO ',FLAG
          ENDIF
         ENDIF
         ENABLE(IBIT) = FLAG

         TNAME = CHAINT(ITABL(JXTBN,K,JXTBBM)) //
     1               CHAINT(ITABL(JXTBN,K,JXTBBM+1))
         IF(TNAME.NE.NAM32(IBIT))THEN
          IF(IDEB.GE.2)THEN
          WRITE(6,*)' NAME OF BIT ',IBIT,' CHANGED FROM ',NAM32(IBIT),
     1    ' TO ',TNAME
          ENDIF
         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(IDEB.GE.1)THEN
               IF(BIT(I).NE.IPOINT)THEN
                 IF(IPOINT.GT.-1)THEN
                  WRITE(6,*)'  RUN: ',KRUN
                  WRITE(6,*)' TRIGGER ',NAME(I),' IS NOW ON BIT ',
     1            IPOINT
                 ELSE
                  WRITE(6,*)'  RUN: ',KRUN
                  WRITE(6,*)' TRIGGER ',NAME(I),' IS ON NO BIT NOW '
                 ENDIF
               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
C SINGLE MUON IS A SPECIAL CASE - JUST FOR TRIGGER
C EFFICIENCY CALCULATIONS IN OTHER ROUTINES 
C
C................................................................
        SNMU = .FALSE.
        IF(MASK(ISNGMU).GT.0)SNMU = .TRUE.
        L3BIT(NBITS+1) = 0
        IF(SNMU)L3BIT(NBITS+1) = 1
C................................................................
C
C IF THERE ARE NO TRIGGERS IN A REAL EVENT WE MUST REJECT THE EVENT !
C
C................................................................
      IF(.NOT.XMCEV.AND.IMSUM.EQ.0)THEN
           AL3WT = 0.0
           GOTO 999
      ENDIF
C................................................................
C
C IF USER HAS REQUESTED THAT NO LEVEL 3 CORRECTION SHOULD BE PERFORMED
C - JUMP OUT HERE, HAVING FILLED L3BIT ARRAY
C
C................................................................
       IF(INL3W.NE.0)GOTO 2000
C................................................................
C
C CHECK FOR PERIODS WHEN LEVEL 3 WAS DISABLED
C THIS INORMATION IS CURRENTLY NOT STORED IN THE RUN HEADER !
C LOOP OVER ALL THE PERIODS WHEN LEVEL3 WAS OFF, IF WE ARE IN 
C ONE THEN RETURN 
C
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
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
C................................................................
      CALL QL3REJ(LEVEL3)
      IF(.NOT.LEVEL3)THEN
C................................................................
C
C SEE IF THE X3PS BANKS EXISTS, IF SO LOOK UP REJECT FRACTION IN IT
C
C......................................................
C                                      Subschema: TRIGGERLEVEL3
C+------+
C| X3PS |  Level 3 vetoes, fraction
C+------+  passed .
C..............................................................
C    1          I    Number of columns (=32)
C    2          I    Number of rows = 1
C..............................................................
C  1-32  TR  F    TranRej          [0.0,*]
C                    fractions passed/triggerbit LEVEL 3
       IX3PS = IW(NAMIND('X3PS',0))
       IF(IX3PS.GT.0)THEN
        IF(IDEB.GE.2)THEN
         WRITE(6,*)' FOUND X3PS BANK RUN/EVENT ',KRUN,KEVT
         CALL PRTABL('X3PS',0)
        ENDIF
        DO IX3BIT = 1,32
           FX3(IX3BIT) = RTABL(IX3PS,1,IX3BIT)
        ENDDO
        IF(IDEB.GE.2)WRITE(6,*)' FX3 ',FX3
       ELSE
        IF(IDEB.GE.2)
     >  WRITE(6,*)' NO X3PS BANK - USE DATA STATEMENTS'
       ENDIF
C................................................................
C
C SET THE TEMP FACTORS FROM EITHER (A) HARD WIRED DATA TABLE
C                            OR    (B) X3PS
C................................................................
      DO I = 1,32
        FTEMP(I) = 1.000001
      ENDDO
      DO I = 1,NBITS
        FTEMP(BIT(I)) = FACTOR(I,ISET)
      ENDDO
C
C CHECK FACTORS AGAINST EXPECTATIONS, if we found an X3PS bank
C
      IF(IX3PS.GT.0)THEN
       DO I = 1,32

        IF(FTEMP(I).NE.FX3(I))THEN
           IF(IDEB.GE.2)
     >        WRITE(6,*)' REPLACING LEVEL 3 FACTOR FROM DATA STATEMENT',
     >                  FTEMP(I),' WITH FACTOR FROM X3PS BANK ',
     >                  FX3(I)
              FTEMP(I) = FX3(I)
         ELSEIF(FTEMP(I).LT.1.0)THEN
              IF(IDEB.GE.2)
     >        WRITE(6,*)' X3PS FACTOR AGREES WITH DATA STATEMENT '
         ENDIF
       ENDDO
      ENDIF
C
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
            IF(IDEB.GE.1)
     >       WRITE(6,*)' NON ENABLED BIT SET ',I,
     >       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.FTEMP(BIT(J)).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+FTEMP(BIT(I))*REJECT
                        REJECT=REJECT*(1-FTEMP(BIT(I)))
                ENDIF
        ENDDO
C
        ICALL(ISET) = ICALL(ISET) + 1
        IF(IDEB.GE.1)THEN
         IF(ICALL(ISET).LT.20)THEN
                WRITE(6,*)'SNEL,SCE2,TKC2,SNEC,DCE2',
     1                     SNEL,SCE2,TKC2,SNEC,DCE2
                WRITE(6,*)' ISET = ',ISET,
     1                    ' ACCEPTED FRACTION ',ACCEPT
                WRITE(6,*)L3BIT
         ENDIF
        ENDIF
        IF(ACCEPT.GT.0)THEN
         IF(ACCEPT.LE.1.0)THEN
          AL3WT = 1/ACCEPT
         ELSE
          AL3WT = 1.0
         ENDIF
        ELSE
         IF(IDEB.GE.1)THEN
          WRITE(6,*)'__QUL3WT__ ERROR - ACCEPT = 0.0 !',
     >    KRUN,' ',KEVT
          WRITE(6,*)'SNEL,SCE2,TKC2,SNEC,DCE2',
     >    SNEL,SCE2,TKC2,SNEC,DCE2
         ENDIF
         AL3WT = 1.0
        ENDIF
      ENDIF
C
2000  CONTINUE
999   RETURN
      END
      SUBROUTINE QUXNAM
C---------------------------------------------------------------
C!
C! PRINT OUT TRIGGER DEFINITION INFORMATION
C - A.FINCH 26/8/97
C
C---------------------------------------------------------------

#include "qcde.h"
#include "qhac.h"
      INTEGER   AGETDB
      CHARACTER*4 CHAINT,MNEMON(2),FULL(10)
      EXTERNAL    CHAINT,AGETDB
#include "qmacro.h"
C
C WRITE OUT A SUMMARY OF ALL XTBN INFORMATION
C ON THE DAF
C

C - Get bank with trigger bit names (XTBN)                              
C      ISTAT = AGETDB('XTBN',KRUN)                                       
C      IF(ISTAT.EQ.0)THEN                                                
C         CALL ALTELL('+++QUXNAM+++ Bank XTBN not found',0,'RETURN')     
C         GOTO 999                                                       
C      ENDIF                                                             
      LUN = JUNIDB(0) 
      WRITE(6,*)' DATABASE IS ON ',LUN
      NR = 999999
1     NR = NDANR(LUN,'XTBN','LE',NR-1)
      IF(NR.EQ.0)RETURN
      WRITE(6,*)'    '
      WRITE(6,*)' XTBN ',NR
      WRITE(6,*)'    '
      JXTBN = MDARD(IW,LUN,'XTBN',NR)
C      JXTBN = IW(NAMIND('XTBN'))                                        
      IF(JXTBN.NE.0)THEN                        
        WRITE(6,*)' ID   Validity Range Defined  Bit   Mnemonic ',
     1'    Name '
        WRITE(6,*)'                      Flag   Number '
                       
        DO 50 K=1,LROWS(JXTBN)                                          

         MNEMON(1) = CHAINT(ITABL(JXTBN,K,JXTBBM))
         MNEMON(2) = CHAINT(ITABL(JXTBN,K,JXTBBM+1))
         DO 49 I = 0,9
49       FULL(I+1) = CHAINT(ITABL(JXTBN,K,JXTBBN+I))

         WRITE(6,1000)ITABL(JXTBN,K,JXTBID),
     1ITABL(JXTBN,K,JXTBVR),ITABL(JXTBN,K,JXTBVR+1),
     1ITABL(JXTBN,K,JXTBTN),ITABL(JXTBN,K,JXTBTB),
     1MNEMON,FULL
1000    FORMAT(1X,I3,1X,I8,1X,I8,1X,I5,1X,I5,1X,2A4,2X,10A4)
   50   CONTINUE                                                        
      ENDIF                                                             
   55 CONTINUE                                                          
      GOTO 1
C  999 RETURN                                                           
      END                  

C-----------------------------------------------------------------------
      SUBROUTINE QUWR(TEXT)
C---------------------------------------------------------------
C!
C! PRINT OUT TEXT
C!
C INPUT: TEXT TO PRINT
C---------------------------------------------------------------

C-----------------------------------------------------------------------
#include "qcde.h"
      CHARACTER*(*) TEXT
#include "qmacro.h"
      WRITE(IW(6),*)TEXT
      RETURN
      END
C-----------------------------------------------------------------------
      SUBROUTINE QL3REJ(LEVEL3)
C--------------------------------------------------------------------
C!
C! ANALYSE LEVEL 3 REJECTION
C!
C INPUT: NONE 
C OUTPUT: LEVEL3 
C             TRUE IF LEVEL 3 ACCEPTED THIS EVENT
C             FALSE IF LEVEL 3 REJECTED THIS EVENT
C INPUT BANKS:  EVEH (WORD 8, BITS 16-24)
C--------------------------------------------------------------------
#include "qcde.h"
#include "qhac.h"
C
      LOGICAL FIRST
      LOGICAL LEVEL3
 
#include "qmacro.h"
 
      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

