-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
366 lines (314 loc) · 9.61 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package main
import (
"context"
"encoding/csv"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/mdlayher/packet"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/exp/maps"
)
// App struct
type App struct {
ctx context.Context
sendFrameChannel chan *Frame
recvFrameChannel chan Frame
data DaqData
writerData WriterData
connection *packet.Conn
iface *net.Interface
dataTaking bool
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.dataTaking = false
a.data = DaqData{
devices: make(map[byte]*net.HardwareAddr),
slowControlConfiguration: make(map[byte]map[string]any),
probeConfiguration: make(map[byte]map[string]any),
rates: mapWithMyutex[byte, float32]{internalMap: make(map[byte]float32)},
cards: make(map[byte]bool),
events: make([]EventData, 0, 10000),
charges: mapWithMyutex[byte, ChargeHistogram]{internalMap: make(map[byte]ChargeHistogram)},
chargesRebinned: mapWithMyutex[byte, ChargeHistogram]{internalMap: make(map[byte]ChargeHistogram)},
t0s: mapWithMyutex[byte, []uint32]{internalMap: make(map[byte][]uint32)},
t1s: mapWithMyutex[byte, []uint32]{internalMap: make(map[byte][]uint32)},
lostBuffer: mapWithMyutex[byte, uint32]{internalMap: make(map[byte]uint32)},
lostFPGA: mapWithMyutex[byte, uint32]{internalMap: make(map[byte]uint32)},
}
a.writerData = WriterData{}
a.sendFrameChannel = make(chan *Frame, 2000)
a.recvFrameChannel = make(chan Frame, 2000)
}
func (a *App) onshutdown(ctx context.Context) {
a.connection.Close()
}
func (a *App) ScanDevices() {
scanDevices(a.iface.HardwareAddr, a.sendFrameChannel)
}
func getOutputFilename() string {
fmt.Println("MUON DATA:", os.Getenv("MUONDATA"))
basepath := os.Getenv("MUONDATA")
pattern := filepath.Join(basepath, "muons_run_*.h5")
files, _ := filepath.Glob(pattern)
fmt.Println(files)
var latest_run int64 = 1
if len(files) > 0 {
for i := 0; i < len(files); i++ {
basename := strings.Split(files[i], ".")[0]
fnumberStr := strings.Split(basename, "_")[2]
fnumber, _ := strconv.ParseInt(fnumberStr, 10, 32)
fmt.Println("file: ", fnumber)
if fnumber >= latest_run {
latest_run = fnumber + 1
fmt.Println("latest run: ", latest_run)
}
}
}
filename := fmt.Sprintf("%s/muons_run_%d.h5", basepath, latest_run)
return filename
}
func createOutputFile(writerData *WriterData) {
fname := getOutputFilename()
h5file := openFile(fname)
writerData.file = h5file
dataset := createTable(writerData.file)
chargesArray := createChargesArray(writerData.file)
writerData.data = dataset
writerData.charges = chargesArray
}
func closeOutputFile(writerData *WriterData) {
fmt.Println("closing output file")
writerData.file.Close()
writerData.data.Close()
writerData.charges.Close()
}
func (a *App) StartRun() {
startRun(a.iface.HardwareAddr, a.sendFrameChannel)
a.dataTaking = true
a.data.nEvents = 0
runtime.EventsEmit(a.ctx, "dataTaking", a.dataTaking)
devices := maps.Values(a.data.devices)
for _, device := range devices {
card := (*device)[5]
initialize_charge_histograms(card, &a.data)
a.data.t0s.write(card, make([]uint32, 0))
a.data.t1s.write(card, make([]uint32, 0))
a.data.lostBuffer.write(card, 0)
a.data.lostFPGA.write(card, 0)
}
createOutputFile(&a.writerData)
go readAllCards(a.iface.HardwareAddr, devices, a.sendFrameChannel, a)
}
func (a *App) StopRun() {
stopRun(a.iface.HardwareAddr, a.sendFrameChannel)
// Write remaining events
writeData(a.writerData.data, &a.data.events)
writeCharges(a.writerData.charges, &a.data.events)
a.data.events = make([]EventData, 0, 10000)
// Close file and stop data taking
if a.dataTaking {
// Close the file only once
closeOutputFile(&a.writerData)
}
a.dataTaking = false
runtime.EventsEmit(a.ctx, "dataTaking", a.dataTaking)
}
func (a *App) HVOn(cardID byte) {
dst := getMacAddressDevice(cardID)
hvOn(a.iface.HardwareAddr, dst, a.sendFrameChannel)
}
func (a *App) HVOff(cardID byte) {
dst := getMacAddressDevice(cardID)
hvOff(a.iface.HardwareAddr, dst, a.sendFrameChannel)
}
func (a *App) SetVCXO(cardID byte, vcxoValue uint16) {
dst := getMacAddressDevice(cardID)
fmt.Println("set vcxo")
setVCXO(vcxoValue, a.iface.HardwareAddr, dst, a.sendFrameChannel)
}
func (a *App) UpdateCardConfig(card int, slowControl map[string]any, probe map[string]any) {
fmt.Println(card)
fmt.Println(slowControl)
fmt.Println(probe)
cardID := byte(card)
for key, value := range slowControl {
a.data.slowControlConfiguration[cardID][key] = value
}
for key, value := range probe {
a.data.probeConfiguration[cardID][key] = value
}
src := a.iface.HardwareAddr
dst := a.data.devices[cardID]
updateCardConfig(cardID, &a.data, src, *dst, a.sendFrameChannel)
}
func (a *App) UpdateGlobalConfig(slowControl map[byte]map[string]any, probe map[byte]map[string]any) {
fmt.Println(slowControl)
fmt.Println(probe)
cards := maps.Keys(slowControl)
for _, card := range cards {
a.UpdateCardConfig(int(card), slowControl[card], probe[card])
}
}
func (a *App) SelectConfigFile() string {
directory, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
options := runtime.SaveDialogOptions{
DefaultDirectory: directory,
DefaultFilename: "config.yaml",
Title: "Select file to save configuration",
TreatPackagesAsDirectories: true,
}
file, _ := runtime.SaveFileDialog(a.ctx, options)
return file
}
func (a *App) SelectCalibrationFile() string {
directory, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
options := runtime.OpenDialogOptions{
DefaultDirectory: directory,
DefaultFilename: "config.yaml",
Title: "Select file to save configuration",
TreatPackagesAsDirectories: true,
}
file, _ := runtime.OpenFileDialog(a.ctx, options)
return file
}
func (a *App) SaveConfiguration(file string) {
saveConfigYaml(&a.data, file)
}
func (a *App) LoadConfiguration(file string) {
readConfigYaml(&a.data, file)
fmt.Println("file read")
sendConfigToUI(&a.data, a.ctx)
fmt.Println("event sent")
}
type CalibrationConfig struct {
Duration int
Events int
Bias int
Gain int
Dac int
Finished bool
}
type CalibrationLog struct {
Timestamp int
Configuration CalibrationConfig
}
func (a *App) LoadCalibrationFile(filename string) {
fmt.Println(filename)
file, err := os.Open(filename)
if err != nil {
log.Fatal("Error while reading the file", err)
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
// Checks for the error
if err != nil {
fmt.Println("Error reading records")
}
// Loop to iterate through
// and print each of the string slice
for index, record := range records {
fmt.Println(record)
if index > 0 {
duration, err := strconv.Atoi(record[0])
if err != nil {
fmt.Println("error reading calibration file")
}
events, err := strconv.Atoi(record[1])
if err != nil {
fmt.Println("error reading calibration file")
}
bias, err := strconv.Atoi(record[2])
if err != nil {
fmt.Println("error reading calibration file")
}
gain, err := strconv.Atoi(record[3])
if err != nil {
fmt.Println("error reading calibration file")
}
dac, err := strconv.Atoi(record[4])
if err != nil {
fmt.Println("error reading calibration file")
}
config := CalibrationConfig{
Duration: duration,
Events: events,
Bias: bias,
Gain: gain,
Dac: dac,
}
fmt.Println(config)
for card, configuration := range a.data.slowControlConfiguration {
configuration["dac1_code"] = config.Dac
configuration["dac2_code"] = config.Dac
gains := make([]int, 32)
for i := range gains {
gains[i] = config.Gain
}
biases := make([]int, 32)
for i := range biases {
biases[i] = config.Bias
}
configuration["channel_preamp_HG"] = gains
configuration["input_dac"] = biases
fmt.Println(card, configuration)
}
runtime.EventsEmit(a.ctx, "configSlowControl", a.data.slowControlConfiguration)
a.UpdateGlobalConfig(a.data.slowControlConfiguration, a.data.probeConfiguration)
time.Sleep(1 * time.Second)
runtime.EventsEmit(a.ctx, "calibration", CalibrationLog{Timestamp: int(time.Now().Unix()), Configuration: config})
a.StartRun()
timerTotal := time.NewTimer(time.Duration(config.Duration) * time.Second)
ticker := time.NewTicker(time.Second)
done := false
for !done {
select {
case <-timerTotal.C:
done = true
case t := <-ticker.C:
fmt.Println("Tick at", t)
if a.data.nEvents > config.Events {
done = true
}
}
}
a.StopRun()
}
}
runtime.EventsEmit(a.ctx, "calibrationFginished", true)
}
func (a *App) GetNetworkInterfaces() []string {
return getNetworkInterfacesNames()
}
func (a *App) StartConnection(iface string) bool {
a.iface = getNetworkInterface(iface)
a.connection = createSocket(a.iface)
// Start go routines
go sendFrameViaSocket(a.sendFrameChannel, a.connection)
go receiveMessages(a.recvFrameChannel, a.connection, a.iface.MTU)
go decodeFrame(a.recvFrameChannel, &a.data, &a.writerData, a.ctx)
return a.connection != nil
}
func (a *App) SetDACThreshold(card byte, dacValue uint16) {
dst := getMacAddressDevice(card)
setDACThr(card, &a.data, dacValue, a.iface.HardwareAddr, dst, a.sendFrameChannel)
}