Пример #1
0
        ROOT.TH1F("h_selected_numu_%i" % u, ";#nu_{e} energy [GeV];Efficiency",
                  len(bins) - 1, bins2))
    h_selected_precuts.append(
        ROOT.TH1F("h_selected_precuts_%i" % u,
                  ";#nu_{e} energy [GeV];Efficiency",
                  len(bins) - 1, bins2))
    h_selected_cuts.append(
        ROOT.TH1F("h_selected_cuts_%i" % u, ";#nu_{e} energy [GeV];Efficiency",
                  len(bins) - 1, bins2))
    h_selected_bdt.append(
        ROOT.TH1F("h_selected_bdt_%i" % u, ";#nu_{e} energy [GeV];Efficiency",
                  len(bins) - 1, bins2))
var_dict = dict(variables + spectators)
ROOT.TMVA.Tools.Instance()
reader = ROOT.TMVA.Reader(":".join(["!V", "!Silent", "Color"]))
load_bdt(reader)

for i_evt in range(entries):
    printProgressBar(i_evt,
                     entries,
                     prefix="Progress:",
                     suffix="Complete",
                     length=20)
    chain_nue.GetEntry(i_evt)

    protons = 0
    electrons = 0
    photons = 0
    pions = 0
    electron_energy = 0
    proton_energy = 0
