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

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