22 srand(static_cast<unsigned int>(time(0)));
41 if (_n_wf_to_csvfile > 0) {
42 _csvfile.open(
"wf_pedalgormsslider.csv", std::ofstream::out | std::ofstream::trunc);
43 _csvfile <<
"n,time,wf,wf_ped_mean,wf_ped_rms" << std::endl;
51 std::cout <<
"PedAlgoRmsSlider setting:" 61 if (!nsample) nsample = wf.size();
62 if (start > wf.size() || (start + nsample) > wf.size())
65 double sum = std::accumulate(wf.begin() + start, wf.begin() + start + nsample, 0.0);
67 sum /= ((double)nsample);
74 const double ped_mean,
79 if (!nsample) nsample = wf.size();
80 if (start > wf.size() || (start + nsample) > wf.size())
85 for (
size_t index = start; index < (start + nsample); ++index) {
86 sigma += pow((wf[index] - ped_mean), 2);
89 sigma = sqrt(sigma / ((
double)(nsample)));
106 mean_v.resize(wf.size(), 0);
107 sigma_v.resize(wf.size(), 0);
115 mean_temp_v.resize(wf.size(), 0);
117 for (
size_t i = 0; i < wf.size(); ++i) {
118 mean_temp_v[i] = wf[i];
126 int last_good_index = -1;
127 double local_mean, local_rms;
128 std::vector<double> local_mean_v(wf.size(), -1.);
129 std::vector<double> local_sigma_v(wf.size(), -1.);
137 std::cout <<
"\033[93mPedAlgoRmsSlider\033[00m: i " << i <<
" local_mean: " << local_mean
138 <<
" local_rms: " << local_rms << std::endl;
142 local_mean_v[i] = local_mean;
143 local_sigma_v[i] = local_rms;
146 std::cout <<
"\033[93mBelow threshold\033[00m: " 147 <<
"at i " << i <<
" last good index was: " << last_good_index << std::endl;
152 last_good_index = -1;
153 std::vector<bool> ped_interapolated(wf.size(),
false);
156 if (local_mean_v[i] > -0.1) {
159 if ((last_good_index + 1) < (int)i) {
162 int start_tick = last_good_index;
166 for (
int j = start_tick; j >= start_bound; --j) {
167 if (local_mean_v[j] < 0)
continue;
170 for (
int j = end_tick; j <= end_bound; ++j) {
171 if (local_mean_v[j] < 0)
continue;
177 (local_mean_v[end_tick] - local_mean_v[start_tick]) / (
float(end_tick - start_tick));
179 for (
int j = start_tick + 1; j < end_tick; ++j) {
180 mean_temp_v[j] = slope * (float(j - start_tick)) + local_mean_v[start_tick];
184 sigma_v[j] = (local_sigma_v[end_tick] != 0 ?
185 local_sigma_v[end_tick] :
186 local_sigma_v[start_tick]);
187 ped_interapolated[j] =
true;
266 bool end_found =
false;
282 for (
size_t j = 0; j < i; j++) {
283 mean_temp_v[j] = local_mean;
284 sigma_v[j] = local_rms;
285 ped_interapolated[j] =
true;
292 std::cerr <<
"\033[93m<<" << __FUNCTION__
293 <<
">>\033[00m Could not find good pedestal for CDF" 294 <<
"There is pulse on first sample and baseline never went back down. Returning " 302 bool start_found =
false;
318 for (
size_t j = wf.size() - 1; j > i; j--) {
319 mean_temp_v[j] = local_mean;
320 sigma_v[j] = local_rms;
321 ped_interapolated[j] =
true;
328 std::cerr <<
"\033[93m<<" << __FUNCTION__
329 <<
">>\033[00m Could not find good pedestal for CDF" 330 <<
"There is pulse on last sample and baseline never went back down. Returning " 343 for (
size_t i = 0; i < mean_temp_v.size(); ++i) {
345 if (i < _sample_size || i >= (wf.size() -
_sample_size))
continue;
348 if (!ped_interapolated[i]) {
357 if (!ped_interapolated[i]) { sigma_v[i] = sigma_v[
_sample_size]; }
361 for (
size_t i = (mean_temp_v.size() -
_sample_size); i < mean_temp_v.size(); ++i) {
363 mean_v[i] = mean_v[wf.size() - _sample_size - 1];
364 if (!ped_interapolated[i]) { sigma_v[i] = sigma_v[wf.size() - _sample_size - 1]; }
370 for (
size_t i = 0; i < wf.size(); i++) {
372 << sigma_v[i] << std::endl;
387 float best_sigma = 1.1e9;
388 size_t best_sigma_index = 0;
389 size_t num_good_adc = 0;
391 for (
size_t i = 0; i < sigma_v.size(); ++i) {
393 auto const&
mean = mean_v[i];
397 auto const& sigma = sigma_v[i];
398 if (sigma < best_sigma) {
400 best_sigma_index = i;
406 if (num_good_adc < 1) {
407 std::cerr <<
"\033[93m<<" << __FUNCTION__
408 <<
">>\033[00m Could not find good pedestal at all..." << std::endl;
413 if (best_sigma >
_max_sigma || num_good_adc < 3) {
416 std::cout <<
"\033[93mPedAlgoRmsSlider\033[00m: Not enough number of good mean indices." 417 <<
"Using the best guess within this waveform." << std::endl;
420 for (
size_t i = 0; i < mean_v.size(); ++i) {
421 mean_v[i] = mean_v[best_sigma_index];
422 sigma_v[i] = sigma_v[best_sigma_index];
Class def header for exception classes in OpticalDetector package.
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
void PrintInfo()
Print settings.
bool CheckSanity(pmtana::PedestalMean_t &mean_v, pmtana::PedestalSigma_t &sigma_v)
Checks the sanity of the estimated pedestal, returns false if not sane.
std::vector< double > PedestalSigma_t
float _ped_range_min
Min value of adc to consider adc as 'sane'.
int _num_postsample
number of ADCs to sample after the gap
float _max_sigma
Max sigma to consider adc as 'sane'.
PedAlgoRmsSlider(const std::string name="PedRmsSlider")
Default constructor.
int _num_presample
number of ADCs to sample before the gap
double CalcStd(const std::vector< double > &wf, const double ped_mean, size_t start, size_t nsample)
Returns the std of the elements of the vector from start to start+nsample.
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 _threshold
Threshold applied to local rms to claim a pulse.
float _ped_range_max
Max value of adc to consider adc as 'sane'.
T get(std::string const &key) const
std::vector< short > Waveform_t
int _n_wf_to_csvfile
If greater than zero saves firsts waveforms with pedestal to csv file.
size_t _sample_size
How many samples are used to calculate local rms and mean.
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Class definition file of PedAlgoRmsSlider.
double CalcMean(const std::vector< double > &wf, size_t start, size_t nsample)
Returns the mean of the elements of the vector from start to start+nsample.
std::vector< double > PedestalMean_t
bool _verbose
For debugging.