From e419e9d7923ebac1e0c6974b15489c2dd1e016dc Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Thu, 27 Feb 2025 22:45:36 -0600 Subject: [PATCH 01/20] first implementation of pulsestartextractor --- icaruscode/PMT/Calibration/CMakeLists.txt | 2 +- icaruscode/Timing/CMakeLists.txt | 3 + .../PMTWaveformTimeCorrectionExtractor.cxx | 14 +- icaruscode/Timing/Tools/CMakeLists.txt | 14 ++ .../Timing/Tools/PulseStartExtractor.cc | 211 ++++++++++++++++++ icaruscode/Timing/Tools/PulseStartExtractor.h | 65 ++++++ 6 files changed, 307 insertions(+), 2 deletions(-) create mode 100644 icaruscode/Timing/Tools/CMakeLists.txt create mode 100644 icaruscode/Timing/Tools/PulseStartExtractor.cc create mode 100644 icaruscode/Timing/Tools/PulseStartExtractor.h diff --git a/icaruscode/PMT/Calibration/CMakeLists.txt b/icaruscode/PMT/Calibration/CMakeLists.txt index c4317a71e..539fb8752 100644 --- a/icaruscode/PMT/Calibration/CMakeLists.txt +++ b/icaruscode/PMT/Calibration/CMakeLists.txt @@ -6,7 +6,7 @@ cet_build_plugin(PMTLaserCalibration art::module icaruscode_PMT_Algorithms icaruscode_PMT_Calibration_CaloTools icaruscode_IcarusObj - lardataobj::RawData + lardataobj::RawData lardataobj::Simulation lardataobj::RecoBase art::Framework_Core diff --git a/icaruscode/Timing/CMakeLists.txt b/icaruscode/Timing/CMakeLists.txt index 85fc1cc05..14de1d53e 100644 --- a/icaruscode/Timing/CMakeLists.txt +++ b/icaruscode/Timing/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(Tools) + cet_enable_asserts() set( MODULE_LIBRARIES @@ -19,6 +21,7 @@ set( LIB_LIBRARIES sbnobj::Common_PMT_Data larevt::CalibrationDBI_IOVData larevt::CalibrationDBI_Providers + icaruscode_Timing_Tools ) set( SERVICE_LIBRARIES icaruscode_Timing diff --git a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx index 72de9db6d..1ed8b124a 100644 --- a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx +++ b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx @@ -10,6 +10,7 @@ #include "icaruscode/Decode/ChannelMapping/IICARUSChannelMap.h" #include "icaruscode/Timing/PMTTimingCorrections.h" #include "icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h" +#include "icaruscode/Timing/Tools/PulseStartExtractor.h" // framework libraries #include "canvas/Persistency/Provenance/ProcessConfiguration.h" @@ -190,8 +191,19 @@ void icarus::timing::PMTWaveformTimeCorrectionExtractor::findWaveformTimeCorrect // This will be the first sample of the falling edge of the special channel signal // Which corresponds to the global trigger time. - int startSampleSignal = static_cast( getStartSample( wave ) ); + PulseStartExtractor extractor(PulseStartExtractor::SIGMOID_FIT, 500.0); + double startSampleSignal = extractor.extractStart(wave); + + PulseStartExtractor extractor1(PulseStartExtractor::CONSTANT_FRACTION, 500.0); + double startSampleSignal1 = extractor1.extractStart(wave); + std::cout << "channel " << waveChannelID << " crate " << crateSignalID << " sample " << startSampleSignal1 << " fit " << startSampleSignal << + "diff " << startSampleSignal - startSampleSignal1 << std::endl; + + //int startSampleSignal = static_cast( getStartSample( wave ) ); + if (startSampleSignal<1) + throw Error("Can't find digitized trigger signal!"); + // allocates room for correction for `channel`; intermediate ones are defaulted auto correctionFor = [&corrections](unsigned int channel) -> PMTWaveformTimeCorrection& diff --git a/icaruscode/Timing/Tools/CMakeLists.txt b/icaruscode/Timing/Tools/CMakeLists.txt new file mode 100644 index 000000000..13d80ae23 --- /dev/null +++ b/icaruscode/Timing/Tools/CMakeLists.txt @@ -0,0 +1,14 @@ +cet_enable_asserts() + +# Gather all source files in this Tools subdirectory. +file(GLOB tool_srcs *.cc) + +# Build a library target from these sources. +# This will create a target whose name is based on the directory (or you can force it with a variable). +art_make_library( + LIBRARIES PUBLIC ROOT::Core ROOT::Gpad ROOT::Geom + SOURCE ${tool_srcs} +) + +install_headers() +install_source() diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.cc b/icaruscode/Timing/Tools/PulseStartExtractor.cc new file mode 100644 index 000000000..0a45b0828 --- /dev/null +++ b/icaruscode/Timing/Tools/PulseStartExtractor.cc @@ -0,0 +1,211 @@ +/* + * Author: M. Vicenzi (mvicenzi@bnl.gov) + * File: PulseStartExtractor.cc + * Description: Source file for the PulseStartExtractor class implementation. It provides + * templated functions to extract the start (rising edge) time of reference + * signals (square pulses) using either constant fraction discrimination or + * a refined sigmoid fit. + */ + +#include "PulseStartExtractor.h" +#include +#include +#include +#include +#include +#include +#include "TF1.h" +#include "TGraph.h" +#include "TFitResultPtr.h" +#include "TRandom.h" + +// Constructor +PulseStartExtractor::PulseStartExtractor(ExtractionMethod method, double threshold) + : fMethod(method), fThreshold(threshold) {} + +// -------------------------------------------------------------------------------- +template +std::size_t PulseStartExtractor::findMinBin(const std::vector &wf, std::size_t start, std::size_t end) +{ + auto minel = std::min_element(wf.begin() + start, wf.begin() + end); + size_t minsample = std::distance(wf.begin() + start, minel); + return minsample; +} + +template +std::size_t PulseStartExtractor::findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) +{ + auto maxel = std::max_element(wf.begin() + start, wf.begin() + end); + size_t maxsample = std::distance(wf.begin() + start, maxel); + return maxsample; +} + +// -------------------------------------------------------------------------------- +template +T PulseStartExtractor::computeMedian(const std::vector &data) +{ + std::vector copy = data; + std::nth_element(copy.begin(), copy.begin() + copy.size() / 2, copy.end()); + return copy[copy.size() / 2]; +} + +// -------------------------------------------------------------------------------- +template +double PulseStartExtractor::extractStart(const std::vector &wf) +{ + + // Try constant-fraction discrimination (default method) + // if fit method is selected, the result is used as initial guess for the fit + std::size_t cf_sample = extractStartSampleCF(wf, fThreshold); + + // if no signal is found above threshold, first sample (0) is returned + // no need to attempt a fit if no signal is there + if (cf_sample == 0) + return 0.; + + if (fMethod == SIGMOID_FIT) + { + double par[4] = {0}; + bool ok = extractStartSigmoidFit(wf, cf_sample, par); + if (ok) + return par[0]; // Rising edge time from the sigmoid fit (can be non-integer). + else + std::cout << "Sigmoid fit did not converge; falling back to constant-fraction discrimation." << std::endl; + } + + // Defaulting to constant-fraction discrimation + return static_cast(cf_sample); +} + +// -------------------------------------------------------------------------------- + +template +std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector &wf, double thres) +{ + // Find the position of the minimum along the waveform + std::size_t minbin = findMinBin(wf, 0, wf.size()); + + // Search only a cropped region of the waveform backward from the min (20 samples) + std::size_t maxbin = (minbin >= 20) ? (minbin - 20) : 0; + std::size_t startbin = maxbin; + + // check if peak is high enough (remove noise) + auto delta = wf[maxbin] - wf[minbin]; + if (delta < thres) + return 0; // return first sample + + // Now we crawl betweem maxbin and minbin and we stop when: + // maxbin value - bin value > (maxbin value - minbin value )*0.2 + for (std::size_t bin = maxbin; bin < minbin; ++bin) + { + auto val = wf[maxbin] - wf[bin]; + if (val >= 0.2 * delta) // 20% + { + startbin = bin - 1; + break; + } + } + if (startbin < maxbin) + startbin = maxbin; + + return startbin; +} + +// -------------------------------------------------------------------------------- + +template +bool PulseStartExtractor::extractStartSigmoidFit(const std::vector &wf, std::size_t guess, double par[4]) +{ + std::size_t nsize = wf.size(); + std::vector x(nsize); + std::vector y(nsize); + + for (std::size_t i = 0; i < nsize; i++) + { + x[i] = static_cast(i); + y[i] = static_cast(wf[i]); + } + + TGraph *g = new TGraph(nsize, &x[0], &y[0]); + + // prepare initial parameter guesses for the fit + // baseline: level before signal + auto baseline = computeMedian(y); + + // amplitude: level after the signal (avg over 50 samples) + auto amplitude = 0.0; + std::size_t endIndex = (guess + 50 < nsize) ? guess + 50 : nsize; + for (std::size_t i = guess; i < endIndex; i++) + amplitude += y[i]; + amplitude /= (endIndex - guess); + amplitude -= baseline; + + // crop a window around the initial guess: +/- 25 samples + std::size_t llim = (guess >= 25) ? guess - 25 : 0; + std::size_t ulim = (guess + 25 < nsize) ? guess + 25 : nsize - 1; + + std::string sigmoidModel = "1/(1+exp(([0]-x)/[1]))*[2]+[3]"; + TF1 *fitFunc = new TF1("sigmoid", sigmoidModel.c_str(), x[llim], x[ulim]); + + fitFunc->SetParameter(0, x[guess]); // initial guess for rising edge time. + fitFunc->SetParameter(1, 0.3); // initial slope. + fitFunc->SetParameter(2, amplitude); + fitFunc->SetParameter(3, baseline); + + TFitResultPtr fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); + bool converged = !(bool)(int(fp)); + + // if fit did not converge, try again a few times + // tweak some parameters until it works... + int maxtries = 100; + int trial = 0; + while (!converged && trial < maxtries) + { + double newGuess = x[guess] + gRandom->Uniform(-1.5, 1.5); + double newSlope = 0.3 + gRandom->Uniform(-0.2, 0.2); + fitFunc->SetParameter(0, newGuess); + fitFunc->SetParameter(1, newSlope); + fitFunc->SetParameter(2, amplitude); + fitFunc->SetParameter(3, baseline); + fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); + converged = !(bool)(int(fp)); + trial++; + } + + // return the fit results in the parameters vector. + // p0 : rising edge as the inflection point (50% of amplitude) + par[0] = fitFunc->GetParameter(0); + par[1] = fitFunc->GetParameter(1); + par[2] = fitFunc->GetParameter(2); + par[3] = fitFunc->GetParameter(3); + + delete fitFunc; + delete g; + return converged; +} + + +template double PulseStartExtractor::extractStart(const std::vector& wf); +template double PulseStartExtractor::extractStart(const std::vector& wf); +template double PulseStartExtractor::extractStart(const std::vector& wf); +template double PulseStartExtractor::extractStart(const std::vector& wf); + +template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); +template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); +template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); +template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); + +template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); +template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); +template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); +template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); + +template short PulseStartExtractor::computeMedian(const std::vector& data); +template int PulseStartExtractor::computeMedian(const std::vector& data); +template float PulseStartExtractor::computeMedian(const std::vector& data); +template double PulseStartExtractor::computeMedian(const std::vector& data); + +template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); +template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); +template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); +template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.h b/icaruscode/Timing/Tools/PulseStartExtractor.h new file mode 100644 index 000000000..fad8580f3 --- /dev/null +++ b/icaruscode/Timing/Tools/PulseStartExtractor.h @@ -0,0 +1,65 @@ +/* + * File: PulseStartExtractor.h + * Author: M. Vicenzi (mvicenzi@bnl.gov) + * Description: Header file for the PulseStartExtractor class, which provides methods + * templated functions to extract the start (rising edge) time of reference + * signals (square pulses) using either constant fraction discrimination or + * a refined sigmoid fit. + */ + +#ifndef PULSE_START_EXTRACTOR_H +#define PULSE_START_EXTRACTOR_H + +#include +#include +#include +#include +#include "TF1.h" +#include "TGraph.h" +#include "TFitResultPtr.h" +#include "TRandom.h" + +class PulseStartExtractor +{ +public: + enum ExtractionMethod + { + CONSTANT_FRACTION, + SIGMOID_FIT + }; + + // Constructor: select extraction method and threshold (used in the CF method). + PulseStartExtractor(ExtractionMethod method = CONSTANT_FRACTION, double threshold = 10.0); + + // Templated public function to extract the start sample from a waveform. + // Returns the start sample index (std::size_t). + template + double extractStart(const std::vector &wf); + +private: + ExtractionMethod fMethod; + double fThreshold; + + // Templated helper: constant-fraction extraction. + template + std::size_t extractStartSampleCF(const std::vector &wf, double thres); + + // Templated helper: finds the index of the minimum element in the range [start, end). + template + std::size_t findMinBin(const std::vector &wf, std::size_t start, std::size_t end); + + // Templated helper: finds the index of the maximum element in the range [start, end). + template + std::size_t findMaxBin(const std::vector &wf, std::size_t start, std::size_t end); + + // Templated helper: computes the median of a vector. + template + T computeMedian(const std::vector &data); + + // Templated helper: sigmoid-fit extraction; fills par[4] with fit parameters. + // Returns true if the fit converged. + template + bool extractStartSigmoidFit(const std::vector &wf, std::size_t guess, double par[4]); +}; + +#endif // PULSE_START_EXTRACTOR_H From fabb6bbbaf51b2272e5fc87c51dc64e125d65ea0 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Fri, 28 Feb 2025 14:23:35 -0600 Subject: [PATCH 02/20] update to pulse start class --- .../Timing/Tools/PulseStartExtractor.cc | 341 +++++++++--------- icaruscode/Timing/Tools/PulseStartExtractor.h | 156 ++++++-- 2 files changed, 294 insertions(+), 203 deletions(-) diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.cc b/icaruscode/Timing/Tools/PulseStartExtractor.cc index 0a45b0828..4309905de 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.cc +++ b/icaruscode/Timing/Tools/PulseStartExtractor.cc @@ -1,11 +1,13 @@ -/* - * Author: M. Vicenzi (mvicenzi@bnl.gov) - * File: PulseStartExtractor.cc +/** + * @file icaruscode/Timing/Tools/PulseStartExtractor.cc + * @brief `icarus::timing::PulseStartExtractor` source. + * @author Matteo Vicenzi (mvicenzi@bnl.gov) + * @date Sun Feb 11 11:37:14 2024 * Description: Source file for the PulseStartExtractor class implementation. It provides * templated functions to extract the start (rising edge) time of reference * signals (square pulses) using either constant fraction discrimination or - * a refined sigmoid fit. - */ + * a refined logistic function fit. + **/ #include "PulseStartExtractor.h" #include @@ -14,198 +16,203 @@ #include #include #include +#include #include "TF1.h" #include "TGraph.h" #include "TFitResultPtr.h" #include "TRandom.h" -// Constructor -PulseStartExtractor::PulseStartExtractor(ExtractionMethod method, double threshold) - : fMethod(method), fThreshold(threshold) {} - -// -------------------------------------------------------------------------------- -template -std::size_t PulseStartExtractor::findMinBin(const std::vector &wf, std::size_t start, std::size_t end) -{ - auto minel = std::min_element(wf.begin() + start, wf.begin() + end); - size_t minsample = std::distance(wf.begin() + start, minel); - return minsample; -} - -template -std::size_t PulseStartExtractor::findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) -{ - auto maxel = std::max_element(wf.begin() + start, wf.begin() + end); - size_t maxsample = std::distance(wf.begin() + start, maxel); - return maxsample; -} - -// -------------------------------------------------------------------------------- -template -T PulseStartExtractor::computeMedian(const std::vector &data) -{ - std::vector copy = data; - std::nth_element(copy.begin(), copy.begin() + copy.size() / 2, copy.end()); - return copy[copy.size() / 2]; -} - -// -------------------------------------------------------------------------------- -template -double PulseStartExtractor::extractStart(const std::vector &wf) -{ - - // Try constant-fraction discrimination (default method) - // if fit method is selected, the result is used as initial guess for the fit - std::size_t cf_sample = extractStartSampleCF(wf, fThreshold); - - // if no signal is found above threshold, first sample (0) is returned - // no need to attempt a fit if no signal is there - if (cf_sample == 0) - return 0.; - - if (fMethod == SIGMOID_FIT) +namespace icarus::timing { + + // Constructor + PulseStartExtractor::PulseStartExtractor(ExtractionMethod method, double threshold) + : fMethod(method), fThreshold(threshold) {} + + // -------------------------------------------------------------------------------- + template + std::size_t PulseStartExtractor::findMinBin(const std::vector &wf, std::size_t start, std::size_t end) { - double par[4] = {0}; - bool ok = extractStartSigmoidFit(wf, cf_sample, par); - if (ok) - return par[0]; // Rising edge time from the sigmoid fit (can be non-integer). - else - std::cout << "Sigmoid fit did not converge; falling back to constant-fraction discrimation." << std::endl; + auto minel = std::min_element(wf.begin() + start, wf.begin() + end); + size_t minsample = std::distance(wf.begin() + start, minel); + return minsample; } - // Defaulting to constant-fraction discrimation - return static_cast(cf_sample); -} + template + std::size_t PulseStartExtractor::findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) + { + auto maxel = std::max_element(wf.begin() + start, wf.begin() + end); + size_t maxsample = std::distance(wf.begin() + start, maxel); + return maxsample; + } -// -------------------------------------------------------------------------------- + // -------------------------------------------------------------------------------- + template + T PulseStartExtractor::computeMedian(const std::vector &data) + { + std::vector copy = data; + std::nth_element(copy.begin(), copy.begin() + copy.size() / 2, copy.end()); + return copy[copy.size() / 2]; + } -template -std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector &wf, double thres) -{ - // Find the position of the minimum along the waveform - std::size_t minbin = findMinBin(wf, 0, wf.size()); + // -------------------------------------------------------------------------------- + template + double PulseStartExtractor::extractStart(const std::vector &wf) + { - // Search only a cropped region of the waveform backward from the min (20 samples) - std::size_t maxbin = (minbin >= 20) ? (minbin - 20) : 0; - std::size_t startbin = maxbin; + // Try constant-fraction discrimination (default method) + // if fit method is selected, the result is used as initial guess for the fit + std::size_t cf_sample = extractStartSampleCF(wf, fThreshold); - // check if peak is high enough (remove noise) - auto delta = wf[maxbin] - wf[minbin]; - if (delta < thres) - return 0; // return first sample + // if no signal is found above threshold, first sample (0) is returned + // no need to attempt a fit if no signal is there + if (cf_sample == 0) + return 0.; - // Now we crawl betweem maxbin and minbin and we stop when: - // maxbin value - bin value > (maxbin value - minbin value )*0.2 - for (std::size_t bin = maxbin; bin < minbin; ++bin) - { - auto val = wf[maxbin] - wf[bin]; - if (val >= 0.2 * delta) // 20% + if (fMethod == LOGISTIC_FIT) { - startbin = bin - 1; - break; + double par[4] = {0}; + bool ok = extractStartLogisticFit(wf, cf_sample, par); + if (ok) + return par[0]; // Rising edge time from the logistic fit (can be non-integer). + else + std::cout << "Sigmoid fit did not converge; falling back to constant-fraction discrimation." << std::endl; } + + // Defaulting to constant-fraction discrimation + return static_cast(cf_sample); } - if (startbin < maxbin) - startbin = maxbin; - return startbin; -} + // -------------------------------------------------------------------------------- -// -------------------------------------------------------------------------------- + template + std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector &wf, double thres) + { + // Find the position of the minimum along the waveform + std::size_t minbin = findMinBin(wf, 0, wf.size()); -template -bool PulseStartExtractor::extractStartSigmoidFit(const std::vector &wf, std::size_t guess, double par[4]) -{ - std::size_t nsize = wf.size(); - std::vector x(nsize); - std::vector y(nsize); + // Search only a cropped region of the waveform backward from the min (20 samples) + std::size_t maxbin = (minbin >= 20) ? (minbin - 20) : 0; + std::size_t startbin = maxbin; - for (std::size_t i = 0; i < nsize; i++) - { - x[i] = static_cast(i); - y[i] = static_cast(wf[i]); + // check if peak is high enough (remove noise) + auto delta = wf[maxbin] - wf[minbin]; + if (delta < thres) + return 0; // return first sample + + // Now we crawl betweem maxbin and minbin and we stop when: + // maxbin value - bin value > (maxbin value - minbin value )*0.2 + for (std::size_t bin = maxbin; bin < minbin; ++bin) + { + auto val = wf[maxbin] - wf[bin]; + if (val >= 0.2 * delta) // 20% + { + startbin = bin - 1; + break; + } + } + if (startbin < maxbin) + startbin = maxbin; + + return startbin; } - TGraph *g = new TGraph(nsize, &x[0], &y[0]); + // -------------------------------------------------------------------------------- - // prepare initial parameter guesses for the fit - // baseline: level before signal - auto baseline = computeMedian(y); + template + bool PulseStartExtractor::extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) + { + std::size_t nsize = wf.size(); + std::vector x(nsize); + std::vector y(nsize); - // amplitude: level after the signal (avg over 50 samples) - auto amplitude = 0.0; - std::size_t endIndex = (guess + 50 < nsize) ? guess + 50 : nsize; - for (std::size_t i = guess; i < endIndex; i++) - amplitude += y[i]; - amplitude /= (endIndex - guess); - amplitude -= baseline; + for (std::size_t i = 0; i < nsize; i++) + { + x[i] = static_cast(i); + y[i] = static_cast(wf[i]); + } - // crop a window around the initial guess: +/- 25 samples - std::size_t llim = (guess >= 25) ? guess - 25 : 0; - std::size_t ulim = (guess + 25 < nsize) ? guess + 25 : nsize - 1; + TGraph *g = new TGraph(nsize, &x[0], &y[0]); - std::string sigmoidModel = "1/(1+exp(([0]-x)/[1]))*[2]+[3]"; - TF1 *fitFunc = new TF1("sigmoid", sigmoidModel.c_str(), x[llim], x[ulim]); + // prepare initial parameter guesses for the fit + // baseline: level before signal + auto baseline = computeMedian(y); - fitFunc->SetParameter(0, x[guess]); // initial guess for rising edge time. - fitFunc->SetParameter(1, 0.3); // initial slope. - fitFunc->SetParameter(2, amplitude); - fitFunc->SetParameter(3, baseline); + // amplitude: level after the signal (avg over 50 samples) + auto amplitude = 0.0; + std::size_t endIndex = (guess + 50 < nsize) ? guess + 50 : nsize; + for (std::size_t i = guess; i < endIndex; i++) + amplitude += y[i]; + amplitude /= (endIndex - guess); + amplitude -= baseline; - TFitResultPtr fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); - bool converged = !(bool)(int(fp)); + // crop a window around the initial guess: +/- 25 samples + std::size_t llim = (guess >= 25) ? guess - 25 : 0; + std::size_t ulim = (guess + 25 < nsize) ? guess + 25 : nsize - 1; - // if fit did not converge, try again a few times - // tweak some parameters until it works... - int maxtries = 100; - int trial = 0; - while (!converged && trial < maxtries) - { - double newGuess = x[guess] + gRandom->Uniform(-1.5, 1.5); - double newSlope = 0.3 + gRandom->Uniform(-0.2, 0.2); - fitFunc->SetParameter(0, newGuess); - fitFunc->SetParameter(1, newSlope); + std::string logisticModel = "1/(1+exp(([0]-x)/[1]))*[2]+[3]"; + TF1 *fitFunc = new TF1("logistic", logisticModel.c_str(), x[llim], x[ulim]); + + fitFunc->SetParameter(0, x[guess]); // initial guess for rising edge time. + fitFunc->SetParameter(1, 0.3); // initial slope. fitFunc->SetParameter(2, amplitude); fitFunc->SetParameter(3, baseline); - fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); - converged = !(bool)(int(fp)); - trial++; + + TFitResultPtr fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); + bool converged = !(bool)(int(fp)); + + // if fit did not converge, try again a few times + // tweak some parameters until it works... + int maxtries = 100; + int trial = 0; + while (!converged && trial < maxtries) + { + double newGuess = x[guess] + gRandom->Uniform(-1.5, 1.5); + double newSlope = 0.3 + gRandom->Uniform(-0.2, 0.2); + fitFunc->SetParameter(0, newGuess); + fitFunc->SetParameter(1, newSlope); + fitFunc->SetParameter(2, amplitude); + fitFunc->SetParameter(3, baseline); + fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); + converged = !(bool)(int(fp)); + trial++; + } + + // return the fit results in the parameters vector. + // p0 : rising edge as the inflection point (50% of amplitude) + par[0] = fitFunc->GetParameter(0); + par[1] = fitFunc->GetParameter(1); + par[2] = fitFunc->GetParameter(2); + par[3] = fitFunc->GetParameter(3); + + delete fitFunc; + delete g; + return converged; } - // return the fit results in the parameters vector. - // p0 : rising edge as the inflection point (50% of amplitude) - par[0] = fitFunc->GetParameter(0); - par[1] = fitFunc->GetParameter(1); - par[2] = fitFunc->GetParameter(2); - par[3] = fitFunc->GetParameter(3); - - delete fitFunc; - delete g; - return converged; -} - - -template double PulseStartExtractor::extractStart(const std::vector& wf); -template double PulseStartExtractor::extractStart(const std::vector& wf); -template double PulseStartExtractor::extractStart(const std::vector& wf); -template double PulseStartExtractor::extractStart(const std::vector& wf); - -template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); -template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); -template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); -template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); - -template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); -template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); -template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); -template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); - -template short PulseStartExtractor::computeMedian(const std::vector& data); -template int PulseStartExtractor::computeMedian(const std::vector& data); -template float PulseStartExtractor::computeMedian(const std::vector& data); -template double PulseStartExtractor::computeMedian(const std::vector& data); - -template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); -template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); -template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); -template bool PulseStartExtractor::extractStartSigmoidFit(const std::vector& wf, std::size_t guess, double par[4]); + + template double PulseStartExtractor::extractStart(const std::vector& wf); + template double PulseStartExtractor::extractStart(const std::vector& wf); + template double PulseStartExtractor::extractStart(const std::vector& wf); + template double PulseStartExtractor::extractStart(const std::vector& wf); + + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); + + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); + + template short PulseStartExtractor::computeMedian(const std::vector& data); + template int PulseStartExtractor::computeMedian(const std::vector& data); + template float PulseStartExtractor::computeMedian(const std::vector& data); + template double PulseStartExtractor::computeMedian(const std::vector& data); + + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); + +} // icarus::timing namespace \ No newline at end of file diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.h b/icaruscode/Timing/Tools/PulseStartExtractor.h index fad8580f3..d5f117fa3 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.h +++ b/icaruscode/Timing/Tools/PulseStartExtractor.h @@ -1,11 +1,13 @@ -/* - * File: PulseStartExtractor.h - * Author: M. Vicenzi (mvicenzi@bnl.gov) - * Description: Header file for the PulseStartExtractor class, which provides methods +/** + * @file icaruscode/Timing/Tools/PulseStartExtractor.h + * @brief `icarus::timing::PulseStartExtractor` header. + * @author Matteo Vicenzi (mvicenzi@bnl.gov) + * @date Sun Feb 11 11:37:14 2024 + * Description: Header file for the PulseStartExtractor class implementation. It provides * templated functions to extract the start (rising edge) time of reference * signals (square pulses) using either constant fraction discrimination or - * a refined sigmoid fit. - */ + * a refined logistic function fit. + **/ #ifndef PULSE_START_EXTRACTOR_H #define PULSE_START_EXTRACTOR_H @@ -14,52 +16,134 @@ #include #include #include +#include #include "TF1.h" #include "TGraph.h" #include "TFitResultPtr.h" #include "TRandom.h" -class PulseStartExtractor -{ -public: +namespace icarus::timing { + enum ExtractionMethod { CONSTANT_FRACTION, - SIGMOID_FIT + LOGISTIC_FIT }; - // Constructor: select extraction method and threshold (used in the CF method). - PulseStartExtractor(ExtractionMethod method = CONSTANT_FRACTION, double threshold = 10.0); + // map for string to enum conversion + std::map const stringToExtractionMethod = { + { "LogisticFit", LOGISTIC_FIT }, + { "ConstantFraction", CONSTANT_FRACTION } + }; + +/** + * @brief Extracts the start time of a pulse from a waveform. + * + * This class implements methods for extracting the start time of reference pulses + * digitized in PMT special channels. These reference waveforms are expected to exhibit + * a negative-polarity square pulse with a sharp rising edge. + * + * The extraction can be performed using either a legacy constant-fraction discrimination (CF) + * algorithm or a refined logistic function fit. + * @see icarus::timing::ExtractionMethod for the available methods. + * + * Inputs + * ------------------------- + * + * The extraction method is configured via the constructor by specifying one of the available + * methods (defined by the `icarus::timing::ExtractionMethod` enum) and a threshold value. + * * `method` (`icarus::timing::ExtractionMethod`): specifies the selected method for the + * pulse start extraction. The default is `icarus::timing::CONSTANT_FRACTION`. + * * `threshold` (`double`): detection threshold in ADC counts to avoid selecting noise of + * the reference pulse is missing from its waveform. Default id `500` ADC. + * + * When using a fit method, the initial parameters guesses are determined using the + * constant-fraction algorithm to help ensure fit convergence. + * + * Signal timing extraction + * ---------------------------------- + * + * The reference waveform is expected to exhibit a negative-polarity square wave. + * Its selection is performed simply by looking at the absolute minimum of the waveform. + * A detection threshold on the amplitude is used to exclude picking up spurios signals + * in case the large square pulse is missing (which could happen for EW/RWM signals). + * + * ### Constant-fraction discrimination + * + * The procedure is the following: + * * the absolute minimum of the waveform is found; + * * an interval starting 20 ticks before that minimum is considered and the baseline level + * is defined as the value at the start of that interval; + * * if the baseline-minimum difference is below a threshold, it is assumed to be noise + * and the start sample (`0`) is returned; + * * within the defined window, the algorithm searches the first sample where the signal + * deviates from the baseline by at least 20% of the full amplitude (baseline-minimum difference); + * The sample immediately before is returned as the pulse start time. + * + * This algorithm is affected by the 2ns discretization of the input waveforms as there is no + * interpolation between the samples. + * + * ### Logistic Fit Method: + * The pulse rising edge is fitted using a logistic function: `1/(1+exp(([0]-x)/[1]))*[2]+[3]`. + * The estimate for the pulse start is then taken to be the inflection point (ie. the steepest point, + * where the first derivative is maximal) which corresponds to parameter `p0`. + * It also represents the point at 50% of the amplitude. + * + * The procedure is the following: + * * the constant-fraction method is ran first to obtain an initial guess for the pulse + * start time (`p0`). If no signal is found, the start sample (`0`) is returned; + * * an initial guess for the baseline (`p3`) is estimated as the median of the full waveform; + * * an initial guess for the amplitude (`p2`) is estimated averaging the first 50 samples + * after the initial guess for the rise time (`p0`). + * * the logistic function fit is performed over a window of `[-25,25]` samples around the + * initial guess for the rise time; + * * if the fit converges, `p0` is returned as the pulse start time; + * * if the fit fails to converge, multiple retries with slight random variations around the + * initial parameters are attempted. If all retries are exahusted, it falls back to + * returning the constant-fraction method’s result. + * + * This algorithm allows to interpolate between samples, achieving a better timing resolution. + * + */ + class PulseStartExtractor + { + public: + + + // Constructor: select extraction method and threshold in ADC (used in the CF method). + PulseStartExtractor(ExtractionMethod method = CONSTANT_FRACTION, double threshold= 500); - // Templated public function to extract the start sample from a waveform. - // Returns the start sample index (std::size_t). - template - double extractStart(const std::vector &wf); + // Templated public function to extract the start sample from a waveform. + // Returns the start sample index (std::size_t). + template + double extractStart(const std::vector &wf); -private: - ExtractionMethod fMethod; - double fThreshold; + private: + ExtractionMethod fMethod; + double fThreshold; - // Templated helper: constant-fraction extraction. - template - std::size_t extractStartSampleCF(const std::vector &wf, double thres); + // Templated helper: constant-fraction extraction. + template + std::size_t extractStartSampleCF(const std::vector &wf, double thres); - // Templated helper: finds the index of the minimum element in the range [start, end). - template - std::size_t findMinBin(const std::vector &wf, std::size_t start, std::size_t end); + // Templated helper: finds the index of the minimum element in the range [start, end). + template + std::size_t findMinBin(const std::vector &wf, std::size_t start, std::size_t end); - // Templated helper: finds the index of the maximum element in the range [start, end). - template - std::size_t findMaxBin(const std::vector &wf, std::size_t start, std::size_t end); + // Templated helper: finds the index of the maximum element in the range [start, end). + template + std::size_t findMaxBin(const std::vector &wf, std::size_t start, std::size_t end); - // Templated helper: computes the median of a vector. - template - T computeMedian(const std::vector &data); + // Templated helper: computes the median of a vector. + template + T computeMedian(const std::vector &data); + + // Templated helper: logistic-fit extraction; fills par[4] with fit parameters. + // Returns true if the fit converged. + template + bool extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]); + }; - // Templated helper: sigmoid-fit extraction; fills par[4] with fit parameters. - // Returns true if the fit converged. - template - bool extractStartSigmoidFit(const std::vector &wf, std::size_t guess, double par[4]); -}; +} // icarus::timing namespace #endif // PULSE_START_EXTRACTOR_H From 0cad80939b3b519b8fa175d6637327590cec30e3 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Fri, 28 Feb 2025 14:24:28 -0600 Subject: [PATCH 03/20] integrate pulse start class for waveform corrections --- .../Decode/DaqDecoderICARUSPMT_module.cc | 24 ++++- .../PMTWaveformTimeCorrectionExtractor.cxx | 101 ++++-------------- .../PMTWaveformTimeCorrectionExtractor.h | 44 ++++---- 3 files changed, 66 insertions(+), 103 deletions(-) diff --git a/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc b/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc index 6fa1f4a7f..13b37701c 100644 --- a/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc +++ b/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc @@ -15,6 +15,7 @@ #include "icaruscode/Decode/ChannelMapping/IICARUSChannelMap.h" #include "icaruscode/IcarusObj/PMTWaveformTimeCorrection.h" #include "icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h" +#include "icaruscode/Timing/Tools/PulseStartExtractor.h" #include "icaruscode/Timing/IPMTTimingCorrectionService.h" #include "icaruscode/Timing/PMTTimingCorrections.h" #include "icarusalg/Utilities/BinaryDumpUtils.h" // icarus::ns::util::bin() @@ -192,6 +193,12 @@ namespace icarus { class DaqDecoderICARUSPMT; } * waveform-based timing correction is used. * * `ApplyCableDelayCorrection` (flag, default: `true`): if set, applies the * cable delay corrections from a database. + * * `PulseStartExtractionMethod` (string, default: `LogisticFit`): pulse start + * extraction method for the time corrections. + * @see icarus::timing::PulseStartExtractor for details and options. + * * `PulseStartExtractionThreshold` (double, default: `500.`): detection threshold + * in ADC counts for the extraction of time corrections. + * @see icarus::timing::PulseStartExtractor for details and options. * * `DataTrees` (list of strings, default: none): list of data trees to be * produced; if none (default), then `TFileService` is not required. * * `SkipWaveforms` (flag, default: `false`) if set, waveforms won't be @@ -704,6 +711,18 @@ class icarus::DaqDecoderICARUSPMT: public art::EDProducer { true }; + fhicl::Atom PulseStartExtractionMethod { + Name("PulseStartExtractionMethod"), + Comment("pulse start extraction method for the time corrections"), + "LogisticFit" + }; + + fhicl::Atom PulseStartExtractionThreshold { + Name("PulseStartExtractionThreshold"), + Comment("detection threshold in ADC counts for the extraction of time corrections"), + 500. + }; + fhicl::OptionalAtom PMTconfigTag { Name("PMTconfigTag"), Comment("input tag for the PMT readout board configuration information") @@ -1632,7 +1651,10 @@ icarus::DaqDecoderICARUSPMT::DaqDecoderICARUSPMT(Parameters const& params) } , fPMTWaveformTimeCorrectionManager{ fDetTimings.clockData(), fChannelMap, - fPMTTimingCorrectionsService, fDiagnosticOutput + fPMTTimingCorrectionsService, + icarus::timing::stringToExtractionMethod.at(params().PulseStartExtractionMethod()), + params().PulseStartExtractionThreshold(), + fDiagnosticOutput } , fOpticalTick{ fDetTimings.OpticalClockPeriod() } , fNominalTriggerTime{ fDetTimings.TriggerTime() } diff --git a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx index 1ed8b124a..55bcca186 100644 --- a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx +++ b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.cxx @@ -85,6 +85,19 @@ auto icarus::timing::PMTWaveformTimeCorrectionExtractor::UnknownCrate::makeBaseE } +icarus::timing::PMTWaveformTimeCorrectionExtractor::NoSignalFound::NoSignalFound + (unsigned int channel, double threshold) + : Error{ makeBaseException(channel, threshold) } + {} + +auto icarus::timing::PMTWaveformTimeCorrectionExtractor::NoSignalFound::makeBaseException + (unsigned int channel, double threshold) -> Error +{ + return Error{} + << "No signal found in special channel ID " << channel + << " (0x" << std::hex << channel << ") with " << threshold << " ADC threshold.\n"; +} + // ----------------------------------------------------------------------------- @@ -92,81 +105,19 @@ auto icarus::timing::PMTWaveformTimeCorrectionExtractor::UnknownCrate::makeBaseE icarus::timing::PMTWaveformTimeCorrectionExtractor::PMTWaveformTimeCorrectionExtractor( detinfo::DetectorClocksData const detTimingService, icarusDB::IICARUSChannelMap const & channelMapService, - icarusDB::PMTTimingCorrections const* pmtTimingCorrectionsService, + icarusDB::PMTTimingCorrections const* pmtTimingCorrectionsService, + icarus::timing::ExtractionMethod const pulseStartExtractionMethod, + double pulseStartExtractionThreshold, bool verbose ) : fClocksData( detTimingService ) , fChannelMap( channelMapService ) , fPMTTimingCorrectionsService( pmtTimingCorrectionsService ) +, fPulseStartExtractionMethod( pulseStartExtractionMethod ) +, fPulseStartExtractionThreshold( pulseStartExtractionThreshold ) , fVerbose( verbose ) {} -// ----------------------------------------------------------------------------- - - -template - size_t icarus::timing::PMTWaveformTimeCorrectionExtractor::getMinBin( - std::vector const& vv, size_t startElement, size_t endElement ){ - - auto minel = - std::min_element( vv.begin()+startElement, vv.begin()+endElement ); - size_t minsample = std::distance( vv.begin()+startElement, minel ); - - return minsample; -} - - -// ----------------------------------------------------------------------------- - - -template - size_t icarus::timing::PMTWaveformTimeCorrectionExtractor::getMaxBin( - std::vector const& vv, size_t startElement, size_t endElement){ - - auto maxel = - std::max_element( vv.begin()+startElement, vv.begin()+endElement ); - - size_t maxsample = std::distance( vv.begin()+startElement, maxel ); - - return maxsample; -} - - -// ----------------------------------------------------------------------------- - - -template - size_t icarus::timing::PMTWaveformTimeCorrectionExtractor::getStartSample( std::vector const& vv ){ - - // NOTE: when changing this algorithm, also update the documentation - // in the section "Signal timing extraction" of the class documentation - - // We are thinking in inverted polarity - size_t minbin = getMinBin( vv, 0, vv.size() ); - - //Search only a cropped region of the waveform backward from the min - size_t maxbin = minbin-20; //getMaxBin( wave, minbin-20, minbin ); - - // Now we crawl betweem maxbin and minbin and we stop when: - //( maxbin value - minbin value )*0.05 > (maxbin value - bin value ) - size_t startbin = maxbin; - auto delta = vv[maxbin]-vv[minbin]; - for( size_t bin=maxbin; bin= 0.2*delta ){ - startbin = bin - 1; - break; - } - } - - if( startbin < maxbin ){ - startbin=maxbin; - } - - return startbin; -} - - //--------------------------------------------------------------------------------------- @@ -190,19 +141,13 @@ void icarus::timing::PMTWaveformTimeCorrectionExtractor::findWaveformTimeCorrect throw UnknownCrate{ waveChannelID }; // This will be the first sample of the falling edge of the special channel signal - // Which corresponds to the global trigger time. - PulseStartExtractor extractor(PulseStartExtractor::SIGMOID_FIT, 500.0); + // which corresponds to the global trigger time. + icarus::timing::PulseStartExtractor extractor(fPulseStartExtractionMethod, fPulseStartExtractionThreshold); double startSampleSignal = extractor.extractStart(wave); - PulseStartExtractor extractor1(PulseStartExtractor::CONSTANT_FRACTION, 500.0); - double startSampleSignal1 = extractor1.extractStart(wave); - - std::cout << "channel " << waveChannelID << " crate " << crateSignalID << " sample " << startSampleSignal1 << " fit " << startSampleSignal << - "diff " << startSampleSignal - startSampleSignal1 << std::endl; - - //int startSampleSignal = static_cast( getStartSample( wave ) ); - if (startSampleSignal<1) - throw Error("Can't find digitized trigger signal!"); + // if it returns the first sample, it means no signal was found + if (startSampleSignal < 1 ) + throw NoSignalFound(waveChannelID, fPulseStartExtractionThreshold); // allocates room for correction for `channel`; intermediate ones are defaulted auto correctionFor diff --git a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h index d8c330f7c..8fe0a2d65 100644 --- a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h +++ b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h @@ -12,6 +12,7 @@ #include "icaruscode/Decode/ChannelMapping/IICARUSChannelMap.h" #include "icaruscode/Timing/PMTTimingCorrections.h" #include "icaruscode/IcarusObj/PMTWaveformTimeCorrection.h" +#include "icaruscode/Timing/Tools/PulseStartExtractor.h" // LArSoft libraries #include "lardataobj/RawData/OpDetWaveform.h" @@ -47,16 +48,13 @@ namespace icarus::timing { class PMTWaveformTimeCorrectionExtractor; } * Signal timing extraction * ------------------------- * - * The detection algorithm is currently quite unsophisticated. - * * The reference signal is expected to be a sharp square wave in negative - * polarity. The time of the correction is based on the front side of that wave: + * polarity. The time of the correction is based on the front side of that wave. + * The extraction is perfomed by `icarus::timing::PulseStartExtractor` by + * specifying one of the available extraction methods (constant-fraction discrimation, + * logitstic function fit) and an ADC threshold for the signal identification. * - * * the absolute minimum of the waveform is found - * * an interval starting 20 ticks before that minimum is considered - * * the baseline level is defined as the value at the start of that interval - * * the start time is set to the exact tick with an amplitude exceeding 20% - * of the maximum of the signal from the baseline + * @see `icarus::timing::PulseStartExtractor` for details. * */ class icarus::timing::PMTWaveformTimeCorrectionExtractor { @@ -88,13 +86,21 @@ class icarus::timing::PMTWaveformTimeCorrectionExtractor { private: static Error makeBaseException(unsigned int channel); }; + /// Exception thrown when no signal is found above threshold + struct NoSignalFound: Error { + NoSignalFound(unsigned int channel, double threshold); + private: static Error makeBaseException(unsigned int channel, double threshold); + }; + // --- END ---- Exceptions --------------------------------------------- PMTWaveformTimeCorrectionExtractor( detinfo::DetectorClocksData const detTimingService, icarusDB::IICARUSChannelMap const & channelMapService, - icarusDB::PMTTimingCorrections const* pmtTimingCorrectionsService, + icarusDB::PMTTimingCorrections const* pmtTimingCorrectionsService, + icarus::timing::ExtractionMethod const pulseStartExtractionMethod, + double threshold, bool verbose ); /** @@ -106,6 +112,7 @@ class icarus::timing::PMTWaveformTimeCorrectionExtractor { * @throw UnknownCrate if `wave` is from an unexpected readout crate * @throw MultipleCorrectionsForChannel if `corrections` already contains * a correction for any of the channels we are associating to the new correction + * @throw NoSignalFound if `wave` does not contain any pulse above threshold * * This function performs the analysis of the reference waveform `wave`, * extracts the correction out of it and associates it in `corrections` @@ -141,6 +148,10 @@ class icarus::timing::PMTWaveformTimeCorrectionExtractor { icarusDB::PMTTimingCorrections const* fPMTTimingCorrectionsService = nullptr; + icarus::timing::ExtractionMethod const fPulseStartExtractionMethod; + + double const fPulseStartExtractionThreshold; + bool const fVerbose; std::map> const @@ -155,21 +166,6 @@ class icarus::timing::PMTWaveformTimeCorrectionExtractor { {0x0000 , { 21, 22, 23 }}, }; - template - static size_t getMaxBin( - std::vector const& vv, - size_t startElement, - size_t endElement); - - template - static size_t getMinBin( - std::vector const& vv, - size_t startElement, - size_t endElement); - - template - static size_t getStartSample( std::vector const& vv ); - }; From e6554043d0a5ee4e7a39d82f3da853f4afc33ab5 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Sun, 2 Mar 2025 16:49:13 -0600 Subject: [PATCH 04/20] pulse start extractor integration with beam signals module --- icaruscode/IcarusObj/PMTBeamSignal.h | 2 +- icaruscode/Timing/CMakeLists.txt | 1 + .../Timing/PMTBeamSignalsExtractor_module.cc | 133 ++++-------------- .../Timing/Tools/PulseStartExtractor.cc | 52 +++---- icaruscode/Timing/Tools/PulseStartExtractor.h | 12 +- icaruscode/Timing/timing_beam.fcl | 3 +- 6 files changed, 62 insertions(+), 141 deletions(-) diff --git a/icaruscode/IcarusObj/PMTBeamSignal.h b/icaruscode/IcarusObj/PMTBeamSignal.h index 842af0375..d0fdfd0ff 100644 --- a/icaruscode/IcarusObj/PMTBeamSignal.h +++ b/icaruscode/IcarusObj/PMTBeamSignal.h @@ -52,7 +52,7 @@ namespace icarus::timing std::string crate = ""; /// Sample within the waveform where the reference signal is found. - std::size_t sample = NoSample; + double sample = NoSample; /// Start time in electronics time [us]. double startTimeAbs = NoTime; diff --git a/icaruscode/Timing/CMakeLists.txt b/icaruscode/Timing/CMakeLists.txt index 14de1d53e..92d8b0dc8 100644 --- a/icaruscode/Timing/CMakeLists.txt +++ b/icaruscode/Timing/CMakeLists.txt @@ -11,6 +11,7 @@ set( MODULE_LIBRARIES art_root_io::tfile_support lardataobj::RawData ROOT::Tree + icaruscode_Timing_Tools ) set( LIB_LIBRARIES art::Framework_Services_Registry diff --git a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc index c190f770f..d79edb717 100644 --- a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc +++ b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc @@ -19,6 +19,7 @@ #include "icaruscode/Decode/ChannelMapping/IICARUSChannelMap.h" #include "icaruscode/IcarusObj/PMTWaveformTimeCorrection.h" #include "icaruscode/IcarusObj/PMTBeamSignal.h" +#include "icaruscode/Timing/Tools/PulseStartExtractor.h" #include "lardataalg/DetectorInfo/DetectorTimingTypes.h" // electronics_time #include "lardataalg/Utilities/quantities/spacetime.h" #include "lardataobj/RawData/OpDetWaveform.h" @@ -65,7 +66,8 @@ namespace icarus::timing * V1730 setup from `CAEN_V1730_setup_icarus.fcl` mapping the special signals. * It is meant to be the same configuration as used by the PMT decoding * (see `BoardSetup` configuration parameter in `icarus::DaqDecoderICARUSPMT`). - + * * `TimeExtractionMethod` (string): pulse start time extraction method. + * @see icarus::timing::PulseStartExtractor for details and options. * * `ADCThreshold` (int): detection threshold to avoid cross-talk * noise if one signal is missing from its waveform. * * `DebugTrees` (bool): flag to produce plain ROOT trees for debugging. @@ -74,18 +76,13 @@ namespace icarus::timing * Signal timing extraction * ------------------------- * - * The algorithm is quite unsophisticated and follows what is done for - * the digitized trigger signal in `PMTWaveformTimeCorrectionExtractor`. - * The reference signal is expected to be a sharp square wave in negative - * polarity. The time is based on the front side of that wave: - * - * * the absolute minimum of the waveform is found - * * an interval starting 20 ticks before that minimum is considered - * * the baseline level is defined as the value at the start of that interval - * * if the baseline-minimum difference is below a threshold, it is assumed to be noise - * and no time is returned - * * if not, the start time is set to the exact tick with an amplitude exceeding 20% - * of the minimum of the signal from the baseline + * The RWM/EW signals are expected to be a sharp square wave in negative + * polarity. The time of the correction is based on the front side of that wave. + * The extraction is perfomed by `icarus::timing::PulseStartExtractor` by + * specifying one of the available extraction methods (constant-fraction discrimation, + * logitstic function fit) and an ADC threshold for the signal identification. + * + * @see `icarus::timing::PulseStartExtractor` for details. * * * Output products @@ -100,7 +97,7 @@ namespace icarus::timing * * If the event is offbeam, these vectors are produced empty. * - * * + * * Debugging tree * --------------- * @@ -129,14 +126,6 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer // process waveforms void extractBeamSignalTime(art::Event &e, art::InputTag const &label); - template - static T Median(std::vector data); - template - static std::size_t getMaxBin(std::vector const &vv, std::size_t startElement, std::size_t endElement); - template - static std::size_t getMinBin(std::vector const &vv, std::size_t startElement, std::size_t endElement); - template - static std::size_t getStartSample(std::vector const &vv, T thres); // unpack the V1730 special channels settings in a useful way static std::map extractBoardBySpecialChannel(std::vector const &setup); @@ -176,8 +165,8 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer art::InputTag const fEWlabel; /// Trigger-hardware correction instance art::InputTag const fTriggerCorrectionLabel; - /// Threshold for pulse selection - short int const fADCThreshold; + /// Manager for pulse start extraction + icarus::timing::PulseStartExtractor const fPulseStartExtractor; /// Special channel to board association in a map std::map const fBoardBySpecialChannel; @@ -200,7 +189,7 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer int m_n_channels; unsigned int m_channel; double m_wfstart; - std::size_t m_sample; + double m_sample; double m_time; double m_time_abs; double m_utime_abs; @@ -226,7 +215,8 @@ icarus::timing::PMTBeamSignalsExtractor::PMTBeamSignalsExtractor(fhicl::Paramete fRWMlabel(pset.get("RWMlabel")), fEWlabel(pset.get("EWlabel")), fTriggerCorrectionLabel(pset.get("TriggerCorrectionLabel")), - fADCThreshold(pset.get("ADCThreshold")), + fPulseStartExtractor{ icarus::timing::stringToExtractionMethod.at(pset.get("TimeExtractionMethod")), + pset.get("ADCThreshold")}, fBoardBySpecialChannel(extractBoardBySpecialChannel(pset.get>("BoardSetup"))) { // Call appropriate consumes<>() functions here. @@ -389,16 +379,22 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & detinfo::timescales::electronics_time tstart = util::quantities::points::microsecond{wave.TimeStamp()}; - // if nothing is found, first sample is returned (0) - m_sample = getStartSample(wave.Waveform(), fADCThreshold); - m_channel = wave.ChannelNumber(); m_wfstart = tstart.value(); + std::string crate = getCrate(m_channel); + + // if nothing is found, first sample is returned (0) + m_sample = fPulseStartExtractor.extractStart(wave.Waveform()); + if (m_sample < 1){ + m_sample = icarus::timing::NoSample; + mf::LogTrace("PMTBeamSignalsExtractor") << "No " << label.encode() << " signal found in channel " << m_channel + << " (crate " << crate << ")"; + } + m_utime_abs = (m_sample != icarus::timing::NoSample) ? tstart.value() + fPMTsamplingTick * m_sample : icarus::timing::NoTime; m_time_abs = (m_sample != icarus::timing::NoSample) ? m_utime_abs + getTriggerCorrection(m_channel) : icarus::timing::NoTime; m_time = (m_sample != icarus::timing::NoSample) ? m_time_abs - ftriggerTime : icarus::timing::NoTime; - std::string crate = getCrate(m_channel); icarus::timing::PMTBeamSignal beamTime{m_channel, getDigitizerLabel(m_channel), crate, m_sample, m_time_abs, m_time}; fBeamSignals[l].emplace(crate, std::move(beamTime)); @@ -414,83 +410,6 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & } } -// --------------------------------------------------------------------------- - -template -T icarus::timing::PMTBeamSignalsExtractor::Median(std::vector data) -{ - - std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end()); - return data[data.size() / 2]; -} - -// ----------------------------------------------------------------------------- - -template -std::size_t icarus::timing::PMTBeamSignalsExtractor::getMinBin( - std::vector const &vv, std::size_t startElement, std::size_t endElement) -{ - - auto minel = - std::min_element(vv.begin() + startElement, vv.begin() + endElement); - std::size_t minsample = std::distance(vv.begin() + startElement, minel); - - return minsample; -} - -// ----------------------------------------------------------------------------- - -template -std::size_t icarus::timing::PMTBeamSignalsExtractor::getMaxBin( - std::vector const &vv, std::size_t startElement, std::size_t endElement) -{ - - auto maxel = - std::max_element(vv.begin() + startElement, vv.begin() + endElement); - - std::size_t maxsample = std::distance(vv.begin() + startElement, maxel); - - return maxsample; -} - -// ----------------------------------------------------------------------------- - -template -std::size_t icarus::timing::PMTBeamSignalsExtractor::getStartSample(std::vector const &vv, T thres) -{ - - // We are thinking in inverted polarity - std::size_t minbin = getMinBin(vv, 0, vv.size()); - - // Search only a cropped region of the waveform backward from the min - std::size_t maxbin = (minbin - 20) ? (minbin - 20) : 0; - - // Now we crawl betweem maxbin and minbin and we stop when: - // maxbin value - bin value > (maxbin value - minbin value )*0.2 - std::size_t startbin = maxbin; - auto delta = vv[maxbin] - vv[minbin]; - - if (delta < thres) // just noise - return icarus::timing::NoSample; // return no sample - - for (std::size_t bin = maxbin; bin < minbin; bin++) - { - auto val = vv[maxbin] - vv[bin]; - if (val >= 0.2 * delta) - { // 20% - startbin = bin - 1; - break; - } - } - - if (startbin < maxbin) - { - startbin = maxbin; - } - - return startbin; -} - // ----------------------------------------------------------------------------- std::map icarus::timing::PMTBeamSignalsExtractor::extractBoardBySpecialChannel( diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.cc b/icaruscode/Timing/Tools/PulseStartExtractor.cc index 4309905de..c7212861c 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.cc +++ b/icaruscode/Timing/Tools/PulseStartExtractor.cc @@ -30,7 +30,7 @@ namespace icarus::timing { // -------------------------------------------------------------------------------- template - std::size_t PulseStartExtractor::findMinBin(const std::vector &wf, std::size_t start, std::size_t end) + std::size_t PulseStartExtractor::findMinBin(const std::vector &wf, std::size_t start, std::size_t end) const { auto minel = std::min_element(wf.begin() + start, wf.begin() + end); size_t minsample = std::distance(wf.begin() + start, minel); @@ -38,7 +38,7 @@ namespace icarus::timing { } template - std::size_t PulseStartExtractor::findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) + std::size_t PulseStartExtractor::findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) const { auto maxel = std::max_element(wf.begin() + start, wf.begin() + end); size_t maxsample = std::distance(wf.begin() + start, maxel); @@ -47,7 +47,7 @@ namespace icarus::timing { // -------------------------------------------------------------------------------- template - T PulseStartExtractor::computeMedian(const std::vector &data) + T PulseStartExtractor::computeMedian(const std::vector &data) const { std::vector copy = data; std::nth_element(copy.begin(), copy.begin() + copy.size() / 2, copy.end()); @@ -56,7 +56,7 @@ namespace icarus::timing { // -------------------------------------------------------------------------------- template - double PulseStartExtractor::extractStart(const std::vector &wf) + double PulseStartExtractor::extractStart(const std::vector &wf) const { // Try constant-fraction discrimination (default method) @@ -85,7 +85,7 @@ namespace icarus::timing { // -------------------------------------------------------------------------------- template - std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector &wf, double thres) + std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector &wf, double thres) const { // Find the position of the minimum along the waveform std::size_t minbin = findMinBin(wf, 0, wf.size()); @@ -119,7 +119,7 @@ namespace icarus::timing { // -------------------------------------------------------------------------------- template - bool PulseStartExtractor::extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) + bool PulseStartExtractor::extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) const { std::size_t nsize = wf.size(); std::vector x(nsize); @@ -190,29 +190,29 @@ namespace icarus::timing { } - template double PulseStartExtractor::extractStart(const std::vector& wf); - template double PulseStartExtractor::extractStart(const std::vector& wf); - template double PulseStartExtractor::extractStart(const std::vector& wf); - template double PulseStartExtractor::extractStart(const std::vector& wf); + template double PulseStartExtractor::extractStart(const std::vector& wf) const; + template double PulseStartExtractor::extractStart(const std::vector& wf) const; + template double PulseStartExtractor::extractStart(const std::vector& wf) const; + template double PulseStartExtractor::extractStart(const std::vector& wf) const; - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres); + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; + template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end); + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; + template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; - template short PulseStartExtractor::computeMedian(const std::vector& data); - template int PulseStartExtractor::computeMedian(const std::vector& data); - template float PulseStartExtractor::computeMedian(const std::vector& data); - template double PulseStartExtractor::computeMedian(const std::vector& data); + template short PulseStartExtractor::computeMedian(const std::vector& data) const; + template int PulseStartExtractor::computeMedian(const std::vector& data) const; + template float PulseStartExtractor::computeMedian(const std::vector& data) const; + template double PulseStartExtractor::computeMedian(const std::vector& data) const; - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]); + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; + template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; } // icarus::timing namespace \ No newline at end of file diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.h b/icaruscode/Timing/Tools/PulseStartExtractor.h index d5f117fa3..cb38fa910 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.h +++ b/icaruscode/Timing/Tools/PulseStartExtractor.h @@ -116,7 +116,7 @@ namespace icarus::timing { // Templated public function to extract the start sample from a waveform. // Returns the start sample index (std::size_t). template - double extractStart(const std::vector &wf); + double extractStart(const std::vector &wf) const; private: ExtractionMethod fMethod; @@ -124,24 +124,24 @@ namespace icarus::timing { // Templated helper: constant-fraction extraction. template - std::size_t extractStartSampleCF(const std::vector &wf, double thres); + std::size_t extractStartSampleCF(const std::vector &wf, double thres) const; // Templated helper: finds the index of the minimum element in the range [start, end). template - std::size_t findMinBin(const std::vector &wf, std::size_t start, std::size_t end); + std::size_t findMinBin(const std::vector &wf, std::size_t start, std::size_t end) const; // Templated helper: finds the index of the maximum element in the range [start, end). template - std::size_t findMaxBin(const std::vector &wf, std::size_t start, std::size_t end); + std::size_t findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) const; // Templated helper: computes the median of a vector. template - T computeMedian(const std::vector &data); + T computeMedian(const std::vector &data) const; // Templated helper: logistic-fit extraction; fills par[4] with fit parameters. // Returns true if the fit converged. template - bool extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]); + bool extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) const; }; } // icarus::timing namespace diff --git a/icaruscode/Timing/timing_beam.fcl b/icaruscode/Timing/timing_beam.fcl index a68836ff5..b71d9065c 100644 --- a/icaruscode/Timing/timing_beam.fcl +++ b/icaruscode/Timing/timing_beam.fcl @@ -9,7 +9,8 @@ icarus_beam_signal_extractor: EWlabel: "daqPMT:EW" RWMlabel: "daqPMT:RWM" TriggerCorrectionLabel: "daqPMT:globtrg" - ADCThreshold: 200 + TimeExtractionMethod: "LogisticFit" + ADCThreshold: 500 BoardSetup: @local::icarus_V1730_setup DebugTrees: false SaveWaveforms: false From 9ae76939778d12a0abff0fad7fba8356c1fa9160 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Thu, 6 Mar 2025 22:29:43 -0600 Subject: [PATCH 05/20] add signal filter by consensus --- icaruscode/IcarusObj/PMTBeamSignal.h | 2 +- .../Timing/PMTBeamSignalsExtractor_module.cc | 198 ++++++++++++++---- 2 files changed, 155 insertions(+), 45 deletions(-) diff --git a/icaruscode/IcarusObj/PMTBeamSignal.h b/icaruscode/IcarusObj/PMTBeamSignal.h index d0fdfd0ff..9dbf33701 100644 --- a/icaruscode/IcarusObj/PMTBeamSignal.h +++ b/icaruscode/IcarusObj/PMTBeamSignal.h @@ -61,7 +61,7 @@ namespace icarus::timing double startTime = NoTime; /// Returns whether the time is valid. - bool isValid() const { return (sample != NoSample); } + bool isValid() const { return (startTime != NoTime); } }; } // namespace icarus::timing diff --git a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc index d79edb717..65f3808e8 100644 --- a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc +++ b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc @@ -26,6 +26,7 @@ #include "lardataobj/RawData/TriggerData.h" #include "TTree.h" +#include "TMath.h" #include #include @@ -81,9 +82,19 @@ namespace icarus::timing * The extraction is perfomed by `icarus::timing::PulseStartExtractor` by * specifying one of the available extraction methods (constant-fraction discrimation, * logitstic function fit) and an ADC threshold for the signal identification. - * * @see `icarus::timing::PulseStartExtractor` for details. * + * The extracted times are then corrected using the standard waveform time corrections + * based on the digitized trigger signals (`icarus::timing::PMTWaveformTimeCorrection`). + * After this step, times should match between crates of the same module apart from + * small differences in the fit results. + + * Unfortunately, this is not always the case: a crate might be completely missing a signal + * or seeing it distorted. While these issues are eventually corrected in the hardware, + * the redundancy due to the other crates is exploited to recover a value for the affected crate. + * All the valid (non-missing) signals are used to estimate the mean, median and standard + * deviation of the extracted times in each cryostat. Large outliers from the median + * (more than 2 &sigma away) are then replaced with the median itself. * * Output products * ------------------------- @@ -113,8 +124,9 @@ namespace icarus::timing * * `wfstart`: (uncorrected) waveform time start. * * `sample`: the sample number in the waveform at which the signal was found. * * `utime_abs`: uncorrected signal time [µs] - * * `time_abs`: corrected signal time [µs] - * * `time`: corrected signal time (relative to the trigger time) [µs] + * * `itime_abs`: intermediate (corrected) signal time [µs] + * * `time_abs`: corrected and adjusted signal time [µs] + * * `time`: corrected and adjusted signal time (relative to the trigger time) [µs] * * `nsize` (only if `SaveWaveforms` is set): number of samples in waveform * * `wf` (only if `SaveWaveforms` is set): full waveform. */ @@ -127,18 +139,24 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer // process waveforms void extractBeamSignalTime(art::Event &e, art::InputTag const &label); - // unpack the V1730 special channels settings in a useful way - static std::map extractBoardBySpecialChannel(std::vector const &setup); + // filter times based on consensus among crates + void filterBeamSignalsByCryostat(std::string const &l, std::vector const ×, std::string const &c); // associate times to PMT channels void associateBeamSignalsToChannels(art::InputTag const &label); // trigger-hardware timing correction - double getTriggerCorrection(int channel) const; + double getTriggerCorrection(unsigned int channel) const; // quick mapping conversions - std::string getDigitizerLabel(int channel) const; - std::string getCrate(int channel) const; + std::string getDigitizerLabel(unsigned int channel) const; + std::string getCrate(unsigned int channel) const; + + // unpack the V1730 special channels settings in a useful way + static std::map extractBoardBySpecialChannel(std::vector const &setup); + + // fill debug trees + void fillDebugTrees(); // Plugins should not be copied or assigned. PMTBeamSignalsExtractor(PMTBeamSignalsExtractor const &) = delete; @@ -193,14 +211,29 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer double m_time; double m_time_abs; double m_utime_abs; + double m_itime_abs; std::size_t m_nsize; std::vector m_wf; + /// Struct for storing data + struct SignalData { + unsigned int channel; + std::string diglabel; + std::string crate; + double wfstart; + double sample; + double utime_abs; + double itime_abs; + double time_abs; + double time; + std::vector wf; + }; + // prepare pointers for data products using BeamSignalCollection = std::vector; using BeamSignalCollectionPtr = std::unique_ptr; - std::map> fBeamSignals; + std::map> fBeamSignals; std::map fSignalCollection; }; @@ -251,6 +284,7 @@ void icarus::timing::PMTBeamSignalsExtractor::beginJob() tree->Branch("wfstart", &m_wfstart); tree->Branch("sample", &m_sample); tree->Branch("utime_abs", &m_utime_abs); + tree->Branch("itime_abs", &m_itime_abs); tree->Branch("time_abs", &m_time_abs); tree->Branch("time", &m_time); @@ -338,17 +372,19 @@ void icarus::timing::PMTBeamSignalsExtractor::produce(art::Event &e) extractBeamSignalTime(e, fRWMlabel); extractBeamSignalTime(e, fEWlabel); + // fix the cable swap for part of Run 2 right here!! + // see SBN-doc-34631 for details + if (gateType == sbn::triggerSource::BNB && m_run > 9704 && m_run < 11443) + std::swap(fBeamSignals[fRWMlabel.instance()], fBeamSignals[fEWlabel.instance()]); + + if (fDebugTrees) fillDebugTrees(); + // associating the proper RWM and EW time to each PMT channel // collections are vectors of 360 elements (one value for each channel) associateBeamSignalsToChannels(fRWMlabel); associateBeamSignalsToChannels(fEWlabel); // place data products in the stream - // fix the cable swap for part of Run 2 right here!! - // see SBN-doc-34631 for details - if (gateType == sbn::triggerSource::BNB && m_run > 9704 && m_run < 11443) - std::swap(fSignalCollection[fRWMlabel.instance()], fSignalCollection[fEWlabel.instance()]); - e.put(std::move(fSignalCollection[fRWMlabel.instance()]), "RWM"); e.put(std::move(fSignalCollection[fEWlabel.instance()]), "EW"); } @@ -360,54 +396,96 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & std::string const &l = label.instance(); auto const &waveforms = e.getProduct>(label); - m_n_channels = waveforms.size(); + int n_channels = waveforms.size(); - if (m_n_channels < 1) + if (n_channels < 1) mf::LogError("PMTBeamSignalsExtractor") << "Not found raw::OpDetWaveform with label '" << label.encode() << "'"; - else if (m_n_channels < 8) - mf::LogError("PMTBeamSignalsExtractor") << "Missing " << 8 - m_n_channels << " raw::OpDetWaveform with label '" + else if (n_channels < 8) + mf::LogError("PMTBeamSignalsExtractor") << "Missing " << 8 - n_channels << " raw::OpDetWaveform with label '" << label.encode() << "'"; m_wf.clear(); + // prepare to separe out east/west signals + std::vector east; + std::vector west; + // get the start sample of the signals, one instance per PMT crate - // use threshold to skip spikes from electric crosstalk see SBN-doc-34928, slides 4-5. - // if no signal is found, set both times to icarus::timing::NoTime - // so that `isValid()` in PMTBeamSignal will complain if someone checks it + // threshold is used to skip spikes from electric crosstalk see SBN-doc-34928, slides 4-5. + // if no signal is found, set time to icarus::timing::NoTime for (auto const &wave : waveforms) { detinfo::timescales::electronics_time tstart = util::quantities::points::microsecond{wave.TimeStamp()}; - m_channel = wave.ChannelNumber(); - m_wfstart = tstart.value(); - std::string crate = getCrate(m_channel); + unsigned int channel = wave.ChannelNumber(); + double wfstart = tstart.value(); + std::string crate = getCrate(channel); // if nothing is found, first sample is returned (0) - m_sample = fPulseStartExtractor.extractStart(wave.Waveform()); - if (m_sample < 1){ - m_sample = icarus::timing::NoSample; + double sample = fPulseStartExtractor.extractStart(wave.Waveform()); + if (sample < 1){ + sample = icarus::timing::NoSample; mf::LogTrace("PMTBeamSignalsExtractor") << "No " << label.encode() << " signal found in channel " << m_channel << " (crate " << crate << ")"; } - m_utime_abs = (m_sample != icarus::timing::NoSample) ? tstart.value() + fPMTsamplingTick * m_sample : icarus::timing::NoTime; - m_time_abs = (m_sample != icarus::timing::NoSample) ? m_utime_abs + getTriggerCorrection(m_channel) : icarus::timing::NoTime; - m_time = (m_sample != icarus::timing::NoSample) ? m_time_abs - ftriggerTime : icarus::timing::NoTime; + double utime_abs = (sample != icarus::timing::NoSample) ? tstart.value() + fPMTsamplingTick * sample : icarus::timing::NoTime; + double itime_abs = (sample != icarus::timing::NoSample) ? utime_abs + getTriggerCorrection(channel) : icarus::timing::NoTime; + double itime = (sample != icarus::timing::NoSample) ? itime_abs - ftriggerTime : icarus::timing::NoTime; - icarus::timing::PMTBeamSignal beamTime{m_channel, getDigitizerLabel(m_channel), crate, m_sample, m_time_abs, m_time}; + // store signal information, final time might be updated later + SignalData beamTime{channel, getDigitizerLabel(channel), crate, wfstart, sample, utime_abs, itime_abs, itime_abs, itime, + fSaveWaveforms ? wave.Waveform() : std::vector{}}; fBeamSignals[l].emplace(crate, std::move(beamTime)); - if (fDebugTrees) - { - if (fSaveWaveforms) - { - m_wf = wave.Waveform(); - m_nsize = m_wf.size(); - } - fOutTree[l]->Fill(); - } + // skip invalid times (signal completely missing) + if( sample == icarus::timing::NoSample ) continue; + + // save valid times in east/west separately to estimate consensus + // this will be used to computed median and RMS + if(crate[0] == 'E') east.push_back(itime_abs); + else if( crate[0] == 'W') west.push_back(itime_abs); + } + + // filter out and correct missing signals or large outliers + // using the consensus from the majority of crates in the same cryostat + filterBeamSignalsByCryostat(l, east, "E"); + filterBeamSignalsByCryostat(l, west, "W"); + +} + +// ----------------------------------------------------------------------------- + +void icarus::timing::PMTBeamSignalsExtractor::filterBeamSignalsByCryostat( + std::string const &l, std::vector const ×, std::string const &c) +{ + + // if there is only one valid time in this cryostat, + // do no attempt any filtering/adjustments + if (times.size() < 2) return; + + // compute stddev and median among valid signals (consensus) + double median = TMath::Median(times.size(), times.data()); + double stddev = TMath::RMS(times.size(), times.data()); + + for (auto const& [crate, signal] : fBeamSignals[l]){ + + // skip the other cryostat + if( crate[0] != c) continue; + double t = signal.itime_abs; + + // if the current signal is more than 2 standard deviations away, substitute it with the median + // this affects both missing signals (whose value is currently icarus::timing::NoTime) + // and distorted signal for which the time extraction is skewed + if ( std::fabs(t-median) > 2*stddev ) + { + fBeamSignals[l][crate].time_abs = median; + fBeamSignals[l][crate].time = median - ftriggerTime; + } + } + } // ----------------------------------------------------------------------------- @@ -431,7 +509,7 @@ std::map icarus::timing::PMTBeamSignalsExtractor::extractBoard // ----------------------------------------------------------------------------- -std::string icarus::timing::PMTBeamSignalsExtractor::getDigitizerLabel(int channel) const +std::string icarus::timing::PMTBeamSignalsExtractor::getDigitizerLabel(unsigned int channel) const { // get the board name, convert to digitizer_label @@ -450,7 +528,7 @@ std::string icarus::timing::PMTBeamSignalsExtractor::getDigitizerLabel(int chann // ----------------------------------------------------------------------------- -std::string icarus::timing::PMTBeamSignalsExtractor::getCrate(int channel) const +std::string icarus::timing::PMTBeamSignalsExtractor::getCrate(unsigned int channel) const { std::string digitizer_label = getDigitizerLabel(channel); @@ -459,7 +537,7 @@ std::string icarus::timing::PMTBeamSignalsExtractor::getCrate(int channel) const // ----------------------------------------------------------------------------- -double icarus::timing::PMTBeamSignalsExtractor::getTriggerCorrection(int channel) const +double icarus::timing::PMTBeamSignalsExtractor::getTriggerCorrection(unsigned int channel) const { std::string digitizer_label = getDigitizerLabel(channel); @@ -487,6 +565,10 @@ void icarus::timing::PMTBeamSignalsExtractor::associateBeamSignalsToChannels(art for (auto const &signal : fBeamSignals[l]) { + // convert SignalData into icarus::timing::PMTBeamSignal + icarus::timing::PMTBeamSignal beamTime{ signal.second.channel, signal.second.diglabel, signal.first, + signal.second.sample, signal.second.time_abs, signal.second.time}; + // build the PMT digitizer labels that live in this crate // then convert it into fragment id std::array const letters{"-A", "-B", "-C"}; @@ -505,11 +587,39 @@ void icarus::timing::PMTBeamSignalsExtractor::associateBeamSignalsToChannels(art std::size_t channel = pmtinfo.channelID; // collections are resized to fNPMTChannels // always room in the collection (channel -> vector index) - fSignalCollection[l]->at(channel) = signal.second; + fSignalCollection[l]->at(channel) = beamTime; } // for each channel } // for each board } // for each crate } +// ----------------------------------------------------------------------------- + +void icarus::timing::PMTBeamSignalsExtractor::fillDebugTrees() +{ + for (const auto& [label, signals] : fBeamSignals) + { + m_n_channels = signals.size(); + for (const auto& [crate, signal] : signals) + { + m_channel = signal.channel; + m_wfstart = signal.wfstart; + m_sample = signal.sample; + m_utime_abs = signal.utime_abs; + m_itime_abs = signal.itime_abs; + m_time_abs = signal.time_abs; + m_time = signal.time; + + if (fSaveWaveforms) { + m_nsize = signal.wf.size(); + m_wf = signal.wf; + } + + fOutTree[label]->Fill(); + } + } +} + DEFINE_ART_MODULE(icarus::timing::PMTBeamSignalsExtractor) + From b7e60bcec532d926d77cdaa8f7ba19a68d44f0e4 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Fri, 7 Mar 2025 15:45:33 -0600 Subject: [PATCH 06/20] update std computation --- icaruscode/Timing/PMTBeamSignalsExtractor_module.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc index 65f3808e8..73978214f 100644 --- a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc +++ b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc @@ -141,6 +141,7 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer // filter times based on consensus among crates void filterBeamSignalsByCryostat(std::string const &l, std::vector const ×, std::string const &c); + double stdDev(std::vector data, double mean); // associate times to PMT channels void associateBeamSignalsToChannels(art::InputTag const &label); @@ -458,6 +459,13 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & // ----------------------------------------------------------------------------- +double icarus::timing::PMTBeamSignalsExtractor::stdDev(std::vector data, double mean) +{ + double sum = 0; + for(auto d : data) sum += (d-mean)*(d-mean); + return TMath::Sqrt(sum/data.size()); +} + void icarus::timing::PMTBeamSignalsExtractor::filterBeamSignalsByCryostat( std::string const &l, std::vector const ×, std::string const &c) { @@ -468,7 +476,8 @@ void icarus::timing::PMTBeamSignalsExtractor::filterBeamSignalsByCryostat( // compute stddev and median among valid signals (consensus) double median = TMath::Median(times.size(), times.data()); - double stddev = TMath::RMS(times.size(), times.data()); + double mean = TMath::Mean(times.size(), times.data()); + double stddev = stdDev(times, mean); for (auto const& [crate, signal] : fBeamSignals[l]){ @@ -484,6 +493,7 @@ void icarus::timing::PMTBeamSignalsExtractor::filterBeamSignalsByCryostat( fBeamSignals[l][crate].time_abs = median; fBeamSignals[l][crate].time = median - ftriggerTime; } + } } From c11b76b1760cf79ba51a074b403d857246b502a7 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Thu, 5 Jun 2025 11:13:18 -0500 Subject: [PATCH 07/20] add flag for consensun filtering --- .../Timing/PMTBeamSignalsExtractor_module.cc | 20 ++++++++++++------- icaruscode/Timing/timing_beam.fcl | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc index 73978214f..fb2815cf3 100644 --- a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc +++ b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc @@ -71,6 +71,8 @@ namespace icarus::timing * @see icarus::timing::PulseStartExtractor for details and options. * * `ADCThreshold` (int): detection threshold to avoid cross-talk * noise if one signal is missing from its waveform. + * * `ApplyConsensusFiltering` (bool): if true, filter bad or missing times using + * the consensus among other crates in the same cryostat. * * `DebugTrees` (bool): flag to produce plain ROOT trees for debugging. * * `SaveWaveforms` (bool): flag to save full waveforms in the debug trees. * @@ -188,6 +190,8 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer icarus::timing::PulseStartExtractor const fPulseStartExtractor; /// Special channel to board association in a map std::map const fBoardBySpecialChannel; + /// Apply filtering for bad/missing signals + bool const fApplyConsensusFiltering; /// PMT sample duration [µs] static constexpr double fPMTsamplingTick = 0.002; @@ -251,7 +255,8 @@ icarus::timing::PMTBeamSignalsExtractor::PMTBeamSignalsExtractor(fhicl::Paramete fTriggerCorrectionLabel(pset.get("TriggerCorrectionLabel")), fPulseStartExtractor{ icarus::timing::stringToExtractionMethod.at(pset.get("TimeExtractionMethod")), pset.get("ADCThreshold")}, - fBoardBySpecialChannel(extractBoardBySpecialChannel(pset.get>("BoardSetup"))) + fBoardBySpecialChannel(extractBoardBySpecialChannel(pset.get>("BoardSetup"))), + fApplyConsensusFiltering(pset.get("ApplyConsensusFiltering")) { // Call appropriate consumes<>() functions here. consumes>(fEWlabel); @@ -452,9 +457,10 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & // filter out and correct missing signals or large outliers // using the consensus from the majority of crates in the same cryostat - filterBeamSignalsByCryostat(l, east, "E"); - filterBeamSignalsByCryostat(l, west, "W"); - + if( fApplyConsensusFiltering ){ + filterBeamSignalsByCryostat(l, east, "E"); + filterBeamSignalsByCryostat(l, west, "W"); + } } // ----------------------------------------------------------------------------- @@ -616,9 +622,9 @@ void icarus::timing::PMTBeamSignalsExtractor::fillDebugTrees() m_channel = signal.channel; m_wfstart = signal.wfstart; m_sample = signal.sample; - m_utime_abs = signal.utime_abs; - m_itime_abs = signal.itime_abs; - m_time_abs = signal.time_abs; + m_utime_abs = signal.utime_abs; //uncorrected + m_itime_abs = signal.itime_abs; //pre-filtering + m_time_abs = signal.time_abs; //post-filtering m_time = signal.time; if (fSaveWaveforms) { diff --git a/icaruscode/Timing/timing_beam.fcl b/icaruscode/Timing/timing_beam.fcl index b71d9065c..0451d777c 100644 --- a/icaruscode/Timing/timing_beam.fcl +++ b/icaruscode/Timing/timing_beam.fcl @@ -10,6 +10,7 @@ icarus_beam_signal_extractor: RWMlabel: "daqPMT:RWM" TriggerCorrectionLabel: "daqPMT:globtrg" TimeExtractionMethod: "LogisticFit" + ApplyConsensusFiltering: true ADCThreshold: 500 BoardSetup: @local::icarus_V1730_setup DebugTrees: false From 4defe4abc2ef4f97cedb05a4ac1b9a324d16878e Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Fri, 20 Jun 2025 16:43:16 -0500 Subject: [PATCH 08/20] flatten debug tree structure --- .../Timing/PMTBeamSignalsExtractor_module.cc | 234 ++++++++++++------ icaruscode/Timing/timing_beam.fcl | 2 +- 2 files changed, 154 insertions(+), 82 deletions(-) diff --git a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc index fb2815cf3..f731f26cd 100644 --- a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc +++ b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc @@ -73,8 +73,8 @@ namespace icarus::timing * noise if one signal is missing from its waveform. * * `ApplyConsensusFiltering` (bool): if true, filter bad or missing times using * the consensus among other crates in the same cryostat. - * * `DebugTrees` (bool): flag to produce plain ROOT trees for debugging. - * * `SaveWaveforms` (bool): flag to save full waveforms in the debug trees. + * * `DebugTree` (bool): flag to produce plain ROOT tree for debugging. + * * `SaveWaveforms` (bool): flag to save full waveforms in the debug tree. * * Signal timing extraction * ------------------------- @@ -102,7 +102,7 @@ namespace icarus::timing * ------------------------- * * This modules produces two `std::vector` - * with 360 elements each, representing the relevent RWM or EW time for + * with 360 elements each, representing the relevant RWM or EW time for * the corresponding PMT channel. When the discrimination algorithm decides * the "peak" on a waveform to be noise, the corresponding entries are placed * into the vector with an invalid value (`isValid()` returns `false`, @@ -114,23 +114,33 @@ namespace icarus::timing * Debugging tree * --------------- * - * There are two debugging trees, enabled by `DebugTrees`, one for the Early Warning (EW) signals, - * named after the instance name of `EWlabel`, and one for the Resistive Wall Monitor (RWM) signals, - * named after the instance name of `RWMlabel`. Each entry in the tree represents a single signal - * in an event (thus, typically for ICARUS there are 8 entries per event). + * The debugging tree, enabled by `DebugTree`, contains both Early Warning (EW) signals, + * named after the instance name of `EWlabel`, and Resistive Wall Monitor (RWM) signals, + * named after the instance name of `RWMlabel`. Each entry in the tree represents what a + * PMT crate sees in an event (thus, typically for ICARUS there are 8 entries per event). * The content of an entry includes: - * * `run`, `event`: identifier of the event. + * * `run`, `event`: identifier of the run/event. * * `timestamp`: timestamp of the event (in UTC), truncated at the second. - * * `n_channels`: the number of special signal channels in the event. - * * `channel`: the special channel nummber assigned to this signal. - * * `wfstart`: (uncorrected) waveform time start. - * * `sample`: the sample number in the waveform at which the signal was found. - * * `utime_abs`: uncorrected signal time [µs] - * * `itime_abs`: intermediate (corrected) signal time [µs] - * * `time_abs`: corrected and adjusted signal time [µs] - * * `time`: corrected and adjusted signal time (relative to the trigger time) [µs] - * * `nsize` (only if `SaveWaveforms` is set): number of samples in waveform - * * `wf` (only if `SaveWaveforms` is set): full waveform. + * * `n_crates`: number of crates with special signals in the event. + * * `crate`: string identifier for the PMT crate (eg, EE-BOT). + * * `rmw_channel`: special RWM channel number assigned to this signal. + * * `rwm_wfstart`: (uncorrected) RWM waveform time start. + * * `rwm_sample`: the sample number in the RWM waveform at which the signal was found. + * * `rwm_utime_abs`: uncorrected RWM signal time [µs]. + * * `rwm_itime_abs`: intermediate (corrected) RWM signal time [µs]. + * * `rwm_time_abs`: corrected and adjusted RWM signal time [µs]. + * * `rwm_time`: corrected and adjusted RWM signal time (relative to the trigger time) [µs]. + * * `rwm_nsize` (only if `SaveWaveforms` is set): number of samples in RWM waveform. + * * `rwm_wf` (only if `SaveWaveforms` is set): full RWM waveform. + * * `ew_channel`: special EW channel number assigned to this signal. + * * `ew_wfstart`: (uncorrected) EW waveform time start. + * * `ew_sample`: the sample number in the EW waveform at which the signal was found. + * * `ew_utime_abs`: uncorrected EW signal time [µs]. + * * `ew_itime_abs`: intermediate (corrected) EW signal time [µs]. + * * `ew_time_abs`: corrected and adjusted EW signal time [µs]. + * * `ew_time`: corrected and adjusted EW signal time (relative to the trigger time) [µs]. + * * `ew_nsize` (only if `SaveWaveforms` is set): number of samples in EW waveform. + * * `ew_wf` (only if `SaveWaveforms` is set): full EW waveform. */ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer @@ -159,7 +169,7 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer static std::map extractBoardBySpecialChannel(std::vector const &setup); // fill debug trees - void fillDebugTrees(); + void fillDebugTree(); // Plugins should not be copied or assigned. PMTBeamSignalsExtractor(PMTBeamSignalsExtractor const &) = delete; @@ -174,8 +184,8 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer private: /// Channel mappping icarusDB::IICARUSChannelMap const &fChannelMap; - /// Save plain ROOT TTrees for debugging - bool const fDebugTrees; + /// Save plain ROOT TTree for debugging + bool const fDebugTree; /// Save raw waveforms in debug TTrees bool const fSaveWaveforms; /// Trigger instance label @@ -203,22 +213,33 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer double ftriggerTime; // output TTrees - std::map fOutTree; + TTree* fOutTree; // event info int m_run; int m_event; int m_timestamp; - // special signal info - int m_n_channels; - unsigned int m_channel; - double m_wfstart; - double m_sample; - double m_time; - double m_time_abs; - double m_utime_abs; - double m_itime_abs; - std::size_t m_nsize; - std::vector m_wf; + int m_n_crates; + std::string m_crate; + // rwm signal info + unsigned int m_rwm_channel; + double m_rwm_wfstart; + double m_rwm_sample; + double m_rwm_time; + double m_rwm_time_abs; + double m_rwm_utime_abs; + double m_rwm_itime_abs; + std::size_t m_rwm_nsize; + std::vector m_rwm_wf; + // ew signal info + unsigned int m_ew_channel; + double m_ew_wfstart; + double m_ew_sample; + double m_ew_time; + double m_ew_time_abs; + double m_ew_utime_abs; + double m_ew_itime_abs; + std::size_t m_ew_nsize; + std::vector m_ew_wf; /// Struct for storing data struct SignalData { @@ -247,7 +268,7 @@ class icarus::timing::PMTBeamSignalsExtractor : public art::EDProducer icarus::timing::PMTBeamSignalsExtractor::PMTBeamSignalsExtractor(fhicl::ParameterSet const &pset) : EDProducer{pset}, fChannelMap(*(art::ServiceHandle{})), - fDebugTrees(pset.get("DebugTrees")), + fDebugTree(pset.get("DebugTree")), fSaveWaveforms(pset.get("SaveWaveforms")), fTriggerLabel(pset.get("TriggerLabel")), fRWMlabel(pset.get("RWMlabel")), @@ -271,37 +292,42 @@ icarus::timing::PMTBeamSignalsExtractor::PMTBeamSignalsExtractor(fhicl::Paramete void icarus::timing::PMTBeamSignalsExtractor::beginJob() { - // prepare outupt TTrees if requested - if (!fDebugTrees) + // prepare outupt TTree if requested + if (!fDebugTree) return; art::ServiceHandle tfs; - std::array const labels{fRWMlabel.instance(), fEWlabel.instance()}; - for (auto l : labels) + std::string name = fRWMlabel.instance() + "_" + fEWlabel.instance() + "_tree"; + std::string desc = fRWMlabel.instance() + " " + fEWlabel.instance() + " info"; + fOutTree = tfs->make(name.c_str(), desc.c_str()); + fOutTree->Branch("run", &m_run); + fOutTree->Branch("event", &m_event); + fOutTree->Branch("timestamp", &m_timestamp); + fOutTree->Branch("n_crates", &m_n_crates); + fOutTree->Branch("crate", &m_crate); + fOutTree->Branch("rwm_channel", &m_rwm_channel); + fOutTree->Branch("rwm_wfstart", &m_rwm_wfstart); + fOutTree->Branch("rwm_sample", &m_rwm_sample); + fOutTree->Branch("rwm_utime_abs", &m_rwm_utime_abs); + fOutTree->Branch("rwm_itime_abs", &m_rwm_itime_abs); + fOutTree->Branch("rwm_time_abs", &m_rwm_time_abs); + fOutTree->Branch("rwm_time", &m_rwm_time); + fOutTree->Branch("ew_channel", &m_ew_channel); + fOutTree->Branch("ew_wfstart", &m_ew_wfstart); + fOutTree->Branch("ew_sample", &m_ew_sample); + fOutTree->Branch("ew_utime_abs", &m_ew_utime_abs); + fOutTree->Branch("ew_itime_abs", &m_ew_itime_abs); + fOutTree->Branch("ew_time_abs", &m_ew_time_abs); + fOutTree->Branch("ew_time", &m_ew_time); + + // add std::vector with full waveforms + // this can quickly make the TTrees quite heavy + if (fSaveWaveforms) { - std::string name = l + "tree"; - std::string desc = l + " info"; - TTree *tree = tfs->make(name.c_str(), desc.c_str()); - tree->Branch("run", &m_run); - tree->Branch("event", &m_event); - tree->Branch("timestamp", &m_timestamp); - tree->Branch("n_channels", &m_n_channels); - tree->Branch("channel", &m_channel); - tree->Branch("wfstart", &m_wfstart); - tree->Branch("sample", &m_sample); - tree->Branch("utime_abs", &m_utime_abs); - tree->Branch("itime_abs", &m_itime_abs); - tree->Branch("time_abs", &m_time_abs); - tree->Branch("time", &m_time); - - // add std::vector with full waveforms - // this can quickly make the TTrees quite heavy - if (fSaveWaveforms) - { - tree->Branch("nsize", &m_nsize); - tree->Branch("wf", &m_wf); - } - fOutTree[l] = tree; + fOutTree->Branch("rwm_nsize", &m_rwm_nsize); + fOutTree->Branch("rwm_wf", &m_rwm_wf); + fOutTree->Branch("ew_nsize", &m_ew_nsize); + fOutTree->Branch("ew_wf", &m_ew_wf); } } @@ -383,7 +409,7 @@ void icarus::timing::PMTBeamSignalsExtractor::produce(art::Event &e) if (gateType == sbn::triggerSource::BNB && m_run > 9704 && m_run < 11443) std::swap(fBeamSignals[fRWMlabel.instance()], fBeamSignals[fEWlabel.instance()]); - if (fDebugTrees) fillDebugTrees(); + if (fDebugTree) fillDebugTree(); // associating the proper RWM and EW time to each PMT channel // collections are vectors of 360 elements (one value for each channel) @@ -410,7 +436,8 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & mf::LogError("PMTBeamSignalsExtractor") << "Missing " << 8 - n_channels << " raw::OpDetWaveform with label '" << label.encode() << "'"; - m_wf.clear(); + m_rwm_wf.clear(); + m_ew_wf.clear(); // prepare to separe out east/west signals std::vector east; @@ -432,7 +459,7 @@ void icarus::timing::PMTBeamSignalsExtractor::extractBeamSignalTime(art::Event & double sample = fPulseStartExtractor.extractStart(wave.Waveform()); if (sample < 1){ sample = icarus::timing::NoSample; - mf::LogTrace("PMTBeamSignalsExtractor") << "No " << label.encode() << " signal found in channel " << m_channel + mf::LogTrace("PMTBeamSignalsExtractor") << "No " << label.encode() << " signal found in channel " << channel << " (crate " << crate << ")"; } @@ -612,29 +639,74 @@ void icarus::timing::PMTBeamSignalsExtractor::associateBeamSignalsToChannels(art // ----------------------------------------------------------------------------- -void icarus::timing::PMTBeamSignalsExtractor::fillDebugTrees() +void icarus::timing::PMTBeamSignalsExtractor::fillDebugTree() { - for (const auto& [label, signals] : fBeamSignals) + + auto RWMSignals = fBeamSignals[fRWMlabel.instance()]; + auto EWSignals = fBeamSignals[fEWlabel.instance()]; + + std::unordered_set crates; + for (const auto& [crate, _] : RWMSignals ) crates.insert(crate); + for (const auto& [crate, _] : EWSignals ) crates.insert(crate); + + m_n_crates = crates.size(); + + for (const auto& crate : crates) { - m_n_channels = signals.size(); - for (const auto& [crate, signal] : signals) + auto itRWM = RWMSignals.find(crate); + auto itEW = EWSignals.find(crate); + m_crate = crate; + + if (itRWM != RWMSignals.end()) { - m_channel = signal.channel; - m_wfstart = signal.wfstart; - m_sample = signal.sample; - m_utime_abs = signal.utime_abs; //uncorrected - m_itime_abs = signal.itime_abs; //pre-filtering - m_time_abs = signal.time_abs; //post-filtering - m_time = signal.time; - - if (fSaveWaveforms) { - m_nsize = signal.wf.size(); - m_wf = signal.wf; + m_rwm_channel = itRWM->second.channel; + m_rwm_wfstart = itRWM->second.wfstart; + m_rwm_sample = itRWM->second.sample; + m_rwm_utime_abs = itRWM->second.utime_abs; //uncorrected + m_rwm_itime_abs = itRWM->second.itime_abs; //pre-filtering + m_rwm_time_abs = itRWM->second.time_abs; //post-filtering + m_rwm_time = itRWM->second.time; + if (fSaveWaveforms) + { + m_rwm_nsize = itRWM->second.wf.size(); + m_rwm_wf = itRWM->second.wf; } + } else { + m_rwm_channel = 0; + m_rwm_wfstart = icarus::timing::NoTime; + m_rwm_sample = icarus::timing::NoTime; + m_rwm_utime_abs = icarus::timing::NoTime; + m_rwm_itime_abs = icarus::timing::NoTime; + m_rwm_time_abs = icarus::timing::NoTime; + m_rwm_time = icarus::timing::NoTime; + } - fOutTree[label]->Fill(); + if (itEW != EWSignals.end()) + { + m_ew_channel = itEW->second.channel; + m_ew_wfstart = itEW->second.wfstart; + m_ew_sample = itEW->second.sample; + m_ew_utime_abs = itEW->second.utime_abs; //uncorrected + m_ew_itime_abs = itEW->second.itime_abs; //pre-filtering + m_ew_time_abs = itEW->second.time_abs; //post-filtering + m_ew_time = itEW->second.time; + if (fSaveWaveforms) + { + m_ew_nsize = itEW->second.wf.size(); + m_ew_wf = itEW->second.wf; + } + } else { + m_ew_channel = 0; + m_ew_wfstart = icarus::timing::NoTime; + m_ew_sample = icarus::timing::NoTime; + m_ew_utime_abs = icarus::timing::NoTime; + m_ew_itime_abs = icarus::timing::NoTime; + m_ew_time_abs = icarus::timing::NoTime; + m_ew_time = icarus::timing::NoTime; } - } + + fOutTree->Fill(); + } } DEFINE_ART_MODULE(icarus::timing::PMTBeamSignalsExtractor) diff --git a/icaruscode/Timing/timing_beam.fcl b/icaruscode/Timing/timing_beam.fcl index 0451d777c..ade1c4455 100644 --- a/icaruscode/Timing/timing_beam.fcl +++ b/icaruscode/Timing/timing_beam.fcl @@ -13,7 +13,7 @@ icarus_beam_signal_extractor: ApplyConsensusFiltering: true ADCThreshold: 500 BoardSetup: @local::icarus_V1730_setup - DebugTrees: false + DebugTree: false SaveWaveforms: false } From 84817272c57cbf85c58225c6b364e606152253d7 Mon Sep 17 00:00:00 2001 From: Saurav Saha Date: Wed, 9 Jul 2025 14:07:38 -0500 Subject: [PATCH 09/20] Updated PulseStartExtractor.cc and .h with new logic --- .../Timing/Tools/PulseStartExtractor.cc | 118 +++++++++++++++++- icaruscode/Timing/Tools/PulseStartExtractor.h | 12 +- 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.cc b/icaruscode/Timing/Tools/PulseStartExtractor.cc index c7212861c..111f9cd8c 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.cc +++ b/icaruscode/Timing/Tools/PulseStartExtractor.cc @@ -78,6 +78,15 @@ namespace icarus::timing { std::cout << "Sigmoid fit did not converge; falling back to constant-fraction discrimation." << std::endl; } +// ***** MODOFICATIONS ***** // + +if (fMethod == INTERPOLATED_CF) { + return extractStartInterpolated(wf, 0.2, 2.0); //above 20% fraction (threshold), 2 ns sampling (this is the t0 -t1, t2-t1...fixing it) + + std::cout << "Using interpolated CF method..." << std::endl; + + } + // Defaulting to constant-fraction discrimation return static_cast(cf_sample); } @@ -190,6 +199,104 @@ namespace icarus::timing { } +// ***** MODIFICATIONS ***** // + +template +double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, double fraction, double dt_ns) const { + if (wf.size() < 2) return 0.0; + + double baseline = computeMedian(wf); + auto minIt = std::min_element(wf.begin(), wf.end()); + std::size_t minIndex = std::distance(wf.begin(), minIt); + double peak = wf[minIndex]; + + double threshold = baseline + fraction * (peak - baseline); + + std::size_t maxbin = (minIndex >= 20) ? (minIndex - 20) : 0; + + for (std::size_t i = maxbin; i + 1 < minIndex; ++i) { + if (wf[i] > threshold && wf[i + 1] <= threshold) { + // Interpolate between sample i and i+1 + double y1 = wf[i]; + double y2 = wf[i + 1]; + double frac = (y1 - threshold) / (y1 - y2); // fractional distance + return static_cast(i) + frac; // sample index (float) + } + } + + return 0.0; +} + +/* +template +double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, double fraction, double dt_ns) const { + if (wf.size() < 2) return 0.0; + + double baseline = computeMedian(wf); + auto minIt = std::min_element(wf.begin(), wf.end()); + std::size_t minIndex = std::distance(wf.begin(), minIt); + double peak = wf[minIndex]; + double threshold = baseline + fraction * (peak - baseline); + + std::size_t prebin = 0, postbin = 0; + std::size_t start = (minIndex >= 20) ? (minIndex - 20) : 0; + + for (std::size_t bin = start; bin < minIndex; ++bin) { + if (wf[bin] > threshold && wf[bin + 1] <= threshold) { + prebin = bin; + postbin = bin + 1; + break; + } + } + + if (prebin == postbin) return 0.0; // no crossing found + + // Linear interpolation + double y1 = wf[prebin]; + double y2 = wf[postbin]; + double m = (y2 - y1); // ADC diff + double frac = (threshold - y1) / m; // fractional position between prebin and postbin + + double interpolated_sample = static_cast(prebin) + frac; + return interpolated_sample * dt_ns; // time in ns + } +*/ +/* +template +double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, double fraction) const { + if (wf.size() < 2) return 0.0; + + double baseline = computeMedian(wf); + auto minIt = std::min_element(wf.begin(), wf.end()); + std::size_t minIndex = std::distance(wf.begin(), minIt); + double peak = wf[minIndex]; + double threshold = baseline + fraction * (peak - baseline); + + std::size_t start = (minIndex >= 20) ? (minIndex - 20) : 0; + std::size_t prebin = 0, postbin = 0; + bool found = false; + + for (std::size_t bin = start; bin < minIndex; ++bin) { + if (wf[bin] > threshold && wf[bin + 1] <= threshold) { + prebin = bin; + postbin = bin + 1; + found = true; + break; + } + } + + if (!found) return 0.0; + + double y1 = wf[prebin], y2 = wf[postbin]; + double m = y2 - y1; + double x1 = static_cast(prebin); + double inter = x1 + (threshold - y1) / m; + + return inter; // This is the sample index! +} +*/ + +// template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; @@ -215,4 +322,13 @@ namespace icarus::timing { template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; -} // icarus::timing namespace \ No newline at end of file +// ***** MODIFICATIONS ***** // + +template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; +template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; +template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; +template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; + + + +} // icarus::timing namespace diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.h b/icaruscode/Timing/Tools/PulseStartExtractor.h index cb38fa910..32252b78f 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.h +++ b/icaruscode/Timing/Tools/PulseStartExtractor.h @@ -27,13 +27,15 @@ namespace icarus::timing { enum ExtractionMethod { CONSTANT_FRACTION, - LOGISTIC_FIT + LOGISTIC_FIT, + INTERPOLATED_CF }; // map for string to enum conversion std::map const stringToExtractionMethod = { { "LogisticFit", LOGISTIC_FIT }, - { "ConstantFraction", CONSTANT_FRACTION } + { "ConstantFraction", CONSTANT_FRACTION }, +{ "InterpolatedCF", INTERPOLATED_CF } }; /** @@ -142,7 +144,11 @@ namespace icarus::timing { // Returns true if the fit converged. template bool extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) const; - }; + +template +double extractStartInterpolated(const std::vector& wf, double fraction, double dt_ns) const; + + }; } // icarus::timing namespace From b5e393def71ccc19a869d237526f1a26cb3fe7c2 Mon Sep 17 00:00:00 2001 From: Saurav Saha Date: Wed, 9 Jul 2025 14:10:14 -0500 Subject: [PATCH 10/20] Update timing_beam.fcl with revised configuration --- icaruscode/Timing/timing_beam.fcl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/icaruscode/Timing/timing_beam.fcl b/icaruscode/Timing/timing_beam.fcl index ade1c4455..3ccb2e34d 100644 --- a/icaruscode/Timing/timing_beam.fcl +++ b/icaruscode/Timing/timing_beam.fcl @@ -9,11 +9,13 @@ icarus_beam_signal_extractor: EWlabel: "daqPMT:EW" RWMlabel: "daqPMT:RWM" TriggerCorrectionLabel: "daqPMT:globtrg" - TimeExtractionMethod: "LogisticFit" + #TimeExtractionMethod: "InterpolatedCF" + #TimeExtractionMethod: "LogisticFit" + TimeExtractionMethod: "ConstantFraction" ApplyConsensusFiltering: true ADCThreshold: 500 BoardSetup: @local::icarus_V1730_setup - DebugTree: false + DebugTree: true SaveWaveforms: false } From 7a208ad99d6c4cfa998a14ac41a3ff3fbe09f218 Mon Sep 17 00:00:00 2001 From: Saurav Saha Date: Thu, 10 Jul 2025 12:13:05 -0500 Subject: [PATCH 11/20] Further edits to PulseStartExtractor.cc --- .../Timing/Tools/PulseStartExtractor.cc | 70 ------------------- 1 file changed, 70 deletions(-) diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.cc b/icaruscode/Timing/Tools/PulseStartExtractor.cc index 111f9cd8c..5ab39f785 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.cc +++ b/icaruscode/Timing/Tools/PulseStartExtractor.cc @@ -226,77 +226,7 @@ double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, d return 0.0; } - -/* -template -double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, double fraction, double dt_ns) const { - if (wf.size() < 2) return 0.0; - - double baseline = computeMedian(wf); - auto minIt = std::min_element(wf.begin(), wf.end()); - std::size_t minIndex = std::distance(wf.begin(), minIt); - double peak = wf[minIndex]; - double threshold = baseline + fraction * (peak - baseline); - - std::size_t prebin = 0, postbin = 0; - std::size_t start = (minIndex >= 20) ? (minIndex - 20) : 0; - - for (std::size_t bin = start; bin < minIndex; ++bin) { - if (wf[bin] > threshold && wf[bin + 1] <= threshold) { - prebin = bin; - postbin = bin + 1; - break; - } - } - - if (prebin == postbin) return 0.0; // no crossing found - - // Linear interpolation - double y1 = wf[prebin]; - double y2 = wf[postbin]; - double m = (y2 - y1); // ADC diff - double frac = (threshold - y1) / m; // fractional position between prebin and postbin - double interpolated_sample = static_cast(prebin) + frac; - return interpolated_sample * dt_ns; // time in ns - } -*/ -/* -template -double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, double fraction) const { - if (wf.size() < 2) return 0.0; - - double baseline = computeMedian(wf); - auto minIt = std::min_element(wf.begin(), wf.end()); - std::size_t minIndex = std::distance(wf.begin(), minIt); - double peak = wf[minIndex]; - double threshold = baseline + fraction * (peak - baseline); - - std::size_t start = (minIndex >= 20) ? (minIndex - 20) : 0; - std::size_t prebin = 0, postbin = 0; - bool found = false; - - for (std::size_t bin = start; bin < minIndex; ++bin) { - if (wf[bin] > threshold && wf[bin + 1] <= threshold) { - prebin = bin; - postbin = bin + 1; - found = true; - break; - } - } - - if (!found) return 0.0; - - double y1 = wf[prebin], y2 = wf[postbin]; - double m = y2 - y1; - double x1 = static_cast(prebin); - double inter = x1 + (threshold - y1) / m; - - return inter; // This is the sample index! -} -*/ - -// template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; From 9d3270ed958b75f60bc3bf4c51063a537caa6b3d Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Thu, 10 Jul 2025 13:07:26 -0500 Subject: [PATCH 12/20] update default method in decoded --- icaruscode/Decode/DaqDecoderICARUSPMT_module.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc b/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc index 13b37701c..c1412f641 100644 --- a/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc +++ b/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc @@ -714,7 +714,7 @@ class icarus::DaqDecoderICARUSPMT: public art::EDProducer { fhicl::Atom PulseStartExtractionMethod { Name("PulseStartExtractionMethod"), Comment("pulse start extraction method for the time corrections"), - "LogisticFit" + "InterpolatedCF" }; fhicl::Atom PulseStartExtractionThreshold { From 96088a27dbcbe65d330c3569bc08451669bca9e7 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Mon, 2 Feb 2026 09:01:42 -0600 Subject: [PATCH 13/20] add peak time --- icaruscode/PMT/OpReco/ICARUSFlashAssAna_module.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/icaruscode/PMT/OpReco/ICARUSFlashAssAna_module.cc b/icaruscode/PMT/OpReco/ICARUSFlashAssAna_module.cc index acd96a95e..9951ddf02 100644 --- a/icaruscode/PMT/OpReco/ICARUSFlashAssAna_module.cc +++ b/icaruscode/PMT/OpReco/ICARUSFlashAssAna_module.cc @@ -141,6 +141,7 @@ class opana::ICARUSFlashAssAna : public art::EDAnalyzer float &sum_pe_left, float &sum_pe_right, std::vector &pmt_start_time, std::vector &pmt_rise_time, + std::vector &pmt_peak_time, std::vector &pmt_start_rwm_time, std::vector &pmt_pe, std::vector &pmt_amplitude, @@ -216,6 +217,7 @@ class opana::ICARUSFlashAssAna : public art::EDAnalyzer float m_flash_width_z; std::vector m_pmt_start_time; std::vector m_pmt_rise_time; + std::vector m_pmt_peak_time; std::vector m_pmt_start_time_rwm; std::vector m_pmt_pe; std::vector m_pmt_amplitude; @@ -405,8 +407,10 @@ void opana::ICARUSFlashAssAna::beginJob() ttree->Branch("pmt_x", &m_pmt_x); ttree->Branch("pmt_y", &m_pmt_y); ttree->Branch("pmt_z", &m_pmt_z); - ttree->Branch("time_pmt", &m_pmt_start_time); - ttree->Branch("time_pmt_rwm", &m_pmt_start_time_rwm); + ttree->Branch("time_start_pmt", &m_pmt_start_time); + ttree->Branch("time_rise_pmt", &m_pmt_rise_time); + ttree->Branch("time_peak_pmt", &m_pmt_peak_time); + ttree->Branch("time_start_pmt_rwm", &m_pmt_start_time_rwm); ttree->Branch("pe_pmt", &m_pmt_pe); ttree->Branch("amplitude_pmt", &m_pmt_amplitude); @@ -624,6 +628,7 @@ void opana::ICARUSFlashAssAna::processOpHitsFlash(std::vector &pmt_start_time, std::vector &pmt_rise_time, + std::vector &pmt_peak_time, std::vector &pmt_start_time_rwm, std::vector &pmt_pe, std::vector &pmt_amplitude, @@ -669,6 +674,7 @@ void opana::ICARUSFlashAssAna::processOpHitsFlash(std::vector Date: Sun, 8 Nov 2026 20:32:34 -0600 Subject: [PATCH 14/20] Link icarusalg::Utilities in SelectCathodeCrossingGenParticles The module includes icarusalg/Utilities/AtomicPassCounter.h but did not link any icarusalg target, so it only built when icarusalg was checked out in the same mrb area. --- icaruscode/Generators/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/icaruscode/Generators/CMakeLists.txt b/icaruscode/Generators/CMakeLists.txt index a80d94381..b2b8f94b8 100644 --- a/icaruscode/Generators/CMakeLists.txt +++ b/icaruscode/Generators/CMakeLists.txt @@ -25,6 +25,7 @@ cet_build_plugin(HepMCFileGen art::module cet_build_plugin(SelectCathodeCrossingGenParticles art::module LIBRARIES sbnalg::Utilities + icarusalg::Utilities larcore::Geometry_Geometry_service larcore::headers lardataalg::MCDumpers From 592de6257a85ff0d9954fe625064d6ed766191d4 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Sun, 8 Nov 2026 21:09:04 -0600 Subject: [PATCH 15/20] add new calib services to driver fcl --- icaruscode/PMT/OpReco/driver/run_beamstructure_ana.fcl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/icaruscode/PMT/OpReco/driver/run_beamstructure_ana.fcl b/icaruscode/PMT/OpReco/driver/run_beamstructure_ana.fcl index f279723b9..2e4a211de 100644 --- a/icaruscode/PMT/OpReco/driver/run_beamstructure_ana.fcl +++ b/icaruscode/PMT/OpReco/driver/run_beamstructure_ana.fcl @@ -11,6 +11,8 @@ #include "services_common_icarus.fcl" #include "channelmapping_icarus.fcl" #include "timing_icarus.fcl" +#include "pmt_calibration_icarus.fcl" +#include "pmt_channel_status_icarus.fcl" #include "decoderdefs_icarus.fcl" #include "icarus_ophitfinder.fcl" @@ -33,6 +35,8 @@ services: DetectorClocksService: @local::icarus_detectorclocks IICARUSChannelMap: @local::icarus_channelmappinggservice IPMTTimingCorrectionService: @local::icarus_pmttimingservice + IPhotonCalibratorService: @local::icarus_photon_calibration + IPMTChannelStatusService: @local::icarus_pmt_channel_status TFileService: { fileName: "supplemental-%ifb-%p.root" } } From a71e0c5986aa1c31ce2aba440dbe6979478ea9a0 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Sun, 8 Nov 2026 22:20:56 -0600 Subject: [PATCH 16/20] make pulse start extractor more robust to noise, share baseline and findPeak procedure across methods, make fit more deterministic --- icaruscode/Timing/Tools/CMakeLists.txt | 7 +- .../Timing/Tools/PulseStartExtractor.cc | 316 +++++++----------- icaruscode/Timing/Tools/PulseStartExtractor.h | 173 +++++----- 3 files changed, 211 insertions(+), 285 deletions(-) diff --git a/icaruscode/Timing/Tools/CMakeLists.txt b/icaruscode/Timing/Tools/CMakeLists.txt index 13d80ae23..a62a4156b 100644 --- a/icaruscode/Timing/Tools/CMakeLists.txt +++ b/icaruscode/Timing/Tools/CMakeLists.txt @@ -6,7 +6,12 @@ file(GLOB tool_srcs *.cc) # Build a library target from these sources. # This will create a target whose name is based on the directory (or you can force it with a variable). art_make_library( - LIBRARIES PUBLIC ROOT::Core ROOT::Gpad ROOT::Geom + LIBRARIES + PRIVATE + messagefacility::MF_MessageLogger + ROOT::Hist + ROOT::MathCore + ROOT::Core SOURCE ${tool_srcs} ) diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.cc b/icaruscode/Timing/Tools/PulseStartExtractor.cc index 5ab39f785..4b8c3eefb 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.cc +++ b/icaruscode/Timing/Tools/PulseStartExtractor.cc @@ -5,260 +5,176 @@ * @date Sun Feb 11 11:37:14 2024 * Description: Source file for the PulseStartExtractor class implementation. It provides * templated functions to extract the start (rising edge) time of reference - * signals (square pulses) using either constant fraction discrimination or - * a refined logistic function fit. + * signals (square pulses) using either constant fraction discrimination, + * an interpolated constant fraction or a refined logistic function fit. **/ #include "PulseStartExtractor.h" -#include -#include -#include -#include -#include -#include -#include + +#include "messagefacility/MessageLogger/MessageLogger.h" + #include "TF1.h" #include "TGraph.h" #include "TFitResultPtr.h" -#include "TRandom.h" -namespace icarus::timing { - - // Constructor - PulseStartExtractor::PulseStartExtractor(ExtractionMethod method, double threshold) - : fMethod(method), fThreshold(threshold) {} +#include +#include +#include +#include +#include - // -------------------------------------------------------------------------------- - template - std::size_t PulseStartExtractor::findMinBin(const std::vector &wf, std::size_t start, std::size_t end) const - { - auto minel = std::min_element(wf.begin() + start, wf.begin() + end); - size_t minsample = std::distance(wf.begin() + start, minel); - return minsample; - } +namespace { - template - std::size_t PulseStartExtractor::findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) const + // median of the [begin, end) range (the range is copied) + double median(std::vector::const_iterator begin, std::vector::const_iterator end) { - auto maxel = std::max_element(wf.begin() + start, wf.begin() + end); - size_t maxsample = std::distance(wf.begin() + start, maxel); - return maxsample; + std::vector copy(begin, end); + auto const mid = copy.begin() + copy.size() / 2; + std::nth_element(copy.begin(), mid, copy.end()); + return *mid; } - // -------------------------------------------------------------------------------- - template - T PulseStartExtractor::computeMedian(const std::vector &data) const - { - std::vector copy = data; - std::nth_element(copy.begin(), copy.begin() + copy.size() / 2, copy.end()); - return copy[copy.size() / 2]; - } +} // local namespace + +namespace icarus::timing { + + // Constructor + PulseStartExtractor::PulseStartExtractor(ExtractionMethod method, double threshold) + : fMethod(method), fThreshold(threshold) {} // -------------------------------------------------------------------------------- template double PulseStartExtractor::extractStart(const std::vector &wf) const { - - // Try constant-fraction discrimination (default method) - // if fit method is selected, the result is used as initial guess for the fit - std::size_t cf_sample = extractStartSampleCF(wf, fThreshold); + std::vector const y(wf.begin(), wf.end()); // if no signal is found above threshold, first sample (0) is returned - // no need to attempt a fit if no signal is there - if (cf_sample == 0) + Pulse pulse; + if (!findPulse(y, pulse)) return 0.; - if (fMethod == LOGISTIC_FIT) + switch (fMethod) { - double par[4] = {0}; - bool ok = extractStartLogisticFit(wf, cf_sample, par); - if (ok) - return par[0]; // Rising edge time from the logistic fit (can be non-integer). - else - std::cout << "Sigmoid fit did not converge; falling back to constant-fraction discrimation." << std::endl; - } + case INTERPOLATED_CF: + return crossingTime(y, pulse, kCFFraction, true); -// ***** MODOFICATIONS ***** // - -if (fMethod == INTERPOLATED_CF) { - return extractStartInterpolated(wf, 0.2, 2.0); //above 20% fraction (threshold), 2 ns sampling (this is the t0 -t1, t2-t1...fixing it) - - std::cout << "Using interpolated CF method..." << std::endl; + case LOGISTIC_FIT: + { + double start = 0.; + double const guess = crossingTime(y, pulse, kCFFraction, false); + if (fitLogistic(y, pulse, guess, start)) + return start; // Rising edge time from the logistic fit (can be non-integer). + + mf::LogDebug("PulseStartExtractor") << "Logistic fit did not converge;" + << " falling back to the interpolated 50% crossing."; + return crossingTime(y, pulse, 0.5, true); + } + case CONSTANT_FRACTION: + default: + return crossingTime(y, pulse, kCFFraction, false); } - - // Defaulting to constant-fraction discrimation - return static_cast(cf_sample); } // -------------------------------------------------------------------------------- - - template - std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector &wf, double thres) const + bool PulseStartExtractor::findPulse(std::vector const &wf, Pulse &pulse) const { - // Find the position of the minimum along the waveform - std::size_t minbin = findMinBin(wf, 0, wf.size()); + if (wf.empty()) + return false; - // Search only a cropped region of the waveform backward from the min (20 samples) - std::size_t maxbin = (minbin >= 20) ? (minbin - 20) : 0; - std::size_t startbin = maxbin; + // Find the position of the minimum along the waveform + pulse.minBin = std::distance(wf.begin(), std::min_element(wf.begin(), wf.end())); - // check if peak is high enough (remove noise) - auto delta = wf[maxbin] - wf[minbin]; - if (delta < thres) - return 0; // return first sample + // Search only a cropped region of the waveform backward from the min + pulse.searchStart = (pulse.minBin >= kLookBackSamples) ? (pulse.minBin - kLookBackSamples) : 0; - // Now we crawl betweem maxbin and minbin and we stop when: - // maxbin value - bin value > (maxbin value - minbin value )*0.2 - for (std::size_t bin = maxbin; bin < minbin; ++bin) - { - auto val = wf[maxbin] - wf[bin]; - if (val >= 0.2 * delta) // 20% - { - startbin = bin - 1; - break; - } - } - if (startbin < maxbin) - startbin = maxbin; + // baseline from everything before the search region (at least the first sample) + std::size_t const baselineEnd = std::max(pulse.searchStart, 1); + pulse.baseline = median(wf.begin(), wf.begin() + baselineEnd); + pulse.amplitude = pulse.baseline - wf[pulse.minBin]; - return startbin; + // check if peak is high enough (remove noise) + return pulse.amplitude >= fThreshold; } // -------------------------------------------------------------------------------- - - template - bool PulseStartExtractor::extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) const + double PulseStartExtractor::crossingTime(std::vector const &wf, Pulse const &pulse, + double fraction, bool interpolate) const { - std::size_t nsize = wf.size(); - std::vector x(nsize); - std::vector y(nsize); + double const level = pulse.baseline - fraction * pulse.amplitude; - for (std::size_t i = 0; i < nsize; i++) - { - x[i] = static_cast(i); - y[i] = static_cast(wf[i]); - } + // first sample at or below the level; the minimum always is + std::size_t bin = pulse.searchStart; + while (bin < pulse.minBin && wf[bin] > level) + ++bin; - TGraph *g = new TGraph(nsize, &x[0], &y[0]); + // already below the level at the start of the search window: nothing before to use + if (bin == pulse.searchStart) + return static_cast(bin); - // prepare initial parameter guesses for the fit - // baseline: level before signal - auto baseline = computeMedian(y); + if (!interpolate) + return static_cast(bin - 1); - // amplitude: level after the signal (avg over 50 samples) - auto amplitude = 0.0; - std::size_t endIndex = (guess + 50 < nsize) ? guess + 50 : nsize; - for (std::size_t i = guess; i < endIndex; i++) - amplitude += y[i]; - amplitude /= (endIndex - guess); - amplitude -= baseline; + // interpolate between the samples across the level + double const before = wf[bin - 1]; + double const after = wf[bin]; + return static_cast(bin - 1) + (before - level) / (before - after); + } - // crop a window around the initial guess: +/- 25 samples - std::size_t llim = (guess >= 25) ? guess - 25 : 0; - std::size_t ulim = (guess + 25 < nsize) ? guess + 25 : nsize - 1; + // -------------------------------------------------------------------------------- + bool PulseStartExtractor::fitLogistic(std::vector const &wf, Pulse const &pulse, + double guess, double &start) const + { + std::size_t const nsize = wf.size(); + std::size_t const guessBin = static_cast(guess); - std::string logisticModel = "1/(1+exp(([0]-x)/[1]))*[2]+[3]"; - TF1 *fitFunc = new TF1("logistic", logisticModel.c_str(), x[llim], x[ulim]); + // amplitude: level after the signal (avg over kFitAmplitudeSamples samples) + std::size_t const ampEnd = std::min(guessBin + kFitAmplitudeSamples, nsize); + double const amplitude = std::accumulate(wf.begin() + guessBin, wf.begin() + ampEnd, 0.) + / (ampEnd - guessBin) - pulse.baseline; - fitFunc->SetParameter(0, x[guess]); // initial guess for rising edge time. - fitFunc->SetParameter(1, 0.3); // initial slope. - fitFunc->SetParameter(2, amplitude); - fitFunc->SetParameter(3, baseline); + // crop a window around the initial guess + std::size_t const llim = (guessBin >= kFitHalfWindow) ? guessBin - kFitHalfWindow : 0; + std::size_t const ulim = std::min(guessBin + kFitHalfWindow, nsize - 1); - TFitResultPtr fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); - bool converged = !(bool)(int(fp)); + std::vector x(ulim - llim + 1); + std::iota(x.begin(), x.end(), static_cast(llim)); + TGraph graph(x.size(), x.data(), wf.data() + llim); - // if fit did not converge, try again a few times - // tweak some parameters until it works... - int maxtries = 100; - int trial = 0; - while (!converged && trial < maxtries) + // local function, not registered in the ROOT global list + auto const logistic = [](double const *xx, double const *p) + { return p[2] / (1. + std::exp((p[0] - xx[0]) / p[1])) + p[3]; }; + TF1 fitFunc("logistic", logistic, x.front(), x.back(), 4, 1, TF1::EAddToList::kNo); + + // try a fixed set of initial values until the fit is acceptable + for (double const offset : kFitStartOffsets) { - double newGuess = x[guess] + gRandom->Uniform(-1.5, 1.5); - double newSlope = 0.3 + gRandom->Uniform(-0.2, 0.2); - fitFunc->SetParameter(0, newGuess); - fitFunc->SetParameter(1, newSlope); - fitFunc->SetParameter(2, amplitude); - fitFunc->SetParameter(3, baseline); - fp = g->Fit(fitFunc, "SRQ", "", x[llim], x[ulim]); - converged = !(bool)(int(fp)); - trial++; + for (double const slope : kFitSlopeSeeds) + { + fitFunc.SetParameters(guess + offset, slope, amplitude, pulse.baseline); + TFitResultPtr const fp = graph.Fit(&fitFunc, "SRQN"); + if (int(fp) != 0) + continue; + + // p0 : rising edge as the inflection point (50% of amplitude) + double const p0 = fitFunc.GetParameter(0); + bool const inWindow = (p0 >= x.front()) && (p0 <= x.back()); + bool const negativePulse = (fitFunc.GetParameter(2) < 0.); + if (inWindow && negativePulse) + { + start = p0; + return true; + } + } } - // return the fit results in the parameters vector. - // p0 : rising edge as the inflection point (50% of amplitude) - par[0] = fitFunc->GetParameter(0); - par[1] = fitFunc->GetParameter(1); - par[2] = fitFunc->GetParameter(2); - par[3] = fitFunc->GetParameter(3); - - delete fitFunc; - delete g; - return converged; + return false; } - -// ***** MODIFICATIONS ***** // - -template -double PulseStartExtractor::extractStartInterpolated(const std::vector& wf, double fraction, double dt_ns) const { - if (wf.size() < 2) return 0.0; - - double baseline = computeMedian(wf); - auto minIt = std::min_element(wf.begin(), wf.end()); - std::size_t minIndex = std::distance(wf.begin(), minIt); - double peak = wf[minIndex]; - - double threshold = baseline + fraction * (peak - baseline); - - std::size_t maxbin = (minIndex >= 20) ? (minIndex - 20) : 0; - - for (std::size_t i = maxbin; i + 1 < minIndex; ++i) { - if (wf[i] > threshold && wf[i + 1] <= threshold) { - // Interpolate between sample i and i+1 - double y1 = wf[i]; - double y2 = wf[i + 1]; - double frac = (y1 - threshold) / (y1 - y2); // fractional distance - return static_cast(i) + frac; // sample index (float) - } - } - - return 0.0; -} - + // -------------------------------------------------------------------------------- template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; template double PulseStartExtractor::extractStart(const std::vector& wf) const; - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; - template std::size_t PulseStartExtractor::extractStartSampleCF(const std::vector& wf, double thres) const; - - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; - template std::size_t PulseStartExtractor::findMinBin(const std::vector& wf, std::size_t start, std::size_t end) const; - - template short PulseStartExtractor::computeMedian(const std::vector& data) const; - template int PulseStartExtractor::computeMedian(const std::vector& data) const; - template float PulseStartExtractor::computeMedian(const std::vector& data) const; - template double PulseStartExtractor::computeMedian(const std::vector& data) const; - - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; - template bool PulseStartExtractor::extractStartLogisticFit(const std::vector& wf, std::size_t guess, double par[4]) const; - -// ***** MODIFICATIONS ***** // - -template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; -template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; -template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; -template double PulseStartExtractor::extractStartInterpolated(const std::vector&, double, double) const; - - - } // icarus::timing namespace diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.h b/icaruscode/Timing/Tools/PulseStartExtractor.h index 32252b78f..c6c9be6ad 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.h +++ b/icaruscode/Timing/Tools/PulseStartExtractor.h @@ -5,22 +5,18 @@ * @date Sun Feb 11 11:37:14 2024 * Description: Header file for the PulseStartExtractor class implementation. It provides * templated functions to extract the start (rising edge) time of reference - * signals (square pulses) using either constant fraction discrimination or - * a refined logistic function fit. + * signals (square pulses) using either constant fraction discrimination, + * an interpolated constant fraction or a refined logistic function fit. **/ #ifndef PULSE_START_EXTRACTOR_H #define PULSE_START_EXTRACTOR_H +#include #include #include -#include #include #include -#include "TF1.h" -#include "TGraph.h" -#include "TFitResultPtr.h" -#include "TRandom.h" namespace icarus::timing { @@ -31,92 +27,105 @@ namespace icarus::timing { INTERPOLATED_CF }; - // map for string to enum conversion - std::map const stringToExtractionMethod = { + // map for string to enum conversion + std::map const stringToExtractionMethod = { { "LogisticFit", LOGISTIC_FIT }, { "ConstantFraction", CONSTANT_FRACTION }, -{ "InterpolatedCF", INTERPOLATED_CF } + { "InterpolatedCF", INTERPOLATED_CF } }; /** * @brief Extracts the start time of a pulse from a waveform. - * + * * This class implements methods for extracting the start time of reference pulses - * digitized in PMT special channels. These reference waveforms are expected to exhibit - * a negative-polarity square pulse with a sharp rising edge. - * - * The extraction can be performed using either a legacy constant-fraction discrimination (CF) - * algorithm or a refined logistic function fit. + * digitized in PMT special channels. These reference waveforms are expected to exhibit + * a negative-polarity square pulse with a sharp rising edge. Because of the RC coupling, + * the signal does not hold a plateau: it reaches its minimum shortly after the edge + * (from a few to several tens of samples, depending on the channel) and then decays back. + * + * The extraction can be performed using either a constant-fraction discrimination (CF), + * an interpolated constant-fraction discrimination or a refined logistic function fit. * @see icarus::timing::ExtractionMethod for the available methods. - * + * * Inputs * ------------------------- - * - * The extraction method is configured via the constructor by specifying one of the available + * + * The extraction method is configured via the constructor by specifying one of the available * methods (defined by the `icarus::timing::ExtractionMethod` enum) and a threshold value. * * `method` (`icarus::timing::ExtractionMethod`): specifies the selected method for the * pulse start extraction. The default is `icarus::timing::CONSTANT_FRACTION`. - * * `threshold` (`double`): detection threshold in ADC counts to avoid selecting noise of - * the reference pulse is missing from its waveform. Default id `500` ADC. - * - * When using a fit method, the initial parameters guesses are determined using the - * constant-fraction algorithm to help ensure fit convergence. + * * `threshold` (`double`): detection threshold in ADC counts to avoid selecting noise if + * the reference pulse is missing from its waveform. Default is `500` ADC. + * + * The other parameters of the algorithms are defined as constants at the top of the class. * - * Signal timing extraction + * Pulse finding * ---------------------------------- - * - * The reference waveform is expected to exhibit a negative-polarity square wave. - * Its selection is performed simply by looking at the absolute minimum of the waveform. - * A detection threshold on the amplitude is used to exclude picking up spurios signals - * in case the large square pulse is missing (which could happen for EW/RWM signals). - * - * ### Constant-fraction discrimination - * - * The procedure is the following: + * + * The waveform is expected to contain either one large pulse or only noise. + * All methods share the same pulse finding: * * the absolute minimum of the waveform is found; - * * an interval starting 20 ticks before that minimum is considered and the baseline level - * is defined as the value at the start of that interval; - * * if the baseline-minimum difference is below a threshold, it is assumed to be noise - * and the start sample (`0`) is returned; - * * within the defined window, the algorithm searches the first sample where the signal - * deviates from the baseline by at least 20% of the full amplitude (baseline-minimum difference); - * The sample immediately before is returned as the pulse start time. - * - * This algorithm is affected by the 2ns discretization of the input waveforms as there is no - * interpolation between the samples. - * + * * the baseline is the median of the waveform from its start up to `kLookBackSamples` + * samples before the minimum (at least the first sample is used); + * * if the baseline-minimum difference (the amplitude) is below threshold, the waveform is + * assumed to be noise and the start sample (`0`) is returned; + * * within the `kLookBackSamples` samples before the minimum, the algorithm searches the + * first sample where the signal deviates from the baseline by at least `kCFFraction` + * (20%) of the amplitude. + * + * ### Constant-fraction discrimination + * + * The sample immediately before the 20% crossing is returned as the pulse start time. + * This algorithm is affected by the 2 ns discretization of the input waveforms as there + * is no interpolation between the samples. + * + * ### Interpolated constant-fraction discrimination + * + * The 20% crossing time is linearly interpolated between the two samples across it. + * * ### Logistic Fit Method: * The pulse rising edge is fitted using a logistic function: `1/(1+exp(([0]-x)/[1]))*[2]+[3]`. * The estimate for the pulse start is then taken to be the inflection point (ie. the steepest point, - * where the first derivative is maximal) which corresponds to parameter `p0`. - * It also represents the point at 50% of the amplitude. - * + * where the first derivative is maximal) which corresponds to parameter `p0`. + * It also represents the point at 50% of the amplitude. + * * The procedure is the following: - * * the constant-fraction method is ran first to obtain an initial guess for the pulse - * start time (`p0`). If no signal is found, the start sample (`0`) is returned; - * * an initial guess for the baseline (`p3`) is estimated as the median of the full waveform; - * * an initial guess for the amplitude (`p2`) is estimated averaging the first 50 samples - * after the initial guess for the rise time (`p0`). - * * the logistic function fit is performed over a window of `[-25,25]` samples around the - * initial guess for the rise time; - * * if the fit converges, `p0` is returned as the pulse start time; - * * if the fit fails to converge, multiple retries with slight random variations around the - * initial parameters are attempted. If all retries are exahusted, it falls back to - * returning the constant-fraction method’s result. + * * the initial guess for the pulse start time (`p0`) is the constant-fraction result; + * * the initial guess for the baseline (`p3`) is the baseline from the pulse finding; + * * the initial guess for the amplitude (`p2`) is estimated averaging the first + * `kFitAmplitudeSamples` samples after the initial guess for the rise time (`p0`); + * * the logistic function fit is performed over a window of `kFitHalfWindow` samples + * around the initial guess for the rise time; + * * if the fit converges with `p0` within the fit window and a negative amplitude (`p2`), + * `p0` is returned as the pulse start time; + * * otherwise, the fit is retried with the other initial values in `kFitStartOffsets` and + * `kFitSlopeSeeds`. If all attempts fail, the interpolated 50% crossing time is returned. * * This algorithm allows to interpolate between samples, achieving a better timing resolution. - * + * */ class PulseStartExtractor { public: - - // Constructor: select extraction method and threshold in ADC (used in the CF method). + /// Samples before the minimum where the start is searched; the baseline region ends there. + static constexpr std::size_t kLookBackSamples = 100; + /// Fraction of the amplitude used by the constant-fraction methods. + static constexpr double kCFFraction = 0.2; + /// Samples after the initial start guess averaged for the fit amplitude guess. + static constexpr std::size_t kFitAmplitudeSamples = 50; + /// Fit window half width around the initial start guess [samples]. + static constexpr std::size_t kFitHalfWindow = 25; + /// Offsets to the initial start guess tried by the fit [samples]. + static constexpr std::array kFitStartOffsets { 0., -1., 1. }; + /// Initial values of the fit slope parameter (`p1`) tried by the fit [samples]. + static constexpr std::array kFitSlopeSeeds { 0.3, 0.1, 0.5, 1.0 }; + + // Constructor: select extraction method and threshold in ADC. PulseStartExtractor(ExtractionMethod method = CONSTANT_FRACTION, double threshold= 500); // Templated public function to extract the start sample from a waveform. - // Returns the start sample index (std::size_t). + // Returns the start sample (can be fractional), or `0` if no pulse is found. template double extractStart(const std::vector &wf) const; @@ -124,31 +133,27 @@ namespace icarus::timing { ExtractionMethod fMethod; double fThreshold; - // Templated helper: constant-fraction extraction. - template - std::size_t extractStartSampleCF(const std::vector &wf, double thres) const; - - // Templated helper: finds the index of the minimum element in the range [start, end). - template - std::size_t findMinBin(const std::vector &wf, std::size_t start, std::size_t end) const; + /// Pulse found in the waveform. + struct Pulse + { + std::size_t minBin = 0; ///< sample of the absolute minimum + std::size_t searchStart = 0; ///< first sample of the start search window + double baseline = 0.; ///< baseline level [ADC] + double amplitude = 0.; ///< baseline-minimum difference [ADC] + }; - // Templated helper: finds the index of the maximum element in the range [start, end). - template - std::size_t findMaxBin(const std::vector &wf, std::size_t start, std::size_t end) const; + // Finds the pulse in the waveform; returns false if only noise is found. + bool findPulse(std::vector const &wf, Pulse &pulse) const; - // Templated helper: computes the median of a vector. - template - T computeMedian(const std::vector &data) const; + // Returns the crossing time of `fraction` of the pulse amplitude below baseline. + // If `interpolate` is false, the last sample before the crossing is returned. + double crossingTime(std::vector const &wf, Pulse const &pulse, + double fraction, bool interpolate) const; - // Templated helper: logistic-fit extraction; fills par[4] with fit parameters. - // Returns true if the fit converged. - template - bool extractStartLogisticFit(const std::vector &wf, std::size_t guess, double par[4]) const; + // Logistic-fit extraction seeded with `guess`; returns false if no fit is acceptable. + bool fitLogistic(std::vector const &wf, Pulse const &pulse, double guess, double &start) const; -template -double extractStartInterpolated(const std::vector& wf, double fraction, double dt_ns) const; - - }; + }; } // icarus::timing namespace From aa47647961c8dd4a34de135a69b36930420a9865 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Sun, 8 Nov 2026 22:58:52 -0600 Subject: [PATCH 17/20] update defaults, comments --- icaruscode/Decode/DaqDecoderICARUSPMT_module.cc | 2 +- icaruscode/Timing/PMTBeamSignalsExtractor_module.cc | 5 +++-- icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h | 5 +++-- icaruscode/Timing/Tools/PulseStartExtractor.h | 4 ++-- icaruscode/Timing/timing_beam.fcl | 4 +--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc b/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc index c1412f641..f35ea7f2d 100644 --- a/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc +++ b/icaruscode/Decode/DaqDecoderICARUSPMT_module.cc @@ -193,7 +193,7 @@ namespace icarus { class DaqDecoderICARUSPMT; } * waveform-based timing correction is used. * * `ApplyCableDelayCorrection` (flag, default: `true`): if set, applies the * cable delay corrections from a database. - * * `PulseStartExtractionMethod` (string, default: `LogisticFit`): pulse start + * * `PulseStartExtractionMethod` (string, default: `InterpolatedCF`): pulse start * extraction method for the time corrections. * @see icarus::timing::PulseStartExtractor for details and options. * * `PulseStartExtractionThreshold` (double, default: `500.`): detection threshold diff --git a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc index 4ea91fd90..d24882fb2 100644 --- a/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc +++ b/icaruscode/Timing/PMTBeamSignalsExtractor_module.cc @@ -82,8 +82,9 @@ namespace icarus::timing * The RWM/EW signals are expected to be a sharp square wave in negative * polarity. The time of the correction is based on the front side of that wave. * The extraction is perfomed by `icarus::timing::PulseStartExtractor` by - * specifying one of the available extraction methods (constant-fraction discrimation, - * logitstic function fit) and an ADC threshold for the signal identification. + * specifying one of the available extraction methods (constant-fraction discrimination, + * interpolated constant-fraction discrimination, logistic function fit) and an ADC + * threshold for the signal identification. * @see `icarus::timing::PulseStartExtractor` for details. * * The extracted times are then corrected using the standard waveform time corrections diff --git a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h index 8fe0a2d65..fb6b2da7d 100644 --- a/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h +++ b/icaruscode/Timing/PMTWaveformTimeCorrectionExtractor.h @@ -51,8 +51,9 @@ namespace icarus::timing { class PMTWaveformTimeCorrectionExtractor; } * The reference signal is expected to be a sharp square wave in negative * polarity. The time of the correction is based on the front side of that wave. * The extraction is perfomed by `icarus::timing::PulseStartExtractor` by - * specifying one of the available extraction methods (constant-fraction discrimation, - * logitstic function fit) and an ADC threshold for the signal identification. + * specifying one of the available extraction methods (constant-fraction discrimination, + * interpolated constant-fraction discrimination, logistic function fit) and an ADC + * threshold for the signal identification. * * @see `icarus::timing::PulseStartExtractor` for details. * diff --git a/icaruscode/Timing/Tools/PulseStartExtractor.h b/icaruscode/Timing/Tools/PulseStartExtractor.h index c6c9be6ad..277203f35 100644 --- a/icaruscode/Timing/Tools/PulseStartExtractor.h +++ b/icaruscode/Timing/Tools/PulseStartExtractor.h @@ -53,7 +53,7 @@ namespace icarus::timing { * The extraction method is configured via the constructor by specifying one of the available * methods (defined by the `icarus::timing::ExtractionMethod` enum) and a threshold value. * * `method` (`icarus::timing::ExtractionMethod`): specifies the selected method for the - * pulse start extraction. The default is `icarus::timing::CONSTANT_FRACTION`. + * pulse start extraction. The default is `icarus::timing::INTERPOLATED_CF`. * * `threshold` (`double`): detection threshold in ADC counts to avoid selecting noise if * the reference pulse is missing from its waveform. Default is `500` ADC. * @@ -122,7 +122,7 @@ namespace icarus::timing { static constexpr std::array kFitSlopeSeeds { 0.3, 0.1, 0.5, 1.0 }; // Constructor: select extraction method and threshold in ADC. - PulseStartExtractor(ExtractionMethod method = CONSTANT_FRACTION, double threshold= 500); + PulseStartExtractor(ExtractionMethod method = INTERPOLATED_CF, double threshold= 500); // Templated public function to extract the start sample from a waveform. // Returns the start sample (can be fractional), or `0` if no pulse is found. diff --git a/icaruscode/Timing/timing_beam.fcl b/icaruscode/Timing/timing_beam.fcl index 3ccb2e34d..db469a27d 100644 --- a/icaruscode/Timing/timing_beam.fcl +++ b/icaruscode/Timing/timing_beam.fcl @@ -9,9 +9,7 @@ icarus_beam_signal_extractor: EWlabel: "daqPMT:EW" RWMlabel: "daqPMT:RWM" TriggerCorrectionLabel: "daqPMT:globtrg" - #TimeExtractionMethod: "InterpolatedCF" - #TimeExtractionMethod: "LogisticFit" - TimeExtractionMethod: "ConstantFraction" + TimeExtractionMethod: "InterpolatedCF" # or "ConstantFraction", "LogisticFit" ApplyConsensusFiltering: true ADCThreshold: 500 BoardSetup: @local::icarus_V1730_setup From 8c7c1aee7bbf8904c80e2bcd129c298a894b84fe Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Sun, 8 Nov 2026 23:00:28 -0600 Subject: [PATCH 18/20] enable sub-sample interpolation for rise time --- icaruscode/PMT/OpReco/fcl/icarus_ophitfinder.fcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/icaruscode/PMT/OpReco/fcl/icarus_ophitfinder.fcl b/icaruscode/PMT/OpReco/fcl/icarus_ophitfinder.fcl index 18240ac18..6f39eff01 100644 --- a/icaruscode/PMT/OpReco/fcl/icarus_ophitfinder.fcl +++ b/icaruscode/PMT/OpReco/fcl/icarus_ophitfinder.fcl @@ -217,7 +217,7 @@ icarus_ophit: # some basic configuration RiseTimeCalculator: { tool_type: RiseTimeThreshold PeakRatio: 0.15 # at 15% of the peak amplitude -- not tuned - #InterpolateSamples: true # sub-sample linear interpolation + InterpolateSamples: true # sub-sample linear interpolation } } From 7a439fd5f9b1378e2756abb0d6e6a05d4907f97e Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Sun, 8 Nov 2026 23:15:28 -0600 Subject: [PATCH 19/20] fix access index --- icaruscode/PMT/OpReco/ICARUSBeamStructureAna_module.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/icaruscode/PMT/OpReco/ICARUSBeamStructureAna_module.cc b/icaruscode/PMT/OpReco/ICARUSBeamStructureAna_module.cc index 22e3d14f7..26ee58a2e 100644 --- a/icaruscode/PMT/OpReco/ICARUSBeamStructureAna_module.cc +++ b/icaruscode/PMT/OpReco/ICARUSBeamStructureAna_module.cc @@ -502,7 +502,7 @@ double opana::ICARUSBeamStructureAna::getFlashBunchTime(std::vector channel // if any RWM copy is missing (therefore missing for an entire PMT crate), // it might not be possible to use the first hits (they might not have a RMW time) // so return sbn::timing::NoTime as in other bad cases - if (!fRWMTimes[i].isValid()) + if (!fRWMTimes.at(ch).isValid()) return sbn::timing::NoTime; // count hits separetely on the two walls From ac17feab10d16b5f5eb7ce27887b28b421ed1b54 Mon Sep 17 00:00:00 2001 From: Matteo Vicenzi Date: Mon, 9 Nov 2026 10:03:58 -0600 Subject: [PATCH 20/20] turn off consensus filtering by default while it is being validated --- icaruscode/Timing/timing_beam.fcl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/icaruscode/Timing/timing_beam.fcl b/icaruscode/Timing/timing_beam.fcl index db469a27d..f720ac80f 100644 --- a/icaruscode/Timing/timing_beam.fcl +++ b/icaruscode/Timing/timing_beam.fcl @@ -10,7 +10,7 @@ icarus_beam_signal_extractor: RWMlabel: "daqPMT:RWM" TriggerCorrectionLabel: "daqPMT:globtrg" TimeExtractionMethod: "InterpolatedCF" # or "ConstantFraction", "LogisticFit" - ApplyConsensusFiltering: true + ApplyConsensusFiltering: false # consensus logic still under validation ADCThreshold: 500 BoardSetup: @local::icarus_V1730_setup DebugTree: true