1 | class CalibData
|
---|
2 | {
|
---|
3 | protected:
|
---|
4 | uint64_t fNumEntries;
|
---|
5 |
|
---|
6 | size_t fNumSamples;
|
---|
7 | size_t fNumChannels;
|
---|
8 |
|
---|
9 | vector<int64_t> fSum;
|
---|
10 | vector<int64_t> fSum2;
|
---|
11 |
|
---|
12 | /*
|
---|
13 | vector<map<int16_t, uint8_t> > fMedian;
|
---|
14 | vector<int16_t> fResult;
|
---|
15 | */
|
---|
16 | public:
|
---|
17 | CalibData() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { }
|
---|
18 | void Reset()
|
---|
19 | {
|
---|
20 | fNumEntries = 0;
|
---|
21 | fNumSamples = 0;
|
---|
22 | fNumChannels = 0;
|
---|
23 |
|
---|
24 | fSum.clear();
|
---|
25 | fSum2.clear();
|
---|
26 | }
|
---|
27 |
|
---|
28 | void InitSize(uint16_t channels, uint16_t samples)
|
---|
29 | {
|
---|
30 | fNumChannels = channels;
|
---|
31 | fNumSamples = samples;
|
---|
32 |
|
---|
33 | fSum.resize(samples*channels);
|
---|
34 | fSum2.resize(samples*channels);
|
---|
35 |
|
---|
36 | //fMedian.resize(40*9*1024);
|
---|
37 | }
|
---|
38 | /*
|
---|
39 | continue;
|
---|
40 |
|
---|
41 | if (ch<40*9)
|
---|
42 | {
|
---|
43 | fMedian[abs][val[rel]]++;
|
---|
44 |
|
---|
45 | int n= 8;
|
---|
46 | if (fNumEntries>0 && fNumEntries%(n*100)==0)
|
---|
47 | {
|
---|
48 | fResult.resize(40*9*1024);
|
---|
49 |
|
---|
50 | uint16_t sum = 0;
|
---|
51 | for (map<int16_t, uint8_t>::const_iterator it=fMedian[abs].begin();
|
---|
52 | it!=fMedian[abs].end(); it++)
|
---|
53 | {
|
---|
54 | map<int16_t, uint8_t>::const_iterator is = it;
|
---|
55 | is++;
|
---|
56 | if (is==fMedian[abs].end())
|
---|
57 | break;
|
---|
58 |
|
---|
59 | sum += it->second;
|
---|
60 |
|
---|
61 | double med = 0;
|
---|
62 |
|
---|
63 | med += int64_t(it->second)*int64_t(it->first);
|
---|
64 | med += int64_t(is->second)*int64_t(is->first);
|
---|
65 | med /= int64_t(it->second)+int64_t(is->second);
|
---|
66 |
|
---|
67 | if (sum==n*50)
|
---|
68 | {
|
---|
69 | fResult[abs] = it->first;
|
---|
70 | cout << ch << " MED+(50): " << it->first << endl;
|
---|
71 | }
|
---|
72 | if (sum<n*50 && sum+is->second>n*50)
|
---|
73 | {
|
---|
74 | fResult[abs] = med;
|
---|
75 | cout << ch << " MED-(50): " << med << endl;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | cout << ch << " AVG=" << float(fSum[abs])/fNumEntries << endl;
|
---|
79 | fMedian[abs].clear();
|
---|
80 | }
|
---|
81 | } // -2029 -2012 -2003 -1996 -1990 // -1955
|
---|
82 | */
|
---|
83 |
|
---|
84 | void AddRel(const int16_t *val, const int16_t *start)
|
---|
85 | {
|
---|
86 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
87 | {
|
---|
88 | const int16_t spos = start[ch];
|
---|
89 | if (spos<0)
|
---|
90 | continue;
|
---|
91 |
|
---|
92 | const size_t pos = ch*fNumSamples;
|
---|
93 |
|
---|
94 | for (size_t i=0; i<fNumSamples; i++)
|
---|
95 | {
|
---|
96 | // Value is relative to trigger
|
---|
97 | // Abs is corresponding index relative to DRS pipeline
|
---|
98 | const size_t rel = pos + i;
|
---|
99 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
100 |
|
---|
101 | const int64_t v = val[rel];
|
---|
102 |
|
---|
103 | fSum[abs] += v;
|
---|
104 | fSum2[abs] += v*v;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | fNumEntries++;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void AddRel(const int16_t *val, const int16_t *start,
|
---|
112 | const int32_t *offset, const uint32_t scale)
|
---|
113 | {
|
---|
114 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
115 | {
|
---|
116 | const int16_t spos = start[ch];
|
---|
117 | if (spos<0)
|
---|
118 | continue;
|
---|
119 |
|
---|
120 | const size_t pos = ch*fNumSamples;
|
---|
121 |
|
---|
122 | for (size_t i=0; i<fNumSamples; i++)
|
---|
123 | {
|
---|
124 | // Value is relative to trigger
|
---|
125 | // Offset is relative to DRS pipeline
|
---|
126 | // Abs is corresponding index relative to DRS pipeline
|
---|
127 | const size_t rel = pos + i;
|
---|
128 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
129 |
|
---|
130 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
131 |
|
---|
132 | fSum[abs] += v;
|
---|
133 | fSum2[abs] += v*v;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | fNumEntries++;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void AddAbs(const int16_t *val, const int16_t *start,
|
---|
141 | const int32_t *offset, const uint32_t scale)
|
---|
142 | {
|
---|
143 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
144 | {
|
---|
145 | const int16_t spos = start[ch];
|
---|
146 | if (spos<0)
|
---|
147 | continue;
|
---|
148 |
|
---|
149 | const size_t pos = ch*fNumSamples;
|
---|
150 |
|
---|
151 | for (size_t i=0; i<fNumSamples; i++)
|
---|
152 | {
|
---|
153 | // Value is relative to trigger
|
---|
154 | // Offset is relative to DRS pipeline
|
---|
155 | // Abs is corresponding index relative to DRS pipeline
|
---|
156 | const size_t rel = pos + i;
|
---|
157 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
158 |
|
---|
159 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
160 |
|
---|
161 | fSum[rel] += v;
|
---|
162 | fSum2[rel] += v*v;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | fNumEntries++;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void Apply(int16_t *val, const int16_t *start, uint32_t roi,
|
---|
170 | const int32_t *offset, const uint32_t scaleabs,
|
---|
171 | const int64_t *gain, const uint32_t scalegain,
|
---|
172 | const int64_t *trgoff, const uint32_t scalerel)
|
---|
173 | {
|
---|
174 | return ;// "FIXME: scalegain exceeds valid range..."
|
---|
175 | for (size_t ch=0; ch<1440; ch++)
|
---|
176 | {
|
---|
177 | const int16_t spos = start[ch];
|
---|
178 | if (spos<0)
|
---|
179 | continue;
|
---|
180 |
|
---|
181 | const size_t pos = ch*roi;
|
---|
182 |
|
---|
183 | for (size_t i=0; i<roi; i++)
|
---|
184 | {
|
---|
185 | // Value is relative to trigger
|
---|
186 | // Offset is relative to DRS pipeline
|
---|
187 | // Abs is corresponding index relative to DRS pipeline
|
---|
188 | const size_t rel = pos + i;
|
---|
189 | const size_t abs = pos + (spos+i)%1024;
|
---|
190 |
|
---|
191 | const int64_t v =
|
---|
192 | + int64_t(val[rel]) *scaleabs*scalerel
|
---|
193 | - int64_t(trgoff[rel])*scaleabs
|
---|
194 | - int64_t(offset[abs])*scalerel
|
---|
195 | ;
|
---|
196 |
|
---|
197 | // The gain value is multiplied by fNumOffset and fNumGain
|
---|
198 | const int64_t div = int64_t(gain[abs])*scaleabs*scalerel;
|
---|
199 | val[rel] = (v*scalegain)/div;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | static void Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi,
|
---|
205 | const int32_t *offset, const uint32_t scaleabs,
|
---|
206 | const int64_t *gain, /*const*/ uint64_t scalegain,
|
---|
207 | const int64_t *trgoff, /*const*/ uint64_t scalerel)
|
---|
208 | {
|
---|
209 | /*
|
---|
210 | scalegain *= scaleabs;
|
---|
211 | scalerel *= scaleabs;
|
---|
212 |
|
---|
213 | for (size_t ch=0; ch<1440; ch++)
|
---|
214 | {
|
---|
215 | const size_t pos = ch*roi;
|
---|
216 | const int16_t spos = start[ch];
|
---|
217 | if (spos<0)
|
---|
218 | {
|
---|
219 | memset(vec+pos, 0, roi);
|
---|
220 | continue;
|
---|
221 | }
|
---|
222 |
|
---|
223 | for (size_t i=0; i<roi; i++)
|
---|
224 | {
|
---|
225 | // Value is relative to trigger
|
---|
226 | // Offset is relative to DRS pipeline
|
---|
227 | // Abs is corresponding index relative to DRS pipeline
|
---|
228 | const size_t rel = pos + i;
|
---|
229 | const size_t abs = pos + (spos+i)%1024;
|
---|
230 |
|
---|
231 | const int64_t v =
|
---|
232 | + int64_t(val[rel]) *scaleabs*scalerel
|
---|
233 | - int64_t(trgoff[rel])*scaleabs
|
---|
234 | - int64_t(offset[abs])*scalerel
|
---|
235 | ;
|
---|
236 |
|
---|
237 | const int64_t div = int64_t(gain[abs])*scaleabs*scalerel;
|
---|
238 |
|
---|
239 | vec[rel] = double(v)*scalegain/div;
|
---|
240 | }
|
---|
241 | }*/
|
---|
242 |
|
---|
243 | for (size_t ch=0; ch<1440; ch++)
|
---|
244 | {
|
---|
245 | const size_t pos = ch*roi;
|
---|
246 |
|
---|
247 | const int16_t spos = start[ch];
|
---|
248 | if (spos<0)
|
---|
249 | {
|
---|
250 | memset(vec+pos, 0, roi);
|
---|
251 | continue;
|
---|
252 | }
|
---|
253 |
|
---|
254 | for (size_t i=0; i<roi; i++)
|
---|
255 | {
|
---|
256 | // Value is relative to trigger
|
---|
257 | // Offset is relative to DRS pipeline
|
---|
258 | // Abs is corresponding index relative to DRS pipeline
|
---|
259 | const size_t rel = pos + i;
|
---|
260 | const size_t abs = pos + (spos+i)%1024;
|
---|
261 |
|
---|
262 | const int64_t v =
|
---|
263 | + (int64_t(val[rel])*scaleabs-offset[abs])*scalerel
|
---|
264 | - trgoff[rel]
|
---|
265 | ;
|
---|
266 |
|
---|
267 | const int64_t div = gain[abs]*scalerel;
|
---|
268 | vec[rel] = double(v)*scalegain/div;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | static void RemoveSpikes(float *vec, uint32_t roi)
|
---|
274 | {
|
---|
275 | if (roi<4)
|
---|
276 | return;
|
---|
277 |
|
---|
278 | cout << "X" << endl;
|
---|
279 |
|
---|
280 | for (size_t ch=0; ch<1440; ch++)
|
---|
281 | {
|
---|
282 | float *p = vec + ch*roi;
|
---|
283 |
|
---|
284 | for (size_t i=1; i<roi-2; i++)
|
---|
285 | {
|
---|
286 | if (p[i]-p[i-1]>25 && p[i]-p[i+1]>25)
|
---|
287 | {
|
---|
288 | p[i] = (p[i-1]+p[i+1])/2;
|
---|
289 | }
|
---|
290 |
|
---|
291 | if (p[i]-p[i-1]>22 && fabs(p[i]-p[i+1])<4 && p[i+1]-p[i+2]>22)
|
---|
292 | {
|
---|
293 | p[i] = (p[i-1]+p[i+2])/2;
|
---|
294 | p[i+1] = p[i];
|
---|
295 | }
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | pair<vector<double>,vector<double> > GetSampleStats() const
|
---|
301 | {
|
---|
302 | if (fNumEntries==0)
|
---|
303 | return make_pair(vector<double>(),vector<double>());
|
---|
304 |
|
---|
305 | vector<double> mean(fSum.size());
|
---|
306 | vector<double> error(fSum.size());
|
---|
307 |
|
---|
308 | vector<int64_t>::const_iterator it = fSum.begin();
|
---|
309 | vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
310 | vector<double>::iterator im = mean.begin();
|
---|
311 | vector<double>::iterator ie = error.begin();
|
---|
312 |
|
---|
313 | while (it!=fSum.end())
|
---|
314 | {
|
---|
315 | *im = /*cnt<fResult.size() ? fResult[cnt] :*/ double(*it)/fNumEntries;
|
---|
316 | *ie = sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
317 |
|
---|
318 | im++;
|
---|
319 | ie++;
|
---|
320 | it++;
|
---|
321 | i2++;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 | /*
|
---|
326 | valarray<double> ...
|
---|
327 |
|
---|
328 | mean /= fNumEntries;
|
---|
329 | error = sqrt(error/fNumEntries - mean*mean);
|
---|
330 | */
|
---|
331 |
|
---|
332 | return make_pair(mean, error);
|
---|
333 | }
|
---|
334 |
|
---|
335 | void GetSampleStats(float *ptr, float scale) const
|
---|
336 | {
|
---|
337 | if (fNumEntries==0)
|
---|
338 | {
|
---|
339 | memset(ptr, 0, sizeof(float)*1024*1440*2);
|
---|
340 | return;
|
---|
341 | }
|
---|
342 |
|
---|
343 | vector<int64_t>::const_iterator it = fSum.begin();
|
---|
344 | vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
345 |
|
---|
346 | while (it!=fSum.end())
|
---|
347 | {
|
---|
348 | *ptr = scale*double(*it)/fNumEntries;
|
---|
349 | *(ptr+1024*1440) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
350 |
|
---|
351 | ptr++;
|
---|
352 | it++;
|
---|
353 | i2++;
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 | static void GetPixelStats(float *ptr, const float *data, uint16_t roi)
|
---|
358 | {
|
---|
359 | if (roi==0)
|
---|
360 | return;
|
---|
361 |
|
---|
362 | for (int i=0; i<1440; i++)
|
---|
363 | {
|
---|
364 | const float *vec = data+i*roi;
|
---|
365 |
|
---|
366 | int pos = 0;
|
---|
367 | double sum = vec[0];
|
---|
368 | double sum2 = vec[0]*vec[0];
|
---|
369 | for (int j=1; j<roi; j++)
|
---|
370 | {
|
---|
371 | sum += vec[j];
|
---|
372 | sum2 += vec[j]*vec[j];
|
---|
373 |
|
---|
374 | if (vec[j]>vec[pos])
|
---|
375 | pos = j;
|
---|
376 | }
|
---|
377 | sum /= roi;
|
---|
378 | sum2 /= roi;
|
---|
379 |
|
---|
380 | *(ptr+0*1440+i) = sum;
|
---|
381 | *(ptr+1*1440+i) = sqrt(sum2 - sum * sum);
|
---|
382 | *(ptr+2*1440+i) = vec[pos];
|
---|
383 | *(ptr+3*1440+i) = pos;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last)
|
---|
388 | {
|
---|
389 | if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last<first)
|
---|
390 | return;
|
---|
391 |
|
---|
392 | for (int i=0; i<1440; i++)
|
---|
393 | {
|
---|
394 | const float *beg = data+i*roi+first;
|
---|
395 | const float *end = data+i*roi+last;
|
---|
396 |
|
---|
397 | const float *pmax = beg;
|
---|
398 |
|
---|
399 | for (const float *ptr=beg+1; ptr<=end; ptr++)
|
---|
400 | if (*ptr>*pmax)
|
---|
401 | pmax = ptr;
|
---|
402 |
|
---|
403 | max[i] = *pmax;
|
---|
404 | }
|
---|
405 |
|
---|
406 | }
|
---|
407 |
|
---|
408 | const vector<int64_t> &GetSum() const { return fSum; }
|
---|
409 |
|
---|
410 | uint64_t GetNumEntries() const { return fNumEntries; }
|
---|
411 | };
|
---|
412 |
|
---|