Esempio n. 1
0
def check_cc0pinp(chain_nue):
    """Checks if the event is a contained electron neutrino
    CC0π-Np interaction

    Args:
        chain_nue: ROOT TChain of the events

    Returns:
        True if the event is a contained CC0π-Np interaction
    """
    protons = 0
    electrons = 0
    photons = 0
    pions = 0
    electron_energy = 0
    proton_energy = 0

    for i, energy in enumerate(chain_nue.nu_daughters_E):
        if chain_nue.nu_daughters_pdg[i] == 2212:
            p_vertex = [
                chain_nue.nu_daughters_vx[i], chain_nue.nu_daughters_vy[i],
                chain_nue.nu_daughters_vz[i]
            ]

            p_end = [
                chain_nue.nu_daughters_endx[i], chain_nue.nu_daughters_endy[i],
                chain_nue.nu_daughters_endz[i]
            ]

            if energy - PROTON_MASS > PROTON_THRESHOLD:
                protons += 1
                proton_energy += energy - PROTON_MASS
            if not is_fiducial(p_vertex) or not is_fiducial(p_end):
                protons = 0
                break

        if chain_nue.nu_daughters_pdg[i] == 11:
            e_vertex = [
                chain_nue.nu_daughters_endx[i], chain_nue.nu_daughters_endy[i],
                chain_nue.nu_daughters_endz[i]
            ]

            if energy - ELECTRON_MASS > ELECTRON_THRESHOLD and is_fiducial(
                    e_vertex):
                electron_energy += energy
                electrons += 1

        if chain_nue.nu_daughters_pdg[i] == 22:
            # if energy > 0.035:
            photons += 1

        if chain_nue.nu_daughters_pdg[i] == 111 or chain_nue.nu_daughters_pdg[
                i] == 211:
            # if energy > 0.06:
            pions += 1

    if electrons == 1 and pions == 0 and protons >= 1 and photons == 0 and chain_nue.ccnc == 0:
        return electron_energy + proton_energy

    return False
Esempio n. 2
0
def check_cc0pinp(chain_nue):
    """Checks if the event is a contained electron neutrino
    CC0π-Np interaction

    Args:
        chain_nue: ROOT TChain of the events

    Returns:
        True if the event is a contained CC0π-Np interaction
    """
    protons = 0
    electrons = 0
    photons = 0
    pions = 0
    electron_energy = 0
    proton_energy = 0

    for i, energy in enumerate(chain_nue.nu_daughters_E):
        vertex = [
            chain_nue.nu_daughters_start_v[i][0],
            chain_nue.nu_daughters_start_v[i][1],
            chain_nue.nu_daughters_start_v[i][2]
        ]

        end = [
            chain_nue.nu_daughters_end_v[i][0],
            chain_nue.nu_daughters_end_v[i][1],
            chain_nue.nu_daughters_end_v[i][2]
        ]

        if chain_nue.nu_daughters_pdg[i] == 2212:
            if energy - PROTON_MASS > PROTON_THRESHOLD and is_fiducial(
                    vertex) and is_fiducial(end):
                protons += 1
                proton_energy = max(energy - PROTON_MASS, proton_energy)

        if chain_nue.nu_daughters_pdg[i] == 11:
            if energy - ELECTRON_MASS > ELECTRON_THRESHOLD and is_fiducial(
                    vertex):
                electron_energy += energy - ELECTRON_MASS
                electrons += 1

        if chain_nue.nu_daughters_pdg[i] == 22:
            if energy > ELECTRON_THRESHOLD and is_fiducial(vertex):
                photons += 1

        if chain_nue.nu_daughters_pdg[i] == 111:
            if is_fiducial(vertex):
                pions += 1

        if abs(chain_nue.nu_daughters_pdg[i]) == 211:
            if energy - 0.140 > PROTON_THRESHOLD and is_fiducial(
                    vertex) and is_fiducial(end):
                pions += 1

    is_CC0piNp = electrons == 1 and pions == 0 and protons >= 1 and photons == 0
    if is_CC0piNp:
        return (proton_energy, electron_energy)

    return False
Esempio n. 3
0
def select_proton_event(c, var_dict):
    p = 0
    proton_energy = 0
    for i_pdg, energy in enumerate(c.nu_daughters_E):

        p_start = [
            c.nu_daughters_vx[i_pdg],
            c.nu_daughters_vy[i_pdg],
            c.nu_daughters_vz[i_pdg],
        ]

        p_end = [
            c.nu_daughters_endx[i_pdg],
            c.nu_daughters_endy[i_pdg],
            c.nu_daughters_endz[i_pdg],
        ]

        if abs(c.nu_daughters_pdg[i_pdg]) == 2212:
            if (energy - PROTON_MASS) > PROTON_THRESHOLD and is_fiducial(
                    p_start) and is_fiducial(p_end):
                proton_energy = energy - PROTON_MASS
                p += 1

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

    if not c.passed or not p == 1 or not is_fiducial(true_neutrino_vertex):
        return False

    if not c.category == 2:
        return False

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

    if not showers_2_tracks_0:
        return False

    fill_kin_branches(c, 1, var_dict, "nue", False)

    if not pre_cuts(var_dict):
        return False

    return proton_energy
