-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIP.cc
315 lines (245 loc) · 7.92 KB
/
CIP.cc
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
/*
-------------------------------------------------------------------------
OBJECT NAME: CIP.cc
FULL NAME: DMT CIP/PIP Probe Class
DESCRIPTION:
COPYRIGHT: University Corporation for Atmospheric Research, 1997-2018
-------------------------------------------------------------------------
*/
#include "CIP.h"
#include "OAPUserConfig.h"
using namespace OAP;
const unsigned long long CIP::SyncWord = 0xAAAAAAAAAAAAAAAALL;
/* -------------------------------------------------------------------- */
CIP::CIP(UserConfig *cfg, const char xml_entry[], int recSize) : Probe(CIP_T, cfg, xml_entry, recSize, 64)
{
_lrLen = recSize;
std::string id = ::XMLgetAttributeValue(xml_entry, "id");
strcpy(_code, id.c_str());
_name = ::XMLgetAttributeValue(xml_entry, "type");
_name += ::XMLgetAttributeValue(xml_entry, "suffix");
_resolution = atoi(::XMLgetAttributeValue(xml_entry, "resolution").c_str());
dmt_init();
printf("CIP::OAP id=%s, name=%s, resolution=%zu, armWidth=%f, eaw=%f\n", _code, _name.c_str(), _resolution, _armWidth, _eaw);
}
/* -------------------------------------------------------------------- */
CIP::CIP(UserConfig *cfg, const char name[]) : Probe(CIP_T, cfg, name, 64)
{
_resolution = 25;
dmt_init();
printf("CIP::PADS id=%s, name=%s, resolution=%zu, armWidth=%f, eaw=%f\n", _code, _name.c_str(), _resolution, _armWidth, _eaw);
}
void CIP::dmt_init()
{
_clockMhz = 8;
_nResidualBytes = 0;
_carryOver = false;
if (_code[0] == 'C') // CIP
_armWidth = 100.0;
if (_code[0] == 'P') // PIP
_armWidth = 260.0;
SetSampleArea();
}
/* -------------------------------------------------------------------- */
bool CIP::isSyncWord(const unsigned char *p)
{
return memcmp(p, (void *)&SyncWord, sizeof(SyncWord)) == 0;
}
/* -------------------------------------------------------------------- */
struct recStats CIP::ProcessRecord(const P2d_rec *record, float version)
{
int startTime, overload = 0;
const unsigned char *p;
unsigned long startMilliSec;
double sampleVolume[(nDiodes()<<1)+1], totalLiveTime;
unsigned long long firstTimeWord = 0;
ClearStats(record);
stats.DASelapsedTime = stats.thisTime - _prevTime;
stats.SampleVolume = SampleArea() * stats.tas;
if (version == -1) // This means set time stamp only
{
_prevTime = stats.thisTime;
memcpy((char *)&_prevHdr, (char *)record, sizeof(P2d_hdr));
return(stats);
}
#ifdef DEBUG
printf("%c%c %02d:%02d:%02d.%d - \n", ((char *)record)[0], ((char *)record)[1], record->hour, record->minute, record->second, record->msec);
#endif
totalLiveTime = 0.0;
switch (_userConfig->GetConcentration()) {
case CENTER_IN:
case RECONSTRUCTION: _numBins = 128; break;
default: _numBins = nDiodes();
}
for (size_t i = 0; i < NumberBins(); ++i)
sampleVolume[i] = stats.tas * sampleArea[i] * 0.001;
unsigned char image[16000];
memset(image, 0, 16000);
_nSlices = uncompress(image, record->data, 4096);
// Scan record, compute tBarElapsedtime and stats.
p = (unsigned char *)image;
startTime = _prevTime / 1000;
startMilliSec = _prevHdr.msec;
// Loop through all slices in record.
for (size_t i = 0; i < nSlices(); ++i, p += sizeof(long long))
{
/* Have particle, will travel.
*/
if (isSyncWord(p) || _carryOver)
{
if (++i >= nSlices()) // Last slice is a timeWord
{
_carryOver = true;
continue;
}
if (!_carryOver)
p += 8;
_carryOver = false;
unsigned long long thisTimeWord = TimeWord_Microseconds(&p[2]);
if (firstTimeWord == 0)
firstTimeWord = thisTimeWord;
// Start a particle.
cp = new Particle();
cp->timeWord = thisTimeWord;
unsigned long msec = startMilliSec + ((thisTimeWord - firstTimeWord) / 1000);
cp->time = startTime + (msec / 1000);
cp->msec = msec % 1000;
cp->deltaTime = cp->timeWord - _prevTimeWord;
cp->timeWord /= 1000; // Store as millseconds... can move to microseconds...
totalLiveTime += checkRejectionCriteria(cp, stats);
if ((p[7] & 0x01) == 0)
cp->dofReject = true;
stats.particles.push_back(cp);
_prevTimeWord = thisTimeWord;
++stats.nTimeBars;
stats.minBar = std::min(stats.minBar, cp->deltaTime);
stats.maxBar = std::max(stats.maxBar, cp->deltaTime);
continue;
}
if (!cp || isBlankSlice(p))
continue;
++cp->w;
checkEdgeDiodes(cp, p);
cp->area += area(p);
cp->h = std::max(height(p), cp->h);
/* If the particle becomes rejected later, we need to now much time the
* particle consumed, so we can add it to the deadTime, so sampleVolume
* can be reduced accordingly.
*/
cp->liveTime = (unsigned long)((float)(cp->w) * stats.frequency);
#ifdef DEBUG
printf("%06x %zu %zu\n", cp->timeWord, cp->w, cp->h);
#endif
}
stats.SampleVolume *= (stats.DASelapsedTime - overload) * 0.001;
stats.tBarElapsedtime = (_prevTimeWord - firstTimeWord);
if (stats.nTimeBars > 0)
stats.meanBar = stats.tBarElapsedtime / stats.nTimeBars;
stats.tBarElapsedtime /= 1000;
stats.frequency /= 1000;
// Compute "science" data.
totalLiveTime /= 1000000; // convert to seconds
computeDerived(sampleVolume, totalLiveTime);
// Save time for next round.
_prevTime = stats.thisTime;
memcpy((char *)&_prevHdr, (char *)record, sizeof(P2d_hdr));
return(stats);
} // END PROCESSCIP
/* -------------------------------------------------------------------- */
long long CIP::TimeWord_Microseconds(const unsigned char *p) const
{
long long t = *(long long *)p & 0x000000FFFFFFFFFFLL;
long long output;
int hour = (t >> 35) & 0x001F;
int minute = (t >> 29) & 0x003F;
int second = (t >> 23) & 0x003F;
int msec = (t >> 13) & 0x03FF;
int usec = t & 0x1FFF;
output = (hour * 3600 + minute * 60 + second);
output *= 1000000;
output += msec * 1000;
output += usec / _clockMhz;
#ifdef DEBUG
printf("%02d:%02d:%02d.%03d - (%lld)\n", hour, minute, second, msec, output);
#endif
return output;
}
/* -------------------------------------------------------------------- */
// DMT CIP/PIP probes are run length encoded. Decode here.
// Duplicated in class/MainCanvas.cc, not consolidated at this time due to static variables.
size_t CIP::uncompress(unsigned char *dest, const unsigned char src[], int nbytes)
{
int d_idx = 0, i = 0;
if (_nResidualBytes)
{
memcpy(dest, residualBytes, _nResidualBytes);
d_idx = _nResidualBytes;
_nResidualBytes = 0;
}
for (; i < nbytes; ++i)
{
unsigned char b = src[i];
int nBytes = (b & 0x1F) + 1;
if ((b & 0x20)) // This is a dummy byte; for alignment purposes.
{
continue;
}
if ((b & 0xE0) == 0) // Unencoded data, copy it verbatim
{
memcpy(&dest[d_idx], &src[i+1], nBytes);
d_idx += nBytes;
i += nBytes;
}
else
if ((b & 0x80))
{
memset(&dest[d_idx], 0, nBytes);
d_idx += nBytes;
}
else
if ((b & 0x40))
{
memset(&dest[d_idx], 0xFF, nBytes);
d_idx += nBytes;
}
}
// Align data. Find a sync word and put record on mod 8.
for (i = 0; i < d_idx; ++i)
{
if ( isSyncWord(&dest[i]) )
{
int n = (&dest[i] - dest) % 8;
if (n > 0)
{
memmove(dest, &dest[n], d_idx);
d_idx -= n;
}
break;
}
}
if (d_idx % 8)
{
size_t idx = d_idx / 8 * 8;
_nResidualBytes = d_idx % 8;
memcpy(residualBytes, &dest[idx], _nResidualBytes);
}
SwapBytes(dest, d_idx / 8); // Return number of slices.
return d_idx / 8; // return number of slices.
}
void CIP::SwapBytes(unsigned char *cp, size_t nSlices)
{
unsigned char tmp[16];
for (size_t i = 0; i < nSlices; ++i, cp += sizeof(long long))
{
// Don't swap sync or timing words.
if (memcmp(cp, (unsigned char *)&SyncWord, 8) == 0)
{
i++; cp += sizeof(long long);
continue;
}
for (size_t j = 0; j < 8; ++j)
tmp[j] = cp[7-j];
memcpy(cp, tmp, 8);
}
}
// END CIP.CC