def getSelection(l0, l1, jets, met):
    nClJets = len(jets)
    sel = None
    l0pt    = l0.p4.Pt()
    l1pt    = l1.p4.Pt()
    j0      = jets[0]
    mll     = (l0.p4 + l1.p4).M()
    ht      = kin.computeHt(met.p4, [l0.p4, l1.p4]+[j.p4 for j in jets])
    metrel  = kin.computeMetRel(met.p4, [l0.p4, l1.p4]+[j.p4 for j in jets])
    mtl0    = kin.computeMt(l0.p4, met.p4)
    mtl1    = kin.computeMt(l1.p4, met.p4)
    mtmin   = min([mtl0, mtl1])
    mtmax   = max([mtl0, mtl1])
    mlj     = kin.computeMlj(l0.p4, l1.p4, j0.p4)
    detall  = fabs(l0.p4.Eta() - l1.p4.Eta())
    mljj = kin.computeMljj(l0.p4, l1.p4, jets[0].p4, jets[1].p4) if nClJets>1 else None
    ll = kin.getDilepType(l0, l1)
    nj = 'eq1j' if nClJets==1 else 'ge2j'
    if (ll=='mm' and nj=='eq1j'
        and l0pt    >  30.0
        and l1pt    >  20.0
        and detall  <   1.5
        and mtmax   > 100.0
        and ht      > 200.0):
        sel = 'sr_mm_eq1j' if mlj<90.0 else 'cr_mm_eq1j'
    elif (ll=='mm' and nj=='ge2j'
          and l0pt    >  30.0
          and l1pt    >  30.0
          and detall  <   1.5
          and ht      > 220.0):
        sel ='sr_mm_ge2j' if mljj<120.0 else 'cr_mm_ge2j'
    elif (ll=='em' and nj=='eq1j'
          and l0pt    >  30.0
          and l1pt    >  30.0
          and detall  <   1.5
          and ht      > 200.0
          and mtmax   > 110.0):
        sel = 'sr_ee_eq1j' if mlj<90.0 else 'cr_ee_eq1j'
    elif (ll=='em' and nj=='ge2j'
          and l0pt    >  30.0
          and l1pt    >  30.0
          and detall  <   1.5
          and ht      > 200.0
          and mtmax   > 110.0):
        sel = 'sr_em_ge2j' if mljj<120.0 else 'cr_em_ge2j'
    elif (ll=='ee' and nj=='eq1j'
          and l0pt    >  30.0
          and l1pt    >  20.0
          and fabs(mll-91.2) > 10.0
          and metrel  >  55.0
          and ht      > 200.0):
        sel = 'sr_ee_eq1j' if mlj<90.0 else 'cr_ee_eq1j'
    elif (ll=='ee' and nj=='ge2j'
          and l0pt    >  30.0
          and l1pt    >  20.0
          and fabs(mll-91.2) > 10.0
          and metrel  >  30.0
          and mtmax   > 100.0):
        sel = 'sr_ee_ge2j' if mljj<120.0 else 'cr_ee_ge2j'
    return sel
def fillHistosAndCount(histos, files, lls, njs, testRun=False) :
    "Fill the histograms, and provide a dict of event counters[sample][sel] for the summary"
    treename = 'SusySel'
    counts = dict()
    for sample, filename in files.iteritems() :
        countsSample = collections.defaultdict(float)
        histosSample = histos[sample]
        file = r.TFile.Open(filename)
        tree = file.Get(treename)
        nEvents = tree.GetEntries()
        nEventsToProcess = nEvents if not testRun else nEvents/10
        print "processing %s (%d entries %s) %s"%(sample, nEventsToProcess, ", 10% test" if testRun else "", datetime.datetime.now())
        for iEvent, event in enumerate(tree) :
            if iEvent > nEventsToProcess : break
            l0, l1, met, pars = addTlv(event.l0), addTlv(event.l1), addTlv(event.met), event.pars
            jets, lepts = [addTlv(j) for j in event.jets], [addTlv(l) for l in event.lepts]
            ll = getDilepType(l0, l1)
            nJets = len(jets)
            nj = 'eq1j' if nJets==1 else 'ge2j'
            assert nJets>0,"messed something up in the selection upstream"
            if ll not in lls or nj not in njs : continue
            pt0 = l0.p4.Pt()
            pt1 = l1.p4.Pt()
            j0  = jets[0]
            mll  = (l0.p4 + l1.p4).M()
            mtllmet = computeMt(l0.p4 + l1.p4, met.p4)
            ht      = computeHt(met.p4, [l0.p4, l1.p4]+[j.p4 for j in jets])
            metrel  = computeMetRel(met.p4, [l0.p4, l1.p4]+[j.p4 for j in jets])
            mtl0    = computeMt(l0.p4, met.p4)
            mtl1    = computeMt(l1.p4, met.p4)
            mtmin   = min([mtl0, mtl1])
            mtmax   = max([mtl0, mtl1])
            mlj     = computeMlj(l0.p4, l1.p4, j0.p4)
            dphill  = abs(phi_mpi_pi(l0.p4.DeltaPhi(l1.p4)))
            detall  = fabs(l0.p4.Eta() - l1.p4.Eta())
            l3Veto  =  not thirdLepZcandidateIsInWindow(l0, l1, lepts)
            mljj = None
            if nJets >1 :
                j0, j1 = jets[0], jets[1]
                mt2j   = computeMt2j(l0.p4, l1.p4, j0.p4, j1.p4, met.p4)
                mljj   = computeMljj(l0.p4, l1.p4, j0.p4, j1.p4)
                dphijj = fabs(phi_mpi_pi(j0.p4.DeltaPhi(j1.p4)))
                detajj = fabs(j0.p4.Eta() - j1.p4.Eta())
            if passSelection(pt0, pt1, mll, mtllmet, ht, metrel, l3Veto,
                             detall, mtmax, mlj, mljj,
                             ll, nj) :
                llnj = llnjKey(ll, nj)
                weight = pars.weight
                varHistos = histosSample[llnj]
                varValues = dict([(v, eval(v)) for v in variablesToPlot()])
                fillVarHistos(varHistos, varValues, weight, nj)
                countsSample[llnj] += weight
        file.Close()
        file.Delete()
        counts[sample] = countsSample
    return counts
