-
Notifications
You must be signed in to change notification settings - Fork 8
/
datastream.h
457 lines (358 loc) · 10.4 KB
/
datastream.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
// datastream.h
#pragma once
#include <stdint.h>
#include <list>
#include <vector>
#include "config.h"
#include "psi46test.h"
#include "datapipe.h"
#include "protocol.h"
#include "histo.h"
using namespace std;
// === Error Messages =======================================================
DATAPIPE_ERROR(DS_no_dtb_access, "No DTB connection")
DATAPIPE_ERROR(DS_buffer_overflow, "Buffer overflow")
DATAPIPE_ERROR(DS_empty, "Buffer empty")
// === Binary Data Record Format ============================================
class CDataRecord
{
/*
bit 0 = misaligned start
bit 1 = no end detected
bit 2 = overflow
*/
vector<uint16_t> data;
unsigned int flags;
public:
void SetStartError() { flags |= 1; }
void SetEndError() { flags |= 2; }
void SetOverflow() { flags |= 4; }
void ResetStartError() { flags &= ~1; }
void ResetEndError() { flags &= ~2; }
void ResetOverflow() { flags &= ~4; }
void Clear() { flags = 0; data.clear(); }
bool IsStartError() { return (flags & 1) != 0; }
bool IsEndError() { return (flags & 2) != 0; }
bool IsOverflow() { return (flags & 4) != 0; }
unsigned int recordNr;
void Add(uint16_t value) { data.push_back(value); }
unsigned int GetSize() { return data.size(); }
uint16_t& operator[](int index) { return data[index]; }
};
class CAnalogLevelDecoder
{
int level0;
int level1;
int levelS;
public:
void Calibrate(int ublackLevel, int blackLevel);
static int ExpandSign(uint16_t x) { return (x & 0x0800) ? int(x) - 4096 : int(x); }
int Translate(uint16_t x);
int CorrectOffset(uint16_t x) { return ExpandSign(x) - level0; }
};
struct CRocPixel
{
// error bits:{ ph | x | y | c1 | c0 | r2 | r1 | r0 }
int error;
int raw;
int x;
int y;
int ph;
void DecodeRaw(); // PSI46dig
void DecodeRawLinear(); // PROC600
void DecodeAna(CAnalogLevelDecoder &dec, uint16_t *x); // PSI46 analog
};
struct CRocEvent
{
/* error bits:
0: pixel error
*/
unsigned int error;
unsigned short header;
vector<CRocPixel> pixel;
};
struct CEvent
{
unsigned int recordNr;
/* error bits:
0: pixel error
1: missing TBM trailer or ROC header after TBM header
2: missing TBM trailer before idle pattern
3: code error during event data transmission
4: frame error during event data transmission
5: TBM trailer error
6:
7:
8: T1 missing
9: T0 missing
10: H1 missing
11: H0 missing
*/
int error;
/*
ROCA PSI46 single ROC
ROCD PSI46dig single ROC
ROCX PROC600 single ROC
MODA PSI46 module (analog)
MODD PSI46dig module
MODX PROC600 module
*/
enum DeviceType { ROCX, ROCD, ROCA, MODX, MODD, MODA } deviceType;
unsigned short header;
unsigned short trailer;
vector<CRocEvent> roc;
};
// === Data Sources =========================================================
// --- PixelDTB
#define DTB_SOURCE_BLOCK_SIZE 65536
class CDtbSource : public CSource<uint16_t>
{
bool isOpen;
bool logging;
unsigned int channel;
unsigned int dtbFifoSize;
volatile bool stopAtEmptyData;
// --- DTB control/state
CTestboard *tb;
uint32_t dtbRemainingSize;
uint8_t dtbState;
// --- data buffer
uint16_t lastSample;
unsigned int pos;
vector<uint16_t> buffer;
uint16_t FillBuffer();
// --- virtual data access methods
uint16_t Read() { return (pos < buffer.size()) ? lastSample = buffer[pos++] : FillBuffer(); }
uint16_t ReadLast() { return lastSample; }
bool Open(CTestboard &dtb, unsigned int dataChannel,
bool endless, unsigned int dtbBufferSize);
public:
CDtbSource() : isOpen(false), logging(false) {}
~CDtbSource() { Close(); }
bool OpenRocAna(CTestboard &dtb, uint8_t tinDelay, uint8_t toutDelay, uint16_t timeout,
bool endless = true, unsigned int dtbBufferSize = 5000000);
bool OpenRocDig(CTestboard &dtb, uint8_t deserAdjust,
bool endless = true, unsigned int dtbBufferSize = 5000000);
bool OpenModDig(CTestboard &dtb, unsigned int channel, bool endless = true, unsigned int dtbBufferSize = 5000000);
bool OpenSimulator(CTestboard &dtb,
bool endless = true, unsigned int dtbBufferSize = 5000000);
void Close();
void Logging(bool on) { logging = on; }
void Enable();
void Disable();
void Clear() { Disable(); Enable(); }
// --- control and status
uint8_t GetState() { return dtbState; }
uint32_t GetRemainingSize() { return dtbRemainingSize; }
void Stop() { stopAtEmptyData = true; }
};
// --- File
#define FILE_SOURCE_BLOCK_SIZE 16384
class CBinaryFileSource : public CSource<uint16_t>
{
FILE *f;
uint16_t lastSample;
unsigned int size;
unsigned int pos;
vector<uint16_t> buffer;
uint16_t FillBuffer();
uint16_t Read() { return (pos < size) ? lastSample = buffer[pos++] : FillBuffer(); }
uint16_t ReadLast() { return lastSample; }
public:
CBinaryFileSource() : f(0), lastSample(0), size(0), pos(0) { buffer.reserve(FILE_SOURCE_BLOCK_SIZE); }
~CBinaryFileSource() { Close(); }
bool Open(const char *filename) { return (f = fopen(filename, "rb")) != 0; }
void Close() { if (f) { fclose(f); f = 0; } }
};
// === CStreamDump (uint16_t, uint16_t) ==============
class CStreamDump : public CDataPipe<uint16_t>
{
FILE *f;
int row;
uint16_t x;
uint16_t Read();
uint16_t ReadLast() { return x; }
public:
CStreamDump(const char *filename) { row = 0; f = fopen(filename, "wt"); }
~CStreamDump() { fclose(f); }
};
// === CStreamErrorDump (uint16_t, uint16_t) ==============
class CStreamErrorDump : public CDataPipe<uint16_t>
{
FILE *f;
bool good;
unsigned int m, n1, n2;
uint16_t x;
uint16_t Read();
uint16_t ReadLast() { return x; }
public:
CStreamErrorDump(const char *filename) { good = true; m = n1 = n2 = 0; f = fopen(filename, "wt"); }
~CStreamErrorDump() { fclose(f); }
unsigned int ByteCount() { return n2; }
};
// === CDataRecordScannerROCD (uint16_t, CDataRecord*) ==============
class CDataRecordScannerROC : public CDataPipe<uint16_t, CDataRecord*>
{
unsigned int recCounter;
bool nextStartDetected;
CDataRecord record;
CDataRecord* Read();
CDataRecord* ReadLast() { return &record; }
public:
CDataRecordScannerROC() : recCounter(0), nextStartDetected(false) {}
};
// === CDataRecordScannerMODD (uint16_t, CDataRecord*) ==============
class CDataRecordScannerMODD_old : public CDataPipe<uint16_t, CDataRecord*>
{
unsigned int recCounter;
bool nextStartDetected;
CDataRecord record;
CDataRecord* Read();
CDataRecord* ReadLast() { return &record; }
public:
CDataRecordScannerMODD_old() : recCounter(0), nextStartDetected(false) {}
};
class CDataRecordScannerMODD : public CDataPipe<uint16_t, CDataRecord*>
{
unsigned int recCounter;
bool nextStartDetected;
CDataRecord record;
CDataRecord* Read();
CDataRecord* ReadLast() { return &record; }
public:
CDataRecordScannerMODD() : recCounter(0), nextStartDetected(false) {}
};
// === CRocRawDataPrinter (CDataRecord*, CDataRecord*) ==============
class CRocRawDataPrinter : public CDataPipe<CDataRecord*, CDataRecord*>
{
FILE *f;
bool adc_samples;
CDataRecord* record;
CDataRecord* Read();
CDataRecord* ReadLast() { return record; }
public:
CRocRawDataPrinter(const char *filename, bool roc_ana = false)
{ adc_samples = roc_ana, f = fopen(filename, "wt"); }
~CRocRawDataPrinter() { fclose(f); }
};
// === CLevelHistogram (CDataRecord*, CDataRecord*) ==============
class CLevelHistogram : public CDataPipe<CDataRecord*>
{
CHistogram h;
CDataRecord* x;
CDataRecord* Read();
CDataRecord* ReadLast() { return x; }
public:
CLevelHistogram() : h(-500,500,1) {}
~CLevelHistogram() {}
void Report(CProtocol &log) { h.Print(log, 10); }
};
// === CReadBack (CDataRecord*, CDataRecord*) ====================
class CReadBack : public CDataPipe<CDataRecord*>
{
unsigned int count;
unsigned int shiftReg;
unsigned int data;
bool updated;
bool valid;
CDataRecord* x;
CDataRecord* Read();
CDataRecord* ReadLast() { return x; }
public:
CReadBack() : count(0), shiftReg(0), data(0), updated(false), valid(false) {}
~CReadBack() {}
bool IsUpdated() { return updated; }
bool IsValid() { return valid; }
unsigned int GetRdbData() { updated = false; return data; }
};
// === CRocAnaDecoder (CDataRecord*, CEvent*) ============================
// PSI46 analog
class CRocAnaDecoder : public CDataPipe<CDataRecord*, CEvent*>
{
CAnalogLevelDecoder dec;
CEvent x;
CEvent* Read();
CEvent* ReadLast() { return &x; }
public:
void Calibrate(int ublackLevel, int blackLevel)
{ dec.Calibrate(ublackLevel, blackLevel); }
};
// === CRocDigDecoder (CDataRecord*, CEvent*) ============================
// PSI46dig
class CRocDigDecoder : public CDataPipe<CDataRecord*, CEvent*>
{
CEvent x;
CEvent* Read();
CEvent* ReadLast() { return &x; }
};
// === CRocDigLinearDecoder (CDataRecord*, CEvent*) ============================
// PROC600
class CRocDigLinearDecoder : public CDataPipe<CDataRecord*, CEvent*>
{
CEvent x;
CEvent* Read();
CEvent* ReadLast() { return &x; }
};
// === CModDigDecoder_old (CDataRecord*, CEvent*) ============================
class CModDigDecoder_old : public CDataPipe<CDataRecord*, CEvent*>
{
CEvent x;
CEvent* Read();
CEvent* ReadLast() { return &x; }
};
// === CModDigDecoder (CDataRecord*, CEvent*) ============================
class CModDigDecoder : public CDataPipe<CDataRecord*, CEvent*>
{
CEvent x;
CEvent* Read();
CEvent* ReadLast() { return &x; }
};
// === CModDigLinearDecoder (CDataRecord*, CEvent*) ======================
class CModDigLinearDecoder : public CDataPipe<CDataRecord*, CEvent*>
{
CEvent x;
CEvent* Read();
CEvent* ReadLast() { return &x; }
};
// === CAnalyzer (CEvent*, CEvent*) ===================================
class CAnalyzer : public CDataPipe<CEvent*>
{
protected:
CEvent* x;
CEvent* Read() { return x = Get(); };
CEvent* ReadLast() { return x; }
};
// === CEventPrinter (CEvent*, CEvent*) ============================
class CEventPrinter : public CAnalyzer
{
FILE *f;
bool listAll;
CEvent* Read();
public:
CEventPrinter(const char *filename)
{ x = 0; listAll = true; f = fopen(filename, "wt"); }
~CEventPrinter() { fclose(f); }
void ListOnlyErrors(bool on) { listAll = !on; }
};
// === CReadBackLogger(CEvent*, CEvent*) ============================
class CReadbackValue
{
bool updated;
unsigned short n;
unsigned short shift;
unsigned short value;
public:
CReadbackValue() : updated(false), n(0), shift(0), value(0) {}
void Reset() { updated = false; n = 0; shift = 0; value = 0; }
void Add(unsigned int v);
bool Get(unsigned short &rdbValue);
};
class CReadbackLogger : public CAnalyzer
{
FILE *f;
CReadbackValue rdb[8];
CEvent* Read();
public:
CReadbackLogger(const char *filename) { f = fopen(filename, "wt"); }
~CReadbackLogger() { fclose(f); }
};