def optimize_ToF_parameters(forward=True, num_bins=100, data=None):
    d = rd.read_h5py_file()
    d = data if not type(data) == type(None) else d
    d = rd.slice_selection(d, 17, 3.5e-9, 9e-9)
    if forward:

        df = rd.cut_selection(d, 9, 0)
        tf = rd.extract_parameter(df, 17)
        n, bins, patches = plt.hist(tf, num_bins)
        x = bins
        x_data = bins[:-1] + (bins[1] - bins[0]) / 2
        y_data = n
        popt, pcov = curve_fit(forward_ToF, x_data, y_data,
                               (0.4e-9, 150, 1e-13))
        y = np.array([forward_ToF(i, popt[0], popt[1], popt[2]) for i in x])
        plt.plot(x, y, lw=2.0)

    else:
        db = rd.cut_selection(d, 9, 0, False)
        tb = rd.extract_parameter(db, 17)
        n, bins, patches = plt.hist(tb, num_bins)
        x = bins
        x_data = bins[:-1] + (bins[1] - bins[0]) / 2
        y_data = n
        popt, pcov = curve_fit(backward_ToF, x_data, y_data,
                               (0.4e-9, 150, 1e-13))
        y = np.array([backward_ToF(i, popt[0], popt[1], popt[2]) for i in x])
        plt.plot(x, y, lw=2.0)

    # plt.xlabel("Time of Flight [s]")
    # plt.ylabel("Frequency [Counts/Bin]")
    return popt, pcov
Esempio n. 2
0
def plot_AoI_sum(remove_fast=True,
                 return_values=False,
                 num_bins=50,
                 lims=(-0.5, 0.5)):
    sim_d, sim_t = (raw_sim_data,
                    sim_times) if not remove_fast else (raw_sim_data[:-1],
                                                        sim_times[:-1])
    x = np.linspace(lims[0], lims[1], num_bins)
    x_dist = x[:-1] + (x[1] - x[0]) / 2
    n = [np.zeros(len(x) - 1) for i in range(len(sim_d))]
    for i in range(len(n)):
        d = sim_d[i]
        E_e = rd.extract_parameter(d, 3)
        E_g = rd.extract_parameter(d, 1)
        c = 2.998e8
        m = 511 / c**2
        a = np.arccos(1 - (E_e / E_g * m * c**2 / (E_e + E_g)))
        tra_a = np.arccos(
            -(-d[:, 9] + d[:, 15]) / d[:, 24]) * np.sign(d[:, 11] - d[:, 5])
        or_a = np.zeros(len(tra_a))
        for j in range(len(or_a)):
            if abs(tra_a[j] - a[j]) < abs(tra_a[j] + a[j]):
                or_a[j] = tra_a[j] - a[j]
            else:
                or_a[j] = tra_a[j] + a[j]

        n[i] = (np.histogram(or_a, bins=x) / sim_times[i] * np.min(sim_t))[0]
    ns = np.zeros(len(n[0]))
    for i in n:
        ns += i
    if return_values:
        return x, n
    n2, bins, patches = plt.hist(x_dist, bins=x, weights=ns)
    return x, n2
Esempio n. 3
0
def plot_ET(d, return_values=False, num_bins=100):
    E = rd.extract_parameter(d, 1) / 1000 + rd.extract_parameter(d, 3) / 1000
    logbins = np.geomspace(0.4, 40, num_bins)
    if return_values:
        n, bins = np.histogram(E, bins=logbins)
        x_dist = np.zeros(len(logbins) - 1)
        for i in range(len(x_dist)):
            x_dist[i] = np.exp((np.log(bins[i + 1]) + np.log(bins[i])) / 2)
        return x_dist, n
    n, bins, patches = plt.hist(E, bins=logbins)
    plt.xscale("log")
    x_dist = np.zeros(len(logbins) - 1)
    for i in range(len(x_dist)):
        x_dist[i] = np.exp((np.log(bins[i + 1]) + np.log(bins[i])) / 2)
    return x_dist, n
Esempio n. 4
0
def plot_ToF(d, return_values=False, num_bins=100):
    t = rd.extract_parameter(d, 17) * 1e9
    if return_values:
        n, bins = np.histogram(t, num_bins)
        x_dist = bins[:-1] + (bins[1] - bins[0]) / 2
        return x_dist, n
    n, bins, patches = plt.hist(t, num_bins)
    x_dist = bins[:-1] + (bins[1] - bins[0]) / 2
    return x_dist, n
