-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFERSA5202Unpacker.cpp
271 lines (232 loc) · 7.48 KB
/
CFERSA5202Unpacker.cpp
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
#include "CFERSA5202Unpacker.h"
#include <string>
#include <stdexcept>
#include <iostream>
#include "FERS_constant.h"
using namespace std;
template <typename T>
T CFERSA5202Unpacker::getCombined(Iter &iter) {
size_t outputBitWidth = sizeof(T)*8;
size_t inputBitWidth = sizeof(uint16_t)*8;
int totalORs = outputBitWidth/inputBitWidth;
T value = 0;
for (int i = 0; i < totalORs; i++) {
value |= (T) *iter++ << i*inputBitWidth;
}
return value;
}
void
CFERSA5202Unpacker::parseCommonHeader(Iter& iter, ParsedFERSA5202Event &anEvent) {
int eventSize = *iter++;
TranslatorPointer<uint8_t> oneByteIter(iter);
anEvent.board_id = *oneByteIter++;
iter = oneByteIter;
anEvent.tstamp_us = (double)getCombined<uint64_t>(iter);
anEvent.trigger_id = getCombined<uint64_t>(iter);
anEvent.chmask = getCombined<uint64_t>(iter);
}
void
CFERSA5202Unpacker::initialize(ParsedFERSA5202Event &anEvent) {
anEvent.tstamp_us = 0;
anEvent.board_id = 0;
anEvent.nhits = 0;
anEvent.trigger_id = 0;
anEvent.chmask = 0;
for (int iCh = 0; iCh < 64; iCh++) {
anEvent.hasEnergyHG[iCh] = 0;
anEvent.energyHG[iCh] = 0;
anEvent.hasEnergyLG[iCh] = 0;
anEvent.energyLG[iCh] = 0;
anEvent.hasToA_int[iCh] = 0;
anEvent.ToA_int[iCh] = 0;
anEvent.hasToA_float[iCh] = 0;
anEvent.ToA_float[iCh] = 0.f;
anEvent.hasToT_int[iCh] = 0;
anEvent.ToT_int[iCh] = 0;
anEvent.hasToT_float[iCh] = 0;
anEvent.ToT_float[iCh] = 0.f;
anEvent.hasCounts[iCh] = 0;
anEvent.counts[iCh] = 0;
}
}
vector<ParsedFERSA5202Event>
CFERSA5202Unpacker::parseAll(const Iter& begin,
const uint16_t metadata)
{
vector<ParsedFERSA5202Event> parsedData;
int acqmode = (metadata & 0xFF00) >> 8;
int timeunit = metadata & 0XFF;
auto iter = begin;
ParsedFERSA5202Event anEvent;
initialize(anEvent);
switch (acqmode) {
case ACQMODE_SPECT:
{
parseCommonHeader(iter, anEvent);
int numChannels = __builtin_popcountll(anEvent.chmask);
for (int iCh = 0; iCh < numChannels; iCh++) {
TranslatorPointer<uint8_t> oneByteIter(iter);
int chIdx = *oneByteIter++;
int dataType = *oneByteIter++;
iter = oneByteIter;
if (dataType & DATATYPE_LG) {
anEvent.hasEnergyLG[chIdx] = true;
anEvent.energyLG[chIdx] = *iter++;
}
if (dataType & DATATYPE_HG) {
anEvent.hasEnergyHG[chIdx] = true;
anEvent.energyHG[chIdx] = *iter++;
}
}
parsedData.push_back(anEvent);
break;
};
case ACQMODE_SPECTIMING:
{
parseCommonHeader(iter, anEvent);
int numChannels = __builtin_popcountll(anEvent.chmask);
switch (timeunit) {
case TIMEUNIT_LSB:
{
for (int iCh = 0; iCh < numChannels; iCh++) {
TranslatorPointer<uint8_t> oneByteIter(iter);
int chIdx = *oneByteIter++;
int dataType = *oneByteIter++;
iter = oneByteIter;
if (dataType & DATATYPE_LG) {
anEvent.hasEnergyLG[chIdx] = true;
anEvent.energyLG[chIdx] = *iter++;
}
if (dataType & DATATYPE_HG) {
anEvent.hasEnergyHG[chIdx] = true;
anEvent.energyHG[chIdx] = *iter++;
}
if (dataType & DATATYPE_TOA) {
anEvent.hasToA_int[chIdx] = true;
anEvent.ToA_int[chIdx] = getCombined<uint32_t>(iter);
}
if (dataType & DATATYPE_TOT) {
anEvent.hasToT_int[chIdx] = true;
anEvent.ToT_int[chIdx] = *iter++;
}
}
break;
}
case TIMEUNIT_NS:
{
for (int iCh = 0; iCh < numChannels; iCh++) {
TranslatorPointer<uint8_t> oneByteIter(iter);
int chIdx = *oneByteIter++;
int dataType = *oneByteIter++;
iter = oneByteIter;
if (dataType & DATATYPE_LG) {
anEvent.hasEnergyLG[chIdx] = true;
anEvent.energyLG[chIdx] = *iter++;
}
if (dataType & DATATYPE_HG) {
anEvent.hasEnergyHG[chIdx] = true;
anEvent.energyHG[chIdx] = *iter++;
}
if (dataType & DATATYPE_TOA) {
anEvent.hasToA_float[chIdx] = true;
uint32_t tempValue = getCombined<uint32_t>(iter);
memcpy(&anEvent.ToA_float[chIdx], &tempValue, sizeof(float));
}
if (dataType & DATATYPE_TOT) {
anEvent.hasToT_float[chIdx] = true;
uint32_t tempValue = getCombined<uint32_t>(iter);
memcpy(&anEvent.ToT_float[chIdx], &tempValue, sizeof(float));
}
}
break;
}
default:
{
string errmsg = "Invalid TimeUnit! (" + to_string(timeunit) + ")";
throw runtime_error(errmsg);
}
}
parsedData.push_back(anEvent);
break;
}
case ACQMODE_COUNTING:
{
parseCommonHeader(iter, anEvent);
int numChannels = __builtin_popcountll(anEvent.chmask);
for (int iCh = 0; iCh < numChannels; iCh++) {
TranslatorPointer<uint8_t> oneByteIter(iter);
int chIdx = *oneByteIter++;
iter = oneByteIter;
anEvent.hasCounts[chIdx] = true;
anEvent.counts[chIdx] = getCombined<uint32_t>(iter);
}
parsedData.push_back(anEvent);
break;
}
case ACQMODE_TIMING:
{
int eventSize = *iter++;
TranslatorPointer<uint8_t> oneByteIter(iter);
anEvent.board_id = *oneByteIter++;
iter = oneByteIter;
anEvent.tstamp_us = (double) getCombined<uint64_t>(iter);
anEvent.nhits = *iter++;
switch (timeunit) {
case TIMEUNIT_LSB:
{
for (int iHit = 0; iHit < anEvent.nhits; iHit++) {
TranslatorPointer<uint8_t> oneByteIter(iter);
int chIdx = *oneByteIter++;
int dataType = *oneByteIter++;
iter = oneByteIter;
if (dataType & DATATYPE_TOA) {
anEvent.hasToA_int[chIdx] = true;
anEvent.ToA_int[chIdx] = getCombined<uint32_t>(iter);
}
if (dataType & DATATYPE_TOT) {
anEvent.hasToT_int[chIdx] = true;
anEvent.ToT_int[chIdx] = *iter++;
}
}
break;
}
case TIMEUNIT_NS:
{
for (int iHit = 0; iHit < anEvent.nhits; iHit++) {
TranslatorPointer<uint8_t> oneByteIter(iter);
int chIdx = *oneByteIter++;
int dataType = *oneByteIter++;
iter = oneByteIter;
if (dataType & DATATYPE_TOA) {
anEvent.hasToA_float[chIdx] = true;
uint32_t tempValue = getCombined<uint32_t>(iter);
memcpy(&anEvent.ToA_float[chIdx], &tempValue, sizeof(float));
}
if (dataType & DATATYPE_TOT) {
anEvent.hasToT_float[chIdx] = true;
uint32_t tempValue = getCombined<uint32_t>(iter);
memcpy(&anEvent.ToT_float[chIdx], &tempValue, sizeof(float));
}
}
break;
}
default:
{
string errmsg = "Invalid TimeUnit! (" + to_string(timeunit) + ")";
throw runtime_error(errmsg);
}
}
parsedData.push_back(anEvent);
break;
}
default:
{
string errmsg = "Invalid AcqMode! (" + to_string(acqmode) + ")";
throw runtime_error(errmsg);
}
}
// Skipping ender
*iter++;
*iter++;
return parsedData;
}