Esempio n. 4
0
def check_reco_fidvol(chain_nue):
    """Checks if the neutrino vertex, the reconstructed tracks
    and the starting point of reconstructed showers are contained
    in the fiducial volume

    Args:
        chain_nue: ROOT TChain of the events

    Returns:
        True if the event is fully contained
    """
    track_fidvol = True

    for i_tr in range(chain_nue.n_tracks):
        track_start = [
            chain_nue.track_start_x[i_tr], chain_nue.track_start_y[i_tr],
            chain_nue.track_start_z[i_tr]
        ]
        track_end = [
            chain_nue.track_end_x[i_tr], chain_nue.track_end_y[i_tr],
            chain_nue.track_end_z[i_tr]
        ]

        track_fidvol = track_fidvol and is_fiducial(
            track_start) and is_fiducial(track_end)
        if not track_fidvol:
            break

    shower_fidvol = True

    for i_sh in range(chain_nue.n_showers):
        shower_start = [
            chain_nue.shower_start_x[i_sh], chain_nue.shower_start_y[i_sh],
            chain_nue.shower_start_z[i_sh]
        ]

        shower_fidvol = shower_fidvol and is_fiducial(shower_start)
        if not shower_fidvol:
            break

    neutrino_vertex = [chain_nue.vx, chain_nue.vy, chain_nue.vz]
    neutrino_fidvol = is_fiducial(neutrino_vertex)

    return track_fidvol and shower_fidvol and neutrino_fidvol
Esempio n. 5
0
def select_electron_event(c, var_dict):
    e = 0
    electron_energy = 0
    for i_pdg, energy in enumerate(c.nu_daughters_E):

        p_start = [
            c.nu_daughters_vx[i_pdg],
            c.nu_daughters_vy[i_pdg],
            c.nu_daughters_vz[i_pdg],
        ]

        if abs(c.nu_daughters_pdg[i_pdg]) == 11:
            if not (is_fiducial(p_start)):
                e = 0
                break
            if energy - ELECTRON_MASS > ELECTRON_THRESHOLD:
                electron_energy = energy
                e += 1

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

    if not c.passed or not e or not is_fiducial(true_neutrino_vertex):
        return False

    if not c.category == 2:
        return False

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

    if not showers_2_tracks_0:
        return False

    fill_kin_branches(c, 1, var_dict, "nue", False)

    if not pre_cuts(var_dict):
        return False

    return electron_energy