Esempio n. 5
0
def SAD_E(forward, num_bins=100, data_array=None):
    d = rd.read_h5py_file() if type(data_array) == type(None) else data_array
    d = rd.cut_selection(d, 9, 0, forward)
    E_e = rd.extract_parameter(d, 3)
    E_g = rd.extract_parameter(d, 1)
    c = 2.998e8
    m = 511 / c**2
    a = np.arccos(1 - (E_e / E_g * m * c**2 / (E_e + E_g)))
    n, bins, patches = plt.hist(a, num_bins)
    if forward:
        D = pd.NF
        x_min = at.fsad_min
        x_max = at.fsad_max
    else:
        D = pd.NB
        x_min = at.bsad_min
        x_max = at.bsad_max
    D = D * np.amax(n) / np.amax(D)
    rd.plot_points(D, x_min, x_max, 2.0)
Esempio n. 6
0
def plot_ET_sum(remove_fast=True,
                return_values=False,
                num_bins=50,
                lims=(0.4, 40)):
    sim_d, sim_t = (raw_sim_data,
                    sim_times) if not remove_fast else (raw_sim_data[:-1],
                                                        sim_times[:-1])
    x = np.geomspace(lims[0], lims[1], num_bins)
    n = [np.zeros(len(x) - 1) for i in range(len(sim_d))]
    for i in range(len(n)):
        d = sim_d[i]
        d = rd.extract_parameter(d, 1) / 1000 + rd.extract_parameter(d,
                                                                     3) / 1000
        n[i] = (np.histogram(d, bins=x)[0] / sim_times[i] * np.min(sim_t))
    ns = np.zeros(len(n[0]))
    for i in n:
        ns += i
    if return_values:
        return x, n
    n2, bins, patches = plt.hist(x[:-1], bins=x, weights=ns)
    plt.xscale("log")
    return x, n2
Esempio n. 7
0
def create_AoI_plot(d, num_bins=200, rang=0.5, ploto=True):
    E_e = rd.extract_parameter(d, 3)
    E_g = rd.extract_parameter(d, 1)
    c = 2.998e8
    m = 511 / c**2
    a = np.arccos(1 - (E_e / E_g * m * c**2 / (E_e + E_g)))
    tra_a = np.arccos(
        -(-d[:, 9] + d[:, 15]) / d[:, 24]) * np.sign(d[:, 11] - d[:, 5])
    or_a = np.zeros(len(tra_a))
    for i in range(len(or_a)):
        if abs(tra_a[i] - a[i]) < abs(tra_a[i] + a[i]):
            or_a[i] = tra_a[i] - a[i]
        else:
            or_a[i] = tra_a[i] + a[i]
    or_a = or_a[or_a[:] > -rang]
    or_a = or_a[or_a[:] < rang]
    if not ploto:
        n, bins = np.histogram(or_a, num_bins)

    else:
        n, bins, patches = plt.hist(or_a, num_bins)
    bins = bins[:-1] + (bins[1] - bins[0]) / 2
    return n, bins
Esempio n. 8
0
def plot_AoI(d, return_values=False, num_bins=100):
    E_e = rd.extract_parameter(d, 3)
    E_g = rd.extract_parameter(d, 1)
    c = 2.998e8
    m = 511 / c**2
    a = np.arccos(1 - (E_e / E_g * m * c**2 / (E_e + E_g)))
    tra_a = np.arccos(
        -(-d[:, 9] + d[:, 15]) / d[:, 24]) * np.sign(d[:, 11] - d[:, 5])
    or_a = np.zeros(len(tra_a))
    for i in range(len(or_a)):
        if abs(tra_a[i] - a[i]) < abs(tra_a[i] + a[i]):
            or_a[i] = tra_a[i] - a[i]
        else:
            or_a[i] = tra_a[i] + a[i]
    or_a = or_a[or_a[:] > -0.5]
    or_a = or_a[or_a[:] < 0.5]
    if return_values:
        n, bins = np.histogram(or_a, num_bins)
        x_dist = bins[:-1] + (bins[1] - bins[0]) / 2
        return x_dist, n
    n, bins, patches = plt.hist(or_a, num_bins)
    x_dist = bins[:-1] + (bins[1] - bins[0]) / 2
    return x_dist, n
