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

Last change on this file since 12924 was 12924, checked in by tbretz, 13 years ago
Added DrsCalibrateTime; removed obsolete old AddT
File size: 21.5 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
9class DrsCalibrate
10{
11protected:
12 uint64_t fNumEntries;
13
14 size_t fNumSamples;
15 size_t fNumChannels;
16
17 std::vector<int64_t> fSum;
18 std::vector<int64_t> fSum2;
19
20public:
21 DrsCalibrate() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { }
22 void Reset()
23 {
24 fNumEntries = 0;
25 fNumSamples = 0;
26 fNumChannels = 0;
27
28 fSum.clear();
29 fSum2.clear();
30 }
31
32 void InitSize(uint16_t channels, uint16_t samples)
33 {
34 fNumChannels = channels;
35 fNumSamples = samples;
36
37 fSum.resize(samples*channels);
38 fSum2.resize(samples*channels);
39 }
40
41 void AddRel(const int16_t *val, const int16_t *start)
42 {
43 for (size_t ch=0; ch<fNumChannels; ch++)
44 {
45 const int16_t spos = start[ch];
46 if (spos<0)
47 continue;
48
49 const size_t pos = ch*1024;
50
51 for (size_t i=0; i<1024; i++)
52 {
53 // Value is relative to trigger
54 // Abs is corresponding index relative to DRS pipeline
55 const size_t rel = pos + i;
56 const size_t abs = pos + (spos+i)%1024;
57
58 const int64_t v = val[rel];
59
60 fSum[abs] += v;
61 fSum2[abs] += v*v;
62 }
63 }
64
65 fNumEntries++;
66 }
67
68 void AddRel(const int16_t *val, const int16_t *start,
69 const int32_t *offset, const uint32_t scale)
70 {
71 for (size_t ch=0; ch<fNumChannels; ch++)
72 {
73 const int16_t spos = start[ch];
74 if (spos<0)
75 continue;
76
77 const size_t pos = ch*fNumSamples;
78
79 for (size_t i=0; i<fNumSamples; i++)
80 {
81 // Value is relative to trigger
82 // Offset is relative to DRS pipeline
83 // Abs is corresponding index relative to DRS pipeline
84 const size_t rel = pos + i;
85 const size_t abs = pos + (spos+i)%fNumSamples;
86
87 const int64_t v = int64_t(val[rel])*scale-offset[abs];
88
89 fSum[abs] += v;
90 fSum2[abs] += v*v;
91 }
92 }
93
94 fNumEntries++;
95 }
96 /*
97 void AddAbs(const int16_t *val, const int16_t *start,
98 const int32_t *offset, const uint32_t scale)
99 {
100 for (size_t ch=0; ch<fNumChannels; ch++)
101 {
102 const int16_t spos = start[ch];
103 if (spos<0)
104 continue;
105
106 const size_t pos = ch*fNumSamples;
107
108 for (size_t i=0; i<fNumSamples; i++)
109 {
110 // Value is relative to trigger
111 // Offset is relative to DRS pipeline
112 // Abs is corresponding index relative to DRS pipeline
113 const size_t rel = pos + i;
114 const size_t abs = pos + (spos+i)%fNumSamples;
115
116 const int64_t v = int64_t(val[rel])*scale-offset[abs];
117
118 fSum[rel] += v;
119 fSum2[rel] += v*v;
120 }
121 }
122
123 fNumEntries++;
124 }*/
125
126 void AddAbs(const int16_t *val, const int16_t *start,
127 const int32_t *offset, const uint32_t scale)
128 {
129 // 1440 without tm, 1600 with tm
130 for (size_t ch=0; ch<fNumChannels; ch++)
131 {
132 const int16_t spos = start[ch];
133 if (spos<0)
134 continue;
135
136 const size_t pos = ch*fNumSamples;
137 const size_t drs = ch>1439 ? ((ch-1440)*9+8)*1024 : ch*1024;
138
139 for (size_t i=0; i<fNumSamples; i++)
140 {
141 // Value is relative to trigger
142 // Offset is relative to DRS pipeline
143 // Abs is corresponding index relative to DRS pipeline
144 const size_t rel = pos + i;
145 const size_t abs = drs + (spos+i)%1024;
146
147 const int64_t v = int64_t(val[rel])*scale-offset[abs];
148
149 fSum[rel] += v;
150 fSum2[rel] += v*v;
151 }
152 }
153
154 fNumEntries++;
155 }
156
157 static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
158 const int32_t *offset, const uint32_t scaleabs,
159 const int64_t *gain, const uint64_t scalegain)
160 {
161 if (start<0)
162 {
163 memset(vec, 0, roi);
164 return;
165 }
166
167 for (size_t i=0; i<roi; i++)
168 {
169 // Value is relative to trigger
170 // Offset is relative to DRS pipeline
171 // Abs is corresponding index relative to DRS pipeline
172 const size_t abs = (start+i)%1024;
173
174 const int64_t v =
175 + int64_t(val[i])*scaleabs-offset[abs]
176 ;
177
178 const int64_t div = gain[abs];
179 vec[i] = double(v)*scalegain/div;
180 }
181 }
182
183 static void ApplyCh(float *vec, const int16_t *val, int16_t start, uint32_t roi,
184 const int32_t *offset, const uint32_t scaleabs,
185 const int64_t *gain, const uint64_t scalegain,
186 const int64_t *trgoff, const uint64_t scalerel)
187 {
188 if (start<0)
189 {
190 memset(vec, 0, roi);
191 return;
192 }
193
194 for (size_t i=0; i<roi; i++)
195 {
196 // Value is relative to trigger
197 // Offset is relative to DRS pipeline
198 // Abs is corresponding index relative to DRS pipeline
199 const size_t abs = (start+i)%1024;
200
201 const int64_t v =
202 + (int64_t(val[i])*scaleabs-offset[abs])*scalerel
203 - trgoff[i]
204 ;
205
206 const int64_t div = gain[abs]*scalerel;
207 vec[i] = double(v)*scalegain/div;
208 }
209 }
210
211 static void RemoveSpikes(float *vec, uint32_t roi)
212 {
213 if (roi<4)
214 return;
215
216 for (size_t ch=0; ch<1440; ch++)
217 {
218 float *p = vec + ch*roi;
219
220 for (size_t i=1; i<roi-2; i++)
221 {
222 if (p[i]-p[i-1]>25 && p[i]-p[i+1]>25)
223 {
224 p[i] = (p[i-1]+p[i+1])/2;
225 }
226
227 if (p[i]-p[i-1]>22 && fabs(p[i]-p[i+1])<4 && p[i+1]-p[i+2]>22)
228 {
229 p[i] = (p[i-1]+p[i+2])/2;
230 p[i+1] = p[i];
231 }
232 }
233 }
234 }
235
236 std::pair<std::vector<double>,std::vector<double> > GetSampleStats() const
237 {
238 if (fNumEntries==0)
239 return make_pair(std::vector<double>(),std::vector<double>());
240
241 std::vector<double> mean(fSum.size());
242 std::vector<double> error(fSum.size());
243
244 std::vector<int64_t>::const_iterator it = fSum.begin();
245 std::vector<int64_t>::const_iterator i2 = fSum2.begin();
246 std::vector<double>::iterator im = mean.begin();
247 std::vector<double>::iterator ie = error.begin();
248
249 while (it!=fSum.end())
250 {
251 *im = /*cnt<fResult.size() ? fResult[cnt] :*/ double(*it)/fNumEntries;
252 *ie = sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
253
254 im++;
255 ie++;
256 it++;
257 i2++;
258 }
259
260
261 /*
262 valarray<double> ...
263
264 mean /= fNumEntries;
265 error = sqrt(error/fNumEntries - mean*mean);
266 */
267
268 return make_pair(mean, error);
269 }
270
271 void GetSampleStats(float *ptr, float scale) const
272 {
273 const size_t sz = fNumSamples*fNumChannels;
274
275 if (fNumEntries==0)
276 {
277 memset(ptr, 0, sizeof(float)*sz*2);
278 return;
279 }
280
281 std::vector<int64_t>::const_iterator it = fSum.begin();
282 std::vector<int64_t>::const_iterator i2 = fSum2.begin();
283
284 while (it!=fSum.end())
285 {
286 *ptr = scale*double(*it)/fNumEntries;
287 *(ptr+sz) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
288
289 ptr++;
290 it++;
291 i2++;
292 }
293 }
294
295 static void GetPixelStats(float *ptr, const float *data, uint16_t roi)
296 {
297 if (roi==0)
298 return;
299
300 for (int i=0; i<1440; i++)
301 {
302 const float *vec = data+i*roi;
303
304 int pos = 0;
305 double sum = vec[0];
306 double sum2 = vec[0]*vec[0];
307 for (int j=1; j<roi; j++)
308 {
309 sum += vec[j];
310 sum2 += vec[j]*vec[j];
311
312 if (vec[j]>vec[pos])
313 pos = j;
314 }
315 sum /= roi;
316 sum2 /= roi;
317
318 *(ptr+0*1440+i) = sum;
319 *(ptr+1*1440+i) = sqrt(sum2 - sum * sum);
320 *(ptr+2*1440+i) = vec[pos];
321 *(ptr+3*1440+i) = pos;
322 }
323 }
324
325 static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last)
326 {
327 if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last<first)
328 return;
329
330 for (int i=0; i<1440; i++)
331 {
332 const float *beg = data+i*roi+first;
333 const float *end = data+i*roi+last;
334
335 const float *pmax = beg;
336
337 for (const float *ptr=beg+1; ptr<=end; ptr++)
338 if (*ptr>*pmax)
339 pmax = ptr;
340
341 max[i] = *pmax;
342 }
343 }
344
345 const std::vector<int64_t> &GetSum() const { return fSum; }
346
347 uint64_t GetNumEntries() const { return fNumEntries; }
348};
349
350class DrsCalibrateTime
351{
352public:
353 uint64_t fNumEntries;
354
355 size_t fNumSamples;
356 size_t fNumChannels;
357
358 std::vector<std::pair<double, double>> fStat;
359
360public:
361 DrsCalibrateTime() : fNumEntries(0), fNumSamples(0), fNumChannels(0)
362 {
363 InitSize(160, 1024);
364 }
365
366 DrsCalibrateTime(const DrsCalibrateTime &p) : fNumEntries(p.fNumEntries), fNumSamples(p.fNumSamples), fNumChannels(p.fNumChannels), fStat(p.fStat)
367 {
368 }
369
370 double Sum(uint32_t i) const { return fStat[i].first; }
371 double W(uint32_t i) const { return fStat[i].second; }
372
373 void InitSize(uint16_t channels, uint16_t samples)
374 {
375 fNumChannels = channels;
376 fNumSamples = samples;
377
378 fNumEntries = 0;
379
380 fStat.clear();
381
382 fStat.resize(samples*channels);
383 }
384
385 void AddT(const float *val, const int16_t *start, signed char edge=0)
386 {
387 if (fNumSamples!=1024 || fNumChannels!=160)
388 return;
389
390 // Rising or falling edge detection has the advantage that
391 // we are much less sensitive to baseline shifts
392
393 for (size_t ch=0; ch<160; ch++)
394 {
395 const size_t tm = ch*9+8;
396
397 const int16_t spos = start[tm];
398 if (spos<0)
399 continue;
400
401 const size_t pos = ch*1024;
402
403 double p_prev = 0;
404 int32_t i_prev = -1;
405
406 for (size_t i=0; i<1024-1; i++)
407 {
408 const size_t rel = tm*1024 + i;
409
410 const float &v0 = val[rel]; //-avg;
411 const float &v1 = val[rel+1];//-avg;
412
413 // Is rising edge?
414 if (edge>0 && v0>0)
415 continue;
416
417 // Is falling edge?
418 if (edge<0 && v0<0)
419 continue;
420
421 // Has sign changed?
422 if ((v0<0 && v1<0) || (v0>0 && v1>0))
423 continue;
424
425 const double p = v0==v1 ? 0.5 : v0/(v0-v1);
426
427 const double l = i+p - (i_prev+p_prev);
428
429 if (i_prev>=0)
430 {
431 const double w0 = 1-p_prev;
432 const double w1 = p;
433
434 fStat[pos+(spos+i_prev)%1024].first += w0*l;
435 fStat[pos+(spos+i_prev)%1024].second += w0;
436
437 for (size_t k=i_prev+1; k<i; k++)
438 {
439 fStat[pos+(spos+k)%1024].first += l;
440 fStat[pos+(spos+k)%1024].second += 1;
441 }
442
443 fStat[pos+(spos+i)%1024].first += w1*l;
444 fStat[pos+(spos+i)%1024].second += w1;
445 }
446
447 p_prev = p;
448 i_prev = i;
449 }
450 }
451 fNumEntries++;
452 }
453
454 void FillEmptyBins()
455 {
456 for (int ch=0; ch<160; ch++)
457 {
458 const auto beg = fStat.begin() + ch*1024;
459 const auto end = beg + 1024;
460
461 double avg = 0;
462 uint32_t num = 0;
463 for (auto it=beg; it!=end; it++)
464 {
465 if (it->second<fNumEntries-0.5)
466 continue;
467
468 avg += it->first / it->second;
469 num++;
470 }
471 avg /= num;
472
473 for (auto it=beg; it!=end; it++)
474 {
475 if (it->second>=fNumEntries-0.5)
476 continue;
477
478 // {
479 // result[i+1].first = *is2;
480 // result[i+1].second = *iw2;
481 // }
482 // else
483 // {
484 it->first = avg*fNumEntries;
485 it->second = fNumEntries;
486 // }
487 }
488 }
489 }
490
491 DrsCalibrateTime GetComplete() const
492 {
493 DrsCalibrateTime rc(*this);
494 rc.FillEmptyBins();
495 return rc;
496 }
497
498 void CalcResult()
499 {
500 for (int ch=0; ch<160; ch++)
501 {
502 const auto beg = fStat.begin() + ch*1024;
503 const auto end = beg + 1024;
504
505 // Calculate mean
506 double s = 0;
507 double w = 0;
508
509 for (auto it=beg; it!=end; it++)
510 {
511 s += it->first;
512 w += it->second;
513 }
514
515 s /= w;
516
517 double sumw = 0;
518 double sumv = 0;
519 int n = 0;
520
521 // Sums about many values are numerically less stable than
522 // just sums over less. So we do the exercise from both sides.
523 for (auto it=beg; it!=end-512; it++, n++)
524 {
525 const double v = it->first;
526 const double w = it->second;
527
528 it->first = sumv>0 ? n*(1-s*sumw/sumv) :0;
529
530 sumw += w;
531 sumv += v;
532 }
533
534 sumw = 0;
535 sumv = 0;
536 n = 1;
537
538 for (auto it=end-1; it!=beg-1+512; it--, n++)
539 {
540 const double v = it->first;
541 const double w = it->second;
542
543 sumw += w;
544 sumv += v;
545
546 it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0;
547 }
548 }
549 }
550
551 DrsCalibrateTime GetResult() const
552 {
553 DrsCalibrateTime rc(*this);
554 rc.CalcResult();
555 return rc;
556 }
557
558 double Offset(uint32_t ch, double pos) const
559 {
560 const auto p = fStat.begin() + ch*1024;
561
562 const uint32_t f = floor(pos);
563
564 const double v0 = p[f].first;
565 const double v1 = p[(f+1)%1024].first;
566
567 return v0 + fmod(pos, 1)*(v1-v0);
568 }
569
570 double Calib(uint32_t ch, double pos) const
571 {
572 return pos-Offset(ch, pos);
573 }
574};
575
576struct DrsCalibration
577{
578 std::vector<int32_t> fOffset;
579 std::vector<int64_t> fGain;
580 std::vector<int64_t> fTrgOff;
581
582 uint64_t fNumOffset;
583 uint64_t fNumGain;
584 uint64_t fNumTrgOff;
585
586 uint32_t fStep;
587 uint16_t fRoi; // Region of interest for trgoff
588 uint16_t fNumTm; // Number of time marker channels in trgoff
589
590// uint16_t fDAC[8];
591
592 DrsCalibration() :
593 fOffset (1440*1024, 0),
594 fGain (1440*1024, 4096),
595 fTrgOff (1600*1024, 0),
596 fNumOffset(1),
597 fNumGain(2000),
598 fNumTrgOff(1),
599 fStep(0)
600 {
601 }
602
603 void Clear()
604 {
605 // Default gain:
606 // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
607 fOffset.assign(1440*1024, 0);
608 fGain.assign (1440*1024, 4096);
609 fTrgOff.assign(1600*1024, 0);
610
611 fNumOffset = 1;
612 fNumGain = 2000;
613 fNumTrgOff = 1;
614
615 fStep = 0;
616 }
617
618 std::string ReadFitsImp(const std::string &str, std::vector<float> &vec)
619 {
620 std::fits file(str);
621 if (!file)
622 {
623 std::ostringstream msg;
624 msg << "Could not open file " << str << ": " << strerror(errno);
625 return msg.str();
626 }
627
628 if (file.GetStr("TELESCOP")!="FACT")
629 {
630 std::ostringstream msg;
631 msg << "Reading " << str << " failed: Not a valid FACT file (TELESCOP not FACT in header)";
632 return msg.str();
633 }
634
635 if (!file.HasKey("STEP"))
636 {
637 std::ostringstream msg;
638 msg << "Reading " << str << " failed: Is not a DRS calib file (STEP not found in header)";
639 return msg.str();
640 }
641
642 if (file.GetNumRows()!=1)
643 {
644 std::ostringstream msg;
645 msg << "Reading " << str << " failed: Number of rows in table is not 1.";
646 return msg.str();
647 }
648
649 fStep = file.GetUInt("STEP");
650 fNumOffset = file.GetUInt("NBOFFSET");
651 fNumGain = file.GetUInt("NBGAIN");
652 fNumTrgOff = file.GetUInt("NBTRGOFF");
653 fRoi = file.GetUInt("NROI");
654 fNumTm = file.HasKey("NTM") ? file.GetUInt("NTM") : 0;
655/*
656 fDAC[0] = file.GetUInt("DAC_A");
657 fDAC[1] = file.GetUInt("DAC_B");
658 fDAC[4] = file.GetUInt("DAC_C");
659*/
660 vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
661
662 float *base = vec.data();
663
664 reinterpret_cast<uint32_t*>(base)[0] = fRoi;
665
666 file.SetPtrAddress("RunNumberBaseline", base+1, 1);
667 file.SetPtrAddress("RunNumberGain", base+2, 1);
668 file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1);
669 file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440);
670 file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440);
671 file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440);
672 file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440);
673 file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440);
674 file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+fRoi*1440, fRoi*1440);
675 if (fNumTm>0)
676 {
677 file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm);
678 file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
679 }
680
681 if (!file.GetNextRow())
682 {
683 std::ostringstream msg;
684 msg << "Reading data from " << str << " failed.";
685 return msg.str();
686 }
687/*
688 fDAC[2] = fDAC[1];
689 fDAC[4] = fDAC[1];
690
691 fDAC[5] = fDAC[4];
692 fDAC[6] = fDAC[4];
693 fDAC[7] = fDAC[4];
694*/
695 fOffset.resize(1024*1440);
696 fGain.resize(1024*1440);
697
698 fTrgOff.resize(fRoi*(1440+fNumTm));
699
700 // Convert back to ADC counts: 256/125 = 4096/2000
701 // Convert back to sum (mean * num_entries)
702 for (int i=0; i<1024*1440; i++)
703 {
704 fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125;
705 fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
706 }
707
708 for (int i=0; i<fRoi*1440; i++)
709 fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
710
711 for (int i=0; i<fRoi*fNumTm; i++)
712 fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
713
714
715 // DAC: 0..2.5V == 0..65535
716 // V-mV: 1000
717 //fNumGain *= 2500*50000;
718 //for (int i=0; i<1024*1440; i++)
719 // fGain[i] *= 65536;
720 if (fStep==0)
721 {
722 for (int i=0; i<1024*1440; i++)
723 fGain[i] = fNumOffset*4096;
724 }
725 else
726 {
727 fNumGain *= 1953125;
728 for (int i=0; i<1024*1440; i++)
729 fGain[i] *= 1024;
730 }
731
732 // Now mark the stored DRS data as "officially valid"
733 // However, this is not thread safe. It only ensures that
734 // this data is not used before it is completely and correctly
735 // read.
736 fStep++;
737
738 return std::string();
739 }
740
741 std::string ReadFitsImp(const std::string &str)
742 {
743 std::vector<float> vec;
744 return ReadFitsImp(str, vec);
745 }
746
747 bool IsValid() { return fStep>2; }
748
749 bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
750 {
751 if (roi!=fRoi)
752 {
753 for (size_t ch=0; ch<1440; ch++)
754 {
755 const size_t pos = ch*roi;
756 const size_t drs = ch*1024;
757
758 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
759 fOffset.data()+drs, fNumOffset,
760 fGain.data() +drs, fNumGain);
761 }
762
763 return false;
764 }
765
766 for (size_t ch=0; ch<1440; ch++)
767 {
768 const size_t pos = ch*fRoi;
769 const size_t drs = ch*1024;
770
771 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
772 fOffset.data()+drs, fNumOffset,
773 fGain.data() +drs, fNumGain,
774 fTrgOff.data()+pos, fNumTrgOff);
775 }
776
777 for (size_t ch=0; ch<fNumTm; ch++)
778 {
779 const size_t pos = (ch+1440)*fRoi;
780 const size_t drs = (ch*9+8)*1024;
781
782 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
783 fOffset.data()+drs, fNumOffset,
784 fGain.data() +drs, fNumGain,
785 fTrgOff.data()+pos, fNumTrgOff);
786 }
787
788 return true;
789 }
790};
791
792#endif
Note: See TracBrowser for help on using the repository browser.