7#include "TLinearFitter.h"
19 std::fill(v.begin(), v.end(), f);
26 std::vector<float>
const& calibFactors
44 (*m_msgFunc_p)(ZDCMsg::Fatal,
45 "RPDDataAnalyzer::RPDDataAnalyzer: received incorrect number of channels in calibration factors ("
46 + std::to_string(m_outputCalibFactors.size()) +
" != " + std::to_string(s_nChannels) +
")"
75 "RPDDataAnalyzer::loadChannelData: received incorrect number of samples "
76 "in FADC data (" + std::to_string(FadcData.size()) +
", expected " + std::to_string(
m_nSamples) +
")"
126 for (
unsigned int sample = 0; sample <
m_nSamples; sample++) {
141 float prePulseSize = 0;
142 unsigned int prePulseSample = 0;
143 float goodPulseSize = 0;
144 unsigned int goodPulseSample = 0;
145 float postPulseSize = 0;
146 unsigned int postPulseSample = 0;
147 for (
unsigned int sample = 1; sample <
m_nSamples - 1; sample++) {
151 prePulseSize = secondDiff;
152 prePulseSample = sample;
154 goodPulseSize = secondDiff;
155 goodPulseSample = sample;
157 postPulseSize = secondDiff;
158 postPulseSample = sample;
162 bool hasPrePulse = prePulseSample;
163 bool hasGoodPulse = goodPulseSample;
164 bool hasPostPulse = postPulseSample;
169 if (hasGoodPulse && hasPostPulse && postPulseSize/goodPulseSize <=
m_postPulseFracThresh) hasPostPulse =
false;
171 bool hasNoPulse = !hasPrePulse && !hasGoodPulse && !hasPostPulse;
177 return !hasPrePulse && !hasPostPulse;
199 auto pFitter = std::make_unique<TLinearFitter>(1,
"1 ++ x");
201 for (
auto const& [sample,
y] : pileupFitPoints) {
203 pFitter->AddPoint(&
x, std::log(
y));
205 if (pFitter->Eval()) {
206 (*m_msgFunc_p)(
ZDCMsg::Warn,
"RPDDataAnalyzer::doPileupExpFit: there was an error while evaluating TLinearFitter!");
210 m_chPileupExpFitParams.at(channel) = {
static_cast<float>(pFitter->GetParameter(0)),
static_cast<float>(pFitter->GetParameter(1))};
211 m_chPileupExpFitParamErrs.at(channel) = {
static_cast<float>(pFitter->GetParError(0)),
static_cast<float>(pFitter->GetParError(1))};
212 m_chExpPileupFuncs.at(channel) = [intercept = pFitter->GetParameter(0), slope = pFitter->GetParameter(1)](
unsigned int sample) {
return std::exp(intercept + slope*sample); };
216 if (pFitter->GetParameter(1) >= 0) {
217 (*m_msgFunc_p)(
ZDCMsg::Debug,
"RPDDataAnalyzer::doPileupExpFit: p1 is " + std::to_string(pFitter->GetParameter(1)) +
" > 0 -> there is exponential growth in fit function!");
230 auto pFitter = std::make_unique<TLinearFitter>(1,
"1 ++ (x + 4)**(0.5) ++ (x + 4)**(-0.5)");
232 for (
auto const& [sample,
y] : pileupFitPoints) {
234 pFitter->AddPoint(&
x, std::log(
y));
236 if (pFitter->Eval()) {
237 (*m_msgFunc_p)(
ZDCMsg::Warn,
"RPDDataAnalyzer::doPileupStretchedExpFit: there was an error while evaluating TLinearFitter!");
241 auto getFitParam = [&pFitter](
int i){
return pFitter->GetParameter(i);};
242 auto fGetFitErr = [&pFitter](
int i){
return static_cast<float>(pFitter->GetParError(i));};
243 auto fGetFitParam = [&pFitter](
int i){
return static_cast<float>(pFitter->GetParameter(i));};
248 return std::exp(p0 + p1*std::pow(sample + 4, 0.5) + p2*std::pow(sample + 4, -0.5));
253 if (getFitParam(1) >= 0) {
254 (*m_msgFunc_p)(
ZDCMsg::Debug,
"RPDDataAnalyzer::doPileupStretchedExpFit: p1 is " + std::to_string(getFitParam(1)) +
" > 0 -> there is exponential growth in fit function!");
258 if (getFitParam(2)/getFitParam(1) - 4 > 0) {
259 (*m_msgFunc_p)(
ZDCMsg::Debug,
"RPDDataAnalyzer::doPileupStretchedExpFit: 1st deriv max occurs at sample " + std::to_string(getFitParam(1)) +
" > 0 -> fit probably looks like a pulse (and not like pileup)");
271 unsigned int nNegatives = 0;
273 if (values.at(sample) < 0) nNegatives++;
283 float nominalBaselineSubtrSum = 0;
285 std::vector<std::pair<unsigned int, float>> pileupFitPoints;
289 nominalBaselineSubtrSum += adcBaselineSubtr;
290 if (adcBaselineSubtr > 0) {
292 pileupFitPoints.emplace_back(sample, adcBaselineSubtr);
301 for (
unsigned int sample = 0; sample <
m_nSamples; sample++) {
317 for (
unsigned int sample = 0; sample <
m_nSamples; sample++) {
329 if (stretchedExpFitSuccess) {
331 for (
unsigned int sample = 0; sample <
m_nSamples; sample++) {
345 for (
unsigned int sample = 0; sample <
m_nSamples; sample++) {
370 float maxAdc = -std::numeric_limits<float>::infinity();
371 unsigned int maxSample = 0;
394 float signalRangeAdcSum = 0;
404 std::function<float(
unsigned int)> pileupFunc;
416 float totalAdcSum = 0;
417 float pileupTotalAdcSum = 0;
420 pileupTotalAdcSum += pileupFunc(sample);
422 if (totalAdcSum > 0) {
436 for (
unsigned int channel = 0; channel <
s_nChannels; channel++) {
463 "RPDDataAnalyzer::analyzeData: analyzing data with " + std::to_string(
m_nChannelsLoaded) +
" of "
467 for (
unsigned int channel = 0; channel <
s_nChannels; channel++) {
593 return static_cast<unsigned int>(
m_chStatus.at(channel).to_ulong());
601 return static_cast<unsigned int>(
m_sideStatus.to_ulong());
float m_pulse2ndDerivThresh
Samples before (not including) this sample are the signal region; nSamples goes to end of window.
unsigned int m_goodPulseSampleStop
Pulses before this sample are considered pre-pulses.
unsigned int m_nBaselineSamples
void reset()
Reset all member variables to default values.
std::array< float, s_nChannels > m_chBaseline
OOT pileup sum as a fraction of non-pileup sum in entire window (0 if no OOT pileup,...
bool doPileupStretchedExpFit(unsigned int channel, std::vector< std::pair< unsigned int, float > > const &pileupFitPoints)
Perform a stretched exponential fit in baseline-subtracted baseline samples and set relevant status b...
const std::vector< float > & getChPileupExpFitParams(unsigned int channel) const
Get parameters for pileup exponential fit (if pileup was detected and fit did not fail): exp( [0] + [...
const std::vector< float > & getChPileupStretchedExpFitParams(unsigned int channel) const
Get parameters for pileup stretched exponential fit (if pileup was detected and fit did not fail): ex...
float getChPileupFrac(unsigned int channel) const
Get OOT pileup sum as a fraction of non-pileup sum in entire window (0 if no OOT pileup,...
std::array< float, s_nChannels > m_chSumAdc
sample of max of RPD data in signal range after pileup subtraction; per channel
std::array< std::bitset< N_STATUS_BITS >, s_nChannels > m_chStatus
mean squared error of pileup stretched exponential fit in baseline samples (if pileup was detected an...
std::array< float, s_nChannels > m_ch2ndOrderStretchedExpPileupMSE
mean squared error of pileup exponential fit in baseline samples (if pileup was detected and fit did ...
std::array< float, s_nChannels > m_chSumAdcCalib
sum of RPD data in signal range after baseline and pileup subtraction; per channel
bool checkPulses(unsigned int channel)
Calculate 2nd difference, identify pulses, and set relevant status bits.
float m_pileupBaselineSumThresh
The global nominal baseline; used when pileup is detected.
std::array< float, s_nChannels > m_outputCalibFactors
ADC values greater than or equal to this number are considered overflow.
void analyzeData()
Analyze RPD data.
std::array< std::vector< float >, s_nChannels > m_chPileupStretchedExpFitParams
parameters for pileup exponential fit (if pileup was detected and fit did not fail): exp( [0] + [1]*s...
std::array< std::vector< float >, s_nChannels > m_chCorrectedFadcData
raw RPD data; index channel then sample
const std::vector< float > & getChPileupStretchedExpFitParamErrs(unsigned int channel) const
Get parameter errors for pileup stretched exponential fit (if pileup was detected and fit did not fai...
float getChMaxAdcCalib(unsigned int channel) const
Get max of RPD data in signal range after baseline and pileup subtraction, with output calibration fa...
bool doBaselinePileupSubtraction(unsigned int channel)
Determine if there is pileup, subtract baseline and pileup, and set relevant status bits.
bool checkOverflow(unsigned int channel)
Check for overflow and set relevant status bit.
unsigned int getChMaxSample(unsigned int channel) const
Get sample of max of RPD data in signal range after pileup subtraction.
float m_nominalBaseline
Pulses after this sample are considered post-pulses.
std::array< float, s_nChannels > m_chPileupFrac
max of RPD data in signal range after baseline and pileup subtraction, with output calibration factor...
unsigned int countSignalRangeNegatives(std::vector< float > const &values) const
Calculate the number of negative values in signal range.
ZDCMsg::MessageFunctionPtr m_msgFunc_p
void calculateMaxSampleMaxAdc(unsigned int channel)
Calculate max ADC and max sample.
float getChPileupStretchedExpFitMSE(unsigned int channel) const
Get mean squared error of pileup stretched exponential fit in baseline samples (if pileup was detecte...
static unsigned int constexpr s_minPileupFitPoints
status bits for side
float calculateBaselineSamplesMSE(unsigned int channel, std::function< float(unsigned int)> const &fit) const
Calculate the mean squared error of the fit function in the baseline samples.
void calculateSumAdc(unsigned int channel)
Calculate sum ADC and if there is pileup, calculate fractional pileup.
std::bitset< N_STATUS_BITS > m_sideStatus
status bits per channel
std::array< float, s_nChannels > m_chMaxAdcCalib
max of RPD data in signal range after baseline and pileup subtraction; per channel
bool doPileupExpFit(unsigned int channel, std::vector< std::pair< unsigned int, float > > const &pileupFitPoints)
Perform an exponential fit in baseline-subtracted baseline samples and set relevant status bits.
std::array< float, s_nChannels > m_chExpPileupMSE
pileup stretched exponential fit function (if pileup was detected and fit did not fail); per channel
unsigned int m_nChannelsLoaded
void loadChannelData(unsigned int channel, const std::vector< uint16_t > &FadcData)
Load a single channel's FADC data into member variable.
unsigned int getSideStatus() const
Get status word for side.
unsigned int m_goodPulseSampleStart
If there is a good pulse and post-pulse and size of post-pulse as a fraction of good pulse is less th...
float getChPileupExpFitMSE(unsigned int channel) const
Get mean squared error of pileup exponential fit in baseline samples (if pileup was detected and fit ...
float getChMaxAdc(unsigned int channel) const
Get max of RPD data in signal range after baseline and pileup subtraction.
std::array< std::vector< float >, s_nChannels > m_chPileupExpFitParams
baseline used in baseline subtraction; per channel
const std::vector< float > & getChPileupExpFitParamErrs(unsigned int channel) const
Get parameter errors for pileup exponential fit (if pileup was detected and fit did not fail).
unsigned int m_endSignalSample
Number of baseline samples; the sample equal to this number is the start of signal region.
std::array< float, s_nChannels > m_chMaxAdc
sum of RPD data in signal range after baseline and pileup subtraction, with output calibration factor...
unsigned int m_nNegativesAllowed
Baseline standard deviations less than this number indicate there is not pileup.
std::array< std::vector< uint16_t >, s_nChannels > m_chFADCData
multiplicative calibration factors to apply to output, e.g., max and sum ADC; per channel
RPDDataAnalyzer(ZDCMsg::MessageFunctionPtr messageFunc_p, std::string tag, RPDConfig const &config, std::vector< float > const &calibFactors)
void setSideStatusBits()
Set side status bits according to channel status bits.
float m_pileupBaselineStdDevThresh
Baseline sums less than this number indicate there is not pileup.
@ SecondOrderStretchedExp
static unsigned int constexpr s_nChannels
float getChSumAdcCalib(unsigned int channel) const
Get sum of RPD data in signal range after baseline and pileup subtraction, with output calibration fa...
unsigned int m_AdcOverflow
Maximum number of negative ADC values after baseline and pileup subtraction allowed in signal range.
std::array< std::vector< float >, s_nChannels > m_chPileupExpFitParamErrs
parameters for pileup stretched exponential fit (if pileup was detected and fit did not fail): exp( [...
std::array< std::function< float(unsigned int)>, s_nChannels > m_ch2ndOrderStretchedExpPileupFuncs
pileup exponential fit function (if pileup was detected and fit did not fail); per channel
unsigned int getChStatus(unsigned int channel) const
Get status word for channel.
float getChSumAdc(unsigned int channel) const
Get sum of RPD data in signal range after baseline and pileup subtraction.
std::array< unsigned int, s_nChannels > m_chMaxSample
RPD data with baseline and pileup subtracted; index channel then sample.
float getChBaseline(unsigned int channel) const
Get baseline used in baseline subtraction.
float m_postPulseFracThresh
Second differences less than or equal to this number indicate a pulse.
std::array< std::vector< float >, s_nChannels > m_chPileupStretchedExpFitParamErrs
parameter errors for pileup exponential fit (if pileup was detected and fit did not fail); per channe...
std::array< std::function< float(unsigned int)>, s_nChannels > m_chExpPileupFuncs
enum indicating type of pileup fit function; per channel
@ InsufficientPileupFitPointsBit
@ PileupStretchedExpPulseLikeBit
@ PileupStretchedExpGrowthBit
@ PileupStretchedExpFitFailBit
@ PileupBadStretchedExpSubtrBit
std::array< PileupFitFuncType, s_nChannels > m_chPileupFuncType
parameter errors for pileup stretched exponential fit (if pileup was detected and fit did not fail); ...
std::shared_ptr< MessageFunction > MessageFunctionPtr
float nullPileupFunc(unsigned int)
void helpResetFuncs(std::span< std::function< float(unsigned int)> > v)