Пример #2
0
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
Пример #3
0
def efficiency(files_path,
               eff_variables=[],
               systematics=False,
               scale=1,
               is_1e1p=False):
    nue_cosmic = glob(files_path + "/output*.root")
    chain_nue = ROOT.TChain("robertoana/pandoratree")
    chain_filter = ROOT.TChain("nueFilter/filtertree")
    chain_pot = ROOT.TChain("nueFilter/pot")

    for filename in nue_cosmic:
        chain_filter.Add(filename)
        chain_nue.Add(filename)
        chain_pot.Add(filename)

    entries = int(chain_filter.GetEntries() / scale)
    pot_entries = int(chain_pot.GetEntries() / scale)

    total_pot = 0
    for i in range(pot_entries):
        chain_pot.GetEntry(i)
        total_pot += chain_pot.pot

    total_pot /= 1.028
    categories = [
        "passed", "quality cuts", "CC #nu_{#mu} selected", "not contained",
        "cosmic selected", "1 shower", "no showers", "no flash",
        "no data products"
    ]
    #   "cuts"]

    h_tot = {}
    h_tot_sys = {}

    for v in eff_variables:
        h_tot[v] = ROOT.TH1F("h_tot_%s" % v, labels[v], binning[v][0],
                             binning[v][1], binning[v][2])
        h_tot_uni = []
        for u in range(N_UNI):
            h_tot_uni.append(
                ROOT.TH1F("h_tot_%s_%i" % (v, u), labels[v], binning[v][0],
                          binning[v][1], binning[v][2]))
        h_tot_sys[v] = h_tot_uni

    h_selected = {}
    h_passed_sys = {}
    for v in eff_variables:
        h_sel = {}
        for c in categories:
            h_sel[c] = ROOT.TH1F("%s" % c, labels[v], binning[v][0],
                                 binning[v][1], binning[v][2])
        h_selected[v] = h_sel

        h_passed_uni = []
        for u in range(N_UNI):
            h_passed_uni.append(
                ROOT.TH1F("h_tot_%s_%i" % (v, u), labels[v], binning[v][0],
                          binning[v][1], binning[v][2]))
        h_passed_sys[v] = h_passed_uni

    i_nue = -1
    eff_vars = {}
    var_dict = dict(variables + spectators)
    ROOT.TMVA.Tools.Instance()
    reader = ROOT.TMVA.Reader(":".join(["!V", "Silent", "Color"]))
    load_bdt(reader)

    for i_evt in tqdm_notebook(range(entries)):
        chain_filter.GetEntry(i_evt)

        if is_1e1p:
            eNp_interaction = check_1e1p(chain_filter)
        else:
            eNp_interaction = check_cc0pinp(chain_filter)

        true_neutrino_vertex = [
            chain_filter.true_vx_sce, chain_filter.true_vy_sce,
            chain_filter.true_vz_sce
        ]

        if chain_filter.passed:
            i_nue += 1

        if not (is_fiducial(true_neutrino_vertex) and eNp_interaction):
            continue

        proton_energy, electron_energy = eNp_interaction[0], eNp_interaction[1]
        eff_vars["proton_energy"] = proton_energy
        eff_vars["electron_energy"] = electron_energy
        eff_vars["energy"] = chain_filter.nu_energy
        eff_vars["vx"] = chain_filter.true_vx_sce
        eff_vars["vy"] = chain_filter.true_vy_sce
        eff_vars["vz"] = chain_filter.true_vz_sce

        eff_vars["theta"], eff_vars["phi"] = lepton_angles(chain_filter)

        if systematics:
            flux_weights = [1] * len(chain_filter.flux_weights[0])
            for flux in chain_filter.flux_weights:
                flux_weights = [a * b for a, b in zip(flux_weights, flux)]
            genie_weights = chain_filter.genie_weights[0]
        weight = chain_filter.bnbweight * 1.04

        for v in eff_variables:
            h_tot[v].Fill(eff_vars[v], weight)

            for u in range(N_UNI):
                h_tot_sys[v][u].Fill(eff_vars[v], weight * flux_weights[u])

            if not chain_filter.passed:
                if chain_filter.selection_result == 2:
                    h_selected[v]["no showers"].Fill(eff_vars[v], weight)
                if chain_filter.selection_result == 4:
                    h_selected[v]["no flash"].Fill(eff_vars[v], weight)
                if chain_filter.selection_result == 5:
                    h_selected[v]["no data products"].Fill(eff_vars[v], weight)
                continue

            chain_nue.GetEntry(i_nue)
            contaminated = chain_nue.cosmic_fraction < 0.5 and chain_nue.category == 7
            selected = (chain_nue.category == 2
                        or contaminated) and chain_nue.passed == 1
            if not selected:
                h_selected[v]["cosmic selected"].Fill(eff_vars[v], weight)
                continue

            # If there are no tracks we require at least two showers
            showers_2_tracks_0 = True
            if chain_nue.n_tracks == 0 and chain_nue.n_showers == 1:
                showers_2_tracks_0 = False

            if not showers_2_tracks_0:
                h_selected[v]["1 shower"].Fill(eff_vars[v], weight)
                continue

            if chain_nue.numu_passed == 1:
                h_selected[v]["CC #nu_{#mu} selected"].Fill(
                    eff_vars[v], weight)
                continue

            if not check_reco_fidvol(chain_nue):
                h_selected[v]["not contained"].Fill(eff_vars[v], weight)
                continue

            fill_kin_branches(chain_nue, 1, var_dict, "nue", True)

            if not pre_cuts(var_dict):
                h_selected[v]["quality cuts"].Fill(eff_vars[v], weight)
                continue

            if v == "proton_energy":
                there_is_proton = False
                for i_sh in range(chain_nue.n_showers):
                    if chain_nue.matched_showers[i_sh] == 2212:
                        there_is_proton = True
                        break
                for i_tr in range(chain_nue.n_tracks):
                    if there_is_proton:
                        break
                    if chain_nue.matched_tracks[i_tr] == 2212:
                        there_is_proton = True
                        break
                if there_is_proton:
                    h_selected[v]["passed"].Fill(eff_vars[v], weight)
            elif v == "electron_energy":
                there_is_electron = False
                for i_sh in range(chain_nue.n_showers):
                    if chain_nue.matched_showers[
                            i_sh] == 11 and electron_energy > 0.02:
                        there_is_electron = True
                        break
                for i_tr in range(chain_nue.n_tracks):
                    if there_is_electron:
                        break
                    if chain_nue.matched_tracks[
                            i_tr] == 11 and electron_energy > 0.02:
                        there_is_electron = True
                        break
                if there_is_electron:
                    h_selected[v]["passed"].Fill(eff_vars[v], weight)
            else:
                h_selected[v]["passed"].Fill(eff_vars[v], weight)

            for u in range(N_UNI):
                h_passed_sys[v][u].Fill(eff_vars[v], weight * flux_weights[u])

            # bdt_values = {}
            # for bdt_name in bdt_types:
            #     bdt_values[bdt_name] = reader.EvaluateMVA("BDT%s" % bdt_name)

            # if apply_cuts(bdt_values, var_dict, bdt=False, manual=True):
            #     h_selected[v]["cuts"].Fill(eff_vars[v], weight * 3.2/2.9 * 1.04)
        #     if apply_cuts(bdt_values, var_dict, bdt=True, manual=False):
        #         h_selected["bdt"].Fill(eff_vars[v], weight)

    eff_objects = {}
    for v in eff_variables:
        h_2d = eff_2d(h_tot_sys[v], h_passed_sys[v], v)
        sys_err = eff_sys_error(h_tot_sys[v], h_passed_sys[v])
        total_sys_err = eff_total_sys_error(h_tot_sys[v], h_passed_sys[v])
        eff_objects[v] = Efficiency(h_tot[v], h_selected[v], v, total_pot,
                                    total_sys_err, sys_err, h_2d)

    return eff_objects
Пример #4
0
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
Пример #5
0
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