def getTTGJetsEventType(r):
    type = 0
    genparts = getGenPartsAll(r)
    for g in genparts:  # Type 0: no photon
        if g["pdgId"] != 22:
            continue  # pdgId == 22 for photons
        type = max(type, 1)  # Type 1: photon found
        if g["pt"] < 10:
            continue  # pt > 10 GeV
        if abs(g["eta"]) > 2.6:
            continue  # |eta| < 2.6
        type = max(type, 2)  # Type 2: photon within generator cuts
        #   if not isIsolatedPhoton(g, genparts, 0.05): continue					# According to MG the photon isolation is 0.05, probably too small to really apply this cut
        # (TTGJets has a few events where other stuff is found within 0.05 of the photon)

        parents = getParentIds(genparts, g)  # Get complete parentsList
        parents = filter(lambda x: abs(x) != 2212, parents)  # Remove the protons from the list

        if len(parents) == 0:
            type = max(
                type, 3
            )  # Rare, not sure what's happening here, apparently sometimes the photon has no parents or the proton as parent
        elif max(parents) < 37 and min(parents) > -37:
            type = max(
                type, 4
            )  # Only quarks, leptons, gluons and bosons allowed in the parent list (i.e. we avoid photons from pions) [those are the ones which should be removed]
    return type
Exemple #2
0
def makeTTJetsQT( data ):
    
    gPart = getGenPartsAll( data )
    genW  = filter( lambda p:abs(p['pdgId'])==24 and abs(p['motherId'])==6, gPart)
    if not len(genW)==2:
        print "Warning, found %i W from t"%len(genW)
    qx = sum([p['pt']*cos(p['phi']) for p in genW ],0.) 
    qy = sum([p['pt']*sin(p['phi']) for p in genW ],0.)
    qt = sqrt( qx**2 + qy**2 )
    data.ttjets_qt = qt 
def getTTGJetsEventType(r):
  type = 0
  genparts = getGenPartsAll(r)
  for g in genparts:										# Type 0: no photon
    if g['pdgId'] != 22:                        continue					# pdgId == 22 for photons
    type = max(type, 1)										# Type 1: photon found
    if g['pt'] < 10:                            continue					# pt > 10 GeV
    if abs(g['eta']) > 2.6:                     continue					# |eta| < 2.6
    type = max(type, 2)										# Type 2: photon within generator cuts
#   if not isIsolatedPhoton(g, genparts, 0.05): continue					# According to MG the photon isolation is 0.05, probably too small to really apply this cut
												# (TTGJets has a few events where other stuff is found within 0.05 of the photon)

    parents = getParentIds(genparts, g)								# Get complete parentsList
    parents = filter(lambda x: abs(x) != 2212, parents)						# Remove the protons from the list

    if len(parents) == 0:                          type = max(type, 3)				# Rare, not sure what's happening here, apparently sometimes the photon has no parents or the proton as parent
    elif max(parents) < 37 and min(parents) > -37: type = max(type, 4)				# Only quarks, leptons, gluons and bosons allowed in the parent list (i.e. we avoid photons from pions) [those are the ones which should be removed]
  return type
