Recipes: How do I …?#
This page contains frequently used code-snippets (“recipes”). Below, da
refers to a data array. The examples generally assume that data is in “tof” unit (time-of-flight), i.e., no unit conversion was applied yet, and that there is a “spectrum” dimension in addition to the “tof” dimension. Replace these by the actual dimensions as required.
General#
Compute total counts per pixel#
[ ]:
counts = da.sum('tof') # for histogrammed data
counts = da.bins.sum().sum('tof') # for binned event data
Event data#
Compute number of events#
[ ]:
da.bins.size() # events per bin (ignoring event weights and event masks)
da.bins.size().sum() # total events from all non-masked bins
If the events have been normalized the event weights may differ from 1 and bins.sum()
should be used instead of bins.size()
. This also respects event masks:
[ ]:
da.bins.sum() # effective events per bin
da.bins.sum().sum() # total effective events from all non-masked bins
Mask a time-of-flight region such as a prompt-pulse#
[ ]:
tof = sc.array(dims=['tof'], unit='ms', values=[tof_min, mask_start, mask_end, tof_max])
da = da.bin(tof=tof) # bin in 'tof', updating prior 'tof' binning if present
da.masks['prompt_pulse'] = (tof >= tof['tof', 1]) & (tof < tof['tof', 2])
Plotting#
Plot a single spectrum#
[ ]:
da['spectrum', index].plot() # plot spectrum with given index
# plot spectrum with given spectrum-number, provided da.coords['spectrum'] exists
da['spectrum', sc.scalar(spectrum_number)].plot()
Plot comparison of multiple spectra#
[ ]:
spectra = {}
spectra['name1'] = da1['spectrum', index1]
spectra['name2'] = da1['spectrum', index2]
spectra['name3'] = da2['spectrum', index3]
sc.plot(spectra)