7 #include "TFitResult.h"
8 #include "TFitResultPtr.h"
9 #include "TVirtualFitter.h"
46 float delayBaselineAdjust =
par[0];
50 for (
int isample = 0; isample <
nSamples; isample++) {
60 double pull = (histValue - funcVal) / histError;
68 for (
int isample = 0; isample <
nSamples; isample++) {
77 double pull = (histValue - funcVal) / histError;
88 float gainHG,
const std::string& fitFunction,
int peak2ndDerivMinSample,
89 float peak2ndDerivMinThreshHG,
float peak2ndDerivMinThreshLG) :
90 m_msgFunc_p(std::move(msgFunc_p)),
91 m_tag(
tag), m_Nsample(Nsample),
92 m_preSampleIdx(preSampleIdx),
93 m_deltaTSample(deltaTSample),
94 m_pedestal(pedestal), m_gainHG(gainHG), m_fitFunction(fitFunction),
95 m_peak2ndDerivMinSample(peak2ndDerivMinSample),
96 m_peak2ndDerivMinThreshLG(peak2ndDerivMinThreshLG),
97 m_peak2ndDerivMinThreshHG(peak2ndDerivMinThreshHG),
98 m_ADCSamplesHGSub(Nsample, 0), m_ADCSamplesLGSub(Nsample, 0),
99 m_ADCSSampSigHG(Nsample, 0), m_ADCSSampSigLG(Nsample, 0),
100 m_samplesSub(Nsample, 0)
104 m_tmin = -deltaTSample / 2;
108 std::string histNameLGRefit =
"ZDCFitHist" +
tag +
"_LGRefit";
131 std::string delayedHGName = std::string(
m_fitHist->GetName()) +
"delayed";
132 std::string delayedLGName = std::string(
m_fitHistLGRefit->GetName()) +
"delayed";
380 std::ostringstream ostrm;
410 float deltaT0MinHG,
float deltaT0MaxHG,
411 float deltaT0MinLG,
float deltaT0MaxLG)
424 const std::vector<double>& parsHG,
425 const std::vector<double>& parsLG)
432 std::string timeResHGName =
"TimeResFuncHG_" +
m_tag;
433 std::string timeResLGName =
"TimeResFuncLG_" +
m_tag;
435 TF1* funcHG_p =
new TF1(timeResHGName.c_str(), TF1String.c_str(), 0,
m_HGOverflowADC);
436 TF1* funcLG_p =
new TF1(timeResLGName.c_str(), TF1String.c_str(), 0,
m_LGOverflowADC);
438 if (parsHG.size() !=
static_cast<unsigned int>(funcHG_p->GetNpar()) ||
439 parsLG.size() !=
static_cast<unsigned int>(funcLG_p->GetNpar())) {
444 funcHG_p->SetParameters(&parsHG[0]);
445 funcLG_p->SetParameters(&parsLG[0]);
459 auto getXmin=[](
const TH1 * pH){
460 return pH->GetXaxis()->GetXmin();
462 auto getXmax=[](
const TH1 * pH){
463 return pH->GetXaxis()->GetXmax();
465 auto xmin= getXmin(correHistHG.get());
466 auto xmax= getXmax(correHistHG.get());
467 if (std::abs(
xmin+0.5) > 1
e-3 || std::abs(
xmax - 4095.5) > 1
e-3) {
468 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid high gain correction histogram range: xmin, xmax = " +
474 xmin= getXmin(correHistLG.get());
475 xmax= getXmax(correHistLG.get());
476 if (std::abs(
xmin+0.5) > 1
e-3 ||
477 std::abs(
xmax - 4095.5) > 1
e-3) {
478 (*m_msgFunc_p)(
ZDCMsg::Error,
"ZDCPulseAnalyzer::enableFADCCorrections:: invalid low gain correction histogram range: xmin, xmax = " +
505 std::vector<float> pulls(
m_Nsample, -100);
508 const TF1* fit_p =
static_cast<const TF1*
>(dataHist_p->GetListOfFunctions()->Last());
510 for (
size_t ibin = 0; ibin <
m_Nsample ; ibin++) {
511 float t = dataHist_p->GetBinCenter(ibin + 1);
512 float fitVal = fit_p->Eval(
t);
513 float histVal = dataHist_p->GetBinContent(ibin + 1);
514 float histErr = dataHist_p->GetBinError(ibin + 1);
515 float pull = (histVal - fitVal)/histErr;
525 double amplCorrFactor = 1;
531 amplCorrFactor *= fadcCorr;
543 float invNLCorr = 1.0;
557 amplCorrFactor /= invNLCorr;
560 return amplCorrFactor;
565 float prePulseTMin = 0;
659 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting both delayed and undelayed samples");
682 const std::vector<float>& ADCSamplesHGDelayed,
const std::vector<float>& ADCSamplesLGDelayed)
685 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting only undelayed samples");
707 for (
size_t isample = 0; isample <
m_Nsample; isample++) {
708 float ADCHG = ADCSamplesHG[isample];
709 float ADCLG = ADCSamplesLG[isample];
750 bool doDump = (*m_msgFunc_p)(
ZDCMsg::Verbose,
"Dumping all samples before subtraction: ");
752 std::ostringstream dumpStringHG;
753 dumpStringHG <<
"HG: ";
755 dumpStringHG << std::setw(4) <<
val <<
" ";
763 std::ostringstream dumpStringLG;
764 dumpStringLG <<
"LG: " << std::setw(4) << std::setfill(
' ');
766 dumpStringLG << std::setw(4) <<
val <<
" ";
788 for (
size_t isample = 0; isample <
m_NSamplesAna; isample++) {
888 std::ostringstream dumpStringUseHG;
889 dumpStringUseHG <<
"HG: ";
891 dumpStringUseHG <<
val <<
" ";
895 std::ostringstream dumpStringUseLG;
896 dumpStringUseLG <<
"LG: ";
898 dumpStringUseLG <<
val <<
" ";
910 if (m_lastHGOverFlowSample < 2 && m_lastHGOverFlowSample > -1) {
925 (*m_msgFunc_p)(
ZDCMsg::Verbose,
"ZDCPulseAnalyzer:: " +
m_tag +
": ScanAndSubtractSamples done");
932 float deriv2ndThreshHG = 0;
933 float deriv2ndThreshLG = 0;
1037 const std::vector<float>& samples,
1038 const std::vector<bool>& useSample,
1039 float peak2ndDerivMinThresh,
1041 const std::vector<float>& t0CorrParams,
1042 float maxChisqDivAmp,
1043 float minT0Corr,
float maxT0Corr
1056 bool haveFirst =
false;
1057 unsigned int lastUsed = 0;
1095 std::ostringstream pedMessage;
1117 std::ostringstream baselineMsg;
1118 baselineMsg <<
"Delayed samples baseline correction = " <<
m_baselineCorr << std::endl;
1124 for (
size_t isample = 0; isample <
nSamples; isample++) {
1135 std::for_each(
m_samplesSub.begin(),
m_samplesSub.end(), [ = ] (
float & adcUnsub) {return adcUnsub -= m_preSample;} );
1210 if (derivPresampleSig < -5) {
1216 if (!useSample[isample])
continue;
1218 float sampleSig = -
m_samplesSub[isample]/(std::sqrt(2.0)*noiseSig);
1221 if ((sampleSig > 5 && sigRatio > 0.02) || sigRatio > 0.5) {
1232 float maxPrepulseSig = 0;
1233 unsigned int maxPrepulseSample = 0;
1235 for (
int isample = loopStart; isample <= loopLimit; isample++) {
1236 if (!useSample[isample])
continue;
1252 if (prePulseSig > maxPrepulseSig) {
1253 maxPrepulseSig = prePulseSig;
1254 maxPrepulseSample = isample;
1290 if (!useSample[isample])
continue;
1321 float derivSig = deriv/(std::sqrt(2)*noiseSig);
1330 if (std::abs(deriv2ndTest) > 0.15) {
1343 if (deriv2ndSig > 5 && deriv2ndTest < -0.1) {
1354 std::ostringstream ostrm;
1355 ostrm <<
"Post pulse found, m_maxSampleEvt = " <<
m_maxSampleEvt;
1373 std::ostringstream ostrm;
1374 ostrm <<
"Pulse fit successful with chisquare = " <<
m_fitChisq;
1392 for (
unsigned int ipow = 0; ipow < t0CorrParams.size(); ipow++) {
1401 bool failFixedCut = m_fitTimeCorr < minT0Corr || m_fitTimeCorr > maxT0Corr;
1411 double timeResolution = 0;
1423 if (failFixedCut)
m_badT0 =
true;
1443 const std::vector<bool>& useSamples)
1452 for (
unsigned int idx = 0;
idx < samplesLG.size();
idx++) {
1455 if (useSamples[
idx]) {
1469 TH1* hist_p =
nullptr;
1474 float ampInitial, fitAmpMin, fitAmpMax, t0Initial;
1495 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1517 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1535 int fitStatus = result_ptr;
1541 if (fitStatus != 0 && result_ptr->Edm() > 0.001)
1551 if ((
int) constrFitResult_ptr != 0) {
1561 if ((
int) unconstrFitResult_ptr != 0) {
1568 if ((
int) constrFit2Result_ptr != 0) {
1575 result_ptr = constrFit2Result_ptr;
1579 result_ptr = unconstrFitResult_ptr;
1585 hist_p->GetListOfFunctions()->Clear();
1588 std::string
name = func->GetName();
1590 TF1* copyFunc =
static_cast<TF1*
>(func->Clone((
name +
"_copy").c_str()));
1591 hist_p->GetListOfFunctions()->Add(copyFunc);
1636 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1648 float fitAmpMin, fitAmpMax, t0Initial, ampInitial;
1664 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1690 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1695 TFitter* theFitter =
nullptr;
1719 size_t numFitPar = theFitter->GetNumberTotalParameters();
1721 theFitter->GetMinuit()->fISW[4] = -1;
1726 theFitter->GetMinuit()->fISW[4] = -1;
1729 theFitter->GetMinuit()->mnexcm(
"SET NOWarnings",
nullptr,0,ierr);
1731 else theFitter->GetMinuit()->fISW[4] = 0;
1736 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1737 theFitter->ReleaseParameter(0);
1740 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1741 theFitter->FixParameter(0);
1745 double arglist[100];
1748 int status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1750 double fitAmp = theFitter->GetParameter(1);
1754 double chi2, edm, errdef;
1757 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1762 if (
status || fitAmp < fitAmpMin * 1.01 || edm > 0.01){
1767 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1768 theFitter->FixParameter(0);
1772 if (fitAmp < fitAmpMin * 1.01) {
1778 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1782 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1787 theFitter->ReleaseParameter(0);
1795 theFitter->ReleaseParameter(0);
1796 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1801 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1802 theFitter->FixParameter(0);
1803 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1811 fitAmp = theFitter->GetParameter(1);
1815 if (fitAmp < fitAmpMin * 1.01) {
1819 if (!
s_quietFits) theFitter->GetMinuit()->fISW[4] = -1;
1821 std::vector<double> funcParams(numFitPar - 1);
1822 std::vector<double> funcParamErrs(numFitPar - 1);
1830 for (
size_t ipar = 1; ipar < numFitPar; ipar++) {
1831 funcParams[ipar - 1] = theFitter->GetParameter(ipar);
1832 funcParamErrs[ipar - 1] = theFitter->GetParError(ipar);
1840 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1860 theFitter->ExecuteCommand(
"Cal1fcn", arglist, 1);
1903 TVirtualFitter::SetDefaultFitter(
"Minuit");
1905 size_t nFitParams = func->GetNpar() + 1;
1906 std::unique_ptr<TFitter>
fitter = std::make_unique<TFitter>(nFitParams);
1908 fitter->GetMinuit()->fISW[4] = -1;
1909 fitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1911 for (
size_t ipar = 0; ipar < nFitParams - 1; ipar++) {
1912 double parLimitLow, parLimitHigh;
1914 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
1915 if (std::abs(parLimitHigh / parLimitLow - 1) < 1
e-6) {
1916 double value = func->GetParameter(ipar);
1920 fitter->SetParameter(ipar + 1, func->GetParName(ipar), func->GetParameter(ipar), 0.01, lowLim, highLim);
1921 fitter->FixParameter(ipar + 1);
1924 double value = func->GetParameter(ipar);
1925 if (
value >= parLimitHigh)
value = parLimitHigh * 0.99;
1926 else if (
value <= parLimitLow)
value = parLimitLow * 1.01;
1928 double step =
std::min((parLimitHigh - parLimitLow)/100., (
value - parLimitLow)/100.);
1930 fitter->SetParameter(ipar + 1, func->GetParName(ipar),
value,
step, parLimitLow, parLimitHigh);
1941 double parLimitLow, parLimitHigh;
1944 func_p->GetParLimits(1, parLimitLow, parLimitHigh);
1946 fitter->SetParameter(2, func_p->GetParName(1), func_p->GetParameter(1), 0.01, parLimitLow, parLimitHigh);
1951 func_p->GetParLimits(parIndex, parLimitLow, parLimitHigh);
1952 fitter->SetParameter(parIndex + 1, func_p->GetParName(parIndex), func_p->GetParameter(parIndex), 0.01, parLimitLow, parLimitHigh);
1967 std::ostringstream message1;
1968 message1 <<
"samplesSub ";
1974 std::ostringstream message3;
1975 message3 <<
"samplesDeriv2nd ";
1986 std::string
message =
"Dump of TF1: " + std::string(func->GetName());
1988 if (!continueDump)
return;
1990 unsigned int npar = func->GetNpar();
1991 for (
unsigned int ipar = 0; ipar < npar; ipar++) {
1992 std::ostringstream msgstr;
1994 double parMin = 0, parMax = 0;
1995 func->GetParLimits(ipar, parMin, parMax);
1997 msgstr <<
"Parameter " << ipar <<
", value = " << func->GetParameter(ipar) <<
", error = "
1998 << func->GetParError(ipar) <<
", min = " << parMin <<
", max = " << parMax;
2022 unsigned int statusMask = 0;
2056 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
2066 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(2 *
m_Nsample));
2069 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2070 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2071 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2074 for (
int iDelayPt = 0; iDelayPt < delayedHist_p->GetNbinsX(); iDelayPt++) {
2075 theGraph->SetPoint(npts, delayedHist_p->GetBinCenter(iDelayPt + 1), delayedHist_p->GetBinContent(iDelayPt + 1) -
m_delayedBaselineShift);
2076 theGraph->SetPointError(npts++, 0, delayedHist_p->GetBinError(iDelayPt + 1));
2079 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2081 theGraph->GetListOfFunctions()->Add(
new TF1(*func_p));
2082 hist_p->GetListOfFunctions()->SetOwner (
false);
2085 theGraph->SetName(( std::string(hist_p->GetName()) +
"combinaed").c_str());
2087 theGraph->SetMarkerStyle(20);
2088 theGraph->SetMarkerColor(1);
2101 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(
m_Nsample));
2104 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2105 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2106 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2109 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2110 theGraph->GetListOfFunctions()->Add(func_p);
2111 theGraph->SetName(( std::string(hist_p->GetName()) +
"not_combinaed").c_str());
2113 theGraph->SetMarkerStyle(20);
2114 theGraph->SetMarkerColor(1);
2122 unsigned int nSamples = inputData.size();
2127 std::vector<float>
results(vecSize, 0);
2131 unsigned int fillIdx =
step - 1;
2135 results.at(fillIdx++) = deriv;
2143 unsigned int nSamples = inputData.size();
2149 std::vector<float>
results(vecSize, 0);
2151 unsigned int fillIndex =
step;
2154 results.at(fillIndex++) = deriv2nd;
2175 const unsigned int nsamples = samples.size();
2183 float minScore = 1.0e9;
2184 unsigned int minIndex = 0;
2187 float deriv = derivVec[
idx];
2188 float prevDeriv = derivVec[
idx - 1];
2190 float derivDiff = deriv - prevDeriv;
2192 float deriv2nd = deriv2ndVec[
idx];
2198 float score = (deriv*deriv + 2*derivDiff*derivDiff +
2199 0.5*deriv2nd*deriv2nd);
2201 if (
score < minScore) {
2212 if (minIndex<2 or (minIndex+1) >=
nsamples){
2213 throw std::out_of_range(
"minIndex out of range in ZDCPulseAnalyzer::obtainDelayedBaselineCorr");
2215 float sample0 = samples[minIndex - 2];
2216 float sample1 = samples[minIndex - 1];
2217 float sample2 = samples[minIndex];
2218 float sample3 = samples[minIndex + 1];
2222 float baselineCorr = (0.5 * (sample1 - sample0 + sample3 - sample2) -
2223 0.25 * (sample3 - sample1 + sample2 - sample0));
2225 if (minIndex % 2 != 0) baselineCorr =-baselineCorr;
2227 return baselineCorr;