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;
171 std::string histName =
"ZDCFitHist" + tag;
172 std::string histNameLGRefit =
"ZDCFitHist" + tag +
"_LGRefit";
193 (*m_msgFunc_p)(
ZDCMsg::Debug,
"ConfigFromJSON produced result: "+ resultString2);
204 std::string histName =
"ZDCFitHist" +
m_tag;
205 std::string histNameLGRefit =
"ZDCFitHist" +
m_tag +
"_LGRefit";
230 std::string delayedHGName = std::string(
m_fitHist->GetName()) +
"delayed";
231 std::string delayedLGName = std::string(
m_fitHistLGRefit->GetName()) +
"delayed";
494 std::ostringstream ostrm;
505 (*m_msgFunc_p)(
ZDCMsg::Error, (
"ZDCPulseAnalyzer::SetFitTimeMax:: invalid FitTimeMax: " + std::to_string(tmax)));
522 float deltaT0MinHG,
float deltaT0MaxHG,
523 float deltaT0MinLG,
float deltaT0MaxLG)
536 const std::vector<double>& parsHG,
537 const std::vector<double>& parsLG)
544 std::string timeResHGName =
"TimeResFuncHG_" +
m_tag;
545 std::string timeResLGName =
"TimeResFuncLG_" +
m_tag;
547 TF1* funcHG_p =
new TF1(timeResHGName.c_str(), TF1String.c_str(), 0,
m_HGOverflowADC);
548 TF1* funcLG_p =
new TF1(timeResLGName.c_str(), TF1String.c_str(), 0,
m_LGOverflowADC);
550 if (parsHG.size() !=
static_cast<unsigned int>(funcHG_p->GetNpar()) ||
551 parsLG.size() !=
static_cast<unsigned int>(funcLG_p->GetNpar())) {
556 funcHG_p->SetParameters(&parsHG[0]);
557 funcLG_p->SetParameters(&parsLG[0]);
571 auto getXmin=[](
const TH1 * pH){
572 return pH->GetXaxis()->GetXmin();
574 auto getXmax=[](
const TH1 * pH){
575 return pH->GetXaxis()->GetXmax();
577 auto xmin= getXmin(correHistHG.get());
578 auto xmax= getXmax(correHistHG.get());
579 if (std::abs(
xmin+0.5) > 1e-3 || std::abs(
xmax - 4095.5) > 1e-3) {
580 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid high gain correction histogram range: xmin, xmax = " +
581 std::to_string(
xmin ) +
", " + std::to_string(
xmax) );
586 xmin= getXmin(correHistLG.get());
587 xmax= getXmax(correHistLG.get());
588 if (std::abs(
xmin+0.5) > 1e-3 ||
589 std::abs(
xmax - 4095.5) > 1e-3) {
590 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid low gain correction histogram range: xmin, xmax = " +
591 std::to_string(
xmin) +
", " + std::to_string(
xmax) );
617 std::vector<float> pulls(
m_Nsample, -100);
620 const TF1* fit_p =
static_cast<const TF1*
>(dataHist_p->GetListOfFunctions()->Last());
622 for (
size_t ibin = 0; ibin <
m_Nsample ; ibin++) {
623 float t = dataHist_p->GetBinCenter(ibin + 1);
624 float fitVal = fit_p->Eval(t);
625 float histVal = dataHist_p->GetBinContent(ibin + 1);
626 float histErr = dataHist_p->GetBinError(ibin + 1);
627 float pull = (histVal - fitVal)/histErr;
637 double amplCorrFactor = 1;
643 amplCorrFactor *= fadcCorr;
655 float invNLCorr = 1.0;
669 amplCorrFactor /= invNLCorr;
672 return amplCorrFactor;
677 float prePulseTMin = 0;
771 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting both delayed and undelayed samples");
794 const std::vector<float>& ADCSamplesHGDelayed,
const std::vector<float>& ADCSamplesLGDelayed)
797 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting only undelayed samples");
819 for (
size_t isample = 0; isample <
m_Nsample; isample++) {
820 float ADCHG = ADCSamplesHG[isample];
821 float ADCLG = ADCSamplesLG[isample];
862 bool doDump = (*m_msgFunc_p)(
ZDCMsg::Verbose,
"Dumping all samples before subtraction: ");
880 for (
size_t isample = 0; isample <
m_NSamplesAna; isample++) {
903 std::ostringstream dumpString;
904 dumpString <<
"After FADC correction, sample " << isample <<
", HG ADC = " <<
m_ADCSamplesHGSub[isample]
1057 float deriv2ndThreshHG = 0;
1058 float deriv2ndThreshLG = 0;
1078 (
float chisq,
float amp,
unsigned int fitNDoF)->
bool
1080 double ratio = chisq / (std::pow(amp, power) + offset);
1081 if (chisq/fitNDoF > 2 && ratio > cut)
return false;
1124 (
float chisq,
float amp,
unsigned int fitNDoF)->
bool
1126 double ratio = chisq / (std::pow(amp, power) + offset);
1128 if (chisq/
float(fitNDoF) > 2 && ratio > cut)
return false;
1182 const std::vector<float>& samples,
1183 const std::vector<bool>& useSample,
1184 float peak2ndDerivMinThresh,
1186 const std::vector<float>& t0CorrParams,
1188 float minT0Corr,
float maxT0Corr
1201 bool haveFirst =
false;
1202 unsigned int lastUsed = 0;
1204 for (
unsigned int sample = preSampleIdx; sample < nSamples; sample++) {
1205 if (useSample[sample]) {
1262 std::ostringstream baselineMsg;
1263 baselineMsg <<
"Delayed samples baseline correction = " <<
m_baselineCorr << std::endl;
1269 for (
size_t isample = 0; isample < nSamples; isample++) {
1280 std::for_each(
m_samplesSub.begin(),
m_samplesSub.end(), [ = ] (
float & adcUnsub) {return adcUnsub -= m_preSample;} );
1353 if (derivPresampleSig < -5) {
1359 if (!useSample[isample])
continue;
1361 float sampleSig = -
m_samplesSub[isample]/(std::sqrt(2.0)*noiseSig);
1377 float maxPrepulseSig = 0;
1378 unsigned int maxPrepulseSample = 0;
1380 for (
int isample = loopStart; isample <= loopLimit; isample++) {
1381 if (!useSample[isample])
continue;
1393 if (prePulseSig > maxPrepulseSig) {
1394 maxPrepulseSig = prePulseSig;
1395 maxPrepulseSample = isample;
1425 unsigned int postStartIdx = std::max(
static_cast<unsigned int>(
m_minDeriv2ndIndex + 2),
1429 for (
int isample = postStartIdx; isample < (int) nSamples - 1; isample++) {
1430 if (!useSample.at(isample))
continue;
1461 float derivSig = deriv/(std::sqrt(2)*noiseSig);
1470 if (std::abs(deriv2ndTest) > 0.15) {
1483 if (deriv2ndSig > 5 && deriv2ndTest < -0.1) {
1494 std::ostringstream ostrm;
1495 ostrm <<
"Post pulse found, m_maxSampleEvt = " <<
m_maxSampleEvt;
1513 std::ostringstream ostrm;
1530 float correction = 0;
1532 for (
unsigned int ipow = 0; ipow < t0CorrParams.size(); ipow++) {
1533 correction += t0CorrParams[ipow]*std::pow(t0CorrFact,
double(ipow));
1551 double timeResolution = 0;
1563 if (failFixedCut)
m_badT0 =
true;
1583 const std::vector<bool>& useSamples)
1592 for (
unsigned int idx = 0; idx < samplesLG.size(); idx++) {
1595 if (useSamples[idx]) {
1609 TH1* hist_p =
nullptr;
1614 float ampInitial, fitAmpMin, fitAmpMax, t0Initial;
1635 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1657 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1676 int fitStatus = result_ptr;
1682 if (fitStatus != 0 && result_ptr->Edm() > 0.001)
1692 if ((
int) constrFitResult_ptr != 0) {
1702 if ((
int) unconstrFitResult_ptr != 0) {
1709 if ((
int) constrFit2Result_ptr != 0) {
1716 result_ptr = constrFit2Result_ptr;
1720 result_ptr = unconstrFitResult_ptr;
1726 hist_p->GetListOfFunctions()->Clear();
1729 std::string name = func->GetName();
1731 TF1* copyFunc =
static_cast<TF1*
>(func->Clone((name +
"_copy").c_str()));
1732 hist_p->GetListOfFunctions()->Add(copyFunc);
1785 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1797 float fitAmpMin, fitAmpMax, t0Initial, ampInitial;
1813 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1839 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1844 TFitter* theFitter =
nullptr;
1868 size_t numFitPar = theFitter->GetNumberTotalParameters();
1870 theFitter->GetMinuit()->fISW[4] = -1;
1875 theFitter->GetMinuit()->fISW[4] = -1;
1878 theFitter->GetMinuit()->mnexcm(
"SET NOWarnings",
nullptr,0,ierr);
1880 else theFitter->GetMinuit()->fISW[4] = 0;
1885 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1886 theFitter->ReleaseParameter(0);
1889 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1890 theFitter->FixParameter(0);
1894 double arglist[100];
1897 int status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1899 double fitAmp = theFitter->GetParameter(1);
1903 double chi2, edm, errdef;
1906 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1911 if (status || fitAmp < fitAmpMin * 1.01 || edm > 0.01){
1916 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1917 theFitter->FixParameter(0);
1921 if (fitAmp < fitAmpMin * 1.01) {
1927 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1931 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1936 theFitter->ReleaseParameter(0);
1944 theFitter->ReleaseParameter(0);
1945 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1950 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1951 theFitter->FixParameter(0);
1952 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1960 fitAmp = theFitter->GetParameter(1);
1964 if (fitAmp < fitAmpMin * 1.01) {
1975 if (!
m_quietFits) theFitter->GetMinuit()->fISW[4] = -1;
1977 std::vector<double> funcParams(numFitPar - 1);
1978 std::vector<double> funcParamErrs(numFitPar - 1);
1986 for (
size_t ipar = 1; ipar < numFitPar; ipar++) {
1987 funcParams[ipar - 1] = theFitter->GetParameter(ipar);
1988 funcParamErrs[ipar - 1] = theFitter->GetParError(ipar);
1996 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
2016 theFitter->ExecuteCommand(
"Cal1fcn", arglist, 1);
2058 for (
int ipar = 0; ipar < func->GetNpar(); ipar++) {
2059 double parLimitLow, parLimitHigh;
2061 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2064 "ZDCPulseAnalyzer name=" + std::string(func->GetName())
2065 +
" ipar=" + std::to_string(ipar)
2066 +
" parLimitLow=" + std::to_string(parLimitLow)
2067 +
" parLimitHigh="+ std::to_string(parLimitHigh)
2071 if (std::abs(parLimitHigh - parLimitLow) > (1e-6)*std::abs(parLimitLow)) {
2072 double value = func->GetParameter(ipar);
2073 if (value >= parLimitHigh) {
2074 value = parLimitHigh * 0.9;
2076 else if (value <= parLimitLow) {
2077 value = parLimitLow + 0.1*std::abs(parLimitLow);
2079 func->SetParameter(ipar, value);
2086 TVirtualFitter::SetDefaultFitter(
"Minuit");
2088 size_t nFitParams = func->GetNpar() + 1;
2089 std::unique_ptr<TFitter> fitter = std::make_unique<TFitter>(nFitParams);
2091 fitter->GetMinuit()->fISW[4] = -1;
2092 fitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2094 for (
size_t ipar = 0; ipar < nFitParams - 1; ipar++) {
2095 double parLimitLow, parLimitHigh;
2097 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2098 if (std::abs(parLimitHigh - parLimitLow) < (1e-6)*std::abs(parLimitLow)) {
2099 double value = func->GetParameter(ipar);
2100 double lowLim = std::min(value * 0.99, value * 1.01);
2101 double highLim = std::max(value * 0.99, value * 1.01);
2103 fitter->SetParameter(ipar + 1, func->GetParName(ipar), func->GetParameter(ipar), 0.01, lowLim, highLim);
2104 fitter->FixParameter(ipar + 1);
2107 double value = func->GetParameter(ipar);
2108 if (value >= parLimitHigh) value = parLimitHigh * 0.99;
2109 else if (value <= parLimitLow) value = parLimitLow * 1.01;
2111 double step = std::min((parLimitHigh - parLimitLow)/100., (value - parLimitLow)/100.);
2113 fitter->SetParameter(ipar + 1, func->GetParName(ipar), value, step, parLimitLow, parLimitHigh);
2124 double parLimitLow, parLimitHigh;
2127 func_p->GetParLimits(1, parLimitLow, parLimitHigh);
2129 fitter->SetParameter(2, func_p->GetParName(1), func_p->GetParameter(1), 0.01, parLimitLow, parLimitHigh);
2134 func_p->GetParLimits(parIndex, parLimitLow, parLimitHigh);
2135 fitter->SetParameter(parIndex + 1, func_p->GetParName(parIndex), func_p->GetParameter(parIndex), 0.01, parLimitLow, parLimitHigh);
2146 (*m_msgFunc_p)(
ZDCMsg::Info, (
"using delayed samples with delta T = " + std::to_string(
m_delayedDeltaT) +
", and pedestalDiff == " +
2150 std::ostringstream message1;
2151 message1 <<
"samplesSub ";
2152 for (
size_t sample = 0; sample <
m_samplesSub.size(); sample++) {
2153 message1 <<
", [" << sample <<
"] = " <<
m_samplesSub[sample];
2157 std::ostringstream message3;
2158 message3 <<
"samplesDeriv2nd ";
2169 std::string message =
"Dump of TF1: " + std::string(func->GetName());
2170 bool continueDump = (*m_msgFunc_p)(
ZDCMsg::Verbose, std::move(message));
2171 if (!continueDump)
return;
2173 unsigned int npar = func->GetNpar();
2174 for (
unsigned int ipar = 0; ipar < npar; ipar++) {
2175 std::ostringstream msgstr;
2177 double parMin = 0, parMax = 0;
2178 func->GetParLimits(ipar, parMin, parMax);
2180 msgstr <<
"Parameter " << ipar <<
", value = " << func->GetParameter(ipar) <<
", error = "
2181 << func->GetParError(ipar) <<
", min = " << parMin <<
", max = " << parMax;
2188 std::ostringstream ostrStream;
2190 (*m_msgFunc_p)(
ZDCMsg::Info, (
"ZDCPulserAnalyzer:: settings for instance: " +
m_tag));
2192 ostrStream <<
"Nsample = " <<
m_Nsample <<
" at frequency " <<
m_freqMHz <<
" MHz, preSample index = "
2195 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2198 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2203 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2206 ostrStream <<
"using delayed samples with delta T = " <<
m_delayedDeltaT <<
", and default pedestalDiff == "
2208 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2216 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2220 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2225 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2229 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2233 ostrStream <<
"Pre-exclusion enabled for up to " <<
m_maxSamplesPreExcl <<
", samples with ADC threshold HG = "
2235 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2238 ostrStream <<
"Post-exclusion enabled for up to " <<
m_maxSamplesPostExcl <<
", samples with ADC threshold HG = "
2240 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2246 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2251 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2254 ostrStream <<
"Minimum significance cuts applied: HG min. sig. = " <<
m_sigMinHG <<
", LG min. sig. " <<
m_sigMinLG;
2255 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2262 unsigned int statusMask = 0;
2299 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
2309 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(2 *
m_Nsample));
2312 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2313 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2314 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2317 for (
int iDelayPt = 0; iDelayPt < delayedHist_p->GetNbinsX(); iDelayPt++) {
2318 theGraph->SetPoint(npts, delayedHist_p->GetBinCenter(iDelayPt + 1), delayedHist_p->GetBinContent(iDelayPt + 1) -
m_delayedBaselineShift);
2319 theGraph->SetPointError(npts++, 0, delayedHist_p->GetBinError(iDelayPt + 1));
2322 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2324 theGraph->GetListOfFunctions()->Add(
new TF1(*func_p));
2325 hist_p->GetListOfFunctions()->SetOwner (
false);
2328 theGraph->SetName(( std::string(hist_p->GetName()) +
"combinaed").c_str());
2330 theGraph->SetMarkerStyle(20);
2331 theGraph->SetMarkerColor(1);
2344 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(
m_Nsample));
2347 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2348 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2349 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2352 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2353 theGraph->GetListOfFunctions()->Add(func_p);
2354 theGraph->SetName(( std::string(hist_p->GetName()) +
"not_combinaed").c_str());
2356 theGraph->SetMarkerStyle(20);
2357 theGraph->SetMarkerColor(1);
2365 unsigned int nSamples = inputData.size();
2369 unsigned int vecSize = 2*(step - 1) + nSamples - step - 1;
2370 std::vector<float> results(vecSize, 0);
2374 unsigned int fillIdx = step - 1;
2376 for (
unsigned int sample = 0; sample < nSamples - step; sample++) {
2377 int deriv = inputData[sample + step] - inputData[sample];
2378 results.at(fillIdx++) = deriv;
2386 unsigned int nSamples = inputData.size();
2391 unsigned int vecSize = 2*step + nSamples - step - 1;
2392 std::vector<float> results(vecSize, 0);
2394 unsigned int fillIndex = step;
2395 for (
unsigned int sample = step; sample < nSamples - step; sample++) {
2396 int deriv2nd = inputData[sample + step] + inputData[sample - step] - 2*inputData[sample];
2397 results.at(fillIndex++) = deriv2nd;
2418 const unsigned int nsamples = samples.size();
2426 float minScore = 1.0e9;
2427 unsigned int minIndex = 0;
2429 for (
unsigned int idx = 2; idx < nsamples - 1; idx++) {
2430 float deriv = derivVec[idx];
2431 float prevDeriv = derivVec[idx - 1];
2433 float derivDiff = deriv - prevDeriv;
2435 float deriv2nd = deriv2ndVec[idx];
2436 if (idx > nsamples - 2) deriv2nd = deriv2ndVec[idx - 1];
2441 float score = (deriv*deriv + 2*derivDiff*derivDiff +
2442 0.5*deriv2nd*deriv2nd);
2444 if (score < minScore) {
2455 if (minIndex<2 or (minIndex+1) >=nsamples){
2456 throw std::out_of_range(
"minIndex out of range in ZDCPulseAnalyzer::obtainDelayedBaselineCorr");
2458 float sample0 = samples[minIndex - 2];
2459 float sample1 = samples[minIndex - 1];
2460 float sample2 = samples[minIndex];
2461 float sample3 = samples[minIndex + 1];
2465 float baselineCorr = (0.5 * (sample1 - sample0 + sample3 - sample2) -
2466 0.25 * (sample3 - sample1 + sample2 - sample0));
2468 if (minIndex % 2 != 0) baselineCorr =-baselineCorr;
2470 return baselineCorr;
2476 std::string resultString =
"success";
2479 auto iter =
config.find(key);
2480 if (iter !=
config.end()) {
2484 auto jsonType = iter.value().type();
2485 auto paramType = std::get<0>(descr);
2486 if (jsonType != paramType) {
2488 resultString =
"Bad type for parameter " + key +
", type in JSON = " + std::to_string((
unsigned int) jsonType) ;
2492 size_t paramSize = std::get<1>(descr);
2493 size_t jsonSize = iter.value().size();
2494 if (jsonSize != paramSize) {
2496 resultString =
"Bad length for parameter " + key +
", length in JSON = " + std::to_string(jsonSize) ;
2501 bool required = std::get<2>(descr);
2504 resultString =
"Missing required parameter " + key;
2514 for (
auto [key, value] :
config.items()) {
2523 resultString =
"Unknown parameter, key = " + key;
2529 return {
result, resultString};
2535 std::string resultString =
"success";
2537 for (
auto [key, value] :
config.items()) {
2541 if (key ==
"Nsample")
m_Nsample = value;
2542 else if (key ==
"tag")
m_tag = value;
2543 else if (key ==
"LGMode")
m_LGMode = value;
2544 else if (key ==
"Nsample")
m_Nsample = value;
2547 else if (key ==
"FADCFreqMHz")
m_freqMHz = value;
2548 else if (key ==
"nominalPedestal")
m_pedestal = value;
2562 else if (key ==
"fixTau1")
m_fixTau1 = value;
2563 else if (key ==
"fixTau2")
m_fixTau2 = value;
2564 else if (key ==
"T0CutsHG") {
2568 else if (key ==
"T0CutsLG") {
2583 else if (key ==
"fitTimeMax") {
2589 else if (key ==
"fitAmpMinMaxHG") {
2593 else if (key ==
"fitAmpMinMaxLG") {
2597 else if (key ==
"quietFits") {
2600 else if (key ==
"enablePreExclusion") {
2606 else if (key ==
"enablePostExclusion") {
2612 else if (key ==
"enableUnderflowExclusionHG") {
2617 else if (key ==
"enableUnderflowExclusionLG") {
2622 else if (key ==
"ampMinSignifHGLG") {
2627 else if (key ==
"enableFADCCorrections") {
2628 auto fileNameJson = value[
"filename"];
2629 auto doPerSampleCorrJson = value[
"doPerSampleCorr"];
2631 if (fileNameJson.is_null() || doPerSampleCorrJson.is_null()) {
2633 resultString =
"failure processing enableFADCCorrections object";
2641 else if(key ==
"useDelayed"){
2648 else if (key ==
"enableTimingCorrection") {
2653 else if(key ==
"timeCorrCoeffHG"){
2654 for(
int i = 0;
auto coeff:value){
2659 else if(key ==
"timeCorrCoeffLG"){
2660 for(
int i = 0;
auto coeff:value){
2665 else if(key ==
"enableNLCorrection"){
2674 else if(key ==
"HGNLCorrCoeffs"){
2675 std::string HGParamsStr =
"HG coefficients = ";
2676 for (
auto coeff : value) {
2682 else if(key ==
"LGNLCorrCoeffs"){
2683 std::string LGParamsStr =
"LG coefficients = ";
2684 for(
auto coeff:value){
2692 resultString =
"unprocessed parameter";
2697 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