8#include "TFitResultPtr.h"
9#include "TVirtualFitter.h"
24template<
typename T> T
Sqr(T in) {
return in*in;}
34 {
"tag", {JSON::value_t::string, 1,
true,
true}},
35 {
"enabled", {JSON::value_t::boolean, 1,
true,
true}},
36 {
"LGMode", {JSON::value_t::number_unsigned, 1,
false,
true}},
37 {
"Nsample", {JSON::value_t::number_integer, 1,
false,
true}},
38 {
"FADCFreqMHz", {JSON::value_t::number_integer, 1,
false,
true}},
39 {
"preSampleIdx", {JSON::value_t::number_integer, 1,
true,
true}},
40 {
"nominalPedestal", {JSON::value_t::number_integer, 1,
false,
true}},
41 {
"fitFunction", {JSON::value_t::string, 1,
true,
true}},
42 {
"peakSample", {JSON::value_t::number_integer, 1,
true,
true}},
43 {
"peakTolerance", {JSON::value_t::number_integer, 1,
true,
false}},
44 {
"quietFits", {JSON::value_t::boolean, 1,
false,
false}},
45 {
"2ndDerivThreshHG", {JSON::value_t::number_integer, 1,
true,
true}},
46 {
"2ndDerivThreshLG", {JSON::value_t::number_integer, 1,
true,
true}},
47 {
"2ndDerivStep", {JSON::value_t::number_integer, 1,
false,
false}},
48 {
"HGOverflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
49 {
"HGUnderflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
50 {
"LGOverflowADC", {JSON::value_t::number_integer, 1,
true,
true}},
51 {
"nominalT0HG", {JSON::value_t::number_float, 1,
true,
true}},
52 {
"nominalT0LG", {JSON::value_t::number_float, 1,
true,
true}},
53 {
"nominalTau1", {JSON::value_t::number_float, 1,
true,
false}},
54 {
"nominalTau2", {JSON::value_t::number_float, 1,
true,
false}},
55 {
"fixTau1", {JSON::value_t::boolean, 1,
true,
false}},
56 {
"fixTau2", {JSON::value_t::boolean, 1,
true,
false}},
57 {
"T0CutsHG", {JSON::value_t::array, 2,
true,
true}},
58 {
"T0CutsLG", {JSON::value_t::array, 2,
true,
true}},
59 {
"chisqDivAmpCutHG", {JSON::value_t::number_float, 1,
true,
true}},
60 {
"chisqDivAmpCutLG", {JSON::value_t::number_float, 1,
true,
true}},
61 {
"chisqDivAmpScaleHG", {JSON::value_t::number_float, 1,
true,
true}},
62 {
"chisqDivAmpScaleLG", {JSON::value_t::number_float, 1,
true,
true}},
63 {
"chisqDivAmpOffsetHG", {JSON::value_t::number_float, 1,
true,
true}},
64 {
"chisqDivAmpOffsetLG", {JSON::value_t::number_float, 1,
true,
true}},
65 {
"chisqDivAmpPowerHG", {JSON::value_t::number_float, 1,
true,
true}},
66 {
"chisqDivAmpPowerLG", {JSON::value_t::number_float, 1,
true,
true}},
67 {
"gainFactorHG", {JSON::value_t::number_float, 1,
true,
true}},
68 {
"gainFactorLG", {JSON::value_t::number_float, 1,
true,
true}},
69 {
"noiseSigmaHG", {JSON::value_t::number_float, 1,
true,
true}},
70 {
"noiseSigmaLG", {JSON::value_t::number_float, 1,
true,
true}},
71 {
"perSampleNoiseSigmaHG", {JSON::value_t::array, -1,
true,
true}},
72 {
"perSampleNoiseSigmaLG", {JSON::value_t::array, -1,
true,
true}},
73 {
"enableRepass", {JSON::value_t::boolean, 1,
false,
false}},
74 {
"Repass2ndDerivThreshHG", {JSON::value_t::number_integer, 1,
true,
true}},
75 {
"Repass2ndDerivThreshLG", {JSON::value_t::number_integer, 1,
true,
true}},
76 {
"fitAmpMinMaxHG", {JSON::value_t::array, 2,
true,
false}},
77 {
"fitAmpMinMaxLG", {JSON::value_t::array, 2,
true,
false}},
78 {
"ampMinSignifHGLG", {JSON::value_t::array, 2,
true,
false}},
79 {
"enablePreExclusion", {JSON::value_t::array, 3,
false,
false}},
80 {
"enablePostExclusion", {JSON::value_t::array, 3,
false,
false}},
81 {
"enableUnderflowExclusionHG", {JSON::value_t::array, 2,
true,
false}},
82 {
"enableUnderflowExclusionLG", {JSON::value_t::array, 2,
true,
false}},
83 {
"enableTimingCorrection", {JSON::value_t::array, 3,
false,
false}},
84 {
"timeCorrCoeffHG", {JSON::value_t::array, 6,
true,
false}},
85 {
"timeCorrCoeffLG", {JSON::value_t::array, 6,
true,
false}},
86 {
"enableADCNLCorrection", {JSON::value_t::array, 3,
false,
false}},
87 {
"ADCNLCorrCoeffs", {JSON::value_t::array, 0,
true,
false}},
88 {
"enableNLCorrection", {JSON::value_t::array, 2,
false,
false}},
89 {
"HGNLCorrCoeffs", {JSON::value_t::array, 5,
true,
false}},
90 {
"LGNLCorrCoeffs", {JSON::value_t::array, 5,
true,
false}},
91 {
"useDelayed", {JSON::value_t::boolean, 1,
false,
false}},
92 {
"delayDeltaT", {JSON::value_t::number_float, 1,
true,
false}},
93 {
"delayDefaultPedestalShift", {JSON::value_t::number_float, 1,
true,
false}},
94 {
"fitTimeMax", {JSON::value_t::number_float, 1,
true,
false}}
115 double chiSquare = 0;
117 float delayBaselineAdjust = par[0];
121 for (
int isample = 0; isample < nSamples; isample++) {
131 double pull = (histValue - funcVal) / histError;
134 chiSquare += pull * pull;
139 for (
int isample = 0; isample < nSamples; isample++) {
141 double histError = std::max(
s_delayedFitHist->GetBinError(isample + 1), 1.0);
148 double pull = (histValue - funcVal) / histError;
151 chiSquare += pull * pull;
159 const std::string& fitFunction,
int peak2ndDerivMinSample,
160 float peak2ndDerivMinThreshHG,
float peak2ndDerivMinThreshLG) :
172 m_tmin = -deltaTSample / 2;
177 std::string histName =
"ZDCFitHist" + tag;
178 std::string histNameLGRefit =
"ZDCFitHist" + tag +
"_LGRefit";
199 (*m_msgFunc_p)(
ZDCMsg::Debug,
"ConfigFromJSON produced result: "+ resultString2);
210 std::string histName =
"ZDCFitHist" +
m_tag;
211 std::string histNameLGRefit =
"ZDCFitHist" +
m_tag +
"_LGRefit";
236 std::string delayedHGName = std::string(
m_fitHist->GetName()) +
"delayed";
237 std::string delayedLGName = std::string(
m_fitHistLGRefit->GetName()) +
"delayed";
541 std::ostringstream ostrm;
552 (*m_msgFunc_p)(
ZDCMsg::Error, (
"ZDCPulseAnalyzer::SetFitTimeMax:: invalid FitTimeMax: " + std::to_string(tmax)));
569 float deltaT0MinHG,
float deltaT0MaxHG,
570 float deltaT0MinLG,
float deltaT0MaxLG)
592 float deltaT0MinLG,
float deltaT0MaxLG)
602 float chisqDivAmpCutLG,
float chisqDivAmpScaleLG,
float chisqDivAmpOffsetLG,
float chisqDivAmpPowerLG)
616 const std::vector<double>& parsHG,
617 const std::vector<double>& parsLG)
624 std::string timeResHGName =
"TimeResFuncHG_" +
m_tag;
625 std::string timeResLGName =
"TimeResFuncLG_" +
m_tag;
627 TF1* funcHG_p =
new TF1(timeResHGName.c_str(), TF1String.c_str(), 0,
m_HGOverflowADC);
628 TF1* funcLG_p =
new TF1(timeResLGName.c_str(), TF1String.c_str(), 0,
m_LGOverflowADC);
630 if (parsHG.size() !=
static_cast<unsigned int>(funcHG_p->GetNpar()) ||
631 parsLG.size() !=
static_cast<unsigned int>(funcLG_p->GetNpar())) {
636 funcHG_p->SetParameters(&parsHG[0]);
637 funcLG_p->SetParameters(&parsLG[0]);
651 auto getXmin=[](
const TH1 * pH){
652 return pH->GetXaxis()->GetXmin();
654 auto getXmax=[](
const TH1 * pH){
655 return pH->GetXaxis()->GetXmax();
657 auto xmin= getXmin(correHistHG.get());
658 auto xmax= getXmax(correHistHG.get());
659 if (std::abs(
xmin+0.5) > 1e-3 || std::abs(
xmax - 4095.5) > 1e-3) {
660 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid high gain correction histogram range: xmin, xmax = " +
661 std::to_string(
xmin ) +
", " + std::to_string(
xmax) );
666 xmin= getXmin(correHistLG.get());
667 xmax= getXmax(correHistLG.get());
668 if (std::abs(
xmin+0.5) > 1e-3 ||
669 std::abs(
xmax - 4095.5) > 1e-3) {
670 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid low gain correction histogram range: xmin, xmax = " +
671 std::to_string(
xmin) +
", " + std::to_string(
xmax) );
697 std::vector<float> pulls(
m_Nsample, -100);
700 const TF1* fit_p =
static_cast<const TF1*
>(dataHist_p->GetListOfFunctions()->Last());
702 for (
size_t ibin = 0; ibin <
m_Nsample ; ibin++) {
703 float t = dataHist_p->GetBinCenter(ibin + 1);
704 float fitVal = fit_p->Eval(t);
705 float histVal = dataHist_p->GetBinContent(ibin + 1);
706 float histErr = dataHist_p->GetBinError(ibin + 1);
707 float pull = (histVal - fitVal)/histErr;
717 double amplCorrFactor = 1;
723 amplCorrFactor *= fadcCorr;
735 float invNLCorr = 1.0;
749 amplCorrFactor /= invNLCorr;
752 return amplCorrFactor;
757 float prePulseTMin = 0;
863 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting both delayed and undelayed samples");
887 const std::vector<float>& ADCSamplesHGDelayed,
const std::vector<float>& ADCSamplesLGDelayed)
890 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting only undelayed samples");
917 for (
size_t isample = 0; isample <
m_Nsample; isample++) {
918 float ADCHG = ADCSamplesHG[isample];
919 float ADCLG = ADCSamplesLG[isample];
960 bool doDump = (*m_msgFunc_p)(
ZDCMsg::Verbose,
"Dumping all samples before subtraction: ");
962 std::ostringstream dumpStringHG;
963 dumpStringHG <<
"HG: ";
965 dumpStringHG << std::setw(4) << val <<
" ";
973 std::ostringstream dumpStringLG;
974 dumpStringLG <<
"LG: " << std::setw(4) << std::setfill(
' ');
976 dumpStringLG << std::setw(4) << val <<
" ";
990 for (
size_t isample = 0; isample <
m_NSamplesAna; isample++) {
1013 std::ostringstream dumpString;
1014 dumpString <<
"After FADC correction, sample " << isample <<
", HG ADC = " <<
m_ADCSamplesHGSub[isample]
1165 float deriv2ndThreshHG = 0;
1166 float deriv2ndThreshLG = 0;
1187 (
float chisq,
float amp,
unsigned int fitNDoF,
float& ratio)->
bool
1191 if (amp < 1e-6)
return true;
1192 ratio = chisq /(scale* (std::pow(amp/1000 + offset, power)));
1193 if (chisq/fitNDoF > 2 && ratio > cut)
return false;
1236 (
float chisq,
float amp,
unsigned int fitNDoF,
float& ratio)->
bool
1240 if (amp < 1e-6)
return true;
1241 ratio = chisq /(scale*(std::pow(amp/1000 + offset, power)));
1242 if (chisq/
float(fitNDoF) > 2 && ratio > cut)
return false;
1295 const std::vector<float>& samples,
1296 const std::vector<float>& samplesNoise,
1297 const std::vector<bool>& useSample,
1298 float peak2ndDerivMinThresh,
1299 const std::vector<float>& t0CorrParams,
1301 float minT0Corr,
float maxT0Corr
1314 bool haveFirst =
false;
1315 unsigned int lastUsed = 0;
1317 for (
unsigned int sample = preSampleIdx; sample < nSamples; sample++) {
1318 if (useSample[sample]) {
1374 std::ostringstream baselineMsg;
1375 baselineMsg <<
"Delayed samples baseline correction = " <<
m_baselineCorr << std::endl;
1381 for (
size_t isample = 0; isample < nSamples; isample++) {
1392 std::for_each(
m_samplesSub.begin(),
m_samplesSub.end(), [ = ] (
float & adcUnsub) {return adcUnsub -= m_preSample;} );
1427 if ((deltaLeft2 > 0 || deltaLeft > 0) && (deltaRight2 > 0 || deltaRight > 0)) {
1444 if ((deltaLeft2 > 0 || deltaLeft > 0) && (deltaRight2 > 0 || deltaRight > 0)) {
1495 if (derivPresampleSig < -5) {
1501 if (!useSample[isample])
continue;
1522 float maxPrepulseSig = 0;
1523 unsigned int maxPrepulseSample = 0;
1525 for (
int isample = loopStart; isample <= loopLimit; isample++) {
1526 if (!useSample[isample])
continue;
1540 if (prePulseSig > maxPrepulseSig) {
1541 maxPrepulseSig = prePulseSig;
1542 maxPrepulseSample = isample;
1572 if (!useSample[isample] || !useSample[isample +1])
continue;
1590 std::ostringstream
msg;
1591 msg <<
"ZDCPulseAnalyzer for " <<
m_tag <<
" Found a post pulse at sample " << isample
1592 <<
" deriv = " << deriv <<
", derivErr = " << derivErr
1593 <<
" deriv2nd = " << deriv2nd <<
", deriv2ndErr = " << deriv2nd;
1617 std::ostringstream ostrm;
1634 float correction = 0;
1636 for (
unsigned int ipow = 0; ipow < t0CorrParams.size(); ipow++) {
1637 correction += t0CorrParams[ipow]*std::pow(t0CorrFact,
double(ipow));
1655 double timeResolution = 0;
1667 if (failFixedCut)
m_badT0 =
true;
1687 const std::vector<bool>& useSamples)
1696 for (
unsigned int idx = 0; idx < samplesLG.size(); idx++) {
1699 if (useSamples[idx]) {
1713 TH1* hist_p =
nullptr;
1718 float ampInitial, fitAmpMin, fitAmpMax, t0Initial;
1739 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1761 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1780 int fitStatus = result_ptr;
1786 if (fitStatus != 0 && result_ptr->Edm() > 0.001)
1796 if ((
int) constrFitResult_ptr != 0) {
1806 if ((
int) unconstrFitResult_ptr != 0) {
1813 if ((
int) constrFit2Result_ptr != 0) {
1820 result_ptr = constrFit2Result_ptr;
1824 result_ptr = unconstrFitResult_ptr;
1830 hist_p->GetListOfFunctions()->Clear();
1833 std::string name = func->GetName();
1835 TF1* copyFunc =
static_cast<TF1*
>(func->Clone((name +
"_copy").c_str()));
1836 hist_p->GetListOfFunctions()->Add(copyFunc);
1879 for (
size_t ipar = 0; ipar < numPars; ipar++) {
1898 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1910 float fitAmpMin, fitAmpMax, t0Initial, ampInitial;
1926 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1952 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1957 TFitter* theFitter =
nullptr;
1981 size_t numFitPar = theFitter->GetNumberTotalParameters();
1983 theFitter->GetMinuit()->fISW[4] = -1;
1988 theFitter->GetMinuit()->fISW[4] = -1;
1991 theFitter->GetMinuit()->mnexcm(
"SET NOWarnings",
nullptr,0,ierr);
1993 else theFitter->GetMinuit()->fISW[4] = 0;
1998 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1999 theFitter->ReleaseParameter(0);
2002 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2003 theFitter->FixParameter(0);
2007 double arglist[100];
2010 int status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
2012 double fitAmp = theFitter->GetParameter(1);
2016 double chi2, edm, errdef;
2019 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
2024 if (status || fitAmp < fitAmpMin * 1.01 || edm > 0.01){
2029 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2030 theFitter->FixParameter(0);
2034 if (fitAmp < fitAmpMin * 1.01) {
2040 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
2044 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
2049 theFitter->ReleaseParameter(0);
2057 theFitter->ReleaseParameter(0);
2058 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
2063 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2064 theFitter->FixParameter(0);
2065 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
2073 fitAmp = theFitter->GetParameter(1);
2077 if (fitAmp < fitAmpMin * 1.01) {
2088 if (!
m_quietFits) theFitter->GetMinuit()->fISW[4] = -1;
2090 std::vector<double> funcParams(numFitPar - 1);
2091 std::vector<double> funcParamErrs(numFitPar - 1);
2099 for (
size_t ipar = 1; ipar < numFitPar; ipar++) {
2100 funcParams[ipar - 1] = theFitter->GetParameter(ipar);
2101 funcParamErrs[ipar - 1] = theFitter->GetParError(ipar);
2109 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
2129 theFitter->ExecuteCommand(
"Cal1fcn", arglist, 1);
2162 for (
size_t ipar = 0; ipar < numPars; ipar++) {
2180 for (
int ipar = 0; ipar < func->GetNpar(); ipar++) {
2181 double parLimitLow, parLimitHigh;
2183 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2186 "ZDCPulseAnalyzer name=" + std::string(func->GetName())
2187 +
" ipar=" + std::to_string(ipar)
2188 +
" parLimitLow=" + std::to_string(parLimitLow)
2189 +
" parLimitHigh="+ std::to_string(parLimitHigh)
2193 if (std::abs(parLimitHigh - parLimitLow) > (1e-6)*std::abs(parLimitLow)) {
2194 double value = func->GetParameter(ipar);
2195 if (value >= parLimitHigh) {
2196 value = parLimitHigh * 0.9;
2198 else if (value <= parLimitLow) {
2199 value = parLimitLow + 0.1*std::abs(parLimitLow);
2201 func->SetParameter(ipar, value);
2208 TVirtualFitter::SetDefaultFitter(
"Minuit");
2210 size_t nFitParams = func->GetNpar() + 1;
2211 std::unique_ptr<TFitter> fitter = std::make_unique<TFitter>(nFitParams);
2213 fitter->GetMinuit()->fISW[4] = -1;
2214 fitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
2216 for (
size_t ipar = 0; ipar < nFitParams - 1; ipar++) {
2217 double parLimitLow, parLimitHigh;
2219 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
2220 if (std::abs(parLimitHigh - parLimitLow) < (1e-6)*std::abs(parLimitLow)) {
2221 double value = func->GetParameter(ipar);
2222 double lowLim = std::min(value * 0.99, value * 1.01);
2223 double highLim = std::max(value * 0.99, value * 1.01);
2225 fitter->SetParameter(ipar + 1, func->GetParName(ipar), func->GetParameter(ipar), 0.01, lowLim, highLim);
2226 fitter->FixParameter(ipar + 1);
2229 double value = func->GetParameter(ipar);
2230 if (value >= parLimitHigh) value = parLimitHigh * 0.99;
2231 else if (value <= parLimitLow) value = parLimitLow * 1.01;
2233 double step = std::min((parLimitHigh - parLimitLow)/100., (value - parLimitLow)/100.);
2235 fitter->SetParameter(ipar + 1, func->GetParName(ipar), value, step, parLimitLow, parLimitHigh);
2246 double parLimitLow, parLimitHigh;
2249 func_p->GetParLimits(1, parLimitLow, parLimitHigh);
2251 fitter->SetParameter(2, func_p->GetParName(1), func_p->GetParameter(1), 0.01, parLimitLow, parLimitHigh);
2256 func_p->GetParLimits(parIndex, parLimitLow, parLimitHigh);
2257 fitter->SetParameter(parIndex + 1, func_p->GetParName(parIndex), func_p->GetParameter(parIndex), 0.01, parLimitLow, parLimitHigh);
2268 (*m_msgFunc_p)(
ZDCMsg::Info, (
"using delayed samples with delta T = " + std::to_string(
m_delayedDeltaT) +
", and pedestalDiff == " +
2272 std::ostringstream message1;
2273 message1 <<
"samplesSub ";
2274 for (
size_t sample = 0; sample <
m_samplesSub.size(); sample++) {
2275 message1 <<
", [" << sample <<
"] = " <<
m_samplesSub[sample];
2279 std::ostringstream message3;
2280 message3 <<
"samplesDeriv2nd ";
2291 std::string message =
"Dump of TF1: " + std::string(func->GetName());
2292 bool continueDump = (*m_msgFunc_p)(
ZDCMsg::Verbose, std::move(message));
2293 if (!continueDump)
return;
2295 unsigned int npar = func->GetNpar();
2296 for (
unsigned int ipar = 0; ipar < npar; ipar++) {
2297 std::ostringstream msgstr;
2299 double parMin = 0, parMax = 0;
2300 func->GetParLimits(ipar, parMin, parMax);
2302 msgstr <<
"Parameter " << ipar <<
", value = " << func->GetParameter(ipar) <<
", error = "
2303 << func->GetParError(ipar) <<
", min = " << parMin <<
", max = " << parMax;
2310 std::ostringstream ostrStream;
2311 (*m_msgFunc_p)(
ZDCMsg::Info, (
"\n ZDCPulserAnalyzer:: ======================================================================="));
2312 (*m_msgFunc_p)(
ZDCMsg::Info, (
"ZDCPulserAnalyzer:: settings for instance: " +
m_tag));
2314 ostrStream <<
"Nsample = " <<
m_Nsample <<
" at frequency " <<
m_freqMHz <<
" MHz, preSample index = "
2317 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2320 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2323 ostrStream <<
"Using per-sample noise sigmas for high gain, values = ";
2325 ostrStream <<
"end";
2326 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2330 ostrStream <<
"Using per-sample noise sigmas for low gain, values = ";
2332 ostrStream <<
"end";
2333 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2339 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2342 ostrStream <<
"using delayed samples with delta T = " <<
m_delayedDeltaT <<
", and default pedestalDiff == "
2344 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2352 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2356 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2361 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2365 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2369 ostrStream <<
"Pre-exclusion enabled for up to " <<
m_maxSamplesPreExcl <<
", samples with ADC threshold HG = "
2371 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2374 ostrStream <<
"Post-exclusion enabled for up to " <<
m_maxSamplesPostExcl <<
", samples with ADC threshold HG = "
2376 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2382 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2387 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2390 ostrStream <<
"Minimum significance cuts applied: HG min. sig. = " <<
m_sigMinHG <<
", LG min. sig. " <<
m_sigMinLG;
2391 (*m_msgFunc_p)(
ZDCMsg::Info, ostrStream.str()); ostrStream.str(
""); ostrStream.clear();
2398 unsigned int statusMask = 0;
2435 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
2445 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(2 *
m_Nsample));
2448 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2449 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2450 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2453 for (
int iDelayPt = 0; iDelayPt < delayedHist_p->GetNbinsX(); iDelayPt++) {
2454 theGraph->SetPoint(npts, delayedHist_p->GetBinCenter(iDelayPt + 1), delayedHist_p->GetBinContent(iDelayPt + 1) -
m_delayedBaselineShift);
2455 theGraph->SetPointError(npts++, 0, delayedHist_p->GetBinError(iDelayPt + 1));
2458 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2460 theGraph->GetListOfFunctions()->Add(
new TF1(*func_p));
2461 hist_p->GetListOfFunctions()->SetOwner (
false);
2464 theGraph->SetName(( std::string(hist_p->GetName()) +
"combinaed").c_str());
2466 theGraph->SetMarkerStyle(20);
2467 theGraph->SetMarkerColor(1);
2480 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(
m_Nsample));
2483 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2484 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2485 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2488 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2489 theGraph->GetListOfFunctions()->Add(func_p);
2490 theGraph->SetName(( std::string(hist_p->GetName()) +
"not_combinaed").c_str());
2492 theGraph->SetMarkerStyle(20);
2493 theGraph->SetMarkerColor(1);
2501 unsigned int nSamples = inputData.size();
2505 unsigned int vecSize = 2*(step - 1) + nSamples - step - 1;
2506 std::vector<float> results(vecSize, 0);
2510 unsigned int fillIdx = step - 1;
2512 for (
unsigned int sample = 0; sample < nSamples - step; sample++) {
2513 int deriv = inputData[sample + step] - inputData[sample];
2514 results.at(fillIdx++) = deriv;
2522 unsigned int nSamples = inputData.size();
2527 unsigned int vecSize = 2*step + nSamples - step - 1;
2528 std::vector<float> results(vecSize, 0);
2530 unsigned int fillIndex = step;
2531 for (
unsigned int sample = step; sample < nSamples - step; sample++) {
2532 auto deriv2nd = inputData[sample + step] + inputData[sample - step] - 2*inputData[sample];
2533 results.at(fillIndex++) = deriv2nd;
2539std::pair<std::vector<float>, std::vector<float>>
2542 unsigned int nSamples = inputData.size();
2547 unsigned int vecSize = 2*step + nSamples - step - 1;
2548 std::vector<float> results(vecSize, 0);
2549 std::vector<float> resultsErr(vecSize, 0);
2551 unsigned int fillIndex = step;
2552 for (
unsigned int sample = step; sample < nSamples - step; sample++) {
2553 float deriv2nd = inputData[sample + step] + inputData[sample - step] - 2*inputData[sample];
2554 float deriv2ndErr = std::sqrt(
Sqr(inputNoise[sample + step]) +
Sqr(inputNoise[sample - step]) + 2*
Sqr(inputNoise[sample]));
2556 results[fillIndex] = deriv2nd;
2557 resultsErr[fillIndex++] = deriv2ndErr;
2560 return {results, resultsErr};
2578 const unsigned int nsamples = samples.size();
2586 float minScore = 1.0e9;
2587 unsigned int minIndex = 0;
2589 for (
unsigned int idx = 2; idx < nsamples - 1; idx++) {
2590 float deriv = derivVec[idx];
2591 float prevDeriv = derivVec[idx - 1];
2593 float derivDiff = deriv - prevDeriv;
2595 float deriv2nd = deriv2ndVec[idx];
2596 if (idx > nsamples - 2) deriv2nd = deriv2ndVec[idx - 1];
2601 float score = (deriv*deriv + 2*derivDiff*derivDiff +
2602 0.5*deriv2nd*deriv2nd);
2604 if (score < minScore) {
2615 if (minIndex<2 or (minIndex+1) >=nsamples){
2616 throw std::out_of_range(
"minIndex out of range in ZDCPulseAnalyzer::obtainDelayedBaselineCorr");
2618 float sample0 = samples[minIndex - 2];
2619 float sample1 = samples[minIndex - 1];
2620 float sample2 = samples[minIndex];
2621 float sample3 = samples[minIndex + 1];
2625 float baselineCorr = (0.5 * (sample1 - sample0 + sample3 - sample2) -
2626 0.25 * (sample3 - sample1 + sample2 - sample0));
2628 if (minIndex % 2 != 0) baselineCorr =-baselineCorr;
2630 return baselineCorr;
2636 std::string resultString =
"success";
2639 auto iter = config.find(key);
2640 if (iter != config.end()) {
2644 auto jsonType = iter.value().type();
2645 auto paramType = std::get<0>(descr);
2646 if (jsonType != paramType) {
2648 resultString =
"Bad type for parameter " + key +
", type in JSON = " + std::to_string((
unsigned int) jsonType) ;
2652 size_t paramSize = std::get<1>(descr);
2653 size_t jsonSize = iter.value().size();
2654 if (jsonSize != paramSize) {
2656 resultString =
"Bad length for parameter " + key +
", length in JSON = " + std::to_string(jsonSize) ;
2661 bool required = std::get<2>(descr);
2664 resultString =
"Missing required parameter " + key;
2674 for (
auto [key, value] : config.items()) {
2683 resultString =
"Unknown parameter, key = " + key;
2689 return {result, resultString};
2695 std::string resultString =
"success";
2697 for (
auto [key, value] : config.items()) {
2701 if (key ==
"Nsample")
m_Nsample = value;
2702 else if (key ==
"tag")
m_tag = value;
2703 else if (key ==
"LGMode")
m_LGMode = value;
2704 else if (key ==
"Nsample")
m_Nsample = value;
2707 else if (key ==
"FADCFreqMHz")
m_freqMHz = value;
2708 else if (key ==
"nominalPedestal")
m_pedestal = value;
2722 else if (key ==
"fixTau1")
m_fixTau1 = value;
2723 else if (key ==
"fixTau2")
m_fixTau2 = value;
2724 else if (key ==
"T0CutsHG") {
2728 else if (key ==
"T0CutsLG") {
2745 else if (key ==
"perSampleNoiseSigmaHG") {
2749 else if (key ==
"perSampleNoiseSigmaLG") {
2758 else if (key ==
"fitTimeMax") {
2764 else if (key ==
"fitAmpMinMaxHG") {
2768 else if (key ==
"fitAmpMinMaxLG") {
2772 else if (key ==
"quietFits") {
2775 else if (key ==
"enablePreExclusion") {
2781 else if (key ==
"enablePostExclusion") {
2787 else if (key ==
"enableUnderflowExclusionHG") {
2792 else if (key ==
"enableUnderflowExclusionLG") {
2797 else if (key ==
"ampMinSignifHGLG") {
2802 else if (key ==
"enableFADCCorrections") {
2803 auto fileNameJson = value[
"filename"];
2804 auto doPerSampleCorrJson = value[
"doPerSampleCorr"];
2806 if (fileNameJson.is_null() || doPerSampleCorrJson.is_null()) {
2808 resultString =
"failure processing enableFADCCorrections object";
2816 else if(key ==
"useDelayed"){
2823 else if (key ==
"enableTimingCorrection") {
2828 else if(key ==
"timeCorrCoeffHG"){
2829 for(
int i = 0;
auto coeff:value){
2834 else if(key ==
"timeCorrCoeffLG"){
2835 for(
int i = 0;
auto coeff:value){
2840 else if(key ==
"enableNLCorrection"){
2849 else if(key ==
"HGNLCorrCoeffs"){
2850 std::string HGParamsStr =
"HG coefficients = ";
2851 for (
auto coeff : value) {
2857 else if(key ==
"LGNLCorrCoeffs"){
2858 std::string LGParamsStr =
"LG coefficients = ";
2859 for(
auto coeff:value){
2867 resultString =
"unprocessed parameter";
2872 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 GetShapeParameter(size_t index) const =0
virtual float GetTau2() const =0
virtual float GetAmplitude() const =0
virtual unsigned int GetNumShapeParameters() 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_chisqDivAmpScaleHG
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
size_t m_peak2ndDerivMinSample
std::vector< float > m_setPerSampleNoiseHG
bool ScanAndSubtractSamples()
void dumpConfiguration() const
std::vector< float > m_nonLinCorrParamsLG
bool excludeEarlyLG() const
float m_peak2ndDerivMinThreshHG
bool m_havePerSampleNoiseLG
std::vector< float > m_samplesNoiseLGRefit
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)
static std::vector< float > calculate2ndDerivative(const std::vector< float > &inputData, unsigned int step)
std::unique_ptr< ZDCFitWrapper > m_defaultFitWrapper
std::string m_fitFunction
unsigned int m_preSampleIdx
std::vector< float > m_LGT0CorrParams
std::vector< float > m_samplesLGRefit
std::vector< float > m_samplesNoise
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
unsigned int m_preExclLGADCThresh
unsigned int m_prePulseDelta
void enablePostPulseCheck(unsigned int postPulseSampleDelta, float postPulseDerivMinSig, float postPulseAbsDer2ndMinSig, float minMainDer2ndRatio)
unsigned int m_postExclLGADCThresh
std::vector< float > m_shapeParameters
unsigned int m_NSamplesAna
bool DoAnalysis(bool repass)
void setMinimumSignificance(float sigMinHG, float sigMinLG)
static std::unique_ptr< TFitter > MakeCombinedFitter(TF1 *func)
std::unique_ptr< TH1 > m_delayedHistLGRefit
float m_delayedBaselineShift
unsigned int m_timingCorrMode
float m_chisqDivAmpScaleLG
std::vector< float > m_sampleNoiseLG
unsigned int m_maxSampleEvt
int m_firstHGOverFlowSample
unsigned int m_maxSamplesPreExcl
std::vector< float > m_setPerSampleNoiseLG
std::vector< float > m_ADCSamplesLG
unsigned int m_underFlowExclSamplesPreLG
float m_postPulseMainMinDer2ndRatio
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
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)
void prepareLGRefit(const std::vector< float > &samplesLG, const std::vector< float > &samplesNoise, const std::vector< bool > &useSamples)
double getAmplitudeCorrection(bool highGain)
float m_peak2ndDerivMinRepassHG
unsigned int m_underFlowExclSamplesPreHG
float m_peak2ndDerivMinThreshLG
float m_postPulseAbsDer2ndMinSig
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
std::vector< bool > m_useSampleLG
void reset(bool reanalyze=false)
unsigned int m_underFlowExclSamplesPostLG
unsigned int m_postPulseDelta
std::vector< float > m_ADCSamplesHG
void checkTF1Limits(TF1 *func)
bool AnalyzeData(size_t nSamples, size_t preSample, const std::vector< float > &samples, const std::vector< float > &samplesNoise, const std::vector< bool > &useSamples, float peak2ndDerivMinThresh, const std::vector< float > &toCorrParams, ChisqCutLambdatype chisqCutLambda, float minT0Corr, float maxT0Corr)
void SetTimeCuts(float deltaT0MinHG, float deltaT0MaxHG, float deltaT0MinLG, float deltaT0MaxLG)
bool m_underflowExclusion
std::vector< float > m_sampleNoiseHG
std::vector< float > GetFitPulls(bool forceLG=false) const
bool armSumInclude() const
void SetCutValues(float chisqDivAmpCutHG, float chisqDivAmpCutLG, float deltaT0MinHG, float deltaT0MaxHG, float deltaT0MinLG, float deltaT0MaxLG)
std::vector< float > m_HGT0CorrParams
void SetChisqCuts(float chisqDivAmpCutHG, float chisqDivAmpScaleHG, float chisqDivAmpOffsetHG, float chisqDivAmpPowerHG, float chisqDivAmpCutLG, float chisqDivAmpScaleLG, float chisqDivAmpOffsetLG, float chisqDivAmpPowerLG)
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
std::vector< float > m_ADCSSampNoiseLG
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_havePerSampleNoiseHG
bool m_enableUnderflowExclLG
unsigned int m_postExclHGADCThresh
bool m_haveFADCCorrections
void SetFitMinMaxAmp(float minAmpHG, float minAmpLG, float maxAmpHG, float maxAmpLG)
static std::vector< float > calculateDerivative(const std::vector< float > &inputData, unsigned int step)
float m_chisqDivAmpOffsetHG
std::unique_ptr< const TH1 > m_FADCCorrHG
std::vector< float > m_samplesDeriv2ndErr
std::unique_ptr< ZDCPreExpFitWrapper > m_preExpFitWrapper
float m_postPulseDerivMinSig
ZDCMsg::MessageFunctionPtr m_msgFunc_p
void FillHistogram(bool refitLG)
std::vector< float >::const_iterator SampleCIter
std::vector< float > m_ADCSSampNoiseHG
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
std::function< bool(float, float, float, float &)> ChisqCutLambdatype
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
Tell the compiler to optimize assuming that FP may trap.
#define CXXUTILS_TRAPPING_FP