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

Last change on this file since 11788 was 11774, checked in by tbretz, 13 years ago
Fixed the default for fNumGain
File size: 5.6 KB
Line 
1#include "DataCalib.h"
2
3#include "FAD.h"
4#include "FitsFile.h"
5#include "DimDescriptionService.h"
6
7using namespace std;
8
9int DataCalib::fStep = 0;
10bool DataCalib::fProcessing = false;
11
12vector<int32_t> DataCalib::fOffset(1440*1024, 0);
13vector<int64_t> DataCalib::fGain (1440*1024, 4096);
14vector<int64_t> DataCalib::fTrgOff(1440*1024, 0);
15vector<float> DataCalib::fStats (1440*1024*6+3);
16
17uint64_t DataCalib::fNumOffset = 1;
18uint64_t DataCalib::fNumGain = 2000;
19uint64_t DataCalib::fNumTrgOff = 1;
20
21void DataCalib::Restart()
22{
23 // Default gain:
24 // 0.575*[45590]*2.5V / 2^16 = 0.99999 V
25 fOffset.assign(1440*1024, 0);
26 fGain.assign (1440*1024, 4096);
27 fTrgOff.assign(1440*1024, 0);
28
29 fNumOffset = 1;
30 fNumGain = 2000;
31 fNumTrgOff = 1;
32
33 reinterpret_cast<uint32_t*>(fStats.data())[0] = 0;
34 reinterpret_cast<uint32_t*>(fStats.data())[1] = 0;
35 reinterpret_cast<uint32_t*>(fStats.data())[2] = 0;
36
37 int i=0;
38 while (i<1024*1440*2+3) // Set mean and RMS to 0
39 fStats[i++] = 0;
40 while (i<1024*1440*3+3)
41 fStats[i++] = 2000./4096; // Set mean to 0.5
42 while (i<1024*1440*6+3)
43 fStats[i++] = 0; // Set everything else to 0
44
45 fStep = 0;
46 fProcessing = false;
47}
48
49void DataCalib::Update(DimDescribedService &dim)
50{
51 dim.Update(fStats);
52}
53
54bool DataCalib::Open(RUN_HEAD* h)
55{
56 if (h->NPix != 1440)
57 {
58 fMsg.Error("Number of pixels in header not 1440.");
59 return false;
60 }
61
62 if (h->Nroi != 1024)
63 {
64 fMsg.Error("Region of interest not 1024.");
65 return false;
66 }
67
68 if (fProcessing)
69 {
70 fMsg.Warn("Previous run not yet finished!");
71 return false;
72 }
73
74 if (fStep==4)
75 {
76 fMsg.Warn("DRS Calibration already finished... please restart!");
77 return false;
78 }
79
80 fProcessing = true;
81
82 ostringstream name;
83 name << "drs-calib-" << fStep;
84 fFileName = name.str();
85
86 Reset();
87 InitSize(1440, 1024);
88
89 return true;
90}
91
92bool DataCalib::WriteEvt(EVENT *e)
93{
94 // FIXME: SET StartPix to 0 if StartPix is -1
95
96 if (fStep==0)
97 {
98 AddRel(e->Adc_Data, e->StartPix);
99 }
100 if (fStep==1)
101 {
102 AddRel(e->Adc_Data, e->StartPix, fOffset.data(), fNumOffset);
103 }
104 if (fStep==2)
105 {
106 AddAbs(e->Adc_Data, e->StartPix, fOffset.data(), fNumOffset);
107 }
108
109 return true;
110}
111
112void DataCalib::WriteFits()
113{
114#ifdef HAVE_FITS
115 FitsFile file(fMsg);
116
117 file.AddColumn('I', "RunNumberBaseline");
118 file.AddColumn('I', "RunNumberGain");
119 file.AddColumn('I', "RunNumberTriggerOffset");
120 file.AddColumn('F', "BaselineMean", 1024*1440);
121 file.AddColumn('F', "BaselineRms", 1024*1440);
122 file.AddColumn('F', "GainMean", 1024*1440);
123 file.AddColumn('F', "GainRms", 1024*1440);
124 file.AddColumn('F', "TriggerOffsetMean", 1024*1440);
125 file.AddColumn('F', "TriggerOffsetRms", 1024*1440);
126
127 fFileName = FormFileName(GetRunId(), "drs.fits");
128
129 if (!file.OpenFile(fFileName))
130 return;
131
132 if (!file.OpenTable("DrsCalibration"))
133 return;
134
135 if (!file.WriteDefaultKeys("fadctrl"))
136 return;
137
138 if (!file.WriteKeyNT("STEP", fStep, ""))
139 return;
140
141 vector<char> buf;
142 buf.reserve(fStats.size()*sizeof(float));
143
144 char *src = reinterpret_cast<char*>(fStats.data());
145 char *end = reinterpret_cast<char*>(fStats.data()+1024*1440*6+3);
146 char *dest = buf.data();
147
148 while (src<end)
149 {
150 reverse_copy(src, src+sizeof(float), dest);
151 src += sizeof(float);
152 dest += sizeof(float);
153 }
154
155 if (!file.AddRow())
156 return;
157
158 if (!file.WriteData(buf.data(), 1024*1440*sizeof(float)*6+3))
159 return;
160
161 ostringstream str;
162 str << "Wrote DRS calibration data (step=" << fStep << ") to '" << fFileName << "'";
163 Info(str.str());
164#endif
165}
166
167bool DataCalib::Close(RUN_TAIL *)
168{
169 if (fStep==0)
170 {
171 fOffset.assign(fSum.begin(), fSum.end());
172 fNumOffset = fNumEntries;
173
174 // Scale ADC data from 12bit to 2000mV
175 GetSampleStats(fStats.data()+3, 2000./4096);
176 reinterpret_cast<uint32_t*>(fStats.data())[0] = GetRunId();;
177 }
178 if (fStep==1)
179 {
180 fGain.assign(fSum.begin(), fSum.end());
181 fNumGain = fNumEntries*fNumOffset;
182
183 // DAC: 0..2.5V == 0..65535
184 // V-mV: 1000
185 //fNumGain *= 2500*50000;
186 //for (int i=0; i<1024*1440; i++)
187 // fGain[i] *= 65536;
188 fNumGain *= 1953125;
189 for (int i=0; i<1024*1440; i++)
190 fGain[i] *= 1024;
191
192 // Scale ADC data from 12bit to 2000mV
193 GetSampleStats(fStats.data()+1024*1440*2+3, 2000./4096/fNumOffset);//0.5);
194 reinterpret_cast<uint32_t*>(fStats.data())[1] = GetRunId();;
195 }
196 if (fStep==2)
197 {
198 fTrgOff.assign(fSum.begin(), fSum.end());
199 fNumTrgOff = fNumEntries*fNumOffset;
200
201 // Scale ADC data from 12bit to 2000mV
202 GetSampleStats(fStats.data()+1024*1440*4+3, 2000./4096/fNumOffset);//0.5);
203 reinterpret_cast<uint32_t*>(fStats.data())[2] = GetRunId();;
204 }
205
206 if (fStep>=0 && fStep<=2)
207 WriteFits();
208
209 fProcessing = false;
210
211 fDim.Update(fStats);
212
213 fStep++;
214
215 return true;
216}
217
218void DataCalib::Apply(int16_t *val, const int16_t *start, uint32_t roi)
219{
220 CalibData::Apply(val, start, roi,
221 fOffset.data(), fNumOffset,
222 fGain.data(), fNumGain,
223 fTrgOff.data(), fNumTrgOff);
224}
225
226void DataCalib::Apply(float *vec, int16_t *val, const int16_t *start, uint32_t roi)
227{
228 CalibData::Apply(vec, val, start, roi,
229 fOffset.data(), fNumOffset,
230 fGain.data(), fNumGain,
231 fTrgOff.data(), fNumTrgOff);
232}
233
Note: See TracBrowser for help on using the repository browser.