def fillHistos(chain, histosThisGroupPerSource, isData, lepton, group, selection, verbose=False): "expect histos[sel][source][var][loose,tight]" normFactor = 3.2 if group=='heavyflavor' else 1.0 # bb/cc hand-waving normalization factor, see notes 2014-04-17 nLepFilled = 0 num_processed_entries = 0 for event in chain : num_processed_entries += 1 pars = event.pars weight, evtN, runN = pars.weight, pars.eventNumber, pars.runNumber weight = weight*normFactor l0, l1 = kin.addTlv(event.l0), kin.addTlv(event.l1) met = kin.addTlv(event.met) jets = [kin.addTlv(j) for j in event.jets] dphi_ll_vBetaT, mdeltar = kin.computeRazor(l0.p4, l1.p4, met.p4) sourceReal = 3 # see FakeLeptonSources.h l0IsFake = l0.source!=sourceReal and not isData l1IsFake = l0.source!=sourceReal and not isData atLeastOneIsFake = l0IsFake or l1IsFake if not atLeastOneIsFake : continue is_same_sign = int((l0.charge * l1.charge)>0) is_opp_sign = not is_same_sign l0_pt, l1_pt = l0.p4.Pt(), 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)) pass_selection = (is_opp_sign and l0_pt>45.0 and l1_pt>12.0) # and # dphi_l0_l1>2.3 and dphi_l1_met<0.7 and dphi_l0_met>2.5 # and (l0_pt-l1_pt)>7.0) if not pass_selection : continue def fillHistosBySource(lep): isTight = lep.isTight source = lep.source leptonSource = enum2source(lep) isReal = source==sourceReal and not isData isFake = not isReal and not isData sourceIsKnown = not isData isRightLep = lep.isMu and lepton=='mu' or lep.isEl and lepton=='el' 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) filled = False # if isRightLep and sourceIsKnown and isFake: # DG-2014-08-08: pt cut still needed? and lep.p4.Pt()>20.0: if isRightLep and sourceIsKnown and isFake: # and mdeltar>20.0: # DG-2014-10-17: try razor mod sel fill() filled = True return filled if fillHistosBySource(l0) : nLepFilled += 1 if fillHistosBySource(l1) : nLepFilled += 1 if verbose : print "filled histos for %d leptons"%nLepFilled return num_processed_entries
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 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