def Recon_WF(wfs, spe, dn, up, h_init): for wf in wfs: Recon_wf=np.zeros(len(wf)) Real_Recon_wf=np.zeros(len(wf)) t=[] blw=np.sqrt(np.mean(wf[:150]**2)) WF=WaveForm(100, blw) find_hits(WF, wf) init=sorted(filter(lambda hit: hit.height>h_init, WF.hits), key=lambda hit: hit.init)[0].init while len(WF.hits)>0: if len(WF.hits[0].groups)==0: Recon_wf[WF.hits[0].init:WF.hits[0].fin]+=(wf-Recon_wf)[WF.hits[0].init:WF.hits[0].fin] else: i=WF.hits[0].groups[0].maxi if i<init or (wf-Recon_wf)[i]>0.5*np.amin(spe): Recon_wf[WF.hits[0].groups[0].left:WF.hits[0].groups[0].right]+=np.array(wf[WF.hits[0].groups[0].left:WF.hits[0].groups[0].right]- Recon_wf[WF.hits[0].groups[0].left:WF.hits[0].groups[0].right]) N=0 J=-1 else: recon_wf=np.zeros(1000) Chi2=1e17 J=i N=1 for j in range(np.amax((init, WF.hits[0].groups[0].left)), np.amin((WF.hits[0].groups[0].maxi+5,999))): if (wf-Recon_wf)[j]>0.5*np.amin(spe): temp=1 else: if j>np.argmin(spe): recon_wf[j-np.argmin(spe):]=spe[:len(spe)-(j-np.argmin(spe))] else: recon_wf[:len(recon_wf)-(np.argmin(spe)-j)]=spe[np.argmin(spe)-j:] n=1 chi2=np.mean((recon_wf[j-dn:j-up]-(wf-Recon_wf)[j-dn:j-up])**2) if chi2<Chi2: Chi2=chi2 J=j N=n if J>np.argmin(spe): Real_Recon_wf[J-np.argmin(spe):]+=spe[:len(spe)-(J-np.argmin(spe))] Recon_wf[J-np.argmin(spe):]+=spe[:len(spe)-(J-np.argmin(spe))] else: Real_Recon_wf[:len(recon_wf)-(np.argmin(spe)-J)]+=spe[np.argmin(spe)-J:] Recon_wf[:len(recon_wf)-(np.argmin(spe)-J)]+=spe[np.argmin(spe)-J:] for i in range(N): t.append(J) WF.hits=[] find_hits(WF, wf-Recon_wf) yield [Real_Recon_wf, np.sum(((Real_Recon_wf-wf)[init:])**2), np.histogram(t, bins=1000, range=[-0.5, 999.5])[0]]
def hits_from_data(data): blw = np.sqrt(np.mean(data[:40]**2, axis=0)) for i, wf in enumerate(data.T): WF = WaveForm(pmts[i], blw[i]) find_hits(WF, wf) if len(WF.hits): yield WF.hits[0], pmts[i], blw[i]
def make_event(events, SPEs, N, tau, St): id=0 while events>0: Rec=[] print(events) wf=np.zeros(1000) events-=1 t=np.round(np.random.normal(200+np.random.exponential(tau*5, N), St*5, N)).astype(int) h, bins=np.histogram(t, bins=1000, range=[-0.5, 999.5]) for pe in shoot_pes(SPEs[np.random.randint(0,len(SPEs),N)], t): wf+=pe wf=wf-np.median(wf[:150]) blw=np.sqrt(np.mean(wf[:150]**2)) WF=WaveForm(100, blw) find_hits(WF, wf) rec=np.recarray(len(WF.hits), dtype=[ ('id', 'i8'), ('blw', 'f8'), ('init', 'i8'), ('height', 'f8')]) for i, hit in enumerate(WF.hits): rec[i]=id, blw, hit.init, hit.height yield [h, wf, rec] id+=1
break Data = np.reshape(Data, (PMT_num + 4, time_samples + 2)).T trig = np.argmin(Data[2:1002, 0]) H = np.zeros(200) for i, pmt in enumerate(pmts): wf = Data[2:1002, chns[i]] wf = wf - np.median(wf[:Init]) blw = np.sqrt(np.mean(wf[:Init]**2)) wf -= BL[i] for k in range(np.argmin(wf)): if np.all(wf[k:k + 20] < -blw): rec[j]['init_wf'][i] = k break #if pmts[i]<14: wf = np.roll(wf, int(np.round(delays[i] * 5))) waveform = WaveForm(blw) h, recon_wf = Recon_wf(waveform, wf, height_cuts[i], rise_time_cuts[i], spes[i], Init) H += h chi2 = np.sqrt(np.sum((wf[Init:] - recon_wf[Init:])**2)) if blw < 10 and rec[j]['init_wf'][i] > 20 and chi2 < 500: WFs[i] += wf recon_WFs[i] += recon_wf rec[j]['area'] = -np.sum(wf[Init:]) rec[j]['blw'][i] = blw rec[j]['id'] = id rec[j]['chi2'][i] = chi2 rec[j]['h'][:, i] = h if len(np.nonzero(H > 0)[0]) == 0:
def Show_Recon_WF(wf, spe, dn, up, h_init, p): Recon_wf = np.zeros(len(wf)) Real_Recon_wf = np.zeros(len(wf)) t = [] blw = np.sqrt(np.mean(wf[:150]**2)) WF = WaveForm(100, blw) find_hits(WF, wf) if len(list(filter(lambda hit: hit.height > h_init, WF.hits))) == 0: return Real_Recon_wf, np.sum(((Real_Recon_wf - wf))**2), np.zeros(1000) init = sorted(filter(lambda hit: hit.height > h_init, WF.hits), key=lambda hit: hit.init)[0].init while len(WF.hits) > 0: if len(WF.hits[0].groups) == 0: Recon_wf[WF.hits[0].init:WF.hits[0].fin] += ( wf - Recon_wf)[WF.hits[0].init:WF.hits[0].fin] else: i = WF.hits[0].groups[0].maxi if i < init or (wf - Recon_wf)[i] > 0.5 * np.amin(spe): Recon_wf[WF.hits[0].groups[0].left:WF.hits[0].groups[0]. right] += np.array( wf[WF.hits[0].groups[0].left:WF.hits[0].groups[0]. right] - Recon_wf[WF.hits[0].groups[0].left:WF. hits[0].groups[0].right]) N = 0 J = -1 else: recon_wf = np.zeros(1000) Chi2 = 1e17 J = i N = 1 for j in range(np.amax((init, WF.hits[0].groups[0].left)), np.amin((WF.hits[0].groups[0].maxi + 5, 999))): if (wf - Recon_wf)[j] > 0.5 * np.amin(spe): temp = 1 else: if j > np.argmin(spe): recon_wf[j - np.argmin(spe):] = spe[:len(spe) - (j - np.argmin(spe))] else: recon_wf[:len(recon_wf) - (np.argmin(spe) - j)] = spe[np.argmin(spe) - j:] n = 1 chi2 = np.mean((recon_wf[j - dn:j - up] - (wf - Recon_wf)[j - dn:j - up])**2) if chi2 < Chi2: Chi2 = chi2 J = j N = n x = np.arange(1000) peak = np.zeros(1000) if J > np.argmin(spe): Real_Recon_wf[J - np.argmin(spe):] += spe[:len(spe) - (J - np.argmin(spe))] Recon_wf[J - np.argmin(spe):] += spe[:len(spe) - (J - np.argmin(spe))] peak[J - np.argmin(spe):] += spe[:len(spe) - (J - np.argmin(spe))] if p == 1: plt.figure() plt.plot(x, wf, 'k.-', label='wf') plt.plot(x, Real_Recon_wf, 'r.-', label='Real_Recon_wf') plt.plot(x, Recon_wf, 'g.-', label='Recon_wf') plt.plot(x, wf - Recon_wf, 'b.-', label='fit') plt.plot(x, peak, 'c.-', label='peak') plt.legend() plt.show() else: Real_Recon_wf[:len(recon_wf) - (np.argmin(spe) - J)] += spe[np.argmin(spe) - J:] Recon_wf[:len(recon_wf) - (np.argmin(spe) - J)] += spe[np.argmin(spe) - J:] peak[:len(recon_wf) - (np.argmin(spe) - J)] += spe[np.argmin(spe) - J:] if p == 1: plt.figure() plt.plot(x, wf, 'k.-', label='wf') plt.plot(x, Real_Recon_wf, 'r.-', label='Real_Recon_wf') plt.plot(x, Recon_wf, 'g.-', label='Recon_wf') plt.plot(x, wf - Recon_wf, 'b.-', label='fit') plt.plot(x, peak, 'c.-', label='peak') plt.legend() plt.show() for i in range(N): t.append(J) WF.hits = [] find_hits(WF, wf - Recon_wf) return Real_Recon_wf, np.sqrt(np.mean( (Real_Recon_wf - wf)[init:]**2)), np.histogram(t, bins=1000, range=[-0.5, 999.5])[0]
j = 0 start_time = time.time() while j < len(rec_hits): if id % 10 == 0: print( source + type + ' In PMT {}, Event number {} ({} sec for event). {} Events were saved' .format(pmt, id, (time.time() - start_time) / 10, j)) start_time = time.time() Data = np.fromfile(file, np.float32, (PMT_num + 4) * (time_samples + 2)) if len(Data) < (PMT_num + 4) * (time_samples + 2): break Data = np.reshape(Data, (PMT_num + 4, time_samples + 2)).T data = np.array(Data[2:1002, 1:21]) bl, blw = find_bl(wf) wf = np.array(wf - bl) WF = WaveForm(pmt, blw) find_hits(WF, wf) for hit in WF.hits: rec[j] = id, blw, hit.init, hit.height j += 1 if j == len(rec): np.savez(path + 'PMT{}/WFs{}to{}'.format(pmt, id0, id - 1), rec=rec) id0 = id j = 0 id += 1 np.savez(path + 'PMT{}/WFs{}to{}'.format(pmt, id0, id), rec=rec[:j - 1])
first_id = 13 id = first_id start_time = time.time() N = 100 spectrum = np.zeros((len(pmts), 1000, N)) mean_WF = np.zeros((len(pmts), 1000)) Recon_wf = np.zeros((len(pmts), 1000)) Chi2 = np.zeros(N) ID = np.zeros(N) first_pmt = np.zeros(len(pmts)) BLW = np.zeros((len(pmts), N)) x = np.arange(1000) plt.figure() for i, [recon_wfs, chi2, recon_Hs, wfs, blw] in enumerate( Recon_WFs(file, spe, delay, dn, up, 100, height_cut, pmts, first_id)): if (first_id + i) % 10 == 0: print('in', path, '. ID=', first_id + i) WF = WaveForm(100, np.sqrt(np.mean(np.sum(wfs, axis=0)[:40]**2))) find_hits(WF, np.sum(wfs, axis=0)) init = sorted(WF.hits, key=lambda hit: hit.area)[-1].init wfs = np.roll(wfs, 100 - init, axis=1) plt.plot(x, np.sum(wfs, axis=0), '.-') # plt.plot(x, np.sum(recon_wfs, axis=0), 'r.-') if i == 2: break plt.show()