Esempio n. 6
0
        if chain_nue.nu_daughters_pdg[i] == 2212:
            proton_energy += energy - 0.938

            p_vertex = [
                chain_nue.nu_daughters_vx[i], chain_nue.nu_daughters_vy[i],
                chain_nue.nu_daughters_vz[i]
            ]

            p_end = [
                chain_nue.nu_daughters_endx[i], chain_nue.nu_daughters_endy[i],
                chain_nue.nu_daughters_endz[i]
            ]

            if energy - 0.938 > PROTON_THRESHOLD:
                protons += 1
            if not is_fiducial(p_vertex) or not is_fiducial(p_end):
                proton = 0
                break

        if chain_nue.nu_daughters_pdg[i] == 11:
            electron_energy += energy
            e_vertex = [
                chain_nue.nu_daughters_vx[i], chain_nue.nu_daughters_vy[i],
                chain_nue.nu_daughters_vz[i]
            ]
            e_end = [
                chain_nue.nu_daughters_endx[i], chain_nue.nu_daughters_endy[i],
                chain_nue.nu_daughters_endz[i]
            ]

            if energy - 0.51e-3 > ELECTRON_THRESHOLD and is_fiducial(
Esempio n. 7
0
def fill_tree(chain, weight, tree, option=""):
    total_events = 0
    total_entries = int(chain.GetEntries() / 1)
    print("Events", total_entries)
    for ievt in tqdm(range(total_entries)):
        chain.GetEntry(ievt)

        if not chain.passed:
            continue

        track_fidvol = True

        for i_tr in range(chain.n_tracks):
            track_start = [
                chain.track_start_x[i_tr], chain.track_start_y[i_tr],
                chain.track_start_z[i_tr]
            ]
            track_end = [
                chain.track_end_x[i_tr], chain.track_end_y[i_tr],
                chain.track_end_z[i_tr]
            ]

            track_fidvol = track_fidvol and is_fiducial(
                track_start) and is_fiducial(track_end)
            if not track_fidvol:
                break

        if not track_fidvol:
            continue

        shower_fidvol = True

        for i_sh in range(chain.n_showers):
            shower_start = [
                chain.shower_start_x[i_sh], chain.shower_start_y[i_sh],
                chain.shower_start_z[i_sh]
            ]

            shower_fidvol = shower_fidvol and is_fiducial(shower_start)
            if not shower_fidvol:
                break

        if not shower_fidvol:
            continue

        option_check = True
        event_weight = weight

        if option == "bnb":
            option_check = abs(chain.nu_pdg) != 12
            event_weight = weight * chain.bnbweight
        if abs(chain.nu_pdg) == 12:
            event_weight = weight * chain.bnbweight
        if "nue" in option:
            option_check = abs(chain.nu_pdg) == 12

        if option == "lee":
            event_weight = weight * chain.bnbweight * chain.leeweight

        if not option_check:
            continue

        neutrino_vertex = [chain.vx, chain.vy, chain.vz]

        if not is_fiducial(neutrino_vertex):
            continue

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

        total_events += event_weight

        if "nue" in option and (chain.category == 2 or chain.category == 7
                                or chain.category == 8):
            if not check_cc0pinp(chain):
                option = "nue_cc"
            else:
                option = "nue"

        fill_kin_branches(chain, event_weight, variables, option)

        tree.Fill()

    return total_events
Esempio n. 8
0
def fill_kin_branches(root_chain,
                      weight,
                      variables,
                      option="",
                      particleid=True):
    no_tracks = False
    hit_index = 2
    shower_id = choose_shower(root_chain, hit_index)
    track_id, track_score = choose_track(root_chain, particleid)

    if STORE_SYS and (option == "bnb" or option == "nue"):
        if (len(root_chain.flux_weights) > 0
                and len(root_chain.genie_weights) > 0):

            flux_weights = [1] * len(root_chain.flux_weights[0])
            for fl in root_chain.flux_weights:
                flux_weights = [a * b for a, b in zip(flux_weights, fl)]

            for u in range(N_UNI):
                variables["genie_weights"][u] = root_chain.genie_weights[0][u]
                variables["flux_weights"][u] = flux_weights[u]
        else:
            raise ValueError("Not valid weights")

    variables["E_dep"][0] = 0

    if "nue" in option:
        for i, energy in enumerate(root_chain.nu_daughters_E):
            if root_chain.nu_daughters_pdg[i] == 2212:
                if energy - 0.938 > PROTON_THRESHOLD:
                    variables["E_dep"][0] += energy - 0.938

            if root_chain.nu_daughters_pdg[i] == 11:
                if energy - 0.51e-3 > ELECTRON_THRESHOLD:
                    variables["E_dep"][0] += energy - 0.51e-3

    track_like_shower_id = -1
    if root_chain.n_tracks == 0 and root_chain.n_showers > 1:
        shower_res_std_list = list(root_chain.shower_res_std)
        track_like_shower_id = shower_res_std_list.index(
            min(shower_res_std_list))
        if track_like_shower_id == shower_id:
            track_like_shower_id = abs(shower_id - 1)
        no_tracks = True

    vec_shower = TVector3(root_chain.shower_dir_x[shower_id],
                          root_chain.shower_dir_y[shower_id],
                          root_chain.shower_dir_z[shower_id])

    true_vertex = [
        root_chain.true_vx_sce, root_chain.true_vy_sce, root_chain.true_vz_sce
    ]
    neutrino_vertex = [root_chain.vx, root_chain.vy, root_chain.vz]

    # variables["is_signal"][0] = int(root_chain.category == 2)
    variables["true_nu_is_fidvol"][0] = int(is_fiducial(true_vertex))

    variables["n_objects"][0] = root_chain.n_tracks + root_chain.n_showers
    variables["n_tracks_before"][0] = root_chain.n_tracks
    variables["n_showers_before"][0] = root_chain.n_showers

    if no_tracks:
        variables["n_tracks_before"][0] = 1
        variables["n_showers_before"][0] = root_chain.n_showers - 1

    variables["no_tracks"][0] = int(no_tracks)
    variables["nu_E"][0] = max(-999, root_chain.nu_E)

    y_shower = sum([
        root_chain.shower_nhits[i_sh][2]
        for i_sh in range(root_chain.n_showers)
    ])
    y_track = sum([
        root_chain.track_nhits[i_sh][2] for i_sh in range(root_chain.n_tracks)
    ])

    u_shower = sum([
        root_chain.shower_nhits[i_sh][0]
        for i_sh in range(root_chain.n_showers)
    ])
    u_track = sum([
        root_chain.track_nhits[i_sh][0] for i_sh in range(root_chain.n_tracks)
    ])

    v_shower = sum([
        root_chain.shower_nhits[i_sh][1]
        for i_sh in range(root_chain.n_showers)
    ])
    v_trackh = sum([
        root_chain.track_nhits[i_sh][1] for i_sh in range(root_chain.n_tracks)
    ])

    variables["total_hits"][
        0] = u_shower + u_track + v_shower + v_trackh + y_shower + y_track

    variables["total_hits_u"][0] = u_shower + u_track
    variables["total_hits_v"][0] = v_shower + v_trackh
    variables["total_hits_y"][0] = y_shower + y_track

    if option == "cosmic_mc" or option == "ext_data":
        variables["category"][0] = 0
    elif option == "lee":
        variables["category"][0] = 10
    elif option == "nue_cc":
        variables["category"][0] = 8
    else:
        variables["category"][0] = root_chain.category

    variables["event_weight"][0] = weight

    if option == "nue":
        if root_chain.cosmic_fraction < 0.5 and root_chain.category == 7:
            variables["category"][0] = 2

    if option == "nue_cc":
        if root_chain.cosmic_fraction < 0.5 and root_chain.category == 7 and root_chain.ccnc == 0:
            variables["category"][0] = 8

    if option == "lee":
        if root_chain.cosmic_fraction < 0.5 and root_chain.category == 7:
            variables["category"][0] = 10

    variables["p"][0], variables["pt"][0] = pt_plot(root_chain, 2)
    variables["pt_ratio"][0] = variables["pt"][0] / variables["p"][0]
    variables["event"][0] = root_chain.event
    variables["run"][0] = root_chain.run
    variables["subrun"][0] = root_chain.subrun
    variables["interaction_type"][0] = root_chain.interaction_type

    total_shower_energy = 0
    total_shower_energy_cali = 0
    total_track_energy_length = 0

    for i_tr in range(MAX_N_TRACKS):
        variables["track_pdg"][i_tr] = -999
        variables["track_phi"][i_tr] = -999
        variables["track_theta"][i_tr] = -999
        variables["track_length"][i_tr] = -999
        variables["track_energy_length"][i_tr] = -999
        variables["track_start_x"][i_tr] = -999
        variables["track_start_y"][i_tr] = -999
        variables["track_start_z"][i_tr] = -999
        variables["track_res_mean"][i_tr] = -999
        variables["track_res_std"][i_tr] = -999
        variables["track_pca"][i_tr] = -999
        variables["track_end_x"][i_tr] = -999
        variables["track_end_y"][i_tr] = -999
        variables["track_end_z"][i_tr] = -999
        variables["track_shower_angle"][i_tr] = -999
        variables["track_distance"][i_tr] = -999
        variables["track_pidchipr"][i_tr] = -999
        variables["track_likelihood"][i_tr] = -999
        variables["track_mip_likelihood"][i_tr] = -999
        variables["track_p_likelihood"][i_tr] = -999
        variables["track_dqdx"][i_tr] = -999

    for i_sh in range(MAX_N_SHOWERS):
        variables["shower_pdg"][i_sh] = -999
        variables["shower_start_x"][i_sh] = -999
        variables["shower_start_y"][i_sh] = -999
        variables["shower_start_z"][i_sh] = -999
        variables["shower_energy"][i_sh] = -999
        variables["shower_pca"][i_sh] = -999
        variables["shower_open_angle"][i_sh] = -999
        variables["shower_dedx"][i_sh] = -999
        variables["shower_dqdx"][i_sh] = -999
        variables["shower_dedx_u"][i_sh] = -999
        variables["shower_dedx_v"][i_sh] = -999
        variables["shower_dedx_cali"][i_sh] = -999
        variables["shower_res_mean"][i_sh] = -999
        variables["shower_res_std"][i_sh] = -999
        variables["shower_phi"][i_sh] = -999
        variables["shower_theta"][i_sh] = -999
        variables["shower_distance"][i_sh] = -999
        variables["shower_angle"][i_sh] = -999
        variables["shower_pidchimu"][i_sh] = -999
        variables["shower_pidchipr"][i_sh] = -999
        variables["shower_pidchipi"][i_sh] = -999

    variables["n_showers"][0] = 0
    variables["n_tracks"][0] = 0
    variables["shower_id"][0] = shower_id
    variables["track_id"][0] = track_id

    total_track_nhits = 0
    total_shower_nhits = 0

    check_conversion = len(
        root_chain.track_start_x) == root_chain.n_showers + root_chain.n_tracks
    n_tr = root_chain.n_tracks

    for i_sh in range(root_chain.n_showers):
        shower_v = [
            root_chain.shower_start_x[i_sh], root_chain.shower_start_y[i_sh],
            root_chain.shower_start_z[i_sh]
        ]

        v_sh = TVector3(root_chain.shower_dir_x[i_sh],
                        root_chain.shower_dir_y[i_sh],
                        root_chain.shower_dir_z[i_sh])

        cos = v_sh.Dot(vec_shower) / (v_sh.Mag() * vec_shower.Mag())
        shower_angle = math.degrees(math.acos(min(cos, 1)))
        shower_pca = root_chain.shower_pca[i_sh][2]
        converted_showers = 0
        shower_res_std = root_chain.shower_res_std[i_sh]
        shower_open_angle = root_chain.shower_open_angle[i_sh]
        shower_n_hits = root_chain.shower_nhits[i_sh][2]
        length = root_chain.shower_length[i_sh]
        ratio = shower_n_hits / length
        score = shower_score(shower_angle, shower_pca, shower_res_std,
                             shower_open_angle, shower_n_hits, ratio)
        shower_end = [s + d * length for s, d in zip(shower_v, v_sh)]

        ll = -999
        if check_conversion:
            mip_likelihood = root_chain.track_bragg_mip[n_tr + i_sh]
            p_likelihood = root_chain.track_bragg_p[n_tr + i_sh]
            if p_likelihood > 1e-32:
                ll = math.log(mip_likelihood / p_likelihood)

        if (i_sh == track_like_shower_id or score > 0) and i_sh != shower_id:
            variables["n_tracks"][0] += 1
            variables["track_pdg"][
                n_tr + converted_showers] = root_chain.matched_showers[i_sh]

            if check_conversion:
                track_v = [
                    root_chain.track_start_x[n_tr + i_sh],
                    root_chain.track_start_y[n_tr + i_sh],
                    root_chain.track_start_z[n_tr + i_sh]
                ]

                vec_track = TVector3(root_chain.track_dir_x[n_tr + i_sh],
                                     root_chain.track_dir_y[n_tr + i_sh],
                                     root_chain.track_dir_z[n_tr + i_sh])

                variables["track_start_x"][
                    n_tr + converted_showers] = root_chain.track_start_x[n_tr +
                                                                         i_sh]
                variables["track_start_y"][
                    n_tr + converted_showers] = root_chain.track_start_y[n_tr +
                                                                         i_sh]
                variables["track_start_z"][
                    n_tr + converted_showers] = root_chain.track_start_z[n_tr +
                                                                         i_sh]
                variables["track_phi"][n_tr +
                                       converted_showers] = math.degrees(
                                           root_chain.track_phi[n_tr + i_sh])
                variables["track_theta"][n_tr +
                                         converted_showers] = math.degrees(
                                             root_chain.track_theta[n_tr +
                                                                    i_sh])
                variables["track_length"][
                    n_tr + converted_showers] = root_chain.track_len[n_tr +
                                                                     i_sh]
                variables["track_pca"][n_tr + converted_showers] = max(
                    -999, root_chain.track_pca[n_tr + i_sh][2])
                variables["track_res_mean"][n_tr + converted_showers] = max(
                    -999, root_chain.track_res_mean[n_tr + i_sh])
                variables["track_res_std"][n_tr + converted_showers] = max(
                    -999, root_chain.track_res_std[n_tr + i_sh])
                if particleid:
                    variables["track_pidchipr"][
                        n_tr +
                        converted_showers] = root_chain.track_pid_chipr[n_tr +
                                                                        i_sh]

                    if root_chain.track_pid_chipr[n_tr + i_sh] < track_score:
                        variables["track_id"][0] = n_tr + i_sh
                        track_score = root_chain.track_pid_chipr[n_tr + i_sh]

                    mip_likelihood = root_chain.track_bragg_mip[n_tr + i_sh]
                    p_likelihood = root_chain.track_bragg_p[n_tr + i_sh]
                else:
                    mip_likelihood = 0
                    p_likelihood = 0
                variables["track_mip_likelihood"][
                    n_tr + converted_showers] = mip_likelihood
                variables["track_p_likelihood"][
                    n_tr + converted_showers] = p_likelihood
                if p_likelihood > 1e-9:
                    variables["track_likelihood"][
                        n_tr + converted_showers] = math.log(mip_likelihood /
                                                             p_likelihood)
                variables["track_dqdx"][n_tr + converted_showers] = max(
                    -999, root_chain.track_dQdx[n_tr + i_sh][2])
                if root_chain.category == 0 or root_chain.category == 6:
                    variables["track_dqdx"][0] *= 1.2
                variables["track_end_x"][
                    n_tr + converted_showers] = root_chain.track_end_x[n_tr +
                                                                       i_sh]
                variables["track_end_y"][
                    n_tr + converted_showers] = root_chain.track_end_y[n_tr +
                                                                       i_sh]
                variables["track_end_z"][
                    n_tr + converted_showers] = root_chain.track_end_z[n_tr +
                                                                       i_sh]
                length_e = length2energy(root_chain.track_len[n_tr + i_sh])
                variables["track_energy_length"][n_tr +
                                                 converted_showers] = length_e
                total_track_energy_length += length_e
                total_track_nhits += root_chain.track_nhits[n_tr + i_sh][2]
                costheta_shower_track = v_sh.Dot(vec_track) / (v_sh.Mag() *
                                                               vec_track.Mag())
                variables["track_shower_angle"][
                    n_tr + converted_showers] = costheta_shower_track
                track_vertex_d = math.sqrt(
                    sum([(t - n)**2
                         for t, n in zip(track_v, neutrino_vertex)]))
                variables["track_distance"][n_tr +
                                            converted_showers] = track_vertex_d
            else:
                variables["track_start_x"][
                    n_tr + converted_showers] = root_chain.shower_start_x[i_sh]
                variables["track_start_y"][
                    n_tr + converted_showers] = root_chain.shower_start_y[i_sh]
                variables["track_start_z"][
                    n_tr + converted_showers] = root_chain.shower_start_z[i_sh]
                variables["track_phi"][n_tr +
                                       converted_showers] = math.degrees(
                                           root_chain.shower_phi[i_sh])
                variables["track_theta"][n_tr +
                                         converted_showers] = math.degrees(
                                             root_chain.shower_theta[i_sh])
                variables["track_length"][
                    n_tr + converted_showers] = root_chain.shower_length[i_sh]
                variables["track_pca"][n_tr + converted_showers] = max(
                    -999, root_chain.shower_pca[i_sh][2])
                variables["track_res_mean"][n_tr + converted_showers] = max(
                    -999, root_chain.shower_res_mean[i_sh])
                variables["track_res_std"][n_tr + converted_showers] = max(
                    -999, root_chain.shower_res_std[i_sh])
                variables["track_pidchipr"][n_tr + converted_showers] = -999
                variables["track_likelihood"][n_tr + converted_showers] = -999
                variables["track_mip_likelihood"][n_tr +
                                                  converted_showers] = -999
                variables["track_p_likelihood"][n_tr +
                                                converted_showers] = -999
                variables["track_dqdx"][n_tr + converted_showers] = -999
                variables["track_end_x"][n_tr +
                                         converted_showers] = shower_end[0]
                variables["track_end_y"][n_tr +
                                         converted_showers] = shower_end[1]
                variables["track_end_z"][n_tr +
                                         converted_showers] = shower_end[2]
                length_e = length2energy(root_chain.shower_length[i_sh])
                total_track_energy_length += length_e
                variables["track_energy_length"][n_tr +
                                                 converted_showers] = length_e
                total_track_nhits += root_chain.shower_nhits[i_sh][2]
                costheta_shower_track = v_sh.Dot(vec_shower) / (
                    v_sh.Mag() * vec_shower.Mag())
                variables["track_shower_angle"][
                    n_tr + converted_showers] = costheta_shower_track
                track_vertex_d = math.sqrt(
                    sum([(t - n)**2
                         for t, n in zip(shower_v, neutrino_vertex)]))
                variables["track_distance"][n_tr +
                                            converted_showers] = track_vertex_d
            if variables["no_tracks"][0] == 1:
                variables["track_id"][0] = 0
            converted_showers += 1
        else:
            variables["n_showers"][0] += 1
            if i_sh != shower_id:
                variables["shower_angle"][i_sh] = shower_angle
            variables["shower_pdg"][i_sh] = root_chain.matched_showers[i_sh]
            variables["shower_start_x"][i_sh] = root_chain.shower_start_x[i_sh]
            variables["shower_start_y"][i_sh] = root_chain.shower_start_y[i_sh]
            variables["shower_start_z"][i_sh] = root_chain.shower_start_z[i_sh]
            variables["shower_theta"][i_sh] = math.degrees(
                root_chain.shower_theta[i_sh])
            variables["shower_phi"][i_sh] = math.degrees(
                root_chain.shower_phi[i_sh])
            shower_energy_cali = root_chain.shower_energy[i_sh][
                2] * root_chain.shower_energy_cali[i_sh][2]
            variables["shower_energy"][i_sh] = max(-999, shower_energy_cali)
            dedx = root_chain.shower_dEdx[i_sh][2]
            dqdx = root_chain.shower_dQdx[i_sh][
                2] * root_chain.shower_dQdx_cali[i_sh][2]
            variables["shower_dedx"][i_sh] = max(-999, dedx)
            dedx_cali = dqdx * root_chain.shower_dQdx_cali[i_sh][2] * 3.85e-5
            variables["shower_dqdx"][i_sh] = max(-999, dqdx)
            variables["shower_dedx_cali"][i_sh] = max(-999, dedx_cali)
            dedx_u = root_chain.shower_dEdx[shower_id][
                0] * root_chain.shower_dQdx_cali[shower_id][0]
            variables["shower_dedx_u"][i_sh] = max(-999, dedx_u)
            dedx_v = root_chain.shower_dEdx[shower_id][
                1] * root_chain.shower_dQdx_cali[shower_id][1]
            variables["shower_dedx_v"][i_sh] = max(-999, dedx_v)
            variables["shower_res_mean"][i_sh] = max(
                -999, root_chain.shower_res_mean[i_sh])
            variables["shower_res_std"][i_sh] = max(
                -999, root_chain.shower_res_std[i_sh])
            variables["shower_open_angle"][i_sh] = math.degrees(
                root_chain.shower_open_angle[i_sh])
            variables["shower_pca"][i_sh] = max(0,
                                                root_chain.shower_pca[i_sh][2])
            total_shower_energy += root_chain.shower_energy[i_sh][hit_index]
            total_shower_energy_cali += shower_energy_cali
            total_shower_nhits += root_chain.shower_nhits[i_sh][2]
            shower_vertex_d = math.sqrt(
                sum([(t - n)**2 for t, n in zip(shower_v, neutrino_vertex)]))
            variables["shower_distance"][i_sh] = shower_vertex_d
            if particleid and check_conversion:
                variables["shower_pidchimu"][
                    i_sh] = root_chain.track_pid_chimu[n_tr + i_sh]
                variables["shower_pidchipi"][
                    i_sh] = root_chain.track_pid_chimu[n_tr + i_sh]
                variables["shower_pidchipr"][
                    i_sh] = root_chain.track_pid_chipr[n_tr + i_sh]

    for i_tr in range(root_chain.n_tracks):
        v_track = TVector3(root_chain.track_dir_x[i_tr],
                           root_chain.track_dir_y[i_tr],
                           root_chain.track_dir_z[i_tr])

        track_vertex = [
            root_chain.track_start_x[i_tr], root_chain.track_start_y[i_tr],
            root_chain.track_start_z[i_tr]
        ]

        total_track_nhits += root_chain.track_nhits[i_tr][2]
        variables["n_tracks"][0] += 1
        variables["track_pdg"][i_tr] = root_chain.matched_tracks[i_tr]

        if particleid:
            variables["track_pidchipr"][i_tr] = root_chain.track_pid_chipr[
                i_tr]
            mip_likelihood = root_chain.track_bragg_mip[i_tr]
            p_likelihood = root_chain.track_bragg_p[i_tr]
        else:
            mip_likelihood = 0
            p_likelihood = 0
        variables["track_mip_likelihood"][i_tr] = mip_likelihood
        variables["track_p_likelihood"][i_tr] = p_likelihood
        if p_likelihood > 1e-9:
            variables["track_likelihood"][i_tr] = math.log(mip_likelihood /
                                                           p_likelihood)
        variables["track_dqdx"][i_tr] = max(-999,
                                            root_chain.track_dQdx[i_tr][2])
        if root_chain.category == 0 or root_chain.category == 6 and variables[
                "track_dqdx"][i_tr] != -999:
            variables["track_dqdx"][i_tr] *= 1.2
        variables["track_phi"][i_tr] = math.degrees(root_chain.track_phi[i_tr])
        variables["track_theta"][i_tr] = math.degrees(
            root_chain.track_theta[i_tr])
        variables["track_length"][i_tr] = root_chain.track_len[i_tr]
        variables["track_start_x"][i_tr] = root_chain.track_start_x[i_tr]
        variables["track_start_y"][i_tr] = root_chain.track_start_y[i_tr]
        variables["track_start_z"][i_tr] = root_chain.track_start_z[i_tr]
        variables["track_pca"][i_tr] = max(-999, root_chain.track_pca[i_tr][2])
        variables["track_res_mean"][i_tr] = max(
            -999, root_chain.track_res_mean[i_tr])
        variables["track_res_std"][i_tr] = max(-999,
                                               root_chain.track_res_std[i_tr])
        variables["track_end_x"][i_tr] = root_chain.track_end_x[i_tr]
        variables["track_end_y"][i_tr] = root_chain.track_end_y[i_tr]
        variables["track_end_z"][i_tr] = root_chain.track_end_z[i_tr]
        length_e = length2energy(root_chain.track_len[i_tr])
        total_track_energy_length += length_e
        variables["track_energy_length"][i_tr] = length_e
        costheta_shower_track = v_track.Dot(vec_shower) / (v_track.Mag() *
                                                           vec_shower.Mag())
        variables["track_shower_angle"][i_tr] = costheta_shower_track
        track_vertex_d = math.sqrt(
            sum([(t - n)**2 for t, n in zip(track_vertex, neutrino_vertex)]))
        variables["track_distance"][i_tr] = track_vertex_d

    variables["track_hits"][0] = total_track_nhits
    variables["shower_hits"][0] = total_shower_nhits
    variables["hits_ratio"][0] = total_shower_nhits / (total_track_nhits +
                                                       total_shower_nhits)

    variables["total_shower_energy"][0] = total_shower_energy
    variables["total_shower_energy_cali"][0] = total_shower_energy_cali
    variables["total_track_energy_length"][0] = total_track_energy_length
    # print(root_chain.reconstructed_neutrino_energy, (total_shower_energy_cali + 3.33417e-02) / 7.70732e-01 + total_track_energy_length)
    variables["reco_energy"][0] = (
        (total_shower_energy_cali + 3.33417e-02) / 7.70732e-01 +
        total_track_energy_length + 2.85112e-02) / 9.80163e-01
    variables["numu_score"][0] = root_chain.numu_passed

    if root_chain.ccnc == 1 and variables["category"][0] not in (0, 1, 5, 7):
        variables["category"][0] = 4
    if not variables["true_nu_is_fidvol"][0] and variables["category"][
            0] != 0 and variables["category"][0] != 6 and variables[
                "category"][0] != 1 and variables["category"][0] != 7:
        variables["category"][0] = 5

    if variables["category"][0] not in (0, 2, 6, 8) and option not in ("nue",
                                                                       "dirt"):
        variables["is_signal"][0] = 0.5
    else:
        variables["is_signal"][0] = -1

    variables["theta_ratio"][0] = variables["shower_theta"][
        shower_id] / variables["track_theta"][track_id]
Esempio n. 9
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
Esempio n. 10
0
def bkg_efficiency_nue(files_path, scale=1):
    bnb_cosmic = glob(files_path + "/*.root")
    chain_nue = ROOT.TChain("robertoana/pandoratree")
    chain_filter = ROOT.TChain("nueFilter/filtertree")
    chain_pot = ROOT.TChain("nueFilter/pot")
    for filename in bnb_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

    tot_bkg = {}
    passed_bkg = {}
    for i in range(10):
        tot_bkg[i] = ROOT.TH1F("h_tot_bkg_%i" % i, labels["energy"],
                               binning["energy"][0], binning["energy"][1],
                               binning["energy"][2])

        passed_bkg[i] = ROOT.TH1F("h_passed_bkg_%i" % i, labels["energy"],
                                  binning["energy"][0], binning["energy"][1],
                                  binning["energy"][2])

    i_nue = -1
    var_dict = dict(variables + spectators)
    for i_evt in tqdm_notebook(range(entries)):
        chain_filter.GetEntry(i_evt)
        true_neutrino_vertex = [
            chain_filter.true_vx, chain_filter.true_vy, chain_filter.true_vz
        ]

        true_category = 0
        if not is_fiducial(true_neutrino_vertex):
            true_category = 5

        if chain_filter.passed:
            i_nue += 1

        if is_fiducial(true_neutrino_vertex) and not check_cc0pinp(
                chain_filter) and chain_filter.ccnc == 0:
            true_category = 8
            # print(list(chain_filter.nu_daughters_pdg), list(chain_filter.nu_daughters_E))

        energy = chain_filter.nu_energy
        tot_bkg[true_category].Fill(energy, chain_filter.bnbweight)

        if chain_filter.passed:
            chain_nue.GetEntry(i_nue)

            true_neutrino_vertex_sce = [
                chain_nue.true_vx_sce, chain_nue.true_vy_sce,
                chain_nue.true_vz_sce
            ]

            if chain_nue.numu_passed == 1:
                continue

            if chain_nue.category not in (2, 7, 8):
                continue

            if chain_nue.ccnc == 1:
                continue

            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:
                continue

            if not check_reco_fidvol(chain_nue):
                continue

            if check_cc0pinp(chain_filter):
                continue

            fill_kin_branches(chain_nue, chain_filter.bnbweight, var_dict,
                              "nue_cc", False)
            if not pre_cuts(var_dict):
                continue

            reco_category = var_dict["category"][0]

            if not is_fiducial(true_neutrino_vertex_sce):
                reco_category = 5

            passed_bkg[reco_category].Fill(energy, chain_filter.bnbweight)

    effs = {}
    for t in tot_bkg:
        if t == 2 or t == 0:
            continue
        if tot_bkg[t].Integral():
            effs[t] = Efficiency(tot_bkg[t], {"passed": passed_bkg[t]},
                                 "energy", total_pot)

    return effs
Esempio n. 11
0
def bkg_efficiency(files_path, scale=1):

    tot_bkg = {}
    passed_bkg = {}
    for i in range(10):
        tot_bkg[i] = ROOT.TH1F("h_tot_bkg_%i" % i, labels["energy"],
                               binning["energy"][0], binning["energy"][1],
                               binning["energy"][2])

        passed_bkg[i] = ROOT.TH1F("h_passed_bkg_%i" % i, labels["energy"],
                                  binning["energy"][0], binning["energy"][1],
                                  binning["energy"][2])

    bnb_cosmic = glob(files_path + "/*.root")
    nue_cosmic = glob("data_files/mc_nue_sbnfit/*.root")
    dirt = glob("data_files/dirt/*.root")
    samples = [bnb_cosmic, nue_cosmic, dirt]

    for s in tqdm_notebook(samples):
        chain_nue = ROOT.TChain("robertoana/pandoratree")
        chain_filter = ROOT.TChain("nueFilter/filtertree")
        chain_pot = ROOT.TChain("nueFilter/pot")
        for filename in s:
            chain_filter.Add(filename)
            chain_nue.Add(filename)
            chain_pot.Add(filename)

        # 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
        weight = 4.341e+19 / total_pot
        if s == dirt:
            weight *= 0.5
        var_dict = dict(variables + spectators)

        i_nue = -1
        for i_evt in tqdm_notebook(range(entries)):
            chain_filter.GetEntry(i_evt)
            true_neutrino_vertex = [
                chain_filter.true_vx, chain_filter.true_vy,
                chain_filter.true_vz
            ]

            true_category = 0
            if not is_fiducial(true_neutrino_vertex):
                true_category = 5

            if chain_filter.passed:
                i_nue += 1

            if is_fiducial(true_neutrino_vertex):
                if chain_filter.ccnc == 1:
                    true_category = 4
                elif chain_filter.ccnc == 0 and chain_filter.nu_pdg == 14:
                    true_category = 3

            energy = chain_filter.nu_energy

            tot_bkg[1].Fill(energy, weight)
            tot_bkg[true_category].Fill(energy, weight)
            if true_category != 5:
                tot_bkg[7].Fill(energy, weight)

            if chain_filter.passed:
                chain_nue.GetEntry(i_nue)
                true_neutrino_vertex_sce = [
                    chain_nue.true_vx_sce, chain_nue.true_vy_sce,
                    chain_nue.true_vz_sce
                ]

                if chain_nue.numu_passed == 1:
                    continue

                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:
                    continue

                if not check_reco_fidvol(chain_nue):
                    continue

                fill_kin_branches(chain_nue, chain_nue.bnbweight, var_dict,
                                  "bnb", False)

                if not pre_cuts(var_dict):
                    continue

                category = var_dict["category"][0]

                if not is_fiducial(
                        true_neutrino_vertex_sce
                ) and category != 0 and category != 6 and category != 1 and category != 7:
                    category = 5

                passed_bkg[category].Fill(chain_nue.nu_E,
                                          chain_nue.bnbweight * weight)

    effs = {}
    for t in tot_bkg:
        if t == 2 or t == 0:
            continue
        if tot_bkg[t].Integral():
            effs[t] = Efficiency(tot_bkg[t], {"passed": passed_bkg[t]},
                                 "energy", total_pot)

    return effs