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

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