我有一个文本文件,里面充满了来自实验的数据,每行(我认为第12行之后)对应一个单一的bin(或实时18s)。
我希望能够返回计数的总数(即,文本文件中所有这些bin的加法),并在此直方图上标记它(我认为目前实际上是散点图,但是关闭了足够),这发生在18秒钟的窗口中。 然后最好能够将所有计数总数加起来,从而在整个时间段内得出一个总数
这是我认为与代码最相关的部分:
for n in range(0, numfiles):
## Print progress
print('\rFile {0} / {1}'.format(n+1, numfiles), end='')
## Load text file
x = np.linspace(0, 8191, 8192)
finalprefix = str(n).zfill(3)
fullprefix = folderToAnalyze + prefix + finalprefix
y = loadtxt(fullprefix + ".Spe", skiprows= 12, max_rows = 8192)
## Make figure and label
fig, ax = plt.subplots(figsize=(15,8))
fig.suptitle('Photon coincidence detections from $β^+$ + $β^-$ annhilation', fontsize=18)
plt.xlabel('Bins', fontsize=14)
plt.ylabel('Counts', fontsize=14)
## Plot data
ax.bar(x, y)
ax.set_xlim(600,960)
## Fit data to a gaussian
gmodel = Model(gaussian)
result = gmodel.fit(y, x=x, amp=8, cen=approxcen, wid=1)
## Plot results and save figure
ax.plot(x, result.best_fit, 'r-', label='Gaussian Fit')
ax.legend(loc='best')
texttoplot = result.fit_report()
ax.text(0.02, 0.5, texttoplot, transform=ax.transAxes)
plt.close()
fig.savefig("Coincidence_" + fileToRun + "_" + finalprefix + ".png", pad_inches='0.5')
## Print progress
if n==numfiles-1:
print('\rDone')
## Append to list if error in amplitude and amplitude itself is within reasonable bounds
if result.params['amp'].stderr < stderrThreshold and result.params['amp'] > minimumAmplitude:
amps.append(result.params['amp'].value)
ampserr.append(result.params['amp'].stderr)
ts.append(MaestroT*n)
## Plot decay curve
fig, ax = plt.subplots()
ax.errorbar(ts, amps, yerr= 2*np.array(ampserr), fmt="ko-", capsize = 5, capthick= 2, elinewidth=3, markersize=5)
plt.xlabel('Time', fontsize=14)
plt.ylabel('Peak amplitude', fontsize=14)
plt.title("Decay curve of P-31 by $β^+$ emission", fontsize=14)
## Fit decay curve
emodel = Model(expdecay)
decayresult = emodel.fit(amps, x=ts, weights=1/np.array(ampserr), t=150, A=140)
ax.plot(ts, decayresult.best_fit, 'r-', label='best fit')
## Add text to decay curve plot
plottext = f'{fileToRun}\n'
plottext += 'N: {0} / {1}\n'.format(len(ts), numfiles)
plottext += 't: {0:.2f} ± {1:.2f}\n'.format(decayresult.params['t'].value, decayresult.params['t'].stderr)
plottext += 'A: {0:.2f} ± {1:.2f}\n'.format(decayresult.params['A'].value, decayresult.params['A'].stderr)
plottext += 'Reduced $χ^2$: {0:.2f}\n'.format(decayresult.redchi)
ax.text(0.55, 0.5, plottext, transform=ax.transAxes, fontsize=14)
plt.show()
## Save figure
fig.savefig(fileToRun + "_decayplot.jpg", pad_inches='0.5')
但是,以防万一Full code is here,每个18秒窗口的直方图看起来像this:
最多使用50个来绘制this:
1个18秒窗口的原始数据为here
如何在相关直方图的每个18s窗口中获取计数?以及最终衰减曲线图上的计数总数(即所有18s直方图加在一起)?