def createOutTree(filenames, dilepChan, nJetChan, tag='', overwrite=False) :
    assert dilepChan in ['ee','mm','em']
    assert nJetChan in ['eq1j', 'ge2j']
    outFilenames = dict()
    for sample, filename in filenames.iteritems() :
        outFilename = '/tmp/'+sample+'_'+dilepChan+'_'+nJetChan+'.root'
        if os.path.exists(outFilename) and not overwrite :
            outFilenames[sample] = outFilename
            continue
        outFile = r.TFile.Open(outFilename, 'recreate')
        outTree = r.TTree("training","Training tree")
        outTree.Branch('vars', vars, '/F:'.join(leafNames))
        outTree.SetDirectory(outFile)
        file = r.TFile.Open(filename)
        tree = file.Get(treename)
        print "processing %s %s %s (%d entries)"%(sample, dilepChan, nJetChan, tree.GetEntries())
        for iEvent, event in enumerate(tree) :
            resetVars(vars)
            l0 = addTlv(event.l0)
            l1 = addTlv(event.l1)
            met = addTlv(event.met)
            jets = [addTlv(j) for j in event.jets]
            lepts = [addTlv(l) for l in event.lepts]
            pars = event.pars
            dilepType = getDilepType(event.l0, event.l1)
            nJets = len(jets)
            if dilepType != dilepChan : continue
            if nJets<1 or (nJets>1 and nJetChan=='eq1j') : continue
            if thirdLepZcandidateIsInWindow(l0, l1, lepts, 20.0) : continue
            mt0, mt1 = computeMt(l0.p4, met.p4), computeMt(l1.p4, met.p4)
            vars.pt0 = l0.p4.Pt()
            vars.pt1 = l1.p4.Pt()
            vars.mll = (l0.p4+l1.p4).M()
            vars.mtmin = min([mt0, mt1])
            vars.mtmax = max([mt0, mt1])
            vars.mtllmet = computeMt(l0.p4 + l1.p4, met.p4)
            vars.ht = computeHt(met.p4, [l0.p4, l1.p4]+[j.p4 for j in jets])
            vars.metrel = computeMetRel(met.p4, [l0.p4, l1.p4]+[j.p4 for j in jets])
            vars.dphill = fabs(phi_mpi_pi(l0.p4.DeltaPhi(l1.p4)))
            vars.detall = fabs(l0.p4.Eta() - l1.p4.Eta())
            if nJets >1 :
                j0, j1 = jets[0], jets[1]
                vars.mt2j = computeMt2j(l0.p4, l1.p4, j0.p4, j1.p4, met.p4)
                vars.mljj = computeMljj(l0.p4, l1.p4, j0.p4, j1.p4)
                vars.dphijj = fabs(phi_mpi_pi(j0.p4.DeltaPhi(j1.p4)))
                vars.detajj = fabs(j0.p4.Eta() - j1.p4.Eta())
            outTree.Fill()
        print "filled ",outTree.GetEntries()," entries"
        outFile.Write()
        outFile.Close()
        outFilenames[sample] = outFile.GetName()
    return outFilenames
 def fill():
     pt, eta, mt = lep.p4.Pt(), abs(lep.p4.Eta()), computeMt(lep.p4, met.p4)
     pt, eta, mt = shiftWithinRange(pt, eta, mt) # avoid loosing entries due to over/underflow
     histosThisGroupPerSource[selection][leptonSource]['mt'     ]['loose'].Fill(mt,      weight)
     histosThisGroupPerSource[selection][leptonSource]['pt'     ]['loose'].Fill(pt,      weight)
     histosThisGroupPerSource[selection][leptonSource]['eta'    ]['loose'].Fill(eta,     weight)
     histosThisGroupPerSource[selection][leptonSource]['mdeltar']['loose'].Fill(mdeltar, weight)
     histosThisGroupPerSource[selection][leptonSource]['pt_eta' ]['loose'].Fill(pt, eta, weight)