Exemple #4
0
        looseMu = filter(
            lambda l: abs(l['pdgId']) == 13 and l['miniRelIso'] < 0.4 and l[
                'pt'] > 15, allLeptons)
        looseEle = filter(
            lambda l: abs(l['pdgId']) == 11 and l['miniRelIso'] < 0.4 and l[
                'pt'] > 15, allLeptons)
        mu = filter(lambda l: abs(l['pdgId']) == 13, leptons)
        ele = filter(lambda l: abs(l['pdgId']) == 11, leptons)
        tau = getGoodTaus(chain)

        #RECO mathes
        muMatched = filter(lambda l: abs(l['mcMatchAny']) == 1, mu)
        eleMatched = filter(lambda l: abs(l['mcMatchAny']) == 1, ele)
        tauMatched = filter(lambda l: abs(l['mcMatchId']) >= 1, tau)
        #GEN
        genParts = getGenPartsAll(chain)
        status1MuEle = filter(
            lambda p: abs(p['pdgId']) in [11, 13] and p['status'] == 1 and p[
                'pt'] > 10, genParts)
        genLeptons = [
            descendDecay(q, genParts) for q in filter(
                lambda p: abs(p['motherId']) in [24] and abs(p['pdgId']) in
                lepPdgs, genParts)
        ]
        genLeptonsFromTau = [
            descendDecay(q, genParts) for q in filter(
                lambda p: abs(p['motherId']) in [15] and abs(p['pdgId']) in
                lepPdgs, genParts)
        ]
        genNeutrinosFromW = [
            descendDecay(q, genParts) for q in filter(
def filler(s):
    # shortcut
    r = reader.data
    s.reweightPU = puRW(r.nTrueInt)
    s.weight = lumiScaleFactor*r.genWeight if lumiScaleFactor is not None else 1

    s.met_pt  = r.met_genPt
    s.met_phi = r.met_genPhi

    genParts = getGenPartsAll(r)

    s.leptonicDecays = 0
    for g in genParts:
      if abs(g['pdgId']) == 6 and g['nDaughters'] == 2:
        try:
	  # Look for top decay to W boson and (b-)jet
	  if abs(genParts[g['daughterIndex1']]['pdgId']) != 24 and abs(genParts[g['daughterIndex2']]['pdgId']) != 24: continue
	  if abs(genParts[g['daughterIndex1']]['pdgId']) == 24:
	    wBoson = genParts[g['daughterIndex1']]
	    bJet   = genParts[g['daughterIndex2']]
	  elif abs(genParts[g['daughterIndex2']]['pdgId']) == 24:
	    wBoson = genParts[g['daughterIndex2']]
	    bJet   = genParts[g['daughterIndex1']]
	  else:
	    raise Exception('Logic is wrong')

	  # Go down through the W-boson radiations until we find the W decay
	  while abs(genParts[wBoson['daughterIndex1']]['pdgId']) == 24 or abs(genParts[wBoson['daughterIndex2']]['pdgId']) == 24:
	    if   abs(genParts[wBoson['daughterIndex1']]['pdgId']) == 24: wBoson = genParts[wBoson['daughterIndex1']] 
	    elif abs(genParts[wBoson['daughterIndex2']]['pdgId']) == 24: wBoson = genParts[wBoson['daughterIndex2']] 
	    else:
	      raise Exception('Logic is wrong')

	  if g['pdgId'] == 6:
	    s.t1_pt         = g['pt']
	    s.t1_eta        = g['eta']
	    s.t1_phi        = g['phi']
	    s.t1_pdgId      = g['pdgId']
	    s.bJet1_pt      = bJet['pt']
	    s.bJet1_eta     = bJet['eta']
	    s.bJet1_phi     = bJet['phi']
	    s.bJet1_pdgId   = bJet['pdgId']
	  elif g['pdgId'] == -6:
	    s.t2_pt         = g['pt']
	    s.t2_eta        = g['eta']
	    s.t2_phi        = g['phi']
	    s.t2_pdgId      = g['pdgId']
	    s.bJet2_pt      = bJet['pt']
	    s.bJet2_eta     = bJet['eta']
	    s.bJet2_phi     = bJet['phi']
	    s.bJet2_pdgId   = bJet['pdgId']
	  else:
	    raise Exception('Logic is wrong')

	  # Check for leptonic decay
	  if abs(genParts[wBoson['daughterIndex1']]['pdgId']) in (11,12,13,14,15,16):
	    s.leptonicDecays += 1
	    if abs(genParts[wBoson['daughterIndex1']]['pdgId']) in (11,13,15):
	      l  = genParts[wBoson['daughterIndex1']]
	      nu = genParts[wBoson['daughterIndex2']]
	    elif abs(genParts[wBoson['daughterIndex2']]['pdgId']) in (11,13,15):
	      l  = genParts[wBoson['daughterIndex2']]
	      nu = genParts[wBoson['daughterIndex1']]
	    else:
	      raise Exception('Logic is wrong')

	    if g['pdgId'] == 6:
	      s.l1_pt     = l['pt']
	      s.l1_eta    = l['eta']
	      s.l1_phi    = l['phi']
	      s.l1_pdgId  = l['pdgId']
	      s.nu1_pt    = nu['pt']
	      s.nu1_eta   = nu['eta']
	      s.nu1_phi   = nu['phi']
	      s.nu1_pdgId = nu['pdgId']
              s.mt1       = sqrt(2*s.l1_pt*s.nu1_pt*(1-cos(s.l1_phi-s.nu1_phi)))
	    elif g['pdgId'] == -6:
	      s.l2_pt     = l['pt']
	      s.l2_eta    = l['eta']
	      s.l2_phi    = l['phi']
	      s.l2_pdgId  = l['pdgId']
	      s.nu2_pt    = nu['pt']
	      s.nu2_eta   = nu['eta']
	      s.nu2_phi   = nu['phi']
	      s.nu2_pdgId = nu['pdgId']
              s.mt2       = sqrt(2*s.l2_pt*s.nu2_pt*(1-cos(s.l2_phi-s.nu2_phi)))
	    else:
	      raise Exception('Logic is wrong')

        except Exception, e:
           print str(e)
Exemple #6
0
 def makeGenNeutrinos( data ):
     setattr( data, "genNu", filter( lambda p:abs(p['pdgId']) in [12, 14, 16] and p['status']==1, getGenPartsAll( data ) ) )
Exemple #7
0
 def makeGenNeutrinos(data):
     setattr(
         data, "genNu",
         filter(
             lambda p: abs(p['pdgId']) in [12, 14, 16] and p['status']
             == 1, getGenPartsAll(data)))
#LepGood_mcMatchAny  Match to any final state leptons: 0 if unmatched, 1 if light flavour (including prompt), 4 if charm, 5 if bottom for Leptons after the preselection
#LepGood_mcMatchTau True if the leptons comes from a tau for Leptons after the preselection

#    for l in leptons:
#      if (l['mcMatchAny']==0 and (not (l['mcMatchId']==0))) or ( (not l['mcMatchAny']==0) and (l['mcMatchId']==0)):
#        print "Match?",l

#RECO
    mu      = filter(lambda l: abs(l['pdgId'])==13, leptons)
    ele     = filter(lambda l: abs(l['pdgId'])==11, leptons)

#RECO mathes
    muMatched   = filter(lambda l: abs(l['mcMatchAny'])==1, mu)
    eleMatched  = filter(lambda l: abs(l['mcMatchAny'])==1, ele)
#GEN    
    genParts = getGenPartsAll(chain)
    genLeptons          =   filter(lambda p: abs(p['motherId']) in [24] and abs(p['pdgId']) in lepPdgs, genParts)
    genLeptonsFromTau   =   filter(lambda p: abs(p['motherId']) in [15] and abs(p['pdgId']) in lepPdgs, genParts)
    genNeutrinos        =   filter(lambda p: abs(p['motherId']) ==24 and abs(p['pdgId']) in muPdgs, genParts)
    genNeutrinosFromTau =   filter(lambda p: abs(p['motherId']) ==15 and abs(p['pdgId']) in muPdgs, genParts)
    genEle =       filter(lambda p: abs(p['motherId']) in [24] and abs(p['pdgId'])==11 , genParts)
    genMu=         filter(lambda p: abs(p['motherId']) in [24] and abs(p['pdgId'])==13 , genParts)
    genEleFromTau= filter(lambda p: abs(p['motherId']) in [15] and abs(p['pdgId'])==11 , genParts)
    genMuFromTau=  filter(lambda p: abs(p['motherId']) in [15] and abs(p['pdgId'])==13 , genParts)
    genTau=        filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 , genParts)
    genTauToE=     filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 and abs(descendTauDecay(p, genParts))==11, genParts)
    genTauToMu=    filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 and abs(descendTauDecay(p, genParts))==13, genParts)
    genTauToHad=   filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 and abs(descendTauDecay(p, genParts))==0, genParts)
    genNuE =         filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==12 , genParts)
    genNuMu=         filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==14 , genParts)
    genNuTau=        filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==16 , genParts)
