Ejemplo n.º 1
0
def L(p):
    rec = p_to_rec(p)
    global counter
    counter += 1

    names = [
        'NQ', 'Ts', 'T', 'a_delay', 'q0', 'a0', 'a_pad', 'a_spe', 'a_dpe',
        'a_trpe', 'Spad', 'Spe'
    ]
    for name in names:
        if np.any(rec[name] < 0):
            return 1e10 * (1 - np.amin(rec[name]))
    names = ['q0', 'a0', 'Spad']
    for name in names:
        if np.any(rec[name] > 1):
            return 1e10 * (np.amax(rec[name]))
    if rec['Ts'][0] > 100:
        return 1e10 * rec['Ts'][0]
    if np.any(rec['St'][0] < 0.2):
        return 1e10 * (1 + np.abs(np.amin(rec['St'][0])))
    # if np.any(rec['Tf'][0]<1):
    #     return 1e10*(1+np.abs(np.amin(rec['Tf'][0])))

    l = 0
    P = make_P(rec['a0'][0], rec['Spad'][0], rec['Spe'][0], rec['m_pad'][0])
    m = Model(rec['NQ'][0], rec['T'][0], [0, 0], 0, 1, rec['Ts'][0],
              rec['St'][0], rec['q0'][0], P)
    m_area = model_area(areas, rec['m_pad'][0], rec['a_pad'][0],
                        rec['a_spe'][0], rec['a_dpe'][0], rec['a_trpe'][0],
                        rec['Spad'][0], rec['Spe'][0])
    for i in range(len(pmts)):
        model = np.sum(H[:, 0, i]) * np.ravel(m[:, :500, i])
        if np.any(np.isnan(model)) or np.any(np.isinf(model)):
            print('model is nan or inf')
            print('NQ=', rec['NQ'][0, i], 'T=', rec['T'][0, i], 'F=',
                  rec['F'][0, i], 'Tf=', rec['Tf'][0, i], 'Ts=',
                  rec['Ts'][0, i], 'St=', rec['St'][0, i])
            plt.figure()
            plt.plot(np.mean(t.T * np.arange(np.shape(t)[0])), 'k.')
            plt.show()
            sys.exit()
        data = np.ravel(H[:, :500, i])
        L = len(model)
        for j in range(L):
            if model[j] > 0 and data[j] <= 0:
                l -= model[j] - data[j]
            elif model[j] <= 0 and data[j] > 0:
                return 1e10 * (data[j] - model[j])
            elif model[j] == 0 and data[j] == 0:
                l += 1
            else:
                l += data[j] * np.log(model[j]) - data[j] * np.log(
                    data[j]) + data[j] - model[j]

        model = np.sum(h_q0[i]) * q0_model(n_q0, rec['q0'][0, i])
        data = h_q0[i]
        L = len(model)
        for j in range(L):
            if model[j] > 0 and data[j] <= 0:
                l -= model[j] - data[j]
            elif model[j] <= 0 and data[j] > 0:
                return 1e10 * (data[j] - model[j])
            elif model[j] == 0 and data[j] == 0:
                l += 1
            else:
                l += data[j] * np.log(model[j]) - data[j] * np.log(
                    data[j]) + data[j] - model[j]

        spectra_rng = np.nonzero(
            np.logical_and(PEs > PEs[np.argmax(spectra[i]) - 5],
                           PEs < PEs[np.argmax(spectra[i]) + 5]))[0]
        model = np.sum(H[:, 0, i]) * poisson.pmf(
            PEs, np.sum(m[:, :, i].T * np.arange(np.shape(H)[0])))[spectra_rng]
        data = spectra[i][spectra_rng]
        L = len(model)
        for j in range(L):
            if model[j] > 0 and data[j] <= 0:
                l -= model[j] - data[j]
            elif model[j] <= 0 and data[j] > 0:
                return 1e10 * (data[j] - model[j])
            elif model[j] == 0 and data[j] == 0:
                l += 1
            else:
                l += data[j] * np.log(model[j]) - data[j] * np.log(
                    data[j]) + data[j] - model[j]

        model = m_area[i]
        data = H_areas[i]
        L = len(model)
        for j in range(L):
            if model[j] > 0 and data[j] <= 0:
                l -= model[j] - data[j]
            elif model[j] <= 0 and data[j] > 0:
                return 1e10 * (data[j] - model[j])
            elif model[j] == 0 and data[j] == 0:
                l += 1
            else:
                l += data[j] * np.log(model[j]) - data[j] * np.log(
                    data[j]) + data[j] - model[j]

    model = rec['a_delay'][0] * np.exp(
        -0.5 * (delays[rng_delay] - rec['T'][0, 1] + rec['T'][0, 0])**2 /
        (rec['St'][0, 0]**2 + rec['St'][0, 1]**2)) / np.sqrt(
            2 * np.pi * (rec['St'][0, 0]**2 + rec['St'][0, 1]**2))
    data = delay_h[rng_delay]

    L = len(model)
    for j in range(L):
        if model[j] > 0 and data[j] <= 0:
            l -= model[j] - data[j]
        elif model[j] <= 0 and data[j] > 0:
            return 1e10 * (data[j] - model[j])
        elif model[j] == 0 and data[j] == 0:
            l += 1
        else:
            l += data[j] * np.log(model[j]) - data[j] * np.log(
                data[j]) + data[j] - model[j]

    if counter % (len(p) + 1) == 0:
        print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
        print('iteration=', int(counter / (len(p) + 1)), 'fanc=', -l)
        print('--------------------------------')
        print(rec)
    return -l
Ejemplo n.º 2
0
fig, ((ax1, ax3), (ax2, ax4)) = plt.subplots(2, 2)
ax3.plot(delays, delay_h, 'ko')
ax3.plot(delays[rng_delay],
         rec['a_delay'][0] *
         np.exp(-0.5 *
                (delays[rng_delay] - rec['T'][0, 1] + rec['T'][0, 0])**2 /
                (rec['St'][0, 0]**2 + rec['St'][0, 1]**2)) /
         np.sqrt(2 * np.pi * (rec['St'][0, 0]**2 + rec['St'][0, 1]**2)),
         'r.-',
         linewidth=5)
ax3.set_xlabel('Delay [ns]', fontsize='15')

ax4.plot(n_q0, h_q0[0], 'o', label='PMT{} - data'.format(pmts[0]))
ax4.plot(n_q0, h_q0[1], 'o', label='PMT{} - data'.format(pmts[1]))
ax4.plot(n_q0,
         np.sum(h_q0[0]) * q0_model(n_q0, rec['q0'][0, 0]),
         '+',
         label='PMT{} - model'.format(pmts[0]))
ax4.plot(n_q0,
         np.sum(h_q0[1]) * q0_model(n_q0, rec['q0'][0, 1]),
         '+',
         label='PMT{} - model'.format(pmts[1]))
ax4.set_xlabel('Dark PEs', fontsize='15')

ax3.legend(fontsize=15)
ax4.legend(fontsize=15)
ax4.set_yscale('log')
# plt.subplots_adjust(hspace=0)

m_area = model_area(areas, rec['m_pad'][0], rec['a_pad'][0], rec['a_spe'][0],
                    rec['a_dpe'][0], rec['a_trpe'][0], rec['Spad'][0],