8#include "TFitResultPtr.h"
9#include "TVirtualFitter.h"
29 {
"tag", {JSON::value_t::string, 1,
true,
true}},
30 {
"enabled", {JSON::value_t::boolean, 1,
true,
true}},
31 {
"LGMode", {JSON::value_t::number_unsigned, 1,
false,
true}},
32 {
"Nsample", {JSON::value_t::number_integer, 1,
false,
true}},
33 {
"FADCFreqMHz", {JSON::value_t::number_integer, 1,
false,
true}},
34 {
"preSampleIdx", {JSON::value_t::number_integer, 1,
true,
true}},
35 {
"nominalPedestal", {JSON::value_t::number_integer, 1,
false,
true}},
36 {
"fitFunction", {JSON::value_t::string, 1,
true,
true}},
37 {
"peakSample", {JSON::value_t::number_integer, 1,
true,
true}},
38 {
"peakTolerance", {JSON::value_t::number_integer, 1,
true,
false}},
39 {
"2ndDerivThreshHG", {JSON::value_t::number_integer, 1,
true,
true}},
40 {
"2ndDerivThreshLG", {JSON::value_t::number_integer, 1,
true,
true}},
41 {
"2ndDerivStep", {JSON::value_t::number_integer, 1,
false,
false}},
42 {
"HGOverflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
43 {
"HGUnderflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
44 {
"LGOverflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
45 {
"nominalT0HG", {JSON::value_t::number_float, 1,
true,
true}},
46 {
"nominalT0LG", {JSON::value_t::number_float, 1,
true,
true}},
47 {
"nominalTau1", {JSON::value_t::number_float, 1,
true,
false}},
48 {
"nominalTau2", {JSON::value_t::number_float, 1,
true,
false}},
49 {
"fixTau1", {JSON::value_t::boolean, 1,
true,
false}},
50 {
"fixTau2", {JSON::value_t::boolean, 1,
true,
false}},
51 {
"T0CutsHG", {JSON::value_t::array, 2,
true,
true}},
52 {
"T0CutsLG", {JSON::value_t::array, 2,
true,
true}},
53 {
"chisqDivAmpCutHG", {JSON::value_t::number_float, 1,
true,
true}},
54 {
"chisqDivAmpCutLG", {JSON::value_t::number_float, 1,
true,
true}},
55 {
"chisqDivAmpOffsetHG", {JSON::value_t::number_float, 1,
true,
true}},
56 {
"chisqDivAmpOffsetLG", {JSON::value_t::number_float, 1,
true,
true}},
57 {
"chisqDivAmpPowerHG", {JSON::value_t::number_float, 1,
true,
true}},
58 {
"chisqDivAmpPowerLG", {JSON::value_t::number_float, 1,
true,
true}},
59 {
"gainFactorHG", {JSON::value_t::number_float, 1,
true,
true}},
60 {
"gainFactorLG", {JSON::value_t::number_float, 1,
true,
true}},
61 {
"noiseSigmaHG", {JSON::value_t::number_float, 1,
true,
true}},
62 {
"noiseSigmaLG", {JSON::value_t::number_float, 1,
true,
true}},
63 {
"enableRepass", {JSON::value_t::boolean, 1,
false,
false}},
64 {
"Repass2ndDerivThreshHG", {JSON::value_t::number_integer, 1,
true,
true}},
65 {
"Repass2ndDerivThreshLG", {JSON::value_t::number_integer, 1,
true,
true}},
66 {
"fitAmpMinMaxHG", {JSON::value_t::array, 2,
true,
false}},
67 {
"fitAmpMinMaxLG", {JSON::value_t::array, 2,
true,
false}},
68 {
"ampMinSignifHGLG", {JSON::value_t::array, 2,
true,
false}},
69 {
"enablePreExclusion", {JSON::value_t::array, 3,
false,
false}},
70 {
"enablePostExclusion", {JSON::value_t::array, 3,
false,
false}},
71 {
"enableUnderflowExclusionHG", {JSON::value_t::array, 2,
true,
false}},
72 {
"enableUnderflowExclusionLG", {JSON::value_t::array, 2,
true,
false}},
73 {
"enableTimingCorrection", {JSON::value_t::array, 2,
false,
false}},
74 {
"timeCorrCoeffHG", {JSON::value_t::array, 0,
true,
false}},
75 {
"timeCorrCoeffLG", {JSON::value_t::array, 0,
true,
false}},
76 {
"enableADCNLCorrection", {JSON::value_t::array, 3,
false,
false}},
77 {
"ADCNLCorrCoeffs", {JSON::value_t::array, 0,
true,
false}},
78 {
"useDelayed", {JSON::value_t::boolean, 1,
false,
false}},
79 {
"delayDeltaT", {JSON::value_t::number_float, 1,
true,
false}}
101 double chiSquare = 0;
103 float delayBaselineAdjust = par[0];
107 for (
int isample = 0; isample < nSamples; isample++) {
117 double pull = (histValue - funcVal) / histError;
120 chiSquare += pull * pull;
125 for (
int isample = 0; isample < nSamples; isample++) {
127 double histError = std::max(
s_delayedFitHist->GetBinError(isample + 1), 1.0);
134 double pull = (histValue - funcVal) / histError;
137 chiSquare += pull * pull;
145 const std::string& fitFunction,
int peak2ndDerivMinSample,
146 float peak2ndDerivMinThreshHG,
float peak2ndDerivMinThreshLG) :
161 m_tmin = -deltaTSample / 2;
165 std::string histName =
"ZDCFitHist" + tag;
166 std::string histNameLGRefit =
"ZDCFitHist" + tag +
"_LGRefit";
187 (*m_msgFunc_p)(
ZDCMsg::Debug,
"ConfigFromJSON produced result: "+ resultString2);
198 std::string histName =
"ZDCFitHist" +
m_tag;
199 std::string histNameLGRefit =
"ZDCFitHist" +
m_tag +
"_LGRefit";
220 std::string delayedHGName = std::string(
m_fitHist->GetName()) +
"delayed";
221 std::string delayedLGName = std::string(
m_fitHistLGRefit->GetName()) +
"delayed";
484 std::ostringstream ostrm;
495 (*m_msgFunc_p)(
ZDCMsg::Error, (
"ZDCPulseAnalyzer::SetFitTimeMax:: invalid FitTimeMax: " + std::to_string(tmax)));
512 float deltaT0MinHG,
float deltaT0MaxHG,
513 float deltaT0MinLG,
float deltaT0MaxLG)
526 const std::vector<double>& parsHG,
527 const std::vector<double>& parsLG)
534 std::string timeResHGName =
"TimeResFuncHG_" +
m_tag;
535 std::string timeResLGName =
"TimeResFuncLG_" +
m_tag;
537 TF1* funcHG_p =
new TF1(timeResHGName.c_str(), TF1String.c_str(), 0,
m_HGOverflowADC);
538 TF1* funcLG_p =
new TF1(timeResLGName.c_str(), TF1String.c_str(), 0,
m_LGOverflowADC);
540 if (parsHG.size() !=
static_cast<unsigned int>(funcHG_p->GetNpar()) ||
541 parsLG.size() !=
static_cast<unsigned int>(funcLG_p->GetNpar())) {
546 funcHG_p->SetParameters(&parsHG[0]);
547 funcLG_p->SetParameters(&parsLG[0]);
561 auto getXmin=[](
const TH1 * pH){
562 return pH->GetXaxis()->GetXmin();
564 auto getXmax=[](
const TH1 * pH){
565 return pH->GetXaxis()->GetXmax();
567 auto xmin= getXmin(correHistHG.get());
568 auto xmax= getXmax(correHistHG.get());
569 if (std::abs(
xmin+0.5) > 1e-3 || std::abs(
xmax - 4095.5) > 1e-3) {
570 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid high gain correction histogram range: xmin, xmax = " +
571 std::to_string(
xmin ) +
", " + std::to_string(
xmax) );
576 xmin= getXmin(correHistLG.get());
577 xmax= getXmax(correHistLG.get());
578 if (std::abs(
xmin+0.5) > 1e-3 ||
579 std::abs(
xmax - 4095.5) > 1e-3) {
580 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid low gain correction histogram range: xmin, xmax = " +
581 std::to_string(
xmin) +
", " + std::to_string(
xmax) );
607 std::vector<float> pulls(
m_Nsample, -100);
610 const TF1* fit_p =
static_cast<const TF1*
>(dataHist_p->GetListOfFunctions()->Last());
612 for (
size_t ibin = 0; ibin <
m_Nsample ; ibin++) {
613 float t = dataHist_p->GetBinCenter(ibin + 1);
614 float fitVal = fit_p->Eval(t);
615 float histVal = dataHist_p->GetBinContent(ibin + 1);
616 float histErr = dataHist_p->GetBinError(ibin + 1);
617 float pull = (histVal - fitVal)/histErr;
627 double amplCorrFactor = 1;
633 amplCorrFactor *= fadcCorr;
645 float invNLCorr = 1.0;
659 amplCorrFactor /= invNLCorr;
662 return amplCorrFactor;
667 float prePulseTMin = 0;
761 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting both delayed and undelayed samples");
784 const std::vector<float>& ADCSamplesHGDelayed,
const std::vector<float>& ADCSamplesLGDelayed)
787 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting only undelayed samples");
809 for (
size_t isample = 0; isample <
m_Nsample; isample++) {
810 float ADCHG = ADCSamplesHG[isample];
811 float ADCLG = ADCSamplesLG[isample];
852 bool doDump = (*m_msgFunc_p)(
ZDCMsg::Verbose,
"Dumping all samples before subtraction: ");
870 for (
size_t isample = 0; isample <
m_NSamplesAna; isample++) {
893 std::ostringstream dumpString;
894 dumpString <<
"After FADC correction, sample " << isample <<
", HG ADC = " <<
m_ADCSamplesHGSub[isample]
1047 float deriv2ndThreshHG = 0;
1048 float deriv2ndThreshLG = 0;
1068 (
float chisq,
float amp,
unsigned int fitNDoF)->
bool
1070 double ratio = chisq / (std::pow(amp, power) + offset);
1071 if (chisq/fitNDoF > 2 && ratio > cut)
return false;
1114 (
float chisq,
float amp,
unsigned int fitNDoF)->
bool
1116 double ratio = chisq / (std::pow(amp, power) + offset);
1118 if (chisq/
float(fitNDoF) > 2 && ratio > cut)
return false;
1172 const std::vector<float>& samples,
1173 const std::vector<bool>& useSample,
1174 float peak2ndDerivMinThresh,
1176 const std::vector<float>& t0CorrParams,
1178 float minT0Corr,
float maxT0Corr
1191 bool haveFirst =
false;
1192 unsigned int lastUsed = 0;
1194 for (
unsigned int sample = preSampleIdx; sample < nSamples; sample++) {
1195 if (useSample[sample]) {
1252 std::ostringstream baselineMsg;
1253 baselineMsg <<
"Delayed samples baseline correction = " <<
m_baselineCorr << std::endl;
1259 for (
size_t isample = 0; isample < nSamples; isample++) {
1270 std::for_each(
m_samplesSub.begin(),
m_samplesSub.end(), [ = ] (
float & adcUnsub) {return adcUnsub -= m_preSample;} );
1343 if (derivPresampleSig < -5) {
1349 if (!useSample[isample])
continue;
1351 float sampleSig = -
m_samplesSub[isample]/(std::sqrt(2.0)*noiseSig);
1367 float maxPrepulseSig = 0;
1368 unsigned int maxPrepulseSample = 0;
1370 for (
int isample = loopStart; isample <= loopLimit; isample++) {
1371 if (!useSample[isample])
continue;
1383 if (prePulseSig > maxPrepulseSig) {
1384 maxPrepulseSig = prePulseSig;
1385 maxPrepulseSample = isample;
1415 unsigned int postStartIdx = std::max(
static_cast<unsigned int>(
m_minDeriv2ndIndex + 2),
1419 for (
int isample = postStartIdx; isample < (int) nSamples - 1; isample++) {
1420 if (!useSample.at(isample))
continue;
1451 float derivSig = deriv/(std::sqrt(2)*noiseSig);
1460 if (std::abs(deriv2ndTest) > 0.15) {
1473 if (deriv2ndSig > 5 && deriv2ndTest < -0.1) {
1484 std::ostringstream ostrm;
1485 ostrm <<
"Post pulse found, m_maxSampleEvt = " <<
m_maxSampleEvt;
1503 std::ostringstream ostrm;
1520 float correction = 0;
1522 for (
unsigned int ipow = 0; ipow < t0CorrParams.size(); ipow++) {
1523 correction += t0CorrParams[ipow]*std::pow(t0CorrFact,
double(ipow));
1541 double timeResolution = 0;
1553 if (failFixedCut)
m_badT0 =
true;
1573 const std::vector<bool>& useSamples)
1582 for (
unsigned int idx = 0; idx < samplesLG.size(); idx++) {
1585 if (useSamples[idx]) {
1599 TH1* hist_p =
nullptr;
1604 float ampInitial, fitAmpMin, fitAmpMax, t0Initial;
1625 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1647 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1666 int fitStatus = result_ptr;
1672 if (fitStatus != 0 && result_ptr->Edm() > 0.001)
1682 if ((
int) constrFitResult_ptr != 0) {
1692 if ((
int) unconstrFitResult_ptr != 0) {
1699 if ((
int) constrFit2Result_ptr != 0) {
1706 result_ptr = constrFit2Result_ptr;
1710 result_ptr = unconstrFitResult_ptr;
1716 hist_p->GetListOfFunctions()->Clear();
1719 std::string name = func->GetName();
1721 TF1* copyFunc =
static_cast<TF1*
>(func->Clone((name +
"_copy").c_str()));
1722 hist_p->GetListOfFunctions()->Add(copyFunc);
1775 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1787 float fitAmpMin, fitAmpMax, t0Initial, ampInitial;
1803 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1829 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1834 TFitter* theFitter =
nullptr;
1858 size_t numFitPar = theFitter->GetNumberTotalParameters();
1860 theFitter->GetMinuit()->fISW[4] = -1;
1865 theFitter->GetMinuit()->fISW[4] = -1;
1868 theFitter->GetMinuit()->mnexcm(
"SET NOWarnings",
nullptr,0,ierr);
1870 else theFitter->GetMinuit()->fISW[4] = 0;
1875 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1876 theFitter->ReleaseParameter(0);
1879 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1880 theFitter->FixParameter(0);
1884 double arglist[100];
1887 int status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1889 double fitAmp = theFitter->GetParameter(1);
1893 double chi2, edm, errdef;
1896 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1901 if (status || fitAmp < fitAmpMin * 1.01 || edm > 0.01){
1906 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1907 theFitter->FixParameter(0);
1911 if (fitAmp < fitAmpMin * 1.01) {
1917 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1921 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1926 theFitter->ReleaseParameter(0);
1934 theFitter->ReleaseParameter(0);
1935 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1940 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1941 theFitter->FixParameter(0);
1942 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1950 fitAmp = theFitter->GetParameter(1);
1954 if (fitAmp < fitAmpMin * 1.01) {
1965 if (!
m_quietFits) theFitter->GetMinuit()->fISW[4] = -1;
1967 std::vector<double> funcParams(numFitPar - 1);
1968 std::vector<double> funcParamErrs(numFitPar - 1);
1976 for (
size_t ipar = 1; ipar < numFitPar; ipar++) {
1977 funcParams[ipar - 1] = theFitter->GetParameter(ipar);
1978 funcParamErrs[ipar - 1] = theFitter->GetParError(ipar);
1986 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
2006 theFitter->ExecuteCommand(
"Cal1fcn", arglist, 1);
2048 for (
int ipar = 0; ipar < func->GetNpar(); ipar++) {
2049 double parLimitLow, parLimitHigh;
2051 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2054 "ZDCPulseAnalyzer name=" + std::string(func->GetName())
2055 +
" ipar=" + std::to_string(ipar)
2056 +
" parLimitLow=" + std::to_string(parLimitLow)
2057 +
" parLimitHigh="+ std::to_string(parLimitHigh)
2062 if (std::abs(parLimitHigh - parLimitLow) > (1e-6)*std::abs(parLimitLow)) {
2063 double value = func->GetParameter(ipar);
2064 if (value >= parLimitHigh) {
2065 value = parLimitHigh * 0.9;
2067 else if (value <= parLimitLow) {
2068 value = parLimitLow + 0.1*std::abs(parLimitLow);
2070 func->SetParameter(ipar, value);
2077 TVirtualFitter::SetDefaultFitter(
"Minuit");
2079 size_t nFitParams = func->GetNpar() + 1;
2080 std::unique_ptr<TFitter> fitter = std::make_unique<TFitter>(nFitParams);
2082 fitter->GetMinuit()->fISW[4] = -1;
2083 fitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2085 for (
size_t ipar = 0; ipar < nFitParams - 1; ipar++) {
2086 double parLimitLow, parLimitHigh;
2088 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2089 if (std::abs(parLimitHigh / parLimitLow - 1) < 1e-6) {
2090 double value = func->GetParameter(ipar);
2091 double lowLim = std::min(value * 0.99, value * 1.01);
2092 double highLim = std::max(value * 0.99, value * 1.01);
2094 fitter->SetParameter(ipar + 1, func->GetParName(ipar), func->GetParameter(ipar), 0.01, lowLim, highLim);
2095 fitter->FixParameter(ipar + 1);
2098 double value = func->GetParameter(ipar);
2099 if (value >= parLimitHigh) value = parLimitHigh * 0.99;
2100 else if (value <= parLimitLow) value = parLimitLow * 1.01;
2102 double step = std::min((parLimitHigh - parLimitLow)/100., (value - parLimitLow)/100.);
2104 fitter->SetParameter(ipar + 1, func->GetParName(ipar), value, step, parLimitLow, parLimitHigh);
2115 double parLimitLow, parLimitHigh;
2118 func_p->GetParLimits(1, parLimitLow, parLimitHigh);
2120 fitter->SetParameter(2, func_p->GetParName(1), func_p->GetParameter(1), 0.01, parLimitLow, parLimitHigh);
2125 func_p->GetParLimits(parIndex, parLimitLow, parLimitHigh);
2126 fitter->SetParameter(parIndex + 1, func_p->GetParName(parIndex), func_p->GetParameter(parIndex), 0.01, parLimitLow, parLimitHigh);
2137 (*m_msgFunc_p)(
ZDCMsg::Info, (
"using delayed samples with delta T = " + std::to_string(
m_delayedDeltaT) +
", and pedestalDiff == " +
2141 std::ostringstream message1;
2142 message1 <<
"samplesSub ";
2143 for (
size_t sample = 0; sample <
m_samplesSub.size(); sample++) {
2144 message1 <<
", [" << sample <<
"] = " <<
m_samplesSub[sample];
2148 std::ostringstream message3;
2149 message3 <<
"samplesDeriv2nd ";
2160 std::string message =
"Dump of TF1: " + std::string(func->GetName());
2161 bool continueDump = (*m_msgFunc_p)(
ZDCMsg::Verbose, std::move(message));
2162 if (!continueDump)
return;
2164 unsigned int npar = func->GetNpar();
2165 for (
unsigned int ipar = 0; ipar < npar; ipar++) {
2166 std::ostringstream msgstr;
2168 double parMin = 0, parMax = 0;
2169 func->GetParLimits(ipar, parMin, parMax);
2171 msgstr <<
"Parameter " << ipar <<
", value = " << func->GetParameter(ipar) <<
", error = "
2172 << func->GetParError(ipar) <<
", min = " << parMin <<
", max = " << parMax;
2179 std::ostringstream ostrStream;
2181 (*m_msgFunc_p)(
ZDCMsg::Info, (
"ZDCPulserAnalyzer:: settings for instance: " +
m_tag));
2183 ostrStream <<
"Nsample = " <<
m_Nsample <<
" at frequency " <<
m_freqMHz <<
" MHz, preSample index = "
2186 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2189 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2194 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2197 ostrStream <<
"using delayed samples with delta T = " <<
m_delayedDeltaT <<
", and default pedestalDiff == "
2199 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2207 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2211 (*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();
2224 ostrStream <<
"Pre-exclusion enabled for up to " <<
m_maxSamplesPreExcl <<
", samples with ADC threshold HG = "
2226 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2229 ostrStream <<
"Post-exclusion enabled for up to " <<
m_maxSamplesPostExcl <<
", samples with ADC threshold HG = "
2231 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2237 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2242 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2245 ostrStream <<
"Minimum significance cuts applied: HG min. sig. = " <<
m_sigMinHG <<
", LG min. sig. " <<
m_sigMinLG;
2246 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2253 unsigned int statusMask = 0;
2290 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
2300 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(2 *
m_Nsample));
2303 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2304 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2305 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2308 for (
int iDelayPt = 0; iDelayPt < delayedHist_p->GetNbinsX(); iDelayPt++) {
2309 theGraph->SetPoint(npts, delayedHist_p->GetBinCenter(iDelayPt + 1), delayedHist_p->GetBinContent(iDelayPt + 1) -
m_delayedBaselineShift);
2310 theGraph->SetPointError(npts++, 0, delayedHist_p->GetBinError(iDelayPt + 1));
2313 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2315 theGraph->GetListOfFunctions()->Add(
new TF1(*func_p));
2316 hist_p->GetListOfFunctions()->SetOwner (
false);
2319 theGraph->SetName(( std::string(hist_p->GetName()) +
"combinaed").c_str());
2321 theGraph->SetMarkerStyle(20);
2322 theGraph->SetMarkerColor(1);
2335 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(
m_Nsample));
2338 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2339 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2340 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2343 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2344 theGraph->GetListOfFunctions()->Add(func_p);
2345 theGraph->SetName(( std::string(hist_p->GetName()) +
"not_combinaed").c_str());
2347 theGraph->SetMarkerStyle(20);
2348 theGraph->SetMarkerColor(1);
2356 unsigned int nSamples = inputData.size();
2360 unsigned int vecSize = 2*(step - 1) + nSamples - step - 1;
2361 std::vector<float> results(vecSize, 0);
2365 unsigned int fillIdx = step - 1;
2367 for (
unsigned int sample = 0; sample < nSamples - step; sample++) {
2368 int deriv = inputData[sample + step] - inputData[sample];
2369 results.at(fillIdx++) = deriv;
2377 unsigned int nSamples = inputData.size();
2382 unsigned int vecSize = 2*step + nSamples - step - 1;
2383 std::vector<float> results(vecSize, 0);
2385 unsigned int fillIndex = step;
2386 for (
unsigned int sample = step; sample < nSamples - step; sample++) {
2387 int deriv2nd = inputData[sample + step] + inputData[sample - step] - 2*inputData[sample];
2388 results.at(fillIndex++) = deriv2nd;
2409 const unsigned int nsamples = samples.size();
2417 float minScore = 1.0e9;
2418 unsigned int minIndex = 0;
2420 for (
unsigned int idx = 2; idx < nsamples - 1; idx++) {
2421 float deriv = derivVec[idx];
2422 float prevDeriv = derivVec[idx - 1];
2424 float derivDiff = deriv - prevDeriv;
2426 float deriv2nd = deriv2ndVec[idx];
2427 if (idx > nsamples - 2) deriv2nd = deriv2ndVec[idx - 1];
2432 float score = (deriv*deriv + 2*derivDiff*derivDiff +
2433 0.5*deriv2nd*deriv2nd);
2435 if (score < minScore) {
2446 if (minIndex<2 or (minIndex+1) >=nsamples){
2447 throw std::out_of_range(
"minIndex out of range in ZDCPulseAnalyzer::obtainDelayedBaselineCorr");
2449 float sample0 = samples[minIndex - 2];
2450 float sample1 = samples[minIndex - 1];
2451 float sample2 = samples[minIndex];
2452 float sample3 = samples[minIndex + 1];
2456 float baselineCorr = (0.5 * (sample1 - sample0 + sample3 - sample2) -
2457 0.25 * (sample3 - sample1 + sample2 - sample0));
2459 if (minIndex % 2 != 0) baselineCorr =-baselineCorr;
2461 return baselineCorr;
2467 std::string resultString =
"success";
2470 auto iter =
config.find(key);
2471 if (iter !=
config.end()) {
2475 auto jsonType = iter.value().type();
2476 auto paramType = std::get<0>(descr);
2477 if (jsonType != paramType) {
2479 resultString =
"Bad type for parameter " + key +
", type in JSON = " + std::to_string((
unsigned int) jsonType) ;
2483 size_t paramSize = std::get<1>(descr);
2484 size_t jsonSize = iter.value().size();
2485 if (jsonSize != paramSize) {
2487 resultString =
"Bad length for parameter " + key +
", length in JSON = " + std::to_string(jsonSize) ;
2492 bool required = std::get<2>(descr);
2495 resultString =
"Missing required parameter " + key;
2505 for (
auto [key, value] :
config.items()) {
2514 resultString =
"Unknown parameter, key = " + key;
2520 return {
result, resultString};
2526 std::string resultString =
"success";
2528 for (
auto [key, value] :
config.items()) {
2532 if (key ==
"Nsample")
m_Nsample = value;
2533 else if (key ==
"tag")
m_tag = value;
2534 else if (key ==
"LGMode")
m_LGMode = value;
2535 else if (key ==
"Nsample")
m_Nsample = value;
2538 else if (key ==
"FADCFreqMHz")
m_freqMHz = value;
2539 else if (key ==
"nominalPedestal")
m_pedestal = value;
2553 else if (key ==
"fixTau1")
m_fixTau1 = value;
2554 else if (key ==
"fixTau2")
m_fixTau2 = value;
2555 else if (key ==
"T0CutsHG") {
2559 else if (key ==
"T0CutsLG") {
2576 else if (key ==
"fitAmpMinMaxHG") {
2580 else if (key ==
"fitAmpMinMaxLG") {
2584 else if (key ==
"quietFits") {
2587 else if (key ==
"enablePreExclusion") {
2593 else if (key ==
"enablePostExclusion") {
2599 else if (key ==
"enableUnderflowExclusionHG") {
2604 else if (key ==
"enableUnderflowExclusionLG") {
2609 else if (key ==
"ampMinSignifHGLG") {
2614 else if (key ==
"enableFADCCorrections") {
2615 auto fileNameJson = value[
"filename"];
2616 auto doPerSampleCorrJson = value[
"doPerSampleCorr"];
2618 if (fileNameJson.is_null() || doPerSampleCorrJson.is_null()) {
2620 std::string resultString =
"failure processing enableFADCCorrections object";
2630 std::string resultString =
"unprocessed parameter";
2635 return {
result, resultString};
constexpr int pow(int base, int exp) noexcept
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