Пример #5
0
def count_and_fill(chain, sample='', syst='', verbose=False, debug=False, blinded=True,
                   onthefly_tight_def=None, tightight=False, quicktest=False,
                   cached_cut=None, noncached_cuts=[]):
    """
    count and fill for one sample (or group), one syst.
    """
    sysGroup = systUtils.Group(sample).setSyst(syst)
    is_mc = systUtils.Group(sample).isMc
    is_data = systUtils.Group(sample).isData
    is_qflip_sample = dataset.DatasetGroup(sample).is_qflip
    assert bool(cached_cut) != bool(noncached_cuts),"must choose either cached selection or non-cached selections: {}, {}".format(cached_cut, noncached_cuts)
    cuts = [cached_cut] if cached_cut else noncached_cuts
    if noncached_cuts:
        chain.preselect(None)
    selections = [c.GetName() for c in cuts]
    counters = book_counters(selections)
    histos = book_histograms(sample_name=sample, variables=variables_to_fill(),
                             systematics=[syst], selections=selections
                             )[syst]
    if is_qflip_sample : # for qflip, only fill ss histos
        selections = [s for s in selections if s.endswith('_ss')]
    weight_expr = 'event.pars.weight'
    weight_expr = sysGroup.weightLeafname
    qflip_expr = 'event.pars.qflipWeight'
    print 'weight_expr: ',weight_expr
    print 'selections: ','\n'.join(["%d) %s : %s"%(i, cut.GetName(), cut.GetTitle()) for i, cut in enumerate(cuts)])
    start_time = time.clock()
    num_total_entries = chain.GetEntries()
    num_processed_entries = 0
    fields_to_print = ['l0_pt', 'l1_pt', 'l0_eta', 'l1_eta',
                       'met_pt',
                       'm_ll', 'pt_ll', 'dpt_l0_l1',
                       'dphi_l0_met', 'dphi_l1_met', 'dphi_l0_l1',
                       'mt0', 'mt1',
                       'n_soft_jets',
                       'eta_csj0', 'phi_csj0', 'eta_csj1', 'phi_csj1']
    if debug : print ",".join(fields_to_print)
    for iEntry, event in enumerate(chain):
        if quicktest and 100*iEntry > num_total_entries: break
        run_num = event.pars.runNumber
        evt_num = event.pars.eventNumber
        l0 = addTlv(event.l0)
        l1 = addTlv(event.l1)
        met = addTlv(event.met)
        l0_is_el, l0_is_mu = l0.isEl, l0.isMu
        l1_is_el, l1_is_mu = l1.isEl, l1.isMu
        l0_is_t = onthefly_tight_def(l0) if onthefly_tight_def else l0.isTight
        l1_is_t = onthefly_tight_def(l1) if onthefly_tight_def else l1.isTight
        is_emu = int(l0_is_el and l1_is_mu)
        is_mue = int(l0_is_mu and l1_is_el)
        is_mumu = int(l0_is_mu and l1_is_mu)
        is_ee = int(l0_is_el and l1_is_el)
        is_same_sign = int((l0.charge * l1.charge)>0)
        is_opp_sign  = not is_same_sign
        is_qflippable = is_opp_sign and (l0_is_el or l1_is_el) and is_mc
        weight = eval(weight_expr)
        qflip_prob = eval(qflip_expr)
        # print "event : same sign {0}, opp_sign {1}, qflippable {2}, qflip_prob {3}".format(is_same_sign, is_opp_sign, is_qflippable, eval(qflip_expr))
        l0_pt, l1_pt = l0.p4.Pt(), l1.p4.Pt()
        d_pt0_pt1 = l0_pt - l1_pt
        l0_eta, l1_eta = l0.p4.Eta(), l1.p4.Eta()
        l0_phi, l1_phi = l0.p4.Phi(), l1.p4.Phi()
        met_pt = met.p4.Pt()
        m_ll = (l0.p4 + l1.p4).M()
        pt_ll = (l0.p4 + l1.p4).Pt()
        dphi_l0_met = abs(l0.p4.DeltaPhi(met.p4))
        dphi_l1_met = abs(l1.p4.DeltaPhi(met.p4))
        dphi_l0_l1 = abs(l0.p4.DeltaPhi(l1.p4))
        dpt_l0_l1 = l0.p4.Pt()-l1.p4.Pt()
        m_coll = computeCollinearMassLepTau(l0.p4, l1.p4, met.p4)
        mt0, mt1 = computeMt(l0.p4, met.p4), computeMt(l1.p4, met.p4)
        dphillbeta, mdr = computeRazor(l0.p4, l1.p4, met.p4)
        def jet_pt2(j) : return j.px*j.px+j.py*j.py
        cl_jets   = [addTlv(j) for j in event.jets if jet_pt2(j)>30.*30.]
        n_cl_jets = len(cl_jets)
        n_b_jets  = event.pars.numBjets
        n_f_jets  = event.pars.numFjets
        n_bf_jets = n_b_jets + n_f_jets
        n_jets = n_cl_jets + n_b_jets + n_f_jets
        # n_jets = event.pars.numFjets + event.pars.numBjets
        soft_jets = [addTlv(j) for j in event.jets if jet_pt2(j)<30.**2] # todo: merge with cl_jets loop
        n_soft_jets = len(soft_jets)
        csj0 = first(sorted(soft_jets, key=lambda j : j.p4.DeltaR(l0.p4)))
        csj1 = first(sorted(soft_jets, key=lambda j : j.p4.DeltaR(l1.p4)))
        eta_csj0 = csj0.p4.Eta() if csj0 else -5.0
        phi_csj0 = csj0.p4.Phi() if csj0 else -5.0
        eta_csj1 = csj1.p4.Eta() if csj1 else -5.0
        phi_csj1 = csj1.p4.Phi() if csj1 else -5.0
        drl0csj  = csj0.p4.DeltaR(l0.p4) if csj0 else None
        drl1csj  = csj1.p4.DeltaR(l1.p4) if csj1 else None
        m_jj     = (cl_jets[0].p4 + cl_jets[1].p4).M() if n_cl_jets>1 else None
        deta_jj  = abs(cl_jets[0].p4.Eta() - cl_jets[1].p4.Eta()) if n_cl_jets>1 else None
        pass_sels = {}
        if tightight and not (l0_is_t and l1_is_t) : continue

        for cut in cuts:
            sel = cut.GetName()
            sel_expr = cut.GetTitle()
            pass_sel = eval(sel_expr)# and (l0_pt>60.0 and dphi_l1_met<0.7)
            pass_sels[sel] = pass_sel
            is_ss_sel = sel.endswith('_ss')
            as_qflip = is_qflippable and (is_opp_sign and is_ss_sel)
            if is_qflip_sample and not as_qflip : pass_sel = False
            if not is_qflip_sample and as_qflip : pass_sel = False
            if not pass_sel : continue
            if pass_sel and not cached_cut : chain.add_entry_to_list(cut, iEntry)
            # <isElectron 1> <isElectron 2> <isTight 1> <isTight 2> <pt 1> <pt 2> <eta 1> <eta 2>
            lltype = "{0}{1}".format('e' if l0_is_el else 'mu', 'e' if l1_is_el else 'mu')
            qqtype = "{0}{1}".format('T' if l0_is_t else 'L', 'T' if l1_is_t else 'L')
            if debug : print ','.join([str(eval(_)) for _ in fields_to_print])
            def fmt(b) : return '1' if b else '0'
            # --- begin dbg
            # print "event: {0:12s} {1} {2} {3} {4} {5} {6} {7} {8}".format(lltype+' '+qqtype, #+' '+sel,
            #                                                               fmt(l0_is_el), fmt(l1_is_el),
            #                                                               fmt(l0_is_t), fmt(l1_is_t),
            #                                                               l0_pt, l1_pt,
            #                                                               l0.p4.Eta(), l1.p4.Eta())
            # print "event: {0:12s} {1} {2} {3:.2f} {4:.2f}".format(lltype+' '+qqtype+' '+sel,
            #                                                       run_num, evt_num,
            #                                                       l0_pt, l1_pt)
            # --- end dbg
            fill_weight = (weight * qflip_prob) if as_qflip else weight
            h = histos[sel]
            h['onebin'   ].Fill(1.0, fill_weight)
            h['njets'    ].Fill(n_jets, fill_weight)
            h['pt0'      ].Fill(l0_pt, fill_weight)
            h['pt1'      ].Fill(l1_pt, fill_weight)
            h['d_pt0_pt1'].Fill(d_pt0_pt1, fill_weight)
            h['eta0'     ].Fill(l0_eta, fill_weight)
            h['eta1'     ].Fill(l1_eta, fill_weight)
            h['phi0'     ].Fill(l0_phi, fill_weight)
            h['phi1'     ].Fill(l1_phi, fill_weight)
            h['mll'      ].Fill(m_ll, fill_weight)
            h['ptll'     ].Fill(pt_ll, fill_weight)
            h['met'      ].Fill(met_pt, fill_weight)
            h['dphil0met'].Fill(dphi_l0_met, fill_weight)
            h['dphil1met'].Fill(dphi_l1_met, fill_weight)
            h['nsj'          ].Fill(n_soft_jets,   fill_weight)
            h['pt0_vs_pt1'      ].Fill(l1_pt, l0_pt,       fill_weight)
            h['met_vs_pt1'      ].Fill(l1_pt, met.p4.Pt(), fill_weight)
            h['dphil0l1_vs_pt1' ].Fill(l1_pt, dphi_l0_l1,  fill_weight)
            h['dphil0met_vs_pt1'].Fill(l1_pt, dphi_l0_met, fill_weight)
            h['dphil1met_vs_pt1'].Fill(l1_pt, dphi_l1_met, fill_weight)
            if n_soft_jets:
                h['drl0csj'].Fill(drl0csj, fill_weight)
                h['drl1csj'].Fill(drl1csj, fill_weight)
            if n_jets==2 and n_cl_jets==2: # fixme: f jets are not saved, but we need them for vbf
                h['m_jj'   ].Fill(m_jj,    fill_weight)
                h['deta_jj'].Fill(deta_jj, fill_weight)
            if is_data and (blinded and 100.0<m_coll and m_coll<150.0) : pass
            else :
                h['mcoll'].Fill(m_coll, fill_weight)
                h['mcollcoarse'].Fill(m_coll, fill_weight)
                h['mcoll_vs_pt1'].Fill(l1_pt, m_coll, fill_weight)
            counters[sel] += (fill_weight)
        # print ('e' if l0_is_el else 'm'),('e' if l1_is_el else 'm'),' : ',
        # print ' is_opp_sign: ',is_opp_sign,
        # print ' is_qflippable: ',is_qflippable,
        # print pass_sels
        num_processed_entries += 1
    end_time = time.clock()
    delta_time = end_time - start_time
    if verbose:
        print ("processed {0:d} entries ".format(num_processed_entries)
               +"in "+("{0:d} min ".format(int(delta_time/60)) if delta_time>60 else
                       "{0:.1f} s ".format(delta_time))
               +"({0:.1f} kHz)".format((num_processed_entries/delta_time) if delta_time else 1.0e9)
               )
    if verbose:
        for v in ['onebin']: #, 'pt0', 'pt1']:
            for sel in selections:
                h = histos[sel][v]
                print "{0}: integral {1}, entries {2}".format(h.GetName(), h.Integral(), h.GetEntries())
    return counters, histos