def filler(s):
    # shortcut
    r = reader.data
    if isMC: gPart = getGenPartsAll(r)

    # weight
    if options.T2tt:
        s.weight=signalWeight[(r.GenSusyMScan1, r.GenSusyMScan2)]['weight']
        s.mStop = r.GenSusyMScan1
        s.mNeu  = r.GenSusyMScan2
        s.reweightXSecUp    = signalWeight[(r.GenSusyMScan1, r.GenSusyMScan2)]['xSecFacUp']
        s.reweightXSecDown  = signalWeight[(r.GenSusyMScan1, r.GenSusyMScan2)]['xSecFacDown']
    elif isMC:
        s.weight = lumiScaleFactor*r.genWeight if lumiScaleFactor is not None else 1
    elif isData:
        s.weight = 1
    else:
        raise NotImplementedError( "isMC %r isData %r T2tt? %r TTDM?" % (isMC, isData, options.T2tt, options.TTDM) )

    # lumi lists and vetos
    if isData:
        s.vetoPassed  = vetoList.passesVeto(r.run, r.lumi, r.evt)
        s.jsonPassed  = lumiList.contains(r.run, r.lumi)
        # store decision to use after filler has been executed
        s.jsonPassed_ = s.jsonPassed

    if isMC:
        s.reweightPU     = puRW(r.nTrueInt)
        s.reweightPUDown = puRWDown(r.nTrueInt)
        s.reweightPUUp   = puRWUp(r.nTrueInt)

    # top pt reweighting
    if isMC: s.reweightTopPt = topPtReweightingFunc(getTopPtsForReweighting(r))/topScaleF if doTopPtReweighting else 1.

    # jet/met related quantities, also load the leptons already
    allJets      = getGoodJets(r, ptCut=0, jetVars = jetVarNames )
    jets         = filter(lambda j:jetId(j, ptCut=30, absEtaCut=2.4), allJets)
    bJets        = filter(lambda j:isBJet(j), jets)
    nonBJets     = filter(lambda j:not isBJet(j), jets)
    if isVeryLoose:
        # all leptons up to relIso 1
        miniRelIso = 999.
        ptCut = 20 if not isVeryLoosePt10 else 10 
        leptons_pt10 = getGoodAndOtherLeptons(r, ptCut=10, miniRelIso = miniRelIso , dz = 0.1, dxy = 1.)
        leptons      = filter(lambda l:l['pt']>ptCut, leptons_pt10)
    elif isLoose:
        # reliso 0.4
        miniRelIso = 0.4
        leptons_pt10 = getGoodLeptons(r, ptCut=10, miniRelIso = miniRelIso)
        leptons      = filter(lambda l:l['pt']>20, leptons_pt10)
    else:
        miniRelIso = 0.2
        leptons_pt10 = getGoodLeptons(r, ptCut=10, miniRelIso = miniRelIso)
        # relIso 0.2
        leptons      = filter(lambda l:l['pt']>20, leptons_pt10)

    s.met_pt  = r.met_pt
    s.met_phi = r.met_phi

    # Filling jets
    s.nJetGood   = len(jets)
    for iJet, jet in enumerate(jets):
        for b in jetVarNames:
            getattr(s, "JetGood_"+b)[iJet] = jet[b]
    if isSingleLep:
        # Compute M3 and the three indiced of the jets entering m3
        s.m3, s.m3_ind1, s.m3_ind2, s.m3_ind3 = m3( jets )

    s.ht         = sum([j['pt'] for j in jets])
    s.metSig     = s.met_pt/sqrt(s.ht) if s.ht>0 else float('nan')
    s.nBTag      = len(bJets)

    jets_sys      = {}
    bjets_sys     = {}
    nonBjets_sys  = {}

    metVariants = [''] # default

    # Keep photons and estimate met including (leading pt) photon
    if options.keepPhotons:
       photons = getGoodPhotons(r, ptCut=20, idLevel="loose", isData=isData)
       s.nPhotonGood = len(photons)
       if s.nPhotonGood > 0:
         metVariants += ['_photonEstimated']  # do all met calculations also for the photonEstimated variant
         s.photon_pt         = photons[0]['pt']
         s.photon_eta        = photons[0]['eta']
         s.photon_phi        = photons[0]['phi']
         s.photon_idCutBased = photons[0]['idCutBased']
         if isMC:
           genPhoton       = getGenPhoton(gPart)
           s.photon_genPt  = genPhoton['pt']  if genPhoton is not None else float('nan')
           s.photon_genEta = genPhoton['eta'] if genPhoton is not None else float('nan')

         s.met_pt_photonEstimated, s.met_phi_photonEstimated = getMetPhotonEstimated(r.met_pt, r.met_phi, photons[0])
         s.metSig_photonEstimated = s.met_pt_photonEstimated/sqrt(s.ht) if s.ht>0 else float('nan')

         s.photonJetdR = min(deltaR(photons[0], j) for j in jets) if len(jets) > 0 else 999
         s.photonLepdR = min(deltaR(photons[0], l) for l in leptons_pt10) if len(leptons_pt10) > 0 else 999

    if options.checkTTGJetsOverlap and isMC:
       s.TTGJetsEventType = getTTGJetsEventType(r)

    if addSystematicVariations:
        for j in allJets:
            j['pt_JECUp']   =j['pt']/j['corr']*j['corr_JECUp']
            j['pt_JECDown'] =j['pt']/j['corr']*j['corr_JECDown']
            # JERUp, JERDown, JER
            addJERScaling(j)
        for var in ['JECUp', 'JECDown', 'JERUp', 'JERDown']:
            jets_sys[var]       = filter(lambda j:jetId(j, ptCut=30, absEtaCut=2.4, ptVar='pt_'+var), allJets)
            bjets_sys[var]      = filter(isBJet, jets_sys[var])
            nonBjets_sys[var]   = filter(lambda j: not isBJet(j), jets_sys[var])

            setattr(s, "nJetGood_"+var, len(jets_sys[var]))
            setattr(s, "ht_"+var,       sum([j['pt_'+var] for j in jets_sys[var]]))
            setattr(s, "nBTag_"+var,    len(bjets_sys[var]))

        for var in ['JECUp', 'JECDown', 'JERUp', 'JERDown', 'UnclusteredEnUp', 'UnclusteredEnDown']:
            for i in metVariants:
                # use cmg MET correction values ecept for JER where it is zero. There, propagate jet variations.
                if 'JER' in var:
                  (met_corr_pt, met_corr_phi) = getMetJetCorrected(getattr(s, "met_pt" + i), getattr(s,"met_phi" + i), jets_sys[var], var)
                else:
                  (met_corr_pt, met_corr_phi) = getMetCorrected(r, var, photons[0] if i.count("photonEstimated") else None)

                setattr(s, "met_pt" +i+"_"+var, met_corr_pt)
                setattr(s, "met_phi"+i+"_"+var, met_corr_phi)
                ht = getattr(s, "ht_"+var) if 'Unclustered' not in var else s.ht 
                setattr(s, "metSig" +i+"_"+var, getattr(s, "met_pt"+i+"_"+var)/sqrt( ht ) if ht>0 else float('nan') )

    if isSingleLep or isDiLep:
        s.nGoodMuons      = len(filter( lambda l:abs(l['pdgId'])==13, leptons))
        s.nGoodElectrons  = len(filter( lambda l:abs(l['pdgId'])==11, leptons))

        if len(leptons)>=1:
            s.l1_pt     = leptons[0]['pt']
            s.l1_eta    = leptons[0]['eta']
            s.l1_phi    = leptons[0]['phi']
            s.l1_pdgId  = leptons[0]['pdgId']
            s.l1_index  = leptons[0]['index']
            s.l1_jetPtRelv2  = leptons[0]['jetPtRelv2']
            s.l1_jetPtRatiov2  = leptons[0]['jetPtRatiov2']
            s.l1_jetPtRelv2    = leptons[0]['jetPtRelv2']
            s.l1_miniRelIso = leptons[0]['miniRelIso']
            s.l1_dxy = leptons[0]['dxy']
            s.l1_dz = leptons[0]['dz']

        # For TTZ studies: find Z boson candidate, and use third lepton to calculate mt
        (s.mlmZ_mass, zl1, zl2) = closestOSDLMassToMZ(leptons_pt10)
