#ifndef MARS_DrsCalib #define MARS_DrsCalib #include // fabs #include // errno #include "fits.h" class DrsCalibrate { protected: uint64_t fNumEntries; size_t fNumSamples; size_t fNumChannels; std::vector fSum; std::vector fSum2; public: DrsCalibrate() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { } void Reset() { fNumEntries = 0; fNumSamples = 0; fNumChannels = 0; fSum.clear(); fSum2.clear(); } void InitSize(uint16_t channels, uint16_t samples) { fNumChannels = channels; fNumSamples = samples; fSum.resize(samples*channels); fSum2.resize(samples*channels); } void AddRel(const int16_t *val, const int16_t *start) { for (size_t ch=0; ch1439 ? ((ch-1440)*9+8)*1024 : ch*1024; for (size_t i=0; i=roi) return 0; double step = 0; // before // Exclude TM channel for (int p=0; p<8; p++) { const size_t hw = ch0+p; const size_t sw = map[hw]*roi + pos; step += vec[sw]-vec[sw-1]; } return step/8; } static void SubtractStep(const size_t ch0, const double avg, float *vec, int16_t roi, uint32_t dist, const uint16_t *map) { const int begin = avg>0 ? dist : 0; const int end = avg>0 ? roi : dist; const double sub = fabs(avg); for (int p=0; p<9; p++) { for (int j=begin; j25 && p[i]-p[i+1]>25) { p[i] = (p[i-1]+p[i+1])/2; } if (p[i]-p[i-1]>22 && fabs(p[i]-p[i+1])<4 && p[i+1]-p[i+2]>22) { p[i] = (p[i-1]+p[i+2])/2; p[i+1] = p[i]; } } } } static void RemoveSpikes2(float *vec, uint32_t roi)//from Werner { if (roi<4) return; for (size_t ch=0; ch<1440; ch++) { float *p = vec + ch*roi; std::vector Ameas(p, p+roi); std::vector N1mean(roi); for (size_t i=2; i -5.) continue; xp = Ameas[i+1] - N1mean[i+1]; xpp = Ameas[i+2] - N1mean[i+2]; if(Ameas[i+2] - (Ameas[i] + Ameas[i+3])/2. > 10.) { p[i+1]=(Ameas[i] + Ameas[i+3])/2.; p[i+2]=(Ameas[i] + Ameas[i+3])/2.; i += 3; continue; } if ( (xp > -2.*x*fract) && (xpp < -10.) ) { p[i+1] = N1mean[i+1]; N1mean[i+2] = (Ameas[i+1] - Ameas[i+3] / 2.); i += 2; } } } } std::pair,std::vector > GetSampleStats() const { if (fNumEntries==0) return make_pair(std::vector(),std::vector()); std::vector mean(fSum.size()); std::vector error(fSum.size()); std::vector::const_iterator it = fSum.begin(); std::vector::const_iterator i2 = fSum2.begin(); std::vector::iterator im = mean.begin(); std::vector::iterator ie = error.begin(); while (it!=fSum.end()) { *im = /*cnt ... mean /= fNumEntries; error = sqrt(error/fNumEntries - mean*mean); */ return make_pair(mean, error); } void GetSampleStats(float *ptr, float scale) const { const size_t sz = fNumSamples*fNumChannels; if (fNumEntries==0) { memset(ptr, 0, sizeof(float)*sz*2); return; } std::vector::const_iterator it = fSum.begin(); std::vector::const_iterator i2 = fSum2.begin(); while (it!=fSum.end()) { *ptr = scale*double(*it)/fNumEntries; *(ptr+sz) = scale*sqrt(double(*i2*int64_t(fNumEntries) - *it * *it))/fNumEntries; ptr++; it++; i2++; } } static double GetPixelStats(float *ptr, const float *data, uint16_t roi) { if (roi==0) return -1; const int beg = roi>10 ? 10 : 0; double max = 0; for (int i=0; i<1440; i++) { const float *vec = data+i*roi; int pos = beg; double sum = vec[beg]; double sum2 = vec[beg]*vec[beg]; for (int j=beg; jvec[pos]) pos = j; } sum /= roi-beg; sum2 /= roi-beg; if (vec[pos]>0) max = vec[pos]; *(ptr+0*1440+i) = sum; *(ptr+1*1440+i) = sqrt(sum2 - sum * sum); *(ptr+2*1440+i) = vec[pos]; *(ptr+3*1440+i) = pos; } return max; } static void GetPixelMax(float *max, const float *data, uint16_t roi, int32_t first, int32_t last) { if (roi==0 || first<0 || last<0 || first>=roi || last>=roi || last*pmax) pmax = ptr; max[i] = *pmax; } } const std::vector &GetSum() const { return fSum; } uint64_t GetNumEntries() const { return fNumEntries; } }; class DrsCalibrateTime { public: uint64_t fNumEntries; size_t fNumSamples; size_t fNumChannels; std::vector> fStat; public: DrsCalibrateTime() : fNumEntries(0), fNumSamples(0), fNumChannels(0) { InitSize(160, 1024); } DrsCalibrateTime(const DrsCalibrateTime &p) : fNumEntries(p.fNumEntries), fNumSamples(p.fNumSamples), fNumChannels(p.fNumChannels), fStat(p.fStat) { } double Sum(uint32_t i) const { return fStat[i].first; } double W(uint32_t i) const { return fStat[i].second; } virtual void InitSize(uint16_t channels, uint16_t samples) { fNumChannels = channels; fNumSamples = samples; fNumEntries = 0; fStat.clear(); fStat.resize(samples*channels); } void AddT(const float *val, const int16_t *start, signed char edge=0) { if (fNumSamples!=1024 || fNumChannels!=160) return; // Rising or falling edge detection has the advantage that // we are much less sensitive to baseline shifts for (size_t ch=0; ch<160; ch++) { const size_t tm = ch*9+8; const int16_t spos = start[tm]; if (spos<0) continue; const size_t pos = ch*1024; double p_prev = 0; int32_t i_prev = -1; for (size_t i=0; i<1024-1; i++) { const size_t rel = tm*1024 + i; const float &v0 = val[rel]; //-avg; const float &v1 = val[rel+1];//-avg; // Is rising edge? if (edge>0 && v0>0) continue; // Is falling edge? if (edge<0 && v0<0) continue; // Has sign changed? if ((v0<0 && v1<0) || (v0>0 && v1>0)) continue; const double p = v0==v1 ? 0.5 : v0/(v0-v1); const double l = i+p - (i_prev+p_prev); if (i_prev>=0) { const double w0 = 1-p_prev; const double w1 = p; fStat[pos+(spos+i_prev)%1024].first += w0*l; fStat[pos+(spos+i_prev)%1024].second += w0; for (size_t k=i_prev+1; ksecondfirst / it->second; num++; } avg /= num; for (auto it=beg; it!=end; it++) { if (it->second>=fNumEntries-0.5) continue; // { // result[i+1].first = *is2; // result[i+1].second = *iw2; // } // else // { it->first = avg*fNumEntries; it->second = fNumEntries; // } } } } DrsCalibrateTime GetComplete() const { DrsCalibrateTime rc(*this); rc.FillEmptyBins(); return rc; } void CalcResult() { for (int ch=0; ch<160; ch++) { const auto beg = fStat.begin() + ch*1024; const auto end = beg + 1024; // Calculate mean double s = 0; double w = 0; for (auto it=beg; it!=end; it++) { s += it->first; w += it->second; } s /= w; double sumw = 0; double sumv = 0; int n = 0; // Sums about many values are numerically less stable than // just sums over less. So we do the exercise from both sides. for (auto it=beg; it!=end-512; it++, n++) { const double valv = it->first; const double valw = it->second; it->first = sumv>0 ? n*(1-s*sumw/sumv) :0; sumv += valv; sumw += valw; } sumw = 0; sumv = 0; n = 1; for (auto it=end-1; it!=beg-1+512; it--, n++) { const double valv = it->first; const double valw = it->second; sumv += valv; sumw += valw; it->first = sumv>0 ? n*(s*sumw/sumv-1) : 0; } } } DrsCalibrateTime GetResult() const { DrsCalibrateTime rc(*this); rc.CalcResult(); return rc; } double Offset(uint32_t ch, double pos) const { const auto p = fStat.begin() + ch*1024; const uint32_t f = floor(pos); const double v0 = p[f].first; const double v1 = p[(f+1)%1024].first; return v0 + fmod(pos, 1)*(v1-v0); } double Calib(uint32_t ch, double pos) const { return pos-Offset(ch, pos); } }; struct DrsCalibration { std::vector fOffset; std::vector fGain; std::vector fTrgOff; uint64_t fNumOffset; uint64_t fNumGain; uint64_t fNumTrgOff; uint32_t fStep; uint16_t fRoi; // Region of interest for trgoff uint16_t fNumTm; // Number of time marker channels in trgoff // uint16_t fDAC[8]; DrsCalibration() : fOffset (1440*1024, 0), fGain (1440*1024, 4096), fTrgOff (1600*1024, 0), fNumOffset(1), fNumGain(2000), fNumTrgOff(1), fStep(0) { } void Clear() { // Default gain: // 0.575*[45590]*2.5V / 2^16 = 0.99999 V fOffset.assign(1440*1024, 0); fGain.assign (1440*1024, 4096); fTrgOff.assign(1600*1024, 0); fNumOffset = 1; fNumGain = 2000; fNumTrgOff = 1; fStep = 0; } std::string ReadFitsImp(const std::string &str, std::vector &vec) { std::fits file(str); if (!file) { std::ostringstream msg; msg << "Could not open file '" << str << "': " << strerror(errno); return msg.str(); } if (file.GetStr("TELESCOP")!="FACT") { std::ostringstream msg; msg << "Reading '" << str << "' failed: Not a valid FACT file (TELESCOP not FACT in header)"; return msg.str(); } if (!file.HasKey("STEP")) { std::ostringstream msg; msg << "Reading '" << str << "' failed: Is not a DRS calib file (STEP not found in header)"; return msg.str(); } if (file.GetNumRows()!=1) { std::ostringstream msg; msg << "Reading '" << str << "' failed: Number of rows in table is not 1."; return msg.str(); } fStep = file.GetUInt("STEP"); fNumOffset = file.GetUInt("NBOFFSET"); fNumGain = file.GetUInt("NBGAIN"); fNumTrgOff = file.GetUInt("NBTRGOFF"); fRoi = file.GetUInt("NROI"); fNumTm = file.HasKey("NTM") ? file.GetUInt("NTM") : 0; /* fDAC[0] = file.GetUInt("DAC_A"); fDAC[1] = file.GetUInt("DAC_B"); fDAC[4] = file.GetUInt("DAC_C"); */ vec.resize(1440*1024*4 + (1440+fNumTm)*fRoi*2 + 4); float *base = vec.data(); reinterpret_cast(base)[0] = fRoi; file.SetPtrAddress("RunNumberBaseline", base+1, 1); file.SetPtrAddress("RunNumberGain", base+2, 1); file.SetPtrAddress("RunNumberTriggerOffset", base+3, 1); file.SetPtrAddress("BaselineMean", base+4+0*1024*1440, 1024*1440); file.SetPtrAddress("BaselineRms", base+4+1*1024*1440, 1024*1440); file.SetPtrAddress("GainMean", base+4+2*1024*1440, 1024*1440); file.SetPtrAddress("GainRms", base+4+3*1024*1440, 1024*1440); file.SetPtrAddress("TriggerOffsetMean", base+4+4*1024*1440, fRoi*1440); file.SetPtrAddress("TriggerOffsetRms", base+4+4*1024*1440+fRoi*1440, fRoi*1440); if (fNumTm>0) { file.SetPtrAddress("TriggerOffsetTMMean", base+4+4*1024*1440+ 2*fRoi*1440, fRoi*fNumTm); file.SetPtrAddress("TriggerOffsetTMRms", base+4+4*1024*1440+ 2*fRoi*1440+ fRoi*fNumTm, fRoi*fNumTm); } if (!file.GetNextRow()) { std::ostringstream msg; msg << "Reading data from " << str << " failed."; return msg.str(); } /* fDAC[2] = fDAC[1]; fDAC[4] = fDAC[1]; fDAC[5] = fDAC[4]; fDAC[6] = fDAC[4]; fDAC[7] = fDAC[4]; */ fOffset.resize(1024*1440); fGain.resize(1024*1440); fTrgOff.resize(fRoi*(1440+fNumTm)); // Convert back to ADC counts: 256/125 = 4096/2000 // Convert back to sum (mean * num_entries) for (int i=0; i<1024*1440; i++) { fOffset[i] = fNumOffset *256*base[i+1024*1440*0+4]/125; fGain[i] = fNumOffset*fNumGain*256*base[i+1024*1440*2+4]/125; } for (int i=0; i vec; return ReadFitsImp(str, vec); } bool IsValid() { return fStep>2; } bool Apply(float *vec, const int16_t *val, const int16_t *start, uint32_t roi) { if (roi!=fRoi) { for (size_t ch=0; ch<1440; ch++) { const size_t pos = ch*roi; const size_t drs = ch*1024; DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi, fOffset.data()+drs, fNumOffset, fGain.data() +drs, fNumGain); } return false; } for (size_t ch=0; ch<1440; ch++) { const size_t pos = ch*fRoi; const size_t drs = ch*1024; DrsCalibrate::ApplyCh(vec+pos, val+pos, start[ch], roi, fOffset.data()+drs, fNumOffset, fGain.data() +drs, fNumGain, fTrgOff.data()+pos, fNumTrgOff); } for (size_t ch=0; ch