8#include "TFitResultPtr.h"
9#include "TVirtualFitter.h"
30 {
"tag", {JSON::value_t::string, 1,
true,
true}},
31 {
"enabled", {JSON::value_t::boolean, 1,
true,
true}},
32 {
"LGMode", {JSON::value_t::number_unsigned, 1,
false,
true}},
33 {
"Nsample", {JSON::value_t::number_integer, 1,
false,
true}},
34 {
"FADCFreqMHz", {JSON::value_t::number_integer, 1,
false,
true}},
35 {
"preSampleIdx", {JSON::value_t::number_integer, 1,
true,
true}},
36 {
"nominalPedestal", {JSON::value_t::number_integer, 1,
false,
true}},
37 {
"fitFunction", {JSON::value_t::string, 1,
true,
true}},
38 {
"peakSample", {JSON::value_t::number_integer, 1,
true,
true}},
39 {
"peakTolerance", {JSON::value_t::number_integer, 1,
true,
false}},
40 {
"quietFits", {JSON::value_t::boolean, 1,
false,
false}},
41 {
"2ndDerivThreshHG", {JSON::value_t::number_integer, 1,
true,
true}},
42 {
"2ndDerivThreshLG", {JSON::value_t::number_integer, 1,
true,
true}},
43 {
"2ndDerivStep", {JSON::value_t::number_integer, 1,
false,
false}},
44 {
"HGOverflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
45 {
"HGUnderflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
46 {
"LGOverflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
47 {
"nominalT0HG", {JSON::value_t::number_float, 1,
true,
true}},
48 {
"nominalT0LG", {JSON::value_t::number_float, 1,
true,
true}},
49 {
"nominalTau1", {JSON::value_t::number_float, 1,
true,
false}},
50 {
"nominalTau2", {JSON::value_t::number_float, 1,
true,
false}},
51 {
"fixTau1", {JSON::value_t::boolean, 1,
true,
false}},
52 {
"fixTau2", {JSON::value_t::boolean, 1,
true,
false}},
53 {
"T0CutsHG", {JSON::value_t::array, 2,
true,
true}},
54 {
"T0CutsLG", {JSON::value_t::array, 2,
true,
true}},
55 {
"chisqDivAmpCutHG", {JSON::value_t::number_float, 1,
true,
true}},
56 {
"chisqDivAmpCutLG", {JSON::value_t::number_float, 1,
true,
true}},
57 {
"chisqDivAmpOffsetHG", {JSON::value_t::number_float, 1,
true,
true}},
58 {
"chisqDivAmpOffsetLG", {JSON::value_t::number_float, 1,
true,
true}},
59 {
"chisqDivAmpPowerHG", {JSON::value_t::number_float, 1,
true,
true}},
60 {
"chisqDivAmpPowerLG", {JSON::value_t::number_float, 1,
true,
true}},
61 {
"gainFactorHG", {JSON::value_t::number_float, 1,
true,
true}},
62 {
"gainFactorLG", {JSON::value_t::number_float, 1,
true,
true}},
63 {
"noiseSigmaHG", {JSON::value_t::number_float, 1,
true,
true}},
64 {
"noiseSigmaLG", {JSON::value_t::number_float, 1,
true,
true}},
65 {
"enableRepass", {JSON::value_t::boolean, 1,
false,
false}},
66 {
"Repass2ndDerivThreshHG", {JSON::value_t::number_integer, 1,
true,
true}},
67 {
"Repass2ndDerivThreshLG", {JSON::value_t::number_integer, 1,
true,
true}},
68 {
"fitAmpMinMaxHG", {JSON::value_t::array, 2,
true,
false}},
69 {
"fitAmpMinMaxLG", {JSON::value_t::array, 2,
true,
false}},
70 {
"ampMinSignifHGLG", {JSON::value_t::array, 2,
true,
false}},
71 {
"enablePreExclusion", {JSON::value_t::array, 3,
false,
false}},
72 {
"enablePostExclusion", {JSON::value_t::array, 3,
false,
false}},
73 {
"enableUnderflowExclusionHG", {JSON::value_t::array, 2,
true,
false}},
74 {
"enableUnderflowExclusionLG", {JSON::value_t::array, 2,
true,
false}},
75 {
"enableTimingCorrection", {JSON::value_t::array, 3,
false,
false}},
76 {
"timeCorrCoeffHG", {JSON::value_t::array, 6,
true,
false}},
77 {
"timeCorrCoeffLG", {JSON::value_t::array, 6,
true,
false}},
78 {
"enableADCNLCorrection", {JSON::value_t::array, 3,
false,
false}},
79 {
"ADCNLCorrCoeffs", {JSON::value_t::array, 0,
true,
false}},
80 {
"enableNLCorrection", {JSON::value_t::array, 2,
false,
false}},
81 {
"HGNLCorrCoeffs", {JSON::value_t::array, 5,
true,
false}},
82 {
"LGNLCorrCoeffs", {JSON::value_t::array, 5,
true,
false}},
83 {
"useDelayed", {JSON::value_t::boolean, 1,
false,
false}},
84 {
"delayDeltaT", {JSON::value_t::number_float, 1,
true,
false}},
85 {
"delayDefaultPedestalShift", {JSON::value_t::number_float, 1,
true,
false}},
86 {
"fitTimeMax", {JSON::value_t::number_float, 1,
true,
false}}
107 double chiSquare = 0;
109 float delayBaselineAdjust = par[0];
113 for (
int isample = 0; isample < nSamples; isample++) {
123 double pull = (histValue - funcVal) / histError;
126 chiSquare += pull * pull;
131 for (
int isample = 0; isample < nSamples; isample++) {
133 double histError = std::max(
s_delayedFitHist->GetBinError(isample + 1), 1.0);
140 double pull = (histValue - funcVal) / histError;
143 chiSquare += pull * pull;
151 const std::string& fitFunction,
int peak2ndDerivMinSample,
152 float peak2ndDerivMinThreshHG,
float peak2ndDerivMinThreshLG) :
167 m_tmin = -deltaTSample / 2;
172 std::string histName =
"ZDCFitHist" + tag;
173 std::string histNameLGRefit =
"ZDCFitHist" + tag +
"_LGRefit";
194 (*m_msgFunc_p)(
ZDCMsg::Debug,
"ConfigFromJSON produced result: "+ resultString2);
205 std::string histName =
"ZDCFitHist" +
m_tag;
206 std::string histNameLGRefit =
"ZDCFitHist" +
m_tag +
"_LGRefit";
231 std::string delayedHGName = std::string(
m_fitHist->GetName()) +
"delayed";
232 std::string delayedLGName = std::string(
m_fitHistLGRefit->GetName()) +
"delayed";
495 std::ostringstream ostrm;
506 (*m_msgFunc_p)(
ZDCMsg::Error, (
"ZDCPulseAnalyzer::SetFitTimeMax:: invalid FitTimeMax: " + std::to_string(tmax)));
523 float deltaT0MinHG,
float deltaT0MaxHG,
524 float deltaT0MinLG,
float deltaT0MaxLG)
537 const std::vector<double>& parsHG,
538 const std::vector<double>& parsLG)
545 std::string timeResHGName =
"TimeResFuncHG_" +
m_tag;
546 std::string timeResLGName =
"TimeResFuncLG_" +
m_tag;
548 TF1* funcHG_p =
new TF1(timeResHGName.c_str(), TF1String.c_str(), 0,
m_HGOverflowADC);
549 TF1* funcLG_p =
new TF1(timeResLGName.c_str(), TF1String.c_str(), 0,
m_LGOverflowADC);
551 if (parsHG.size() !=
static_cast<unsigned int>(funcHG_p->GetNpar()) ||
552 parsLG.size() !=
static_cast<unsigned int>(funcLG_p->GetNpar())) {
557 funcHG_p->SetParameters(&parsHG[0]);
558 funcLG_p->SetParameters(&parsLG[0]);
572 auto getXmin=[](
const TH1 * pH){
573 return pH->GetXaxis()->GetXmin();
575 auto getXmax=[](
const TH1 * pH){
576 return pH->GetXaxis()->GetXmax();
578 auto xmin= getXmin(correHistHG.get());
579 auto xmax= getXmax(correHistHG.get());
580 if (std::abs(
xmin+0.5) > 1e-3 || std::abs(
xmax - 4095.5) > 1e-3) {
581 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid high gain correction histogram range: xmin, xmax = " +
582 std::to_string(
xmin ) +
", " + std::to_string(
xmax) );
587 xmin= getXmin(correHistLG.get());
588 xmax= getXmax(correHistLG.get());
589 if (std::abs(
xmin+0.5) > 1e-3 ||
590 std::abs(
xmax - 4095.5) > 1e-3) {
591 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid low gain correction histogram range: xmin, xmax = " +
592 std::to_string(
xmin) +
", " + std::to_string(
xmax) );
618 std::vector<float> pulls(
m_Nsample, -100);
621 const TF1* fit_p =
static_cast<const TF1*
>(dataHist_p->GetListOfFunctions()->Last());
623 for (
size_t ibin = 0; ibin <
m_Nsample ; ibin++) {
624 float t = dataHist_p->GetBinCenter(ibin + 1);
625 float fitVal = fit_p->Eval(t);
626 float histVal = dataHist_p->GetBinContent(ibin + 1);
627 float histErr = dataHist_p->GetBinError(ibin + 1);
628 float pull = (histVal - fitVal)/histErr;
638 double amplCorrFactor = 1;
644 amplCorrFactor *= fadcCorr;
656 float invNLCorr = 1.0;
670 amplCorrFactor /= invNLCorr;
673 return amplCorrFactor;
678 float prePulseTMin = 0;
772 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting both delayed and undelayed samples");
795 const std::vector<float>& ADCSamplesHGDelayed,
const std::vector<float>& ADCSamplesLGDelayed)
798 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting only undelayed samples");
820 for (
size_t isample = 0; isample <
m_Nsample; isample++) {
821 float ADCHG = ADCSamplesHG[isample];
822 float ADCLG = ADCSamplesLG[isample];
863 bool doDump = (*m_msgFunc_p)(
ZDCMsg::Verbose,
"Dumping all samples before subtraction: ");
881 for (
size_t isample = 0; isample <
m_NSamplesAna; isample++) {
904 std::ostringstream dumpString;
905 dumpString <<
"After FADC correction, sample " << isample <<
", HG ADC = " <<
m_ADCSamplesHGSub[isample]
1058 float deriv2ndThreshHG = 0;
1059 float deriv2ndThreshLG = 0;
1079 (
float chisq,
float amp,
unsigned int fitNDoF)->
bool
1081 double ratio = chisq / (std::pow(amp, power) + offset);
1082 if (chisq/fitNDoF > 2 && ratio > cut)
return false;
1125 (
float chisq,
float amp,
unsigned int fitNDoF)->
bool
1127 double ratio = chisq / (std::pow(amp, power) + offset);
1129 if (chisq/
float(fitNDoF) > 2 && ratio > cut)
return false;
1183 const std::vector<float>& samples,
1184 const std::vector<bool>& useSample,
1185 float peak2ndDerivMinThresh,
1187 const std::vector<float>& t0CorrParams,
1189 float minT0Corr,
float maxT0Corr
1202 bool haveFirst =
false;
1203 unsigned int lastUsed = 0;
1205 for (
unsigned int sample = preSampleIdx; sample < nSamples; sample++) {
1206 if (useSample[sample]) {
1263 std::ostringstream baselineMsg;
1264 baselineMsg <<
"Delayed samples baseline correction = " <<
m_baselineCorr << std::endl;
1270 for (
size_t isample = 0; isample < nSamples; isample++) {
1281 std::for_each(
m_samplesSub.begin(),
m_samplesSub.end(), [ = ] (
float & adcUnsub) {return adcUnsub -= m_preSample;} );
1354 if (derivPresampleSig < -5) {
1360 if (!useSample[isample])
continue;
1362 float sampleSig = -
m_samplesSub[isample]/(std::sqrt(2.0)*noiseSig);
1378 float maxPrepulseSig = 0;
1379 unsigned int maxPrepulseSample = 0;
1381 for (
int isample = loopStart; isample <= loopLimit; isample++) {
1382 if (!useSample[isample])
continue;
1394 if (prePulseSig > maxPrepulseSig) {
1395 maxPrepulseSig = prePulseSig;
1396 maxPrepulseSample = isample;
1426 unsigned int postStartIdx = std::max(
static_cast<unsigned int>(
m_minDeriv2ndIndex + 2),
1430 for (
int isample = postStartIdx; isample < (int) nSamples - 1; isample++) {
1431 if (!useSample.at(isample))
continue;
1462 float derivSig = deriv/(std::sqrt(2)*noiseSig);
1471 if (std::abs(deriv2ndTest) > 0.15) {
1484 if (deriv2ndSig > 5 && deriv2ndTest < -0.1) {
1495 std::ostringstream ostrm;
1496 ostrm <<
"Post pulse found, m_maxSampleEvt = " <<
m_maxSampleEvt;
1514 std::ostringstream ostrm;
1531 float correction = 0;
1533 for (
unsigned int ipow = 0; ipow < t0CorrParams.size(); ipow++) {
1534 correction += t0CorrParams[ipow]*std::pow(t0CorrFact,
double(ipow));
1552 double timeResolution = 0;
1564 if (failFixedCut)
m_badT0 =
true;
1584 const std::vector<bool>& useSamples)
1593 for (
unsigned int idx = 0; idx < samplesLG.size(); idx++) {
1596 if (useSamples[idx]) {
1610 TH1* hist_p =
nullptr;
1615 float ampInitial, fitAmpMin, fitAmpMax, t0Initial;
1636 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1658 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1677 int fitStatus = result_ptr;
1683 if (fitStatus != 0 && result_ptr->Edm() > 0.001)
1693 if ((
int) constrFitResult_ptr != 0) {
1703 if ((
int) unconstrFitResult_ptr != 0) {
1710 if ((
int) constrFit2Result_ptr != 0) {
1717 result_ptr = constrFit2Result_ptr;
1721 result_ptr = unconstrFitResult_ptr;
1727 hist_p->GetListOfFunctions()->Clear();
1730 std::string name = func->GetName();
1732 TF1* copyFunc =
static_cast<TF1*
>(func->Clone((name +
"_copy").c_str()));
1733 hist_p->GetListOfFunctions()->Add(copyFunc);
1786 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1798 float fitAmpMin, fitAmpMax, t0Initial, ampInitial;
1814 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1840 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1845 TFitter* theFitter =
nullptr;
1869 size_t numFitPar = theFitter->GetNumberTotalParameters();
1871 theFitter->GetMinuit()->fISW[4] = -1;
1876 theFitter->GetMinuit()->fISW[4] = -1;
1879 theFitter->GetMinuit()->mnexcm(
"SET NOWarnings",
nullptr,0,ierr);
1881 else theFitter->GetMinuit()->fISW[4] = 0;
1886 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1887 theFitter->ReleaseParameter(0);
1890 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1891 theFitter->FixParameter(0);
1895 double arglist[100];
1898 int status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1900 double fitAmp = theFitter->GetParameter(1);
1904 double chi2, edm, errdef;
1907 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1912 if (status || fitAmp < fitAmpMin * 1.01 || edm > 0.01){
1917 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1918 theFitter->FixParameter(0);
1922 if (fitAmp < fitAmpMin * 1.01) {
1928 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1932 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1937 theFitter->ReleaseParameter(0);
1945 theFitter->ReleaseParameter(0);
1946 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1951 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1952 theFitter->FixParameter(0);
1953 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1961 fitAmp = theFitter->GetParameter(1);
1965 if (fitAmp < fitAmpMin * 1.01) {
1976 if (!
m_quietFits) theFitter->GetMinuit()->fISW[4] = -1;
1978 std::vector<double> funcParams(numFitPar - 1);
1979 std::vector<double> funcParamErrs(numFitPar - 1);
1987 for (
size_t ipar = 1; ipar < numFitPar; ipar++) {
1988 funcParams[ipar - 1] = theFitter->GetParameter(ipar);
1989 funcParamErrs[ipar - 1] = theFitter->GetParError(ipar);
1997 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
2017 theFitter->ExecuteCommand(
"Cal1fcn", arglist, 1);
2059 for (
int ipar = 0; ipar < func->GetNpar(); ipar++) {
2060 double parLimitLow, parLimitHigh;
2062 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2065 "ZDCPulseAnalyzer name=" + std::string(func->GetName())
2066 +
" ipar=" + std::to_string(ipar)
2067 +
" parLimitLow=" + std::to_string(parLimitLow)
2068 +
" parLimitHigh="+ std::to_string(parLimitHigh)
2072 if (std::abs(parLimitHigh - parLimitLow) > (1e-6)*std::abs(parLimitLow)) {
2073 double value = func->GetParameter(ipar);
2074 if (value >= parLimitHigh) {
2075 value = parLimitHigh * 0.9;
2077 else if (value <= parLimitLow) {
2078 value = parLimitLow + 0.1*std::abs(parLimitLow);
2080 func->SetParameter(ipar, value);
2087 TVirtualFitter::SetDefaultFitter(
"Minuit");
2089 size_t nFitParams = func->GetNpar() + 1;
2090 std::unique_ptr<TFitter> fitter = std::make_unique<TFitter>(nFitParams);
2092 fitter->GetMinuit()->fISW[4] = -1;
2093 fitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2095 for (
size_t ipar = 0; ipar < nFitParams - 1; ipar++) {
2096 double parLimitLow, parLimitHigh;
2098 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2099 if (std::abs(parLimitHigh - parLimitLow) < (1e-6)*std::abs(parLimitLow)) {
2100 double value = func->GetParameter(ipar);
2101 double lowLim = std::min(value * 0.99, value * 1.01);
2102 double highLim = std::max(value * 0.99, value * 1.01);
2104 fitter->SetParameter(ipar + 1, func->GetParName(ipar), func->GetParameter(ipar), 0.01, lowLim, highLim);
2105 fitter->FixParameter(ipar + 1);
2108 double value = func->GetParameter(ipar);
2109 if (value >= parLimitHigh) value = parLimitHigh * 0.99;
2110 else if (value <= parLimitLow) value = parLimitLow * 1.01;
2112 double step = std::min((parLimitHigh - parLimitLow)/100., (value - parLimitLow)/100.);
2114 fitter->SetParameter(ipar + 1, func->GetParName(ipar), value, step, parLimitLow, parLimitHigh);
2125 double parLimitLow, parLimitHigh;
2128 func_p->GetParLimits(1, parLimitLow, parLimitHigh);
2130 fitter->SetParameter(2, func_p->GetParName(1), func_p->GetParameter(1), 0.01, parLimitLow, parLimitHigh);
2135 func_p->GetParLimits(parIndex, parLimitLow, parLimitHigh);
2136 fitter->SetParameter(parIndex + 1, func_p->GetParName(parIndex), func_p->GetParameter(parIndex), 0.01, parLimitLow, parLimitHigh);
2147 (*m_msgFunc_p)(
ZDCMsg::Info, (
"using delayed samples with delta T = " + std::to_string(
m_delayedDeltaT) +
", and pedestalDiff == " +
2151 std::ostringstream message1;
2152 message1 <<
"samplesSub ";
2153 for (
size_t sample = 0; sample <
m_samplesSub.size(); sample++) {
2154 message1 <<
", [" << sample <<
"] = " <<
m_samplesSub[sample];
2158 std::ostringstream message3;
2159 message3 <<
"samplesDeriv2nd ";
2170 std::string message =
"Dump of TF1: " + std::string(func->GetName());
2171 bool continueDump = (*m_msgFunc_p)(
ZDCMsg::Verbose, std::move(message));
2172 if (!continueDump)
return;
2174 unsigned int npar = func->GetNpar();
2175 for (
unsigned int ipar = 0; ipar < npar; ipar++) {
2176 std::ostringstream msgstr;
2178 double parMin = 0, parMax = 0;
2179 func->GetParLimits(ipar, parMin, parMax);
2181 msgstr <<
"Parameter " << ipar <<
", value = " << func->GetParameter(ipar) <<
", error = "
2182 << func->GetParError(ipar) <<
", min = " << parMin <<
", max = " << parMax;
2189 std::ostringstream ostrStream;
2191 (*m_msgFunc_p)(
ZDCMsg::Info, (
"ZDCPulserAnalyzer:: settings for instance: " +
m_tag));
2193 ostrStream <<
"Nsample = " <<
m_Nsample <<
" at frequency " <<
m_freqMHz <<
" MHz, preSample index = "
2196 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2199 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2204 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2207 ostrStream <<
"using delayed samples with delta T = " <<
m_delayedDeltaT <<
", and default pedestalDiff == "
2209 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2217 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2221 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2226 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2230 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2234 ostrStream <<
"Pre-exclusion enabled for up to " <<
m_maxSamplesPreExcl <<
", samples with ADC threshold HG = "
2236 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2239 ostrStream <<
"Post-exclusion enabled for up to " <<
m_maxSamplesPostExcl <<
", samples with ADC threshold HG = "
2241 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2247 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2252 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2255 ostrStream <<
"Minimum significance cuts applied: HG min. sig. = " <<
m_sigMinHG <<
", LG min. sig. " <<
m_sigMinLG;
2256 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2263 unsigned int statusMask = 0;
2300 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
2310 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(2 *
m_Nsample));
2313 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2314 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2315 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2318 for (
int iDelayPt = 0; iDelayPt < delayedHist_p->GetNbinsX(); iDelayPt++) {
2319 theGraph->SetPoint(npts, delayedHist_p->GetBinCenter(iDelayPt + 1), delayedHist_p->GetBinContent(iDelayPt + 1) -
m_delayedBaselineShift);
2320 theGraph->SetPointError(npts++, 0, delayedHist_p->GetBinError(iDelayPt + 1));
2323 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2325 theGraph->GetListOfFunctions()->Add(
new TF1(*func_p));
2326 hist_p->GetListOfFunctions()->SetOwner (
false);
2329 theGraph->SetName(( std::string(hist_p->GetName()) +
"combinaed").c_str());
2331 theGraph->SetMarkerStyle(20);
2332 theGraph->SetMarkerColor(1);
2345 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(
m_Nsample));
2348 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2349 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2350 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2353 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2354 theGraph->GetListOfFunctions()->Add(func_p);
2355 theGraph->SetName(( std::string(hist_p->GetName()) +
"not_combinaed").c_str());
2357 theGraph->SetMarkerStyle(20);
2358 theGraph->SetMarkerColor(1);
2366 unsigned int nSamples = inputData.size();
2370 unsigned int vecSize = 2*(step - 1) + nSamples - step - 1;
2371 std::vector<float> results(vecSize, 0);
2375 unsigned int fillIdx = step - 1;
2377 for (
unsigned int sample = 0; sample < nSamples - step; sample++) {
2378 int deriv = inputData[sample + step] - inputData[sample];
2379 results.at(fillIdx++) = deriv;
2387 unsigned int nSamples = inputData.size();
2392 unsigned int vecSize = 2*step + nSamples - step - 1;
2393 std::vector<float> results(vecSize, 0);
2395 unsigned int fillIndex = step;
2396 for (
unsigned int sample = step; sample < nSamples - step; sample++) {
2397 int deriv2nd = inputData[sample + step] + inputData[sample - step] - 2*inputData[sample];
2398 results.at(fillIndex++) = deriv2nd;
2419 const unsigned int nsamples = samples.size();
2427 float minScore = 1.0e9;
2428 unsigned int minIndex = 0;
2430 for (
unsigned int idx = 2; idx < nsamples - 1; idx++) {
2431 float deriv = derivVec[idx];
2432 float prevDeriv = derivVec[idx - 1];
2434 float derivDiff = deriv - prevDeriv;
2436 float deriv2nd = deriv2ndVec[idx];
2437 if (idx > nsamples - 2) deriv2nd = deriv2ndVec[idx - 1];
2442 float score = (deriv*deriv + 2*derivDiff*derivDiff +
2443 0.5*deriv2nd*deriv2nd);
2445 if (score < minScore) {
2456 if (minIndex<2 or (minIndex+1) >=nsamples){
2457 throw std::out_of_range(
"minIndex out of range in ZDCPulseAnalyzer::obtainDelayedBaselineCorr");
2459 float sample0 = samples[minIndex - 2];
2460 float sample1 = samples[minIndex - 1];
2461 float sample2 = samples[minIndex];
2462 float sample3 = samples[minIndex + 1];
2466 float baselineCorr = (0.5 * (sample1 - sample0 + sample3 - sample2) -
2467 0.25 * (sample3 - sample1 + sample2 - sample0));
2469 if (minIndex % 2 != 0) baselineCorr =-baselineCorr;
2471 return baselineCorr;
2477 std::string resultString =
"success";
2480 auto iter = config.find(key);
2481 if (iter != config.end()) {
2485 auto jsonType = iter.value().type();
2486 auto paramType = std::get<0>(descr);
2487 if (jsonType != paramType) {
2489 resultString =
"Bad type for parameter " + key +
", type in JSON = " + std::to_string((
unsigned int) jsonType) ;
2493 size_t paramSize = std::get<1>(descr);
2494 size_t jsonSize = iter.value().size();
2495 if (jsonSize != paramSize) {
2497 resultString =
"Bad length for parameter " + key +
", length in JSON = " + std::to_string(jsonSize) ;
2502 bool required = std::get<2>(descr);
2505 resultString =
"Missing required parameter " + key;
2515 for (
auto [key, value] : config.items()) {
2524 resultString =
"Unknown parameter, key = " + key;
2530 return {result, resultString};
2536 std::string resultString =
"success";
2538 for (
auto [key, value] : config.items()) {
2542 if (key ==
"Nsample")
m_Nsample = value;
2543 else if (key ==
"tag")
m_tag = value;
2544 else if (key ==
"LGMode")
m_LGMode = value;
2545 else if (key ==
"Nsample")
m_Nsample = value;
2548 else if (key ==
"FADCFreqMHz")
m_freqMHz = value;
2549 else if (key ==
"nominalPedestal")
m_pedestal = value;
2563 else if (key ==
"fixTau1")
m_fixTau1 = value;
2564 else if (key ==
"fixTau2")
m_fixTau2 = value;
2565 else if (key ==
"T0CutsHG") {
2569 else if (key ==
"T0CutsLG") {
2584 else if (key ==
"fitTimeMax") {
2590 else if (key ==
"fitAmpMinMaxHG") {
2594 else if (key ==
"fitAmpMinMaxLG") {
2598 else if (key ==
"quietFits") {
2601 else if (key ==
"enablePreExclusion") {
2607 else if (key ==
"enablePostExclusion") {
2613 else if (key ==
"enableUnderflowExclusionHG") {
2618 else if (key ==
"enableUnderflowExclusionLG") {
2623 else if (key ==
"ampMinSignifHGLG") {
2628 else if (key ==
"enableFADCCorrections") {
2629 auto fileNameJson = value[
"filename"];
2630 auto doPerSampleCorrJson = value[
"doPerSampleCorr"];
2632 if (fileNameJson.is_null() || doPerSampleCorrJson.is_null()) {
2634 resultString =
"failure processing enableFADCCorrections object";
2642 else if(key ==
"useDelayed"){
2649 else if (key ==
"enableTimingCorrection") {
2654 else if(key ==
"timeCorrCoeffHG"){
2655 for(
int i = 0;
auto coeff:value){
2660 else if(key ==
"timeCorrCoeffLG"){
2661 for(
int i = 0;
auto coeff:value){
2666 else if(key ==
"enableNLCorrection"){
2675 else if(key ==
"HGNLCorrCoeffs"){
2676 std::string HGParamsStr =
"HG coefficients = ";
2677 for (
auto coeff : value) {
2683 else if(key ==
"LGNLCorrCoeffs"){
2684 std::string LGParamsStr =
"LG coefficients = ";
2685 for(
auto coeff:value){
2693 resultString =
"unprocessed parameter";
2698 return {result, resultString};
virtual float GetTau1() const =0
void Initialize(float initialAmp, float initialT0, float ampMin, float ampMax)
virtual float GetBkgdMaxFraction() const =0
virtual void ConstrainFit()=0
virtual TF1 * GetWrapperTF1RawPtr() const
virtual std::shared_ptr< TF1 > GetWrapperTF1()
virtual float GetAmpError() const =0
virtual void UnconstrainFit()=0
virtual float GetTime() const =0
virtual float GetTau2() const =0
virtual float GetAmplitude() const =0
std::map< std::string, JSONParamDescr > JSONParamList
std::vector< bool > m_useSampleHG
std::shared_ptr< TGraphErrors > GetCombinedGraph(bool forceLG=false)
static TF1 * s_combinedFitFunc
float m_initialPrePulseAmp
std::unique_ptr< const TF1 > m_timeResFuncHG_p
void dumpTF1(const TF1 *) const
float m_chisqDivAmpOffsetLG
std::unique_ptr< TFitter > m_prePulseCombinedFitter
bool underflowExclusion() const
float m_initialPostPulseT0
unsigned int m_timeCutMode
std::unique_ptr< const TH1 > m_FADCCorrLG
size_t m_peak2ndDerivMinTolerance
std::vector< float > m_fitPulls
std::vector< float > m_ADCSSampSigHG
size_t m_peak2ndDerivMinSample
bool ScanAndSubtractSamples()
void dumpConfiguration() const
std::vector< float > m_nonLinCorrParamsLG
bool excludeEarlyLG() const
float m_peak2ndDerivMinThreshHG
static std::vector< float > s_pullValues
std::unique_ptr< const TF1 > m_timeResFuncLG_p
static TH1 * s_undelayedFitHist
std::pair< bool, std::string > ConfigFromJSON(const JSON &config)
std::unique_ptr< ZDCFitWrapper > m_defaultFitWrapper
std::string m_fitFunction
unsigned int m_preSampleIdx
std::vector< float > m_LGT0CorrParams
std::vector< float > m_samplesLGRefit
void SetTauT0Values(bool fixTau1, bool fixTau2, float tau1, float tau2, float t0HG, float t0LG)
bool fitMinimumAmplitude() const
float m_chisqDivAmpPowerLG
const TH1 * GetHistogramPtr(bool refitLG=false)
float m_peak2ndDerivMinRepassLG
void SetGainFactorsHGLG(float gainFactorHG, float gainFactorLG)
int m_lastHGOverFlowSample
void prepareLGRefit(const std::vector< float > &samplesLG, const std::vector< float > &samplesSig, const std::vector< bool > &useSamples)
unsigned int m_preExclLGADCThresh
std::vector< float > m_ADCSSampSigLG
unsigned int m_postExclLGADCThresh
unsigned int m_NSamplesAna
bool DoAnalysis(bool repass)
void setMinimumSignificance(float sigMinHG, float sigMinLG)
std::vector< float > m_samplesSig
static std::unique_ptr< TFitter > MakeCombinedFitter(TF1 *func)
std::unique_ptr< TH1 > m_delayedHistLGRefit
float m_delayedBaselineShift
unsigned int m_timingCorrMode
static std::vector< float > Calculate2ndDerivative(const std::vector< float > &inputData, unsigned int step)
unsigned int m_maxSampleEvt
int m_firstHGOverFlowSample
unsigned int m_maxSamplesPreExcl
std::vector< float > m_ADCSamplesLG
unsigned int m_underFlowExclSamplesPreLG
static float s_combinedFitTMin
std::vector< float > m_nonLinCorrParamsHG
ZDCPulseAnalyzer(ZDCMsg::MessageFunctionPtr msgFunc_p, const std::string &tag, int Nsample, float deltaTSample, size_t preSampleIdx, int pedestal, const std::string &fitFunction, int peak2ndDerivMinSample, float peak2DerivMinThreshHG, float peak2DerivMinThreshLG)
unsigned int m_underFlowExclSamplesPostHG
static std::vector< float > CalculateDerivative(const std::vector< float > &inputData, unsigned int step)
unsigned int m_maxSamplesPostExcl
void SetADCOverUnderflowValues(int HGOverflowADC, int HGUnderflowADC, int LGOverflowADC)
float m_chisqDivAmpPowerHG
std::vector< float > m_ADCSamplesHGSub
std::unique_ptr< ZDCPrePulseFitWrapper > m_prePulseFitWrapper
void UpdateFitterTimeLimits(TFitter *fitter, ZDCFitWrapper *wrapper, bool prePulse)
static float obtainDelayedBaselineCorr(const std::vector< float > &samples)
double getAmplitudeCorrection(bool highGain)
float m_peak2ndDerivMinRepassHG
unsigned int m_underFlowExclSamplesPreHG
float m_peak2ndDerivMinThreshLG
void enableRepass(float peak2ndDerivMinRepassHG, float peak2ndDerivMinRepassLG)
void enableDelayed(float deltaT, float pedestalShift, bool fixedBaseline=false)
void DoFit(bool refitLG=false)
std::vector< float > m_ADCSamplesLGSub
bool m_enableUnderflowExclHG
float m_delayedPedestalDiff
std::vector< float > m_samplesSub
void SetFitTimeMax(float tmax)
static float s_combinedFitTMax
bool AnalyzeData(size_t nSamples, size_t preSample, const std::vector< float > &samples, const std::vector< bool > &useSamples, float peak2ndDerivMinThresh, float noiseSig, const std::vector< float > &toCorrParams, ChisqCutLambdatype chisqCutLambda, float minT0Corr, float maxT0Corr)
std::vector< bool > m_useSampleLG
unsigned int m_underFlowExclSamplesPostLG
std::vector< float > m_ADCSamplesHG
void checkTF1Limits(TF1 *func)
std::vector< float > m_samplesSigLGRefit
bool m_underflowExclusion
std::vector< float > GetFitPulls(bool forceLG=false) const
bool armSumInclude() const
std::function< bool(float, float, float)> ChisqCutLambdatype
void SetCutValues(float chisqDivAmpCutHG, float chisqDivAmpCutLG, float deltaT0MinHG, float deltaT0MaxHG, float deltaT0MinLG, float deltaT0MaxLG)
std::vector< float > m_HGT0CorrParams
std::unique_ptr< TH1 > m_fitHistLGRefit
std::unique_ptr< TH1 > m_delayedHist
std::shared_ptr< TGraphErrors > GetGraph(bool forceLG=false)
unsigned int m_preExclHGADCThresh
static TH1 * s_delayedFitHist
std::unique_ptr< TH1 > m_fitHist
std::unique_ptr< TFitter > m_defaultCombinedFitter
unsigned int m_minSampleEvt
bool PSHGOverUnderflow() const
unsigned int GetStatusMask() const
float m_nonLinCorrRefScale
bool LoadAndAnalyzeData(const std::vector< float > &ADCSamplesHG, const std::vector< float > &ADCSamplesLG)
void enableTimeSigCut(bool AND, float sigCut, const std::string &TF1String, const std::vector< double > &parsHG, const std::vector< double > &parsLG)
static void CombinedPulsesFCN(int &numParam, double *, double &f, double *par, int flag)
bool m_enableUnderflowExclLG
unsigned int m_postExclHGADCThresh
bool m_haveFADCCorrections
void SetFitMinMaxAmp(float minAmpHG, float minAmpLG, float maxAmpHG, float maxAmpLG)
float m_chisqDivAmpOffsetHG
std::unique_ptr< const TH1 > m_FADCCorrHG
std::unique_ptr< ZDCPreExpFitWrapper > m_preExpFitWrapper
void Reset(bool reanalyze=false)
ZDCMsg::MessageFunctionPtr m_msgFunc_p
void FillHistogram(bool refitLG)
std::vector< float >::const_iterator SampleCIter
float m_initialPrePulseT0
static const ZDCJSONConfig::JSONParamList JSONConfigParams
std::vector< float > m_samplesDeriv2nd
std::pair< bool, std::string > ValidateJSONConfig(const JSON &config)
void DoFitCombined(bool refitLG=false)
std::string m_fadcCorrFileName
bool excludeLateLG() const
void enableFADCCorrections(bool correctPerSample, std::unique_ptr< const TH1 > &correHistHG, std::unique_ptr< const TH1 > &correHistLG)
double chi2(TH1 *h0, TH1 *h1)
std::shared_ptr< MessageFunction > MessageFunctionPtr