EventData.cpp 8.2 KB
Newer Older
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
/*
 * Nomad Instrument Control Software
 *
 * Copyright 2011 Institut Laue-Langevin
 *
 * Licensed under the EUPL, Version 1.1 only (the "License");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://www.osor.eu/eupl
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 */

#include "EventData.h"
#include <iomanip>

using namespace std;

namespace lstdpp128 {

inline float floatData(Event const & event, int index) {
	return *reinterpret_cast<const float *>(&event.data[index]);
}

uint32_t eventData(Event const & event) {
	return event.data[0];
}

uint32_t eventDataPlus(Event const & event) {
	return event.data[1];
}

///////////////////////////////////////////////////////////////////////////////
// Energy

bool boardHasEnergy(BoardType type) {

	if (type == CFD
	 || type == C1724_PHA_RT
	 || type == C1730_PHA_RT
	 || type == C1740_QDC) {
		return true;
	}
	return false;
}

bool eventHasEnergy(Event const & event) {
	return boardHasEnergy(event.boardType());
}

uint16_t boardEnergyResolution(BoardType type) {

	switch (type) {
	case CFD:
		return 12;

	case C1724_PHA_RT:
		return 15;

	case C1730_PHA_RT:
		return 15;

	case C1740_QDC:
		return 16;
	}

	return 0;
}

uint16_t eventEnergyResolution(Event const & event) {
	return boardEnergyResolution(event.boardType());
}

uint16_t eventEnergy(Event const & event) {

	if (event.boardType() == CFD
	 || event.boardType() == C1724_PHA_RT
	 || event.boardType() == C1730_PHA_RT
	 || event.boardType() == C1740_QDC) {

		return event.data[0] & 0xFFFF;
	}

	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// Q

bool boardHasQ(BoardType type) {

	if (type == C1751_PSD
	 || type == C1730_PSD_RT) {
		return true;
	}
	return false;
}

bool eventHasQ(Event const & event) {
	return boardHasQ(event.boardType());
}

uint16_t boardQShortResolution(BoardType type) {

	if (type == C1751_PSD
	 || type == C1730_PSD_RT) {
		return 15;
	}
	return 0;
}

uint16_t eventQShortResolution(Event const & event) {
	return boardQShortResolution(event.boardType());
}

uint16_t boardQLongResolution(BoardType type) {

	if (type == C1751_PSD
	 || type == C1730_PSD_RT) {
		return 16;
	}
	return 0;
}

uint16_t eventQLongResolution(Event const & event) {
	return boardQLongResolution(event.boardType());
}

uint16_t eventQShort(Event const & event) {

	// data = qlong 16bits | 1bit | qshort 15bits
	return event.data[0] & 0x7FFF;
}

uint16_t eventQLong(Event const & event) {

	// data = qlong 16bits | 1bit | qshort 15bits
	return (event.data[0] >> 16);

}

float eventQRatio(Event const & event) {

	uint16_t qLong = eventQLong(event);
	if (qLong == 0) {
		return 0.0f;
	}

	return (static_cast<float>(qLong) - static_cast<float>(eventQShort(event))) / static_cast<float>(qLong);
}


///////////////////////////////////////////////////////////////////////////////
// Position XY

bool boardHasPositionXY(BoardType type) {

	if (type == C1740_QDIV
	 || type == MCC
	 || type == CAMERA) {
		return true;
	}
	return false;
}

bool eventHasPositionXY(Event const & event) {
	return boardHasPositionXY(event.boardType());
}

uint16_t boardPositionXResolution(BoardType type) {

	switch (type) {
	case C1740_QDIV:
		return 5;

	case MCC:
		return 16;

	case CAMERA:
		return 16;
	}

	return 0;
}

uint16_t eventPositionXResolution(Event const & event) {
	return boardPositionXResolution(event.boardType());
}

uint16_t boardPositionYResolution(BoardType type) {

	switch (type) {
	case C1740_QDIV:
		return 8;

	case MCC:
		return 16;

	case CAMERA:
		return 16;
	}

	return 0;
}

uint16_t eventPositionYResolution(Event const & event) {
	return boardPositionYResolution(event.boardType());
}

uint16_t eventPositionX(Event const & event) {

	// data = Position X 16bits | Position Y 16bits
	return (event.data[0] >> 16) & 0xFFFF;
}

uint16_t eventPositionY(Event const & event) {

	// data = Position X 16bits | Position Y 16bits
	return event.data[0] & 0xFFFF;
}


///////////////////////////////////////////////////////////////////////////////
// A, B

bool boardHasAB(BoardType type) {

	if (type == C1740_QDIV) {
		return true;
	}
	return false;
}

bool eventHasAB(Event const & event) {
	return boardHasAB(event.boardType());
}

uint16_t boardAResolution(BoardType type) {

	if (type == C1740_QDIV) {
		return 16;
	}
	return 0;
}

uint16_t eventAResolution(Event const & event) {
	return boardAResolution(event.boardType());
}

uint16_t boardAplusBResolution(BoardType type) {

	if (type == C1740_QDIV) {
		return 16;
	}
	return 0;
}

uint16_t eventAplusBResolution(Event const & event) {
	return boardAplusBResolution(event.boardType());
}

uint16_t eventA(Event const & event) {

	// data = A+B 16bits | A 16bits
	return event.data[1] & 0xFFFF;
}

uint16_t eventAplusB(Event const & event) {

	// data = A+B 16bits | A 16bits
	return (event.data[1] >> 16) & 0xFFFF;
}


///////////////////////////////////////////////////////////////////////////////
// Zero Crossing

bool boardHasZeroCrossing(BoardType type) {
	if (type == CFD) {
		return true;
	}
	return false;
}

bool eventHasZeroCrossing(Event const & event) {
	return boardHasZeroCrossing(event.boardType());
}

uint16_t boardZeroCrossingResolution(BoardType type) {
	if (type == CFD) {
		return 32;
	}
	return 0;
}

uint16_t eventZeroCrossingResolution(Event const & event) {
	return boardZeroCrossingResolution(event.boardType());
}

float eventZeroCrossing(Event const & event) {
	return floatData(event, 1);
}


///////////////////////////////////////////////////////////////////////////////
// Flags

uint8_t boardHasFlags(BoardType type) {

	if (type == C1751_PSD) {
		return PILE_UP;
	}

	if (type == C1730_PSD_RT) {
		return PILE_UP | TRIGGER_LOST | N_TRIGGER_COUNTED | N_LOST_TRIGGER_COUNTED;
	}

	if (type == C1724_PHA_RT) {
		return PILE_UP | DEAD_TIME_OR_LOST_EVENTS;
	}

	if (type == C1730_PHA_RT) {
		return PILE_UP | DEAD_TIME_OR_LOST_EVENTS | OVER_RANGE | N_TRIGGER_COUNTED | N_LOST_TRIGGER_COUNTED;
	}

	if (type == C1740_QDC) {
		return PILE_UP | OVER_RANGE;
	}

	return 0;
}

uint8_t eventHasFlags(Event const & event) {
	return boardHasFlags(event.boardType());
}

uint8_t eventFlags(Event const & event) {

	if (eventHasFlags(event) != 0) {
		// Bits
		//	31 : PU (Pile Up)
		//	30 : Dead Time / Lost Event(s)
		//	29 : Over-Range / Saturation
		//	28 : Trigger Lost
		//	27 : N Trigger Counted
		//	26 : N Lost Trigger Counted
		// Shift 26bits.
		return (event.data[1] >> 26);
	}

	return 0;
}

///////////////////////////////////////////////////////////////////////////////
// To string

std::string eventDataColumnsToString(BoardType type) {

	ostringstream os;

	// data[0]
	if (boardHasEnergy(type)) {
		os << setw(12) << "energy";
	}

	if (boardHasQ(type)) {
		os << setw(12) << "q-short";
		os << setw(12) << "q-long";
	}

	if (boardHasAB(type)) {
		os << setw(12) << "A";
		os << setw(12) << "A+B";
	}

	if (boardHasPositionXY(type)) {
		os << setw(12) << "X";
		os << setw(12) << "Y";
	}

	// data[1]
	if (boardHasZeroCrossing(type)) {
		os << setw(12) << "0-crossing";
	}

	// flags.
	if (boardHasFlags(type) != 0) {
		os << setw(10) << "flags";
	}

	return os.str();
}

std::string eventDataToString(Event const & event) {

	ostringstream os;

	// data[0]
	if (eventHasEnergy(event)) {
		os << setw(12) << eventEnergy(event);
	}

	if (eventHasQ(event)) {
		os << setw(12) << eventQShort(event);
		os << setw(12) << eventQLong(event);
	}

	if (eventHasAB(event)) {
		os << setw(12) << eventA(event);
		os << setw(12) << eventAplusB(event);
	}

	if (eventHasPositionXY(event)) {
		os << setw(12) << eventPositionX(event);
		os << setw(12) << eventPositionY(event);
	}

	// data[1]
	if (eventHasZeroCrossing(event)) {
		os << setw(12) << eventZeroCrossing(event);
	}

	if (eventHasFlags(event) != 0) {

		ostringstream bos;

		uint8_t flags = eventFlags(event);
		for (int i = 7; i >= 0; --i) {
			uint8_t flag = (flags >> i) & 0x1;
			if (flag == 1) {
				bos << "1";
			}
			else {
				bos << "0";
			}
		}

		os << setw(10) << bos.str();
	}

	return os.str();
}

}