#        if len(leptons_pt10) >= 3:
#            thirdLepton = leptons_pt10[[x for x in range(len(leptons_pt10)) if x != zl1 and x != zl2][0]]
#            for i in metVariants:
#              setattr(s, "mt"+i, sqrt(2*thirdLepton['pt']*getattr(s, "met_pt"+i)*(1-cos(thirdLepton['phi']-getattr(s, "met_phi"+i)))))

        if options.fastSim:
            s.reweightLeptonFastSimSF     = reduce(mul, [leptonFastSimSF.get3DSF(pdgId=l['pdgId'], pt=l['pt'], eta=l['eta'] , nvtx = r.nVert) for l in leptons], 1)
            s.reweightLeptonFastSimSFUp   = reduce(mul, [leptonFastSimSF.get3DSF(pdgId=l['pdgId'], pt=l['pt'], eta=l['eta'] , nvtx = r.nVert, sigma = +1) for l in leptons], 1)
            s.reweightLeptonFastSimSFDown = reduce(mul, [leptonFastSimSF.get3DSF(pdgId=l['pdgId'], pt=l['pt'], eta=l['eta'] , nvtx = r.nVert, sigma = -1) for l in leptons], 1)

    if isDiLep:
        if len(leptons)>=2:# and leptons[0]['pdgId']*leptons[1]['pdgId']<0 and abs(leptons[0]['pdgId'])==abs(leptons[1]['pdgId']): #OSSF choice
            mt2Calc.reset()
            s.l2_pt     = leptons[1]['pt']
            s.l2_eta    = leptons[1]['eta']
            s.l2_phi    = leptons[1]['phi']
            s.l2_pdgId  = leptons[1]['pdgId']
            s.l2_index  = leptons[1]['index']
            s.l2_jetPtRatiov2  = leptons[1]['jetPtRatiov2']
            s.l2_jetPtRelv2    = leptons[1]['jetPtRelv2']
            s.l2_miniRelIso = leptons[1]['miniRelIso']
            s.l2_dxy = leptons[1]['dxy']
            s.l2_dz = leptons[1]['dz']

            l_pdgs = [abs(leptons[0]['pdgId']), abs(leptons[1]['pdgId'])]
            l_pdgs.sort()
            s.isMuMu = l_pdgs==[13,13]
            s.isEE   = l_pdgs==[11,11]
            s.isEMu  = l_pdgs==[11,13]
            s.isOS   = s.l1_pdgId*s.l2_pdgId<0

            l1 = ROOT.TLorentzVector()
            l1.SetPtEtaPhiM(leptons[0]['pt'], leptons[0]['eta'], leptons[0]['phi'], 0 )
            l2 = ROOT.TLorentzVector()
            l2.SetPtEtaPhiM(leptons[1]['pt'], leptons[1]['eta'], leptons[1]['phi'], 0 )
            dl = l1+l2
            s.dl_pt   = dl.Pt()
            s.dl_eta  = dl.Eta()
            s.dl_phi  = dl.Phi()
            s.dl_mass = dl.M()
            mt2Calc.setLeptons(s.l1_pt, s.l1_eta, s.l1_phi, s.l2_pt, s.l2_eta, s.l2_phi)

            # To check MC truth when looking at the TTZToLLNuNu sample
            if isMC:
              zBoson          = getGenZ(gPart)
              s.zBoson_genPt  = zBoson['pt']  if zBoson is not None else float('nan')
              s.zBoson_genEta = zBoson['eta'] if zBoson is not None else float('nan')

            if options.keepPhotons and s.nPhotonGood > 0:
              gamma = ROOT.TLorentzVector()
              gamma.SetPtEtaPhiM(photons[0]['pt'], photons[0]['eta'], photons[0]['phi'], photons[0]['mass'] )
              dlg = dl + gamma
              s.dlg_mass = dlg.M()

            for i in metVariants:
                mt2Calc.setMet(getattr(s, 'met_pt'+i), getattr(s, 'met_phi', i))
                setattr(s, "dl_mt2ll"+i, mt2Calc.mt2ll())

                if len(jets)>=2:
                    bj0, bj1 = (bJets+nonBJets)[:2]
                    mt2Calc.setBJets(bj0['pt'], bj0['eta'], bj0['phi'], bj1['pt'], bj1['eta'], bj1['phi'])
                    setattr(s, "dl_mt2bb"+i,   mt2Calc.mt2bb())
                    setattr(s, "dl_mt2blbl"+i, mt2Calc.mt2blbl())

                if addSystematicVariations:
                    for var in ['JECUp', 'JECDown', 'JERUp', 'JERDown', 'UnclusteredEnUp', 'UnclusteredEnDown']:
                        mt2Calc.setMet( getattr(s, "met_pt"+i+"_"+var), getattr(s, "met_phi"+i+"_"+var) )
                        setattr(s, "dl_mt2ll"+i+"_"+var,  mt2Calc.mt2ll())
                        bj0_, bj1_ = bj0, bj1
                        if not 'Unclustered' in var:
                            if len(jets_sys[var])>=2:
                                bj0_, bj1_ = (bjets_sys[var]+nonBjets_sys[var])[:2]
                            else: 
                                bj0_, bj1_ = None, None
                        if bj0_ and bj1_:
                            mt2Calc.setBJets(bj0_['pt'], bj0_['eta'], bj0_['phi'], bj1_['pt'], bj1_['eta'], bj1_['phi'])
                            setattr(s, 'dl_mt2bb'  +i+'_'+var, mt2Calc.mt2bb())
                            setattr(s, 'dl_mt2blbl'+i+'_'+var, mt2Calc.mt2blbl())

    if addSystematicVariations:
        # B tagging weights method 1a
        for j in jets:
            btagEff.addBTagEffToJet(j)
        for var in btagEff.btagWeightNames:
            if var!='MC':
                setattr(s, 'reweightBTag_'+var, btagEff.getBTagSF_1a( var, bJets, nonBJets ) )

    # gen information on extra leptons
    if isMC and not options.skipGenLepMatching:
        genSearch.init( gPart )
        # Start with status 1 gen leptons in acceptance
        gLep = filter( lambda p:abs(p['pdgId']) in [11, 13] and p['status']==1 and p['pt']>20 and abs(p['eta'])<2.5, gPart )
        for l in gLep:
            ancestry = [ gPart[x]['pdgId'] for x in genSearch.ancestry( l ) ]
            l["n_D"]   =  sum([ancestry.count(p) for p in D_mesons])
            l["n_B"]   =  sum([ancestry.count(p) for p in B_mesons])
            l["n_W"]   =  sum([ancestry.count(p) for p in [24, -24]])
            l["n_t"]   =  sum([ancestry.count(p) for p in [6, -6]])
            l["n_tau"] =  sum([ancestry.count(p) for p in [15, -15]])
            matched_lep = bestDRMatchInCollection(l, leptons_pt10)
            if matched_lep:
                l["lepGoodMatchIndex"] = matched_lep['index']
                if isSingleLep:
                    l["matchesPromptGoodLepton"] = l["lepGoodMatchIndex"] in [s.l1_index]
                elif isDiLep:
                    l["matchesPromptGoodLepton"] = l["lepGoodMatchIndex"] in [s.l1_index, s.l2_index]
                else:
                    l["matchesPromptGoodLepton"] = 0
            else:
                l["lepGoodMatchIndex"] = -1
                l["matchesPromptGoodLepton"] = 0
