source: trunk/FACT++/src/DataCalib.cc@ 14990

Last change on this file since 14990 was 14686, checked in by tbretz, 12 years ago
For convenience, set Qos to step.
File size: 10.1 KB
Line 
1#include "DataCalib.h"
2
3#include "FAD.h"
4#include "FitsFile.h"
5#include "DimDescriptionService.h"
6
7#include "externals/fits.h"
8
9using namespace std;
10
11DrsCalibration DataCalib::fData;
12bool DataCalib::fProcessing = false;
13vector<float> DataCalib::fStats(1440*1024*6+160*1024*2+4);
14
15void DataCalib::Restart()
16{
17 fData.Clear();
18
19 reinterpret_cast<uint32_t*>(fStats.data())[0] = 0;
20 reinterpret_cast<uint32_t*>(fStats.data())[1] = 0;
21 reinterpret_cast<uint32_t*>(fStats.data())[2] = 0;
22 reinterpret_cast<uint32_t*>(fStats.data())[3] = 0;
23
24 int i=0;
25 while (i<1024*1440*2+4) // Set mean and RMS to 0
26 fStats[i++] = 0;
27 while (i<1024*1440*3+4)
28 fStats[i++] = 2000./4096; // Set mean to 0.5
29 while (i<1440*1024*6+160*1024*2+4)
30 fStats[i++] = 0; // Set everything else to 0
31
32 fProcessing = false;
33}
34
35bool DataCalib::ResetTrgOff(DimDescribedService &dim, DimDescribedService &runs)
36{
37 if (fData.fStep!=3)
38 return false;
39
40 for (int i=1024*1440*4+4; i<1440*1024*6+160*1024*2+4; i++)
41 fStats[i] = 0;
42
43 reinterpret_cast<uint32_t*>(fStats.data())[0] = 0;
44 reinterpret_cast<uint32_t*>(fStats.data())[3] = 0;
45
46 fData.fStep = 1;
47 Update(dim, runs);
48 fData.fStep = 2;
49
50 return true;
51}
52
53void DataCalib::Update(DimDescribedService &dim, DimDescribedService &runs)
54{
55 const uint16_t roi = fData.fRoi;
56 const uint16_t ntm = fData.fNumTm;
57
58 vector<float> buf(1440*1024*6+160*1024*2+4);
59
60 memcpy(buf.data(), fStats.data(), (4*1024*1440+4)*sizeof(float));
61
62 for (int i=0; i<1440; i++)
63 {
64 memcpy(buf.data()+4+1440*1024*4 + i*1024, fStats.data()+4 + 4*1024*1440 + roi*i, roi*sizeof(float));
65 memcpy(buf.data()+4+1440*1024*5 + i*1024, fStats.data()+4 + 4*1024*1440 + roi*1440 + roi*i, roi*sizeof(float));
66 }
67
68 /*
69 for (int i=0; i<ntm; i++)
70 {
71 memcpy(buf.data()+4+1440*1024*6 + i*1024, fStats.data()+4 + 4*1024*1440 + 2*roi*1440, roi*sizeof(float));
72 memcpy(buf.data()+4+1440*1024*6+160*1024 + i*1024, fStats.data()+4 + 4*1024*1440 + 2*roi*1440+i*roi, roi*sizeof(float));
73 }*/
74
75#warning Time marker channels not sent
76
77 dim.setQuality(fData.fStep);
78 dim.Update(buf);
79
80 runs.setQuality(fData.fStep);
81 runs.setData(buf.data(), 4*sizeof(uint32_t));
82 runs.Update();
83}
84
85bool DataCalib::Open(const RUN_HEAD* h, const FAD::RunDescription &d)
86{
87 if (h->NPix != 1440)
88 {
89 Error("Number of pixels in header for run "+to_string(GetRunId())+" not 1440.");
90 return false;
91 }
92
93 if (fProcessing)
94 {
95 Warn("Previous DRS calibration run not yet finished (current run "+to_string(GetRunId())+")");
96 return false;
97 }
98
99 if (fData.fStep==3)
100 {
101 Warn("DRS Calibration already finished before current run "+to_string(GetRunId())+"... please restart!");
102 return false;
103 }
104
105 if (fData.fStep!=2 && h->Nroi != 1024)
106 {
107 ostringstream msg;
108 msg << "Region of interest of run " << GetRunId() << " not 1024, but " << h->Nroi << " in step " << fData.fStep << " ... as it ought to be.";
109 Error(msg);
110 return false;
111 }
112
113 vector<uint16_t> dac(8);
114/*
115 // We don't check consistency over several boards because this is done
116 // by the eventCheck routine already
117 for (int i=0; i<h->NBoard; i++)
118 {
119 const PEVNT_HEADER &hh = h->FADhead[i];
120
121 if (hh.start_package_flag==0)
122 continue;
123
124 for (int j=0; j<8; j++)
125 dac[j] = hh.dac[j];
126
127 break;
128 }
129
130 for (int i=1; i<7; i++)
131 {
132 if (i==3 || dac[i]==dac[i+1])
133 continue;
134
135 ostringstream msg;
136 msg << "Values of DAC" << i << " (" << dac[i] << ") and DAC" << i+1 <<" (" << dac[i+1] << ") do not match... cannot take DRS calibration!";
137 fMsg.Error(msg);
138 return false;
139 }
140
141 if (fData.fStep>0)
142 {
143 for (int j=0; j<8; j++)
144 {
145 if (fData.fDAC[j]==dac[j])
146 continue;
147
148 ostringstream msg;
149 msg << "DAC value from previous run (DAC" << j << "=" << fData.fDAC[j] << ") and current run ";
150 msg << "(DAC" << j << "=" << dac[j] << ") inconsistent... cannot take DRS calibration!";
151 fMsg.Error(msg);
152 return false;
153 }
154 }
155
156 memcpy(fData.fDAC, dac.data(), 8*sizeof(uint16_t));
157*/
158 fProcessing = true;
159
160 const bool hastm = h->Nroi<=512 && h->NroiTM>=2*h->Nroi;
161
162 Reset();
163 InitSize(hastm ? 1600 : 1440, h->Nroi);
164
165 fData.fRoi = fNumSamples;
166 fData.fNumTm = hastm ? 160 : 0;
167
168 return DataWriteFits2::Open(h, d);
169}
170
171bool DataCalib::WriteEvt(EVENT *e)
172{
173 // FIXME: SET StartPix to 0 if StartPix is -1
174
175 if (fData.fStep==0)
176 {
177 AddRel(e->Adc_Data, e->StartPix);
178 }
179 if (fData.fStep==1)
180 {
181 AddRel(e->Adc_Data, e->StartPix, fData.fOffset.data(), fData.fNumOffset);
182 }
183 if (fData.fStep==2)
184 {
185 AddAbs(e->Adc_Data, e->StartPix, fData.fOffset.data(), fData.fNumOffset);
186 }
187
188 return DataWriteFits2::WriteEvt(e);
189}
190
191bool DataCalib::ReadFits(const string &str, MessageImp &msg)
192{
193 if (fProcessing)
194 {
195 msg.Error("Reading "+str+" failed: DRS calibration in process.");
196 return false;
197 }
198
199 try
200 {
201 const string txt = fData.ReadFitsImp(str, fStats);
202 if (txt.empty())
203 return true;
204
205 msg.Error(txt);
206 return false;
207 }
208 catch (const runtime_error &e)
209 {
210 msg.Error("Exception reading "+str+": "+e.what());
211 return false;
212 }
213}
214
215void DataCalib::WriteFitsImp(const string &filename, const vector<float> &vec) const
216{
217 const uint16_t roi = fData.fRoi;
218 const uint16_t ntm = fData.fNumTm;
219
220 const size_t n = 1440*1024*4 + 1440*roi*2 + ntm*roi*2 + 3;
221
222 // The vector has a fixed size
223 //if (vec.size()!=n+1)
224 // throw runtime_error("Size of vector does not match region-of-interest");
225
226 ofits file(filename.c_str());
227
228 file.AddColumnInt("RunNumberBaseline");
229 file.AddColumnInt("RunNumberGain");
230 file.AddColumnInt("RunNumberTriggerOffset");
231
232 file.AddColumnFloat(1024*1440, "BaselineMean", "mV");
233 file.AddColumnFloat(1024*1440, "BaselineRms", "mV");
234 file.AddColumnFloat(1024*1440, "GainMean", "mV");
235 file.AddColumnFloat(1024*1440, "GainRms", "mV");
236 file.AddColumnFloat( roi*1440, "TriggerOffsetMean", "mV");
237 file.AddColumnFloat( roi*1440, "TriggerOffsetRms", "mV");
238 file.AddColumnFloat( roi*ntm, "TriggerOffsetTMMean", "mV");
239 file.AddColumnFloat( roi*ntm, "TriggerOffsetTMRms", "mV");
240
241 DataWriteFits2::WriteDefaultKeys(file);
242
243 file.SetInt("STEP", fData.fStep, "");
244
245 file.SetInt("ADCRANGE", 2000, "Dynamic range of the ADC in mV");
246 file.SetInt("DACRANGE", 2500, "Dynamic range of the DAC in mV");
247 file.SetInt("ADC", 12, "Resolution of ADC in bits");
248 file.SetInt("DAC", 16, "Resolution of DAC in bits");
249 file.SetInt("NPIX", 1440, "Number of channels in the camera");
250 file.SetInt("NTM", ntm, "Number of time marker channels");
251 file.SetInt("NROI", roi, "Region of interest");
252
253 file.SetInt("NBOFFSET", fData.fNumOffset, "Num of entries for offset calibration");
254 file.SetInt("NBGAIN", fData.fNumGain/1953125, "Num of entries for gain calibration");
255 file.SetInt("NBTRGOFF", fData.fNumTrgOff, "Num of entries for trigger offset calibration");
256
257 // file.WriteKeyNT("DAC_A", fData.fDAC[0], "Level of DAC 0 in DAC counts") ||
258 // file.WriteKeyNT("DAC_B", fData.fDAC[1], "Leval of DAC 1-3 in DAC counts") ||
259 // file.WriteKeyNT("DAC_C", fData.fDAC[4], "Leval of DAC 4-7 in DAC counts") ||
260
261 file.WriteTableHeader("DrsCalibration");
262 file.WriteRow(vec.data()+1, n*sizeof(float));
263}
264
265bool DataCalib::Close(RUN_TAIL *tail)
266{
267 if (fNumEntries==0)
268 {
269 ostringstream str;
270 str << "DRS calibration run (run=" << GetRunId() << ", step=" << fData.fStep << ", roi=" << fData.fRoi << ") has 0 events.";
271 Warn(str);
272 }
273
274 if (fData.fStep==0)
275 {
276 fData.fOffset.assign(fSum.begin(), fSum.end());
277 fData.fNumOffset = fNumEntries;
278
279 for (int i=0; i<1024*1440; i++)
280 fData.fGain[i] = 4096*fNumEntries;
281
282 // Scale ADC data from 12bit to 2000mV
283 GetSampleStats(fStats.data()+4, 2000./4096);
284 reinterpret_cast<uint32_t*>(fStats.data())[1] = GetRunId();;
285 }
286 if (fData.fStep==1)
287 {
288 fData.fGain.assign(fSum.begin(), fSum.end());
289 fData.fNumGain = fNumEntries;
290
291 // DAC: 0..2.5V == 0..65535 2500*50000 625*50000 625*3125
292 // V-mV: 1000 ---------- --------- --------
293 //fNumGain *= 2500*50000; 65536 16384 1024
294 //for (int i=0; i<1024*1440; i++)
295 // fGain[i] *= 65536;
296 fData.fNumGain *= 1953125;
297 for (int i=0; i<1024*1440; i++)
298 fData.fGain[i] *= 1024;
299
300 // Scale ADC data from 12bit to 2000mV
301 GetSampleStats(fStats.data()+1024*1440*2+4, 2000./4096/fData.fNumOffset);//0.5);
302 reinterpret_cast<uint32_t*>(fStats.data())[2] = GetRunId();;
303 }
304 if (fData.fStep==2)
305 {
306 fData.fTrgOff.assign(fSum.begin(), fSum.end());
307 fData.fNumTrgOff = fNumEntries;
308
309 // Scale ADC data from 12bit to 2000mV
310 GetSampleStats(fStats.data()+1024*1440*4+4, 2000./4096/fData.fNumOffset);//0.5);
311 reinterpret_cast<uint32_t*>(fStats.data())[0] = fNumSamples;
312 reinterpret_cast<uint32_t*>(fStats.data())[3] = GetRunId();
313 }
314
315 if (fData.fStep<=2)
316 {
317 const string filename = FormFileName("drs.fits");
318 try
319 {
320 WriteFitsImp(filename, fStats);
321
322 ostringstream str;
323 str << "Wrote DRS calibration data (run=" << GetRunId() << ", step=" << fData.fStep << ", roi=" << fData.fRoi << ") to '" << filename << "'";
324 Info(str);
325 }
326 catch (const exception &e)
327 {
328 Error("Exception writing run "+to_string(GetRunId())+" '"+filename+"': "+e.what());
329 }
330 }
331
332 Update(fDim, fDimRuns);
333
334 fData.fStep++;
335
336 fProcessing = false;
337
338 return DataWriteFits2::Close(tail);
339}
Note: See TracBrowser for help on using the repository browser.