Exemplo n.º 1
0
    particles = pd.read_hdf(file_name, 'MC/particles')
    hits = pd.read_hdf(file_name, 'MC/hits')
    events = particles.event_id.unique()

    for evt in events[:]:

        ### Select photoelectric events only
        evt_parts = particles[particles.event_id == evt]
        evt_hits = hits[hits.event_id == evt]
        select, true_pos = mcf.select_photoelectric(evt_parts, evt_hits)
        if not select: continue

        sns_resp = sel_df[sel_df.event_id == evt]
        if len(sns_resp) == 0: continue

        _, _, pos1, pos2, q1, q2 = rf.assign_sipms_to_gammas(
            sns_resp, true_pos, DataSiPM_idx)

        if len(pos1) > 0:
            pos_phi = rf.from_cartesian_to_cyl(np.array(pos1))[:, 1]
            _, var_phi = rf.phi_mean_var(pos_phi, q1)

            pos_z = np.array(pos1)[:, 2]
            mean_z = np.average(pos_z, weights=q1)
            var_z = np.average((pos_z - mean_z)**2, weights=q1)

            reco_cart = np.average(pos1, weights=q1, axis=0)

            var_phi1.append(var_phi)
            var_z1.append(var_z)
            touched_sipms1.append(len(pos1))
Exemplo n.º 2
0
        evt_hits = hits[hits.event_id == evt]
        select, true_pos = mcf.select_photoelectric(evt_parts, evt_hits)
        if not select: continue

        if (len(true_pos) == 1) & (evt_hits.energy.sum() > 0.511):
            continue

        sns_evt = sns_response[sns_response.event_id == evt]
        evt_tof = tof_response[tof_response.event_id == evt]

        sns_resp_r = rf.find_SiPMs_over_threshold(sns_evt, threshold=thr_r)
        sns_resp_phi = rf.find_SiPMs_over_threshold(sns_evt, threshold=thr_phi)
        sns_resp_z = rf.find_SiPMs_over_threshold(sns_evt, threshold=thr_z)
        sns_resp_e = rf.find_SiPMs_over_threshold(sns_evt, threshold=thr_e)

        _, _, pos1, pos2, q1, q2 = rf.assign_sipms_to_gammas(
            sns_resp_r, true_pos, DataSiPM_idx)
        if len(q1) == len(q2) == 0:
            c1 += 1
        r1 = r2 = None
        if len(pos1) > 0:
            pos1_phi = rf.from_cartesian_to_cyl(np.array(pos1))[:, 1]
            diff_sign = min(pos1_phi) < 0 < max(pos1_phi)
            if diff_sign & (np.abs(np.min(pos1_phi)) > np.pi / 2.):
                pos1_phi[pos1_phi < 0] = np.pi + np.pi + pos1_phi[pos1_phi < 0]
            mean_phi = np.average(pos1_phi, weights=q1)
            var_phi1 = np.average((pos1_phi - mean_phi)**2, weights=q1)
            r1 = Rpos(np.sqrt(var_phi1)).value
        if len(pos2) > 0:
            pos2_phi = rf.from_cartesian_to_cyl(np.array(pos2))[:, 1]
            diff_sign = min(pos2_phi) < 0 < max(pos2_phi)
            if diff_sign & (np.abs(np.min(pos2_phi)) > np.pi / 2.):
Exemplo n.º 3
0
    particles = pd.read_hdf(file_name, 'MC/particles')
    hits = pd.read_hdf(file_name, 'MC/hits')
    events = particles.event_id.unique()

    for evt in events[:]:

        ### Select photoelectric events only
        evt_parts = particles[particles.event_id == evt]
        evt_hits = hits[hits.event_id == evt]
        select, true_pos = mcf.select_photoelectric(evt_parts, evt_hits)
        if not select: continue

        waveforms = sel_df[sel_df.event_id == evt]
        if len(waveforms) == 0: continue

        _, _, pos1, pos2, q1, q2 = rf.assign_sipms_to_gammas(
            waveforms, true_pos, DataSiPM_idx)

        if len(pos1) > 0:
            pos_phi = rf.from_cartesian_to_cyl(np.array(pos1))[:, 1]
            _, var_phi = rf.phi_mean_var(pos_phi, q1)

            pos_z = np.array(pos1)[:, 2]
            mean_z = np.average(pos_z, weights=q1)
            var_z = np.average((pos_z - mean_z)**2, weights=q1)

            reco_cart = np.average(pos1, weights=q1, axis=0)

            var_phi1.append(var_phi)
            var_z1.append(var_z)
            touched_sipms1.append(len(pos1))