Esempio n. 9
0
def SAD_E_vs_Pos(forward, size=100, data_array=None):
    d = rd.read_h5py_file() if type(data_array) == None else data_array
    d = rd.cut_selection(d, 9, 0, forward)
    E_e = rd.extract_parameter(d, 3)
    E_g = rd.extract_parameter(d, 1)
    c = 2.998e8
    m = 511 / c**2
    a_E = np.arccos(1 - (E_e / E_g * m * c**2 / (E_e + E_g)))
    a_P = rd.calculate_angles(d)
    c = 0
    while c < len(a_E):
        if np.isnan(a_E[c]):
            a_E = np.delete(a_E, c)
            a_P = np.delete(a_P, c)
            c += -1
        c += 1
    plt.hist2d(a_E, a_P, bins=(size, size))
    if forward:
        x = np.linspace(at.fsad_min, at.fsad_max, size)
        plt.plot(x, x, color="C1")
    else:
        x = np.linspace(at.bsad_min, at.bsad_max, size)
        plt.plot(x, x, color="C1")
Esempio n. 10
0
def plot_E_dist(
    forward,
    num_bins=300,
):
    x_min = 0000
    x_max = 5000
    y_min = 0
    #y_max=140000
    scale1 = 1.8
    scale2 = 1

    d = rd.read_h5py_file()
    d = rd.cut_selection(d, 9, 0, forward)
    E = rd.extract_parameter(d, 1) + rd.extract_parameter(d, 3)
    n, bins, patches = plt.hist(E, num_bins)
    x = np.linspace(x_min, x_max, 100)
    y = np.array([at.energy_distribution(i) for i in x])
    y = y * np.amax(n) / np.amax(y) * scale1
    plt.plot(x, y, label="A")

    if forward:
        t_min, t_max, sad = at.fsad_min, at.fsad_max, at.fsad
    else:
        t_min, t_max, sad = at.bsad_min, at.bsad_max, at.bsad
    y2 = np.array([
        quad(integrand1, t_min, t_max, (i, forward, t_min, t_max, sad), 0,
             10e-5, 10e-5)[0] for i in x
    ])
    y2 = y2 * np.amax(n) / np.amax(y2) * scale2
    plt.plot(x, y2, label="B")

    plt.xlabel("Total Detected Energy [keV]")
    plt.ylabel("Frequency [Counts/Bin]")
    plt.legend()

    plt.xlim(x_min, x_max)
    plt.ylim(0, 50000)
Esempio n. 11
0
def plot_E_all():
    scale1 = 1.7
    x_min = 0
    x_max = 6000

    d = rd.read_h5py_file()
    d = rd.cut_selection(d, 9, 0, True)
    E = rd.extract_parameter(d, 1)  # + rd.extract_parameter(d, 3)
    n, bins, patches = plt.hist(E, 500)
    x = np.linspace(x_min, x_max, 200)
    y = np.array([at.energy_distribution(i) for i in x])
    y = y * np.amax(n) / np.amax(y) * scale1
    #plt.plot(x,y,label="A")

    plt.xlim(x_min, x_max)
    plt.xlabel("Detected Energy [keV]")
    plt.ylabel("Frequency [Counts/Bin]")
Esempio n. 12
0
def plot_ToF_sum(remove_fast=True,
                 return_values=False,
                 num_bins=50,
                 lims=(0, 8)):
    sim_d, sim_t = (raw_sim_data,
                    sim_times) if not remove_fast else (raw_sim_data[:-1],
                                                        sim_times[:-1])
    x = np.linspace(lims[0], lims[1], num_bins)
    x_dist = x[:-1] + (x[1] - x[0]) / 2
    n = [np.zeros(len(x) - 1) for i in range(len(sim_d))]
    for i in range(len(n)):
        n[i] = (
            np.histogram(rd.extract_parameter(sim_d[i], 17) * 1e9, bins=x)[0] /
            sim_times[i] * np.min(sim_t))
    ns = np.zeros(len(n[0]))
    for i in n:
        ns += i
    if return_values:
        return x, n
    n2, bins, patches = plt.hist(x_dist, bins=x, weights=ns)
    return x, n2
