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

Last change on this file since 17539 was 17539, checked in by lyard, 11 years ago
moved uint64 to int64 in DrsCalibration member variables
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 int64_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 int64_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 int64_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 int64_t scaleabs,
300 const int64_t *gain, const int64_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 int64_t scaleabs,
363 const int64_t *gain, const int64_t scalegain,
364 const int64_t *trgoff, const int64_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*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, uint16_t begskip=0, uint16_t endskip=0)
860 {
861 if (roi==0)
862 return -1;
863
864 // Skip first 10 samples
865 const uint beg = roi>begskip ? begskip : 0;
866 const uint end = roi-beg>endskip ? roi-endskip : 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 sum2 -= sum*sum;
890
891 if (i%9!=8)
892 patch += vec[pos];
893 else
894 {
895 if (patch > max)
896 max = patch;
897 patch = 0;
898 }
899
900 *(ptr+0*1440+i) = sum;
901 *(ptr+1*1440+i) = sum2<0 ? 0 : sqrt(sum2);
902 *(ptr+2*1440+i) = vec[pos];
903 *(ptr+3*1440+i) = pos;
904 }
905
906 return max/8;
907 }
908
909 static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last)
910 {
911 if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last<first)
912 return;
913
914 for (int i=0; i<1440; i++)
915 {
916 const float *beg = data+i*roi+first;
917 const float *end = data+i*roi+last;
918
919 const float *pmax = beg;
920
921 for (const float *ptr=beg+1; ptr<=end; ptr++)
922 if (*ptr>*pmax)
923 pmax = ptr;
924
925 max[i] = *pmax;
926 }
927 }
928
929 const std::vector<int64_t> &GetSum() const { return fSum; }
930
931 int64_t GetNumEntries() const { return fNumEntries; }
932};
933
934class DrsCalibrateTime
935{
936public:
937 int64_t fNumEntries;
938
939 size_t fNumSamples;
940 size_t fNumChannels;
941
942 std::vector<std::pair<double, double>> fStat;
943
944public:
945 DrsCalibrateTime() : fNumEntries(0), fNumSamples(0), fNumChannels(0)
946 {
947 InitSize(160, 1024);
948 }
949
950 DrsCalibrateTime(const DrsCalibrateTime &p) : fNumEntries(p.fNumEntries), fNumSamples(p.fNumSamples), fNumChannels(p.fNumChannels), fStat(p.fStat)
951 {
952 }
953 virtual ~DrsCalibrateTime()
954 {
955 }
956
957 double Sum(uint32_t i) const { return fStat[i].first; }
958 double W(uint32_t i) const { return fStat[i].second; }
959
960 virtual void InitSize(uint16_t channels, uint16_t samples)
961 {
962 fNumChannels = channels;
963 fNumSamples = samples;
964
965 fNumEntries = 0;
966
967 fStat.clear();
968
969 fStat.resize(samples*channels);
970 }
971
972 void AddT(const float *val, const int16_t *start, signed char edge=0)
973 {
974 if (fNumSamples!=1024 || fNumChannels!=160)
975 return;
976
977 // Rising or falling edge detection has the advantage that
978 // we are much less sensitive to baseline shifts
979
980 for (size_t ch=0; ch<160; ch++)
981 {
982 const size_t tm = ch*9+8;
983
984 const int16_t spos = start[tm];
985 if (spos<0)
986 continue;
987
988 const size_t pos = ch*1024;
989
990 double p_prev = 0;
991 int32_t i_prev = -1;
992
993 for (size_t i=0; i<1024-1; i++)
994 {
995 const size_t rel = tm*1024 + i;
996
997 const float &v0 = val[rel]; //-avg;
998 const float &v1 = val[rel+1];//-avg;
999
1000 // If edge is positive ignore all falling edges
1001 if (edge>0 && v0>0)
1002 continue;
1003
1004 // If edge is negative ignore all falling edges
1005 if (edge<0 && v0<0)
1006 continue;
1007
1008 // Check if there is a zero crossing
1009 if ((v0<0 && v1<0) || (v0>0 && v1>0))
1010 continue;
1011
1012 // Calculate the position p of the zero-crossing
1013 // within the interval [rel, rel+1] relative to rel
1014 // by linear interpolation.
1015 const double p = v0==v1 ? 0.5 : v0/(v0-v1);
1016
1017 // If this was at least the second zero-crossing detected
1018 if (i_prev>=0)
1019 {
1020 // Calculate the distance l between the
1021 // current and the last zero-crossing
1022 const double l = i+p - (i_prev+p_prev);
1023
1024 // By summation, the average length of each
1025 // cell is calculated. For the first and last
1026 // fraction of a cell, the fraction is applied
1027 // as a weight.
1028 const double w0 = 1-p_prev;
1029 fStat[pos+(spos+i_prev)%1024].first += w0*l;
1030 fStat[pos+(spos+i_prev)%1024].second += w0;
1031
1032 for (size_t k=i_prev+1; k<i; k++)
1033 {
1034 fStat[pos+(spos+k)%1024].first += l;
1035 fStat[pos+(spos+k)%1024].second += 1;
1036 }
1037
1038 const double w1 = p;
1039 fStat[pos+(spos+i)%1024].first += w1*l;
1040 fStat[pos+(spos+i)%1024].second += w1;
1041 }
1042
1043 // Remember this zero-crossing position
1044 p_prev = p;
1045 i_prev = i;
1046 }
1047 }
1048 fNumEntries++;
1049 }
1050
1051 void FillEmptyBins()
1052 {
1053 for (int ch=0; ch<160; ch++)
1054 {
1055 const auto beg = fStat.begin() + ch*1024;
1056 const auto end = beg + 1024;
1057
1058 double avg = 0;
1059 uint32_t num = 0;
1060 for (auto it=beg; it!=end; it++)
1061 {
1062 if (it->second<fNumEntries-0.5)
1063 continue;
1064
1065 avg += it->first / it->second;
1066 num++;
1067 }
1068 avg /= num;
1069
1070 for (auto it=beg; it!=end; it++)
1071 {
1072 if (it->second>=fNumEntries-0.5)
1073 continue;
1074
1075 // {
1076 // result[i+1].first = *is2;
1077 // result[i+1].second = *iw2;
1078 // }
1079 // else
1080 // {
1081 it->first = avg*fNumEntries;
1082 it->second = fNumEntries;
1083 // }
1084 }
1085 }
1086 }
1087
1088 DrsCalibrateTime GetComplete() const
1089 {
1090 DrsCalibrateTime rc(*this);
1091 rc.FillEmptyBins();
1092 return rc;
1093 }
1094
1095 void CalcResult()
1096 {
1097 for (int ch=0; ch<160; ch++)
1098 {
1099 const auto beg = fStat.begin() + ch*1024;
1100 const auto end = beg + 1024;
1101
1102 // First calculate the average length s of a single
1103 // zero-crossing interval in the whole range [0;1023]
1104 // (which is identical to the/ wavelength of the
1105 // calibration signal)
1106 double s = 0;
1107 double w = 0;
1108 for (auto it=beg; it!=end; it++)
1109 {
1110 s += it->first;
1111 w += it->second;
1112 }
1113 s /= w;
1114
1115 // Dividing the average length s of the zero-crossing
1116 // interval in the range [0;1023] by the average length
1117 // in the interval [0;n] yields the relative size of
1118 // the interval in the range [0;n].
1119 //
1120 // Example:
1121 // Average [0;1023]: 10.00 (global interval size in samples)
1122 // Average [0;512]: 8.00 (local interval size in samples)
1123 //
1124 // Globally, on average one interval is sampled by 10 samples.
1125 // In the sub-range [0;512] one interval is sampled on average
1126 // by 8 samples.
1127 // That means that the interval contains 64 periods, while
1128 // in the ideal case (each sample has the same length), it
1129 // should contain 51.2 periods.
1130 // So, the sampling position 512 corresponds to a time 640,
1131 // while in the ideal case with equally spaces samples,
1132 // it would correspond to a time 512.
1133 //
1134 // The offset (defined as 'ideal - real') is then calculated
1135 // as 512*(1-10/8) = -128, so that the time is calculated as
1136 // 'sampling position minus offset'
1137 //
1138 double sumw = 0;
1139 double sumv = 0;
1140 int n = 0;
1141
1142 // Sums about many values are numerically less stable than
1143 // just sums over less. So we do the exercise from both sides.
1144 // First from the left
1145 for (auto it=beg; it!=end-512; it++, n++)
1146 {
1147 const double valv = it->first;
1148 const double valw = it->second;
1149
1150 it->first = sumv>0 ? n*(1-s*sumw/sumv) : 0;
1151
1152 sumv += valv;
1153 sumw += valw;
1154 }
1155
1156 sumw = 0;
1157 sumv = 0;
1158 n = 1;
1159
1160 // Second from the right
1161 for (auto it=end-1; it!=beg-1+512; it--, n++)
1162 {
1163 const double valv = it->first;
1164 const double valw = it->second;
1165
1166 sumv += valv;
1167 sumw += valw;
1168
1169 it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0;
1170 }
1171
1172 // A crosscheck has shown, that the values from the left
1173 // and right perfectly agree over the whole range. This means
1174 // the a calculation from just one side would be enough, but
1175 // doing it from both sides might still make the numerics
1176 // a bit more stable.
1177 }
1178 }
1179
1180 DrsCalibrateTime GetResult() const
1181 {
1182 DrsCalibrateTime rc(*this);
1183 rc.CalcResult();
1184 return rc;
1185 }
1186
1187 double Offset(uint32_t ch, double pos) const
1188 {
1189 const auto p = fStat.begin() + ch*1024;
1190
1191 const uint32_t f = floor(pos);
1192
1193 const double v0 = p[f].first;
1194 const double v1 = p[(f+1)%1024].first;
1195
1196 return v0 + fmod(pos, 1)*(v1-v0);
1197 }
1198
1199 double Calib(uint32_t ch, double pos) const
1200 {
1201 return pos-Offset(ch, pos);
1202 }
1203};
1204
1205struct DrsCalibration
1206{
1207 std::vector<int32_t> fOffset;
1208 std::vector<int64_t> fGain;
1209 std::vector<int64_t> fTrgOff;
1210
1211 int64_t fNumOffset;
1212 int64_t fNumGain;
1213 int64_t fNumTrgOff;
1214
1215 uint32_t fStep;
1216 uint16_t fRoi; // Region of interest for trgoff
1217 uint16_t fNumTm; // Number of time marker channels in trgoff
1218
1219 std::string fDateObs;
1220 std::string fDateRunBeg[3];
1221 std::string fDateRunEnd[3];
1222 std::string fDateEnd;
1223
1224// uint16_t fDAC[8];
1225
1226 DrsCalibration() :
1227 fOffset(1440*1024, 0),
1228 fGain(1440*1024, 4096),
1229 fTrgOff (1600*1024, 0),
1230 fNumOffset(1),
1231 fNumGain(2000),
1232 fNumTrgOff(1),
1233 fStep(0),
1234 fDateObs("1970-01-01T00:00:00"),
1235 fDateEnd("1970-01-01T00:00:00")
1236 {
1237 for (int i=0; i<3; i++)
1238 {
1239 fDateRunBeg[i] = "1970-01-01T00:00:00";
1240 fDateRunEnd[i] = "1970-01-01T00:00:00";
1241 }
1242 }
1243
1244 DrsCalibration(const DrsCalibration &cpy) :
1245 fOffset(cpy.fOffset),
1246 fGain(cpy.fGain),
1247 fTrgOff(cpy.fTrgOff),
1248 fNumOffset(cpy.fNumOffset),
1249 fNumGain(cpy.fNumGain),
1250 fNumTrgOff(cpy.fNumTrgOff),
1251 fStep(cpy.fStep),
1252 fRoi(cpy.fRoi),
1253 fNumTm(cpy.fNumTm),
1254 fDateObs(cpy.fDateObs),
1255 fDateRunBeg(cpy.fDateRunBeg),
1256 fDateRunEnd(cpy.fDateRunEnd),
1257 fDateEnd(cpy.fDateEnd)
1258 {
1259 }
1260
1261 void Clear()
1262 {
1263 // Default gain:
1264 // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
1265 fOffset.assign(1440*1024, 0);
1266 fGain.assign (1440*1024, 4096);
1267 fTrgOff.assign(1600*1024, 0);
1268
1269 fNumOffset = 1;
1270 fNumGain = 2000;
1271 fNumTrgOff = 1;
1272
1273 fStep = 0;
1274
1275 fDateObs = "1970-01-01T00:00:00";
1276 fDateEnd = "1970-01-01T00:00:00";
1277
1278 for (int i=0; i<3; i++)
1279 {
1280 fDateRunBeg[i] = "1970-01-01T00:00:00";
1281 fDateRunEnd[i] = "1970-01-01T00:00:00";
1282 }
1283 }
1284
1285 std::string ReadFitsImp(const std::string &str, std::vector<float> &vec)
1286 {
1287 fits file(str);
1288 if (!file)
1289 {
1290 std::ostringstream msg;
1291 msg << "Could not open file '" << str << "': " << strerror(errno);
1292 return msg.str();
1293 }
1294
1295 if (file.GetStr("TELESCOP")!="FACT")
1296 {
1297 std::ostringstream msg;
1298 msg << "Reading '" << str << "' failed: Not a valid FACT file (TELESCOP not FACT in header)";
1299 return msg.str();
1300 }
1301
1302 if (!file.HasKey("STEP"))
1303 {
1304 std::ostringstream msg;
1305 msg << "Reading '" << str << "' failed: Is not a DRS calib file (STEP not found in header)";
1306 return msg.str();
1307 }
1308
1309 if (file.GetNumRows()!=1)
1310 {
1311 std::ostringstream msg;
1312 msg << "Reading '" << str << "' failed: Number of rows in table is not 1.";
1313 return msg.str();
1314 }
1315
1316 fStep = file.GetUInt("STEP");
1317 fNumOffset = file.GetInt("NBOFFSET");
1318 fNumGain = file.GetInt("NBGAIN");
1319 fNumTrgOff = file.GetInt("NBTRGOFF");
1320 fRoi = file.GetUInt("NROI");
1321 fNumTm = file.HasKey("NTM") ? file.GetUInt("NTM") : 0;
1322
1323 if (file.HasKey("DATE-OBS"))
1324 fDateObs = file.GetStr("DATE-OBS");
1325 if (file.HasKey("DATE-END"))
1326 fDateEnd = file.GetStr("DATE-END");
1327
1328 if (file.HasKey("RUN0-BEG"))
1329 fDateRunBeg[0]= file.GetStr("RUN0-BEG");
1330 if (file.HasKey("RUN1-BEG"))
1331 fDateRunBeg[1]= file.GetStr("RUN1-BEG");
1332 if (file.HasKey("RUN2-BEG"))
1333 fDateRunBeg[2]= file.GetStr("RUN2-BEG");
1334 if (file.HasKey("RUN0-END"))
1335 fDateRunEnd[0]= file.GetStr("RUN0-END");
1336 if (file.HasKey("RUN1-END"))
1337 fDateRunEnd[1]= file.GetStr("RUN1-END");
1338 if (file.HasKey("RUN2-END"))
1339 fDateRunEnd[2]= file.GetStr("RUN2-END");
1340/*
1341 fDAC[0] = file.GetUInt("DAC_A");
1342 fDAC[1] = file.GetUInt("DAC_B");
1343 fDAC[4] = file.GetUInt("DAC_C");
1344*/
1345 vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
1346
1347 float *base = vec.data();
1348
1349 reinterpret_cast<uint32_t*>(base)[0] = fRoi;
1350
1351 file.SetPtrAddress("RunNumberBaseline", base+1, 1);
1352 file.SetPtrAddress("RunNumberGain", base+2, 1);
1353 file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1);
1354 file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440);
1355 file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440);
1356 file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440);
1357 file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440);
1358 file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440);
1359 file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+fRoi*1440, fRoi*1440);
1360 if (fNumTm>0)
1361 {
1362 file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm);
1363 file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
1364 }
1365
1366 if (!file.GetNextRow())
1367 {
1368 std::ostringstream msg;
1369 msg << "Reading data from " << str << " failed.";
1370 return msg.str();
1371 }
1372/*
1373 fDAC[2] = fDAC[1];
1374 fDAC[4] = fDAC[1];
1375
1376 fDAC[5] = fDAC[4];
1377 fDAC[6] = fDAC[4];
1378 fDAC[7] = fDAC[4];
1379*/
1380 fOffset.resize(1024*1440);
1381 fGain.resize(1024*1440);
1382
1383 fTrgOff.resize(fRoi*(1440+fNumTm));
1384
1385 // Convert back to ADC counts: 256/125 = 4096/2000
1386 // Convert back to sum (mean * num_entries)
1387 for (int i=0; i<1024*1440; i++)
1388 {
1389 fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125;
1390 fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
1391 }
1392
1393 for (int i=0; i<fRoi*1440; i++)
1394 fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
1395
1396 for (int i=0; i<fRoi*fNumTm; i++)
1397 fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
1398
1399
1400 // DAC: 0..2.5V == 0..65535
1401 // V-mV: 1000
1402 //fNumGain *= 2500*50000;
1403 //for (int i=0; i<1024*1440; i++)
1404 // fGain[i] *= 65536;
1405 if (fStep==0)
1406 {
1407 for (int i=0; i<1024*1440; i++)
1408 fGain[i] = fNumOffset*4096;
1409 }
1410 else
1411 {
1412 fNumGain *= 1953125;
1413 for (int i=0; i<1024*1440; i++)
1414 fGain[i] *= 1024;
1415 }
1416
1417 // Now mark the stored DRS data as "officially valid"
1418 // However, this is not thread safe. It only ensures that
1419 // this data is not used before it is completely and correctly
1420 // read.
1421 fStep++;
1422
1423 return std::string();
1424 }
1425
1426 std::string WriteFitsImp(const std::string &filename, const std::vector<float> &vec, uint32_t night=0) const
1427 {
1428 const size_t n = 1440*1024*4 + 1440*fRoi*2 + fNumTm*fRoi*2 + 3;
1429
1430 ofits file(filename.c_str());
1431 if (!file)
1432 {
1433 std::ostringstream msg;
1434 msg << "Could not open file '" << filename << "': " << strerror(errno);
1435 return msg.str();
1436 }
1437
1438 file.AddColumnInt("RunNumberBaseline");
1439 file.AddColumnInt("RunNumberGain");
1440 file.AddColumnInt("RunNumberTriggerOffset");
1441
1442 file.AddColumnFloat(1024*1440, "BaselineMean", "mV");
1443 file.AddColumnFloat(1024*1440, "BaselineRms", "mV");
1444 file.AddColumnFloat(1024*1440, "GainMean", "mV");
1445 file.AddColumnFloat(1024*1440, "GainRms", "mV");
1446 file.AddColumnFloat(fRoi*1440, "TriggerOffsetMean", "mV");
1447 file.AddColumnFloat(fRoi*1440, "TriggerOffsetRms", "mV");
1448 file.AddColumnFloat(fRoi*fNumTm, "TriggerOffsetTMMean", "mV");
1449 file.AddColumnFloat(fRoi*fNumTm, "TriggerOffsetTMRms", "mV");
1450
1451 file.SetDefaultKeys();
1452 if (night>0)
1453 file.SetInt("NIGHT", night, "Night as int");
1454
1455 file.SetStr("DATE-OBS", fDateObs, "First event of whole DRS calibration");
1456 file.SetStr("DATE-END", fDateEnd, "Last event of whole DRS calibration");
1457 file.SetStr("RUN0-BEG", fDateRunBeg[0], "First event of run 0");
1458 file.SetStr("RUN1-BEG", fDateRunBeg[1], "First event of run 1");
1459 file.SetStr("RUN2-BEG", fDateRunBeg[2], "First event of run 2");
1460 file.SetStr("RUN0-END", fDateRunEnd[0], "Last event of run 0");
1461 file.SetStr("RUN1-END", fDateRunEnd[1], "Last event of run 1");
1462 file.SetStr("RUN2-END", fDateRunEnd[2], "Last event of run 2");
1463
1464 file.SetInt("STEP", fStep, "");
1465
1466 file.SetInt("ADCRANGE", 2000, "Dynamic range of the ADC in mV");
1467 file.SetInt("DACRANGE", 2500, "Dynamic range of the DAC in mV");
1468 file.SetInt("ADC", 12, "Resolution of ADC in bits");
1469 file.SetInt("DAC", 16, "Resolution of DAC in bits");
1470 file.SetInt("NPIX", 1440, "Number of channels in the camera");
1471 file.SetInt("NTM", fNumTm, "Number of time marker channels");
1472 file.SetInt("NROI", fRoi, "Region of interest");
1473
1474 file.SetInt("NBOFFSET", fNumOffset, "Num of entries for offset calibration");
1475 file.SetInt("NBGAIN", fNumGain/1953125, "Num of entries for gain calibration");
1476 file.SetInt("NBTRGOFF", fNumTrgOff, "Num of entries for trigger offset calibration");
1477
1478 // file.WriteKeyNT("DAC_A", fData.fDAC[0], "Level of DAC 0 in DAC counts") ||
1479 // file.WriteKeyNT("DAC_B", fData.fDAC[1], "Leval of DAC 1-3 in DAC counts") ||
1480 // file.WriteKeyNT("DAC_C", fData.fDAC[4], "Leval of DAC 4-7 in DAC counts") ||
1481
1482 file.WriteTableHeader("DrsCalibration");
1483
1484 if (!file.WriteRow(vec.data()+1, n*sizeof(float)))
1485 {
1486 std::ostringstream msg;
1487 msg << "Writing data to " << filename << " failed.";
1488 return msg.str();
1489 }
1490
1491 return std::string();
1492 }
1493
1494
1495 std::string ReadFitsImp(const std::string &str)
1496 {
1497 std::vector<float> vec;
1498 return ReadFitsImp(str, vec);
1499 }
1500
1501 bool IsValid() { return fStep>2; }
1502
1503 bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
1504 {
1505 if (roi!=fRoi)
1506 {
1507 for (size_t ch=0; ch<1440; ch++)
1508 {
1509 const size_t pos = ch*roi;
1510 const size_t drs = ch*1024;
1511
1512 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
1513 fOffset.data()+drs, fNumOffset,
1514 fGain.data() +drs, fNumGain);
1515 }
1516
1517 return false;
1518 }
1519
1520 for (size_t ch=0; ch<1440; ch++)
1521 {
1522 const size_t pos = ch*fRoi;
1523 const size_t drs = ch*1024;
1524
1525 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
1526 fOffset.data()+drs, fNumOffset,
1527 fGain.data() +drs, fNumGain,
1528 fTrgOff.data()+pos, fNumTrgOff);
1529 }
1530
1531 for (size_t ch=0; ch<fNumTm; ch++)
1532 {
1533 const size_t pos = (ch+1440)*fRoi;
1534 const size_t drs = (ch*9+8)*1024;
1535
1536 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
1537 fOffset.data()+drs, fNumOffset,
1538 fGain.data() +drs, fNumGain,
1539 fTrgOff.data()+pos, fNumTrgOff);
1540 }
1541
1542 return true;
1543 }
1544};
1545
1546#endif
Note: See TracBrowser for help on using the repository browser.