def fill_histos_data(tree_name, mode="nue"): f_data = ROOT.TFile("root_files_sys/%s_%s_file.root" % (tree_name, variation)) t_data = f_data.Get("%s_tree" % tree_name) ROOT.TMVA.Tools.Instance() reader = ROOT.TMVA.Reader(":".join(["!V", "!Silent", "Color"])) variables_dict = load_variables(t_data) load_bdt(reader) histograms = [] for i, n in enumerate(variables_dict.keys()): if n != "reco_energy": h = ROOT.TH1F("h_%s" % n, labels[n], binning[n][0], binning[n][1], binning[n][2]) else: h = ROOT.TH1F("h_%s" % n, labels[n], len(bins) - 1, bins) histograms.append(h) histo_dict = dict(zip(variables_dict.keys(), histograms)) h_bdts = {} for bdt_name in bdt_types: h_bdts[bdt_name] = ROOT.TH1F( "h_bdt_%s_%s" % (bdt_name, tree_name), "BDT %s response; N. Entries / 0.04" % bdt_name, 50, -1, 1) passed_events = 0 entries = int(t_data.GetEntries() / 1) # bdt_ntuple = ROOT.TNtuple( # "bdt_ntuple", "bdt_ntuple", "single"+":".join(bdt_types) + ":category") f_clone = ROOT.TFile("root_files_sys/filtered_%s.root" % tree_name, "RECREATE") clone = t_data.CloneTree(0) for i_evt in tqdm(range(entries)): t_data.GetEntry(i_evt) bdt_values = {} for bdt_name in bdt_types: bdt_values[bdt_name] = reader.EvaluateMVA("BDT%s" % bdt_name) numu = mode == "numu" if pre_cuts(variables_dict, numu): for bdt_name in bdt_types: h_bdts[bdt_name].Fill(bdt_values[bdt_name], t_data.event_weight) if apply_cuts(bdt_values, variables_dict, BDT, MANUAL, mode): passed_events += t_data.event_weight all_vars = variables + spectators clone.Fill() for name, var in all_vars: for v in var: if v == -999: break histo_dict[name].Fill(v, t_data.event_weight) clone.Write() f_clone.Close() f_bdt = ROOT.TFile("plots/h_bdt_%s.root" % tree_name, "RECREATE") for h in h_bdts: h_bdts[h].Write() f_bdt.Close() for h in histograms: f = ROOT.TFile( "%splots%s/%s_%s.root" % (detsyst, folder, h.GetName(), tree_name), "RECREATE") h.Write() f.Close() return passed_events
def fill_histos(chain, histo_dict, h_bdt_types, option="", mode="nue"): ROOT.TMVA.Tools.Instance() reader = ROOT.TMVA.Reader(":".join(["!V", "!Silent", "Color"])) bdt_ntuple = ROOT.TNtuple("bdt_ntuple", "bdt_ntuple", "single" + ":".join(bdt_types) + ":category") variables_dict = load_variables(chain) load_bdt(reader) passed_events = 0 entries = int(chain.GetEntries() / 1) f = open("selected_events/%s_passed.txt" % option, "w") clone = chain.GetTree().CloneTree(0) for i in tqdm(range(entries)): chain.GetEntry(i) category = int(chain.category) # cuts_response = reader.EvaluateMVA("Cuts method", rectangular_cut) # likelihood_response = reader.EvaluateMVA("Likelihood method") bdts = {} for bdt_name in bdt_types: bdts[bdt_name] = reader.EvaluateMVA("BDT%s" % bdt_name) numu = mode == "numu" if pre_cuts(variables_dict, numu): for bdt_name in bdt_types: h_bdt_types[bdt_name][category].Fill(bdts[bdt_name], chain.event_weight) else: continue bdt_ntuple.Fill(array("f", list(bdts.values()) + [chain.category])) if apply_cuts(bdts, variables_dict, BDT, MANUAL, mode): if not chain.true_nu_is_fidvol and category != 0 and category != 6 and category != 1 and category != 7: category = 5 clone.Fill() print(int(chain.run), int(chain.subrun), int(chain.event), int(category), chain.reco_energy, file=f) if option == "nue": if category == 2: passed_events += chain.event_weight else: passed_events += chain.event_weight all_vars = variables + spectators var_dict = dict(all_vars) for name, var in all_vars: for i_v, v in enumerate(var): if name == "interaction_type" and v == 10: v = 4 if v == -999: break pdg_code = 99999 if "track" in name and name != "no_tracks": pdg_code = int(var_dict["track_pdg"][i_v]) if option != "bnbext" and abs(pdg_code) == 2147483648: pdg_code = int(var_dict["shower_pdg"][0]) elif "shower" in name: pdg_code = int(var_dict["shower_pdg"][i_v]) if option != "bnbext" and abs(pdg_code) == 2147483648: pdg_code = int(var_dict["track_pdg"][0]) if abs(pdg_code) in h_pdgs[name]: if MANUAL and category == 2: h_pdgs[name][abs(pdg_code)].Fill( v, chain.event_weight * 3.3 / 2.9) else: h_pdgs[name][abs(pdg_code)].Fill( v, chain.event_weight) else: h_pdgs[name][99999].Fill(v, chain.event_weight) if MANUAL and category == 2: histo_dict[name][category].Fill( v, chain.event_weight * 3.3 / 2.9) else: histo_dict[name][category].Fill(v, chain.event_weight) f.close() f = ROOT.TFile("plots/bdt_ntuple_%s.root" % option, "RECREATE") clone.Write() bdt_ntuple.Write() f.Close() return passed_events
def fill_histos(chain, histo_dict, h_bdt_types, option="", mode="nue"): ROOT.TMVA.Tools.Instance() reader = ROOT.TMVA.Reader(":".join(["!V", "!Silent", "Color"])) bdt_ntuple = ROOT.TNtuple("bdt_ntuple", "bdt_ntuple", "single" + ":".join(bdt_types) + ":category") variables_dict = load_variables(chain) load_bdt(reader) passed_events = 0 entries = int(chain.GetEntries() / 1) for i in tqdm(range(entries)): chain.GetEntry(i) category = int(chain.category) # cuts_response = reader.EvaluateMVA("Cuts method", rectangular_cut) # likelihood_response = reader.EvaluateMVA("Likelihood method") bdts = {} for bdt_name in bdt_types: bdts[bdt_name] = reader.EvaluateMVA("BDT%s" % bdt_name) numu = mode == "numu" if pre_cuts(variables_dict, numu): for bdt_name in bdt_types: h_bdt_types[bdt_name][category].Fill(bdts[bdt_name], chain.event_weight) else: continue if apply_cuts(bdts, variables_dict, BDT, MANUAL, mode): if not chain.true_nu_is_fidvol and category != 0 and category != 6 and category != 1 and category != 7: category = 5 if option == "nue": if category == 2: passed_events += chain.event_weight else: passed_events += chain.event_weight all_vars = variables + spectators var_dict = dict(all_vars) for name, var in all_vars: for i_v, v in enumerate(var): if v == -999: break if "track" in name and name != "no_tracks": pdg_code = int(var_dict["track_pdg"][i_v]) if option != "bnbext" and abs(pdg_code) == 2147483648: pdg_code = int(var_dict["shower_pdg"][0]) elif "shower" in name: pdg_code = int(var_dict["shower_pdg"][i_v]) if option != "bnbext" and abs(pdg_code) == 2147483648: pdg_code = int(var_dict["track_pdg"][0]) else: pdg_code = int(var_dict["shower_pdg"][0]) if option != "bnbext" and abs(pdg_code) == 2147483648: pdg_code = 99999 if abs(pdg_code) in h_pdgs[name]: h_pdgs[name][abs(pdg_code)].Fill(v, chain.event_weight) else: h_pdgs[name][99999].Fill(v, chain.event_weight) histo_dict[name][category].Fill(v, chain.event_weight) return passed_events
chain = ROOT.TChain("mc_tree") chain.Add("root_files/sys_mc_file.root") # chain.Add("root_files/dirt.root") chain.Add("root_files/sys_nue_file.root/nue_tree") total_entries = int(chain.GetEntries() / 1) h_sys = {} h_2d = {} h_cv = {} h_covariance = {} h_frac = {} h_corr = {} vars = load_variables(chain) for n, b in vars.items(): h_uni = [] for u in range(N_UNI): if n == "reco_energy": h_uni.append( ROOT.TH1F("h_%s_%i" % (n, u), labels[n], len(bins) - 1, bins)) else: h_uni.append( ROOT.TH1F("h_%s_%i" % (n, u), labels[n], binning[n][0], binning[n][1], binning[n][2])) h_sys[n] = h_uni