def E1_E2_2D_hist():
    fig=plt.figure(figsize=(8,12))
    grid=plt.GridSpec(4, 2,hspace=0.15,wspace=0.30)
    
    pa=fig.add_subplot(grid[:,:])
    pa.spines['top'].set_color('none')
    pa.spines['bottom'].set_color('none')
    pa.spines['left'].set_color('none')
    pa.spines['right'].set_color('none')
    plt.tick_params(axis='both', which='both', bottom=False, top=False, labelbottom=False, right=False, left=False, labelleft=False)
    pa.set_xlabel("E1 [MeV]",labelpad=30)
    pa.set_ylabel("E2 [MeV]",labelpad=40)

    rows={0:"1500",1:"5000",2:"15000",3:"50000"}
    energies={0:"1.5",1:"5",2:"15",3:"50"}
    columns={0:True,1:False}
    
    letters=np.array([["a","b"],
                    ["c","d"],
                    ["e","f"],
                    ["g","h"]])
    
    num_bins=100
    
    for row in range(len(rows)):
        for column in range(len(columns)):
            temp=fig.add_subplot(grid[row,column])
            d=rd.read_h5py_file(p_data+"Thesis_Simulations/"+"Mono_"+rows[row]+"/","Mono_"+rows[row]+"_h5")
            d=rd.cut_selection(d,9,0,columns[column])
            if columns[column]:
                E1=rd.extract_parameter(d, 3)/1000
                E2=rd.extract_parameter(d, 1)/1000
            else:
                E1=rd.extract_parameter(d, 1)/1000
                E2=rd.extract_parameter(d, 3)/1000
                
            logbins1 = np.geomspace(E1.min(), E1.max(), num_bins)
            logbins2 = np.geomspace(E2.min(), E2.max(), num_bins)
            
            counts, _, _ = np.histogram2d(E1, E2, bins=(logbins1, logbins2))

            temp.pcolormesh(logbins1, logbins2, counts.T)
            temp.plot()
            temp.set_xscale('log')
            temp.set_yscale('log')
            
            
            temp.plot(logbins1,float(energies[row])-logbins1,color="C1",lw=1.0)
            
            
            plt.ylim(logbins2[0],logbins2[-1])
            if column==1:
                if row==1:
                    plt.ylim(1.5,logbins2[-1])
                elif row==2:
                    plt.ylim(5,logbins2[-1])
                elif row==3:
                    plt.ylim(10,logbins2[-1])
            
            if row==0:
                if column==0:
                    temp.set_xlabel("Forward Photons")
                    temp.xaxis.set_label_position("top")
                elif column==1:
                    temp.set_xlabel("Backward Photons")
                    temp.xaxis.set_label_position("top")

            if column==1:
                temp.set_ylabel(energies[row]+"MeV")
                temp.yaxis.set_label_position("right")
            

            
            
            if column==0:
                angs=(1,10,20,30,40,50)
                ang_lines=np.zeros((len(angs),num_bins))
                
                for ang in range(len(angs)):
                    for b in range(num_bins):
                        e_sca=logbins2[b]
                        wl_sca=6.625e-34*3e8/(1.602e-19*float(e_sca)*1e6)
                        wl_ini=wl_sca-2.426e-12*(1-np.cos(angs[ang]*np.pi/180))
                        e_ini=6.625e-34*3e8/(1.602e-19*wl_ini)*1e-6
                        ang_lines[ang,b]=e_ini-e_sca if e_ini-e_sca>0 else float("NaN")

                for ang in range(len(angs)):
                    temp.plot(ang_lines[ang],logbins2,color="C6",lw=1)
                    for b in range(num_bins):
                        if ang_lines[ang,b]>E1.min():
                            plt.text(ang_lines[ang,b],logbins2[b],str(angs[ang])+"$^\circ$",color="bisque")
                            break
                        
            
            else:
                mult=10
                logbinse=np.geomspace(E1.min(), E1.max(), num_bins*mult)
                angs=(180,140)
                ang_lines=np.zeros((len(angs),num_bins*mult))
                
                for ang in range(len(angs)):
                    for b in range(num_bins*mult):
                        e_sca=logbinse[b]
                        wl_sca=6.625e-34*3e8/(1.602e-19*float(e_sca)*1e6)
                        wl_ini=wl_sca-2.426e-12*(1-np.cos(angs[ang]*np.pi/180))
                        e_ini=6.625e-34*3e8/(1.602e-19*wl_ini)*1e-6
                        ang_lines[ang,b]=e_ini-e_sca if e_ini-e_sca>0 else float("NaN")

                left_shift=((3/4,1),
                            (2/3,1),
                            (3/5,1),
                            (1/2,1))
                for ang in range(len(angs)):
                    temp.plot(logbinse,ang_lines[ang],color="C6",lw=1)
                    for b in range(num_bins*mult):
                        if ang_lines[ang,b]>E2.min():
                            plt.text(logbinse[b]*left_shift[row][ang],ang_lines[ang,b],str(angs[ang])+"$^\circ$",color="bisque")
                            break
                    
            plt.xlim(E1.min(),np.amax(E1))
            plt.ylim(E2.min(),np.amax(E2))
            
            
            
            temp.text(-0.10,0.97,"("+letters[row,column]+")",transform=temp.transAxes,ha='left', va='center')
                
        plt.savefig(p_plots+'E1_E2_2D_hist.pdf',bbox_inches='tight')
