def makeGenArray(tree, what, ptCut, etaCut, _cache={}):
    _key = (id(tree),what,int(ptCut*100),int(etaCut*1000))
    if _key in _cache: return _cache[_key]
    if what == "metmht":
        met = makeGenArray(tree, "met",     0,    5.0, _cache=_cache)
        mht = makeGenArray(tree, "mht", ptCut, etaCut, _cache=_cache)
        ret = map(min, zip(met,mht))
        _cache[_key] = ret
        return ret
    if "met" in what:
        ret = makeGenMETArray(tree, what, etaCut)
        _cache[_key] = ret
        return ret
    progress = _progress("Reading GenJets ...")
    calc = makeCalc(what)
    ret = []
    tree.SetBranchStatus("*",0);
    tree.SetBranchStatus("nGenJets",1);
    tree.SetBranchStatus("GenJets_*",1);
    for i in xrange(tree.GetEntries()):
        tree.GetEntry(i)
        pt,eta,phi = tree.GenJets_pt, tree.GenJets_eta, tree.GenJets_phi
        jets = [ (pt[j],eta[j],phi[j]) for j in xrange(tree.nGenJets) if pt[j] > ptCut and abs(eta[j]) < etaCut ]
        #ret.append(calc(jets))
        ret.append(jets)
    _cache[_key] = ret
    progress.done("done, %d entries" % len(ret))
    return ret
예제 #2
0
def makeCorrArray(tree, what, obj, ptCorrCut, etaCut, corr, _cache={}):
    _key = (id(tree), what, obj, int(ptCorrCut * 100), int(etaCut * 1000))
    if _key in _cache: return _cache[_key]
    if what == "metmht":
        met = makeCorrArray(tree, "met", obj, 0, 5.0, corr, _cache=_cache)
        mht = makeCorrArray(tree,
                            "mht",
                            obj,
                            ptCorrCut,
                            etaCut,
                            corr,
                            _cache=_cache)
        ret = ROOT.makeMinimum(met, mht)
        _cache[_key] = ret
        return ret
    if "met" in what:
        ret = makeMETArray(tree, what, obj, etaCut)
        _cache[_key] = ret
        return ret
    if not tree.GetBranch("n" + obj + "Jets"):
        if obj == "Gen": raise RuntimeError("Missing GenJets!")
        return None
    cppcalc = makeCalcCpp(what)
    progress = _progress("  Reading " + obj + "Jets in C++...")
    ret = ROOT.makeJetArray(tree, obj, ptCorrCut, etaCut, cppcalc, corr)
    _cache[_key] = ret
    progress.done("done, %d entries" % len(ret))
    return ret
예제 #3
0
def makeMETArray(tree, what, obj, etaCut):
    if obj == "Gen": obj = "gen"  # fix issue with naming convention
    if etaCut <= 1.5: post = "MetBarrel"
    elif etaCut <= 2.4: post = "MetCentral"
    else: post = "Met"
    if not tree.GetBranch(obj + post + "_pt"):
        if obj == "gen": raise RuntimeError("Missing gen" + post)
        return None
    progress = _progress("  Reading " + obj + post + " in C++...")
    ret = ROOT.makeMetArray(tree, obj + post)
    progress.done("done, %d entries" % len(ret))
    return ret