#            if      l["n_t"]>0 and l["n_W"]>0 and l["n_B"]==0 and l["n_D"]==0 and l["n_tau"]==0:
#                print "t->W->l"
#            elif    l["n_t"]>0 and l["n_W"]==0 and l["n_B"]>0 and l["n_D"]==0 and l["n_tau"]==0:
#                print "t->b->B->l"
#            elif    l["n_t"]>0 and l["n_W"]==0 and l["n_B"]>0 and l["n_D"]>0 and l["n_tau"]==0:
#                print "t->b->B->D->l"
#            elif    l["n_t"]>0 and l["n_W"]>0 and l["n_B"]==0 and l["n_D"]==0 and l["n_tau"]>0 :
#                print "t->W->tau->l"
#            elif    l["n_t"]>0 and l["n_W"]>0 and l["n_B"]==0 and l["n_D"]>0 and l["n_tau"]==0:
#                print "t->W->c->D->l"
#            elif    l["n_t"]==0 and l["n_W"]==0 and l["n_B"]>0 and l["n_D"]>=0 and l["n_tau"]==0:
#                print l['pdgId'], l['pt'], l['phi'], l['eta'], ",".join(pdgToName( gPart[x]['pdgId']) for x in genSearch.ancestry(l) )
#                for p in genSearch.ancestry(l):
#                    print p, gPart[p]
#            else:
#                pass
                # print l['pdgId'], l['pt'], l['phi'], l['eta'], ",".join(pdgToName(gPart[x]['pdgId']) for x in genSearch.ancestry(l))
        s.nGenLep   = len(gLep)
        for iLep, lep in enumerate(gLep):
            for b in genLepVarNames:
                getattr(s, "GenLep_"+b)[iLep] = lep[b]
Exemple #10
0
def filler(s):
    # shortcut
    r = reader.data
    s.reweightPU = puRW(r.nTrueInt)
    s.weight = lumiScaleFactor * r.genWeight if lumiScaleFactor is not None else 1

    s.met_pt = r.met_genPt
    s.met_phi = r.met_genPhi

    genParts = getGenPartsAll(r)

    s.leptonicDecays = 0
    for g in genParts:
        if abs(g['pdgId']) == 6 and g['nDaughters'] == 2:
            try:
                # Look for top decay to W boson and (b-)jet
                if abs(genParts[g['daughterIndex1']]['pdgId']) != 24 and abs(
                        genParts[g['daughterIndex2']]['pdgId']) != 24:
                    continue
                if abs(genParts[g['daughterIndex1']]['pdgId']) == 24:
                    wBoson = genParts[g['daughterIndex1']]
                    bJet = genParts[g['daughterIndex2']]
                elif abs(genParts[g['daughterIndex2']]['pdgId']) == 24:
                    wBoson = genParts[g['daughterIndex2']]
                    bJet = genParts[g['daughterIndex1']]
                else:
                    raise Exception('Logic is wrong')

                # Go down through the W-boson radiations until we find the W decay
                while abs(genParts[
                        wBoson['daughterIndex1']]['pdgId']) == 24 or abs(
                            genParts[wBoson['daughterIndex2']]['pdgId']) == 24:
                    if abs(genParts[wBoson['daughterIndex1']]['pdgId']) == 24:
                        wBoson = genParts[wBoson['daughterIndex1']]
                    elif abs(
                            genParts[wBoson['daughterIndex2']]['pdgId']) == 24:
                        wBoson = genParts[wBoson['daughterIndex2']]
                    else:
                        raise Exception('Logic is wrong')

                if g['pdgId'] == 6:
                    s.t1_pt = g['pt']
                    s.t1_eta = g['eta']
                    s.t1_phi = g['phi']
                    s.t1_pdgId = g['pdgId']
                    s.bJet1_pt = bJet['pt']
                    s.bJet1_eta = bJet['eta']
                    s.bJet1_phi = bJet['phi']
                    s.bJet1_pdgId = bJet['pdgId']
                elif g['pdgId'] == -6:
                    s.t2_pt = g['pt']
                    s.t2_eta = g['eta']
                    s.t2_phi = g['phi']
                    s.t2_pdgId = g['pdgId']
                    s.bJet2_pt = bJet['pt']
                    s.bJet2_eta = bJet['eta']
                    s.bJet2_phi = bJet['phi']
                    s.bJet2_pdgId = bJet['pdgId']
                else:
                    raise Exception('Logic is wrong')

                # Check for leptonic decay
                if abs(genParts[wBoson['daughterIndex1']]['pdgId']) in (11, 12,
                                                                        13, 14,
                                                                        15,
                                                                        16):
                    s.leptonicDecays += 1
                    if abs(genParts[wBoson['daughterIndex1']]['pdgId']) in (
                            11, 13, 15):
                        l = genParts[wBoson['daughterIndex1']]
                        nu = genParts[wBoson['daughterIndex2']]
                    elif abs(genParts[wBoson['daughterIndex2']]['pdgId']) in (
                            11, 13, 15):
                        l = genParts[wBoson['daughterIndex2']]
                        nu = genParts[wBoson['daughterIndex1']]
                    else:
                        raise Exception('Logic is wrong')

                    if g['pdgId'] == 6:
                        s.l1_pt = l['pt']
                        s.l1_eta = l['eta']
                        s.l1_phi = l['phi']
                        s.l1_pdgId = l['pdgId']
                        s.nu1_pt = nu['pt']
                        s.nu1_eta = nu['eta']
                        s.nu1_phi = nu['phi']
                        s.nu1_pdgId = nu['pdgId']
                        s.mt1 = sqrt(2 * s.l1_pt * s.nu1_pt *
                                     (1 - cos(s.l1_phi - s.nu1_phi)))
                    elif g['pdgId'] == -6:
                        s.l2_pt = l['pt']
                        s.l2_eta = l['eta']
                        s.l2_phi = l['phi']
                        s.l2_pdgId = l['pdgId']
                        s.nu2_pt = nu['pt']
                        s.nu2_eta = nu['eta']
                        s.nu2_phi = nu['phi']
                        s.nu2_pdgId = nu['pdgId']
                        s.mt2 = sqrt(2 * s.l2_pt * s.nu2_pt *
                                     (1 - cos(s.l2_phi - s.nu2_phi)))
                    else:
                        raise Exception('Logic is wrong')

            except Exception, e:
                print str(e)
