from pydiana import Diana, ROOT
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import matplotlib.pyplot as plt
[docs]
def draw_optimum_trigger(chan:int,dataset:int, apinput:str,aninput:str,triggerLevel:float =5,receed:float=2,draw:str='matplotlib'):
OF_trig = Diana.QTriggerOptimumFilter()
OF_trig.SetChannel(chan)
OF_trig.SetCFGParameters(apinput,aninput,dataset,triggerLevel,receed)
OF_trig.BuildOptimumFilter()
ap = OF_trig.GetAveragePulse()
apmaxidx = ap.GetMaxIndex()
ap = np.array([ap[i] for i in range(ap.Size())])
ap_d = OF_trig.GetAveragePulseDoubled()
ap_d = np.array([ap_d[i] for i in range(ap_d.Size())])
smoothed_ap = OF_trig.GetAveragePulseDoubledSmoothed()
smoothed_ap = np.array([smoothed_ap[i] for i in range(smoothed_ap.Size())])
smoothed_ap = np.roll(smoothed_ap,apmaxidx)
smoothing_ap = OF_trig.GetAveragePulseSmoothing()
smoothing_ap = np.array([smoothing_ap[i] for i in range(smoothing_ap.Size())])
apfilt = OF_trig.GetAveragePulseFiltered()
apfilt_max_idx = apfilt.GetMaxIndex()
apfilt = np.array([apfilt[i] for i in range(apfilt.Size())])
filterTD = OF_trig.GetFilterTD()
filterTD = np.array([filterTD[i] for i in range(filterTD.Size())])
filterTD = np.roll(filterTD,apmaxidx)
filterTD_d = OF_trig.GetFilterTDDoubled()
filterTD_d = np.array([filterTD_d[i] for i in range(filterTD_d.Size())])
print("-"*5+"Optimum Trigger"+"-"*5)
print(" Channel: ",OF_trig.GetChannel())
print(" Noise RMS: ",OF_trig.GetRMS()," ADC units")
print(" Threshold for triggering: ",OF_trig.GetTriggerLevel()," ADC units")
print(" Debounce: ",OF_trig.GetDebounce())
print(" Receed Time: ",OF_trig.GetIndexDifference())
if draw == 'plotly':
fig1 = go.Figure()
for x,lab in zip((ap,filterTD,smoothed_ap,smoothing_ap),("Average Pulse","FilterTD (rolled)","Average Pulse Smoothed (rolled)","Smoothing AP")):
fig1.add_trace(go.Scatter(y = x,name=lab))
fig1.add_hline(y=OF_trig.GetTriggerLevel()/OF_trig.GetAPMax(),annotation_text='Trigger Level (Normalized)',line_dash="dash")
fig1.update_xaxes(title = 'Sample idx')
fig1.update_yaxes(title = 'Amplitude [ADC units]')
fig1.update_layout(title="Average Pulse Used to build OF")
fig2 = go.Figure()
for x,lab in zip((ap_d,filterTD_d,apfilt),("Average Pulse","Filter Doubled","Filtered AP")):
fig2.add_trace(go.Scatter(y = x,name=lab))
fig2.add_trace(go.Scatter(x=[apfilt_max_idx-OF_trig.GetIndexDifference()],y = [apfilt[apfilt_max_idx-OF_trig.GetIndexDifference()]],name="Trigger Sample",mode='markers'))
fig2.add_trace(go.Scatter(x=[apfilt_max_idx-OF_trig.GetIndexDifference()],y = [ap_d[apfilt_max_idx-OF_trig.GetIndexDifference()]],name="Trigger Sample",mode='markers'))
fig2.add_hline(y=OF_trig.GetTriggerLevel()/OF_trig.GetAPMax(),annotation_text='Trigger Level (Normalized)',line_dash="dash")
fig2.update_xaxes(title = 'Sample idx')
fig2.update_yaxes(title = 'Amplitude [ADC units]')
fig2.update_layout(title="Padded Quantities used to build OT")
return fig1,fig2
elif draw == 'matplotlib':
fig = plt.figure()
ax1 = fig.add_subplot(121)
for x,lab in zip((ap,filterTD,smoothed_ap,smoothing_ap),("Average Pulse","FilterTD (rolled)","Average Pulse Smoothed (rolled)","Smoothing AP")):
ax1.plot(x,label=lab)
ax1.axhline(y=OF_trig.GetTriggerLevel()/OF_trig.GetAPMax(),xmin=0,xmax=1,linestyle='--',label='Trigger Level (Normalized)')
ax1.legend()
ax1.set_xlabel('Sample idx')
ax1.set_ylabel("Amplitude [ADC units]")
ax1.set_title("Average Pulse Used to build OF")
ax1.grid()
ax2 = fig.add_subplot(122)
for x,lab in zip((ap_d,filterTD_d,apfilt),("Average Pulse","Filter Doubled","Filtered AP")):
ax2.plot(x,label=lab)
ax2.plot(apfilt_max_idx-OF_trig.GetIndexDifference(), apfilt[apfilt_max_idx-OF_trig.GetIndexDifference()],'*',label="Trigger Sample")
ax2.plot(apfilt_max_idx-OF_trig.GetIndexDifference(),ap_d[apfilt_max_idx-OF_trig.GetIndexDifference()],'*',label="Trigger Sample")
ax2.axhline(y=OF_trig.GetTriggerLevel()/OF_trig.GetAPMax(),xmin=0,xmax=1,linestyle='--',label='Trigger Level (Normalized)')
ax2.legend()
ax2.set_xlabel('Sample idx')
ax2.set_ylabel("Amplitude [ADC units]")
ax2.set_title("Padded Quantities used to build OT")
ax2.grid()
plt.show()
else:
print("Drawing Utility not recongised, not plotting")