def makeCorrArray(tree, what, obj, ptCorrCut, etaCut, corr, _cache={}):
    _key = (id(tree),what,obj,int(ptCorrCut*100),int(etaCut*1000))
    if _key in _cache: return _cache[_key]
    if what == "metmht":
        met = makeCorrArray(tree, "met", obj, ptCorrCut,    5.0, None, _cache=_cache)
        mht = makeCorrArray(tree, "mht", obj, ptCorrCut, etaCut, None, _cache=_cache)
        #met = makeCorrArray(tree, "met", obj, ptCorrCut,    5.0, corr, _cache=_cache)
        #mht = makeCorrArray(tree, "mht", obj, ptCorrCut, etaCut, corr, _cache=_cache)
        ret = map(min, zip(met,mht))
        _cache[_key] = ret
        return ret
    if "met" in what:
        #ret = makeCorrArray(tree, "met", obj, ptCorrCut,    5.0, None, _cache=_cache)
        ret = makeRecoMETArray(tree, what, obj, etaCut)
        _cache[_key] = ret
        return ret
    if "corrMet" in what:
        ret = makeCorrMetArray(tree, obj, ptCorrCut, etaCut, corr, _cache)
        #ret = makeCorrMetArray(tree, obj, 15., 2.3, corr, _cache)
        _cache[_key] = ret
        return ret
    calc = makeCalc(what)
    ret = []
    if not tree.GetBranch("n"+obj+"Jets"): 
        return None
    progress = _progress("Reading "+obj+"Jets ...")
    tree.SetBranchStatus("*",0);
    tree.SetBranchStatus("n"+obj+"Jets",1);
    tree.SetBranchStatus(obj+"Jets_pt",1);
    tree.SetBranchStatus(obj+"Jets_eta",1);
    tree.SetBranchStatus(obj+"Jets_phi",1);
    for i in xrange(tree.GetEntries()):
        tree.GetEntry(i)
        number = getattr(tree, "n"+obj+"Jets")
        rawpt,eta,phi = getattr(tree, obj+"Jets_pt"), getattr(tree, obj+"Jets_eta"), getattr(tree, obj+"Jets_phi")
        jets = [ ]
        for j in xrange(number):
            if abs(eta[j]) > etaCut: continue
            if corr:
                pt = corr.correctedPt(rawpt[j], eta[j])
#                pt = rawpt[j]
            else:
                pt = rawpt[j]
            if pt > ptCorrCut: 
                jets.append( (pt,eta[j],phi[j]) ) 
        ret.append(calc(jets))
    _cache[_key] = ret
    progress.done("done, %d entries" % len(ret))
    #print(obj+" corr MET")
    #print(ret)
    return ret
def makeCorrMetArray(tree, obj, ptCorrCut, etaCut, corr, _cache={}):
    ret = []
    retphi = []
    _key = (id(tree), what, int(ptCorrCut * 100), int(etaCut * 1000))
    if etaCut <= 1.5: post = "MetBarrel_pt"
    elif etaCut <= 2.4: post = "MetCentral_pt"
    else: post = "Met_pt"
    if not tree.GetBranch(obj + post):
        return None
    if not tree.GetBranch("n" + obj + "Jets"):
        return None
    progress = _progress("Reading " + obj + "Jets ...")
    tree.SetBranchStatus("*", 0)
    tree.SetBranchStatus("n" + obj + "Jets", 1)
    tree.SetBranchStatus(obj + "Jets_pt", 1)
    tree.SetBranchStatus(obj + "Jets_eta", 1)
    tree.SetBranchStatus(obj + "Jets_phi", 1)
    tree.SetBranchStatus(obj + post, 1)
    tree.SetBranchStatus((obj + post).replace("pt", "phi"), 1)
    corr_vec = ROOT.TVector2()
    tmp_vec = ROOT.TVector2()
    #tree.Print((obj+post).replace("pt", "phi"))
    for i in xrange(tree.GetEntries()):
        tree.GetEntry(i)
        number = getattr(tree, "n" + obj + "Jets")
        rawpt, eta, phi = getattr(tree, obj + "Jets_pt"), getattr(
            tree, obj + "Jets_eta"), getattr(tree, obj + "Jets_phi")
        jets = []
        corr_vec.SetMagPhi(0, 0)
        for j in xrange(number):
            if abs(eta[j]) > etaCut: continue
            if corr and rawpt > ptCorrCut:
                tmp_vec.SetMagPhi(
                    corr.correctedPt(rawpt[j], eta[j]) - rawpt[j], phi[j])
                corr_vec += tmp_vec
        #tmp_vec.SetMagPhi(float(getattr(tree, obj+"Jets_pt")), float(getattr(tree, obj+"Jets_phi")))
        #print(getattr(tree, obj+"Jets_pt"))
        tmp_vec.SetMagPhi(getattr(tree, obj + post),
                          getattr(tree, (obj + post).replace("pt", "phi")))
        # corrected MET = old MET + (JEC-HTmiss - no-JEC-HTmiss)
        # subtract (JEC-noJEC) here instead of adding since 'HTmiss = - vector sum of jets'
        corr_vec = tmp_vec - corr_vec
        ret.append(corr_vec.Mod())
        retphi.append(corr_vec.Phi())
    _cache[_key] = ret
    progress.done("done, %d entries" % len(ret))
    #print(obj+" corr MET")
    #print(ret)
    return ret, retphi
