source: trunk/Mars/mcore/DrsCalib.h@ 17382

Last change on this file since 17382 was 17336, checked in by tbretz, 11 years ago
Added a new spike removal algorithm; changed the interface from 'all pixels' to #one pixel' to allow procesing a single pixel in debugging cases.
File size: 45.8 KB
Line 
1#ifndef MARS_DrsCalib
2#define MARS_DrsCalib
3
4#include <math.h> // fabs
5#include <errno.h> // errno
6
7#ifndef MARS_fits
8#include "fits.h"
9#endif
10
11#ifndef MARS_ofits
12#include "ofits.h"
13#endif
14
15#ifdef __MARS__
16#include "MTime.h"
17#endif
18
19class DrsCalibrate
20{
21protected:
22 uint64_t fNumEntries;
23
24 size_t fNumSamples;
25 size_t fNumChannels;
26
27 std::vector<int64_t> fSum;
28 std::vector<int64_t> fSum2;
29
30public:
31 DrsCalibrate() : fNumEntries(0), fNumSamples(0), fNumChannels(0)
32 {
33 fSum.reserve(1024*1440);
34 fSum2.reserve(1024*1440);
35 }
36
37 void Reset()
38 {
39 fNumEntries = 0;
40 fNumSamples = 0;
41 fNumChannels = 0;
42
43 fSum.clear();
44 fSum2.clear();
45 }
46
47 void InitSize(uint16_t channels, uint16_t samples)
48 {
49 fNumChannels = channels;
50 fNumSamples = samples;
51
52 fSum.assign(samples*channels, 0);
53 fSum2.assign(samples*channels, 0);
54 }
55
56 void AddRel(const int16_t *val, const int16_t *start)
57 {
58 /*
59 for (size_t ch=0; ch<fNumChannels; ch++)
60 {
61 const int16_t &spos = start[ch];
62 if (spos<0)
63 continue;
64
65 const size_t pos = ch*1024;
66 for (size_t i=0; i<1024; i++)
67 {
68 // Value is relative to trigger
69 // Abs is corresponding index relative to DRS pipeline
70 const size_t rel = pos + i;
71 const size_t abs = pos + (spos+i)%1024;
72
73 const int64_t v = val[rel];
74
75 fSum[abs] += v;
76 fSum2[abs] += v*v;
77 }
78 }*/
79
80 // This version is 2.5 times faster because the compilers optimization
81 // is not biased by the evaluation of %1024
82 for (size_t ch=0; ch<fNumChannels; ch++)
83 {
84 const int16_t &spos = start[ch];
85 if (spos<0)
86 continue;
87
88 const size_t pos = ch*1024;
89
90 const int16_t *beg_val = val + pos;
91 int64_t *beg_sum = fSum.data() + pos;
92 int64_t *beg_sum2 = fSum2.data() + pos;
93
94 const int16_t *pval = beg_val; // val[rel]
95 int64_t *psum = beg_sum + spos; // fSum[abs]
96 int64_t *psum2 = beg_sum2 + spos; // fSum2[abs]
97
98 while (psum<beg_sum+1024)
99 {
100 const int64_t v = *pval++;
101
102 *psum++ += v;
103 *psum2++ += v*v;
104 }
105
106 psum = beg_sum;
107 psum2 = beg_sum2;
108
109 while (pval<beg_val+1024)
110 {
111 const int64_t v = *pval++;
112
113 *psum++ += v;
114 *psum2++ += v*v;
115 }
116 }
117
118 fNumEntries++;
119 }
120
121 void AddRel(const int16_t *val, const int16_t *start,
122 const int32_t *offset, const uint32_t scale)
123 {
124 /*
125 for (size_t ch=0; ch<fNumChannels; ch++)
126 {
127 const int16_t spos = start[ch];
128 if (spos<0)
129 continue;
130
131 const size_t pos = ch*1024;
132
133 for (size_t i=0; i<fNumSamples; i++)
134 {
135 // Value is relative to trigger
136 // Offset is relative to DRS pipeline
137 // Abs is corresponding index relative to DRS pipeline
138 const size_t rel = pos + i;
139 const size_t abs = pos + (spos+i)%1024;
140
141 const int64_t v = int64_t(val[rel])*scale-offset[abs];
142
143 fSum[abs] += v;
144 fSum2[abs] += v*v;
145 }
146 }*/
147
148 // This version is 2.5 times faster because the compilers optimization
149 // is not biased by the evaluation of %1024
150 for (size_t ch=0; ch<fNumChannels; ch++)
151 {
152 const int16_t &spos = start[ch];
153 if (spos<0)
154 continue;
155
156 const size_t pos = ch*1024;
157
158 const int16_t *beg_val = val + pos;
159 const int32_t *beg_offset = offset + pos;
160 int64_t *beg_sum = fSum.data() + pos;
161 int64_t *beg_sum2 = fSum2.data() + pos;
162
163
164 const int16_t *pval = beg_val; // val[rel]
165 const int32_t *poffset = beg_offset + spos; // offset[abs]
166 int64_t *psum = beg_sum + spos; // fSum[abs]
167 int64_t *psum2 = beg_sum2 + spos; // fSum2[abs]
168
169 while (psum<beg_sum+1024)
170 {
171 const int64_t v = int64_t(*pval++)*scale - *poffset++;
172
173 *psum++ += v;
174 *psum2++ += v*v;
175 }
176
177 psum = beg_sum;
178 psum2 = beg_sum2;
179 poffset = beg_offset;
180
181 while (pval<beg_val+1024)
182 {
183 const int64_t v = int64_t(*pval++)*scale - *poffset++;
184
185 *psum++ += v;
186 *psum2++ += v*v;
187 }
188 }
189
190 fNumEntries++;
191 }
192 /*
193 void AddAbs(const int16_t *val, const int16_t *start,
194 const int32_t *offset, const uint32_t scale)
195 {
196 for (size_t ch=0; ch<fNumChannels; ch++)
197 {
198 const int16_t spos = start[ch];
199 if (spos<0)
200 continue;
201
202 const size_t pos = ch*fNumSamples;
203
204 for (size_t i=0; i<fNumSamples; i++)
205 {
206 // Value is relative to trigger
207 // Offset is relative to DRS pipeline
208 // Abs is corresponding index relative to DRS pipeline
209 const size_t rel = pos + i;
210 const size_t abs = pos + (spos+i)%1024;
211
212 const int64_t v = int64_t(val[rel])*scale-offset[abs];
213
214 fSum[rel] += v;
215 fSum2[rel] += v*v;
216 }
217 }
218
219 fNumEntries++;
220 }*/
221
222 void AddAbs(const int16_t *val, const int16_t *start,
223 const int32_t *offset, const uint32_t scale)
224 {
225 /*
226 // 1440 without tm, 1600 with tm
227 for (size_t ch=0; ch<fNumChannels; ch++)
228 {
229 const int16_t spos = start[ch];
230 if (spos<0)
231 continue;
232
233 const size_t pos = ch*fNumSamples;
234 const size_t drs = ch>1439 ? ((ch-1440)*9+8)*1024 : ch*1024;
235
236 for (size_t i=0; i<fNumSamples; i++)
237 {
238 // Value is relative to trigger
239 // Offset is relative to DRS pipeline
240 // Abs is corresponding index relative to DRS pipeline
241 const size_t rel = pos + i;
242 const size_t abs = drs + (spos+i)%1024;
243
244 const int64_t v = int64_t(val[rel])*scale-offset[abs];
245
246 fSum[rel] += v;
247 fSum2[rel] += v*v;
248 }
249 }*/
250
251 // This version is 1.5 times faster because the compilers optimization
252 // is not biased by the evaluation of %1024
253 for (size_t ch=0; ch<fNumChannels; ch++)
254 {
255 const int16_t &spos = start[ch];
256 if (spos<0)
257 continue;
258
259 const size_t pos = ch*fNumSamples;
260
261 const int32_t *beg_offset = offset + ch*1024;
262 const int16_t *beg_val = val + pos;
263 int64_t *beg_sum = fSum.data() + pos;
264 int64_t *beg_sum2 = fSum2.data() + pos;
265
266
267 const int16_t *pval = beg_val; // val[rel]
268 const int32_t *poffset = beg_offset + spos; // offset[abs]
269 int64_t *psum = beg_sum; // fSum[rel]
270 int64_t *psum2 = beg_sum2; // fSum2[rel]
271
272 if (spos+fNumSamples>1024)
273 {
274 while (poffset<beg_offset+1024)
275 {
276 const int64_t v = int64_t(*pval++)*scale - *poffset++;
277
278 *psum++ += v;
279 *psum2++ += v*v;
280 }
281
282 poffset = beg_offset;
283 }
284
285 while (psum<beg_sum+fNumSamples)
286 {
287 const int64_t v = int64_t(*pval++)*scale - *poffset++;
288
289 *psum++ += v;
290 *psum2++ += v*v;
291 }
292 }
293
294 fNumEntries++;
295 }
296
297
298 static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
299 const int32_t *offset, const uint32_t scaleabs,
300 const int64_t *gain, const uint64_t scalegain)
301 {
302 if (start<0)
303 {
304 memset(vec, 0, roi);
305 return;
306 }
307 /*
308 for (size_t i=0; i<roi; i++)
309 {
310 // Value is relative to trigger
311 // Offset is relative to DRS pipeline
312 // Abs is corresponding index relative to DRS pipeline
313 const size_t abs = (start+i)%1024;
314
315 const int64_t v =
316 + int64_t(val[i])*scaleabs-offset[abs]
317 ;
318
319 const int64_t div = gain[abs];
320 vec[i] = div==0 ? 0 : double(v)*scalegain/div;
321 }
322 */
323
324 // This version is faster because the compilers optimization
325 // is not biased by the evaluation of %1024
326 // (Here we are dominated by numerics... improvement ~10%)
327 const int32_t *poffset = offset + start; // offset[abs]
328 const int64_t *pgain = gain + start; // gain[abs]
329 const int16_t *pval = val; // val[rel]
330 float *pvec = vec; // vec[rel]
331
332 if (start+roi>1024)
333 {
334 while (poffset<offset+1024)
335 {
336 const int64_t v =
337 + int64_t(*pval++)*scaleabs - *poffset++
338 ;
339
340 *pvec++ = *pgain==0 ? 0 : double(v)*scalegain / *pgain;
341
342 pgain++;
343 }
344
345 poffset = offset;
346 pgain = gain;
347 }
348
349 while (pvec<vec+roi)
350 {
351 const int64_t v =
352 + int64_t(*pval++)*scaleabs - *poffset++
353 ;
354
355 *pvec++ = *pgain==0 ? 0 : double(v)*scalegain / *pgain;
356
357 pgain++;
358 }
359 }
360
361 static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
362 const int32_t *offset, const uint32_t scaleabs,
363 const int64_t *gain, const uint64_t scalegain,
364 const int64_t *trgoff, const uint64_t scalerel)
365 {
366 if (start<0)
367 {
368 memset(vec, 0, roi);
369 return;
370 }
371 /*
372 for (size_t i=0; i<roi; i++)
373 {
374 // Value is relative to trigger
375 // Offset is relative to DRS pipeline
376 // Abs is corresponding index relative to DRS pipeline
377 const size_t abs = (start+i)%1024;
378
379 const int64_t v =
380 + (int64_t(val[i])*scaleabs-offset[abs])*scalerel
381 - trgoff[i]
382 ;
383
384 const int64_t div = gain[abs]*scalerel;
385 vec[i] = div==0 ? 0 : double(v)*scalegain/div;
386 }
387 */
388 // (Here we are dominated by numerics... improvement ~10%)
389 const int32_t *poffset = offset + start; // offset[abs]
390 const int64_t *pgain = gain + start; // gain[abs]
391 const int16_t *pval = val; // val[rel]
392 const int64_t *ptrgoff = trgoff; // trgoff[rel]
393 float *pvec = vec; // vec[rel]
394
395 if (start+roi>1024)
396 {
397 while (poffset<offset+1024)
398 {
399 const int64_t v =
400 + (int64_t(*pval++)*scaleabs - *poffset++)*scalerel
401 - *ptrgoff++;
402 ;
403
404 const int64_t div = *pgain * scalerel;
405 *pvec++ = div==0 ? 0 : double(v)*scalegain / div;
406
407 pgain++;
408 }
409
410 poffset = offset;
411 pgain = gain;
412 }
413
414 while (pvec<vec+roi)
415 {
416 const int64_t v =
417 + (int64_t(*pval++)*scaleabs - *poffset++)*scalerel
418 - *ptrgoff++;
419 ;
420
421 const int64_t div = *pgain * scalerel;
422 *pvec++ = div==0 ? 0 : double(v)*scalegain / div;
423
424 pgain++;
425 }
426 }
427
428 static double FindStep(const size_t ch0, const float *vec, int16_t roi, const int16_t pos, const uint16_t *map=NULL)
429 {
430 // We have about 1% of all cases which are not ahndled here,
431 // because the baseline jumps up just before the readout window
432 // and down just after it. In this cases we could determine the jump
433 // from the board time difference or throw the event away.
434 if (pos==0 || pos>=roi)
435 return 0;
436
437 double step = 0; // before
438 double rms = 0; // before
439 int cnt = 0;
440
441 // Exclude TM channel
442 for (int p=0; p<8; p++)
443 {
444 const size_t hw = ch0+p;
445 const size_t sw = (map?map[hw]:hw)*roi + pos;
446
447 const double diff = vec[sw]-vec[sw-1];
448
449 step += diff;
450 rms += (vec[sw]-vec[sw-1])*(vec[sw]-vec[sw-1]);
451
452 cnt++;
453 }
454
455 return cnt==0 ? 0 : step/cnt;
456 }
457
458 static void SubtractStep(const size_t ch0, const double avg, float *vec, int16_t roi, int32_t pos, const uint16_t *map=NULL)
459 {
460 if (pos==0 || pos>=roi)
461 return;
462
463 const int begin = avg>0 ? pos : 0;
464 const int end = avg>0 ? roi : pos;
465
466 const double sub = fabs(avg);
467
468 for (int p=0; p<9; p++)
469 {
470 for (int j=begin; j<end; j++)
471 {
472 const size_t hw = ch0+p;
473 const size_t sw = (map?map[hw]:hw)*roi + j;
474
475 vec[sw] -= sub;
476 }
477 }
478 }
479
480 struct Step
481 {
482 Step() : avg(0), rms(0), pos(0), cnt(0) { }
483 double avg;
484 double rms;
485 double pos;
486 uint16_t cnt;
487
488 static bool sort(const Step &s, const Step &r) { return s.avg<r.avg; }
489 };
490
491 static Step AverageSteps(const std::vector<Step>::iterator beg, const std::vector<Step>::iterator end)
492 {
493 Step rc;
494 for (auto it=beg; it!=end; it++)
495 {
496 rc.pos += it->pos;
497 rc.avg += it->avg;
498 rc.rms += it->avg*it->avg;
499 }
500
501 rc.cnt = end-beg;
502
503 rc.pos /= rc.cnt;
504 rc.avg /= rc.cnt;
505 rc.rms /= rc.cnt;
506 rc.rms -= rc.avg*rc.avg;
507 rc.rms = rc.rms<0 ? 0 : sqrt(rc.rms);
508
509 return rc;
510 }
511
512
513 static Step CorrectStep(float *vec, uint16_t nch, uint16_t roi,
514 const int16_t *prev, const int16_t *start,
515 const int16_t offset, const uint16_t *map=NULL)
516 {
517
518 std::vector<Step> list;
519 list.reserve(nch);
520
521 // Fill steps into array
522 // Exclude broken pixels?
523 // Remove maximum and minimum patches (4max and 4min)?
524 for (size_t ch=0; ch<nch; ch += 9)
525 {
526 if (prev[ch]<0 || start[ch]<0)
527 continue;
528
529 const int16_t dist = (prev[ch]-start[ch]+1024+offset)%1024;
530 const double step = FindStep(ch, vec, roi, dist, map);
531 if (step==0)
532 continue;
533
534 Step rc;
535 rc.pos = dist;
536 rc.avg = step;
537 list.push_back(rc);
538 }
539
540 if (list.empty())
541 return Step();
542
543 Step rc = AverageSteps(list.begin(), list.begin()+list.size());;
544
545 if (rc.avg==0)
546 return Step();
547
548 // std::cout << " A0=" << rc.avg << " rms=" << rc.rms << std::endl;
549 if (rc.rms>5)
550 {
551 sort(list.begin(), list.end(), Step::sort);
552
553 //for (auto it=list.begin(); it!=list.end(); it++)
554 // std::cout << " " << it->avg << std::endl;
555
556 const size_t skip = list.size()/10;
557 rc = AverageSteps(list.begin()+skip, list.begin()+list.size()-skip);
558
559 // std::cout << " A1=" << rc.avg << " rms=" << rc.rms << std::endl;
560 }
561
562 for (size_t ch=0; ch<nch; ch += 9)
563 {
564 const int16_t dist = (prev[ch]-start[ch]+1024+offset)%1024;
565 SubtractStep(ch, rc.avg, vec, roi, dist, map);
566 }
567
568 return rc;
569 }
570
571 static void RemoveSpikes(float *p, uint32_t roi)
572 {
573 if (roi<4)
574 return;
575
576 for (size_t i=1; i<roi-2; i++)
577 {
578 if (p[i]-p[i-1]>25 && p[i]-p[i+1]>25)
579 {
580 p[i] = (p[i-1]+p[i+1])/2;
581 }
582
583 if (p[i]-p[i-1]>22 && fabs(p[i]-p[i+1])<4 && p[i+1]-p[i+2]>22)
584 {
585 p[i] = (p[i-1]+p[i+2])/2;
586 p[i+1] = p[i];
587 }
588 }
589 }
590
591 static void RemoveSpikes2(float *p, uint32_t roi)//from Werner
592 {
593 if (roi<4)
594 return;
595
596 std::vector<float> Ameas(p, p+roi);
597
598 std::vector<float> diff(roi);
599 for (size_t i=1; i<roi-1; i++)
600 diff[i] = (p[i-1] + p[i+1])/2 - p[i];
601
602 //std::vector<float> N1mean(roi);
603 //for (size_t i=1; i<roi-1; i++)
604 // N1mean[i] = (p[i-1] + p[i+1])/2;
605
606 const float fract = 0.8;
607
608 for (size_t i=0; i<roi-3; i++)
609 {
610 if (diff[i]<5)
611 continue;
612
613 if (Ameas[i+2] - (Ameas[i] + Ameas[i+3])/2 > 10)
614 {
615 p[i+1]= (Ameas[i+3] - Ameas[i])/3 + Ameas[i];
616 p[i+2]= 2*(Ameas[i+3] - Ameas[i])/3 + Ameas[i];
617
618 i += 3;
619
620 continue;
621 }
622
623 if ( (diff[i+1]<-diff[i]*fract*2) && (diff[i+2]>10) )
624 {
625 p[i+1] = (Ameas[i]+Ameas[i+2])/2;
626 diff[i+2] = (p[i+1] + Ameas[i+3])/2 - Ameas[i+2];
627
628 i += 2;
629 }
630
631 // const float x = Ameas[i] - N1mean[i];
632 // if (x > -5.)
633 // continue;
634
635 // if (Ameas[i+2] - (Ameas[i] + Ameas[i+3])/2. > 10.)
636 // {
637 // p[i+1]= (Ameas[i+3] - Ameas[i])/3 + Ameas[i];
638 // p[i+2]= 2*(Ameas[i+3] - Ameas[i])/3 + Ameas[i];
639 // i += 3;
640 // continue;
641 // }
642
643 // const float xp = Ameas[i+1] - N1mean[i+1];
644 // const float xpp = Ameas[i+2] - N1mean[i+2];
645
646 // if ( (xp > -2.*x*fract) && (xpp < -10.) )
647 // {
648 // p[i+1] = N1mean[i+1];
649 // N1mean[i+2] = Ameas[i+1] - Ameas[i+3]/2;
650 //
651 // i += 2;
652 // }
653 }
654 }
655
656 static void RemoveSpikes3(float *vec, uint32_t roi)//from Werner
657 {
658 if (roi<4)
659 return;
660
661 const float SingleCandidateTHR = -10.;
662 const float DoubleCandidateTHR = -5.;
663
664 const std::vector<float> src(vec, vec+roi);
665
666 std::vector<float> diff(roi);
667 for (size_t i=1; i<roi-1; i++)
668 diff[i] = src[i] - (src[i-1] + src[i+1])/2;
669
670 // find the spike and replace it by mean value of neighbors
671 for (unsigned int i=1; i<roi-3; i++)
672 {
673 // Speed up (no leading edge)
674 if (diff[i]>=DoubleCandidateTHR)
675 continue;
676
677 //bool checkDouble = false;
678
679 // a single spike candidate
680 if (diff[i]<SingleCandidateTHR)
681 {
682 // check consistency with a single channel spike
683 if (diff[i+1] > -1.6*diff[i])
684 {
685 vec[i+1] = (src[i] + src[i+2]) / 2;
686
687 i += 2;
688
689 /*** NEW ***/
690 continue;
691 /*** NEW ***/
692 }
693 /*
694 else
695 {
696 // do nothing - not really a single spike,
697 // but check if it is a double
698 checkDouble = true;
699 }*/
700 }
701
702 // a double spike candidate
703 //if (diff[i]>DoubleCandidateTHR || checkDouble == 1)
704 {
705 // check the consistency with a double spike
706 if ((diff[i+1] > -DoubleCandidateTHR) &&
707 (diff[i+2] > -DoubleCandidateTHR))
708 {
709 vec[i+1] = (src[i+3] - src[i])/3 + src[i];
710 vec[i+2] = 2*(src[i+3] - src[i])/3 + src[i];
711
712 //vec[i] = (src[i-1] + src[i+2]) / 2.;
713 //vec[i+1] = (src[i-1] + src[i+2]) / 2.;
714
715 //do not care about the next sample it was the spike
716 i += 3;
717 }
718 }
719 }
720 }
721
722 static void RemoveSpikes4(float *ptr, uint32_t roi)
723 {
724 if (roi<7)
725 return;
726
727 for (uint32_t i=0; i<roi-6; i++)
728 {
729 double d10, d21, d32, d43, d54;
730
731 // ============================================
732 d43 = ptr[i+4]-ptr[i+3];
733 d54 = ptr[i+5]-ptr[i+4];
734
735 if ((d43>35 && -d54>35) || (d43<-35 && -d54<-35))
736 {
737 ptr[i+4] = (ptr[i+3]+ptr[i+5])/2;
738 }
739
740 // ============================================
741 d32 = ptr[i+3]-ptr[i+2];
742 d54 = ptr[i+5]-ptr[i+4];
743
744 if ((d32>9 && -d54>13 && d32-d54>31)/* || (d32<-13 && -d54<-13 && d32+d54<-63)*/)
745 {
746 double avg0 = (ptr[i+2]+ptr[i+5])/2;
747 double avg1 = (ptr[i+3]+ptr[i+4])/2;
748
749 ptr[i+3] = ptr[i+3] - avg1+avg0;
750 ptr[i+4] = ptr[i+4] - avg1+avg0;
751 }
752
753 // ============================================
754 d21 = ptr[i+2]-ptr[i+1];
755 d54 = ptr[i+5]-ptr[i+4];
756
757 if (d21>15 && -d54>17)
758 {
759 double avg0 = (ptr[i+1]+ptr[i+5])/2;
760 double avg1 = (ptr[i+2]+ptr[i+3]+ptr[i+4])/3;
761
762 ptr[i+2] = ptr[i+2] - avg1+avg0;
763 ptr[i+3] = ptr[i+3] - avg1+avg0;
764 ptr[i+4] = ptr[i+4] - avg1+avg0;
765 }
766
767 // ============================================
768 d10 = ptr[i+1]-ptr[i];
769 d54 = ptr[i+5]-ptr[i+4];
770
771 if (d10>18 && -d54>20)
772 {
773 double avg0 = (ptr[i]+ptr[i+5])/2;
774 double avg1 = (ptr[i+1]+ptr[i+2]+ptr[i+3]+ptr[i+4])/4;
775
776 ptr[i+1] = ptr[i+1] - avg1+avg0;
777 ptr[i+2] = ptr[i+2] - avg1+avg0;
778 ptr[i+3] = ptr[i+3] - avg1+avg0;
779 ptr[i+4] = ptr[i+4] - avg1+avg0;
780 }
781 }
782 }
783
784 static void SlidingAverage(float *const vec, const uint32_t roi, const uint16_t w)
785 {
786 if (w==0 || w>roi)
787 return;
788
789 for (float *pix=vec; pix<vec+1440*roi; pix += roi)
790 {
791 for (float *ptr=pix; ptr<pix+roi-w; ptr++)
792 {
793 for (float *p=ptr+1; p<ptr+w; p++)
794 *ptr += *p;
795 *ptr /= w;
796 }
797 }
798 }
799
800 std::pair<std::vector<double>,std::vector<double> > GetSampleStats() const
801 {
802 if (fNumEntries==0)
803 return make_pair(std::vector<double>(),std::vector<double>());
804
805 std::vector<double> mean(fSum.size());
806 std::vector<double> error(fSum.size());
807
808 std::vector<int64_t>::const_iterator it = fSum.begin();
809 std::vector<int64_t>::const_iterator i2 = fSum2.begin();
810 std::vector<double>::iterator im = mean.begin();
811 std::vector<double>::iterator ie = error.begin();
812
813 while (it!=fSum.end())
814 {
815 *im = /*cnt<fResult.size() ? fResult[cnt] :*/ double(*it)/fNumEntries;
816 *ie = sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
817
818 im++;
819 ie++;
820 it++;
821 i2++;
822 }
823
824
825 /*
826 valarray<double> ...
827
828 mean /= fNumEntries;
829 error = sqrt(error/fNumEntries - mean*mean);
830 */
831
832 return make_pair(mean, error);
833 }
834
835 void GetSampleStats(float *ptr, float scale) const
836 {
837 const size_t sz = fNumSamples*fNumChannels;
838
839 if (fNumEntries==0)
840 {
841 memset(ptr, 0, sizeof(float)*sz*2);
842 return;
843 }
844
845 std::vector<int64_t>::const_iterator it = fSum.begin();
846 std::vector<int64_t>::const_iterator i2 = fSum2.begin();
847
848 while (it!=fSum.end())
849 {
850 *ptr = scale*double(*it)/fNumEntries;
851 *(ptr+sz) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
852
853 ptr++;
854 it++;
855 i2++;
856 }
857 }
858
859 static double GetPixelStats(float *ptr, const float *data, uint16_t roi)
860 {
861 if (roi==0)
862 return -1;
863
864 // Skip first 10 samples
865 const uint beg = roi>10 ? 10 : 0;
866 const uint end = roi-beg>5 ? roi-5 : roi;
867 const uint len = end-beg;
868
869 double max = 0;
870 double patch = 0;
871 for (uint i=0; i<1440; i++)
872 {
873 const float *vec = data+i*roi;
874
875 uint pos = beg;
876 double sum = vec[beg];
877 double sum2 = vec[beg]*vec[beg];
878
879 for (uint j=beg+1; j<end; j++)
880 {
881 sum += vec[j];
882 sum2 += vec[j]*vec[j];
883
884 if (vec[j]>vec[pos])
885 pos = j;
886 }
887 sum /= len;
888 sum2 /= len;
889
890 if (i%9!=8 && vec[pos]>0)
891 patch += vec[pos];
892
893 if (i%9==8)
894 {
895 patch /= 8;
896 if (patch > max)
897 max = patch;
898 patch = 0;
899 }
900
901 //if (i%9!=8 && vec[pos]>max)
902 // max = vec[pos];
903
904 *(ptr+0*1440+i) = sum;
905 *(ptr+1*1440+i) = sqrt(sum2 - sum * sum);
906 *(ptr+2*1440+i) = vec[pos];
907 *(ptr+3*1440+i) = pos;
908 }
909
910 return max;
911 }
912
913 static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last)
914 {
915 if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last<first)
916 return;
917
918 for (int i=0; i<1440; i++)
919 {
920 const float *beg = data+i*roi+first;
921 const float *end = data+i*roi+last;
922
923 const float *pmax = beg;
924
925 for (const float *ptr=beg+1; ptr<=end; ptr++)
926 if (*ptr>*pmax)
927 pmax = ptr;
928
929 max[i] = *pmax;
930 }
931 }
932
933 const std::vector<int64_t> &GetSum() const { return fSum; }
934
935 uint64_t GetNumEntries() const { return fNumEntries; }
936};
937
938class DrsCalibrateTime
939{
940public:
941 uint64_t fNumEntries;
942
943 size_t fNumSamples;
944 size_t fNumChannels;
945
946 std::vector<std::pair<double, double>> fStat;
947
948public:
949 DrsCalibrateTime() : fNumEntries(0), fNumSamples(0), fNumChannels(0)
950 {
951 InitSize(160, 1024);
952 }
953
954 DrsCalibrateTime(const DrsCalibrateTime &p) : fNumEntries(p.fNumEntries), fNumSamples(p.fNumSamples), fNumChannels(p.fNumChannels), fStat(p.fStat)
955 {
956 }
957 virtual ~DrsCalibrateTime()
958 {
959 }
960
961 double Sum(uint32_t i) const { return fStat[i].first; }
962 double W(uint32_t i) const { return fStat[i].second; }
963
964 virtual void InitSize(uint16_t channels, uint16_t samples)
965 {
966 fNumChannels = channels;
967 fNumSamples = samples;
968
969 fNumEntries = 0;
970
971 fStat.clear();
972
973 fStat.resize(samples*channels);
974 }
975
976 void AddT(const float *val, const int16_t *start, signed char edge=0)
977 {
978 if (fNumSamples!=1024 || fNumChannels!=160)
979 return;
980
981 // Rising or falling edge detection has the advantage that
982 // we are much less sensitive to baseline shifts
983
984 for (size_t ch=0; ch<160; ch++)
985 {
986 const size_t tm = ch*9+8;
987
988 const int16_t spos = start[tm];
989 if (spos<0)
990 continue;
991
992 const size_t pos = ch*1024;
993
994 double p_prev = 0;
995 int32_t i_prev = -1;
996
997 for (size_t i=0; i<1024-1; i++)
998 {
999 const size_t rel = tm*1024 + i;
1000
1001 const float &v0 = val[rel]; //-avg;
1002 const float &v1 = val[rel+1];//-avg;
1003
1004 // If edge is positive ignore all falling edges
1005 if (edge>0 && v0>0)
1006 continue;
1007
1008 // If edge is negative ignore all falling edges
1009 if (edge<0 && v0<0)
1010 continue;
1011
1012 // Check if there is a zero crossing
1013 if ((v0<0 && v1<0) || (v0>0 && v1>0))
1014 continue;
1015
1016 // Calculate the position p of the zero-crossing
1017 // within the interval [rel, rel+1] relative to rel
1018 // by linear interpolation.
1019 const double p = v0==v1 ? 0.5 : v0/(v0-v1);
1020
1021 // If this was at least the second zero-crossing detected
1022 if (i_prev>=0)
1023 {
1024 // Calculate the distance l between the
1025 // current and the last zero-crossing
1026 const double l = i+p - (i_prev+p_prev);
1027
1028 // By summation, the average length of each
1029 // cell is calculated. For the first and last
1030 // fraction of a cell, the fraction is applied
1031 // as a weight.
1032 const double w0 = 1-p_prev;
1033 fStat[pos+(spos+i_prev)%1024].first += w0*l;
1034 fStat[pos+(spos+i_prev)%1024].second += w0;
1035
1036 for (size_t k=i_prev+1; k<i; k++)
1037 {
1038 fStat[pos+(spos+k)%1024].first += l;
1039 fStat[pos+(spos+k)%1024].second += 1;
1040 }
1041
1042 const double w1 = p;
1043 fStat[pos+(spos+i)%1024].first += w1*l;
1044 fStat[pos+(spos+i)%1024].second += w1;
1045 }
1046
1047 // Remember this zero-crossing position
1048 p_prev = p;
1049 i_prev = i;
1050 }
1051 }
1052 fNumEntries++;
1053 }
1054
1055 void FillEmptyBins()
1056 {
1057 for (int ch=0; ch<160; ch++)
1058 {
1059 const auto beg = fStat.begin() + ch*1024;
1060 const auto end = beg + 1024;
1061
1062 double avg = 0;
1063 uint32_t num = 0;
1064 for (auto it=beg; it!=end; it++)
1065 {
1066 if (it->second<fNumEntries-0.5)
1067 continue;
1068
1069 avg += it->first / it->second;
1070 num++;
1071 }
1072 avg /= num;
1073
1074 for (auto it=beg; it!=end; it++)
1075 {
1076 if (it->second>=fNumEntries-0.5)
1077 continue;
1078
1079 // {
1080 // result[i+1].first = *is2;
1081 // result[i+1].second = *iw2;
1082 // }
1083 // else
1084 // {
1085 it->first = avg*fNumEntries;
1086 it->second = fNumEntries;
1087 // }
1088 }
1089 }
1090 }
1091
1092 DrsCalibrateTime GetComplete() const
1093 {
1094 DrsCalibrateTime rc(*this);
1095 rc.FillEmptyBins();
1096 return rc;
1097 }
1098
1099 void CalcResult()
1100 {
1101 for (int ch=0; ch<160; ch++)
1102 {
1103 const auto beg = fStat.begin() + ch*1024;
1104 const auto end = beg + 1024;
1105
1106 // First calculate the average length s of a single
1107 // zero-crossing interval in the whole range [0;1023]
1108 // (which is identical to the/ wavelength of the
1109 // calibration signal)
1110 double s = 0;
1111 double w = 0;
1112 for (auto it=beg; it!=end; it++)
1113 {
1114 s += it->first;
1115 w += it->second;
1116 }
1117 s /= w;
1118
1119 // Dividing the average length s of the zero-crossing
1120 // interval in the range [0;1023] by the average length
1121 // in the interval [0;n] yields the relative size of
1122 // the interval in the range [0;n].
1123 //
1124 // Example:
1125 // Average [0;1023]: 10.00 (global interval size in samples)
1126 // Average [0;512]: 8.00 (local interval size in samples)
1127 //
1128 // Globally, on average one interval is sampled by 10 samples.
1129 // In the sub-range [0;512] one interval is sampled on average
1130 // by 8 samples.
1131 // That means that the interval contains 64 periods, while
1132 // in the ideal case (each sample has the same length), it
1133 // should contain 51.2 periods.
1134 // So, the sampling position 512 corresponds to a time 640,
1135 // while in the ideal case with equally spaces samples,
1136 // it would correspond to a time 512.
1137 //
1138 // The offset (defined as 'ideal - real') is then calculated
1139 // as 512*(1-10/8) = -128, so that the time is calculated as
1140 // 'sampling position minus offset'
1141 //
1142 double sumw = 0;
1143 double sumv = 0;
1144 int n = 0;
1145
1146 // Sums about many values are numerically less stable than
1147 // just sums over less. So we do the exercise from both sides.
1148 // First from the left
1149 for (auto it=beg; it!=end-512; it++, n++)
1150 {
1151 const double valv = it->first;
1152 const double valw = it->second;
1153
1154 it->first = sumv>0 ? n*(1-s*sumw/sumv) : 0;
1155
1156 sumv += valv;
1157 sumw += valw;
1158 }
1159
1160 sumw = 0;
1161 sumv = 0;
1162 n = 1;
1163
1164 // Second from the right
1165 for (auto it=end-1; it!=beg-1+512; it--, n++)
1166 {
1167 const double valv = it->first;
1168 const double valw = it->second;
1169
1170 sumv += valv;
1171 sumw += valw;
1172
1173 it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0;
1174 }
1175
1176 // A crosscheck has shown, that the values from the left
1177 // and right perfectly agree over the whole range. This means
1178 // the a calculation from just one side would be enough, but
1179 // doing it from both sides might still make the numerics
1180 // a bit more stable.
1181 }
1182 }
1183
1184 DrsCalibrateTime GetResult() const
1185 {
1186 DrsCalibrateTime rc(*this);
1187 rc.CalcResult();
1188 return rc;
1189 }
1190
1191 double Offset(uint32_t ch, double pos) const
1192 {
1193 const auto p = fStat.begin() + ch*1024;
1194
1195 const uint32_t f = floor(pos);
1196
1197 const double v0 = p[f].first;
1198 const double v1 = p[(f+1)%1024].first;
1199
1200 return v0 + fmod(pos, 1)*(v1-v0);
1201 }
1202
1203 double Calib(uint32_t ch, double pos) const
1204 {
1205 return pos-Offset(ch, pos);
1206 }
1207};
1208
1209struct DrsCalibration
1210{
1211 std::vector<int32_t> fOffset;
1212 std::vector<int64_t> fGain;
1213 std::vector<int64_t> fTrgOff;
1214
1215 uint64_t fNumOffset;
1216 uint64_t fNumGain;
1217 uint64_t fNumTrgOff;
1218
1219 uint32_t fStep;
1220 uint16_t fRoi; // Region of interest for trgoff
1221 uint16_t fNumTm; // Number of time marker channels in trgoff
1222
1223 std::string fDateObs;
1224 std::string fDateRunBeg[3];
1225 std::string fDateRunEnd[3];
1226 std::string fDateEnd;
1227
1228// uint16_t fDAC[8];
1229
1230 DrsCalibration() :
1231 fOffset(1440*1024, 0),
1232 fGain(1440*1024, 4096),
1233 fTrgOff (1600*1024, 0),
1234 fNumOffset(1),
1235 fNumGain(2000),
1236 fNumTrgOff(1),
1237 fStep(0),
1238 fDateObs("1970-01-01T00:00:00"),
1239 fDateEnd("1970-01-01T00:00:00")
1240 {
1241 for (int i=0; i<3; i++)
1242 {
1243 fDateRunBeg[i] = "1970-01-01T00:00:00";
1244 fDateRunEnd[i] = "1970-01-01T00:00:00";
1245 }
1246 }
1247
1248 DrsCalibration(const DrsCalibration &cpy) :
1249 fOffset(cpy.fOffset),
1250 fGain(cpy.fGain),
1251 fTrgOff(cpy.fTrgOff),
1252 fNumOffset(cpy.fNumOffset),
1253 fNumGain(cpy.fNumGain),
1254 fNumTrgOff(cpy.fNumTrgOff),
1255 fStep(cpy.fStep),
1256 fRoi(cpy.fRoi),
1257 fNumTm(cpy.fNumTm),
1258 fDateObs(cpy.fDateObs),
1259 fDateRunBeg(cpy.fDateRunBeg),
1260 fDateRunEnd(cpy.fDateRunEnd),
1261 fDateEnd(cpy.fDateEnd)
1262 {
1263 }
1264
1265 void Clear()
1266 {
1267 // Default gain:
1268 // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
1269 fOffset.assign(1440*1024, 0);
1270 fGain.assign (1440*1024, 4096);
1271 fTrgOff.assign(1600*1024, 0);
1272
1273 fNumOffset = 1;
1274 fNumGain = 2000;
1275 fNumTrgOff = 1;
1276
1277 fStep = 0;
1278
1279 fDateObs = "1970-01-01T00:00:00";
1280 fDateEnd = "1970-01-01T00:00:00";
1281
1282 for (int i=0; i<3; i++)
1283 {
1284 fDateRunBeg[i] = "1970-01-01T00:00:00";
1285 fDateRunEnd[i] = "1970-01-01T00:00:00";
1286 }
1287 }
1288
1289 std::string ReadFitsImp(const std::string &str, std::vector<float> &vec)
1290 {
1291 fits file(str);
1292 if (!file)
1293 {
1294 std::ostringstream msg;
1295 msg << "Could not open file '" << str << "': " << strerror(errno);
1296 return msg.str();
1297 }
1298
1299 if (file.GetStr("TELESCOP")!="FACT")
1300 {
1301 std::ostringstream msg;
1302 msg << "Reading '" << str << "' failed: Not a valid FACT file (TELESCOP not FACT in header)";
1303 return msg.str();
1304 }
1305
1306 if (!file.HasKey("STEP"))
1307 {
1308 std::ostringstream msg;
1309 msg << "Reading '" << str << "' failed: Is not a DRS calib file (STEP not found in header)";
1310 return msg.str();
1311 }
1312
1313 if (file.GetNumRows()!=1)
1314 {
1315 std::ostringstream msg;
1316 msg << "Reading '" << str << "' failed: Number of rows in table is not 1.";
1317 return msg.str();
1318 }
1319
1320 fStep = file.GetUInt("STEP");
1321 fNumOffset = file.GetUInt("NBOFFSET");
1322 fNumGain = file.GetUInt("NBGAIN");
1323 fNumTrgOff = file.GetUInt("NBTRGOFF");
1324 fRoi = file.GetUInt("NROI");
1325 fNumTm = file.HasKey("NTM") ? file.GetUInt("NTM") : 0;
1326
1327 if (file.HasKey("DATE-OBS"))
1328 fDateObs = file.GetStr("DATE-OBS");
1329 if (file.HasKey("DATE-END"))
1330 fDateEnd = file.GetStr("DATE-END");
1331
1332 if (file.HasKey("RUN0-BEG"))
1333 fDateRunBeg[0]= file.GetStr("RUN0-BEG");
1334 if (file.HasKey("RUN1-BEG"))
1335 fDateRunBeg[1]= file.GetStr("RUN1-BEG");
1336 if (file.HasKey("RUN2-BEG"))
1337 fDateRunBeg[2]= file.GetStr("RUN2-BEG");
1338 if (file.HasKey("RUN0-END"))
1339 fDateRunEnd[0]= file.GetStr("RUN0-END");
1340 if (file.HasKey("RUN1-END"))
1341 fDateRunEnd[1]= file.GetStr("RUN1-END");
1342 if (file.HasKey("RUN2-END"))
1343 fDateRunEnd[2]= file.GetStr("RUN2-END");
1344/*
1345 fDAC[0] = file.GetUInt("DAC_A");
1346 fDAC[1] = file.GetUInt("DAC_B");
1347 fDAC[4] = file.GetUInt("DAC_C");
1348*/
1349 vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
1350
1351 float *base = vec.data();
1352
1353 reinterpret_cast<uint32_t*>(base)[0] = fRoi;
1354
1355 file.SetPtrAddress("RunNumberBaseline", base+1, 1);
1356 file.SetPtrAddress("RunNumberGain", base+2, 1);
1357 file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1);
1358 file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440);
1359 file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440);
1360 file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440);
1361 file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440);
1362 file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440);
1363 file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+fRoi*1440, fRoi*1440);
1364 if (fNumTm>0)
1365 {
1366 file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm);
1367 file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
1368 }
1369
1370 if (!file.GetNextRow())
1371 {
1372 std::ostringstream msg;
1373 msg << "Reading data from " << str << " failed.";
1374 return msg.str();
1375 }
1376/*
1377 fDAC[2] = fDAC[1];
1378 fDAC[4] = fDAC[1];
1379
1380 fDAC[5] = fDAC[4];
1381 fDAC[6] = fDAC[4];
1382 fDAC[7] = fDAC[4];
1383*/
1384 fOffset.resize(1024*1440);
1385 fGain.resize(1024*1440);
1386
1387 fTrgOff.resize(fRoi*(1440+fNumTm));
1388
1389 // Convert back to ADC counts: 256/125 = 4096/2000
1390 // Convert back to sum (mean * num_entries)
1391 for (int i=0; i<1024*1440; i++)
1392 {
1393 fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125;
1394 fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
1395 }
1396
1397 for (int i=0; i<fRoi*1440; i++)
1398 fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
1399
1400 for (int i=0; i<fRoi*fNumTm; i++)
1401 fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
1402
1403
1404 // DAC: 0..2.5V == 0..65535
1405 // V-mV: 1000
1406 //fNumGain *= 2500*50000;
1407 //for (int i=0; i<1024*1440; i++)
1408 // fGain[i] *= 65536;
1409 if (fStep==0)
1410 {
1411 for (int i=0; i<1024*1440; i++)
1412 fGain[i] = fNumOffset*4096;
1413 }
1414 else
1415 {
1416 fNumGain *= 1953125;
1417 for (int i=0; i<1024*1440; i++)
1418 fGain[i] *= 1024;
1419 }
1420
1421 // Now mark the stored DRS data as "officially valid"
1422 // However, this is not thread safe. It only ensures that
1423 // this data is not used before it is completely and correctly
1424 // read.
1425 fStep++;
1426
1427 return std::string();
1428 }
1429
1430 std::string WriteFitsImp(const std::string &filename, const std::vector<float> &vec, uint32_t night=0) const
1431 {
1432 const size_t n = 1440*1024*4 + 1440*fRoi*2 + fNumTm*fRoi*2 + 3;
1433
1434 ofits file(filename.c_str());
1435 if (!file)
1436 {
1437 std::ostringstream msg;
1438 msg << "Could not open file '" << filename << "': " << strerror(errno);
1439 return msg.str();
1440 }
1441
1442 file.AddColumnInt("RunNumberBaseline");
1443 file.AddColumnInt("RunNumberGain");
1444 file.AddColumnInt("RunNumberTriggerOffset");
1445
1446 file.AddColumnFloat(1024*1440, "BaselineMean", "mV");
1447 file.AddColumnFloat(1024*1440, "BaselineRms", "mV");
1448 file.AddColumnFloat(1024*1440, "GainMean", "mV");
1449 file.AddColumnFloat(1024*1440, "GainRms", "mV");
1450 file.AddColumnFloat(fRoi*1440, "TriggerOffsetMean", "mV");
1451 file.AddColumnFloat(fRoi*1440, "TriggerOffsetRms", "mV");
1452 file.AddColumnFloat(fRoi*fNumTm, "TriggerOffsetTMMean", "mV");
1453 file.AddColumnFloat(fRoi*fNumTm, "TriggerOffsetTMRms", "mV");
1454
1455 file.SetDefaultKeys();
1456 if (night>0)
1457 file.SetInt("NIGHT", night, "Night as int");
1458
1459 file.SetStr("DATE-OBS", fDateObs, "First event of whole DRS calibration");
1460 file.SetStr("DATE-END", fDateEnd, "Last event of whole DRS calibration");
1461 file.SetStr("RUN0-BEG", fDateRunBeg[0], "First event of run 0");
1462 file.SetStr("RUN1-BEG", fDateRunBeg[1], "First event of run 1");
1463 file.SetStr("RUN2-BEG", fDateRunBeg[2], "First event of run 2");
1464 file.SetStr("RUN0-END", fDateRunEnd[0], "Last event of run 0");
1465 file.SetStr("RUN1-END", fDateRunEnd[1], "Last event of run 1");
1466 file.SetStr("RUN2-END", fDateRunEnd[2], "Last event of run 2");
1467
1468 file.SetInt("STEP", fStep, "");
1469
1470 file.SetInt("ADCRANGE", 2000, "Dynamic range of the ADC in mV");
1471 file.SetInt("DACRANGE", 2500, "Dynamic range of the DAC in mV");
1472 file.SetInt("ADC", 12, "Resolution of ADC in bits");
1473 file.SetInt("DAC", 16, "Resolution of DAC in bits");
1474 file.SetInt("NPIX", 1440, "Number of channels in the camera");
1475 file.SetInt("NTM", fNumTm, "Number of time marker channels");
1476 file.SetInt("NROI", fRoi, "Region of interest");
1477
1478 file.SetInt("NBOFFSET", fNumOffset, "Num of entries for offset calibration");
1479 file.SetInt("NBGAIN", fNumGain/1953125, "Num of entries for gain calibration");
1480 file.SetInt("NBTRGOFF", fNumTrgOff, "Num of entries for trigger offset calibration");
1481
1482 // file.WriteKeyNT("DAC_A", fData.fDAC[0], "Level of DAC 0 in DAC counts") ||
1483 // file.WriteKeyNT("DAC_B", fData.fDAC[1], "Leval of DAC 1-3 in DAC counts") ||
1484 // file.WriteKeyNT("DAC_C", fData.fDAC[4], "Leval of DAC 4-7 in DAC counts") ||
1485
1486 file.WriteTableHeader("DrsCalibration");
1487
1488 if (!file.WriteRow(vec.data()+1, n*sizeof(float)))
1489 {
1490 std::ostringstream msg;
1491 msg << "Writing data to " << filename << " failed.";
1492 return msg.str();
1493 }
1494
1495 return std::string();
1496 }
1497
1498
1499 std::string ReadFitsImp(const std::string &str)
1500 {
1501 std::vector<float> vec;
1502 return ReadFitsImp(str, vec);
1503 }
1504
1505 bool IsValid() { return fStep>2; }
1506
1507 bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
1508 {
1509 if (roi!=fRoi)
1510 {
1511 for (size_t ch=0; ch<1440; ch++)
1512 {
1513 const size_t pos = ch*roi;
1514 const size_t drs = ch*1024;
1515
1516 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
1517 fOffset.data()+drs, fNumOffset,
1518 fGain.data() +drs, fNumGain);
1519 }
1520
1521 return false;
1522 }
1523
1524 for (size_t ch=0; ch<1440; ch++)
1525 {
1526 const size_t pos = ch*fRoi;
1527 const size_t drs = ch*1024;
1528
1529 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
1530 fOffset.data()+drs, fNumOffset,
1531 fGain.data() +drs, fNumGain,
1532 fTrgOff.data()+pos, fNumTrgOff);
1533 }
1534
1535 for (size_t ch=0; ch<fNumTm; ch++)
1536 {
1537 const size_t pos = (ch+1440)*fRoi;
1538 const size_t drs = (ch*9+8)*1024;
1539
1540 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
1541 fOffset.data()+drs, fNumOffset,
1542 fGain.data() +drs, fNumGain,
1543 fTrgOff.data()+pos, fNumTrgOff);
1544 }
1545
1546 return true;
1547 }
1548};
1549
1550#endif
Note: See TracBrowser for help on using the repository browser.