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

Last change on this file since 12479 was 12479, checked in by tbretz, 13 years ago
Write the run-numbers to position 1,2,3 instead of 0,1,2; 0 is now used for the roi
File size: 16.0 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
346 const std::vector<int64_t> &GetSum() const { return fSum; }
347
348 uint64_t GetNumEntries() const { return fNumEntries; }
349};
350
351struct DrsCalibration
352{
353 std::vector<int32_t> fOffset;
354 std::vector<int64_t> fGain;
355 std::vector<int64_t> fTrgOff;
356
357 uint64_t fNumOffset;
358 uint64_t fNumGain;
359 uint64_t fNumTrgOff;
360
361 uint32_t fStep;
362 uint16_t fRoi; // Region of interest for trgoff
363 uint16_t fNumTm; // Number of time marker channels in trgoff
364
365// uint16_t fDAC[8];
366
367 DrsCalibration() :
368 fOffset (1440*1024, 0),
369 fGain (1440*1024, 4096),
370 fTrgOff (1600*1024, 0),
371 fNumOffset(1),
372 fNumGain(2000),
373 fNumTrgOff(1),
374 fStep(0)
375 {
376 }
377
378 void Clear()
379 {
380 // Default gain:
381 // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
382 fOffset.assign(1440*1024, 0);
383 fGain.assign (1440*1024, 4096);
384 fTrgOff.assign(1600*1024, 0);
385
386 fNumOffset = 1;
387 fNumGain = 2000;
388 fNumTrgOff = 1;
389
390 fStep = 0;
391 }
392
393 std::string ReadFitsImp(const std::string &str, std::vector<float> &vec)
394 {
395 std::fits file(str);
396 if (!file)
397 {
398 std::ostringstream msg;
399 msg << "Could not open file " << str << ": " << strerror(errno);
400 return msg.str();
401 }
402
403 if (file.GetStr("TELESCOP")!="FACT")
404 {
405 std::ostringstream msg;
406 msg << "Reading " << str << " failed: Not a valid FACT file (TELESCOP not FACT in header)";
407 return msg.str();
408 }
409
410 if (!file.HasKey("STEP"))
411 {
412 std::ostringstream msg;
413 msg << "Reading " << str << " failed: Is not a DRS calib file (STEP not found in header)";
414 return msg.str();
415 }
416
417 if (file.GetNumRows()!=1)
418 {
419 std::ostringstream msg;
420 msg << "Reading " << str << " failed: Number of rows in table is not 1.";
421 return msg.str();
422 }
423
424 fStep = file.GetUInt("STEP");
425 fNumOffset = file.GetUInt("NBOFFSET");
426 fNumGain = file.GetUInt("NBGAIN");
427 fNumTrgOff = file.GetUInt("NBTRGOFF");
428 fRoi = file.GetUInt("NROI");
429 fNumTm = file.GetUInt("NTM");
430/*
431 fDAC[0] = file.GetUInt("DAC_A");
432 fDAC[1] = file.GetUInt("DAC_B");
433 fDAC[4] = file.GetUInt("DAC_C");
434*/
435 vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4);
436
437 float *base = vec.data();
438
439 reinterpret_cast<uint32_t*>(base)[0] = fRoi;
440
441 file.SetPtrAddress("RunNumberBaseline", base+1, 1);
442 file.SetPtrAddress("RunNumberGain", base+2, 1);
443 file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1);
444 file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440);
445 file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440);
446 file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440);
447 file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440);
448 file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440);
449 file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+ fRoi*1440, fRoi*1440);
450 file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm);
451 file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm);
452
453 if (!file.GetNextRow())
454 {
455 std::ostringstream msg;
456 msg << "Reading data from " << str << " failed.";
457 return msg.str();
458 }
459/*
460 fDAC[2] = fDAC[1];
461 fDAC[4] = fDAC[1];
462
463 fDAC[5] = fDAC[4];
464 fDAC[6] = fDAC[4];
465 fDAC[7] = fDAC[4];
466*/
467 fOffset.resize(1024*1440);
468 fGain.resize(1024*1440);
469
470 fTrgOff.resize(fRoi*(1440+fNumTm));
471
472 // Convert back to ADC counts: 256/125 = 4096/2000
473 // Convert back to sum (mean * num_entries)
474 for (int i=0; i<1024*1440; i++)
475 {
476 fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125;
477 fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125;
478 }
479
480 for (int i=0; i<fRoi*1440; i++)
481 fTrgOff[i] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+4]/125;
482
483 for (int i=0; i<fRoi*fNumTm; i++)
484 fTrgOff[i+1440*fRoi] = fNumOffset*fNumTrgOff*256*base[i+1024*1440*4+2*fRoi*1440+4]/125;
485
486
487 // DAC: 0..2.5V == 0..65535
488 // V-mV: 1000
489 //fNumGain *= 2500*50000;
490 //for (int i=0; i<1024*1440; i++)
491 // fGain[i] *= 65536;
492 if (fStep==0)
493 {
494 for (int i=0; i<1024*1440; i++)
495 fGain[i] = fNumOffset*4096;
496 }
497 else
498 {
499 fNumGain *= 1953125;
500 for (int i=0; i<1024*1440; i++)
501 fGain[i] *= 1024;
502 }
503
504 // Now mark the stored DRS data as "officially valid"
505 // However, this is not thread safe. It only ensures that
506 // this data is not used before it is completely and correctly
507 // read.
508 fStep++;
509
510 return std::string();
511 }
512
513 std::string ReadFitsImp(const std::string &str)
514 {
515 std::vector<float> vec;
516 return ReadFitsImp(str, vec);
517 }
518
519 bool IsValid() { return fStep>2; }
520
521 bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
522 {
523 if (roi!=fRoi)
524 {
525 for (size_t ch=0; ch<1440; ch++)
526 {
527 const size_t pos = ch*roi;
528 const size_t drs = ch*1024;
529
530 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
531 fOffset.data()+drs, fNumOffset,
532 fGain.data() +drs, fNumGain);
533 }
534
535 return false;
536 }
537
538 for (size_t ch=0; ch<1440; ch++)
539 {
540 const size_t pos = ch*fRoi;
541 const size_t drs = ch*1024;
542
543 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
544 fOffset.data()+drs, fNumOffset,
545 fGain.data() +drs, fNumGain,
546 fTrgOff.data()+pos, fNumTrgOff);
547 }
548
549 for (size_t ch=0; ch<fNumTm; ch++)
550 {
551 const size_t pos = (ch+1440)*fRoi;
552 const size_t drs = (ch*9+8)*1024;
553
554 DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi,
555 fOffset.data()+drs, fNumOffset,
556 fGain.data() +drs, fNumGain,
557 fTrgOff.data()+pos, fNumTrgOff);
558 }
559
560 return true;
561 }
562};
563
564#endif
Note: See TracBrowser for help on using the repository browser.