19 srand(static_cast<unsigned int>(time(0)));
26 const std::string name)
62 for (
size_t i = 0; i < wf.size(); ++i) {
67 if (i < _sample_size || i >= (wf.size() -
_sample_size))
continue;
80 for (
size_t i = (wf.size() -
_sample_size); i < wf.size(); ++i) {
82 mean_v[i] = mean_v[wf.size() - _sample_size - 1];
83 sigma_v[i] = sigma_v[wf.size() - _sample_size - 1];
86 float best_sigma = 1.1e9;
87 size_t best_sigma_index = 0;
88 size_t num_good_adc = 0;
90 for (
size_t i = 0; i < sigma_v.size(); ++i) {
92 auto const&
mean = mean_v[i];
96 auto const& sigma = sigma_v[i];
97 if (sigma < best_sigma) {
105 if (num_good_adc < 1) {
106 std::cerr <<
"\033[93m<<" << __FUNCTION__
107 <<
">>\033[00m Could not find good pedestal at all..." << std::endl;
112 if (best_sigma >
_max_sigma || num_good_adc < 3) {
113 for (
size_t i = 0; i < mean_v.size(); ++i) {
114 mean_v[i] = mean_v.at(best_sigma_index);
115 sigma_v[i] = sigma_v.at(best_sigma_index);
123 unsigned nbins = 1000;
136 int last_good_index = -1;
138 for (
size_t i = 0; i < wf.size(); ++i) {
140 auto const mean = mean_v[i];
141 auto const sigma = sigma_v[i];
148 if (last_good_index < 0) {
149 last_good_index = (int)i;
153 if ((last_good_index + 1) < (int)i) {
155 auto diff = fabs(mean_v.at(last_good_index) -
mean);
157 if (diff > diff_cutoff) {
159 float slope = (
mean - mean_v.at(last_good_index)) / (
float(i - last_good_index));
161 for (
size_t j = last_good_index + 1; j < i; ++j) {
162 mean_v.at(j) = slope * (float(j - last_good_index)) + mean_v.at(last_good_index);
163 sigma_v.at(j) = mode_sigma;
169 auto presample_mean =
173 auto diff_pre = fabs(presample_mean - mode_mean);
174 auto diff_post = fabs(postsample_mean - mode_mean);
176 auto constant_mean = diff_pre <= diff_post ? presample_mean : postsample_mean;
178 for (
size_t j = last_good_index + 1; j < i; ++j) {
180 mean_v.at(j) = constant_mean;
181 sigma_v.at(j) = mode_sigma;
185 last_good_index = (int)i;
194 if (sigma_v.front() > mode_sigma) {
196 int first_index = -1;
197 int second_index = -1;
199 for (
size_t i = 0; i < wf.size(); ++i) {
200 if (sigma_v.at(i) < mode_sigma) {
202 first_index = (int)i;
203 else if (second_index < 0) {
204 second_index = (int)i;
210 if (first_index < 0 || second_index < 0) {
211 std::cerr <<
"\033[93m<<" << __FUNCTION__
212 <<
">>\033[00m Could not find good pedestal for CDF" 214 <<
"first_index: " << first_index <<
"\n" 215 <<
"second_index: " << second_index <<
"\n" 216 <<
"If one of these is less than zero, this means there is pulse\n" 217 <<
"on first sample and baseline never went back down. Returning false here.";
221 auto diff = fabs(mean_v.at(first_index) - mean_v.at(second_index));
223 if (diff > diff_cutoff) {
226 (mean_v.at(second_index) - mean_v.at(first_index)) / (
float(second_index - first_index));
228 for (
int j = 0; j < first_index; ++j) {
229 mean_v.at(j) = mean_v.at(first_index) - slope * (first_index - j);
237 for (
int j = 0; j < first_index; ++j) {
239 mean_v.at(j) = postsample_mean;
240 sigma_v.at(j) = mode_sigma;
245 if (sigma_v.back() > mode_sigma) {
247 int first_index = -1;
248 int second_index = -1;
250 for (
int i = wf.size() - 1; i >= 0; --i) {
251 if (sigma_v.at(i) < mode_sigma) {
252 if (second_index < 0)
253 second_index = (int)i;
254 else if (first_index < 0) {
255 first_index = (int)i;
261 if (first_index < 0 || second_index < 0) {
262 std::cerr <<
"\033[93m<<" << __FUNCTION__
263 <<
">>\033[00m Could not find good pedestal for CDF" 265 <<
"first_index: " << first_index <<
"\n" 266 <<
"second_index: " << second_index <<
"\n" 267 <<
"If one of these is less than zero, this means there is pulse\n" 268 <<
"on the last sample and baseline never went back down. Returning false here.";
272 auto diff = fabs(mean_v.at(second_index) - mean_v.at(first_index));
274 if (diff > diff_cutoff) {
277 (mean_v.at(second_index) - mean_v.at(first_index)) / (
float(second_index - first_index));
278 for (
int j = second_index + 1; j < int(wf.size()); ++j) {
279 mean_v.at(j) = mean_v.at(second_index) + slope * (j - second_index);
287 for (
int j = second_index + 1; j < int(wf.size()); ++j) {
289 mean_v.at(j) = presample_mean;
290 sigma_v.at(j) = mode_sigma;
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
double BinnedMaxOccurrence(const PedestalMean_t &mean_v, const size_t nbins)
std::vector< double > PedestalSigma_t
bool ComputePedestal(const pmtana::Waveform_t &wf, pmtana::PedestalMean_t &mean_v, pmtana::PedestalSigma_t &sigma_v)
Method to compute a pedestal of the input waveform using "nsample" ADC samples from "start" index...
double edge_aware_mean(const std::vector< short > &wf, int start, int end)
Class definition file of PedAlgoRollingMean.
T get(std::string const &key) const
std::vector< short > Waveform_t
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
PedAlgoRollingMean(const std::string name="PedRollingMean")
Default constructor.
std::vector< double > PedestalMean_t