-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecProbe.cc
208 lines (151 loc) · 5.65 KB
/
SpecProbe.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
/*
-------------------------------------------------------------------------
OBJECT NAME: SpecProbe.cc
FULL NAME: SPEC SpecProbe Probe Class
DESCRIPTION:
COPYRIGHT: University Corporation for Atmospheric Research, 1997-2023
-------------------------------------------------------------------------
*/
#include "SpecProbe.h"
#include "OAPUserConfig.h"
#include "portable.h"
using namespace OAP;
const unsigned char SpecProbe::SyncString[] = { 0xaa, 0xaa, 0xaa };
const unsigned char SpecProbe::OverldString[] = { 0x55, 0x55, 0xaa };
/* -------------------------------------------------------------------- */
SpecProbe::SpecProbe(ProbeType type, UserConfig *cfg, const char xml_entry[], int recSize)
: Probe(type, cfg, xml_entry, recSize, 128)
{
_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());
_armWidth = 50.8;
_dof_const = 5.13;
// Default to SPEC Type48. Uses a 48 bit timing word.
_packetFormat = Type48;
_timingMask = 0x0000ffffffffffffLL;
SetSampleArea();
}
/* -------------------------------------------------------------------- */
uint64_t SpecProbe::TimeWord_Microseconds(const unsigned char *p)
{
return (ntohll((uint64_t *)p) & _timingMask) * stats.frequency;
}
/* -------------------------------------------------------------------- */
bool SpecProbe::isSyncWord(const unsigned char *p)
{
return memcmp(p, (void *)&SyncString, 3) == 0;
}
/* -------------------------------------------------------------------- */
bool SpecProbe::isOverloadWord(const unsigned char *p)
{
return memcmp(p, (void *)&OverldString, 2) == 0;
}
/* -------------------------------------------------------------------- */
uint64_t SpecProbe::timeWordDiff(uint64_t val1, uint64_t val2)
{
return val1 - val2;
}
/* -------------------------------------------------------------------- */
struct recStats SpecProbe::ProcessRecord(const P2d_rec *record, float version)
{
int startTime, overload = 0;
unsigned long startMilliSec;
double sampleVolume[(nDiodes()<<1)+1], totalLiveTime;
uint64_t 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("SPEC %02d:%02d:%02d.%d - ", record->hour, record->minute, record->second, record->msec);
#endif
totalLiveTime = 0.0;
switch (_userConfig->GetConcentration()) {
case CENTER_IN:
case RECONSTRUCTION: _numBins = 256; break;
default: _numBins = nDiodes();
}
for (size_t i = 0; i < NumberBins(); ++i)
sampleVolume[i] = stats.tas * sampleArea[i] * 0.001;
// Scan record, compute tBarElapsedtime and stats.
const unsigned char *p = record->data;
startTime = _prevTime / 1000;
startMilliSec = _prevHdr.msec;
// Loop through all slices in record.
for (size_t i = 0; i < nSlices(); ++i, p += 16)
{
/* Have particle, will travel.
*/
if (isSyncWord(p) || isOverloadWord(p))
{
uint64_t thisTimeWord = TimeWord_Microseconds(&p[8]);
if (firstTimeWord == 0)
firstTimeWord = thisTimeWord;
if (isOverloadWord(p))
{
// Set 'overload' variable here. There is no way to determine overload.
// Leave zero, they are less than a milli-second anyways.
overload = 0;
printf(">>> SpecProbe overload @ %02d:%02d:%02d.%-3d. <<<\n",
record->hour, record->minute, record->second, record->msec);
if (cp)
cp->reject = true;
}
// Close out particle. Timeword belongs to previous particle.
if (cp)
{
cp->timeWord = thisTimeWord;
unsigned long msec = startMilliSec + ((thisTimeWord - firstTimeWord) / 1000);
cp->time = startTime + (msec / 1000);
cp->msec = msec % 1000;
cp->deltaTime = timeWordDiff(cp->timeWord, _prevTimeWord);
cp->timeWord /= 1000; // Store as millseconds for this probe, since this is not a 48 bit word
totalLiveTime += checkRejectionCriteria(cp, stats);
stats.particles.push_back(cp);
cp = new Particle();
}
_prevTimeWord = thisTimeWord;
++stats.nTimeBars;
stats.minBar = std::min(stats.minBar, cp->deltaTime);
stats.maxBar = std::max(stats.maxBar, cp->deltaTime);
continue;
}
if (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 = timeWordDiff(_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 PROCESSTWODS
// END TWODS.CC