forked from SBC-Collaboration/SBC-Queens-SIPMs-GUI-Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCAENDigitizerInterface.h
590 lines (459 loc) · 14.4 KB
/
CAENDigitizerInterface.h
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#pragma once
// std includes
#include <array>
#include <cstdint>
#include <functional>
#include <memory>
#include <numeric>
#include <string>
#include <thread>
#include <chrono>
#include <random>
#include <iostream>
// 3rd party includes
#include <readerwriterqueue.h>
#include <spdlog/spdlog.h>
// my includes
#include "file_helpers.h"
#include "caen_helper.h"
#include "implot_helpers.h"
#include "include/caen_helper.h"
#include "include/timing_events.h"
#include "indicators.h"
#include "spdlog/spdlog.h"
#include "timing_events.h"
namespace SBCQueens {
enum class CAENInterfaceStates {
NullState = 0,
Standby,
AttemptConnection,
OscilloscopeMode,
StatisticsMode,
RunMode,
Disconnected,
Closing
};
struct CAENInterfaceData {
std::string RunDir = "";
std::string RunName = "";
std::string SiPMParameters = "";
CAENDigitizerModel Model;
CAENGlobalConfig GlobalConfig;
std::vector<CAENGroupConfig> GroupConfigs;
int PortNum = 0;
CAENInterfaceStates CurrentState =
CAENInterfaceStates::NullState;
};
using CAENQueueType
= std::function < bool(CAENInterfaceData&) >;
using CAENQueue
= moodycamel::ReaderWriterQueue< CAENQueueType >;
template<typename... Queues>
class CAENDigitizerInterface {
std::tuple<Queues&...> _queues;
CAENInterfaceData state_of_everything;
DataFile<CAENEvent> _pulseFile;
IndicatorSender<IndicatorNames> _plotSender;
CAEN Port = nullptr;
//tmp stuff
uint16_t* data;
size_t length;
double* x_values, *y_values;
CAENEvent osc_event, adj_osc_event;
// 1024 because the caen can only hold 1024 no matter what model (so far)
CAENEvent processing_evts[1024];
// As long as we make the Func template argument a std::fuction
// we can then use pointer magic to initialize them
using CAENInterfaceState
= BlockingTotalTimeEvent<
std::function<bool(void)>,
std::chrono::milliseconds
>;
// We need to turn the class BlockingTotalTimeEvent into a pointer
// because it does not have an empty constructor and for a good reason
// if its empty, bugs and crashes would happen all the time.
using CAENInterfaceState_ptr = std::shared_ptr<CAENInterfaceState>;
CAENInterfaceState_ptr main_loop_state;
CAENInterfaceState_ptr standby_state;
CAENInterfaceState_ptr attemptConnection_state;
CAENInterfaceState_ptr oscilloscopeMode_state;
CAENInterfaceState_ptr statisticsMode_state;
CAENInterfaceState_ptr runMode_state;
CAENInterfaceState_ptr disconnected_state;
CAENInterfaceState_ptr closing_state;
public:
explicit CAENDigitizerInterface(Queues&... queues) :
_queues(forward_as_tuple(queues...)),
_plotSender(std::get<SiPMsPlotQueue&>(_queues)) {
// This is possible because std::function can be assigned
// to whatever std::bind returns
standby_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(100),
std::bind(&CAENDigitizerInterface::standby, this)
);
attemptConnection_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(1),
std::bind(&CAENDigitizerInterface::attempt_connection, this)
);
oscilloscopeMode_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(100),
std::bind(&CAENDigitizerInterface::oscilloscope, this)
);
statisticsMode_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(1),
std::bind(&CAENDigitizerInterface::statistics_mode, this)
);
runMode_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(1),
std::bind(&CAENDigitizerInterface::run_mode, this)
);
disconnected_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(1),
std::bind(&CAENDigitizerInterface::disconnected_mode, this)
);
// auto g =
closing_state = std::make_shared<CAENInterfaceState>(
std::chrono::milliseconds(1),
std::bind(&CAENDigitizerInterface::closing_mode, this)
);
}
// No copying
CAENDigitizerInterface(const CAENDigitizerInterface&) = delete;
~CAENDigitizerInterface() {}
void operator()() {
spdlog::info("Initializing CAEN thread");
main_loop_state = standby_state;
// Actual loop!
while(main_loop_state->operator()());
}
private:
double extract_frequency(CAENEvent& evt) {
auto buf = evt->Data->DataChannel[0];
auto size = evt->Data->ChSize[0];
double counter = 0;
auto offset =
Port->GroupConfigs[0].TriggerThreshold;
for(uint32_t i = 1; i < size; i++) {
if((buf[i] > offset) && (buf[i-1] < offset)) {
counter++;
}
}
return Port->GetSampleRate()*counter /
(Port->GlobalConfig.RecordLength);
}
// Local error checking. Checks if there was an error and prints it
// using spdlog
bool lec() {
return check_error(Port,
[](const std::string& cmd) {
spdlog::error(cmd);
});
}
void switch_state(const CAENInterfaceStates& newState) {
state_of_everything.CurrentState = newState;
switch(state_of_everything.CurrentState) {
case CAENInterfaceStates::Standby:
main_loop_state = standby_state;
break;
case CAENInterfaceStates::AttemptConnection:
main_loop_state = attemptConnection_state;
break;
case CAENInterfaceStates::OscilloscopeMode:
main_loop_state = oscilloscopeMode_state;
break;
case CAENInterfaceStates::StatisticsMode:
main_loop_state = statisticsMode_state;
break;
case CAENInterfaceStates::RunMode:
main_loop_state = runMode_state;
break;
case CAENInterfaceStates::Disconnected:
main_loop_state = disconnected_state;
break;
case CAENInterfaceStates::Closing:
main_loop_state = closing_state;
break;
case CAENInterfaceStates::NullState:
default:
// do nothing other than set to standby state
main_loop_state = standby_state;
}
}
// Envelopes the logic that listens to an external queue
// that can the data inside this thread.
bool change_state() {
// GUI -> CAEN
CAENQueue& guiQueueOut = std::get<CAENQueue&>(_queues);
CAENQueueType task;
// If the queue does not return a valid function, this call will
// do nothing and should return true always.
// The tasks are essentially any GUI driven modification, example
// setting the PID setpoints or constants
// or an user driven reset
if(guiQueueOut.try_dequeue(task)) {
if(!task(state_of_everything)) {
spdlog::warn("Something went wrong with a command!");
} else {
switch_state(state_of_everything.CurrentState);
return true;
}
}
return false;
}
// Does nothing other than wait 100ms to avoid clogging PC resources.
bool standby() {
change_state();
return true;
}
// Attempts a connection to the CAEN digitizer, setups the channels
// and starts acquisition
bool attempt_connection() {
auto err = connect_usb(Port,
state_of_everything.Model,
state_of_everything.PortNum);
// If port resource was not created, it equals a failure!
if(!Port) {
// Print what was the error
check_error(err, [](const std::string& cmd) {
spdlog::error(cmd);
});
// We disconnect because this will free resources (in case
// they were created)
disconnect(Port);
switch_state(CAENInterfaceStates::Standby);
return true;
}
spdlog::info("Connected to CAEN Digitizer!");
reset(Port);
setup(Port,
state_of_everything.GlobalConfig,
state_of_everything.GroupConfigs);
enable_acquisition(Port);
// Allocate memory for events
osc_event = std::make_shared<caenEvent>(Port->Handle);
for(CAENEvent& evt : processing_evts) {
evt = std::make_shared<caenEvent>(Port->Handle);
}
auto failed = lec();
if(failed) {
spdlog::warn("Failed to setup CAEN");
err = disconnect(Port);
check_error(err, [](const std::string& cmd) {
spdlog::error(cmd);
});
switch_state(CAENInterfaceStates::Standby);
} else {
spdlog::info("CAEN Setup complete!");
switch_state(CAENInterfaceStates::OscilloscopeMode);
}
change_state();
return true;
}
// While in this state it shares the data with the GUI but
// no actual file saving is happening. It essentially serves
// as a mode in where the user can see what is happening.
// Similar to an oscilloscope
bool oscilloscope() {
auto events = get_events_in_buffer(Port);
_plotSender(IndicatorNames::CAENBUFFEREVENTS, events);
retrieve_data(Port);
if(Port->Data.DataSize > 0 && Port->Data.NumEvents > 0) {
// spdlog::info("Total size buffer: {0}", Port->Data.TotalSizeBuffer);
// spdlog::info("Data size: {0}", Port->Data.DataSize);
// spdlog::info("Num events: {0}", Port->Data.NumEvents);
extract_event(Port, 0, osc_event);
// spdlog::info("Event size: {0}", osc_event->Info.EventSize);
// spdlog::info("Event counter: {0}", osc_event->Info.EventCounter);
// spdlog::info("Trigger Time Tag: {0}", osc_event->Info.TriggerTimeTag);
process_data_for_gui();
// if(port->Data.NumEvents >= 2) {
// extract_event(port, 1, adj_osc_event);
// auto t0 = osc_event->Info.TriggerTimeTag;
// auto t1 = adj_osc_event->Info.TriggerTimeTag;
// spdlog::info("Time difference between events: {0}", t1 - t0);
// }
}
// Clear events in buffer
clear_data(Port);
lec();
change_state();
return true;
}
// Does the SiPM pulse processing but no file saving
// is done. Serves more for the user to know
// how things are changing.
bool statistics_mode() {
static auto process_events = [&]() {
bool isData = retrieve_data_until_n_events(Port,
Port->GlobalConfig.MaxEventsPerRead);
// While all of this is happening, the digitizer is taking data
if(!isData) {
return;
}
//double frequency = 0.0;
for(uint32_t i = 0; i < Port->Data.NumEvents; i++) {
// Extract event i
extract_event(Port, i, processing_evts[i]);
}
};
static auto extract_for_gui_nb = make_total_timed_event(
std::chrono::milliseconds(200),
std::bind(&CAENDigitizerInterface::rdm_extract_for_gui, this)
);
static auto checkerror = make_total_timed_event(
std::chrono::seconds(1),
std::bind(&CAENDigitizerInterface::lec, this)
);
process_events();
checkerror();
extract_for_gui_nb();
//extract_for_gui_nb();
change_state();
return true;
}
bool run_mode() {
static bool isFileOpen = false;
static auto extract_for_gui_nb = make_total_timed_event(
std::chrono::milliseconds(200),
std::bind(&CAENDigitizerInterface::rdm_extract_for_gui, this)
);
static auto process_events = [&]() {
bool isData = retrieve_data_until_n_events(Port,
Port->GlobalConfig.MaxEventsPerRead);
// While all of this is happening, the digitizer is taking data
if(!isData) {
return;
}
//double frequency = 0.0;
for(uint32_t i = 0; i < Port->Data.NumEvents; i++) {
// Extract event i
extract_event(Port, i, processing_evts[i]);
if(_pulseFile) {
// Copy event to the file buffer
_pulseFile->Add(processing_evts[i]);
}
}
};
if(isFileOpen) {
// spdlog::info("Saving SIPM data");
save(_pulseFile, sbc_save_func, Port);
} else {
// Get current date and time, e.g. 202201051103
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_t = std::chrono::system_clock::to_time_t(now);
char filename[16];
std::strftime(filename, sizeof(filename), "%Y%m%d%H%M",
std::localtime(&now_t));
open(_pulseFile,
state_of_everything.RunDir
+ "/" + state_of_everything.RunName
+ "/" + filename + ".bin",
// + state_of_everything.SiPMParameters + ".bin",
// sbc_init_file is a function that saves the header
// of the sbc data format as a function of record length
// and number of channels
sbc_init_file,
Port);
isFileOpen = _pulseFile > 0;
}
process_events();
extract_for_gui_nb();
if(change_state()) {
// save remaining data
retrieve_data(Port);
if (isFileOpen) {
for(uint32_t i = 0; i < Port->Data.NumEvents; i++) {
extract_event(Port, i, processing_evts[i]);
_pulseFile->Add(processing_evts[i]);
}
save(_pulseFile, sbc_save_func, Port);
}
isFileOpen = false;
close(_pulseFile);
}
return true;
}
bool disconnected_mode() {
spdlog::warn("Manually losing connection to the "
"CAEN digitizer.");
for(CAENEvent& evt : processing_evts) {
evt.reset();
}
osc_event.reset();
adj_osc_event.reset();
auto err = disconnect(Port);
check_error(err, [](const std::string& cmd) {
spdlog::error(cmd);
});
switch_state(CAENInterfaceStates::Standby);
return true;
}
bool closing_mode() {
spdlog::info("Going to close the CAEN thread.");
for(CAENEvent& evt : processing_evts) {
evt.reset();
}
osc_event.reset();
adj_osc_event.reset();
auto err = disconnect(Port);
check_error(err, [](const std::string& cmd) {
spdlog::error(cmd);
});
return false;
}
void rdm_extract_for_gui() {
if(Port->Data.DataSize <= 0) {
return;
}
// For the GUI
std::default_random_engine generator;
std::uniform_int_distribution<uint32_t>
distribution(0, Port->Data.NumEvents);
uint32_t rdm_num = distribution(generator);
extract_event(Port, rdm_num, osc_event);
process_data_for_gui();
};
void process_data_for_gui() {
// int j = 0;
//TODO(Zhiheng): change anything you need here, too...
// for(auto ch : Port->GroupConfigs) {
for (int j=0; j<3; j++) {
// auto& ch_num = ch.second.Number;
auto buf = osc_event->Data->DataChannel[j];
auto size = osc_event->Data->ChSize[j];
//spdlog::info("Event size size: {0}", size);
if(size <= 0) {
continue;
}
x_values = new double[size];
y_values = new double[size];
for(uint32_t i = 0; i < size; i++) {
x_values[i] = i*(1.0/Port->GetSampleRate())*(1e9);
y_values[i] = static_cast<double>(buf[i]);
}
IndicatorNames plotToSend;
switch(j) {
case 1:
plotToSend = IndicatorNames::SiPM_Plot_ONE;
break;
case 2:
plotToSend = IndicatorNames::SiPM_Plot_TWO;
break;
case 3:
plotToSend = IndicatorNames::SiPM_Plot_THREE;
break;
case 0:
default:
plotToSend = IndicatorNames::SiPM_Plot_ZERO;
}
_plotSender(plotToSend,
x_values,
y_values,
size);
delete x_values;
delete y_values;
// j++;
}
}
};
} // namespace SBCQueens