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