1 | #ifndef MARS_DrsCalib
|
---|
2 | #define MARS_DrsCalib
|
---|
3 |
|
---|
4 | #include <math.h> // fabs
|
---|
5 | #include <errno.h> // errno
|
---|
6 |
|
---|
7 | #include "fits.h"
|
---|
8 |
|
---|
9 | class DrsCalibrate
|
---|
10 | {
|
---|
11 | protected:
|
---|
12 | uint64_t fNumEntries;
|
---|
13 |
|
---|
14 | size_t fNumSamples;
|
---|
15 | size_t fNumChannels;
|
---|
16 |
|
---|
17 | std::vector<int64_t> fSum;
|
---|
18 | std::vector<int64_t> fSum2;
|
---|
19 |
|
---|
20 | public:
|
---|
21 | DrsCalibrate() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { }
|
---|
22 | void Reset()
|
---|
23 | {
|
---|
24 | fNumEntries = 0;
|
---|
25 | fNumSamples = 0;
|
---|
26 | fNumChannels = 0;
|
---|
27 |
|
---|
28 | fSum.clear();
|
---|
29 | fSum2.clear();
|
---|
30 | }
|
---|
31 |
|
---|
32 | void InitSize(uint16_t channels, uint16_t samples)
|
---|
33 | {
|
---|
34 | fNumChannels = channels;
|
---|
35 | fNumSamples = samples;
|
---|
36 |
|
---|
37 | fSum.resize(samples*channels);
|
---|
38 | fSum2.resize(samples*channels);
|
---|
39 | }
|
---|
40 |
|
---|
41 | void AddRel(const int16_t *val, const int16_t *start)
|
---|
42 | {
|
---|
43 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
44 | {
|
---|
45 | const int16_t spos = start[ch];
|
---|
46 | if (spos<0)
|
---|
47 | continue;
|
---|
48 |
|
---|
49 | const size_t pos = ch*1024;
|
---|
50 |
|
---|
51 | for (size_t i=0; i<1024; i++)
|
---|
52 | {
|
---|
53 | // Value is relative to trigger
|
---|
54 | // Abs is corresponding index relative to DRS pipeline
|
---|
55 | const size_t rel = pos + i;
|
---|
56 | const size_t abs = pos + (spos+i)%1024;
|
---|
57 |
|
---|
58 | const int64_t v = val[rel];
|
---|
59 |
|
---|
60 | fSum[abs] += v;
|
---|
61 | fSum2[abs] += v*v;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | fNumEntries++;
|
---|
66 | }
|
---|
67 |
|
---|
68 | void AddRel(const int16_t *val, const int16_t *start,
|
---|
69 | const int32_t *offset, const uint32_t scale)
|
---|
70 | {
|
---|
71 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
72 | {
|
---|
73 | const int16_t spos = start[ch];
|
---|
74 | if (spos<0)
|
---|
75 | continue;
|
---|
76 |
|
---|
77 | const size_t pos = ch*fNumSamples;
|
---|
78 |
|
---|
79 | for (size_t i=0; i<fNumSamples; i++)
|
---|
80 | {
|
---|
81 | // Value is relative to trigger
|
---|
82 | // Offset is relative to DRS pipeline
|
---|
83 | // Abs is corresponding index relative to DRS pipeline
|
---|
84 | const size_t rel = pos + i;
|
---|
85 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
86 |
|
---|
87 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
88 |
|
---|
89 | fSum[abs] += v;
|
---|
90 | fSum2[abs] += v*v;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | fNumEntries++;
|
---|
95 | }
|
---|
96 | /*
|
---|
97 | void AddAbs(const int16_t *val, const int16_t *start,
|
---|
98 | const int32_t *offset, const uint32_t scale)
|
---|
99 | {
|
---|
100 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
101 | {
|
---|
102 | const int16_t spos = start[ch];
|
---|
103 | if (spos<0)
|
---|
104 | continue;
|
---|
105 |
|
---|
106 | const size_t pos = ch*fNumSamples;
|
---|
107 |
|
---|
108 | for (size_t i=0; i<fNumSamples; i++)
|
---|
109 | {
|
---|
110 | // Value is relative to trigger
|
---|
111 | // Offset is relative to DRS pipeline
|
---|
112 | // Abs is corresponding index relative to DRS pipeline
|
---|
113 | const size_t rel = pos + i;
|
---|
114 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
115 |
|
---|
116 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
117 |
|
---|
118 | fSum[rel] += v;
|
---|
119 | fSum2[rel] += v*v;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | fNumEntries++;
|
---|
124 | }*/
|
---|
125 |
|
---|
126 | void AddAbs(const int16_t *val, const int16_t *start,
|
---|
127 | const int32_t *offset, const uint32_t scale)
|
---|
128 | {
|
---|
129 | // 1440 without tm, 1600 with tm
|
---|
130 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
131 | {
|
---|
132 | const int16_t spos = start[ch];
|
---|
133 | if (spos<0)
|
---|
134 | continue;
|
---|
135 |
|
---|
136 | const size_t pos = ch*fNumSamples;
|
---|
137 | const size_t drs = ch>1439 ? ((ch-1440)*9+8)*1024 : ch*1024;
|
---|
138 |
|
---|
139 | for (size_t i=0; i<fNumSamples; i++)
|
---|
140 | {
|
---|
141 | // Value is relative to trigger
|
---|
142 | // Offset is relative to DRS pipeline
|
---|
143 | // Abs is corresponding index relative to DRS pipeline
|
---|
144 | const size_t rel = pos + i;
|
---|
145 | const size_t abs = drs + (spos+i)%1024;
|
---|
146 |
|
---|
147 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
148 |
|
---|
149 | fSum[rel] += v;
|
---|
150 | fSum2[rel] += v*v;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | fNumEntries++;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
|
---|
159 | const int32_t *offset, const uint32_t scaleabs,
|
---|
160 | const int64_t *gain, const uint64_t scalegain)
|
---|
161 | {
|
---|
162 | if (start<0)
|
---|
163 | {
|
---|
164 | memset(vec, 0, roi);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | for (size_t i=0; i<roi; i++)
|
---|
169 | {
|
---|
170 | // Value is relative to trigger
|
---|
171 | // Offset is relative to DRS pipeline
|
---|
172 | // Abs is corresponding index relative to DRS pipeline
|
---|
173 | const size_t abs = (start+i)%1024;
|
---|
174 |
|
---|
175 | const int64_t v =
|
---|
176 | + int64_t(val[i])*scaleabs-offset[abs]
|
---|
177 | ;
|
---|
178 |
|
---|
179 | const int64_t div = gain[abs];
|
---|
180 | vec[i] = double(v)*scalegain/div;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
|
---|
185 | const int32_t *offset, const uint32_t scaleabs,
|
---|
186 | const int64_t *gain, const uint64_t scalegain,
|
---|
187 | const int64_t *trgoff, const uint64_t scalerel)
|
---|
188 | {
|
---|
189 | if (start<0)
|
---|
190 | {
|
---|
191 | memset(vec, 0, roi);
|
---|
192 | return;
|
---|
193 | }
|
---|
194 |
|
---|
195 | for (size_t i=0; i<roi; i++)
|
---|
196 | {
|
---|
197 | // Value is relative to trigger
|
---|
198 | // Offset is relative to DRS pipeline
|
---|
199 | // Abs is corresponding index relative to DRS pipeline
|
---|
200 | const size_t abs = (start+i)%1024;
|
---|
201 |
|
---|
202 | const int64_t v =
|
---|
203 | + (int64_t(val[i])*scaleabs-offset[abs])*scalerel
|
---|
204 | - trgoff[i]
|
---|
205 | ;
|
---|
206 |
|
---|
207 | const int64_t div = gain[abs]*scalerel;
|
---|
208 | vec[i] = double(v)*scalegain/div;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | static double FindStep(const size_t ch0, const float *vec, int16_t roi, const int16_t pos, const uint16_t *map)
|
---|
213 | {
|
---|
214 | if (pos<=1 || pos>=roi)
|
---|
215 | return 0;
|
---|
216 |
|
---|
217 | double step = 0; // before
|
---|
218 |
|
---|
219 | // Exclude TM channel
|
---|
220 | for (int p=0; p<8; p++)
|
---|
221 | {
|
---|
222 | const size_t hw = ch0+p;
|
---|
223 | const size_t sw = map[hw]*roi + pos;
|
---|
224 |
|
---|
225 | step += vec[sw]-vec[sw-1];
|
---|
226 | }
|
---|
227 |
|
---|
228 | return step/8;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static void SubtractStep(const size_t ch0, const double avg, float *vec, int16_t roi, uint32_t dist, const uint16_t *map)
|
---|
232 | {
|
---|
233 | const int begin = avg>0 ? dist : 0;
|
---|
234 | const int end = avg>0 ? roi : dist;
|
---|
235 |
|
---|
236 | const double sub = fabs(avg);
|
---|
237 |
|
---|
238 | for (int p=0; p<9; p++)
|
---|
239 | {
|
---|
240 | for (int j=begin; j<end; j++)
|
---|
241 | {
|
---|
242 | const size_t hw = ch0+p;
|
---|
243 | const size_t sw = map[hw]*roi + j;
|
---|
244 |
|
---|
245 | vec[sw] -= sub;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | struct Step
|
---|
251 | {
|
---|
252 | Step() : avg(0), rms(0), pos(0), cnt(0) { }
|
---|
253 | double avg;
|
---|
254 | double rms;
|
---|
255 | double pos;
|
---|
256 | uint16_t cnt;
|
---|
257 | };
|
---|
258 |
|
---|
259 | static Step CorrectStep(float *vec, uint16_t nch, uint16_t roi,
|
---|
260 | const int16_t *prev, const int16_t *start,
|
---|
261 | const int16_t offset, const uint16_t *map)
|
---|
262 | {
|
---|
263 | Step rc;
|
---|
264 |
|
---|
265 | for (size_t ch=0; ch<nch; ch += 9)
|
---|
266 | {
|
---|
267 | if (prev[ch]<0 || start[ch]<0)
|
---|
268 | continue;
|
---|
269 |
|
---|
270 | const int16_t dist = (prev[ch]-start[ch]+1024+offset)%1024;
|
---|
271 | const double step = FindStep(ch, vec, roi, dist, map);
|
---|
272 | if (step==0)
|
---|
273 | continue;
|
---|
274 |
|
---|
275 | rc.pos += dist;
|
---|
276 | rc.avg += step;
|
---|
277 | rc.rms += step*step;
|
---|
278 | rc.cnt++;
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (rc.cnt==0 || rc.avg==0)
|
---|
282 | return Step();
|
---|
283 |
|
---|
284 | rc.pos /= rc.cnt;
|
---|
285 | rc.avg /= rc.cnt;
|
---|
286 | rc.rms /= rc.cnt;
|
---|
287 |
|
---|
288 | rc.rms = sqrt(rc.rms-rc.avg*rc.avg);
|
---|
289 |
|
---|
290 | for (size_t ch=0; ch<nch; ch += 9)
|
---|
291 | {
|
---|
292 | const int16_t dist = (prev[ch]-start[ch]+1024+offset)%1024;
|
---|
293 | SubtractStep(ch, rc.avg, vec, roi, dist, map);
|
---|
294 | }
|
---|
295 |
|
---|
296 | return rc;
|
---|
297 | }
|
---|
298 |
|
---|
299 | static void RemoveSpikes(float *vec, uint32_t roi)
|
---|
300 | {
|
---|
301 | if (roi<4)
|
---|
302 | return;
|
---|
303 |
|
---|
304 | for (size_t ch=0; ch<1440; ch++)
|
---|
305 | {
|
---|
306 | float *p = vec + ch*roi;
|
---|
307 |
|
---|
308 | for (size_t i=1; i<roi-2; i++)
|
---|
309 | {
|
---|
310 | if (p[i]-p[i-1]>25 && p[i]-p[i+1]>25)
|
---|
311 | {
|
---|
312 | p[i] = (p[i-1]+p[i+1])/2;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (p[i]-p[i-1]>22 && fabs(p[i]-p[i+1])<4 && p[i+1]-p[i+2]>22)
|
---|
316 | {
|
---|
317 | p[i] = (p[i-1]+p[i+2])/2;
|
---|
318 | p[i+1] = p[i];
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | static void RemoveSpikes2(float *vec, uint32_t roi)//from Werner
|
---|
325 | {
|
---|
326 | if (roi<4)
|
---|
327 | return;
|
---|
328 |
|
---|
329 | for (size_t ch=0; ch<1440; ch++)
|
---|
330 | {
|
---|
331 | float *p = vec + ch*roi;
|
---|
332 |
|
---|
333 | std::vector<float> Ameas(p, p+roi);
|
---|
334 |
|
---|
335 | std::vector<float> N1mean(roi);
|
---|
336 | for (size_t i=2; i<roi-2; i++)
|
---|
337 | {
|
---|
338 | N1mean[i] = (p[i-1] + p[i+1])/2.;
|
---|
339 | }
|
---|
340 |
|
---|
341 | const float fract = 0.8;
|
---|
342 | float x, xp, xpp;
|
---|
343 |
|
---|
344 | for (size_t i=0; i<roi-3; i++)
|
---|
345 | {
|
---|
346 | x = Ameas[i] - N1mean[i];
|
---|
347 | if ( x > -5.)
|
---|
348 | continue;
|
---|
349 |
|
---|
350 | xp = Ameas[i+1] - N1mean[i+1];
|
---|
351 | xpp = Ameas[i+2] - N1mean[i+2];
|
---|
352 |
|
---|
353 | if(Ameas[i+2] - (Ameas[i] + Ameas[i+3])/2. > 10.)
|
---|
354 | {
|
---|
355 | p[i+1]=(Ameas[i] + Ameas[i+3])/2.;
|
---|
356 | p[i+2]=(Ameas[i] + Ameas[i+3])/2.;
|
---|
357 |
|
---|
358 | i += 3;
|
---|
359 |
|
---|
360 | continue;
|
---|
361 | }
|
---|
362 | if ( (xp > -2.*x*fract) && (xpp < -10.) )
|
---|
363 | {
|
---|
364 | p[i+1] = N1mean[i+1];
|
---|
365 | N1mean[i+2] = (Ameas[i+1] - Ameas[i+3] / 2.);
|
---|
366 |
|
---|
367 | i += 2;
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | std::pair<std::vector<double>,std::vector<double> > GetSampleStats() const
|
---|
374 | {
|
---|
375 | if (fNumEntries==0)
|
---|
376 | return make_pair(std::vector<double>(),std::vector<double>());
|
---|
377 |
|
---|
378 | std::vector<double> mean(fSum.size());
|
---|
379 | std::vector<double> error(fSum.size());
|
---|
380 |
|
---|
381 | std::vector<int64_t>::const_iterator it = fSum.begin();
|
---|
382 | std::vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
383 | std::vector<double>::iterator im = mean.begin();
|
---|
384 | std::vector<double>::iterator ie = error.begin();
|
---|
385 |
|
---|
386 | while (it!=fSum.end())
|
---|
387 | {
|
---|
388 | *im = /*cnt<fResult.size() ? fResult[cnt] :*/ double(*it)/fNumEntries;
|
---|
389 | *ie = sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
390 |
|
---|
391 | im++;
|
---|
392 | ie++;
|
---|
393 | it++;
|
---|
394 | i2++;
|
---|
395 | }
|
---|
396 |
|
---|
397 |
|
---|
398 | /*
|
---|
399 | valarray<double> ...
|
---|
400 |
|
---|
401 | mean /= fNumEntries;
|
---|
402 | error = sqrt(error/fNumEntries - mean*mean);
|
---|
403 | */
|
---|
404 |
|
---|
405 | return make_pair(mean, error);
|
---|
406 | }
|
---|
407 |
|
---|
408 | void GetSampleStats(float *ptr, float scale) const
|
---|
409 | {
|
---|
410 | const size_t sz = fNumSamples*fNumChannels;
|
---|
411 |
|
---|
412 | if (fNumEntries==0)
|
---|
413 | {
|
---|
414 | memset(ptr, 0, sizeof(float)*sz*2);
|
---|
415 | return;
|
---|
416 | }
|
---|
417 |
|
---|
418 | std::vector<int64_t>::const_iterator it = fSum.begin();
|
---|
419 | std::vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
420 |
|
---|
421 | while (it!=fSum.end())
|
---|
422 | {
|
---|
423 | *ptr = scale*double(*it)/fNumEntries;
|
---|
424 | *(ptr+sz) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
425 |
|
---|
426 | ptr++;
|
---|
427 | it++;
|
---|
428 | i2++;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | static double GetPixelStats(float *ptr, const float *data, uint16_t roi)
|
---|
433 | {
|
---|
434 | if (roi==0)
|
---|
435 | return -1;
|
---|
436 |
|
---|
437 | const int beg = roi>10 ? 10 : 0;
|
---|
438 |
|
---|
439 | double max = 0;
|
---|
440 | for (int i=0; i<1440; i++)
|
---|
441 | {
|
---|
442 | const float *vec = data+i*roi;
|
---|
443 |
|
---|
444 | int pos = beg;
|
---|
445 | double sum = vec[beg];
|
---|
446 | double sum2 = vec[beg]*vec[beg];
|
---|
447 | for (int j=beg; j<roi; j++)
|
---|
448 | {
|
---|
449 | sum += vec[j];
|
---|
450 | sum2 += vec[j]*vec[j];
|
---|
451 |
|
---|
452 | if (vec[j]>vec[pos])
|
---|
453 | pos = j;
|
---|
454 | }
|
---|
455 | sum /= roi-beg;
|
---|
456 | sum2 /= roi-beg;
|
---|
457 |
|
---|
458 | if (vec[pos]>0)
|
---|
459 | max = vec[pos];
|
---|
460 |
|
---|
461 | *(ptr+0*1440+i) = sum;
|
---|
462 | *(ptr+1*1440+i) = sqrt(sum2 - sum * sum);
|
---|
463 | *(ptr+2*1440+i) = vec[pos];
|
---|
464 | *(ptr+3*1440+i) = pos;
|
---|
465 | }
|
---|
466 |
|
---|
467 | return max;
|
---|
468 | }
|
---|
469 |
|
---|
470 | static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last)
|
---|
471 | {
|
---|
472 | if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last<first)
|
---|
473 | return;
|
---|
474 |
|
---|
475 | for (int i=0; i<1440; i++)
|
---|
476 | {
|
---|
477 | const float *beg = data+i*roi+first;
|
---|
478 | const float *end = data+i*roi+last;
|
---|
479 |
|
---|
480 | const float *pmax = beg;
|
---|
481 |
|
---|
482 | for (const float *ptr=beg+1; ptr<=end; ptr++)
|
---|
483 | if (*ptr>*pmax)
|
---|
484 | pmax = ptr;
|
---|
485 |
|
---|
486 | max[i] = *pmax;
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | const std::vector<int64_t> &GetSum() const { return fSum; }
|
---|
491 |
|
---|
492 | uint64_t GetNumEntries() const { return fNumEntries; }
|
---|
493 | };
|
---|
494 |
|
---|
495 | class DrsCalibrateTime
|
---|
496 | {
|
---|
497 | public:
|
---|
498 | uint64_t fNumEntries;
|
---|
499 |
|
---|
500 | size_t fNumSamples;
|
---|
501 | size_t fNumChannels;
|
---|
502 |
|
---|
503 | std::vector<std::pair<double, double>> fStat;
|
---|
504 |
|
---|
505 | public:
|
---|
506 | DrsCalibrateTime() : fNumEntries(0), fNumSamples(0), fNumChannels(0)
|
---|
507 | {
|
---|
508 | InitSize(160, 1024);
|
---|
509 | }
|
---|
510 |
|
---|
511 | DrsCalibrateTime(const DrsCalibrateTime &p) : fNumEntries(p.fNumEntries), fNumSamples(p.fNumSamples), fNumChannels(p.fNumChannels), fStat(p.fStat)
|
---|
512 | {
|
---|
513 | }
|
---|
514 |
|
---|
515 | double Sum(uint32_t i) const { return fStat[i].first; }
|
---|
516 | double W(uint32_t i) const { return fStat[i].second; }
|
---|
517 |
|
---|
518 | virtual void InitSize(uint16_t channels, uint16_t samples)
|
---|
519 | {
|
---|
520 | fNumChannels = channels;
|
---|
521 | fNumSamples = samples;
|
---|
522 |
|
---|
523 | fNumEntries = 0;
|
---|
524 |
|
---|
525 | fStat.clear();
|
---|
526 |
|
---|
527 | fStat.resize(samples*channels);
|
---|
528 | }
|
---|
529 |
|
---|
530 | void AddT(const float *val, const int16_t *start, signed char edge=0)
|
---|
531 | {
|
---|
532 | if (fNumSamples!=1024 || fNumChannels!=160)
|
---|
533 | return;
|
---|
534 |
|
---|
535 | // Rising or falling edge detection has the advantage that
|
---|
536 | // we are much less sensitive to baseline shifts
|
---|
537 |
|
---|
538 | for (size_t ch=0; ch<160; ch++)
|
---|
539 | {
|
---|
540 | const size_t tm = ch*9+8;
|
---|
541 |
|
---|
542 | const int16_t spos = start[tm];
|
---|
543 | if (spos<0)
|
---|
544 | continue;
|
---|
545 |
|
---|
546 | const size_t pos = ch*1024;
|
---|
547 |
|
---|
548 | double p_prev = 0;
|
---|
549 | int32_t i_prev = -1;
|
---|
550 |
|
---|
551 | for (size_t i=0; i<1024-1; i++)
|
---|
552 | {
|
---|
553 | const size_t rel = tm*1024 + i;
|
---|
554 |
|
---|
555 | const float &v0 = val[rel]; //-avg;
|
---|
556 | const float &v1 = val[rel+1];//-avg;
|
---|
557 |
|
---|
558 | // Is rising edge?
|
---|
559 | if (edge>0 && v0>0)
|
---|
560 | continue;
|
---|
561 |
|
---|
562 | // Is falling edge?
|
---|
563 | if (edge<0 && v0<0)
|
---|
564 | continue;
|
---|
565 |
|
---|
566 | // Has sign changed?
|
---|
567 | if ((v0<0 && v1<0) || (v0>0 && v1>0))
|
---|
568 | continue;
|
---|
569 |
|
---|
570 | const double p = v0==v1 ? 0.5 : v0/(v0-v1);
|
---|
571 |
|
---|
572 | const double l = i+p - (i_prev+p_prev);
|
---|
573 |
|
---|
574 | if (i_prev>=0)
|
---|
575 | {
|
---|
576 | const double w0 = 1-p_prev;
|
---|
577 | const double w1 = p;
|
---|
578 |
|
---|
579 | fStat[pos+(spos+i_prev)%1024].first += w0*l;
|
---|
580 | fStat[pos+(spos+i_prev)%1024].second += w0;
|
---|
581 |
|
---|
582 | for (size_t k=i_prev+1; k<i; k++)
|
---|
583 | {
|
---|
584 | fStat[pos+(spos+k)%1024].first += l;
|
---|
585 | fStat[pos+(spos+k)%1024].second += 1;
|
---|
586 | }
|
---|
587 |
|
---|
588 | fStat[pos+(spos+i)%1024].first += w1*l;
|
---|
589 | fStat[pos+(spos+i)%1024].second += w1;
|
---|
590 | }
|
---|
591 |
|
---|
592 | p_prev = p;
|
---|
593 | i_prev = i;
|
---|
594 | }
|
---|
595 | }
|
---|
596 | fNumEntries++;
|
---|
597 | }
|
---|
598 |
|
---|
599 | void FillEmptyBins()
|
---|
600 | {
|
---|
601 | for (int ch=0; ch<160; ch++)
|
---|
602 | {
|
---|
603 | const auto beg = fStat.begin() + ch*1024;
|
---|
604 | const auto end = beg + 1024;
|
---|
605 |
|
---|
606 | double avg = 0;
|
---|
607 | uint32_t num = 0;
|
---|
608 | for (auto it=beg; it!=end; it++)
|
---|
609 | {
|
---|
610 | if (it->second<fNumEntries-0.5)
|
---|
611 | continue;
|
---|
612 |
|
---|
613 | avg += it->first / it->second;
|
---|
614 | num++;
|
---|
615 | }
|
---|
616 | avg /= num;
|
---|
617 |
|
---|
618 | for (auto it=beg; it!=end; it++)
|
---|
619 | {
|
---|
620 | if (it->second>=fNumEntries-0.5)
|
---|
621 | continue;
|
---|
622 |
|
---|
623 | // {
|
---|
624 | // result[i+1].first = *is2;
|
---|
625 | // result[i+1].second = *iw2;
|
---|
626 | // }
|
---|
627 | // else
|
---|
628 | // {
|
---|
629 | it->first = avg*fNumEntries;
|
---|
630 | it->second = fNumEntries;
|
---|
631 | // }
|
---|
632 | }
|
---|
633 | }
|
---|
634 | }
|
---|
635 |
|
---|
636 | DrsCalibrateTime GetComplete() const
|
---|
637 | {
|
---|
638 | DrsCalibrateTime rc(*this);
|
---|
639 | rc.FillEmptyBins();
|
---|
640 | return rc;
|
---|
641 | }
|
---|
642 |
|
---|
643 | void CalcResult()
|
---|
644 | {
|
---|
645 | for (int ch=0; ch<160; ch++)
|
---|
646 | {
|
---|
647 | const auto beg = fStat.begin() + ch*1024;
|
---|
648 | const auto end = beg + 1024;
|
---|
649 |
|
---|
650 | // Calculate mean
|
---|
651 | double s = 0;
|
---|
652 | double w = 0;
|
---|
653 |
|
---|
654 | for (auto it=beg; it!=end; it++)
|
---|
655 | {
|
---|
656 | s += it->first;
|
---|
657 | w += it->second;
|
---|
658 | }
|
---|
659 |
|
---|
660 | s /= w;
|
---|
661 |
|
---|
662 | double sumw = 0;
|
---|
663 | double sumv = 0;
|
---|
664 | int n = 0;
|
---|
665 |
|
---|
666 | // Sums about many values are numerically less stable than
|
---|
667 | // just sums over less. So we do the exercise from both sides.
|
---|
668 | for (auto it=beg; it!=end-512; it++, n++)
|
---|
669 | {
|
---|
670 | const double valv = it->first;
|
---|
671 | const double valw = it->second;
|
---|
672 |
|
---|
673 | it->first = sumv>0 ? n*(1-s*sumw/sumv) :0;
|
---|
674 |
|
---|
675 | sumv += valv;
|
---|
676 | sumw += valw;
|
---|
677 | }
|
---|
678 |
|
---|
679 | sumw = 0;
|
---|
680 | sumv = 0;
|
---|
681 | n = 1;
|
---|
682 |
|
---|
683 | for (auto it=end-1; it!=beg-1+512; it--, n++)
|
---|
684 | {
|
---|
685 | const double valv = it->first;
|
---|
686 | const double valw = it->second;
|
---|
687 |
|
---|
688 | sumv += valv;
|
---|
689 | sumw += valw;
|
---|
690 |
|
---|
691 | it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0;
|
---|
692 | }
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | DrsCalibrateTime GetResult() const
|
---|
697 | {
|
---|
698 | DrsCalibrateTime rc(*this);
|
---|
699 | rc.CalcResult();
|
---|
700 | return rc;
|
---|
701 | }
|
---|
702 |
|
---|
703 | double Offset(uint32_t ch, double pos) const
|
---|
704 | {
|
---|
705 | const auto p = fStat.begin() + ch*1024;
|
---|
706 |
|
---|
707 | const uint32_t f = floor(pos);
|
---|
708 |
|
---|
709 | const double v0 = p[f].first;
|
---|
710 | const double v1 = p[(f+1)%1024].first;
|
---|
711 |
|
---|
712 | return v0 + fmod(pos, 1)*(v1-v0);
|
---|
713 | }
|
---|
714 |
|
---|
715 | double Calib(uint32_t ch, double pos) const
|
---|
716 | {
|
---|
717 | return pos-Offset(ch, pos);
|
---|
718 | }
|
---|
719 | };
|
---|
720 |
|
---|
721 | struct DrsCalibration
|
---|
722 | {
|
---|
723 | std::vector<int32_t> fOffset;
|
---|
724 | std::vector<int64_t> fGain;
|
---|
725 | std::vector<int64_t> fTrgOff;
|
---|
726 |
|
---|
727 | uint64_t fNumOffset;
|
---|
728 | uint64_t fNumGain;
|
---|
729 | uint64_t fNumTrgOff;
|
---|
730 |
|
---|
731 | uint32_t fStep;
|
---|
732 | uint16_t fRoi; // Region of interest for trgoff
|
---|
733 | uint16_t fNumTm; // Number of time marker channels in trgoff
|
---|
734 |
|
---|
735 | // uint16_t fDAC[8];
|
---|
736 |
|
---|
737 | DrsCalibration() :
|
---|
738 | fOffset (1440*1024, 0),
|
---|
739 | fGain (1440*1024, 4096),
|
---|
740 | fTrgOff (1600*1024, 0),
|
---|
741 | fNumOffset(1),
|
---|
742 | fNumGain(2000),
|
---|
743 | fNumTrgOff(1),
|
---|
744 | fStep(0)
|
---|
745 | {
|
---|
746 | }
|
---|
747 |
|
---|
748 | void Clear()
|
---|
749 | {
|
---|
750 | // Default gain:
|
---|
751 | // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
|
---|
752 | fOffset.assign(1440*1024, 0);
|
---|
753 | fGain.assign (1440*1024, 4096);
|
---|
754 | fTrgOff.assign(1600*1024, 0);
|
---|
755 |
|
---|
756 | fNumOffset = 1;
|
---|
757 | fNumGain = 2000;
|
---|
758 | fNumTrgOff = 1;
|
---|
759 |
|
---|
760 | fStep = 0;
|
---|
761 | }
|
---|
762 |
|
---|
763 | std::string ReadFitsImp(const std::string &str, std::vector<float> &vec)
|
---|
764 | {
|
---|
765 | std::fits file(str);
|
---|
766 | if (!file)
|
---|
767 | {
|
---|
768 | std::ostringstream msg;
|
---|
769 | msg << "Could not open file '" << str << "': " << strerror(errno);
|
---|
770 | return msg.str();
|
---|
771 | }
|
---|
772 |
|
---|
773 | if (file.GetStr("TELESCOP")!="FACT")
|
---|
774 | {
|
---|
775 | std::ostringstream msg;
|
---|
776 | msg << "Reading '" << str << "' failed: Not a valid FACT file (TELESCOP not FACT in header)";
|
---|
777 | return msg.str();
|
---|
778 | }
|
---|
779 |
|
---|
780 | if (!file.HasKey("STEP"))
|
---|
781 | {
|
---|
782 | std::ostringstream msg;
|
---|
783 | msg << "Reading '" << str << "' failed: Is not a DRS calib file (STEP not found in header)";
|
---|
784 | return msg.str();
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (file.GetNumRows()!=1)
|
---|
788 | {
|
---|
789 | std::ostringstream msg;
|
---|
790 | msg << "Reading '" << str << "' failed: Number of rows in table is not 1.";
|
---|
791 | return msg.str();
|
---|
792 | }
|
---|
793 |
|
---|
794 | fStep = file.GetUInt("STEP");
|
---|
795 | fNumOffset = file.GetUInt("NBOFFSET");
|
---|
796 | fNumGain = file.GetUInt("NBGAIN");
|
---|
797 | fNumTrgOff = file.GetUInt("NBTRGOFF");
|
---|
798 | fRoi = file.GetUInt("NROI");
|
---|
799 | fNumTm = file.HasKey("NTM") ? file.GetUInt("NTM") : 0;
|
---|
800 | /*
|
---|
801 | fDAC[0] = file.GetUInt("DAC_A");
|
---|
802 | fDAC[1] = file.GetUInt("DAC_B");
|
---|
803 | fDAC[4] = file.GetUInt("DAC_C");
|
---|
804 | */
|
---|
805 | vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
|
---|
806 |
|
---|
807 | float *base = vec.data();
|
---|
808 |
|
---|
809 | reinterpret_cast<uint32_t*>(base)[0] = fRoi;
|
---|
810 |
|
---|
811 | file.SetPtrAddress("RunNumberBaseline", base+1, 1);
|
---|
812 | file.SetPtrAddress("RunNumberGain", base+2, 1);
|
---|
813 | file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1);
|
---|
814 | file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440);
|
---|
815 | file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440);
|
---|
816 | file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440);
|
---|
817 | file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440);
|
---|
818 | file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440);
|
---|
819 | file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+fRoi*1440, fRoi*1440);
|
---|
820 | if (fNumTm>0)
|
---|
821 | {
|
---|
822 | file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm);
|
---|
823 | file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
|
---|
824 | }
|
---|
825 |
|
---|
826 | if (!file.GetNextRow())
|
---|
827 | {
|
---|
828 | std::ostringstream msg;
|
---|
829 | msg << "Reading data from " << str << " failed.";
|
---|
830 | return msg.str();
|
---|
831 | }
|
---|
832 | /*
|
---|
833 | fDAC[2] = fDAC[1];
|
---|
834 | fDAC[4] = fDAC[1];
|
---|
835 |
|
---|
836 | fDAC[5] = fDAC[4];
|
---|
837 | fDAC[6] = fDAC[4];
|
---|
838 | fDAC[7] = fDAC[4];
|
---|
839 | */
|
---|
840 | fOffset.resize(1024*1440);
|
---|
841 | fGain.resize(1024*1440);
|
---|
842 |
|
---|
843 | fTrgOff.resize(fRoi*(1440+fNumTm));
|
---|
844 |
|
---|
845 | // Convert back to ADC counts: 256/125 = 4096/2000
|
---|
846 | // Convert back to sum (mean * num_entries)
|
---|
847 | for (int i=0; i<1024*1440; i++)
|
---|
848 | {
|
---|
849 | fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125;
|
---|
850 | fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
|
---|
851 | }
|
---|
852 |
|
---|
853 | for (int i=0; i<fRoi*1440; i++)
|
---|
854 | fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
|
---|
855 |
|
---|
856 | for (int i=0; i<fRoi*fNumTm; i++)
|
---|
857 | fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
|
---|
858 |
|
---|
859 |
|
---|
860 | // DAC: 0..2.5V == 0..65535
|
---|
861 | // V-mV: 1000
|
---|
862 | //fNumGain *= 2500*50000;
|
---|
863 | //for (int i=0; i<1024*1440; i++)
|
---|
864 | // fGain[i] *= 65536;
|
---|
865 | if (fStep==0)
|
---|
866 | {
|
---|
867 | for (int i=0; i<1024*1440; i++)
|
---|
868 | fGain[i] = fNumOffset*4096;
|
---|
869 | }
|
---|
870 | else
|
---|
871 | {
|
---|
872 | fNumGain *= 1953125;
|
---|
873 | for (int i=0; i<1024*1440; i++)
|
---|
874 | fGain[i] *= 1024;
|
---|
875 | }
|
---|
876 |
|
---|
877 | // Now mark the stored DRS data as "officially valid"
|
---|
878 | // However, this is not thread safe. It only ensures that
|
---|
879 | // this data is not used before it is completely and correctly
|
---|
880 | // read.
|
---|
881 | fStep++;
|
---|
882 |
|
---|
883 | return std::string();
|
---|
884 | }
|
---|
885 |
|
---|
886 | std::string ReadFitsImp(const std::string &str)
|
---|
887 | {
|
---|
888 | std::vector<float> vec;
|
---|
889 | return ReadFitsImp(str, vec);
|
---|
890 | }
|
---|
891 |
|
---|
892 | bool IsValid() { return fStep>2; }
|
---|
893 |
|
---|
894 | bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
|
---|
895 | {
|
---|
896 | if (roi!=fRoi)
|
---|
897 | {
|
---|
898 | for (size_t ch=0; ch<1440; ch++)
|
---|
899 | {
|
---|
900 | const size_t pos = ch*roi;
|
---|
901 | const size_t drs = ch*1024;
|
---|
902 |
|
---|
903 | DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
|
---|
904 | fOffset.data()+drs, fNumOffset,
|
---|
905 | fGain.data() +drs, fNumGain);
|
---|
906 | }
|
---|
907 |
|
---|
908 | return false;
|
---|
909 | }
|
---|
910 |
|
---|
911 | for (size_t ch=0; ch<1440; ch++)
|
---|
912 | {
|
---|
913 | const size_t pos = ch*fRoi;
|
---|
914 | const size_t drs = ch*1024;
|
---|
915 |
|
---|
916 | DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
|
---|
917 | fOffset.data()+drs, fNumOffset,
|
---|
918 | fGain.data() +drs, fNumGain,
|
---|
919 | fTrgOff.data()+pos, fNumTrgOff);
|
---|
920 | }
|
---|
921 |
|
---|
922 | for (size_t ch=0; ch<fNumTm; ch++)
|
---|
923 | {
|
---|
924 | const size_t pos = (ch+1440)*fRoi;
|
---|
925 | const size_t drs = (ch*9+8)*1024;
|
---|
926 |
|
---|
927 | DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
|
---|
928 | fOffset.data()+drs, fNumOffset,
|
---|
929 | fGain.data() +drs, fNumGain,
|
---|
930 | fTrgOff.data()+pos, fNumTrgOff);
|
---|
931 | }
|
---|
932 |
|
---|
933 | return true;
|
---|
934 | }
|
---|
935 | };
|
---|
936 |
|
---|
937 | #endif
|
---|