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";
367 std::ostringstream ostrm;
397 float deltaT0MinHG,
float deltaT0MaxHG,
398 float deltaT0MinLG,
float deltaT0MaxLG)
411 const std::vector<double>& parsHG,
412 const std::vector<double>& parsLG)
419 std::string timeResHGName =
"TimeResFuncHG_" +
m_tag;
420 std::string timeResLGName =
"TimeResFuncLG_" +
m_tag;
422 TF1* funcHG_p =
new TF1(timeResHGName.c_str(), TF1String.c_str(), 0,
m_HGOverflowADC);
423 TF1* funcLG_p =
new TF1(timeResLGName.c_str(), TF1String.c_str(), 0,
m_LGOverflowADC);
425 if (parsHG.size() !=
static_cast<unsigned int>(funcHG_p->GetNpar()) ||
426 parsLG.size() !=
static_cast<unsigned int>(funcLG_p->GetNpar())) {
431 funcHG_p->SetParameters(&parsHG[0]);
432 funcLG_p->SetParameters(&parsLG[0]);
459 std::vector<float> pulls(
m_Nsample, -100);
462 const TF1* fit_p =
static_cast<const TF1*
>(dataHist_p->GetListOfFunctions()->Last());
464 for (
size_t ibin = 0; ibin <
m_Nsample ; ibin++) {
465 float t = dataHist_p->GetBinCenter(ibin + 1);
466 float fitVal = fit_p->Eval(
t);
467 float histVal = dataHist_p->GetBinContent(ibin + 1);
468 float histErr = dataHist_p->GetBinError(ibin + 1);
469 float pull = (histVal - fitVal)/histErr;
479 float prePulseTMin = 0;
573 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting both delayed and undelayed samples");
596 const std::vector<float>& ADCSamplesHGDelayed,
const std::vector<float>& ADCSamplesLGDelayed)
599 (*m_msgFunc_p)(
ZDCMsg::Fatal,
"ZDCPulseAnalyzer::LoadAndAnalyzeData:: Wrong LoadAndAnalyzeData called -- expecting only undelayed samples");
621 for (
size_t isample = 0; isample <
m_Nsample; isample++) {
622 float ADCHG = ADCSamplesHG[isample];
623 float ADCLG = ADCSamplesLG[isample];
664 bool doDump = (*m_msgFunc_p)(
ZDCMsg::Verbose,
"Dumping all samples before subtraction: ");
666 std::ostringstream dumpStringHG;
667 dumpStringHG <<
"HG: ";
669 dumpStringHG << std::setw(4) <<
val <<
" ";
677 std::ostringstream dumpStringLG;
678 dumpStringLG <<
"LG: " << std::setw(4) << std::setfill(
' ');
680 dumpStringLG << std::setw(4) <<
val <<
" ";
696 for (
size_t isample = 0; isample <
m_NSamplesAna; isample++) {
778 std::ostringstream dumpStringUseHG;
779 dumpStringUseHG <<
"HG: ";
781 dumpStringUseHG <<
val <<
" ";
785 std::ostringstream dumpStringUseLG;
786 dumpStringUseLG <<
"LG: ";
788 dumpStringUseLG <<
val <<
" ";
800 if (m_lastHGOverFlowSample < 2 && m_lastHGOverFlowSample > -1) {
815 (*m_msgFunc_p)(
ZDCMsg::Verbose,
"ZDCPulseAnalyzer:: " +
m_tag +
": ScanAndSubtractSamples done");
822 float deriv2ndThreshHG = 0;
823 float deriv2ndThreshLG = 0;
860 double invNLCorr = 1.0;
922 float invNLCorr = 1.0;
941 float invNLCorr = 1.0;
955 const std::vector<float>& samples,
956 const std::vector<bool>& useSample,
957 float peak2ndDerivMinThresh,
959 const std::vector<float>& t0CorrParams,
960 float maxChisqDivAmp,
961 float minT0Corr,
float maxT0Corr
974 bool haveFirst =
false;
975 unsigned int lastUsed = 0;
1013 std::ostringstream pedMessage;
1035 std::ostringstream baselineMsg;
1036 baselineMsg <<
"Delayed samples baseline correction = " <<
m_baselineCorr << std::endl;
1042 for (
size_t isample = 0; isample <
nSamples; isample++) {
1053 std::for_each(
m_samplesSub.begin(),
m_samplesSub.end(), [ = ] (
float & adcUnsub) {return adcUnsub -= m_preSample;} );
1130 if (derivPresampleSig < -5) {
1136 if (!useSample[isample])
continue;
1138 float sampleSig = -
m_samplesSub[isample]/(std::sqrt(2.0)*noiseSig);
1141 if ((sampleSig > 5 && sigRatio > 0.02) || sigRatio > 0.5) {
1150 for (
int isample = loopStart; isample <= loopLimit; isample++) {
1151 if (!useSample[isample])
continue;
1193 if (!useSample[isample])
continue;
1224 float derivSig = deriv/(std::sqrt(2)*noiseSig);
1233 if (std::abs(deriv2ndTest) > 0.15) {
1246 if (deriv2ndSig > 5 && deriv2ndTest < -0.1) {
1257 std::ostringstream ostrm;
1258 ostrm <<
"Post pulse found, m_maxSampleEvt = " <<
m_maxSampleEvt;
1276 std::ostringstream ostrm;
1277 ostrm <<
"Pulse fit successful with chisquare = " <<
m_fitChisq;
1295 for (
unsigned int ipow = 0; ipow < t0CorrParams.size(); ipow++) {
1304 bool failFixedCut = m_fitTimeCorr < minT0Corr || m_fitTimeCorr > maxT0Corr;
1314 double timeResolution = 0;
1326 if (failFixedCut)
m_badT0 =
true;
1346 const std::vector<bool>& useSamples)
1355 for (
unsigned int idx = 0;
idx < samplesLG.size();
idx++) {
1358 if (useSamples[
idx]) {
1372 TH1* hist_p =
nullptr;
1377 float ampInitial, fitAmpMin, fitAmpMax, t0Initial;
1398 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1420 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1438 int fitStatus = result_ptr;
1444 if (fitStatus != 0 && result_ptr->Edm() > 0.001)
1454 if ((
int) constrFitResult_ptr != 0) {
1464 if ((
int) unconstrFitResult_ptr != 0) {
1471 if ((
int) constrFit2Result_ptr != 0) {
1478 result_ptr = constrFit2Result_ptr;
1482 result_ptr = unconstrFitResult_ptr;
1488 hist_p->GetListOfFunctions()->Clear();
1491 std::string
name = func->GetName();
1493 TF1* copyFunc =
static_cast<TF1*
>(func->Clone((
name +
"_copy").c_str()));
1494 hist_p->GetListOfFunctions()->Add(copyFunc);
1538 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1550 float fitAmpMin, fitAmpMax, t0Initial;
1565 if (ampInitial < fitAmpMin) ampInitial = fitAmpMin * 1.5;
1591 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1596 TFitter* theFitter =
nullptr;
1620 size_t numFitPar = theFitter->GetNumberTotalParameters();
1622 theFitter->GetMinuit()->fISW[4] = -1;
1627 theFitter->GetMinuit()->fISW[4] = -1;
1630 theFitter->GetMinuit()->mnexcm(
"SET NOWarnings",
nullptr,0,ierr);
1632 else theFitter->GetMinuit()->fISW[4] = 0;
1637 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1638 theFitter->ReleaseParameter(0);
1641 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1642 theFitter->FixParameter(0);
1646 double arglist[100];
1649 int status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1651 double fitAmp = theFitter->GetParameter(1);
1655 double chi2, edm, errdef;
1658 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1663 if (
status || fitAmp < fitAmpMin * 1.01 || edm > 0.01){
1668 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1669 theFitter->FixParameter(0);
1673 if (fitAmp < fitAmpMin * 1.01) {
1679 fitWrapper->
Initialize(ampInitial, t0Initial, fitAmpMin, fitAmpMax);
1683 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1688 theFitter->ReleaseParameter(0);
1696 theFitter->ReleaseParameter(0);
1697 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1702 theFitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1703 theFitter->FixParameter(0);
1704 status = theFitter->ExecuteCommand(
"MIGRAD", arglist, 2);
1712 fitAmp = theFitter->GetParameter(1);
1716 if (fitAmp < fitAmpMin * 1.01) {
1720 if (!
s_quietFits) theFitter->GetMinuit()->fISW[4] = -1;
1722 std::vector<double> funcParams(numFitPar - 1);
1723 std::vector<double> funcParamErrs(numFitPar - 1);
1731 for (
size_t ipar = 1; ipar < numFitPar; ipar++) {
1732 funcParams[ipar - 1] = theFitter->GetParameter(ipar);
1733 funcParamErrs[ipar - 1] = theFitter->GetParError(ipar);
1741 theFitter->GetStats(
chi2, edm, errdef, nvpar, nparx);
1761 theFitter->ExecuteCommand(
"Cal1fcn", arglist, 1);
1803 TVirtualFitter::SetDefaultFitter(
"Minuit");
1805 size_t nFitParams = func->GetNpar() + 1;
1806 std::unique_ptr<TFitter>
fitter = std::make_unique<TFitter>(nFitParams);
1808 fitter->GetMinuit()->fISW[4] = -1;
1809 fitter->SetParameter(0,
"delayBaselineAdjust", 0, 0.01, -100, 100);
1811 for (
size_t ipar = 0; ipar < nFitParams - 1; ipar++) {
1812 double parLimitLow, parLimitHigh;
1814 func->GetParLimits(ipar, parLimitLow, parLimitHigh);
1815 if (std::abs(parLimitHigh / parLimitLow - 1) < 1
e-6) {
1816 double value = func->GetParameter(ipar);
1820 fitter->SetParameter(ipar + 1, func->GetParName(ipar), func->GetParameter(ipar), 0.01, lowLim, highLim);
1821 fitter->FixParameter(ipar + 1);
1824 double value = func->GetParameter(ipar);
1825 if (
value >= parLimitHigh)
value = parLimitHigh * 0.99;
1826 else if (
value <= parLimitLow)
value = parLimitLow * 1.01;
1828 double step =
std::min((parLimitHigh - parLimitLow)/100., (
value - parLimitLow)/100.);
1830 fitter->SetParameter(ipar + 1, func->GetParName(ipar),
value,
step, parLimitLow, parLimitHigh);
1841 double parLimitLow, parLimitHigh;
1844 func_p->GetParLimits(1, parLimitLow, parLimitHigh);
1846 fitter->SetParameter(2, func_p->GetParName(1), func_p->GetParameter(1), 0.01, parLimitLow, parLimitHigh);
1851 func_p->GetParLimits(parIndex, parLimitLow, parLimitHigh);
1852 fitter->SetParameter(parIndex + 1, func_p->GetParName(parIndex), func_p->GetParameter(parIndex), 0.01, parLimitLow, parLimitHigh);
1867 std::ostringstream message1;
1868 message1 <<
"samplesSub ";
1874 std::ostringstream message3;
1875 message3 <<
"samplesDeriv2nd ";
1886 std::string
message =
"Dump of TF1: " + std::string(func->GetName());
1888 if (!continueDump)
return;
1890 unsigned int npar = func->GetNpar();
1891 for (
unsigned int ipar = 0; ipar < npar; ipar++) {
1892 std::ostringstream msgstr;
1894 double parMin = 0, parMax = 0;
1895 func->GetParLimits(ipar, parMin, parMax);
1897 msgstr <<
"Parameter " << ipar <<
", value = " << func->GetParameter(ipar) <<
", error = "
1898 << func->GetParError(ipar) <<
", min = " << parMin <<
", max = " << parMax;
1922 unsigned int statusMask = 0;
1956 TH1* hist_p =
nullptr, *delayedHist_p =
nullptr;
1966 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(2 *
m_Nsample));
1969 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
1970 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
1971 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
1974 for (
int iDelayPt = 0; iDelayPt < delayedHist_p->GetNbinsX(); iDelayPt++) {
1975 theGraph->SetPoint(npts, delayedHist_p->GetBinCenter(iDelayPt + 1), delayedHist_p->GetBinContent(iDelayPt + 1) -
m_delayedBaselineShift);
1976 theGraph->SetPointError(npts++, 0, delayedHist_p->GetBinError(iDelayPt + 1));
1979 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
1981 theGraph->GetListOfFunctions()->Add(
new TF1(*func_p));
1982 hist_p->GetListOfFunctions()->SetOwner (
false);
1985 theGraph->SetName(( std::string(hist_p->GetName()) +
"combinaed").c_str());
1987 theGraph->SetMarkerStyle(20);
1988 theGraph->SetMarkerColor(1);
2001 std::shared_ptr<TGraphErrors> theGraph = std::make_shared<TGraphErrors>(TGraphErrors(
m_Nsample));
2004 for (
int ipt = 0; ipt < hist_p->GetNbinsX(); ipt++) {
2005 theGraph->SetPoint(npts, hist_p->GetBinCenter(ipt + 1), hist_p->GetBinContent(ipt + 1));
2006 theGraph->SetPointError(npts++, 0, hist_p->GetBinError(ipt + 1));
2009 TF1* func_p =
static_cast<TF1*
>(hist_p->GetListOfFunctions()->Last());
2010 theGraph->GetListOfFunctions()->Add(func_p);
2011 theGraph->SetName(( std::string(hist_p->GetName()) +
"not_combinaed").c_str());
2013 theGraph->SetMarkerStyle(20);
2014 theGraph->SetMarkerColor(1);
2022 unsigned int nSamples = inputData.size();
2027 std::vector<float>
results(vecSize, 0);
2031 unsigned int fillIdx =
step - 1;
2035 results.at(fillIdx++) = deriv;
2043 unsigned int nSamples = inputData.size();
2049 std::vector<float>
results(vecSize, 0);
2051 unsigned int fillIndex =
step;
2054 results.at(fillIndex++) = deriv2nd;
2075 const unsigned int nsamples = samples.size();
2083 float minScore = 1.0e9;
2084 unsigned int minIndex = 0;
2087 float deriv = derivVec[
idx];
2088 float prevDeriv = derivVec[
idx - 1];
2090 float derivDiff = deriv - prevDeriv;
2092 float deriv2nd = deriv2ndVec[
idx];
2098 float score = (deriv*deriv + 2*derivDiff*derivDiff +
2099 0.5*deriv2nd*deriv2nd);
2101 if (
score < minScore) {
2112 if (minIndex<2 or (minIndex+1) >=
nsamples){
2113 throw std::out_of_range(
"minIndex out of range in ZDCPulseAnalyzer::obtainDelayedBaselineCorr");
2115 float sample0 = samples[minIndex - 2];
2116 float sample1 = samples[minIndex - 1];
2117 float sample2 = samples[minIndex];
2118 float sample3 = samples[minIndex + 1];
2122 float baselineCorr = (0.5 * (sample1 - sample0 + sample3 - sample2) -
2123 0.25 * (sample3 - sample1 + sample2 - sample0));
2125 if (minIndex % 2 != 0) baselineCorr =-baselineCorr;
2127 return baselineCorr;