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 void RemoveSpikes(float *vec, uint32_t roi)
|
---|
213 | {
|
---|
214 | if (roi<4)
|
---|
215 | return;
|
---|
216 |
|
---|
217 | for (size_t ch=0; ch<1440; ch++)
|
---|
218 | {
|
---|
219 | float *p = vec + ch*roi;
|
---|
220 |
|
---|
221 | for (size_t i=1; i<roi-2; i++)
|
---|
222 | {
|
---|
223 | if (p[i]-p[i-1]>25 && p[i]-p[i+1]>25)
|
---|
224 | {
|
---|
225 | p[i] = (p[i-1]+p[i+1])/2;
|
---|
226 | }
|
---|
227 |
|
---|
228 | if (p[i]-p[i-1]>22 && fabs(p[i]-p[i+1])<4 && p[i+1]-p[i+2]>22)
|
---|
229 | {
|
---|
230 | p[i] = (p[i-1]+p[i+2])/2;
|
---|
231 | p[i+1] = p[i];
|
---|
232 | }
|
---|
233 | }
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | static void RemoveSpikes2(float *vec, uint32_t roi)//from Werner
|
---|
238 | {
|
---|
239 | if (roi<4)
|
---|
240 | return;
|
---|
241 |
|
---|
242 | for (size_t ch=0; ch<1440; ch++)
|
---|
243 | {
|
---|
244 | float *p = vec + ch*roi;
|
---|
245 |
|
---|
246 | std::vector<float> Ameas(p, p+roi);
|
---|
247 |
|
---|
248 | std::vector<float> N1mean(roi);
|
---|
249 | for (size_t i=2; i<roi-2; i++)
|
---|
250 | {
|
---|
251 | N1mean[i] = (p[i-1] + p[i+1])/2.;
|
---|
252 | }
|
---|
253 |
|
---|
254 | const float fract = 0.8;
|
---|
255 | float x, xp, xpp;
|
---|
256 |
|
---|
257 | for (size_t i=0; i<roi-3; i++)
|
---|
258 | {
|
---|
259 | x = Ameas[i] - N1mean[i];
|
---|
260 | if ( x > -5.)
|
---|
261 | continue;
|
---|
262 |
|
---|
263 | xp = Ameas[i+1] - N1mean[i+1];
|
---|
264 | xpp = Ameas[i+2] - N1mean[i+2];
|
---|
265 |
|
---|
266 | if(Ameas[i+2] - (Ameas[i] + Ameas[i+3])/2. > 10.)
|
---|
267 | {
|
---|
268 | p[i+1]=(Ameas[i] + Ameas[i+3])/2.;
|
---|
269 | p[i+2]=(Ameas[i] + Ameas[i+3])/2.;
|
---|
270 |
|
---|
271 | i += 3;
|
---|
272 |
|
---|
273 | continue;
|
---|
274 | }
|
---|
275 | if ( (xp > -2.*x*fract) && (xpp < -10.) )
|
---|
276 | {
|
---|
277 | p[i+1] = N1mean[i+1];
|
---|
278 | N1mean[i+2] = (Ameas[i+1] - Ameas[i+3] / 2.);
|
---|
279 |
|
---|
280 | i += 2;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | std::pair<std::vector<double>,std::vector<double> > GetSampleStats() const
|
---|
287 | {
|
---|
288 | if (fNumEntries==0)
|
---|
289 | return make_pair(std::vector<double>(),std::vector<double>());
|
---|
290 |
|
---|
291 | std::vector<double> mean(fSum.size());
|
---|
292 | std::vector<double> error(fSum.size());
|
---|
293 |
|
---|
294 | std::vector<int64_t>::const_iterator it = fSum.begin();
|
---|
295 | std::vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
296 | std::vector<double>::iterator im = mean.begin();
|
---|
297 | std::vector<double>::iterator ie = error.begin();
|
---|
298 |
|
---|
299 | while (it!=fSum.end())
|
---|
300 | {
|
---|
301 | *im = /*cnt<fResult.size() ? fResult[cnt] :*/ double(*it)/fNumEntries;
|
---|
302 | *ie = sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
303 |
|
---|
304 | im++;
|
---|
305 | ie++;
|
---|
306 | it++;
|
---|
307 | i2++;
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | /*
|
---|
312 | valarray<double> ...
|
---|
313 |
|
---|
314 | mean /= fNumEntries;
|
---|
315 | error = sqrt(error/fNumEntries - mean*mean);
|
---|
316 | */
|
---|
317 |
|
---|
318 | return make_pair(mean, error);
|
---|
319 | }
|
---|
320 |
|
---|
321 | void GetSampleStats(float *ptr, float scale) const
|
---|
322 | {
|
---|
323 | const size_t sz = fNumSamples*fNumChannels;
|
---|
324 |
|
---|
325 | if (fNumEntries==0)
|
---|
326 | {
|
---|
327 | memset(ptr, 0, sizeof(float)*sz*2);
|
---|
328 | return;
|
---|
329 | }
|
---|
330 |
|
---|
331 | std::vector<int64_t>::const_iterator it = fSum.begin();
|
---|
332 | std::vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
333 |
|
---|
334 | while (it!=fSum.end())
|
---|
335 | {
|
---|
336 | *ptr = scale*double(*it)/fNumEntries;
|
---|
337 | *(ptr+sz) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
338 |
|
---|
339 | ptr++;
|
---|
340 | it++;
|
---|
341 | i2++;
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | static void GetPixelStats(float *ptr, const float *data, uint16_t roi)
|
---|
346 | {
|
---|
347 | if (roi==0)
|
---|
348 | return;
|
---|
349 |
|
---|
350 | for (int i=0; i<1440; i++)
|
---|
351 | {
|
---|
352 | const float *vec = data+i*roi;
|
---|
353 |
|
---|
354 | int pos = 0;
|
---|
355 | double sum = vec[0];
|
---|
356 | double sum2 = vec[0]*vec[0];
|
---|
357 | for (int j=1; j<roi; j++)
|
---|
358 | {
|
---|
359 | sum += vec[j];
|
---|
360 | sum2 += vec[j]*vec[j];
|
---|
361 |
|
---|
362 | if (vec[j]>vec[pos])
|
---|
363 | pos = j;
|
---|
364 | }
|
---|
365 | sum /= roi;
|
---|
366 | sum2 /= roi;
|
---|
367 |
|
---|
368 | *(ptr+0*1440+i) = sum;
|
---|
369 | *(ptr+1*1440+i) = sqrt(sum2 - sum * sum);
|
---|
370 | *(ptr+2*1440+i) = vec[pos];
|
---|
371 | *(ptr+3*1440+i) = pos;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last)
|
---|
376 | {
|
---|
377 | if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last<first)
|
---|
378 | return;
|
---|
379 |
|
---|
380 | for (int i=0; i<1440; i++)
|
---|
381 | {
|
---|
382 | const float *beg = data+i*roi+first;
|
---|
383 | const float *end = data+i*roi+last;
|
---|
384 |
|
---|
385 | const float *pmax = beg;
|
---|
386 |
|
---|
387 | for (const float *ptr=beg+1; ptr<=end; ptr++)
|
---|
388 | if (*ptr>*pmax)
|
---|
389 | pmax = ptr;
|
---|
390 |
|
---|
391 | max[i] = *pmax;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | const std::vector<int64_t> &GetSum() const { return fSum; }
|
---|
396 |
|
---|
397 | uint64_t GetNumEntries() const { return fNumEntries; }
|
---|
398 | };
|
---|
399 |
|
---|
400 | class DrsCalibrateTime
|
---|
401 | {
|
---|
402 | public:
|
---|
403 | uint64_t fNumEntries;
|
---|
404 |
|
---|
405 | size_t fNumSamples;
|
---|
406 | size_t fNumChannels;
|
---|
407 |
|
---|
408 | std::vector<std::pair<double, double>> fStat;
|
---|
409 |
|
---|
410 | public:
|
---|
411 | DrsCalibrateTime() : fNumEntries(0), fNumSamples(0), fNumChannels(0)
|
---|
412 | {
|
---|
413 | InitSize(160, 1024);
|
---|
414 | }
|
---|
415 |
|
---|
416 | DrsCalibrateTime(const DrsCalibrateTime &p) : fNumEntries(p.fNumEntries), fNumSamples(p.fNumSamples), fNumChannels(p.fNumChannels), fStat(p.fStat)
|
---|
417 | {
|
---|
418 | }
|
---|
419 |
|
---|
420 | double Sum(uint32_t i) const { return fStat[i].first; }
|
---|
421 | double W(uint32_t i) const { return fStat[i].second; }
|
---|
422 |
|
---|
423 | void InitSize(uint16_t channels, uint16_t samples)
|
---|
424 | {
|
---|
425 | fNumChannels = channels;
|
---|
426 | fNumSamples = samples;
|
---|
427 |
|
---|
428 | fNumEntries = 0;
|
---|
429 |
|
---|
430 | fStat.clear();
|
---|
431 |
|
---|
432 | fStat.resize(samples*channels);
|
---|
433 | }
|
---|
434 |
|
---|
435 | void AddT(const float *val, const int16_t *start, signed char edge=0)
|
---|
436 | {
|
---|
437 | if (fNumSamples!=1024 || fNumChannels!=160)
|
---|
438 | return;
|
---|
439 |
|
---|
440 | // Rising or falling edge detection has the advantage that
|
---|
441 | // we are much less sensitive to baseline shifts
|
---|
442 |
|
---|
443 | for (size_t ch=0; ch<160; ch++)
|
---|
444 | {
|
---|
445 | const size_t tm = ch*9+8;
|
---|
446 |
|
---|
447 | const int16_t spos = start[tm];
|
---|
448 | if (spos<0)
|
---|
449 | continue;
|
---|
450 |
|
---|
451 | const size_t pos = ch*1024;
|
---|
452 |
|
---|
453 | double p_prev = 0;
|
---|
454 | int32_t i_prev = -1;
|
---|
455 |
|
---|
456 | for (size_t i=0; i<1024-1; i++)
|
---|
457 | {
|
---|
458 | const size_t rel = tm*1024 + i;
|
---|
459 |
|
---|
460 | const float &v0 = val[rel]; //-avg;
|
---|
461 | const float &v1 = val[rel+1];//-avg;
|
---|
462 |
|
---|
463 | // Is rising edge?
|
---|
464 | if (edge>0 && v0>0)
|
---|
465 | continue;
|
---|
466 |
|
---|
467 | // Is falling edge?
|
---|
468 | if (edge<0 && v0<0)
|
---|
469 | continue;
|
---|
470 |
|
---|
471 | // Has sign changed?
|
---|
472 | if ((v0<0 && v1<0) || (v0>0 && v1>0))
|
---|
473 | continue;
|
---|
474 |
|
---|
475 | const double p = v0==v1 ? 0.5 : v0/(v0-v1);
|
---|
476 |
|
---|
477 | const double l = i+p - (i_prev+p_prev);
|
---|
478 |
|
---|
479 | if (i_prev>=0)
|
---|
480 | {
|
---|
481 | const double w0 = 1-p_prev;
|
---|
482 | const double w1 = p;
|
---|
483 |
|
---|
484 | fStat[pos+(spos+i_prev)%1024].first += w0*l;
|
---|
485 | fStat[pos+(spos+i_prev)%1024].second += w0;
|
---|
486 |
|
---|
487 | for (size_t k=i_prev+1; k<i; k++)
|
---|
488 | {
|
---|
489 | fStat[pos+(spos+k)%1024].first += l;
|
---|
490 | fStat[pos+(spos+k)%1024].second += 1;
|
---|
491 | }
|
---|
492 |
|
---|
493 | fStat[pos+(spos+i)%1024].first += w1*l;
|
---|
494 | fStat[pos+(spos+i)%1024].second += w1;
|
---|
495 | }
|
---|
496 |
|
---|
497 | p_prev = p;
|
---|
498 | i_prev = i;
|
---|
499 | }
|
---|
500 | }
|
---|
501 | fNumEntries++;
|
---|
502 | }
|
---|
503 |
|
---|
504 | void FillEmptyBins()
|
---|
505 | {
|
---|
506 | for (int ch=0; ch<160; ch++)
|
---|
507 | {
|
---|
508 | const auto beg = fStat.begin() + ch*1024;
|
---|
509 | const auto end = beg + 1024;
|
---|
510 |
|
---|
511 | double avg = 0;
|
---|
512 | uint32_t num = 0;
|
---|
513 | for (auto it=beg; it!=end; it++)
|
---|
514 | {
|
---|
515 | if (it->second<fNumEntries-0.5)
|
---|
516 | continue;
|
---|
517 |
|
---|
518 | avg += it->first / it->second;
|
---|
519 | num++;
|
---|
520 | }
|
---|
521 | avg /= num;
|
---|
522 |
|
---|
523 | for (auto it=beg; it!=end; it++)
|
---|
524 | {
|
---|
525 | if (it->second>=fNumEntries-0.5)
|
---|
526 | continue;
|
---|
527 |
|
---|
528 | // {
|
---|
529 | // result[i+1].first = *is2;
|
---|
530 | // result[i+1].second = *iw2;
|
---|
531 | // }
|
---|
532 | // else
|
---|
533 | // {
|
---|
534 | it->first = avg*fNumEntries;
|
---|
535 | it->second = fNumEntries;
|
---|
536 | // }
|
---|
537 | }
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | DrsCalibrateTime GetComplete() const
|
---|
542 | {
|
---|
543 | DrsCalibrateTime rc(*this);
|
---|
544 | rc.FillEmptyBins();
|
---|
545 | return rc;
|
---|
546 | }
|
---|
547 |
|
---|
548 | void CalcResult()
|
---|
549 | {
|
---|
550 | for (int ch=0; ch<160; ch++)
|
---|
551 | {
|
---|
552 | const auto beg = fStat.begin() + ch*1024;
|
---|
553 | const auto end = beg + 1024;
|
---|
554 |
|
---|
555 | // Calculate mean
|
---|
556 | double s = 0;
|
---|
557 | double w = 0;
|
---|
558 |
|
---|
559 | for (auto it=beg; it!=end; it++)
|
---|
560 | {
|
---|
561 | s += it->first;
|
---|
562 | w += it->second;
|
---|
563 | }
|
---|
564 |
|
---|
565 | s /= w;
|
---|
566 |
|
---|
567 | double sumw = 0;
|
---|
568 | double sumv = 0;
|
---|
569 | int n = 0;
|
---|
570 |
|
---|
571 | // Sums about many values are numerically less stable than
|
---|
572 | // just sums over less. So we do the exercise from both sides.
|
---|
573 | for (auto it=beg; it!=end-512; it++, n++)
|
---|
574 | {
|
---|
575 | const double valv = it->first;
|
---|
576 | const double valw = it->second;
|
---|
577 |
|
---|
578 | it->first = sumv>0 ? n*(1-s*sumw/sumv) :0;
|
---|
579 |
|
---|
580 | sumv += valv;
|
---|
581 | sumw += valw;
|
---|
582 | }
|
---|
583 |
|
---|
584 | sumw = 0;
|
---|
585 | sumv = 0;
|
---|
586 | n = 1;
|
---|
587 |
|
---|
588 | for (auto it=end-1; it!=beg-1+512; it--, n++)
|
---|
589 | {
|
---|
590 | const double valv = it->first;
|
---|
591 | const double valw = it->second;
|
---|
592 |
|
---|
593 | sumv += valv;
|
---|
594 | sumw += valw;
|
---|
595 |
|
---|
596 | it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0;
|
---|
597 | }
|
---|
598 | }
|
---|
599 | }
|
---|
600 |
|
---|
601 | DrsCalibrateTime GetResult() const
|
---|
602 | {
|
---|
603 | DrsCalibrateTime rc(*this);
|
---|
604 | rc.CalcResult();
|
---|
605 | return rc;
|
---|
606 | }
|
---|
607 |
|
---|
608 | double Offset(uint32_t ch, double pos) const
|
---|
609 | {
|
---|
610 | const auto p = fStat.begin() + ch*1024;
|
---|
611 |
|
---|
612 | const uint32_t f = floor(pos);
|
---|
613 |
|
---|
614 | const double v0 = p[f].first;
|
---|
615 | const double v1 = p[(f+1)%1024].first;
|
---|
616 |
|
---|
617 | return v0 + fmod(pos, 1)*(v1-v0);
|
---|
618 | }
|
---|
619 |
|
---|
620 | double Calib(uint32_t ch, double pos) const
|
---|
621 | {
|
---|
622 | return pos-Offset(ch, pos);
|
---|
623 | }
|
---|
624 | };
|
---|
625 |
|
---|
626 | struct DrsCalibration
|
---|
627 | {
|
---|
628 | std::vector<int32_t> fOffset;
|
---|
629 | std::vector<int64_t> fGain;
|
---|
630 | std::vector<int64_t> fTrgOff;
|
---|
631 |
|
---|
632 | uint64_t fNumOffset;
|
---|
633 | uint64_t fNumGain;
|
---|
634 | uint64_t fNumTrgOff;
|
---|
635 |
|
---|
636 | uint32_t fStep;
|
---|
637 | uint16_t fRoi; // Region of interest for trgoff
|
---|
638 | uint16_t fNumTm; // Number of time marker channels in trgoff
|
---|
639 |
|
---|
640 | // uint16_t fDAC[8];
|
---|
641 |
|
---|
642 | DrsCalibration() :
|
---|
643 | fOffset (1440*1024, 0),
|
---|
644 | fGain (1440*1024, 4096),
|
---|
645 | fTrgOff (1600*1024, 0),
|
---|
646 | fNumOffset(1),
|
---|
647 | fNumGain(2000),
|
---|
648 | fNumTrgOff(1),
|
---|
649 | fStep(0)
|
---|
650 | {
|
---|
651 | }
|
---|
652 |
|
---|
653 | void Clear()
|
---|
654 | {
|
---|
655 | // Default gain:
|
---|
656 | // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
|
---|
657 | fOffset.assign(1440*1024, 0);
|
---|
658 | fGain.assign (1440*1024, 4096);
|
---|
659 | fTrgOff.assign(1600*1024, 0);
|
---|
660 |
|
---|
661 | fNumOffset = 1;
|
---|
662 | fNumGain = 2000;
|
---|
663 | fNumTrgOff = 1;
|
---|
664 |
|
---|
665 | fStep = 0;
|
---|
666 | }
|
---|
667 |
|
---|
668 | std::string ReadFitsImp(const std::string &str, std::vector<float> &vec)
|
---|
669 | {
|
---|
670 | std::fits file(str);
|
---|
671 | if (!file)
|
---|
672 | {
|
---|
673 | std::ostringstream msg;
|
---|
674 | msg << "Could not open file '" << str << "': " << strerror(errno);
|
---|
675 | return msg.str();
|
---|
676 | }
|
---|
677 |
|
---|
678 | if (file.GetStr("TELESCOP")!="FACT")
|
---|
679 | {
|
---|
680 | std::ostringstream msg;
|
---|
681 | msg << "Reading '" << str << "' failed: Not a valid FACT file (TELESCOP not FACT in header)";
|
---|
682 | return msg.str();
|
---|
683 | }
|
---|
684 |
|
---|
685 | if (!file.HasKey("STEP"))
|
---|
686 | {
|
---|
687 | std::ostringstream msg;
|
---|
688 | msg << "Reading '" << str << "' failed: Is not a DRS calib file (STEP not found in header)";
|
---|
689 | return msg.str();
|
---|
690 | }
|
---|
691 |
|
---|
692 | if (file.GetNumRows()!=1)
|
---|
693 | {
|
---|
694 | std::ostringstream msg;
|
---|
695 | msg << "Reading '" << str << "' failed: Number of rows in table is not 1.";
|
---|
696 | return msg.str();
|
---|
697 | }
|
---|
698 |
|
---|
699 | fStep = file.GetUInt("STEP");
|
---|
700 | fNumOffset = file.GetUInt("NBOFFSET");
|
---|
701 | fNumGain = file.GetUInt("NBGAIN");
|
---|
702 | fNumTrgOff = file.GetUInt("NBTRGOFF");
|
---|
703 | fRoi = file.GetUInt("NROI");
|
---|
704 | fNumTm = file.HasKey("NTM") ? file.GetUInt("NTM") : 0;
|
---|
705 | /*
|
---|
706 | fDAC[0] = file.GetUInt("DAC_A");
|
---|
707 | fDAC[1] = file.GetUInt("DAC_B");
|
---|
708 | fDAC[4] = file.GetUInt("DAC_C");
|
---|
709 | */
|
---|
710 | vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
|
---|
711 |
|
---|
712 | float *base = vec.data();
|
---|
713 |
|
---|
714 | reinterpret_cast<uint32_t*>(base)[0] = fRoi;
|
---|
715 |
|
---|
716 | file.SetPtrAddress("RunNumberBaseline", base+1, 1);
|
---|
717 | file.SetPtrAddress("RunNumberGain", base+2, 1);
|
---|
718 | file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1);
|
---|
719 | file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440);
|
---|
720 | file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440);
|
---|
721 | file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440);
|
---|
722 | file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440);
|
---|
723 | file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440);
|
---|
724 | file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+fRoi*1440, fRoi*1440);
|
---|
725 | if (fNumTm>0)
|
---|
726 | {
|
---|
727 | file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm);
|
---|
728 | file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
|
---|
729 | }
|
---|
730 |
|
---|
731 | if (!file.GetNextRow())
|
---|
732 | {
|
---|
733 | std::ostringstream msg;
|
---|
734 | msg << "Reading data from " << str << " failed.";
|
---|
735 | return msg.str();
|
---|
736 | }
|
---|
737 | /*
|
---|
738 | fDAC[2] = fDAC[1];
|
---|
739 | fDAC[4] = fDAC[1];
|
---|
740 |
|
---|
741 | fDAC[5] = fDAC[4];
|
---|
742 | fDAC[6] = fDAC[4];
|
---|
743 | fDAC[7] = fDAC[4];
|
---|
744 | */
|
---|
745 | fOffset.resize(1024*1440);
|
---|
746 | fGain.resize(1024*1440);
|
---|
747 |
|
---|
748 | fTrgOff.resize(fRoi*(1440+fNumTm));
|
---|
749 |
|
---|
750 | // Convert back to ADC counts: 256/125 = 4096/2000
|
---|
751 | // Convert back to sum (mean * num_entries)
|
---|
752 | for (int i=0; i<1024*1440; i++)
|
---|
753 | {
|
---|
754 | fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125;
|
---|
755 | fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
|
---|
756 | }
|
---|
757 |
|
---|
758 | for (int i=0; i<fRoi*1440; i++)
|
---|
759 | fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
|
---|
760 |
|
---|
761 | for (int i=0; i<fRoi*fNumTm; i++)
|
---|
762 | fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
|
---|
763 |
|
---|
764 |
|
---|
765 | // DAC: 0..2.5V == 0..65535
|
---|
766 | // V-mV: 1000
|
---|
767 | //fNumGain *= 2500*50000;
|
---|
768 | //for (int i=0; i<1024*1440; i++)
|
---|
769 | // fGain[i] *= 65536;
|
---|
770 | if (fStep==0)
|
---|
771 | {
|
---|
772 | for (int i=0; i<1024*1440; i++)
|
---|
773 | fGain[i] = fNumOffset*4096;
|
---|
774 | }
|
---|
775 | else
|
---|
776 | {
|
---|
777 | fNumGain *= 1953125;
|
---|
778 | for (int i=0; i<1024*1440; i++)
|
---|
779 | fGain[i] *= 1024;
|
---|
780 | }
|
---|
781 |
|
---|
782 | // Now mark the stored DRS data as "officially valid"
|
---|
783 | // However, this is not thread safe. It only ensures that
|
---|
784 | // this data is not used before it is completely and correctly
|
---|
785 | // read.
|
---|
786 | fStep++;
|
---|
787 |
|
---|
788 | return std::string();
|
---|
789 | }
|
---|
790 |
|
---|
791 | std::string ReadFitsImp(const std::string &str)
|
---|
792 | {
|
---|
793 | std::vector<float> vec;
|
---|
794 | return ReadFitsImp(str, vec);
|
---|
795 | }
|
---|
796 |
|
---|
797 | bool IsValid() { return fStep>2; }
|
---|
798 |
|
---|
799 | bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
|
---|
800 | {
|
---|
801 | if (roi!=fRoi)
|
---|
802 | {
|
---|
803 | for (size_t ch=0; ch<1440; ch++)
|
---|
804 | {
|
---|
805 | const size_t pos = ch*roi;
|
---|
806 | const size_t drs = ch*1024;
|
---|
807 |
|
---|
808 | DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
|
---|
809 | fOffset.data()+drs, fNumOffset,
|
---|
810 | fGain.data() +drs, fNumGain);
|
---|
811 | }
|
---|
812 |
|
---|
813 | return false;
|
---|
814 | }
|
---|
815 |
|
---|
816 | for (size_t ch=0; ch<1440; ch++)
|
---|
817 | {
|
---|
818 | const size_t pos = ch*fRoi;
|
---|
819 | const size_t drs = ch*1024;
|
---|
820 |
|
---|
821 | DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
|
---|
822 | fOffset.data()+drs, fNumOffset,
|
---|
823 | fGain.data() +drs, fNumGain,
|
---|
824 | fTrgOff.data()+pos, fNumTrgOff);
|
---|
825 | }
|
---|
826 |
|
---|
827 | for (size_t ch=0; ch<fNumTm; ch++)
|
---|
828 | {
|
---|
829 | const size_t pos = (ch+1440)*fRoi;
|
---|
830 | const size_t drs = (ch*9+8)*1024;
|
---|
831 |
|
---|
832 | DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
|
---|
833 | fOffset.data()+drs, fNumOffset,
|
---|
834 | fGain.data() +drs, fNumGain,
|
---|
835 | fTrgOff.data()+pos, fNumTrgOff);
|
---|
836 | }
|
---|
837 |
|
---|
838 | return true;
|
---|
839 | }
|
---|
840 | };
|
---|
841 |
|
---|
842 | #endif
|
---|