Пример #6
0
def count_and_fill(chain,
                   sample='',
                   syst='',
                   verbose=False,
                   debug=False,
                   blinded=True,
                   onthefly_tight_def=None,
                   tightight=False,
                   quicktest=False,
                   cached_cut=None,
                   noncached_cuts=[]):
    """
    count and fill for one sample (or group), one syst.
    """
    sysGroup = systUtils.Group(sample).setSyst(syst)
    is_mc = systUtils.Group(sample).isMc
    is_data = systUtils.Group(sample).isData
    is_qflip_sample = dataset.DatasetGroup(sample).is_qflip
    assert bool(cached_cut) != bool(
        noncached_cuts
    ), "must choose either cached selection or non-cached selections: {}, {}".format(
        cached_cut, noncached_cuts)
    cuts = [cached_cut] if cached_cut else noncached_cuts
    if noncached_cuts:
        chain.preselect(None)
    selections = [c.GetName() for c in cuts]
    counters = book_counters(selections)
    histos = book_histograms(sample_name=sample,
                             variables=variables_to_fill(),
                             systematics=[syst],
                             selections=selections)[syst]
    if is_qflip_sample:  # for qflip, only fill ss histos
        selections = [s for s in selections if s.endswith('_ss')]
    weight_expr = 'event.pars.weight'
    weight_expr = sysGroup.weightLeafname
    qflip_expr = 'event.pars.qflipWeight'
    print 'weight_expr: ', weight_expr
    print 'selections: ', '\n'.join([
        "%d) %s : %s" % (i, cut.GetName(), cut.GetTitle())
        for i, cut in enumerate(cuts)
    ])
    start_time = time.clock()
    num_total_entries = chain.GetEntries()
    num_processed_entries = 0
    fields_to_print = [
        'l0_pt', 'l1_pt', 'l0_eta', 'l1_eta', 'met_pt', 'm_ll', 'pt_ll',
        'dpt_l0_l1', 'dphi_l0_met', 'dphi_l1_met', 'dphi_l0_l1', 'mt0', 'mt1',
        'n_soft_jets', 'eta_csj0', 'phi_csj0', 'eta_csj1', 'phi_csj1'
    ]
    if debug: print ",".join(fields_to_print)
    for iEntry, event in enumerate(chain):
        if quicktest and 100 * iEntry > num_total_entries: break
        run_num = event.pars.runNumber
        evt_num = event.pars.eventNumber
        l0 = addTlv(event.l0)
        l1 = addTlv(event.l1)
        met = addTlv(event.met)
        l0_is_el, l0_is_mu = l0.isEl, l0.isMu
        l1_is_el, l1_is_mu = l1.isEl, l1.isMu
        l0_is_t = onthefly_tight_def(l0) if onthefly_tight_def else l0.isTight
        l1_is_t = onthefly_tight_def(l1) if onthefly_tight_def else l1.isTight
        is_emu = int(l0_is_el and l1_is_mu)
        is_mue = int(l0_is_mu and l1_is_el)
        is_mumu = int(l0_is_mu and l1_is_mu)
        is_ee = int(l0_is_el and l1_is_el)
        is_same_sign = int((l0.charge * l1.charge) > 0)
        is_opp_sign = not is_same_sign
        is_qflippable = is_opp_sign and (l0_is_el or l1_is_el) and is_mc
        weight = eval(weight_expr)
        qflip_prob = eval(qflip_expr)
        # print "event : same sign {0}, opp_sign {1}, qflippable {2}, qflip_prob {3}".format(is_same_sign, is_opp_sign, is_qflippable, eval(qflip_expr))
        l0_pt, l1_pt = l0.p4.Pt(), l1.p4.Pt()
        d_pt0_pt1 = l0_pt - l1_pt
        l0_eta, l1_eta = l0.p4.Eta(), l1.p4.Eta()
        l0_phi, l1_phi = l0.p4.Phi(), l1.p4.Phi()
        met_pt = met.p4.Pt()
        m_ll = (l0.p4 + l1.p4).M()
        pt_ll = (l0.p4 + l1.p4).Pt()
        dphi_l0_met = abs(l0.p4.DeltaPhi(met.p4))
        dphi_l1_met = abs(l1.p4.DeltaPhi(met.p4))
        dphi_l0_l1 = abs(l0.p4.DeltaPhi(l1.p4))
        dpt_l0_l1 = l0.p4.Pt() - l1.p4.Pt()
        m_coll = computeCollinearMassLepTau(l0.p4, l1.p4, met.p4)
        mt0, mt1 = computeMt(l0.p4, met.p4), computeMt(l1.p4, met.p4)
        dphillbeta, mdr = computeRazor(l0.p4, l1.p4, met.p4)

        def jet_pt2(j):
            return j.px * j.px + j.py * j.py

        cl_jets = [addTlv(j) for j in event.jets if jet_pt2(j) > 30. * 30.]
        n_cl_jets = len(cl_jets)
        n_b_jets = event.pars.numBjets
        n_f_jets = event.pars.numFjets
        n_bf_jets = n_b_jets + n_f_jets
        n_jets = n_cl_jets + n_b_jets + n_f_jets
        # n_jets = event.pars.numFjets + event.pars.numBjets
        soft_jets = [addTlv(j) for j in event.jets
                     if jet_pt2(j) < 30.**2]  # todo: merge with cl_jets loop
        n_soft_jets = len(soft_jets)
        csj0 = first(sorted(soft_jets, key=lambda j: j.p4.DeltaR(l0.p4)))
        csj1 = first(sorted(soft_jets, key=lambda j: j.p4.DeltaR(l1.p4)))
        eta_csj0 = csj0.p4.Eta() if csj0 else -5.0
        phi_csj0 = csj0.p4.Phi() if csj0 else -5.0
        eta_csj1 = csj1.p4.Eta() if csj1 else -5.0
        phi_csj1 = csj1.p4.Phi() if csj1 else -5.0
        drl0csj = csj0.p4.DeltaR(l0.p4) if csj0 else None
        drl1csj = csj1.p4.DeltaR(l1.p4) if csj1 else None
        m_jj = (cl_jets[0].p4 + cl_jets[1].p4).M() if n_cl_jets > 1 else None
        deta_jj = abs(cl_jets[0].p4.Eta() -
                      cl_jets[1].p4.Eta()) if n_cl_jets > 1 else None
        pass_sels = {}
        if tightight and not (l0_is_t and l1_is_t): continue

        for cut in cuts:
            sel = cut.GetName()
            sel_expr = cut.GetTitle()
            pass_sel = eval(sel_expr)  # and (l0_pt>60.0 and dphi_l1_met<0.7)
            pass_sels[sel] = pass_sel
            is_ss_sel = sel.endswith('_ss')
            as_qflip = is_qflippable and (is_opp_sign and is_ss_sel)
            if is_qflip_sample and not as_qflip: pass_sel = False
            if not is_qflip_sample and as_qflip: pass_sel = False
            if not pass_sel: continue
            if pass_sel and not cached_cut:
                chain.add_entry_to_list(cut, iEntry)
            # <isElectron 1> <isElectron 2> <isTight 1> <isTight 2> <pt 1> <pt 2> <eta 1> <eta 2>
            lltype = "{0}{1}".format('e' if l0_is_el else 'mu',
                                     'e' if l1_is_el else 'mu')
            qqtype = "{0}{1}".format('T' if l0_is_t else 'L',
                                     'T' if l1_is_t else 'L')
            if debug: print ','.join([str(eval(_)) for _ in fields_to_print])

            def fmt(b):
                return '1' if b else '0'

            # --- begin dbg
            # print "event: {0:12s} {1} {2} {3} {4} {5} {6} {7} {8}".format(lltype+' '+qqtype, #+' '+sel,
            #                                                               fmt(l0_is_el), fmt(l1_is_el),
            #                                                               fmt(l0_is_t), fmt(l1_is_t),
            #                                                               l0_pt, l1_pt,
            #                                                               l0.p4.Eta(), l1.p4.Eta())
            # print "event: {0:12s} {1} {2} {3:.2f} {4:.2f}".format(lltype+' '+qqtype+' '+sel,
            #                                                       run_num, evt_num,
            #                                                       l0_pt, l1_pt)
            # --- end dbg
            fill_weight = (weight * qflip_prob) if as_qflip else weight
            h = histos[sel]
            h['onebin'].Fill(1.0, fill_weight)
            h['njets'].Fill(n_jets, fill_weight)
            h['pt0'].Fill(l0_pt, fill_weight)
            h['pt1'].Fill(l1_pt, fill_weight)
            h['d_pt0_pt1'].Fill(d_pt0_pt1, fill_weight)
            h['eta0'].Fill(l0_eta, fill_weight)
            h['eta1'].Fill(l1_eta, fill_weight)
            h['phi0'].Fill(l0_phi, fill_weight)
            h['phi1'].Fill(l1_phi, fill_weight)
            h['mll'].Fill(m_ll, fill_weight)
            h['ptll'].Fill(pt_ll, fill_weight)
            h['met'].Fill(met_pt, fill_weight)
            h['dphil0met'].Fill(dphi_l0_met, fill_weight)
            h['dphil1met'].Fill(dphi_l1_met, fill_weight)
            h['nsj'].Fill(n_soft_jets, fill_weight)
            h['pt0_vs_pt1'].Fill(l1_pt, l0_pt, fill_weight)
            h['met_vs_pt1'].Fill(l1_pt, met.p4.Pt(), fill_weight)
            h['dphil0l1_vs_pt1'].Fill(l1_pt, dphi_l0_l1, fill_weight)
            h['dphil0met_vs_pt1'].Fill(l1_pt, dphi_l0_met, fill_weight)
            h['dphil1met_vs_pt1'].Fill(l1_pt, dphi_l1_met, fill_weight)
            if n_soft_jets:
                h['drl0csj'].Fill(drl0csj, fill_weight)
                h['drl1csj'].Fill(drl1csj, fill_weight)
            if n_jets == 2 and n_cl_jets == 2:  # fixme: f jets are not saved, but we need them for vbf
                h['m_jj'].Fill(m_jj, fill_weight)
                h['deta_jj'].Fill(deta_jj, fill_weight)
            if is_data and (blinded and 100.0 < m_coll and m_coll < 150.0):
                pass
            else:
                h['mcoll'].Fill(m_coll, fill_weight)
                h['mcollcoarse'].Fill(m_coll, fill_weight)
                h['mcoll_vs_pt1'].Fill(l1_pt, m_coll, fill_weight)
            counters[sel] += (fill_weight)
        # print ('e' if l0_is_el else 'm'),('e' if l1_is_el else 'm'),' : ',
        # print ' is_opp_sign: ',is_opp_sign,
        # print ' is_qflippable: ',is_qflippable,
        # print pass_sels
        num_processed_entries += 1
    end_time = time.clock()
    delta_time = end_time - start_time
    if verbose:
        print(
            "processed {0:d} entries ".format(num_processed_entries) + "in " +
            ("{0:d} min ".format(int(delta_time / 60))
             if delta_time > 60 else "{0:.1f} s ".format(delta_time)) +
            "({0:.1f} kHz)".format((num_processed_entries /
                                    delta_time) if delta_time else 1.0e9))
    if verbose:
        for v in ['onebin']:  #, 'pt0', 'pt1']:
            for sel in selections:
                h = histos[sel][v]
                print "{0}: integral {1}, entries {2}".format(
                    h.GetName(), h.Integral(), h.GetEntries())
    return counters, histos
