def test_kernel_set_get_in_offsets(self): print("kernel_set_get_in_offsets") dataset = self.dataset muons = dataset.structs["Muon"][0] arr = muons.pt.copy() sel_ev = self.NUMPY_LIB.ones(muons.numevents(), dtype=self.NUMPY_LIB.bool) sel_mu = self.NUMPY_LIB.ones(muons.numobjects(), dtype=self.NUMPY_LIB.bool) inds = self.NUMPY_LIB.zeros(muons.numevents(), dtype=self.NUMPY_LIB.int8) #set the pt of the first muon in each event to 1 inds[:] = 0 target = self.NUMPY_LIB.ones(muons.numevents(), dtype=muons.pt.dtype) kernels.set_in_offsets(self.ha, muons.offsets, arr, inds, target, sel_ev, sel_mu) print("checking set_in_offsets") asnp = self.NUMPY_LIB.asnumpy self.assertTrue( verify_set_in_offsets(asnp(muons.offsets), asnp(inds), asnp(arr), asnp(target))) print("checking get_in_offsets") z = kernels.get_in_offsets(self.ha, muons.offsets, arr, inds, sel_ev, sel_mu) self.assertTrue( verify_get_in_offsets(asnp(muons.offsets), asnp(inds), asnp(arr), asnp(target), asnp(z))) return muons.numevents()
def run_analysis(dataset, out, dnnmodel, use_cuda, ismc): from keras.backend.tensorflow_backend import set_session this_worker = get_worker_wrapper() NUMPY_LIB = this_worker.NUMPY_LIB backend = this_worker.backend hists = {} histo_bins = { "nmu": np.array([0, 1, 2, 3], dtype=np.float32), "njet": np.array([0, 1, 2, 3, 4, 5, 6, 7], dtype=np.float32), "mu_pt": np.linspace(0, 300, 20), "mu_eta": np.linspace(-5, 5, 20), "mu_phi": np.linspace(-5, 5, 20), "mu_iso": np.linspace(0, 1, 20), "mu_charge": np.array([-1, 0, 1], dtype=np.float32), "met_pt": np.linspace(0, 200, 20), "jet_pt": np.linspace(0, 400, 20), "jet_eta": np.linspace(-5, 5, 20), "jet_phi": np.linspace(-5, 5, 20), "jet_btag": np.linspace(0, 1, 20), "dnnpred_m": np.linspace(0, 1, 20), "dnnpred_s": np.linspace(0, 0.2, 20), "inv_mass": np.linspace(150, 200, 20), "sumpt": np.linspace(0, 1000, 20), } t0 = time.time() i = 0 mu = dataset.structs["Muon"][i] el = dataset.structs["Electron"][i] jets = dataset.structs["Jet"][i] evvars = dataset.eventvars[i] mu.hepaccelerate_backend = backend el.hepaccelerate_backend = backend jets.hepaccelerate_backend = backend evs_all = NUMPY_LIB.ones(dataset.numevents(), dtype=NUMPY_LIB.bool) print("Lepton selection") sel_mu, sel_ev_mu = get_selected_muons(mu, 40, 20, 2.4, 0.1) sel_ev_mu = sel_ev_mu & (evvars["HLT_IsoMu24"] == True) mu.masks["selected"] = sel_mu sel_el, sel_ev_el = get_selected_electrons(el, 40, 20, 2.4, 0.1) el.masks["selected"] = sel_el nmu = ha_kernels.sum_in_offsets( backend, mu.offsets, mu.masks["selected"], evs_all, mu.masks["all"], dtype=NUMPY_LIB.int32, ) nel = ha_kernels.sum_in_offsets( backend, el.offsets, el.masks["selected"], evs_all, el.masks["all"], dtype=NUMPY_LIB.int32, ) # get contiguous arrays of the first two muons for all events mu1 = mu.select_nth(0, object_mask=sel_mu) mu2 = mu.select_nth(1, object_mask=sel_mu) el1 = el.select_nth(0, object_mask=sel_el) el2 = el.select_nth(1, object_mask=sel_el) weight_ev_mu = apply_lepton_corrections(mu, sel_mu, this_worker.electron_weights) weight_ev_el = apply_lepton_corrections(el, sel_el, this_worker.electron_weights) weights = {"nominal": weight_ev_mu * weight_ev_el} weights_jet = {} for k in weights.keys(): weights_jet[k] = NUMPY_LIB.zeros_like(jets.pt) ha_kernels.broadcast(backend, jets.offsets, weights["nominal"], weights_jet[k]) all_jecs = [("nominal", "", None)] if ismc: for i in range(this_worker.jecs_up.shape[1]): all_jecs += [(i, "up", this_worker.jecs_up[:, i])] all_jecs += [(i, "down", this_worker.jecs_down[:, i])] jets_pt_orig = NUMPY_LIB.copy(jets.pt) # per-event histograms fill_histograms_several( hists, "nominal", "hist__all__", [ (evvars["MET_pt"], "met_pt", histo_bins["met_pt"]), ], evs_all, weights, use_cuda, ) fill_histograms_several( hists, "nominal", "hist__all__", [ (jets.pt, "jets_pt", histo_bins["jet_pt"]), ], jets.masks["all"], weights_jet, use_cuda, ) print("Jet selection") # loop over the jet corrections for ijec, sdir, jec in all_jecs: systname = "nominal" if ijec != "nominal": systname = ("jec{0}".format(ijec), sdir) if not jec is None: jet_pt_corr = apply_jec(jets_pt_orig, this_worker.jecs_bins, jec) # compute the corrected jet pt jets.pt = jets_pt_orig * NUMPY_LIB.abs(jet_pt_corr) print("jec", ijec, sdir, jets.pt.mean()) # get selected jets sel_jet, sel_bjet = select_jets(jets, mu, el, sel_mu, sel_el, 40, 2.0, 0.3, 0.4) # compute the number of jets per event njet = ha_kernels.sum_in_offsets( backend, jets.offsets, sel_jet, evs_all, jets.masks["all"], dtype=NUMPY_LIB.int32, ) nbjet = ha_kernels.sum_in_offsets( backend, jets.offsets, sel_bjet, evs_all, jets.masks["all"], dtype=NUMPY_LIB.int32, ) inv_mass_3j = NUMPY_LIB.zeros(jets.numevents(), dtype=NUMPY_LIB.float32) best_comb_3j = NUMPY_LIB.zeros((jets.numevents(), 3), dtype=NUMPY_LIB.int32) if use_cuda: this_worker.kernels.comb_3_invmass_closest[32, 256]( jets.pt, jets.eta, jets.phi, jets.mass, jets.offsets, 172.0, inv_mass_3j, best_comb_3j, ) cuda.synchronize() else: this_worker.kernels.comb_3_invmass_closest( jets.pt, jets.eta, jets.phi, jets.mass, jets.offsets, 172.0, inv_mass_3j, best_comb_3j, ) best_btag = NUMPY_LIB.zeros(jets.numevents(), dtype=NUMPY_LIB.float32) if use_cuda: this_worker.kernels.max_val_comb[32, 1024](jets.btag, jets.offsets, best_comb_3j, best_btag) cuda.synchronize() else: this_worker.kernels.max_val_comb(jets.btag, jets.offsets, best_comb_3j, best_btag) # get the events with at least three jets sel_ev_jet = njet >= 3 sel_ev_bjet = nbjet >= 1 selected_events = (sel_ev_mu | sel_ev_el) & sel_ev_jet & sel_ev_bjet print("Selected {0} events".format(selected_events.sum())) # get contiguous vectors of the first two jet data jet1 = jets.select_nth(0, object_mask=sel_jet) jet2 = jets.select_nth(1, object_mask=sel_jet) jet3 = jets.select_nth(2, object_mask=sel_jet) # create a mask vector for the first two jets first_two_jets = NUMPY_LIB.zeros_like(sel_jet) inds = NUMPY_LIB.zeros_like(evs_all, dtype=NUMPY_LIB.int32) targets = NUMPY_LIB.ones_like(evs_all, dtype=NUMPY_LIB.int32) inds[:] = 0 ha_kernels.set_in_offsets( backend, jets.offsets, first_two_jets, inds, targets, selected_events, sel_jet, ) inds[:] = 1 ha_kernels.set_in_offsets( backend, jets.offsets, first_two_jets, inds, targets, selected_events, sel_jet, ) # compute the invariant mass of the first two jets dijet_inv_mass, dijet_pt = compute_inv_mass(jets, selected_events, sel_jet & first_two_jets, use_cuda) sumpt_jets = ha_kernels.sum_in_offsets(backend, jets.offsets, jets.pt, selected_events, sel_jet) # create a keras-like array arr = NUMPY_LIB.vstack([ nmu, nel, njet, dijet_inv_mass, dijet_pt, mu1["pt"], mu1["eta"], mu1["phi"], mu1["charge"], mu1["pfRelIso03_all"], mu2["pt"], mu2["eta"], mu2["phi"], mu2["charge"], mu2["pfRelIso03_all"], el1["pt"], el1["eta"], el1["phi"], el1["charge"], el1["pfRelIso03_all"], el2["pt"], el2["eta"], el2["phi"], el2["charge"], el2["pfRelIso03_all"], jet1["pt"], jet1["eta"], jet1["phi"], jet1["btag"], jet2["pt"], jet2["eta"], jet2["phi"], jet2["btag"], inv_mass_3j, best_btag, sumpt_jets, ]).T # print("evaluating DNN model") with this_worker.graph.as_default(): set_session(this_worker.session) pred = dnnmodel.eval(arr, use_cuda) pred = NUMPY_LIB.vstack(pred).T pred_m = NUMPY_LIB.mean(pred, axis=1) pred_s = NUMPY_LIB.std(pred, axis=1) fill_histograms_several( hists, systname, "hist__nmu1_njetge3_nbjetge1__", [ (pred_m, "pred_m", histo_bins["dnnpred_m"]), (pred_s, "pred_s", histo_bins["dnnpred_s"]), (nmu, "nmu", histo_bins["nmu"]), (nel, "nel", histo_bins["nmu"]), (njet, "njet", histo_bins["njet"]), (mu1["pt"], "mu1_pt", histo_bins["mu_pt"]), (mu1["eta"], "mu1_eta", histo_bins["mu_eta"]), (mu1["phi"], "mu1_phi", histo_bins["mu_phi"]), (mu1["charge"], "mu1_charge", histo_bins["mu_charge"]), (mu1["pfRelIso03_all"], "mu1_iso", histo_bins["mu_iso"]), (mu2["pt"], "mu2_pt", histo_bins["mu_pt"]), (mu2["eta"], "mu2_eta", histo_bins["mu_eta"]), (mu2["phi"], "mu2_phi", histo_bins["mu_phi"]), (mu2["charge"], "mu2_charge", histo_bins["mu_charge"]), (mu2["pfRelIso03_all"], "mu2_iso", histo_bins["mu_iso"]), (el1["pt"], "el1_pt", histo_bins["mu_pt"]), (el1["eta"], "el1_eta", histo_bins["mu_eta"]), (el1["phi"], "el1_phi", histo_bins["mu_phi"]), (el1["charge"], "el1_charge", histo_bins["mu_charge"]), (el1["pfRelIso03_all"], "el1_iso", histo_bins["mu_iso"]), (el2["pt"], "el2_pt", histo_bins["mu_pt"]), (el2["eta"], "el2_eta", histo_bins["mu_eta"]), (el2["phi"], "el2_phi", histo_bins["mu_phi"]), (el2["charge"], "el2_charge", histo_bins["mu_charge"]), (el2["pfRelIso03_all"], "el2_iso", histo_bins["mu_iso"]), (jet1["pt"], "j1_pt", histo_bins["jet_pt"]), (jet1["eta"], "j1_eta", histo_bins["jet_eta"]), (jet1["phi"], "j1_phi", histo_bins["jet_phi"]), (jet1["btag"], "j1_btag", histo_bins["jet_btag"]), (jet2["pt"], "j2_pt", histo_bins["jet_pt"]), (jet2["eta"], "j2_eta", histo_bins["jet_eta"]), (jet2["phi"], "j2_phi", histo_bins["jet_phi"]), (jet2["btag"], "j2_btag", histo_bins["jet_btag"]), (inv_mass_3j, "inv_mass_3j", histo_bins["inv_mass"]), (best_btag, "best_btag", histo_bins["jet_btag"]), (sumpt_jets, "sumpt", histo_bins["sumpt"]), ], selected_events, weights, use_cuda, ) # save the array for the first jet correction scenario only if save_arrays and ijec == 0: outfile_arr = "{0}_arrs.npy".format(out) print("Saving array with shape {0} to {1}".format( arr.shape, outfile_arr)) with open(outfile_arr, "wb") as fi: np.save(fi, NUMPY_LIB.asnumpy(arr)) t1 = time.time() res = Results({}) for hn in hists.keys(): hists[hn] = Results(hists[hn]) res["hists"] = Results(hists) res["numevents"] = dataset.numevents() speed = dataset.numevents() / (t1 - t0) print("run_analysis: {0:.2E} events in {1:.2f} seconds, speed {2:.2E} Hz". format(dataset.numevents(), t1 - t0, speed)) return res