def filler(s):
    # shortcut
    r = reader.data
    if isMC: gPart = getGenPartsAll(r)

    # weight
    if options.T2tt:
        s.weight = signalWeight[(r.GenSusyMScan1, r.GenSusyMScan2)]['weight']
        s.mStop = r.GenSusyMScan1
        s.mNeu = r.GenSusyMScan2
        s.reweightXSecUp = signalWeight[(r.GenSusyMScan1,
                                         r.GenSusyMScan2)]['xSecFacUp']
        s.reweightXSecDown = signalWeight[(r.GenSusyMScan1,
                                           r.GenSusyMScan2)]['xSecFacDown']
    elif isMC:
        s.weight = lumiScaleFactor * r.genWeight if lumiScaleFactor is not None else 1
    elif isData:
        s.weight = 1
    else:
        raise NotImplementedError("isMC %r isData %r T2tt? %r TTDM?" %
                                  (isMC, isData, options.T2tt, options.TTDM))

    # lumi lists and vetos
    if isData:
        s.vetoPassed = vetoList.passesVeto(r.run, r.lumi, r.evt)
        s.jsonPassed = lumiList.contains(r.run, r.lumi)
        # store decision to use after filler has been executed
        s.jsonPassed_ = s.jsonPassed

    if isMC:
        s.reweightPU = puRW(r.nTrueInt)
        s.reweightPUDown = puRWDown(r.nTrueInt)
        s.reweightPUUp = puRWUp(r.nTrueInt)

    # top pt reweighting
    if isMC:
        s.reweightTopPt = topPtReweightingFunc(getTopPtsForReweighting(
            r)) / topScaleF if doTopPtReweighting else 1.

    # jet/met related quantities, also load the leptons already
    allJets = getGoodJets(r, ptCut=0, jetVars=jetVarNames)
    jets = filter(lambda j: jetId(j, ptCut=30, absEtaCut=2.4), allJets)
    bJets = filter(lambda j: isBJet(j), jets)
    nonBJets = filter(lambda j: not isBJet(j), jets)
    if isVeryLoose:
        # all leptons up to relIso 1
        miniRelIso = 999.
        ptCut = 20 if not isVeryLoosePt10 else 10
        leptons_pt10 = getGoodAndOtherLeptons(r,
                                              ptCut=10,
                                              miniRelIso=miniRelIso,
                                              dz=0.1,
                                              dxy=1.)
        leptons = filter(lambda l: l['pt'] > ptCut, leptons_pt10)
    elif isLoose:
        # reliso 0.4
        miniRelIso = 0.4
        leptons_pt10 = getGoodLeptons(r, ptCut=10, miniRelIso=miniRelIso)
        leptons = filter(lambda l: l['pt'] > 20, leptons_pt10)
    else:
        miniRelIso = 0.2
        leptons_pt10 = getGoodLeptons(r, ptCut=10, miniRelIso=miniRelIso)
        # relIso 0.2
        leptons = filter(lambda l: l['pt'] > 20, leptons_pt10)

    s.met_pt = r.met_pt
    s.met_phi = r.met_phi

    # Filling jets
    s.nJetGood = len(jets)
    for iJet, jet in enumerate(jets):
        for b in jetVarNames:
            getattr(s, "JetGood_" + b)[iJet] = jet[b]
    if isSingleLep:
        # Compute M3 and the three indiced of the jets entering m3
        s.m3, s.m3_ind1, s.m3_ind2, s.m3_ind3 = m3(jets)

    s.ht = sum([j['pt'] for j in jets])
    s.metSig = s.met_pt / sqrt(s.ht) if s.ht > 0 else float('nan')
    s.nBTag = len(bJets)

    jets_sys = {}
    bjets_sys = {}
    nonBjets_sys = {}

    metVariants = ['']  # default

    # Keep photons and estimate met including (leading pt) photon
    if options.keepPhotons:
        photons = getGoodPhotons(r, ptCut=20, idLevel="loose", isData=isData)
        s.nPhotonGood = len(photons)
        if s.nPhotonGood > 0:
            metVariants += [
                '_photonEstimated'
            ]  # do all met calculations also for the photonEstimated variant
            s.photon_pt = photons[0]['pt']
            s.photon_eta = photons[0]['eta']
            s.photon_phi = photons[0]['phi']
            s.photon_idCutBased = photons[0]['idCutBased']
            if isMC:
                genPhoton = getGenPhoton(gPart)
                s.photon_genPt = genPhoton[
                    'pt'] if genPhoton is not None else float('nan')
                s.photon_genEta = genPhoton[
                    'eta'] if genPhoton is not None else float('nan')

            s.met_pt_photonEstimated, s.met_phi_photonEstimated = getMetPhotonEstimated(
                r.met_pt, r.met_phi, photons[0])
            s.metSig_photonEstimated = s.met_pt_photonEstimated / sqrt(
                s.ht) if s.ht > 0 else float('nan')

            s.photonJetdR = min(deltaR(photons[0], j)
                                for j in jets) if len(jets) > 0 else 999
            s.photonLepdR = min(
                deltaR(photons[0], l)
                for l in leptons_pt10) if len(leptons_pt10) > 0 else 999

    if options.checkTTGJetsOverlap and isMC:
        s.TTGJetsEventType = getTTGJetsEventType(r)

    if addSystematicVariations:
        for j in allJets:
            j['pt_JECUp'] = j['pt'] / j['corr'] * j['corr_JECUp']
            j['pt_JECDown'] = j['pt'] / j['corr'] * j['corr_JECDown']
            # JERUp, JERDown, JER
            addJERScaling(j)
        for var in ['JECUp', 'JECDown', 'JERUp', 'JERDown']:
            jets_sys[var] = filter(
                lambda j: jetId(j, ptCut=30, absEtaCut=2.4, ptVar='pt_' + var),
                allJets)
            bjets_sys[var] = filter(isBJet, jets_sys[var])
            nonBjets_sys[var] = filter(lambda j: not isBJet(j), jets_sys[var])

            setattr(s, "nJetGood_" + var, len(jets_sys[var]))
            setattr(s, "ht_" + var,
                    sum([j['pt_' + var] for j in jets_sys[var]]))
            setattr(s, "nBTag_" + var, len(bjets_sys[var]))

        for var in [
                'JECUp', 'JECDown', 'JERUp', 'JERDown', 'UnclusteredEnUp',
                'UnclusteredEnDown'
        ]:
            for i in metVariants:
                # use cmg MET correction values ecept for JER where it is zero. There, propagate jet variations.
                if 'JER' in var:
                    (met_corr_pt, met_corr_phi) = getMetJetCorrected(
                        getattr(s, "met_pt" + i), getattr(s, "met_phi" + i),
                        jets_sys[var], var)
                else:
                    (met_corr_pt, met_corr_phi) = getMetCorrected(
                        r, var,
                        photons[0] if i.count("photonEstimated") else None)

                setattr(s, "met_pt" + i + "_" + var, met_corr_pt)
                setattr(s, "met_phi" + i + "_" + var, met_corr_phi)
                ht = getattr(s, "ht_" +
                             var) if 'Unclustered' not in var else s.ht
                setattr(
                    s, "metSig" + i + "_" + var,
                    getattr(s, "met_pt" + i + "_" + var) /
                    sqrt(ht) if ht > 0 else float('nan'))

    if isSingleLep or isDiLep:
        s.nGoodMuons = len(filter(lambda l: abs(l['pdgId']) == 13, leptons))
        s.nGoodElectrons = len(filter(lambda l: abs(l['pdgId']) == 11,
                                      leptons))

        if len(leptons) >= 1:
            s.l1_pt = leptons[0]['pt']
            s.l1_eta = leptons[0]['eta']
            s.l1_phi = leptons[0]['phi']
            s.l1_pdgId = leptons[0]['pdgId']
            s.l1_index = leptons[0]['index']
            s.l1_jetPtRelv2 = leptons[0]['jetPtRelv2']
            s.l1_jetPtRatiov2 = leptons[0]['jetPtRatiov2']
            s.l1_jetPtRelv2 = leptons[0]['jetPtRelv2']
            s.l1_miniRelIso = leptons[0]['miniRelIso']
            s.l1_dxy = leptons[0]['dxy']
            s.l1_dz = leptons[0]['dz']

        # For TTZ studies: find Z boson candidate, and use third lepton to calculate mt
        (s.mlmZ_mass, zl1, zl2) = closestOSDLMassToMZ(leptons_pt10)
        #        if len(leptons_pt10) >= 3:
        #            thirdLepton = leptons_pt10[[x for x in range(len(leptons_pt10)) if x != zl1 and x != zl2][0]]
        #            for i in metVariants:
        #              setattr(s, "mt"+i, sqrt(2*thirdLepton['pt']*getattr(s, "met_pt"+i)*(1-cos(thirdLepton['phi']-getattr(s, "met_phi"+i)))))

        if options.fastSim:
            s.reweightLeptonFastSimSF = reduce(mul, [
                leptonFastSimSF.get3DSF(
                    pdgId=l['pdgId'], pt=l['pt'], eta=l['eta'], nvtx=r.nVert)
                for l in leptons
            ], 1)
            s.reweightLeptonFastSimSFUp = reduce(mul, [
                leptonFastSimSF.get3DSF(pdgId=l['pdgId'],
                                        pt=l['pt'],
                                        eta=l['eta'],
                                        nvtx=r.nVert,
                                        sigma=+1) for l in leptons
            ], 1)
            s.reweightLeptonFastSimSFDown = reduce(mul, [
                leptonFastSimSF.get3DSF(pdgId=l['pdgId'],
                                        pt=l['pt'],
                                        eta=l['eta'],
                                        nvtx=r.nVert,
                                        sigma=-1) for l in leptons
            ], 1)

    if isDiLep:
        if len(
                leptons
        ) >= 2:  # and leptons[0]['pdgId']*leptons[1]['pdgId']<0 and abs(leptons[0]['pdgId'])==abs(leptons[1]['pdgId']): #OSSF choice
            mt2Calc.reset()
            s.l2_pt = leptons[1]['pt']
            s.l2_eta = leptons[1]['eta']
            s.l2_phi = leptons[1]['phi']
            s.l2_pdgId = leptons[1]['pdgId']
            s.l2_index = leptons[1]['index']
            s.l2_jetPtRatiov2 = leptons[1]['jetPtRatiov2']
            s.l2_jetPtRelv2 = leptons[1]['jetPtRelv2']
            s.l2_miniRelIso = leptons[1]['miniRelIso']
            s.l2_dxy = leptons[1]['dxy']
            s.l2_dz = leptons[1]['dz']

            l_pdgs = [abs(leptons[0]['pdgId']), abs(leptons[1]['pdgId'])]
            l_pdgs.sort()
            s.isMuMu = l_pdgs == [13, 13]
            s.isEE = l_pdgs == [11, 11]
            s.isEMu = l_pdgs == [11, 13]
            s.isOS = s.l1_pdgId * s.l2_pdgId < 0

            l1 = ROOT.TLorentzVector()
            l1.SetPtEtaPhiM(leptons[0]['pt'], leptons[0]['eta'],
                            leptons[0]['phi'], 0)
            l2 = ROOT.TLorentzVector()
            l2.SetPtEtaPhiM(leptons[1]['pt'], leptons[1]['eta'],
                            leptons[1]['phi'], 0)
            dl = l1 + l2
            s.dl_pt = dl.Pt()
            s.dl_eta = dl.Eta()
            s.dl_phi = dl.Phi()
            s.dl_mass = dl.M()
            mt2Calc.setLeptons(s.l1_pt, s.l1_eta, s.l1_phi, s.l2_pt, s.l2_eta,
                               s.l2_phi)

            # To check MC truth when looking at the TTZToLLNuNu sample
            if isMC:
                zBoson = getGenZ(gPart)
                s.zBoson_genPt = zBoson['pt'] if zBoson is not None else float(
                    'nan')
                s.zBoson_genEta = zBoson[
                    'eta'] if zBoson is not None else float('nan')

            if options.keepPhotons and s.nPhotonGood > 0:
                gamma = ROOT.TLorentzVector()
                gamma.SetPtEtaPhiM(photons[0]['pt'], photons[0]['eta'],
                                   photons[0]['phi'], photons[0]['mass'])
                dlg = dl + gamma
                s.dlg_mass = dlg.M()

            for i in metVariants:
                mt2Calc.setMet(getattr(s, 'met_pt' + i),
                               getattr(s, 'met_phi', i))
                setattr(s, "dl_mt2ll" + i, mt2Calc.mt2ll())

                if len(jets) >= 2:
                    bj0, bj1 = (bJets + nonBJets)[:2]
                    mt2Calc.setBJets(bj0['pt'], bj0['eta'], bj0['phi'],
                                     bj1['pt'], bj1['eta'], bj1['phi'])
                    setattr(s, "dl_mt2bb" + i, mt2Calc.mt2bb())
                    setattr(s, "dl_mt2blbl" + i, mt2Calc.mt2blbl())

                if addSystematicVariations:
                    for var in [
                            'JECUp', 'JECDown', 'JERUp', 'JERDown',
                            'UnclusteredEnUp', 'UnclusteredEnDown'
                    ]:
                        mt2Calc.setMet(getattr(s, "met_pt" + i + "_" + var),
                                       getattr(s, "met_phi" + i + "_" + var))
                        setattr(s, "dl_mt2ll" + i + "_" + var, mt2Calc.mt2ll())
                        bj0_, bj1_ = bj0, bj1
                        if not 'Unclustered' in var:
                            if len(jets_sys[var]) >= 2:
                                bj0_, bj1_ = (bjets_sys[var] +
                                              nonBjets_sys[var])[:2]
                            else:
                                bj0_, bj1_ = None, None
                        if bj0_ and bj1_:
                            mt2Calc.setBJets(bj0_['pt'], bj0_['eta'],
                                             bj0_['phi'], bj1_['pt'],
                                             bj1_['eta'], bj1_['phi'])
                            setattr(s, 'dl_mt2bb' + i + '_' + var,
                                    mt2Calc.mt2bb())
                            setattr(s, 'dl_mt2blbl' + i + '_' + var,
                                    mt2Calc.mt2blbl())

    if addSystematicVariations:
        # B tagging weights method 1a
        for j in jets:
            btagEff.addBTagEffToJet(j)
        for var in btagEff.btagWeightNames:
            if var != 'MC':
                setattr(s, 'reweightBTag_' + var,
                        btagEff.getBTagSF_1a(var, bJets, nonBJets))

    # gen information on extra leptons
    if isMC and not options.skipGenLepMatching:
        genSearch.init(gPart)
        # Start with status 1 gen leptons in acceptance
        gLep = filter(
            lambda p: abs(p['pdgId']) in [11, 13] and p['status'] == 1 and p[
                'pt'] > 20 and abs(p['eta']) < 2.5, gPart)
        for l in gLep:
            ancestry = [gPart[x]['pdgId'] for x in genSearch.ancestry(l)]
            l["n_D"] = sum([ancestry.count(p) for p in D_mesons])
            l["n_B"] = sum([ancestry.count(p) for p in B_mesons])
            l["n_W"] = sum([ancestry.count(p) for p in [24, -24]])
            l["n_t"] = sum([ancestry.count(p) for p in [6, -6]])
            l["n_tau"] = sum([ancestry.count(p) for p in [15, -15]])
            matched_lep = bestDRMatchInCollection(l, leptons_pt10)
            if matched_lep:
                l["lepGoodMatchIndex"] = matched_lep['index']
                if isSingleLep:
                    l["matchesPromptGoodLepton"] = l["lepGoodMatchIndex"] in [
                        s.l1_index
                    ]
                elif isDiLep:
                    l["matchesPromptGoodLepton"] = l["lepGoodMatchIndex"] in [
                        s.l1_index, s.l2_index
                    ]
                else:
                    l["matchesPromptGoodLepton"] = 0
            else:
                l["lepGoodMatchIndex"] = -1
                l["matchesPromptGoodLepton"] = 0
