1 | class CalibData
|
---|
2 | {
|
---|
3 | protected:
|
---|
4 | uint64_t fNumEntries;
|
---|
5 |
|
---|
6 | size_t fNumSamples;
|
---|
7 | size_t fNumChannels;
|
---|
8 |
|
---|
9 | vector<int64_t> fSum;
|
---|
10 | vector<int64_t> fSum2;
|
---|
11 |
|
---|
12 | /*
|
---|
13 | vector<map<int16_t, uint8_t> > fMedian;
|
---|
14 | vector<int16_t> fResult;
|
---|
15 | */
|
---|
16 | public:
|
---|
17 | CalibData() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { }
|
---|
18 | void Reset()
|
---|
19 | {
|
---|
20 | fNumEntries = 0;
|
---|
21 | fNumSamples = 0;
|
---|
22 | fNumChannels = 0;
|
---|
23 |
|
---|
24 | fSum.clear();
|
---|
25 | fSum2.clear();
|
---|
26 | }
|
---|
27 |
|
---|
28 | void InitSize(uint16_t channels, uint16_t samples)
|
---|
29 | {
|
---|
30 | fNumChannels = channels;
|
---|
31 | fNumSamples = samples;
|
---|
32 |
|
---|
33 | fSum.resize(samples*channels);
|
---|
34 | fSum2.resize(samples*channels);
|
---|
35 |
|
---|
36 | //fMedian.resize(40*9*1024);
|
---|
37 | }
|
---|
38 | /*
|
---|
39 | continue;
|
---|
40 |
|
---|
41 | if (ch<40*9)
|
---|
42 | {
|
---|
43 | fMedian[abs][val[rel]]++;
|
---|
44 |
|
---|
45 | int n= 8;
|
---|
46 | if (fNumEntries>0 && fNumEntries%(n*100)==0)
|
---|
47 | {
|
---|
48 | fResult.resize(40*9*1024);
|
---|
49 |
|
---|
50 | uint16_t sum = 0;
|
---|
51 | for (map<int16_t, uint8_t>::const_iterator it=fMedian[abs].begin();
|
---|
52 | it!=fMedian[abs].end(); it++)
|
---|
53 | {
|
---|
54 | map<int16_t, uint8_t>::const_iterator is = it;
|
---|
55 | is++;
|
---|
56 | if (is==fMedian[abs].end())
|
---|
57 | break;
|
---|
58 |
|
---|
59 | sum += it->second;
|
---|
60 |
|
---|
61 | double med = 0;
|
---|
62 |
|
---|
63 | med += int64_t(it->second)*int64_t(it->first);
|
---|
64 | med += int64_t(is->second)*int64_t(is->first);
|
---|
65 | med /= int64_t(it->second)+int64_t(is->second);
|
---|
66 |
|
---|
67 | if (sum==n*50)
|
---|
68 | {
|
---|
69 | fResult[abs] = it->first;
|
---|
70 | cout << ch << " MED+(50): " << it->first << endl;
|
---|
71 | }
|
---|
72 | if (sum<n*50 && sum+is->second>n*50)
|
---|
73 | {
|
---|
74 | fResult[abs] = med;
|
---|
75 | cout << ch << " MED-(50): " << med << endl;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | cout << ch << " AVG=" << float(fSum[abs])/fNumEntries << endl;
|
---|
79 | fMedian[abs].clear();
|
---|
80 | }
|
---|
81 | } // -2029 -2012 -2003 -1996 -1990 // -1955
|
---|
82 | */
|
---|
83 |
|
---|
84 | void AddRel(const int16_t *val, const int16_t *start)
|
---|
85 | {
|
---|
86 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
87 | {
|
---|
88 | const int16_t spos = start[ch];
|
---|
89 | if (spos<0)
|
---|
90 | continue;
|
---|
91 |
|
---|
92 | const size_t pos = ch*fNumSamples;
|
---|
93 |
|
---|
94 | for (size_t i=0; i<fNumSamples; i++)
|
---|
95 | {
|
---|
96 | // Value is relative to trigger
|
---|
97 | // Abs is corresponding index relative to DRS pipeline
|
---|
98 | const size_t rel = pos + i;
|
---|
99 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
100 |
|
---|
101 | const int64_t v = val[rel];
|
---|
102 |
|
---|
103 | fSum[abs] += v;
|
---|
104 | fSum2[abs] += v*v;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | fNumEntries++;
|
---|
109 | }
|
---|
110 |
|
---|
111 | void AddRel(const int16_t *val, const int16_t *start,
|
---|
112 | const int32_t *offset, const uint32_t scale)
|
---|
113 | {
|
---|
114 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
115 | {
|
---|
116 | const int16_t spos = start[ch];
|
---|
117 | if (spos<0)
|
---|
118 | continue;
|
---|
119 |
|
---|
120 | const size_t pos = ch*fNumSamples;
|
---|
121 |
|
---|
122 | for (size_t i=0; i<fNumSamples; i++)
|
---|
123 | {
|
---|
124 | // Value is relative to trigger
|
---|
125 | // Offset is relative to DRS pipeline
|
---|
126 | // Abs is corresponding index relative to DRS pipeline
|
---|
127 | const size_t rel = pos + i;
|
---|
128 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
129 |
|
---|
130 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
131 |
|
---|
132 | fSum[abs] += v;
|
---|
133 | fSum2[abs] += v*v;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | fNumEntries++;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void AddAbs(const int16_t *val, const int16_t *start,
|
---|
141 | const int32_t *offset, const uint32_t scale)
|
---|
142 | {
|
---|
143 | for (size_t ch=0; ch<fNumChannels; ch++)
|
---|
144 | {
|
---|
145 | const int16_t spos = start[ch];
|
---|
146 | if (spos<0)
|
---|
147 | continue;
|
---|
148 |
|
---|
149 | const size_t pos = ch*fNumSamples;
|
---|
150 |
|
---|
151 | for (size_t i=0; i<fNumSamples; i++)
|
---|
152 | {
|
---|
153 | // Value is relative to trigger
|
---|
154 | // Offset is relative to DRS pipeline
|
---|
155 | // Abs is corresponding index relative to DRS pipeline
|
---|
156 | const size_t rel = pos + i;
|
---|
157 | const size_t abs = pos + (spos+i)%fNumSamples;
|
---|
158 |
|
---|
159 | const int64_t v = int64_t(val[rel])*scale-offset[abs];
|
---|
160 |
|
---|
161 | fSum[rel] += v;
|
---|
162 | fSum2[rel] += v*v;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | fNumEntries++;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void Apply(int16_t *val, const int16_t *start, uint32_t roi,
|
---|
170 | const int32_t *offset, const uint32_t scaleabs,
|
---|
171 | const int32_t *gain, const uint32_t scalegain,
|
---|
172 | const int32_t *trgoff, const uint32_t scalerel)
|
---|
173 | {
|
---|
174 | for (size_t ch=0; ch<1440; ch++)
|
---|
175 | {
|
---|
176 | const int16_t spos = start[ch];
|
---|
177 | if (spos<0)
|
---|
178 | continue;
|
---|
179 |
|
---|
180 | const size_t pos = ch*roi;
|
---|
181 |
|
---|
182 | for (size_t i=0; i<roi; i++)
|
---|
183 | {
|
---|
184 | // Value is relative to trigger
|
---|
185 | // Offset is relative to DRS pipeline
|
---|
186 | // Abs is corresponding index relative to DRS pipeline
|
---|
187 | const size_t rel = pos + i;
|
---|
188 | const size_t abs = pos + (spos+i)%1024;
|
---|
189 |
|
---|
190 | const int64_t v =
|
---|
191 | + int64_t(val[rel]) *scaleabs*scalerel
|
---|
192 | - int64_t(trgoff[rel])*scaleabs
|
---|
193 | - int64_t(offset[abs])*scalerel
|
---|
194 | ;
|
---|
195 |
|
---|
196 | const int64_t div = int64_t(gain[abs])*scaleabs*scalerel;
|
---|
197 |
|
---|
198 | val[rel] = (v*scalegain)/div;
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | static void Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi,
|
---|
204 | const int32_t *offset, const uint32_t scaleabs,
|
---|
205 | const int32_t *gain, const uint32_t scalegain,
|
---|
206 | const int32_t *trgoff, const uint32_t scalerel)
|
---|
207 | {
|
---|
208 | for (size_t ch=0; ch<1440; ch++)
|
---|
209 | {
|
---|
210 | const size_t pos = ch*roi;
|
---|
211 |
|
---|
212 | const int16_t spos = start[ch];
|
---|
213 | if (spos<0)
|
---|
214 | {
|
---|
215 | memset(vec+pos, 0, roi);
|
---|
216 | continue;
|
---|
217 | }
|
---|
218 |
|
---|
219 | for (size_t i=0; i<roi; i++)
|
---|
220 | {
|
---|
221 | // Value is relative to trigger
|
---|
222 | // Offset is relative to DRS pipeline
|
---|
223 | // Abs is corresponding index relative to DRS pipeline
|
---|
224 | const size_t rel = pos + i;
|
---|
225 | const size_t abs = pos + (spos+i)%1024;
|
---|
226 |
|
---|
227 | const int64_t v =
|
---|
228 | + int64_t(val[rel]) *scaleabs*scalerel
|
---|
229 | - int64_t(trgoff[rel])*scaleabs
|
---|
230 | - int64_t(offset[abs])*scalerel
|
---|
231 | ;
|
---|
232 |
|
---|
233 | const int64_t div = int64_t(gain[abs])*scaleabs*scalerel;
|
---|
234 |
|
---|
235 | vec[rel] = double(v*scalegain)/div;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /*
|
---|
241 | static void Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi)
|
---|
242 | {
|
---|
243 | for (size_t ch=0; ch<1440; ch++)
|
---|
244 | {
|
---|
245 | const size_t pos = ch*roi;
|
---|
246 |
|
---|
247 | const int16_t spos = start[ch];
|
---|
248 | if (spos<0)
|
---|
249 | {
|
---|
250 | memset(vec+pos, 0, roi);
|
---|
251 | continue;
|
---|
252 | }
|
---|
253 |
|
---|
254 | for (size_t i=0; i<roi; i++)
|
---|
255 | vec[pos+i] = float(val[pos+i])/2;
|
---|
256 | }
|
---|
257 | }*/
|
---|
258 |
|
---|
259 | pair<vector<double>,vector<double> > GetSampleStats() const
|
---|
260 | {
|
---|
261 | if (fNumEntries==0)
|
---|
262 | return make_pair(vector<double>(),vector<double>());
|
---|
263 |
|
---|
264 | vector<double> mean(fSum.size());
|
---|
265 | vector<double> error(fSum.size());
|
---|
266 |
|
---|
267 | vector<int64_t>::const_iterator it = fSum.begin();
|
---|
268 | vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
269 | vector<double>::iterator im = mean.begin();
|
---|
270 | vector<double>::iterator ie = error.begin();
|
---|
271 |
|
---|
272 | while (it!=fSum.end())
|
---|
273 | {
|
---|
274 | *im = /*cnt<fResult.size() ? fResult[cnt] :*/ double(*it)/fNumEntries;
|
---|
275 | *ie = sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
276 |
|
---|
277 | im++;
|
---|
278 | ie++;
|
---|
279 | it++;
|
---|
280 | i2++;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | /*
|
---|
285 | valarray<double> ...
|
---|
286 |
|
---|
287 | mean /= fNumEntries;
|
---|
288 | error = sqrt(error/fNumEntries - mean*mean);
|
---|
289 | */
|
---|
290 |
|
---|
291 | return make_pair(mean, error);
|
---|
292 | }
|
---|
293 |
|
---|
294 | void GetSampleStats(float *ptr, float scale) const
|
---|
295 | {
|
---|
296 | if (fNumEntries==0)
|
---|
297 | {
|
---|
298 | memset(ptr, 0, sizeof(float)*1024*1440);
|
---|
299 | return;
|
---|
300 | }
|
---|
301 |
|
---|
302 | vector<int64_t>::const_iterator it = fSum.begin();
|
---|
303 | vector<int64_t>::const_iterator i2 = fSum2.begin();
|
---|
304 |
|
---|
305 | while (it!=fSum.end())
|
---|
306 | {
|
---|
307 | *ptr = scale*double(*it)/fNumEntries;
|
---|
308 | *(ptr+1024*1440) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries;
|
---|
309 |
|
---|
310 | ptr++;
|
---|
311 | it++;
|
---|
312 | i2++;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | const vector<int64_t> &GetSum() const { return fSum; }
|
---|
317 |
|
---|
318 | uint64_t GetNumEntries() const { return fNumEntries; }
|
---|
319 | };
|
---|
320 |
|
---|
321 |
|
---|
322 | /*
|
---|
323 | Int_t TH1::MyFill(Int_t x, Double_t w)
|
---|
324 | {
|
---|
325 | fEntries++;
|
---|
326 |
|
---|
327 | fArrayBin[x] += w;
|
---|
328 |
|
---|
329 | fSumw2.fArray[x] += w*w
|
---|
330 | fTsumw += w;
|
---|
331 | fTsumw2 += w*w;
|
---|
332 | fTsumwx += w*x;
|
---|
333 | fTsumwx2 += w*x*x;
|
---|
334 | return bin;
|
---|
335 | }
|
---|
336 | */
|
---|