Exemplo n.º 4
0
def build_r_map(input_file: str, output_file: str, threshold: float):
    """
    This function extracts the true radial position
    and the variance of the SiPM positions
    in phi and z for each gamma interaction.
    """

    DataSiPM = db.DataSiPMsim_only('petalo', 0)  # full body PET
    DataSiPM_idx = DataSiPM.set_index('SensorID')

    try:
        sns_response = pd.read_hdf(input_file, 'MC/sns_response')
    except ValueError:
        print(f'File {input_file} not found')
        exit()
    except OSError:
        print(f'File {input_file} not found')
        exit()
    except KeyError:
        print(f'No object named MC/sns_response in file {input_file}')
        exit()
    print(f'Analyzing file {input_file}')

    sel_df = rf.find_SiPMs_over_threshold(sns_response, threshold)

    particles = pd.read_hdf(input_file, 'MC/particles')
    hits = pd.read_hdf(input_file, 'MC/hits')
    events = particles.event_id.unique()

    true_r1, true_r2 = [], []
    var_phi1, var_phi2 = [], []
    var_z1, var_z2 = [], []

    touched_sipms1, touched_sipms2 = [], []

    for evt in events:

        ### Select photoelectric events only
        evt_parts = particles[particles.event_id == evt]
        evt_hits = hits[hits.event_id == evt]
        select, true_pos = mcf.select_photoelectric(evt_parts, evt_hits)
        if not select: continue

        sns_resp = sel_df[sel_df.event_id == evt]
        if len(sns_resp) == 0: continue

        _, _, pos1, pos2, q1, q2 = rf.assign_sipms_to_gammas(
            sns_resp, true_pos, DataSiPM_idx)

        if len(pos1) > 0:
            pos_phi = rf.from_cartesian_to_cyl(np.array(pos1))[:, 1]
            _, var_phi = rf.phi_mean_var(pos_phi, q1)

            pos_z = np.array(pos1)[:, 2]
            mean_z = np.average(pos_z, weights=q1)
            var_z = np.average((pos_z - mean_z)**2, weights=q1)
            r = np.sqrt(true_pos[0][0]**2 + true_pos[0][1]**2)

            var_phi1.append(var_phi)
            var_z1.append(var_z)
            touched_sipms1.append(len(pos1))
            true_r1.append(r)

        else:
            var_phi1.append(1.e9)
            var_z1.append(1.e9)
            touched_sipms1.append(1.e9)
            true_r1.append(1.e9)

        if len(pos2) > 0:
            pos_phi = rf.from_cartesian_to_cyl(np.array(pos2))[:, 1]
            _, var_phi = rf.phi_mean_var(pos_phi, q2)

            pos_z = np.array(pos2)[:, 2]
            mean_z = np.average(pos_z, weights=q2)
            var_z = np.average((pos_z - mean_z)**2, weights=q2)
            r = np.sqrt(true_pos[1][0]**2 + true_pos[1][1]**2)

            var_phi2.append(var_phi)
            var_z2.append(var_z)
            touched_sipms2.append(len(pos2))
            true_r2.append(r)

        else:
            var_phi2.append(1.e9)
            var_z2.append(1.e9)
            touched_sipms2.append(1.e9)
            true_r2.append(1.e9)

    a_true_r1 = np.array(true_r1)
    a_true_r2 = np.array(true_r2)
    a_var_phi1 = np.array(var_phi1)
    a_var_phi2 = np.array(var_phi2)
    a_var_z1 = np.array(var_z1)
    a_var_z2 = np.array(var_z2)

    a_touched_sipms1 = np.array(touched_sipms1)
    a_touched_sipms2 = np.array(touched_sipms2)

    np.savez(output_file,
             a_true_r1=a_true_r1,
             a_true_r2=a_true_r2,
             a_var_phi1=a_var_phi1,
             a_var_phi2=a_var_phi2,
             a_var_z1=a_var_z1,
             a_var_z2=a_var_z2,
             a_touched_sipms1=a_touched_sipms1,
             a_touched_sipms2=a_touched_sipms2)