#            if      l["n_t"]>0 and l["n_W"]>0 and l["n_B"]==0 and l["n_D"]==0 and l["n_tau"]==0:
#                print "t->W->l"
#            elif    l["n_t"]>0 and l["n_W"]==0 and l["n_B"]>0 and l["n_D"]==0 and l["n_tau"]==0:
#                print "t->b->B->l"
#            elif    l["n_t"]>0 and l["n_W"]==0 and l["n_B"]>0 and l["n_D"]>0 and l["n_tau"]==0:
#                print "t->b->B->D->l"
#            elif    l["n_t"]>0 and l["n_W"]>0 and l["n_B"]==0 and l["n_D"]==0 and l["n_tau"]>0 :
#                print "t->W->tau->l"
#            elif    l["n_t"]>0 and l["n_W"]>0 and l["n_B"]==0 and l["n_D"]>0 and l["n_tau"]==0:
#                print "t->W->c->D->l"
#            elif    l["n_t"]==0 and l["n_W"]==0 and l["n_B"]>0 and l["n_D"]>=0 and l["n_tau"]==0:
#                print l['pdgId'], l['pt'], l['phi'], l['eta'], ",".join(pdgToName( gPart[x]['pdgId']) for x in genSearch.ancestry(l) )
#                for p in genSearch.ancestry(l):
#                    print p, gPart[p]
#            else:
#                pass
# print l['pdgId'], l['pt'], l['phi'], l['eta'], ",".join(pdgToName(gPart[x]['pdgId']) for x in genSearch.ancestry(l))
        s.nGenLep = len(gLep)
        for iLep, lep in enumerate(gLep):
            for b in genLepVarNames:
                getattr(s, "GenLep_" + b)[iLep] = lep[b]
