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

Last change on this file since 13220 was 12809, checked in by tbretz, 13 years ago
Added a compile time warning so that we are not surprised that some stuff is not yet implemented when we make use of the special time marker channel one day.
File size: 10.0 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)
36{
37 if (fData.fStep!=3)
38 return false;
39
40 for (int i=1024*1440*3+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);
48 fData.fStep = 2;
49
50 return true;
51}
52
53void DataCalib::Update(DimDescribedService &dim)
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.Update(buf);
78}
79
80bool DataCalib::Open(const RUN_HEAD* h, const FAD::RunDescription &d)
81{
82 if (h->NPix != 1440)
83 {
84 fMsg.Error("Number of pixels in header not 1440.");
85 return false;
86 }
87
88 if (fProcessing)
89 {
90 fMsg.Warn("Previous DRS calibration run not yet finished!");
91 return false;
92 }
93
94 if (fData.fStep==3)
95 {
96 fMsg.Warn("DRS Calibration already finished... please restart!");
97 return false;
98 }
99
100 if (fData.fStep!=2 && h->Nroi != 1024)
101 {
102 ostringstream msg;
103 msg << "Region of interest not 1024, but " << h->Nroi << " in step " << fData.fStep << " ... as it ought to be.";
104 fMsg.Error(msg);
105 return false;
106 }
107
108 vector<uint16_t> dac(8);
109/*
110 // We don't check consistency over several boards because this is done
111 // by the eventCheck routine already
112 for (int i=0; i<h->NBoard; i++)
113 {
114 const PEVNT_HEADER &hh = h->FADhead[i];
115
116 if (hh.start_package_flag==0)
117 continue;
118
119 for (int j=0; j<8; j++)
120 dac[j] = hh.dac[j];
121
122 break;
123 }
124
125 for (int i=1; i<7; i++)
126 {
127 if (i==3 || dac[i]==dac[i+1])
128 continue;
129
130 ostringstream msg;
131 msg << "Values of DAC" << i << " (" << dac[i] << ") and DAC" << i+1 <<" (" << dac[i+1] << ") do not match... cannot take DRS calibration!";
132 fMsg.Error(msg);
133 return false;
134 }
135
136 if (fData.fStep>0)
137 {
138 for (int j=0; j<8; j++)
139 {
140 if (fData.fDAC[j]==dac[j])
141 continue;
142
143 ostringstream msg;
144 msg << "DAC value from previous run (DAC" << j << "=" << fData.fDAC[j] << ") and current run ";
145 msg << "(DAC" << j << "=" << dac[j] << ") inconsistent... cannot take DRS calibration!";
146 fMsg.Error(msg);
147 return false;
148 }
149 }
150
151 memcpy(fData.fDAC, dac.data(), 8*sizeof(uint16_t));
152*/
153 fProcessing = true;
154
155 const bool hastm = h->Nroi<=512 && h->NroiTM>=2*h->Nroi;
156
157 Reset();
158 InitSize(hastm ? 1600 : 1440, h->Nroi);
159
160 fData.fRoi = fNumSamples;
161 fData.fNumTm = hastm ? 160 : 0;
162
163 return DataWriteFits::Open(h, d);
164}
165
166bool DataCalib::WriteEvt(EVENT *e)
167{
168 // FIXME: SET StartPix to 0 if StartPix is -1
169
170 if (fData.fStep==0)
171 {
172 AddRel(e->Adc_Data, e->StartPix);
173 }
174 if (fData.fStep==1)
175 {
176 AddRel(e->Adc_Data, e->StartPix, fData.fOffset.data(), fData.fNumOffset);
177 }
178 if (fData.fStep==2)
179 {
180 AddAbs(e->Adc_Data, e->StartPix, fData.fOffset.data(), fData.fNumOffset);
181 }
182
183 return DataWriteFits::WriteEvt(e);
184}
185
186bool DataCalib::ReadFits(const string &str, MessageImp &msg)
187{
188 if (fProcessing)
189 {
190 msg.Error("Reading "+str+" failed: DRS calibration in process.");
191 return false;
192 }
193
194 try
195 {
196 const string txt = fData.ReadFitsImp(str, fStats);
197 if (txt.empty())
198 return true;
199
200 msg.Error(txt);
201 return false;
202 }
203 catch (const runtime_error &e)
204 {
205 msg.Error("Exception reading "+str+": "+e.what());
206 return false;
207 }
208}
209
210void ReverseCopy(const void *src, void *dest)
211{
212 reverse_copy(reinterpret_cast<const char*>(src),
213 reinterpret_cast<const char*>(src)+sizeof(float),
214 reinterpret_cast<char*>(dest));
215}
216
217void DataCalib::WriteFits()
218{
219#ifdef HAVE_FITS
220 FitsFile file(fMsg);
221
222 const uint16_t roi = fData.fRoi;
223 const uint16_t ntm = fData.fNumTm;
224
225 file.AddColumn('I', "RunNumberBaseline");
226 file.AddColumn('I', "RunNumberGain");
227 file.AddColumn('I', "RunNumberTriggerOffset");
228 file.AddColumn('F', "BaselineMean", 1024*1440, "mV");
229 file.AddColumn('F', "BaselineRms", 1024*1440, "mV");
230 file.AddColumn('F', "GainMean", 1024*1440, "mV");
231 file.AddColumn('F', "GainRms", 1024*1440, "mV");
232 file.AddColumn('F', "TriggerOffsetMean", roi*1440, "mV");
233 file.AddColumn('F', "TriggerOffsetRms", roi*1440, "mV");
234 file.AddColumn('F', "TriggerOffsetTMMean", roi*ntm, "mV");
235 file.AddColumn('F', "TriggerOffsetTMRms", roi*ntm, "mV");
236
237 const string filename = FormFileName("drs.fits");
238
239 if (!file.OpenFile(filename))
240 return;
241
242 if (!file.OpenTable("DrsCalibration"))
243 return;
244
245 if (!file.WriteDefaultKeys("fadctrl"))
246 return;
247
248 if (!file.WriteKeyNT("STEP", fData.fStep, "") ||
249 !file.WriteKeyNT("ADCRANGE", 2000, "Dynamic range of the ADC in mV") ||
250 !file.WriteKeyNT("DACRANGE", 2500, "Dynamic range of the DAC in mV") ||
251 !file.WriteKeyNT("ADC", 12, "Resolution of ADC in bits") ||
252 !file.WriteKeyNT("DAC", 16, "Resolution of DAC in bits") ||
253// !file.WriteKeyNT("DAC_A", fData.fDAC[0], "Level of DAC 0 in DAC counts") ||
254// !file.WriteKeyNT("DAC_B", fData.fDAC[1], "Leval of DAC 1-3 in DAC counts") ||
255// !file.WriteKeyNT("DAC_C", fData.fDAC[4], "Leval of DAC 4-7 in DAC counts") ||
256 !file.WriteKeyNT("NBOFFSET", fData.fNumOffset, "Number of entries for offset calibration") ||
257 !file.WriteKeyNT("NBGAIN", fData.fNumGain/1953125, "Number of entries for gain calibration") ||
258 !file.WriteKeyNT("NBTRGOFF", fData.fNumTrgOff, "Number of entries for trigger offset calibration") ||
259 !file.WriteKeyNT("NPIX", 1440, "Number of channels in the camera") ||
260 !file.WriteKeyNT("NTM", ntm, "Number of time marker channels") ||
261 !file.WriteKeyNT("NROI", roi, "Region of interest")
262 )
263 return;
264
265 vector<char> buf(fStats.size()*sizeof(float));
266
267 char *src = reinterpret_cast<char*>(fStats.data());
268 char *end = reinterpret_cast<char*>(fStats.data()+fStats.size());
269 char *dest = buf.data();
270
271 while (src<end)
272 {
273 reverse_copy(src, src+sizeof(float), dest);
274 src += sizeof(float);
275 dest += sizeof(float);
276 }
277
278 if (!file.AddRow())
279 return;
280
281 if (!file.WriteData(buf.data()+sizeof(float), (1440*1024*4 + 1440*roi*2 + ntm*roi*2 + 3)*sizeof(float)))
282 return;
283
284 ostringstream str;
285 str << "Wrote DRS calibration data (step=" << fData.fStep << ", roi=" << roi << ") to '" << filename << "'";
286 Info(str.str());
287#endif
288}
289
290bool DataCalib::Close(RUN_TAIL *tail)
291{
292 if (fData.fStep==0)
293 {
294 fData.fOffset.assign(fSum.begin(), fSum.end());
295 fData.fNumOffset = fNumEntries;
296
297 for (int i=0; i<1024*1440; i++)
298 fData.fGain[i] = 4096*fNumEntries;
299
300 // Scale ADC data from 12bit to 2000mV
301 GetSampleStats(fStats.data()+4, 2000./4096);
302 reinterpret_cast<uint32_t*>(fStats.data())[1] = GetRunId();;
303 }
304 if (fData.fStep==1)
305 {
306 fData.fGain.assign(fSum.begin(), fSum.end());
307 fData.fNumGain = fNumEntries;
308
309 // DAC: 0..2.5V == 0..65535 2500*50000 625*50000 625*3125
310 // V-mV: 1000 ---------- --------- --------
311 //fNumGain *= 2500*50000; 65536 16384 1024
312 //for (int i=0; i<1024*1440; i++)
313 // fGain[i] *= 65536;
314 fData.fNumGain *= 1953125;
315 for (int i=0; i<1024*1440; i++)
316 fData.fGain[i] *= 1024;
317
318 // Scale ADC data from 12bit to 2000mV
319 GetSampleStats(fStats.data()+1024*1440*2+4, 2000./4096/fData.fNumOffset);//0.5);
320 reinterpret_cast<uint32_t*>(fStats.data())[2] = GetRunId();;
321 }
322 if (fData.fStep==2)
323 {
324 fData.fTrgOff.assign(fSum.begin(), fSum.end());
325 fData.fNumTrgOff = fNumEntries;
326
327 // Scale ADC data from 12bit to 2000mV
328 GetSampleStats(fStats.data()+1024*1440*4+4, 2000./4096/fData.fNumOffset);//0.5);
329 reinterpret_cast<uint32_t*>(fStats.data())[0] = fNumSamples;
330 reinterpret_cast<uint32_t*>(fStats.data())[3] = GetRunId();
331 }
332
333 if (fData.fStep<=2)
334 WriteFits();
335
336 Update(fDim);
337
338 fData.fStep++;
339
340 fProcessing = false;
341
342 return DataWriteFits::Close(tail);
343}
Note: See TracBrowser for help on using the repository browser.