def makeRecoMETArray(tree, what, obj, etaCut):
    if   etaCut <= 1.5: post = "MetBarrel_pt" 
    elif etaCut <= 2.4: post = "MetCentral_pt"
    else:               post = "Met_pt"
    if not tree.GetBranch(obj+post): 
        return None
    progress = _progress("Reading "+obj+post+" ...")
    tree.SetBranchStatus("*",0);
    tree.SetBranchStatus(obj+post,1);
    ret = []
    for i in xrange(tree.GetEntries()):
        tree.GetEntry(i)
        ret.append(getattr(tree,obj+post))
    progress.done("done, %d entries" % len(ret))
    #print(obj+" Reco MET")
    #print(ret[8208])
    return ret
def makeGenMETArray(tree, what, etaCut):
    if   etaCut <= 1.5: post = "MetBarrel_pt" 
    elif etaCut <= 2.4: post = "MetCentral_pt"
    else:               post = "Met_pt"
    pos = "Met"
    progress = _progress("Reading gen"+post+" ...")
    tree.SetBranchStatus("*",0);
    tree.SetBranchStatus("gen"+post,1);
    tree.SetBranchStatus("gen"+pos+"_phi",1);
    tree.SetBranchStatus("nGenPart", 1);
    tree.SetBranchStatus("GenPart_pt", 1);
    tree.SetBranchStatus("GenPart_phi", 1);
    tree.SetBranchStatus("GenPart_eta", 1);
    tree.SetBranchStatus("GenPart_mass", 1);
    tree.SetBranchStatus("GenPart_pdgId", 1);
    tree.SetBranchStatus("GenPart_statusFlags", 1);
    ret_leMuon = []
    ret_suMuon = []
    retphi = []
    ret = []
    for i in xrange(tree.GetEntries()):
        tree.GetEntry(i)
        ret.append(getattr(tree,"gen"+post))
        retphi.append(getattr(tree,"gen"+pos+"_phi"))
        num = 0
        or1 = 0
        or2 = 0
        nPart = tree.nGenPart
        Id, flag, eta, pt, phi, mass = tree.GenPart_pdgId, tree.GenPart_statusFlags, tree.GenPart_eta, tree.GenPart_pt, tree.GenPart_phi, tree.GenPart_mass
        for j in xrange(getattr(tree, "nGenPart")):
            if abs(Id[j])==13 and (flag[j]&1) and (flag[j]&(1<<13)) and abs(eta[j]) < 2.5 and pt[j] > 15:
                num+=1
                if num==1:
                    or1 = j
                elif num==2:
                    or2 = j
        zero = 0
        ret_leMuon.append([])
        ret_suMuon.append([])
        if num==2:
            ret_leMuon[i].append(pt[or1])
            ret_leMuon[i].append(eta[or1])
            ret_leMuon[i].append(phi[or1])
            ret_leMuon[i].append(mass[or1])
            ret_suMuon[i].append(pt[or2])
            ret_suMuon[i].append(eta[or2])
            ret_suMuon[i].append(phi[or2])
            ret_suMuon[i].append(mass[or2])
        else:
            ret_leMuon[i].append(zero)
            ret_leMuon[i].append(zero)
            ret_leMuon[i].append(zero)
            ret_leMuon[i].append(zero)
            ret_suMuon[i].append(zero)
            ret_suMuon[i].append(zero)
            ret_suMuon[i].append(zero)
            ret_suMuon[i].append(zero)
    progress.done("done, %d entries" % len(ret))
    #print(obj+" MET Array")
    #print(ret)
    return ret, retphi, ret_leMuon, ret_suMuon