reader.start()
cmg_reader.start()

for i_event, event in enumerate( intersec ):

    reader.goToPosition( positions[event] )
    cmg_reader.goToPosition( cmg_positions[event] )

    # Make sure alignement of trees is OK
    assert reader.data.met_pt == cmg_reader.data.met_pt, "Events inconsistent!"

    logger.info( "###################### Event %2i #############################" % i_event )
    logger.info( "Run %i lumi %i event %i", reader.data.run, reader.data.lumi, reader.data.evt )

    # gen particles
    gPart = getGenPartsAll( reader.data )
    genSearch.init( gPart )

    # reco leptons
    all_reco_leps = getGoodAndOtherLeptons(cmg_reader.data, ptCut=10, miniRelIso = 999. , dz = 0.1, dxy = 1.)

    # Start with generated leptons and match to reco ones
    # W decay modes
    W_decay = []
    prompt_gen_leptons = []
    for W in filter(lambda p:abs(p['pdgId'])==24 and p['daughterIndex2']>0, gPart):

        W_daughters = [gPart[W['daughterIndex1']], gPart[W['daughterIndex2']] ]
        W_daughters_pdgId = map(lambda p:abs(p['pdgId']), W_daughters)
        W_daughters_pdgId.sort()
        #prompt e
        looseMu = filter(lambda l: abs(l['pdgId'])==13 and l['miniRelIso']<0.4 and l['pt']>15, allLeptons)
        looseEle= filter(lambda l: abs(l['pdgId'])==11 and l['miniRelIso']<0.4 and l['pt']>15, allLeptons)
        mu      = filter(lambda l: abs(l['pdgId'])==13, leptons)
        ele     = filter(lambda l: abs(l['pdgId'])==11, leptons)
        tau     = getGoodTaus(s.chain)

#multi-iso minValues
        min_jetPtRatiov2 = min(l['jetPtRatiov2'] for l in leptons)
        min_jetPtRelv2   = min(l['jetPtRelv2'] for l in leptons)

#RECO matches
        muMatched   = filter(lambda l: abs(l['mcMatchAny'])==1, mu)
        eleMatched  = filter(lambda l: abs(l['mcMatchAny'])==1, ele)
        tauMatched  = filter(lambda l: abs(l['mcMatchId'])>=1, tau)
#GEN
        genParts = getGenPartsAll(s.chain)
        status1MuEle        =   filter(lambda p: abs(p['pdgId']) in [11,13] and p['status']==1 and p['pt']>10, genParts)
        genLeptons          =   [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) in [24] and abs(p['pdgId']) in lepPdgs, genParts)]
        genLeptonsFromTau   =   [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) in [15] and abs(p['pdgId']) in lepPdgs, genParts)]
        genNeutrinosFromW   =   [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) ==24 and abs(p['pdgId']) in nuPdgs, genParts)]
        genNeutrinosFromTau =   [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) ==15 and abs(p['pdgId']) in nuPdgs, genParts)]
        otherNeutrinos      =   [descendDecay(q, genParts) for q in filter(lambda p: abs(p['pdgId']) in nuPdgs, genParts) if not descendDecay(q, genParts) in genNeutrinosFromW+genNeutrinosFromTau]
        genEle =                [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) in [24] and abs(p['pdgId'])==11 , genParts)]
        genMu  =                [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) in [24] and abs(p['pdgId'])==13 , genParts)]
        genEleFromTau=          [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) in [15] and abs(p['pdgId'])==11 , genParts)]
        genMuFromTau=           [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId']) in [15] and abs(p['pdgId'])==13 , genParts)]
        genTau=                 [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 , genParts)]
        genTauToE=              [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 and decaysTo(p, 11, genParts), genParts)]
        genTauToMu=             [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 and decaysTo(p, 13, genParts), genParts)]
        genTauToHad=            [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==15 and not (decaysTo(p, 11, genParts) or decaysTo(p, 13, genParts)), genParts)]
        genNuE =                [descendDecay(q, genParts) for q in filter(lambda p: abs(p['motherId'])==24 and abs(p['pdgId'])==12 , genParts)]