def fillHistos(chain, histosThisGroup, histosPerSource, histosThisGroupPerSource,
               lepton, group, cut, cut_is_cached, onthefly_tight_def=None, verbose=False):
    nLoose, nTight = 0, 0
    totWeightLoose, totWeightTight = 0.0, 0.0
    normFactor = 1.0 if group=='heavyflavor' else 1.0 # bb/cc hand-waving normalization factor, see notes 2014-04-17
    addTlv, computeMt = kin.addTlv, kin.computeMt
    region = cut.GetName()
    sel = cut.GetName()
    sel_expr = cut.GetTitle()
    print group
    isData = group.name=='data'
    isHflf = region=='hflf'
    print 'TODO: check isHflf'
    print 'TODO: check hasTrigmatch (already in ntuple creation?)'
    isConversion = region=='conv'
    if group=='heavyflavor':
        if lepton=='el' and not isConversion : normFactor = 1.6
        if lepton=='mu' :                      normFactor = 0.87
    num_processed_entries = 0
    sourceReal = r.hlfv.LeptonTruthType.Prompt
    for iEvent, event in enumerate(chain) :
        num_processed_entries += 1
        pars = event.pars
        weight, evtN, runN = pars.weight, pars.eventNumber, pars.runNumber
        hasTrigmatch = True # pars.has2ltrigmatch==1
        weight = weight*normFactor
        tag, probe, met = addTlv(event.l0), addTlv(event.l1), addTlv(event.met)
        if not (tag.isMu and probe.isMu) : continue
        isSameSign = tag.charge*probe.charge > 0.
        isRightProbe = probe.isEl if lepton=='el' else probe.isMu if lepton=='mu' else False
        if not isRightProbe and lepton=='el':
            tag, probe = probe, tag # if we are  picking emu instead of mue
            isRightProbe = probe.isEl if lepton=='el' else False

        tagIsTight = onthefly_tight_def(tag) if onthefly_tight_def else tag.isTight
        isTight = onthefly_tight_def(probe) if onthefly_tight_def else probe.isTight # todo: rename to probeIsTight
        probeSource = probe.source
        isReal = probeSource==sourceReal and not isData
        isFake = not isReal and not isData
        jets = event.jets
        jets = [addTlv(j) for j in jets] # only if needed
        def isBjet(j, mv1_80=0.3511) : return j.mv1 > mv1_80 # see SusyDefs.h
        hasBjets = any(isBjet(j) for j in jets) # compute only if necessary
        hasFjets = any(abs(j.p4.Eta())>2.4 and j.p4.Pt()>30. for j in jets)
        hasCLjets = any(abs(j.p4.Eta())<2.4 and j.p4.Pt()>30 and not isBjet(j) for j in jets)
        hasJ30jets = any(j.p4.Pt()>30. for j in jets)
        hasJets = hasBjets or hasFjets or hasCLjets
        tag4m, probe4m, met4m = r.TLorentzVector(), r.TLorentzVector(), r.TLorentzVector()
        tag4m.SetPxPyPzE(tag.px, tag.py, tag.pz, tag.E)
        probe4m.SetPxPyPzE(probe.px, probe.py, probe.pz, probe.E)
        met4m.SetPxPyPzE(met.px, met.py, met.pz, met.E)
        pt = probe4m.Pt()
        eta = abs(probe4m.Eta())
        mt0 = computeMt(tag4m, met4m)
        mt1 = computeMt(probe4m, met4m)
        pt0 = tag4m.Pt()
        pt1 = probe4m.Pt()
        passTrigBias =  True
        if   isHflf       : passTrigBias = pt0>20.0 and pt1>20.0
        elif isConversion : passTrigBias = pt1>20.0

        is_mumu = tag.isMu and probe.isMu
        is_same_sign = isSameSign
        is_opp_sign = not is_same_sign
        tag_is_mu = tag.isMu
        tag_is_tight = tagIsTight
        probe_is_tight = isTight
        probe_is_mu = probe.isMu
        probe_is_el = probe.isEl
        l0_is_el, l0_is_mu = event.l0.isEl, event.l0.isMu
        l1_is_el, l1_is_mu = event.l1.isEl, event.l1.isMu
        is_ee   = l0_is_el and l1_is_el
        is_emu  = l0_is_el and l1_is_mu
        is_mue  = l0_is_mu and l1_is_el
        is_mumu = l0_is_mu and l1_is_mu
        m_ll = (tag4m+probe4m).M()
        pass_sel = eval(sel_expr)
        if pass_sel and not cut_is_cached : chain.add_entry_to_list(cut, iEvent)
        # if tag.isMu and isRightProbe and isSameSign : # test 1 : no jet req
        # if tag.isMu and tag.isTight and isRightProbe and isSameSign : # test 2 : reproduce counts from hlfv
        def fillHistosBySource(probe):
            leptonSource = enum2source(probe)
            # if leptonSource=='Unknown':
            #     print 'unknown lep from ',group.name,' pt ',pt,' eta ',eta,' file ',chain.GetCurrentFile().GetName()
            def fillPerSource(tightOrLoose):
                histosPerSource         ['mt1' ][leptonSource][tightOrLoose].Fill(mt1, weight)
                histosPerSource         ['pt1' ][leptonSource][tightOrLoose].Fill(pt,  weight)
                histosPerSource         ['eta1'][leptonSource][tightOrLoose].Fill(eta, weight)
                histosThisGroupPerSource['mt1' ][leptonSource][tightOrLoose].Fill(mt1, weight)
                histosThisGroupPerSource['pt1' ][leptonSource][tightOrLoose].Fill(pt,  weight)
                histosThisGroupPerSource['eta1'][leptonSource][tightOrLoose].Fill(eta, weight)
                histosThisGroupPerSource['pt1_eta1'][leptonSource][tightOrLoose].Fill(pt, eta, weight)
            fillPerSource('loose')
            if isTight :
                fillPerSource('tight')
        def fill(lepType=''):
            histosThisGroup['mt0' ][lepType].Fill(mt0, weight)
            histosThisGroup['pt0' ][lepType].Fill(pt0, weight)
            histosThisGroup['mt1' ][lepType].Fill(mt1, weight)
            histosThisGroup['pt1' ][lepType].Fill(pt, weight)
            histosThisGroup['eta1'][lepType].Fill(eta, weight)
            histosThisGroup['pt1_eta1'][lepType].Fill(pt, eta, weight)

        sourceIsKnown = not isData
        nLoose         += 1
        totWeightLoose += weight
        if isTight:
            nTight         += 1
            totWeightTight += weight

        fill(lepType='loose')
        if sourceIsKnown:
            fillHistosBySource(probe)
        if isTight            : fill(lepType='tight')
        if isReal             : fill(lepType='real_loose')
        if isFake             : fill(lepType='fake_loose')
        if isReal and isTight : fill(lepType='real_tight')
        if isFake and isTight : fill(lepType='fake_tight')
    if verbose:
        counterNames = ['nLoose', 'nTight', 'totWeightLoose', 'totWeightTight']
        print ', '.join(["%s : %.1f"%(c, eval(c)) for c in counterNames])
    return num_processed_entries