def makeGenMETArray(tree, what, etaCut):
    if etaCut <= 1.5: post = "MetBarrel_pt"
    elif etaCut <= 2.4: post = "MetCentral_pt"
    else: post = "Met_pt"
    pos = "Met"
    progress = _progress("Reading gen" + post + " ...")
    tree.SetBranchStatus("*", 0)
    tree.SetBranchStatus("gen" + post, 1)
    tree.SetBranchStatus("gen" + pos + "_phi", 1)
    tree.SetBranchStatus("nGenPart", 1)
    tree.SetBranchStatus("GenPart_pt", 1)
    tree.SetBranchStatus("GenPart_phi", 1)
    tree.SetBranchStatus("GenPart_eta", 1)
    tree.SetBranchStatus("GenPart_mass", 1)
    tree.SetBranchStatus("GenPart_pdgId", 1)
    tree.SetBranchStatus("GenPart_statusFlags", 1)
    ret_1nu = []
    ret_2nu = []
    ret_3nu = []
    ret_4nu = []
    retphi = []
    ret = []
    for i in xrange(tree.GetEntries()):
        tree.GetEntry(i)
        ret.append(getattr(tree, "gen" + post))
        retphi.append(getattr(tree, "gen" + pos + "_phi"))
        nPart = tree.nGenPart
        num, num2, order = 0, 0, [0, 0, 0, 0]
        Id, flag, eta, pt, phi, mass = tree.GenPart_pdgId, tree.GenPart_statusFlags, tree.GenPart_eta, tree.GenPart_pt, tree.GenPart_phi, tree.GenPart_mass
        for j in xrange(getattr(tree, "nGenPart")):
            if (abs(Id[j]) == 12 or abs(Id[j]) == 14
                    or abs(Id[j]) == 16) and pt[j] > 5:
                num += 1
        for j in xrange(getattr(tree, "nGenPart")):
            if (num != 4): continue
            if (abs(Id[j]) == 12 or abs(Id[j]) == 14
                    or abs(Id[j]) == 16) and pt[j] > 5:
                order[num2] = j
                num2 += 1
        ret_1nu.append([])
        ret_2nu.append([])
        ret_3nu.append([])
        ret_4nu.append([])
        if num == 4:
            ret_1nu[i].append(pt[order[0]])
            ret_1nu[i].append(eta[order[0]])
            ret_1nu[i].append(phi[order[0]])
            ret_1nu[i].append(mass[order[0]])
            ret_2nu[i].append(pt[order[1]])
            ret_2nu[i].append(eta[order[1]])
            ret_2nu[i].append(phi[order[1]])
            ret_2nu[i].append(mass[order[1]])
            ret_3nu[i].append(pt[order[2]])
            ret_3nu[i].append(eta[order[2]])
            ret_3nu[i].append(phi[order[2]])
            ret_3nu[i].append(mass[order[2]])
            ret_4nu[i].append(pt[order[3]])
            ret_4nu[i].append(eta[order[3]])
            ret_4nu[i].append(phi[order[3]])
            ret_4nu[i].append(mass[order[3]])
        else:
            for k in range(0, 4):
                ret_1nu[i].append(0)
                ret_2nu[i].append(0)
                ret_3nu[i].append(0)
                ret_4nu[i].append(0)
    progress.done("done, %d entries" % len(ret))
    #print(obj+" MET Array")
    #print(ret)
    return ret, retphi, ret_1nu, ret_2nu, ret_3nu, ret_4nu
예제 #9
0
            if len(points) == 0 or points[-1][0] != plat:
                points.append((plat, rate))
        rate *= 1.2
    #platprogress.done(" done, with %d points" % len(points))
    if not points: return (None, None)
    plot = ROOT.TGraph(len(points))
    for i, (x, y) in enumerate(points):
        plot.SetPoint(i, x, y)
    label = name
    _cache[_key] = (plot, label)
    return (plot, label)


print "Plotting for %s (%s)" % (options.var, options.varlabel)
for plotkind in options.plots.split(","):
    progress = _progress("Make plot %s: \n" % plotkind)
    if plotkind != "rate":
        genArray = makeGenArray(signal, what, options.pt, options.eta)
    if plotkind == "isorate":
        plotparams = map(float, options.rate.split(","))
    elif plotkind in ("platroc", "plateff"):
        plotparams = map(float, options.eff.split(","))
    else:
        plotparams = [None]
    for plotparam in plotparams:
        for objset, things in whats:
            if options.what and (objset not in options.what.split(",")):
                continue
            if options.what_reg:
                if not any(
                        re.match(p + "$", objset)