Esempio n. 1
0
    def process(self):

        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk   = cut_flow_tracker(cut_flow_histo)
        cut_flow_trk.Fill('bare')

        def preselection(row, cut_flow_trk):
            double_mu_pass =  row.doubleMuPass and \
                row.m1MatchesDoubleMuPaths > 0 and \
                row.m2MatchesDoubleMuPaths > 0
            double_muTrk_pass = row.doubleMuTrkPass and \
                 row.m1MatchesMu17TrkMu8Path > 0 and \
                 row.m2MatchesMu17TrkMu8Path > 0
            if not ( double_mu_pass or double_muTrk_pass ): return False
            cut_flow_trk.Fill('trigger')

            if not row.m1_m2_SS: return False
            cut_flow_trk.Fill('m1_m2_SS')

            if row.m1_m2_DR < 0.5: return False
            cut_flow_trk.Fill('m1_m2_DR')

            if row.m2Pt > row.m1Pt: return False
            if row.m1_m2_Mass < 20: return False
            cut_flow_trk.Fill('m1_m2_Mass')

            if not selections.muSelection(row, 'm1', cut_flow_trk):  return False
            if not row.m1Pt > 20: return False
            cut_flow_trk.Fill('lead mu pt')
            if not row.m1PFIDTight: return False
            cut_flow_trk.Fill('m1PFIDTight')
            #cut_flow_trk.Fill('lead mu preselection')

            if not selections.muSelection(row, 'm2'):  return False
            cut_flow_trk.Fill('sublead mu preselection')

            if not selections.vetos(row, cut_flow_trk):  return False #applies mu bjet e additional tau vetoes
            #if not (row.jetVeto40_DR05 >= 1): return False
            if row.jetVeto20 == 0: return False
            cut_flow_trk.Fill('Jet presence')

            return True

        def fill(the_histos, row, fillNtuple=False):
            # Get PU weight - fix me
            weight = 1
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU) * \
                         mcCorrectors.double_muon_trigger(row,'m1','m2')
                        
            the_histos['muonPt'].Fill(row.m2Pt, weight)
            the_histos['muonJetPt'].Fill(max(row.m2JetPt, row.m2Pt), weight)
            the_histos['muonAbsEta'].Fill(row.m2AbsEta, weight)
            the_histos['muonPtRatio'].Fill(row.m2Pt/max(row.m2JetPt, row.m2Pt), weight)
            the_histos['muonPtDiff'].Fill(max(row.m2JetPt, row.m2Pt) - row.m2Pt, weight)

            the_histos['m1MtToMET'].Fill(row.m1MtToMET, weight)
            the_histos['m1m2Mass' ].Fill(row.m1_m2_Mass, weight)
            the_histos['m2MtToMET'].Fill(row.m2MtToMET, weight)
            the_histos["m1JetBtag"].Fill(row.m1JetBtag, weight)
            the_histos["m2JetBtag"].Fill(row.m2JetBtag, weight)

            the_histos['m2JetptD'].Fill(row.m2JetptD, weight)
            the_histos['m2Jetmult'].Fill(row.m2Jetmult, weight)

            if fillNtuple:
                pfidiso02    = float( row.m2PFIDTight and row.m2RelPFIsoDB < 0.2)
                h2taucuts    = float( row.m2PFIDTight and ((row.m2RelPFIsoDB < 0.15 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.1 ))
                h2taucuts020 = float( row.m2PFIDTight and ((row.m2RelPFIsoDB < 0.20 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.15))
                muon_jet_mass = -1. #inv_mass(row.m1Pt, row.m1Eta, row.m1Phi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)

                the_histos['muonInfo'].Fill( array("f", [row.m2Pt, max(row.m2JetPt, row.m2Pt), row.m2AbsEta, max(0, row.m2JetCSVBtag), 
                                                         abs(row.m2PVDXY), muon_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight, 
                                                         pfidiso02, h2taucuts, h2taucuts020] ) )
        
        def fill_region(region, tag):
            # This is a QCD or Wjets
            fill(histos[(region, tag)], row, True)

            if row.m2PFIDTight:
                if row.m2RelPFIsoDB < 0.2:
                    fill(histos[(region, tag, 'pfidiso02')], row)

                if (row.m2RelPFIsoDB < 0.15 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.1:
                    fill(histos[(region, tag, 'h2taucuts')], row)

                if (row.m2RelPFIsoDB < 0.20 and row.m2AbsEta < 1.479) or row.m2RelPFIsoDB < 0.15:
                    fill(histos[(region, tag, 'h2taucuts020')], row)

        histos = self.histograms
        for row in self.tree:
            cut_flow_trk.new_row(row.run,row.lumi,row.evt)
            cut_flow_trk.Fill('bare')
            if not preselection(row, cut_flow_trk):
                continue

            region = control_region(row)
            if row.m1RelPFIsoDB > 0.3:
                cut_flow_trk.Fill('muon isolation')
                if row.type1_pfMetEt < 25:
                    cut_flow_trk.Fill('MET')

            if region is None:
                continue
            cut_flow_trk.Fill('region assignment')

            if region_for_event_list and region == region_for_event_list:
                print '%i:%i:%i' % (row.run, row.lumi, row.evt)
                continue

            fill_region(region, 'pt10')
            if region == 'wjetsLtLow' and row.m1MtToMET > 55:
                fill_region('wjets', 'pt10')

            if row.m2Pt > 20:
                fill_region(region, 'pt20')
                if region == 'wjetsLtLow' and row.m1MtToMET > 55:
                    fill_region('wjets', 'pt20')
Esempio n. 2
0
    def process(self):
        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk = cut_flow_tracker(cut_flow_histo)

        def preselection(row, cut_flow_trk):
            if not row.doubleEPass: return False
            if not (row.e1MatchesDoubleEPath > 0 and \
                row.e2MatchesDoubleEPath > 0):
                return False
            cut_flow_trk.Fill('trigger')

            if row.e1_e2_DR < 0.5: return False
            cut_flow_trk.Fill('e1_e2_DR')

            if not row.e1Pt > 20: return False
            if not selections.eSelection(row, 'e1'): return False
            if not row.e1MVAIDH2TauWP: return False
            cut_flow_trk.Fill('e1 presel')

            if not selections.eSelection(row, 'e2'): return False
            cut_flow_trk.Fill('e2 presel')

            #if not (row.jetVeto40_DR05 >= 1):             return False
            if row.jetVeto20 == 0: return False
            cut_flow_trk.Fill('jet requirement')

            if not selections.vetos(row, cut_flow_trk): return False

            return True

        def fill(the_histos, row, fillNtuple=False):
            weight = 1
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU)

            the_histos['electronPt'].Fill(row.e2Pt)
            the_histos['electronJetPt'].Fill(max(row.e2JetPt, row.e2Pt))
            the_histos['electronAbsEta'].Fill(row.e2AbsEta)
            #the_histos['metSignificance'].Fill(row.metSignificance)
            the_histos['e1MtToMET'].Fill(row.e1MtToMET)
            the_histos['e2MtToMET'].Fill(row.e2MtToMET)
            the_histos['MET'].Fill(row.type1_pfMetEt)
            the_histos['OSS'].Fill(int(row.e1_e2_SS))

            the_histos['e2JetptD'].Fill(row.e2JetptD)
            the_histos['e2Jetmult'].Fill(row.e2Jetmult)

            the_histos['e2Jetmultvse2JetPt'].Fill(max(row.e2JetPt, row.e2Pt),
                                                  row.e2Jetmult)
            the_histos['e2JetptDvse2JetPt'].Fill(max(row.e2JetPt, row.e2Pt),
                                                 row.e2JetptD)

            the_histos['e1e2Mass'].Fill(row.e1_e2_Mass)
            the_histos['doubleEPrescale'].Fill(row.doubleEPrescale)

            if fillNtuple:
                id_iso_vals = [
                    selections.lepton_id_iso(row, 'e2', label)
                    for label in self.lepIds
                ]
                #print id_iso_vals
                #print self.lepIds
                id_iso_vals = [float(i) for i in id_iso_vals]
                electron_jet_mass = -1  #inv_mass(row.e2Pt, row.e2Eta, row.e2Phi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)

                the_histos['electronInfo'].Fill(
                    array("f", [
                        row.e2Pt,
                        max(row.e2Pt, row.e2JetPt),
                        max(0, row.e2JetCSVBtag), electron_jet_mass,
                        row.jetVeto20, row.jetVeto40_DR05, weight
                    ] + id_iso_vals))
                the_histos['evtInfo'].Fill(row)

        histos = self.histograms
        #pprint( histos)
        for row in self.tree:
            cut_flow_trk.new_row(row.run, row.lumi, row.evt)
            cut_flow_trk.Fill('bare')

            if not preselection(row, cut_flow_trk):
                continue

            region = selections.control_region_ee(row)
            if region is None:
                continue

            if region == 'zee':
                continue

            if region_for_event_list and region == region_for_event_list:
                cut_flow_trk.Fill('region assignment')
                if zMassCut and not (60 < row.e1_e2_Mass < 120):
                    cut_flow_trk.Fill('zMassCut')

            # This is a QCD or Wjets
            if region_for_event_list and region == region_for_event_list\
               and (not zMassCut or not (60 < row.e1_e2_Mass < 120)) \
               and not SYNC:
                print '%i:%i:%i' % (row.run, row.lumi, row.evt)
                continue

            def make_region_plots(full_region):
                fill(histos[full_region], row, True)

                for idlabel, idfcn in selections.electronIds.iteritems():
                    if not idfcn(row, 'e2'):  #if it does not pass id skip!
                        continue
                    if row.e2RelPFIsoDB < 0.2:
                        fill(histos[full_region + (idlabel + '_idiso02', )],
                             row)
                    if (row.e2RelPFIsoDB < 0.15 and
                            row.e2AbsEta < 1.479) or row.e2RelPFIsoDB < 0.1:
                        fill(histos[full_region + (idlabel + '_h2taucuts', )],
                             row)
                    if (row.e2RelPFIsoDB < 0.20 and
                            row.e2AbsEta < 1.479) or row.e2RelPFIsoDB < 0.15:
                        fill(
                            histos[full_region +
                                   (idlabel + '_h2taucuts020', )], row)

            make_region_plots((region, 'pt10'))
            if not (row.e1_e2_Mass > 60 and row.e1_e2_Mass < 120):
                make_region_plots((region + 'NoZmass', 'pt10'))
                if region == 'wjetsLtLow' and row.e1MtToMET > 55:
                    make_region_plots(('wjetsNoZmass', 'pt10'))

            if row.e2Pt > 20:
                make_region_plots((region, 'pt20'))
                if not (row.e1_e2_Mass > 60 and row.e1_e2_Mass < 120):
                    make_region_plots((region + 'NoZmass', 'pt20'))
                    if region == 'wjetsLtLow' and row.e1MtToMET > 55:
                        make_region_plots(('wjetsNoZmass', 'pt20'))
    def process(self):

        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk = cut_flow_tracker(cut_flow_histo)
        myevent = ()
        #print self.tree.inputfilename
        for row in self.tree:
            jn = row.jetVeto30
            if jn > 3: jn = 3

            #if row.run > 2:
            if not bool(row.singleE27WP80Pass): continue
            #            if hasattr(self.tree, 'row.e1MatchesEle27WP80') and hasattr(self.tree, 'row.e2MatchesEle27WP80') :
            #if not bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : continue

            #else :
            if not bool(row.e3MatchesSingleE27WP80): continue
            #if not bool(row.singleEPass) : continue
            #if not bool(row.e1MatchesSingleE) and  not bool(row.e2MatchesSingleE) : continue

            if row.bjetCSVVeto30 != 0: continue
            if row.e1Pt < 30: continue
            if row.e2Pt < 30: continue
            if row.e3Pt < 30: continue

            #        for i, row in enumerate(self.tree):
            #            if  i >= 100:
            #                return
            # print bool(cut_flow_trk.disabled)
            cut_flow_trk.new_row(row.run, row.lumi, row.evt)
            #print row.run,row.lumi,row.evt
            cut_flow_trk.Fill('allEvents')
            if not selections.eSelection(row, 'e1'): continue
            cut_flow_trk.Fill('e1sel')
            if not selections.lepton_id_iso(row, 'e1', 'eid13Loose_idiso05'):
                continue
            if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta < 1.566): continue

            cut_flow_trk.Fill('e1IDiso')
            if not selections.eSelection(row, 'e2'): continue
            cut_flow_trk.Fill('e2sel')
            if not selections.lepton_id_iso(row, 'e2', 'eid13Loose_idiso05'):
                continue
            if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566: continue

            ##            cut_flow_trk.Fill('e2IDiso')
            ##            if not abs(row.e1_e2_Mass-91.2) < 20: continue
            ##            cut_flow_trk.Fill('ZMass')
            if not selections.eSelection(row, 'e3'): continue
            if not selections.lepton_id_iso(row, 'e3', 'eid13Loose_idiso05'):
                continue  #very loose loose eid13Tight_mvaLoose
            if abs(row.e3Eta) > 1.4442 and abs(row.e3Eta) < 1.566: continue

            Zs = [(abs(row.e1_e2_Mass - 91.2), ['e1', 'e2', 'e3']),
                  (abs(row.e2_e3_Mass - 91.2), ['e2', 'e3', 'e1']),
                  (abs(row.e1_e3_Mass - 91.2), ['e1', 'e3', 'e2'])]

            for ele in range(0, 2):

                if Zs[ele][0] == min(Zs[z][0] for z in range(0, 2)):
                    self.mye1 = Zs[ele][1][0]
                    self.mye2 = Zs[ele][1][1]
                    self.mye3 = Zs[ele][1][2]

            cut_flow_trk.Fill('tsel')

            if row.tauVetoPt20EleTight3MuLoose: continue
            if row.muVetoPt5IsoIdVtx: continue
            if row.eVetoCicLooseIso: continue  # change it with Loose

            #if not row.e3MtToMET < 50:  continue
            cut_flow_trk.Fill('MtToMet')

            #if (row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)==myevent: continue
            #myevent=(row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)

            eleiso = 'eLoose'
            sign = 'ss' if row.e1_e2_SS else 'os'
            folder = sign + '/' + eleiso

            self.fill_histos(row, folder)
            folder = folder + '/' + str(int(jn))
            self.fill_histos(row, folder)

            if selections.lepton_id_iso(row, 'e3', 'eid13Loose_etauiso01'):
                eleiso = 'eTigh'
                folder = sign + '/' + eleiso
                self.fill_histos(row, folder)
                cut_flow_trk.Fill('tTightIso')
                folder = folder + '/' + str(int(jn))
                self.fill_histos(row, folder)

        cut_flow_trk.flush()
Esempio n. 4
0
    def process(self):
        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk   = cut_flow_tracker(cut_flow_histo)

        def preselection(row):
            if not row.e_m_SS: return False
            cut_flow_trk.Fill('e_m_SS')

            if row.e_m_DR < 0.5:                      return False
            cut_flow_trk.Fill('e_m_DR')

            if not selections.muSelection(row, 'm'):  return False #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag)
            cut_flow_trk.Fill('mu presel')

            if not selections.eSelection(row, 'e'):   return False #applies basic selection (eta, pt > 10, DZ, missingHits, jetBTag, HasConversion and chargedIdTight)
            if not row.eChargeIdTight:                return False
            cut_flow_trk.Fill('e presel')

            #if not (row.jetVeto40_DR05 >= 1):         return False
            if row.jetVeto20 == 0: return False
            cut_flow_trk.Fill('jet presence')

            if not selections.vetos(row, cut_flow_trk, False):  return False #applies mu bjet e additional tau vetoes
            #DO NOT APPLY BJet veto. It is used for ttbar CR
            return True
        #if self.is7TeV:
            #base_selection = 'mu17ele8Pass && ' + base_selection

        def fill(the_histos, row, fillNtuple=False):
            weight = 1
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU) *\
                         mcCorrectors.correct_mueg_mu(row.mPt, row.mAbsEta) * \
                         mcCorrectors.correct_mueg_e(row.ePt, row.eAbsEta)
            
            the_histos['ePt'].Fill(row.ePt)
            the_histos['eJetPt'].Fill(max(row.eJetPt, row.ePt))
            the_histos['eAbsEta'].Fill(row.eAbsEta)
            #the_histos['metSignificance'].Fill(row.metSignificance)
            the_histos['mMtToMET'].Fill(row.mMtToMET)
            the_histos['eJetptD'].Fill(row.mJetptD)
            the_histos['eJetmult'].Fill(row.mJetmult)
            the_histos['eJetmultvseJetPt'].Fill(max(row.eJetPt, row.ePt),row.eJetmult)
            the_histos['eJetptDvseJetPt'].Fill(max(row.eJetPt, row.ePt),row.eJetptD) 
            if fillNtuple:
                id_iso_vals = [ float( selections.lepton_id_iso(row, 'e', label) ) for label in self.lepIds]
                electron_jet_mass = -1. #inv_mass(row.ePt, row.eEta, row.ePhi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)
                
                the_histos['electronInfo'].Fill( array("f", [row.ePt, max(row.eJetPt, row.ePt), max(0, row.eJetCSVBtag), 
                                                             electron_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight]+id_iso_vals) )

                the_histos['evtInfo'].Fill(row)

        def fill_muon(the_histos, row, fillNtuple=False):
            weight = 1.
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU) *\
                         mcCorrectors.correct_mueg_mu(row.mPt, row.mAbsEta) * \
                         mcCorrectors.correct_mueg_e(row.ePt, row.eAbsEta)
                        
            the_histos['muonPt'].Fill(row.mPt, weight)
            the_histos['eMtToMET'].Fill(row.eMtToMET)
            the_histos['muonJetPt'].Fill(max(row.mJetPt, row.mPt), weight)
            the_histos['muonAbsEta'].Fill(row.mAbsEta, weight)
            the_histos['muonPtRatio'].Fill(row.mPt/max(row.mJetPt, row.mPt), weight)
            the_histos['muonPtDiff'].Fill(max(row.mJetPt, row.mPt) - row.mPt, weight)
            if fillNtuple:
                pfidiso02    = float( row.mPFIDTight and row.mRelPFIsoDB < 0.2)
                h2taucuts    = float( row.mPFIDTight and ((row.mRelPFIsoDB < 0.15 and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.1 ))
                h2taucuts020 = float( row.mPFIDTight and ((row.mRelPFIsoDB < 0.20 and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.15))
                muon_jet_mass = -1. #inv_mass(row.mPt, row.mEta, row.mPhi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)
                
                the_histos['muonInfo'].Fill( array("f", [row.mPt, max(row.mJetPt, row.mPt), max(0, row.mJetCSVBtag), 
                                                         muon_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight, 
                                                         pfidiso02, h2taucuts, h2taucuts020] ) )

                the_histos['evtInfo'].Fill(row)
                
        def fill_region(region,pt_cut):
            if region is None:
                return None
            fill(histos[(region, pt_cut)], row, True)

            for idlabel, idfcn in selections.electronIds.iteritems():                
                if not idfcn(row, 'e'): #if it does not pass id skip!
                    continue

                if row.eRelPFIsoDB < 0.2:
                    fill(histos[(region, pt_cut, idlabel+'_idiso02')], row)

                if (row.eRelPFIsoDB < 0.15 and row.eAbsEta < 1.479) or row.eRelPFIsoDB < 0.1:
                    fill(histos[(region, pt_cut, idlabel+'_h2taucuts')], row)

                if (row.eRelPFIsoDB < 0.20 and row.eAbsEta < 1.479) or row.eRelPFIsoDB < 0.15:
                    fill(histos[(region, pt_cut, idlabel+'_h2taucuts020')], row)

        def fill_muon_region(region, tag):
            if region is None:
                return None
            # This is a QCD or Wjets
            fill_muon(histos[(region, tag)], row, True)
            
            if row.mPFIDTight:
                if row.mRelPFIsoDB < 0.2:
                    fill_muon(histos[(region, tag, 'pfidiso02')], row)
                    
                if (row.mRelPFIsoDB < 0.15 and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.1:
                    fill_muon(histos[(region, tag, 'h2taucuts')], row)

                if (row.mRelPFIsoDB < 0.20 and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.15:
                    fill_muon(histos[(region, tag, 'h2taucuts020')], row)

                
        histos = self.histograms
        for row in self.tree:
            cut_flow_trk.new_row(row.run,row.lumi,row.evt)
            cut_flow_trk.Fill('bare')
            if not preselection(row):
                continue
            region = control_region(row)
            muon_region = control_region_muon(row)

            is7TeV = bool('7TeV' in os.environ['jobid'])
            use_iso_trigger = not is7TeV
            mu17e8 = (row.mu17ele8isoPass and row.mPt >= 20) if use_iso_trigger else (row.mu17ele8Pass and row.mPt >= 20)
            mu8e17 = (row.mu8ele17isoPass and row.ePt >= 20) if use_iso_trigger else (row.mu8ele17Pass and row.ePt >= 20)

            if env_lepton and env_lepton == 'e':
                if mu17e8:
                    cut_flow_trk.Fill('trigger')
                    if row.mPFIDTight and row.mRelPFIsoDB < 0.15:
                        cut_flow_trk.Fill('tag ID ISO')
                        if row.mMtToMET > 35:
                            cut_flow_trk.Fill('tag MT')
                            if row.eMtToMET < 35:
                                cut_flow_trk.Fill('probe MT')

            elif env_lepton and env_lepton == 'm':
                if mu8e17:
                    cut_flow_trk.Fill('trigger')
                    if selections.lepton_id_iso(row, 'e', 'eid12Medium_h2taucuts'):
                        cut_flow_trk.Fill('tag ID ISO')
                        if row.eMtToMET > 35:
                            cut_flow_trk.Fill('tag MT')
                            if row.mMtToMET < 35:
                                cut_flow_trk.Fill('probe MT')

            cut_flow_trk.flush()

            if mu17e8:
                fill_region(region,'pt10')
                if region == 'wjetsLtLow' and row.mMtToMET > 55:
                    fill_region('wjets', 'pt10')
            if mu8e17:
                fill_region(region,'pt20')
                if region == 'wjetsLtLow' and row.mMtToMET > 55:
                    fill_region('wjets', 'pt20')
                if muon_region:
                    fill_muon_region(muon_region, 'pt10')
                    if muon_region == 'MwjetsLtLow' and row.eMtToMET > 55:
                        fill_muon_region('Mwjets', 'pt10')
                    if row.mPt > 20:
                        fill_muon_region(muon_region, 'pt20')
                        if muon_region == 'MwjetsLtLow' and row.eMtToMET > 55:
                            fill_muon_region('Mwjets', 'pt20')
Esempio n. 5
0
    def process(self):
        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk   = cut_flow_tracker(cut_flow_histo)

        def preselection(row, cut_flow_trk):
            if not row.doubleEPass: return False
            if not (row.e1MatchesDoubleEPath > 0 and \
                row.e2MatchesDoubleEPath > 0): return False 
            cut_flow_trk.Fill('trigger')

            if row.e1_e2_DR < 0.5: return False
            cut_flow_trk.Fill('e1_e2_DR')

            if not row.e1Pt > 20: return False
            if not selections.eSelection(row, 'e1'): return False
            if not row.e1MVAIDH2TauWP: return False
            cut_flow_trk.Fill('e1 presel')

            if not selections.eSelection(row, 'e2'): return False
            cut_flow_trk.Fill('e2 presel')

            #if not (row.jetVeto40_DR05 >= 1):             return False
            if row.jetVeto20 == 0: return False
            cut_flow_trk.Fill('jet requirement')

            if not selections.vetos(row, cut_flow_trk): return False

            return True

        def fill(the_histos, row, fillNtuple=False):
            weight = 1
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU)

            the_histos['electronPt'].Fill(row.e2Pt)
            the_histos['electronJetPt'].Fill(max(row.e2JetPt, row.e2Pt))
            the_histos['electronAbsEta'].Fill(row.e2AbsEta)
            #the_histos['metSignificance'].Fill(row.metSignificance)
            the_histos['e1MtToMET'].Fill(row.e1MtToMET)
            the_histos['e2MtToMET'].Fill(row.e2MtToMET)
            the_histos['MET'].Fill(row.type1_pfMetEt)
            the_histos['OSS'].Fill(int(row.e1_e2_SS))

            the_histos['e2JetptD'].Fill(row.e2JetptD)
            the_histos['e2Jetmult'].Fill(row.e2Jetmult)

            the_histos['e2Jetmultvse2JetPt'].Fill(max(row.e2JetPt, row.e2Pt),row.e2Jetmult)
            the_histos['e2JetptDvse2JetPt'].Fill(max(row.e2JetPt, row.e2Pt),row.e2JetptD) 
                
            the_histos['e1e2Mass'].Fill(row.e1_e2_Mass)
            the_histos['doubleEPrescale'].Fill(row.doubleEPrescale)

            if fillNtuple:
                id_iso_vals = [  selections.lepton_id_iso(row, 'e2', label)  for label in self.lepIds]
                #print id_iso_vals
                #print self.lepIds
                id_iso_vals = [float( i ) for i in id_iso_vals ]
                electron_jet_mass = -1 #inv_mass(row.e2Pt, row.e2Eta, row.e2Phi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)

                the_histos['electronInfo'].Fill( array("f", [row.e2Pt, max(row.e2Pt, row.e2JetPt), max(0, row.e2JetCSVBtag),
                                                             electron_jet_mass, row.jetVeto20, row.jetVeto40_DR05, weight]+id_iso_vals) )
                the_histos['evtInfo'].Fill( row )


        histos = self.histograms
        #pprint( histos)
        for row in self.tree:
            cut_flow_trk.new_row(row.run,row.lumi,row.evt)
            cut_flow_trk.Fill('bare')

            if not preselection(row, cut_flow_trk):
                continue

            region = selections.control_region_ee(row)
            if region is None:
                continue

            if region == 'zee':
                continue

            if region_for_event_list and region == region_for_event_list:
                cut_flow_trk.Fill('region assignment')
                if zMassCut and not (60 < row.e1_e2_Mass < 120):
                    cut_flow_trk.Fill('zMassCut')

            # This is a QCD or Wjets
            if region_for_event_list and region == region_for_event_list\
               and (not zMassCut or not (60 < row.e1_e2_Mass < 120)) \
               and not SYNC:
                print '%i:%i:%i' % (row.run, row.lumi, row.evt)
                continue

            def make_region_plots(full_region):
                fill(histos[full_region], row, True)

                for idlabel, idfcn in selections.electronIds.iteritems():                
                    if not idfcn(row, 'e2'): #if it does not pass id skip!
                        continue
                    if row.e2RelPFIsoDB < 0.2:
                        fill(histos[full_region + ( idlabel+'_idiso02',)], row)
                    if (row.e2RelPFIsoDB < 0.15 and row.e2AbsEta < 1.479) or row.e2RelPFIsoDB < 0.1:
                        fill(histos[full_region + ( idlabel+'_h2taucuts',)], row)
                    if (row.e2RelPFIsoDB < 0.20 and row.e2AbsEta < 1.479) or row.e2RelPFIsoDB < 0.15:
                        fill(histos[full_region + ( idlabel+'_h2taucuts020',)], row)


            make_region_plots((region, 'pt10'))
            if not (row.e1_e2_Mass > 60 and row.e1_e2_Mass < 120):
                make_region_plots((region+'NoZmass', 'pt10'))
                if region == 'wjetsLtLow' and row.e1MtToMET > 55:
                    make_region_plots(('wjetsNoZmass', 'pt10'))
                    
            if row.e2Pt > 20:
                make_region_plots((region, 'pt20'))
                if not (row.e1_e2_Mass > 60 and row.e1_e2_Mass < 120):
                    make_region_plots((region+'NoZmass', 'pt20'))
                    if region == 'wjetsLtLow' and row.e1MtToMET > 55:
                        make_region_plots(('wjetsNoZmass', 'pt20'))
Esempio n. 6
0
    def process(self):
        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk = cut_flow_tracker(cut_flow_histo)

        def preselection(row):
            if not row.e_m_SS: return False
            cut_flow_trk.Fill('e_m_SS')

            if row.e_m_DR < 0.5: return False
            cut_flow_trk.Fill('e_m_DR')

            if not selections.muSelection(row, 'm'):
                return False  #applies basic selection (eta, pt > 10, DZ, pixHits, jetBTag)
            cut_flow_trk.Fill('mu presel')

            if not selections.eSelection(row, 'e'):
                return False  #applies basic selection (eta, pt > 10, DZ, missingHits, jetBTag, HasConversion and chargedIdTight)
            if not row.eChargeIdTight: return False
            cut_flow_trk.Fill('e presel')

            #if not (row.jetVeto40_DR05 >= 1):         return False
            if row.jetVeto20 == 0: return False
            cut_flow_trk.Fill('jet presence')

            if not selections.vetos(row, cut_flow_trk, False):
                return False  #applies mu bjet e additional tau vetoes
            #DO NOT APPLY BJet veto. It is used for ttbar CR
            return True

        #if self.is7TeV:
        #base_selection = 'mu17ele8Pass && ' + base_selection

        def fill(the_histos, row, fillNtuple=False):
            weight = 1
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU) *\
                         mcCorrectors.correct_mueg_mu(row.mPt, row.mAbsEta) * \
                         mcCorrectors.correct_mueg_e(row.ePt, row.eAbsEta)

            the_histos['ePt'].Fill(row.ePt)
            the_histos['eJetPt'].Fill(max(row.eJetPt, row.ePt))
            the_histos['eAbsEta'].Fill(row.eAbsEta)
            #the_histos['metSignificance'].Fill(row.metSignificance)
            the_histos['mMtToMET'].Fill(row.mMtToMET)
            the_histos['eJetptD'].Fill(row.mJetptD)
            the_histos['eJetmult'].Fill(row.mJetmult)
            the_histos['eJetmultvseJetPt'].Fill(max(row.eJetPt, row.ePt),
                                                row.eJetmult)
            the_histos['eJetptDvseJetPt'].Fill(max(row.eJetPt, row.ePt),
                                               row.eJetptD)
            if fillNtuple:
                id_iso_vals = [
                    float(selections.lepton_id_iso(row, 'e', label))
                    for label in self.lepIds
                ]
                electron_jet_mass = -1.  #inv_mass(row.ePt, row.eEta, row.ePhi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)

                the_histos['electronInfo'].Fill(
                    array("f", [
                        row.ePt,
                        max(row.eJetPt, row.ePt),
                        max(0, row.eJetCSVBtag), electron_jet_mass,
                        row.jetVeto20, row.jetVeto40_DR05, weight
                    ] + id_iso_vals))

                the_histos['evtInfo'].Fill(row)

        def fill_muon(the_histos, row, fillNtuple=False):
            weight = 1.
            if row.run == 1:
                weight = self.pucorrector(row.nTruePU) *\
                         mcCorrectors.correct_mueg_mu(row.mPt, row.mAbsEta) * \
                         mcCorrectors.correct_mueg_e(row.ePt, row.eAbsEta)

            the_histos['muonPt'].Fill(row.mPt, weight)
            the_histos['eMtToMET'].Fill(row.eMtToMET)
            the_histos['muonJetPt'].Fill(max(row.mJetPt, row.mPt), weight)
            the_histos['muonAbsEta'].Fill(row.mAbsEta, weight)
            the_histos['muonPtRatio'].Fill(row.mPt / max(row.mJetPt, row.mPt),
                                           weight)
            the_histos['muonPtDiff'].Fill(
                max(row.mJetPt, row.mPt) - row.mPt, weight)
            if fillNtuple:
                pfidiso02 = float(row.mPFIDTight and row.mRelPFIsoDB < 0.2)
                h2taucuts = float(
                    row.mPFIDTight
                    and ((row.mRelPFIsoDB < 0.15 and row.mAbsEta < 1.479)
                         or row.mRelPFIsoDB < 0.1))
                h2taucuts020 = float(
                    row.mPFIDTight
                    and ((row.mRelPFIsoDB < 0.20 and row.mAbsEta < 1.479)
                         or row.mRelPFIsoDB < 0.15))
                muon_jet_mass = -1.  #inv_mass(row.mPt, row.mEta, row.mPhi, row.leadingJetPt, row.leadingJetEta, row.leadingJetPhi)

                the_histos['muonInfo'].Fill(
                    array("f", [
                        row.mPt,
                        max(row.mJetPt, row.mPt),
                        max(0, row.mJetCSVBtag), muon_jet_mass, row.jetVeto20,
                        row.jetVeto40_DR05, weight, pfidiso02, h2taucuts,
                        h2taucuts020
                    ]))

                the_histos['evtInfo'].Fill(row)

        def fill_region(region, pt_cut):
            if region is None:
                return None
            fill(histos[(region, pt_cut)], row, True)

            for idlabel, idfcn in selections.electronIds.iteritems():
                if not idfcn(row, 'e'):  #if it does not pass id skip!
                    continue

                if row.eRelPFIsoDB < 0.2:
                    fill(histos[(region, pt_cut, idlabel + '_idiso02')], row)

                if (row.eRelPFIsoDB < 0.15
                        and row.eAbsEta < 1.479) or row.eRelPFIsoDB < 0.1:
                    fill(histos[(region, pt_cut, idlabel + '_h2taucuts')], row)

                if (row.eRelPFIsoDB < 0.20
                        and row.eAbsEta < 1.479) or row.eRelPFIsoDB < 0.15:
                    fill(histos[(region, pt_cut, idlabel + '_h2taucuts020')],
                         row)

        def fill_muon_region(region, tag):
            if region is None:
                return None
            # This is a QCD or Wjets
            fill_muon(histos[(region, tag)], row, True)

            if row.mPFIDTight:
                if row.mRelPFIsoDB < 0.2:
                    fill_muon(histos[(region, tag, 'pfidiso02')], row)

                if (row.mRelPFIsoDB < 0.15
                        and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.1:
                    fill_muon(histos[(region, tag, 'h2taucuts')], row)

                if (row.mRelPFIsoDB < 0.20
                        and row.mAbsEta < 1.479) or row.mRelPFIsoDB < 0.15:
                    fill_muon(histos[(region, tag, 'h2taucuts020')], row)

        histos = self.histograms
        for row in self.tree:
            cut_flow_trk.new_row(row.run, row.lumi, row.evt)
            cut_flow_trk.Fill('bare')
            if not preselection(row):
                continue
            region = control_region(row)
            muon_region = control_region_muon(row)

            is7TeV = bool('7TeV' in os.environ['jobid'])
            use_iso_trigger = not is7TeV
            mu17e8 = (row.mu17ele8isoPass
                      and row.mPt >= 20) if use_iso_trigger else (
                          row.mu17ele8Pass and row.mPt >= 20)
            mu8e17 = (row.mu8ele17isoPass
                      and row.ePt >= 20) if use_iso_trigger else (
                          row.mu8ele17Pass and row.ePt >= 20)

            if env_lepton and env_lepton == 'e':
                if mu17e8:
                    cut_flow_trk.Fill('trigger')
                    if row.mPFIDTight and row.mRelPFIsoDB < 0.15:
                        cut_flow_trk.Fill('tag ID ISO')
                        if row.mMtToMET > 35:
                            cut_flow_trk.Fill('tag MT')
                            if row.eMtToMET < 35:
                                cut_flow_trk.Fill('probe MT')

            elif env_lepton and env_lepton == 'm':
                if mu8e17:
                    cut_flow_trk.Fill('trigger')
                    if selections.lepton_id_iso(row, 'e',
                                                'eid12Medium_h2taucuts'):
                        cut_flow_trk.Fill('tag ID ISO')
                        if row.eMtToMET > 35:
                            cut_flow_trk.Fill('tag MT')
                            if row.mMtToMET < 35:
                                cut_flow_trk.Fill('probe MT')

            cut_flow_trk.flush()

            if mu17e8:
                fill_region(region, 'pt10')
                if region == 'wjetsLtLow' and row.mMtToMET > 55:
                    fill_region('wjets', 'pt10')
            if mu8e17:
                fill_region(region, 'pt20')
                if region == 'wjetsLtLow' and row.mMtToMET > 55:
                    fill_region('wjets', 'pt20')
                if muon_region:
                    fill_muon_region(muon_region, 'pt10')
                    if muon_region == 'MwjetsLtLow' and row.eMtToMET > 55:
                        fill_muon_region('Mwjets', 'pt10')
                    if row.mPt > 20:
                        fill_muon_region(muon_region, 'pt20')
                        if muon_region == 'MwjetsLtLow' and row.eMtToMET > 55:
                            fill_muon_region('Mwjets', 'pt20')
Esempio n. 7
0
    def process(self):
        # For speed, map the result of the region cuts to a folder path
        # string using a dictionary
        # key = (sign, obj1, obj2, obj3)
        cut_region_map = self.build_wh_folder_structure()
        region_for_event_list = os.environ.get('EVTLIST_REGION','')
        no_histo_fill  = ('NO_HISTO_FILL' in os.environ) and eval(os.environ['NO_HISTO_FILL']) 
        print_region   = ('PRINT_REGION' in os.environ) and eval(os.environ['PRINT_REGION']) 
        debug_mode     = ('DEBUG_MODE' in os.environ) and eval(os.environ['DEBUG_MODE'])

        # Reduce number of self lookups and get the derived functions here
        histos = self.histograms
        preselection = self.preselection
        sign_cut = self.sign_cut
        tau_sign_cut = self.tau_sign_cut
        obj1_id = self.obj1_id
        obj2_id = self.obj2_id
        obj3_id = self.obj3_id
        obj1_matches_gen = self.obj1_matches_gen
        obj2_matches_gen = self.obj2_matches_gen
        obj3_matches_gen = self.obj3_matches_gen
        fill_histos = self.fill_histos
        anti_wz_cut = self.anti_wz
        anti_charge_flip_cut = self.anti_charge_flip if hasattr(self,'anti_charge_flip') else None
        
        weight_func = self.event_weight

        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk   = cut_flow_tracker(cut_flow_histo)
        obj1_charge_flip = self.obj1_charge_flip if hasattr(self,'obj1_charge_flip') else None
        obj2_charge_flip = self.obj2_charge_flip if hasattr(self,'obj2_charge_flip') else None
        obj1_charge_flip_sysup = self.obj1_charge_flip if hasattr(self,'obj1_charge_flip_sysup') else None
        obj2_charge_flip_sysup = self.obj2_charge_flip if hasattr(self,'obj2_charge_flip_sysup') else None

        # Which weight folders correspond to which weight functions
        weight_map = {
            'w1' : (self.obj1_weight, ),
            'w2' : (self.obj2_weight, ),
            'w3' : (self.obj3_weight, ),
            'w12' : (self.obj1_weight, self.obj2_weight),
            'w13' : (self.obj1_weight, self.obj3_weight),
            'w23' : (self.obj2_weight, self.obj3_weight),

            'q1' : (self.obj1_qcd_weight, ),
            'q2' : (self.obj2_qcd_weight, ),
            'q3' : (self.obj3_qcd_weight, ),
            'q12' : (self.obj1_qcd_weight, self.obj2_qcd_weight),
            'q13' : (self.obj1_qcd_weight, self.obj3_qcd_weight),
            'q23' : (self.obj2_qcd_weight, self.obj3_qcd_weight),
        }

        grid_search = self.grid_search
        #print grid_search

        for i, row in enumerate(self.tree):
            ## if i > 100:
            ##     raise

            for cut_label, cut_settings in grid_search.iteritems():
                if 'WZJetsTo3LNu' in os.environ['megatarget']:
                    if 'ZToTauTau' in os.environ['megatarget']:
                        if not (row.isGtautau or row.isZtautau):
                            continue
                    else:
                        if (row.isGtautau or row.isZtautau):
                            continue
                        
                cut_flow_trk.new_row(row.run,row.lumi,row.evt)
                cut_flow_trk.Fill('bare')
                # Apply basic preselection
                #
                #if not cut_flow_trk.disabled:
                #    if row.processID != 26:
                #        continue

                #if row.lumi == 2348 and row.evt == 704038:
                #    print 'preselection', preselection(row, cut_flow_trk, cut_settings['LT'] if lt_tpt_in_presel else 0., cut_settings['tauPT'] if lt_tpt_in_presel else 0.)

                cut_flow_trk.Fill('WH Event')
                lt_tpt_in_presel = not bool(cut_settings['tauID'])
                if not preselection(row, cut_flow_trk, cut_settings['LT'] if lt_tpt_in_presel else 0., cut_settings['tauPT'] if lt_tpt_in_presel else 0.):
                    continue

                if os.environ['megatarget'].startswith('Zjets') and not obj3_matches_gen(row):
                    continue
                
                # Get the generic event weight
                event_weight = weight_func(row)

                # Get the cuts that define the region
                sign_result = sign_cut(row)
                tau_sign_result = tau_sign_cut(row)
                obj1_id_result = obj1_id(row, cut_settings['leading_iso'], cut_settings['subleading_iso'])
                obj2_id_result = obj2_id(row, cut_settings['leading_iso'], cut_settings['subleading_iso'])
                obj3_id_result = obj3_id(row, cut_settings['tauID'], cut_settings['LT'], cut_settings['tauPT'])
                anti_wz = anti_wz_cut(row)
                anti_charge_flip = anti_charge_flip_cut(row, cut_settings['charge_fakes']) if anti_charge_flip_cut else True
                #if there is no anti charge flip always fill the base selestion otherwise fill the base ONLY if passes,
                #otherwise fill the CR
                to_fill = ('',) \
                    if not anti_charge_flip_cut else \
                    ('','charge_flip_CR/') if anti_charge_flip else ('charge_flip_CR/',)
                
                if sign_result:                      
                    cut_flow_trk.Fill('sign cut')    
                    if tau_sign_result:              
                        cut_flow_trk.Fill('tau sign')
                        if anti_wz :
                            cut_flow_trk.Fill('anti WZ')
                            if obj1_id_result:
                                cut_flow_trk.Fill('obj1 IDIso')
                                if obj2_id_result:
                                    cut_flow_trk.Fill('obj2 IDIso')
                                    if obj3_id_result:
                                        cut_flow_trk.Fill('obj3 IDIso')
                                        if anti_charge_flip:
                                            cut_flow_trk.Fill('anti charge flip')
                                        

                if os.environ['megatarget'].startswith('Zjets'):
                    #remove matched but not id/Iso leptons
                    if obj1_matches_gen(row) and not obj1_id_result:
                        continue
                    if obj2_matches_gen(row) and not obj2_id_result:
                        continue

                # Figure out which folder/region we are in
                region_result = cut_region_map.get(
                    (sign_result, tau_sign_result, obj1_id_result, obj2_id_result, obj3_id_result))

                if debug_mode:
                    set_trace() 

                # Ignore stupid regions we don't care about
                if region_result is None:
                    continue

                if anti_wz:
                    base_folder, weights = region_result

                    base_folder = joinDirs(*base_folder)
                    if print_region:
                        print 'event %i:%i:%i assigned to region(s) %s' % \
                            (row.run, row.lumi, row.evt, \
                             ', '.join( joinDirs(base_folder,i) for i in to_fill) )
                    if region_for_event_list:
                        if base_folder == region_for_event_list and \
                           '' in to_fill:
                            print '%i:%i:%i' % (row.run, row.lumi, row.evt)
                        else:
                            continue
                    if no_histo_fill: # and region_for_event_list and \
                       #base_folder == region_for_event_list:
                        continue

                    # Fill the un-fr-weighted histograms
                    for i in to_fill:
                        folder = joinDirs(base_folder,i)
                        fill_histos(histos, folder, row, event_weight, cut_label)

                    # Now loop over all necessary weighted regions and compute & fill
                    for weight_folder in weights:
                        # Compute product of all weights
                        fr_weight = event_weight
                        # Figure out which object weight functions to apply
                        for subweight in weight_map[weight_folder]:
                            fr = subweight(row, cut_settings['leading_iso'], cut_settings['subleading_iso'])
                            fr_weight *= fr/(1.-fr)

                        # Now fill the histos for this weight folder
                        for i in to_fill:
                            folder = joinDirs(base_folder,weight_folder,i)
                            fill_histos(histos, folder, row, fr_weight, cut_label)

                    if not sign_result and obj1_id_result and obj2_id_result:
                        # Object 1 can only flip if it is OS with the tau
                        obj1_obj3_SS     = self.obj1_obj3_SS(row)
                        for flipfunc, flipfunc_sysup, dirname in [(obj1_charge_flip, obj1_charge_flip_sysup, 'c1'), (obj2_charge_flip, obj2_charge_flip_sysup, 'c2')]:
                            if flipfunc:
                                charge_flip_prob = flipfunc(row, cut_settings['leading_iso'], cut_settings['subleading_iso'])
                                charge_flip_sysu = flipfunc_sysup(row, cut_settings['leading_iso'], cut_settings['subleading_iso'])
                                directory        = joinDirs(base_folder, dirname)
                                directory_up     = joinDirs(base_folder, '%s_sysup' % dirname)
                                charge_flip_prob = charge_flip_prob/(1. - charge_flip_prob)
                                for i in to_fill:
                                    fill_histos(histos, joinDirs(directory,i), row, event_weight*charge_flip_prob, cut_label)
                                    fill_histos(histos, joinDirs(directory_up,i), row, event_weight*charge_flip_sysu, cut_label)

                elif sign_result and obj1_id_result and obj3_id_result and tau_sign_result:
                    if no_histo_fill or region_for_event_list:
                        continue

                    # WZ object topology fails. Check if we are in signal region.
                    if self.enhance_wz(row):
                        # Signal region
                        if obj2_id_result:
                            fill_histos(histos, 'ss/tau_os/p1p2p3_enhance_wz/', row, event_weight, cut_label)
                        else:
                            fill_histos(histos, 'ss/tau_os/p1f2p3_enhance_wz/', row, event_weight, cut_label)
                            fake_rate_obj2 = self.obj2_weight(row, cut_settings['leading_iso'], cut_settings['subleading_iso'])
                            fake_weight = fake_rate_obj2/(1.-fake_rate_obj2)
                            fill_histos(histos, 'ss/tau_os/p1f2p3_enhance_wz/w2/', row, event_weight*fake_weight, cut_label)
        cut_flow_trk.flush()
    def process(self):
        
        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk   = cut_flow_tracker(cut_flow_histo)
        myevent =()
        #print self.tree.inputfilename
        for row in self.tree:
            jn = row.jetVeto30
            if jn > 3 : jn = 3
            
            #if row.run > 2: 
            if not bool(row.singleE27WP80Pass) : continue
            #            if hasattr(self.tree, 'row.e1MatchesEle27WP80') and hasattr(self.tree, 'row.e2MatchesEle27WP80') :
            #if not bool(row.e1MatchesEle27WP80) and not bool(row.e2MatchesEle27WP80) : continue
            
            #else :
            if not bool(row.e1MatchesSingleE27WP80) and  not bool(row.e2MatchesSingleE27WP80) : continue
                #if not bool(row.singleEPass) : continue
                #if not bool(row.e1MatchesSingleE) and  not bool(row.e2MatchesSingleE) : continue
                
            if row.bjetCSVVeto30!=0 : continue 
            if row.e1Pt < 30 : continue
            if row.e2Pt < 30 : continue
                       
#        for i, row in enumerate(self.tree):
#            if  i >= 100:
#                return
           # print bool(cut_flow_trk.disabled)
            cut_flow_trk.new_row(row.run,row.lumi,row.evt)
            #print row.run,row.lumi,row.evt
            cut_flow_trk.Fill('allEvents')
            if not selections.eSelection(row, 'e1'): continue
            cut_flow_trk.Fill('e1sel')
            if not selections.lepton_id_iso(row, 'e1', 'eid13Tight_etauiso01'): continue
            if abs(row.e1Eta) > 1.4442 and abs(row.e1Eta < 1.566) : continue
            
            
            cut_flow_trk.Fill('e1IDiso')
            if not selections.eSelection(row, 'e2'): continue
            cut_flow_trk.Fill('e2sel')
            if not selections.lepton_id_iso(row, 'e2', 'eid13Tight_etauiso01'): continue
            if abs(row.e2Eta) > 1.4442 and abs(row.e2Eta) < 1.566 : continue
            
            cut_flow_trk.Fill('e2IDiso')
            if not abs(row.e1_e2_Mass-91.2) < 20: continue
            cut_flow_trk.Fill('ZMass')
            if not selections.tauSelection(row, 't'): continue
            if row.tPt < 30 : continue
            cut_flow_trk.Fill('tsel')

            if not row.tAntiMuon2Loose: continue
            cut_flow_trk.Fill('tAntiMuon')
            if not row.tAntiElectronMVA5Tight: continue #was 3
            cut_flow_trk.Fill('tAntiEle')

            if row.tauVetoPt20EleTight3MuLoose : continue 
            #if row.tauHpsVetoPt20 : continue
            if row.muVetoPt5IsoIdVtx : continue
            if row.eVetoCicLooseIso : continue # change it with Loose

            
            # if not row.tMtToMET < 50:  continue
            cut_flow_trk.Fill('MtToMet')
            
            #            if  etDR(row) < 1. : continue 
            if (row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)==myevent: continue
            myevent=(row.run, row.lumi, row.evt, row.e1Pt, row.e2Pt)

            tauiso = 'tNoCuts'
            sign = 'ss' if row.e1_e2_SS else 'os'
            folder = sign+'/'+tauiso
          
            self.fill_histos(row, folder)
            folder=folder+'/'+str(int(jn))
            self.fill_histos(row, folder)
            
            
            if  row.tPt < 90 and row.tPt>65 : 
                folder = folder+'/tptregion'
                self.fill_histos(row, folder)
                folder = sign+'/'+tauiso+'/tptregion'
                self.fill_histos(row, folder)
                
            if not row.tRawIso3Hits < 10 : continue
            cut_flow_trk.Fill('tRawIso10')
            tauiso = 'tSuperSuperLoose'
            folder = sign+'/'+tauiso
            self.fill_histos(row, folder)
            folder=folder+'/'+str(int(jn))
            self.fill_histos(row, folder)
            if  row.tPt < 90 and row.tPt>65 : 
                folder = folder+'/tptregion'
                self.fill_histos(row, folder)
                folder = sign+'/'+tauiso+'/tptregion'
                self.fill_histos(row, folder)
                
            if not row.tRawIso3Hits < 5 : continue
            cut_flow_trk.Fill('tRawIso5')
            tauiso = 'tSuperLoose'
            folder = sign+'/'+tauiso
            self.fill_histos(row, folder)
            folder=folder+'/'+str(int(jn))
            self.fill_histos(row, folder)
            if  row.tPt < 90 and row.tPt>65 : 
                folder = folder+'/tptregion'
                self.fill_histos(row, folder)
                folder = sign+'/'+tauiso+'/tptregion'
                self.fill_histos(row, folder)

                        
            if  row.tLooseIso3Hits : 
                tauiso = 'tLoose'
                folder = sign+'/'+tauiso
                self.fill_histos(row,  folder)
                cut_flow_trk.Fill('tLooseIso')
                folder=folder+'/'+str(int(jn))
                self.fill_histos(row, folder)
                if  row.tPt < 90 and row.tPt>65 : 
                    folder = folder+'/tptregion'
                    self.fill_histos(row, folder)
                    folder = sign+'/'+tauiso+'/tptregion'
                    self.fill_histos(row, folder)

            if row.tTightIso3Hits :
                tauiso = 'tTigh' 
                folder = sign+'/'+tauiso
                self.fill_histos(row,  folder)
                cut_flow_trk.Fill('tTightIso')
                folder=folder+'/'+str(int(jn))
                self.fill_histos(row, folder)
                if  row.tPt < 90 and row.tPt>65 : 
                    folder = folder+'/tptregion'
                    self.fill_histos(row, folder)
                    folder = sign+'/'+tauiso+'/tptregion'
                    self.fill_histos(row, folder)
                    
 
             
        cut_flow_trk.flush()
Esempio n. 9
0
    def process(self):
        # For speed, map the result of the region cuts to a folder path
        # string using a dictionary
        # key = (sign, obj1, obj2, obj3)
        cut_region_map = self.build_wh_folder_structure()
        region_for_event_list = os.environ.get('EVTLIST_REGION', '')
        no_histo_fill = ('NO_HISTO_FILL' in os.environ) and eval(
            os.environ['NO_HISTO_FILL'])
        print_region = ('PRINT_REGION' in os.environ) and eval(
            os.environ['PRINT_REGION'])
        debug_mode = ('DEBUG_MODE' in os.environ) and eval(
            os.environ['DEBUG_MODE'])

        # Reduce number of self lookups and get the derived functions here
        histos = self.histograms
        preselection = self.preselection
        sign_cut = self.sign_cut
        tau_sign_cut = self.tau_sign_cut
        obj1_id = self.obj1_id
        obj2_id = self.obj2_id
        obj3_id = self.obj3_id
        obj1_matches_gen = self.obj1_matches_gen
        obj2_matches_gen = self.obj2_matches_gen
        obj3_matches_gen = self.obj3_matches_gen
        fill_histos = self.fill_histos
        anti_wz_cut = self.anti_wz
        anti_charge_flip_cut = self.anti_charge_flip if hasattr(
            self, 'anti_charge_flip') else None

        weight_func = self.event_weight

        cut_flow_histo = self.cut_flow_histo
        cut_flow_trk = cut_flow_tracker(cut_flow_histo)
        obj1_charge_flip = self.obj1_charge_flip if hasattr(
            self, 'obj1_charge_flip') else None
        obj2_charge_flip = self.obj2_charge_flip if hasattr(
            self, 'obj2_charge_flip') else None
        obj1_charge_flip_sysup = self.obj1_charge_flip if hasattr(
            self, 'obj1_charge_flip_sysup') else None
        obj2_charge_flip_sysup = self.obj2_charge_flip if hasattr(
            self, 'obj2_charge_flip_sysup') else None

        # Which weight folders correspond to which weight functions
        weight_map = {
            'w1': (self.obj1_weight, ),
            'w2': (self.obj2_weight, ),
            'w3': (self.obj3_weight, ),
            'w12': (self.obj1_weight, self.obj2_weight),
            'w13': (self.obj1_weight, self.obj3_weight),
            'w23': (self.obj2_weight, self.obj3_weight),
            'q1': (self.obj1_qcd_weight, ),
            'q2': (self.obj2_qcd_weight, ),
            'q3': (self.obj3_qcd_weight, ),
            'q12': (self.obj1_qcd_weight, self.obj2_qcd_weight),
            'q13': (self.obj1_qcd_weight, self.obj3_qcd_weight),
            'q23': (self.obj2_qcd_weight, self.obj3_qcd_weight),
        }

        grid_search = self.grid_search
        #print grid_search

        for i, row in enumerate(self.tree):
            ## if i > 100:
            ##     raise

            for cut_label, cut_settings in grid_search.iteritems():
                if 'WZJetsTo3LNu' in os.environ['megatarget']:
                    if 'ZToTauTau' in os.environ['megatarget']:
                        if not (row.isGtautau or row.isZtautau):
                            continue
                    else:
                        if (row.isGtautau or row.isZtautau):
                            continue

                cut_flow_trk.new_row(row.run, row.lumi, row.evt)
                cut_flow_trk.Fill('bare')
                # Apply basic preselection
                #
                #if not cut_flow_trk.disabled:
                #    if row.processID != 26:
                #        continue

                #if row.lumi == 2348 and row.evt == 704038:
                #    print 'preselection', preselection(row, cut_flow_trk, cut_settings['LT'] if lt_tpt_in_presel else 0., cut_settings['tauPT'] if lt_tpt_in_presel else 0.)

                cut_flow_trk.Fill('WH Event')
                lt_tpt_in_presel = not bool(cut_settings['tauID'])
                if not preselection(
                        row, cut_flow_trk,
                        cut_settings['LT'] if lt_tpt_in_presel else 0.,
                        cut_settings['tauPT'] if lt_tpt_in_presel else 0.):
                    continue

                if os.environ['megatarget'].startswith(
                        'Zjets') and not obj3_matches_gen(row):
                    continue

                # Get the generic event weight
                event_weight = weight_func(row)

                # Get the cuts that define the region
                sign_result = sign_cut(row)
                tau_sign_result = tau_sign_cut(row)
                obj1_id_result = obj1_id(row, cut_settings['leading_iso'],
                                         cut_settings['subleading_iso'])
                obj2_id_result = obj2_id(row, cut_settings['leading_iso'],
                                         cut_settings['subleading_iso'])
                obj3_id_result = obj3_id(row, cut_settings['tauID'],
                                         cut_settings['LT'],
                                         cut_settings['tauPT'])
                anti_wz = anti_wz_cut(row)
                anti_charge_flip = anti_charge_flip_cut(
                    row, cut_settings['charge_fakes']
                ) if anti_charge_flip_cut else True
                #if there is no anti charge flip always fill the base selestion otherwise fill the base ONLY if passes,
                #otherwise fill the CR
                to_fill = ('',) \
                    if not anti_charge_flip_cut else \
                    ('','charge_flip_CR/') if anti_charge_flip else ('charge_flip_CR/',)

                if sign_result:
                    cut_flow_trk.Fill('sign cut')
                    if tau_sign_result:
                        cut_flow_trk.Fill('tau sign')
                        if anti_wz:
                            cut_flow_trk.Fill('anti WZ')
                            if obj1_id_result:
                                cut_flow_trk.Fill('obj1 IDIso')
                                if obj2_id_result:
                                    cut_flow_trk.Fill('obj2 IDIso')
                                    if obj3_id_result:
                                        cut_flow_trk.Fill('obj3 IDIso')
                                        if anti_charge_flip:
                                            cut_flow_trk.Fill(
                                                'anti charge flip')

                if os.environ['megatarget'].startswith('Zjets'):
                    #remove matched but not id/Iso leptons
                    if obj1_matches_gen(row) and not obj1_id_result:
                        continue
                    if obj2_matches_gen(row) and not obj2_id_result:
                        continue

                # Figure out which folder/region we are in
                region_result = cut_region_map.get(
                    (sign_result, tau_sign_result, obj1_id_result,
                     obj2_id_result, obj3_id_result))

                if debug_mode:
                    set_trace()

                # Ignore stupid regions we don't care about
                if region_result is None:
                    continue

                if anti_wz:
                    base_folder, weights = region_result

                    base_folder = joinDirs(*base_folder)
                    if print_region:
                        print 'event %i:%i:%i assigned to region(s) %s' % \
                            (row.run, row.lumi, row.evt, \
                             ', '.join( joinDirs(base_folder,i) for i in to_fill) )
                    if region_for_event_list:
                        if base_folder == region_for_event_list and \
                           '' in to_fill:
                            print '%i:%i:%i' % (row.run, row.lumi, row.evt)
                        else:
                            continue
                    if no_histo_fill:  # and region_for_event_list and \
                        #base_folder == region_for_event_list:
                        continue

                    # Fill the un-fr-weighted histograms
                    for i in to_fill:
                        folder = joinDirs(base_folder, i)
                        fill_histos(histos, folder, row, event_weight,
                                    cut_label)

                    # Now loop over all necessary weighted regions and compute & fill
                    for weight_folder in weights:
                        # Compute product of all weights
                        fr_weight = event_weight
                        # Figure out which object weight functions to apply
                        for subweight in weight_map[weight_folder]:
                            fr = subweight(row, cut_settings['leading_iso'],
                                           cut_settings['subleading_iso'])
                            fr_weight *= fr / (1. - fr)

                        # Now fill the histos for this weight folder
                        for i in to_fill:
                            folder = joinDirs(base_folder, weight_folder, i)
                            fill_histos(histos, folder, row, fr_weight,
                                        cut_label)

                    if not sign_result and obj1_id_result and obj2_id_result:
                        # Object 1 can only flip if it is OS with the tau
                        obj1_obj3_SS = self.obj1_obj3_SS(row)
                        for flipfunc, flipfunc_sysup, dirname in [
                            (obj1_charge_flip, obj1_charge_flip_sysup, 'c1'),
                            (obj2_charge_flip, obj2_charge_flip_sysup, 'c2')
                        ]:
                            if flipfunc:
                                charge_flip_prob = flipfunc(
                                    row, cut_settings['leading_iso'],
                                    cut_settings['subleading_iso'])
                                charge_flip_sysu = flipfunc_sysup(
                                    row, cut_settings['leading_iso'],
                                    cut_settings['subleading_iso'])
                                directory = joinDirs(base_folder, dirname)
                                directory_up = joinDirs(
                                    base_folder, '%s_sysup' % dirname)
                                charge_flip_prob = charge_flip_prob / (
                                    1. - charge_flip_prob)
                                for i in to_fill:
                                    fill_histos(
                                        histos, joinDirs(directory, i), row,
                                        event_weight * charge_flip_prob,
                                        cut_label)
                                    fill_histos(
                                        histos, joinDirs(directory_up, i), row,
                                        event_weight * charge_flip_sysu,
                                        cut_label)

                elif sign_result and obj1_id_result and obj3_id_result and tau_sign_result:
                    if no_histo_fill or region_for_event_list:
                        continue

                    # WZ object topology fails. Check if we are in signal region.
                    if self.enhance_wz(row):
                        # Signal region
                        if obj2_id_result:
                            fill_histos(histos, 'ss/tau_os/p1p2p3_enhance_wz/',
                                        row, event_weight, cut_label)
                        else:
                            fill_histos(histos, 'ss/tau_os/p1f2p3_enhance_wz/',
                                        row, event_weight, cut_label)
                            fake_rate_obj2 = self.obj2_weight(
                                row, cut_settings['leading_iso'],
                                cut_settings['subleading_iso'])
                            fake_weight = fake_rate_obj2 / (1. -
                                                            fake_rate_obj2)
                            fill_histos(histos,
                                        'ss/tau_os/p1f2p3_enhance_wz/w2/', row,
                                        event_weight * fake_weight, cut_label)
        cut_flow_trk.flush()