def fillHistos(chain,
               histosThisGroup,
               histosPerSource,
               histosThisGroupPerSource,
               lepton,
               group,
               cut,
               cut_is_cached,
               onthefly_tight_def=None,
               verbose=False):
    nLoose, nTight = 0, 0
    totWeightLoose, totWeightTight = 0.0, 0.0
    normFactor = 1.0 if group == 'heavyflavor' else 1.0  # bb/cc hand-waving normalization factor, see notes 2014-04-17
    addTlv, computeMt = kin.addTlv, kin.computeMt
    region = cut.GetName()
    sel = cut.GetName()
    sel_expr = cut.GetTitle()
    print group
    isData = group.name == 'data'
    isHflf = region == 'hflf'
    print 'TODO: check isHflf'
    print 'TODO: check hasTrigmatch (already in ntuple creation?)'
    isConversion = region == 'conv'
    if group == 'heavyflavor':
        if lepton == 'el' and not isConversion: normFactor = 1.6
        if lepton == 'mu': normFactor = 0.87
    num_processed_entries = 0
    sourceReal = r.hlfv.LeptonTruthType.Prompt
    for iEvent, event in enumerate(chain):
        num_processed_entries += 1
        pars = event.pars
        weight, evtN, runN = pars.weight, pars.eventNumber, pars.runNumber
        hasTrigmatch = True  # pars.has2ltrigmatch==1
        weight = weight * normFactor
        tag, probe, met = addTlv(event.l0), addTlv(event.l1), addTlv(event.met)
        if not (tag.isMu and probe.isMu): continue
        isSameSign = tag.charge * probe.charge > 0.
        isRightProbe = probe.isEl if lepton == 'el' else probe.isMu if lepton == 'mu' else False
        if not isRightProbe and lepton == 'el':
            tag, probe = probe, tag  # if we are  picking emu instead of mue
            isRightProbe = probe.isEl if lepton == 'el' else False

        tagIsTight = onthefly_tight_def(
            tag) if onthefly_tight_def else tag.isTight
        isTight = onthefly_tight_def(
            probe
        ) if onthefly_tight_def else probe.isTight  # todo: rename to probeIsTight
        probeSource = probe.source
        isReal = probeSource == sourceReal and not isData
        isFake = not isReal and not isData
        jets = event.jets
        jets = [addTlv(j) for j in jets]  # only if needed

        def isBjet(j, mv1_80=0.3511):
            return j.mv1 > mv1_80  # see SusyDefs.h

        hasBjets = any(isBjet(j) for j in jets)  # compute only if necessary
        hasFjets = any(abs(j.p4.Eta()) > 2.4 and j.p4.Pt() > 30. for j in jets)
        hasCLjets = any(
            abs(j.p4.Eta()) < 2.4 and j.p4.Pt() > 30 and not isBjet(j)
            for j in jets)
        hasJ30jets = any(j.p4.Pt() > 30. for j in jets)
        hasJets = hasBjets or hasFjets or hasCLjets
        tag4m, probe4m, met4m = r.TLorentzVector(), r.TLorentzVector(
        ), r.TLorentzVector()
        tag4m.SetPxPyPzE(tag.px, tag.py, tag.pz, tag.E)
        probe4m.SetPxPyPzE(probe.px, probe.py, probe.pz, probe.E)
        met4m.SetPxPyPzE(met.px, met.py, met.pz, met.E)
        pt = probe4m.Pt()
        eta = abs(probe4m.Eta())
        mt0 = computeMt(tag4m, met4m)
        mt1 = computeMt(probe4m, met4m)
        pt0 = tag4m.Pt()
        pt1 = probe4m.Pt()
        passTrigBias = True
        if isHflf: passTrigBias = pt0 > 20.0 and pt1 > 20.0
        elif isConversion: passTrigBias = pt1 > 20.0

        is_mumu = tag.isMu and probe.isMu
        is_same_sign = isSameSign
        is_opp_sign = not is_same_sign
        tag_is_mu = tag.isMu
        tag_is_tight = tagIsTight
        probe_is_tight = isTight
        probe_is_mu = probe.isMu
        probe_is_el = probe.isEl
        l0_is_el, l0_is_mu = event.l0.isEl, event.l0.isMu
        l1_is_el, l1_is_mu = event.l1.isEl, event.l1.isMu
        is_ee = l0_is_el and l1_is_el
        is_emu = l0_is_el and l1_is_mu
        is_mue = l0_is_mu and l1_is_el
        is_mumu = l0_is_mu and l1_is_mu
        m_ll = (tag4m + probe4m).M()
        pass_sel = eval(sel_expr)
        if pass_sel and not cut_is_cached: chain.add_entry_to_list(cut, iEvent)

        # if tag.isMu and isRightProbe and isSameSign : # test 1 : no jet req
        # if tag.isMu and tag.isTight and isRightProbe and isSameSign : # test 2 : reproduce counts from hlfv
        def fillHistosBySource(probe):
            leptonSource = enum2source(probe)

            # if leptonSource=='Unknown':
            #     print 'unknown lep from ',group.name,' pt ',pt,' eta ',eta,' file ',chain.GetCurrentFile().GetName()
            def fillPerSource(tightOrLoose):
                histosPerSource['mt1'][leptonSource][tightOrLoose].Fill(
                    mt1, weight)
                histosPerSource['pt1'][leptonSource][tightOrLoose].Fill(
                    pt, weight)
                histosPerSource['eta1'][leptonSource][tightOrLoose].Fill(
                    eta, weight)
                histosThisGroupPerSource['mt1'][leptonSource][
                    tightOrLoose].Fill(mt1, weight)
                histosThisGroupPerSource['pt1'][leptonSource][
                    tightOrLoose].Fill(pt, weight)
                histosThisGroupPerSource['eta1'][leptonSource][
                    tightOrLoose].Fill(eta, weight)
                histosThisGroupPerSource['pt1_eta1'][leptonSource][
                    tightOrLoose].Fill(pt, eta, weight)

            fillPerSource('loose')
            if isTight:
                fillPerSource('tight')

        def fill(lepType=''):
            histosThisGroup['mt0'][lepType].Fill(mt0, weight)
            histosThisGroup['pt0'][lepType].Fill(pt0, weight)
            histosThisGroup['mt1'][lepType].Fill(mt1, weight)
            histosThisGroup['pt1'][lepType].Fill(pt, weight)
            histosThisGroup['eta1'][lepType].Fill(eta, weight)
            histosThisGroup['pt1_eta1'][lepType].Fill(pt, eta, weight)

        sourceIsKnown = not isData
        nLoose += 1
        totWeightLoose += weight
        if isTight:
            nTight += 1
            totWeightTight += weight

        fill(lepType='loose')
        if sourceIsKnown:
            fillHistosBySource(probe)
        if isTight: fill(lepType='tight')
        if isReal: fill(lepType='real_loose')
        if isFake: fill(lepType='fake_loose')
        if isReal and isTight: fill(lepType='real_tight')
        if isFake and isTight: fill(lepType='fake_tight')
    if verbose:
        counterNames = ['nLoose', 'nTight', 'totWeightLoose', 'totWeightTight']
        print ', '.join(["%s : %.1f" % (c, eval(c)) for c in counterNames])
    return num_processed_entries