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