Esempio n. 14
0
def plot_E1_E2(d,
               plot_space,
               num_bins=100,
               label_pos_left=True,
               E1_min=0.04,
               E2_min=0.4,
               E_max=20):
    E1 = rd.extract_parameter(d, 3) / 1000
    E2 = rd.extract_parameter(d, 1) / 1000

    logbins1 = np.geomspace(E1_min, E_max, num_bins)
    logbins2 = np.geomspace(E2_min, E_max, num_bins)

    counts, _, _ = np.histogram2d(E1, E2, bins=(logbins1, logbins2))

    pcmap = plot_space.pcolormesh(logbins1, logbins2, counts.T)
    plot_space.plot()
    plot_space.set_xscale('log')
    plot_space.set_yscale('log')

    mult = 10
    angs = (1, 10, 20, 30, 40, 50)
    ang_lines = np.zeros((len(angs), num_bins * mult))

    logbins1a = np.geomspace(E1_min, E_max, num_bins * mult)
    logbins2a = np.geomspace(E2_min, E_max, num_bins * mult)

    for ang in range(len(angs)):
        for b in range(num_bins * mult):
            e_sca = logbins2a[b]
            wl_sca = 6.625e-34 * 3e8 / (1.602e-19 * float(e_sca) * 1e6)
            wl_ini = wl_sca - 2.426e-12 * (1 - np.cos(angs[ang] * np.pi / 180))
            e_ini = 6.625e-34 * 3e8 / (1.602e-19 * wl_ini) * 1e-6
            ang_lines[ang,
                      b] = e_ini - e_sca if e_ini - e_sca > 0 else float("NaN")

    if label_pos_left:
        for ang in range(len(angs)):
            plot_space.plot(ang_lines[ang], logbins2a, color="C6", lw=1)
            for b in range(num_bins * mult):
                if ang_lines[ang, b] > E1.min():
                    plt.text(ang_lines[ang, b],
                             logbins2a[b],
                             str(angs[ang]) + "$^\circ$",
                             color="bisque")
                    break

    else:
        for ang in range(len(angs)):
            plot_space.plot(ang_lines[ang], logbins2a, color="C6", lw=1)
            for b in range(num_bins * mult):
                if ang_lines[ang, b] > 14 or logbins2a[b] > E_max:
                    plt.text(ang_lines[ang, b - 1],
                             logbins2a[b - 1] / 1.15,
                             str(angs[ang]) + "$^\circ$",
                             color="bisque")
                    break
    equi_ener = [0.75, 1.5, 3, 6, 12]
    for ene in equi_ener:
        plot_space.plot(ene - logbins2a, logbins2a, color="C1", lw=1.0)
        plt.text(ene - E2_min,
                 E2_min * 1.05,
                 str(ene) + "MeV",
                 color="lightyellow")

    plt.xlim(E1.min(), np.amax(E1))
    plt.ylim(E2.min(), np.amax(E2))

    return pcmap