source: branches/FACT++_lidctrl_usb/src/DataCalib.cc@ 18725

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