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

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