Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plot event wf and job script datatypes #81

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jobs/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
storage_to_patch = config.get('job', 'storage_to_patch').split(',')
delete_records = config.getboolean('job', 'delete_records')

to_process_dtypes = ['peaklets', 'merged_s2s', 'peak_basics',
'events', 'event_basics', 'event_info', 'event_pattern_fit',
to_process_dtypes = ['peaklets', 'peaklet_classification', 'merged_s2s', 'peak_basics',
'events', 'peak_positions_mlp', 'peak_positions_gcn', 'peak_positions_cnn',
'event_basics', 'event_info', 'event_pattern_fit',
'event_shadow', 'event_ambience', 'event_n_channel','veto_intervals',
'cuts_basic']
if not skip_records:
Expand Down
235 changes: 200 additions & 35 deletions saltax/match/visual.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import matplotlib.pyplot as plt
import numpy as np

def plot_wf(ind, st_salt, st_simu, st_data, runid, matched_simu,
event_ext_window_ns=2.4e6,
s1_ext_window_samples=25,
s2_ext_window_samples=100):
def plot_event_wf_w_data(ind, st_salt, st_simu, st_data, runid, matched_simu,
event_ext_window_ns=2.4e6,
s1_ext_window_samples=25,
s2_ext_window_samples=100):
"""
Plot waveforms for a single event, including full event waveform and zoomed-in S1 and S2 waveforms.
Plot waveforms for a single event, including full event waveform and zoomed-in S1 and S2 waveforms.
This plot function will show info from salt, simu and data mode.
:param ind: index of the event in the matched_simu dataframe
:param st_salt: saltax context for salt mode
:param st_simu: saltax context for simu mode
Expand Down Expand Up @@ -119,22 +120,22 @@ def plot_wf(ind, st_salt, st_simu, st_data, runid, matched_simu,
print("Plotting waveforms for the whole event...")
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(15, 8), dpi=150)
ax1.plot(wf_data, label='Data', color='k', alpha=0.2)
ax1.plot(wf_simu_s1, label='Simu S1', color='tab:blue')
ax1.plot(wf_simu_s2, label='Simu S2', color='tab:orange')
ax1.plot(wf_simu_others, label='Simu Others', color='tab:green')
ax1.axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simu S1 Range')
ax1.axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simu S2 Range')
ax1.plot(wf_simu_s1, label='Simulated S1', color='tab:blue')
ax1.plot(wf_simu_s2, label='Simulated S2', color='tab:orange')
ax1.plot(wf_simu_others, label='Simulated Others', color='tab:green')
ax1.axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
ax1.axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
ax1.legend()
ax1.set_title("Run %s Event %s: Simu CS1=%sPE, Simu CS2=%sPE"%(runid, ind,
int(10*matched_simu['cs1'][ind])/10,
int(10*matched_simu['cs2'][ind])/10))

ax2.plot(wf_data, label='Data', color='k', alpha=0.2)
ax2.plot(wf_salt_s1, label='Salt S1', color='b')
ax2.plot(wf_salt_s2, label='Salt S2', color='r')
ax2.plot(wf_salt_others, label='Salt Others', color='g')
ax2.axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simu S1 Range')
ax2.axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simu S2 Range')
ax2.plot(wf_salt_s1, label='Sprinkled S1', color='b')
ax2.plot(wf_salt_s2, label='Sprinkled S2', color='r')
ax2.plot(wf_salt_others, label='Sprinkled Others', color='g')
ax2.axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
ax2.axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
ax2.legend()

ax1.set_ylabel("Amplitude [PE/10ns]")
Expand All @@ -146,44 +147,208 @@ def plot_wf(ind, st_salt, st_simu, st_data, runid, matched_simu,
print("Zooming into S1 and S2 respectively...")
fig, axs = plt.subplots(2, 2, figsize=(15, 8), dpi=150)
axs[0,0].plot(wf_data, label='Data', color='k', alpha=0.2)
axs[0,0].plot(wf_simu_s1, label='Simu S1', color='tab:blue')
axs[0,0].plot(wf_simu_s2, label='Simu S2', color='tab:orange')
axs[0,0].plot(wf_simu_others, label='Simu Others', color='tab:green')
axs[0,0].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simu S1 Range')
axs[0,0].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simu S2 Range')
axs[0,0].plot(wf_simu_s1, label='Simulated S1', color='tab:blue')
axs[0,0].plot(wf_simu_s2, label='Simulated S2', color='tab:orange')
axs[0,0].plot(wf_simu_others, label='Simulated Others', color='tab:green')
axs[0,0].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[0,0].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[0,0].set_xlim(matched_simu_s1_timerange_i[0]-s1_ext_window_samples,
matched_simu_s1_timerange_i[1]+s1_ext_window_samples)
axs[0,0].set_ylabel("Amplitude [PE/10ns]")
axs[0,0].legend()

axs[0,1].plot(wf_data, label='Data', color='k', alpha=0.2)
axs[0,1].plot(wf_simu_s1, label='Simu S1', color='tab:blue')
axs[0,1].plot(wf_simu_s2, label='Simu S2', color='tab:orange')
axs[0,1].plot(wf_simu_others, label='Simu Others', color='tab:green')
axs[0,1].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simu S1 Range')
axs[0,1].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simu S2 Range')
axs[0,1].plot(wf_simu_s1, label='Simulated S1', color='tab:blue')
axs[0,1].plot(wf_simu_s2, label='Simulated S2', color='tab:orange')
axs[0,1].plot(wf_simu_others, label='Simulated Others', color='tab:green')
axs[0,1].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[0,1].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[0,1].set_xlim(matched_simu_s2_timerange_i[0]-s2_ext_window_samples,
matched_simu_s2_timerange_i[1]+s2_ext_window_samples)
axs[0,1].legend()

axs[1,0].plot(wf_data, label='Data', color='k', alpha=0.2)
axs[1,0].plot(wf_salt_s1, label='Salt S1', color='b')
axs[1,0].plot(wf_salt_s2, label='Salt S2', color='r')
axs[1,0].plot(wf_salt_others, label='Salt Others', color='g')
axs[1,0].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simu S1 Range')
axs[1,0].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simu S2 Range')
axs[1,0].plot(wf_salt_s1, label='Sprinkled S1', color='b')
axs[1,0].plot(wf_salt_s2, label='Sprinkled S2', color='r')
axs[1,0].plot(wf_salt_others, label='Sprinkled Others', color='g')
axs[1,0].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[1,0].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[1,0].set_xlim(matched_simu_s1_timerange_i[0]-s1_ext_window_samples,
matched_simu_s1_timerange_i[1]+s1_ext_window_samples)
axs[1,0].set_xlabel("Time [10ns]")
axs[1,0].set_ylabel("Amplitude [PE/10ns]")
axs[1,0].legend()

axs[1,1].plot(wf_data, label='Data', color='k', alpha=0.2)
axs[1,1].plot(wf_salt_s1, label='Salt S1', color='b')
axs[1,1].plot(wf_salt_s2, label='Salt S2', color='r')
axs[1,1].plot(wf_salt_others, label='Salt Others', color='g')
axs[1,1].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simu S1 Range')
axs[1,1].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simu S2 Range')
axs[1,1].plot(wf_salt_s1, label='Sprinkled S1', color='b')
axs[1,1].plot(wf_salt_s2, label='Sprinkled S2', color='r')
axs[1,1].plot(wf_salt_others, label='Sprinkled Others', color='g')
axs[1,1].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[1,1].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[1,1].set_xlim(matched_simu_s2_timerange_i[0]-s2_ext_window_samples,
matched_simu_s2_timerange_i[1]+s2_ext_window_samples)
axs[1,1].set_xlabel("Time [10ns]")
axs[1,1].legend()
fig.show()


def plot_event_wf_wo_data(ind, st_salt, st_simu, runid, matched_simu,
event_ext_window_ns=2.4e6,
s1_ext_window_samples=25,
s2_ext_window_samples=100):
"""
Plot waveforms for a single event, including full event waveform and zoomed-in S1 and S2 waveforms.
This plot function will show info from salt, simu but no data mode.
:param ind: index of the event in the matched_simu dataframe
:param st_salt: saltax context for salt mode
:param st_simu: saltax context for simu mode
:param runid: runid of the event, example: '066666'
:param matched_simu: simu event_info already matched to truth. (Not necessarily matched to data!)
:param event_ext_window_ns: time window in ns to plot around the event, default 2.4e6 ns = 2.4 ms
:param s1_ext_window_samples: time window in samples to plot around S1, default 25 samples
:param s2_ext_window_samples: time window in samples to plot around S2, default 100 samples
"""
print("Loading peaks and lone_hits for run %s event %s"%(runid, ind))

# Get time ranges in indices for events, S1 and S2
extended_simu_event_timerange_ns = (matched_simu['s1_time'][ind]-event_ext_window_ns,
matched_simu['s2_endtime'][ind]+event_ext_window_ns)
matched_simu_s1_timerange_i = (int((matched_simu['s1_time'][ind] - extended_simu_event_timerange_ns[0])/10),
int((matched_simu['s1_endtime'][ind]-extended_simu_event_timerange_ns[0])/10))
matched_simu_s2_timerange_i = (int((matched_simu['s2_time'][ind] - extended_simu_event_timerange_ns[0])/10),
int((matched_simu['s2_endtime'][ind]-extended_simu_event_timerange_ns[0])/10))


# Get peaks and lone hits for the event
peaks_salt_selected = st_salt.get_array(runid, "peaks",
time_range=extended_simu_event_timerange_ns,
progress_bar=False)
peaks_simu_selected = st_simu.get_array(runid, "peaks",
time_range=extended_simu_event_timerange_ns,
progress_bar=False)
lhs_salt_selected = st_salt.get_array(runid, "lone_hits",
time_range=extended_simu_event_timerange_ns,
progress_bar=False)
lhs_simu_selected = st_simu.get_array(runid, "lone_hits",
time_range=extended_simu_event_timerange_ns,
progress_bar=False)


# Get waveforms for the event
print("Building waveforms...")
total_length = int((extended_simu_event_timerange_ns[1] - extended_simu_event_timerange_ns[0])/10)
to_pes = st_salt.get_single_plugin(runid, 'peaklets').to_pe

wf_salt_s1 = np.zeros(total_length)
wf_simu_s1 = np.zeros(total_length)
wf_salt_s2 = np.zeros(total_length)
wf_simu_s2 = np.zeros(total_length)
wf_salt_others = np.zeros(total_length)
wf_simu_others = np.zeros(total_length)

if len(peaks_salt_selected):
for p in peaks_salt_selected:
start_i = int((p['time'] - int(extended_simu_event_timerange_ns[0]))/10)
length = p['length']
dt = p['dt']
if p['type'] == 1:
for i in range(length):
wf_salt_s1[start_i+i*int(dt/10):start_i+(i+1)*int(dt/10)] = p['data'][i] / dt * 10
elif p['type'] == 2:
for i in range(length):
wf_salt_s2[start_i+i*int(dt/10):start_i+(i+1)*int(dt/10)] = p['data'][i] / dt * 10
else:
for i in range(length):
wf_salt_others[start_i+i*int(dt/10):start_i+(i+1)*int(dt/10)] = p['data'][i] / dt * 10
if len(lhs_salt_selected):
for l in lhs_salt_selected:
time_i = int((l['time'] - int(extended_simu_event_timerange_ns[0]))/10)
amp = l['area'] * to_pes[l['channel']]
wf_salt_others[time_i] += amp/10

if len(peaks_simu_selected):
for p in peaks_simu_selected:
start_i = int((p['time'] - int(extended_simu_event_timerange_ns[0]))/10)
length = p['length']
dt = p['dt']
if p['type'] == 1:
for i in range(length):
wf_simu_s1[start_i+i*int(dt/10):start_i+(i+1)*int(dt/10)] = p['data'][i] / dt * 10
elif p['type'] == 2:
for i in range(length):
wf_simu_s2[start_i+i*int(dt/10):start_i+(i+1)*int(dt/10)] = p['data'][i] / dt * 10
else:
for i in range(length):
wf_simu_others[start_i+i*int(dt/10):start_i+(i+1)*int(dt/10)] = p['data'][i] / dt * 10
if len(lhs_simu_selected):
for l in lhs_simu_selected:
time_i = int((l['time'] - int(extended_simu_event_timerange_ns[0]))/10)
amp = l['area'] * to_pes[l['channel']]
wf_simu_others[time_i] += amp/10

# Plot full event waveform
print("Plotting waveforms for the whole event...")
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(15, 8), dpi=150)
ax1.plot(wf_simu_s1, label='Simulated S1', color='tab:blue')
ax1.plot(wf_simu_s2, label='Simulated S2', color='tab:orange')
ax1.plot(wf_simu_others, label='Simulated Others', color='tab:green')
ax1.axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
ax1.axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
ax1.legend()
ax1.set_title("Run %s Event %s: Simu CS1=%sPE, Simu CS2=%sPE"%(runid, ind,
int(10*matched_simu['cs1'][ind])/10,
int(10*matched_simu['cs2'][ind])/10))

ax2.plot(wf_salt_s1, label='Sprinkled S1', color='b')
ax2.plot(wf_salt_s2, label='Sprinkled S2', color='r')
ax2.plot(wf_salt_others, label='Sprinkled Others', color='g')
ax2.axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
ax2.axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
ax2.legend()

ax1.set_ylabel("Amplitude [PE/10ns]")
ax2.set_xlabel("Time [10ns]")
ax2.set_ylabel("Amplitude [PE/10ns]")
fig.show()

# Zoom into S1 and S2 waveforms
print("Zooming into S1 and S2 respectively...")
fig, axs = plt.subplots(2, 2, figsize=(15, 8), dpi=150)
axs[0,0].plot(wf_simu_s1, label='Simulated S1', color='tab:blue')
axs[0,0].plot(wf_simu_s2, label='Simulated S2', color='tab:orange')
axs[0,0].plot(wf_simu_others, label='Simulated Others', color='tab:green')
axs[0,0].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[0,0].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[0,0].set_xlim(matched_simu_s1_timerange_i[0]-s1_ext_window_samples,
matched_simu_s1_timerange_i[1]+s1_ext_window_samples)
axs[0,0].set_ylabel("Amplitude [PE/10ns]")
axs[0,0].legend()

axs[0,1].plot(wf_simu_s1, label='Simulated S1', color='tab:blue')
axs[0,1].plot(wf_simu_s2, label='Simulated S2', color='tab:orange')
axs[0,1].plot(wf_simu_others, label='Simulated Others', color='tab:green')
axs[0,1].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[0,1].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[0,1].set_xlim(matched_simu_s2_timerange_i[0]-s2_ext_window_samples,
matched_simu_s2_timerange_i[1]+s2_ext_window_samples)
axs[0,1].legend()

axs[1,0].plot(wf_salt_s1, label='Sprinkled S1', color='b')
axs[1,0].plot(wf_salt_s2, label='Sprinkled S2', color='r')
axs[1,0].plot(wf_salt_others, label='Sprinkled Others', color='g')
axs[1,0].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[1,0].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[1,0].set_xlim(matched_simu_s1_timerange_i[0]-s1_ext_window_samples,
matched_simu_s1_timerange_i[1]+s1_ext_window_samples)
axs[1,0].set_xlabel("Time [10ns]")
axs[1,0].set_ylabel("Amplitude [PE/10ns]")
axs[1,0].legend()

axs[1,1].plot(wf_salt_s1, label='Sprinkled S1', color='b')
axs[1,1].plot(wf_salt_s2, label='Sprinkled S2', color='r')
axs[1,1].plot(wf_salt_others, label='Sprinkled Others', color='g')
axs[1,1].axvspan(matched_simu_s1_timerange_i[0],matched_simu_s1_timerange_i[1], color='tab:blue', alpha=0.1, label='Simulated S1 Range')
axs[1,1].axvspan(matched_simu_s2_timerange_i[0],matched_simu_s2_timerange_i[1], color='tab:orange', alpha=0.1, label='Simulated S2 Range')
axs[1,1].set_xlim(matched_simu_s2_timerange_i[0]-s2_ext_window_samples,
matched_simu_s2_timerange_i[1]+s2_ext_window_samples)
axs[1,1].set_xlabel("Time [10ns]")
Expand Down
Loading