Changeset 11829
- Timestamp:
- 08/07/11 16:36:31 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/DataCalib.cc
r11818 r11829 114 114 bool DataCalib::ReadFits(const string &str, MessageImp &msg) 115 115 { 116 if (fProcessing) 117 { 118 msg.Error("Reading "+str+" failed: DRS calibration in process."); 119 return false; 120 } 121 116 122 try 117 123 { … … 158 164 } 159 165 160 fStep = file.GetUInt("STEP"); 166 fStep = file.GetUInt("STEP"); 167 fNumOffset = file.GetUInt("NBOFFSET"); 168 fNumGain = file.GetUInt("NBGAIN"); 169 fNumTrgOff = file.GetUInt("NBTRGOFF"); 161 170 162 171 memcpy(fStats.data(), base, fStats.size()*sizeof(float)); 163 172 164 return true; 165 } 166 catch (const runtime_error &e) 167 { 168 msg.Error("Exception reading "+str+": "+e.what()); 169 return false; 170 } 171 } 172 173 void DataCalib::WriteFits() 174 { 175 #ifdef HAVE_FITS 176 FitsFile file(fMsg); 177 178 file.AddColumn('I', "RunNumberBaseline"); 179 file.AddColumn('I', "RunNumberGain"); 180 file.AddColumn('I', "RunNumberTriggerOffset"); 181 file.AddColumn('F', "BaselineMean", 1024*1440); 182 file.AddColumn('F', "BaselineRms", 1024*1440); 183 file.AddColumn('F', "GainMean", 1024*1440); 184 file.AddColumn('F', "GainRms", 1024*1440); 185 file.AddColumn('F', "TriggerOffsetMean", 1024*1440); 186 file.AddColumn('F', "TriggerOffsetRms", 1024*1440); 187 188 fFileName = FormFileName(GetRunId(), "drs.fits"); 189 190 if (!file.OpenFile(fFileName)) 191 return; 192 193 if (!file.OpenTable("DrsCalibration")) 194 return; 195 196 if (!file.WriteDefaultKeys("fadctrl")) 197 return; 198 199 if (!file.WriteKeyNT("STEP", fStep, "")) 200 return; 201 202 vector<char> buf; 203 buf.reserve(fStats.size()*sizeof(float)); 204 205 char *src = reinterpret_cast<char*>(fStats.data()); 206 char *end = reinterpret_cast<char*>(fStats.data()+1024*1440*6+3); 207 char *dest = buf.data(); 208 209 while (src<end) 210 { 211 reverse_copy(src, src+sizeof(float), dest); 212 src += sizeof(float); 213 dest += sizeof(float); 214 } 215 216 if (!file.AddRow()) 217 return; 218 219 if (!file.WriteData(buf.data(), 1024*1440*sizeof(float)*6+3)) 220 return; 221 222 ostringstream str; 223 str << "Wrote DRS calibration data (step=" << fStep << ") to '" << fFileName << "'"; 224 Info(str.str()); 225 #endif 226 } 227 228 bool DataCalib::Close(RUN_TAIL *) 229 { 230 if (fStep==0) 231 { 232 fOffset.assign(fSum.begin(), fSum.end()); 233 fNumOffset = fNumEntries; 234 235 // Scale ADC data from 12bit to 2000mV 236 GetSampleStats(fStats.data()+3, 2000./4096); 237 reinterpret_cast<uint32_t*>(fStats.data())[0] = GetRunId();; 238 } 239 if (fStep==1) 240 { 241 fGain.assign(fSum.begin(), fSum.end()); 242 fNumGain = fNumEntries*fNumOffset; 173 // Convert back to ADC counts: 256/125 = 4096/2000 174 // Convert back to sum (mean * num_entries) 175 for (int i=0; i<1024*1440; i++) 176 { 177 178 fOffset[i] = fNumOffset *256*fStats[i+1024*1440*0+3]/125; 179 fGain[i] = fNumOffset*fNumGain *256*fStats[i+1024*1440*2+3]/125; 180 fTrgOff[i] = fNumOffset*fNumTrgOff*256*fStats[i+1024*1440*4+3]/125; 181 } 243 182 244 183 // DAC: 0..2.5V == 0..65535 … … 251 190 fGain[i] *= 1024; 252 191 192 return true; 193 } 194 catch (const runtime_error &e) 195 { 196 msg.Error("Exception reading "+str+": "+e.what()); 197 return false; 198 } 199 } 200 201 void DataCalib::WriteFits() 202 { 203 #ifdef HAVE_FITS 204 FitsFile file(fMsg); 205 206 file.AddColumn('I', "RunNumberBaseline"); 207 file.AddColumn('I', "RunNumberGain"); 208 file.AddColumn('I', "RunNumberTriggerOffset"); 209 file.AddColumn('F', "BaselineMean", 1024*1440, "V"); 210 file.AddColumn('F', "BaselineRms", 1024*1440, "V"); 211 file.AddColumn('F', "GainMean", 1024*1440, "V"); 212 file.AddColumn('F', "GainRms", 1024*1440, "V"); 213 file.AddColumn('F', "TriggerOffsetMean", 1024*1440, "V"); 214 file.AddColumn('F', "TriggerOffsetRms", 1024*1440, "V"); 215 216 fFileName = FormFileName(GetRunId(), "drs.fits"); 217 218 if (!file.OpenFile(fFileName)) 219 return; 220 221 if (!file.OpenTable("DrsCalibration")) 222 return; 223 224 if (!file.WriteDefaultKeys("fadctrl")) 225 return; 226 227 if (!file.WriteKeyNT("STEP", fStep, "") || 228 !file.WriteKeyNT("ADCRANGE", 2000, "Dynamic range of the ADC in mV") || 229 !file.WriteKeyNT("DACRANGE", 2500, "Dynamic range of the DAC in mV") || 230 !file.WriteKeyNT("ADC", 12, "Resolution of ADC in bits") 231 !file.WriteKeyNT("DAC", 16, "Resolution of DAC in bits") 232 !file.WriteKeyNT("DACLEVEL", 50000, "Applied DAC level in counts") 233 !file.WriteKeyNT("NBOFFSET", fNumOffset, "Number of entries for offset calibration") 234 !file.WriteKeyNT("NBGAIN", fNumGain/1953125, "Number of entries for gain calibration") 235 !file.WriteKeyNT("NBTRGOFF", fNumTrgOff, "Number of entries for trigger offset calibration") 236 ) 237 return; 238 239 vector<char> buf; 240 buf.reserve(fStats.size()*sizeof(float)); 241 242 char *src = reinterpret_cast<char*>(fStats.data()); 243 char *end = reinterpret_cast<char*>(fStats.data()+1024*1440*6+3); 244 char *dest = buf.data(); 245 246 while (src<end) 247 { 248 reverse_copy(src, src+sizeof(float), dest); 249 src += sizeof(float); 250 dest += sizeof(float); 251 } 252 253 if (!file.AddRow()) 254 return; 255 256 if (!file.WriteData(buf.data(), 1024*1440*sizeof(float)*6+3)) 257 return; 258 259 ostringstream str; 260 str << "Wrote DRS calibration data (step=" << fStep << ") to '" << fFileName << "'"; 261 Info(str.str()); 262 #endif 263 } 264 265 bool DataCalib::Close(RUN_TAIL *) 266 { 267 if (fStep==0) 268 { 269 fOffset.assign(fSum.begin(), fSum.end()); 270 fNumOffset = fNumEntries; 271 272 // Scale ADC data from 12bit to 2000mV 273 GetSampleStats(fStats.data()+3, 2000./4096); 274 reinterpret_cast<uint32_t*>(fStats.data())[0] = GetRunId();; 275 } 276 if (fStep==1) 277 { 278 fGain.assign(fSum.begin(), fSum.end()); 279 fNumGain = fNumEntries; 280 281 // DAC: 0..2.5V == 0..65535 282 // V-mV: 1000 283 //fNumGain *= 2500*50000; 284 //for (int i=0; i<1024*1440; i++) 285 // fGain[i] *= 65536; 286 fNumGain *= 1953125; 287 for (int i=0; i<1024*1440; i++) 288 fGain[i] *= 1024; 289 253 290 // Scale ADC data from 12bit to 2000mV 254 291 GetSampleStats(fStats.data()+1024*1440*2+3, 2000./4096/fNumOffset);//0.5); … … 258 295 { 259 296 fTrgOff.assign(fSum.begin(), fSum.end()); 260 fNumTrgOff = fNumEntries *fNumOffset;297 fNumTrgOff = fNumEntries; 261 298 262 299 // Scale ADC data from 12bit to 2000mV
Note:
See TracChangeset
for help on using the changeset viewer.