10 #include "fhiclcpp/ParameterSet.h"
23 srand(static_cast<unsigned int>(time(0)));
28 const std::string
name)
34 _threshold = pset.get<
double>(
"Threshold", 0.6 );
35 _max_sigma = pset.get<
float> (
"MaxSigma", 0.5 );
43 if (_n_wf_to_csvfile > 0) {
44 _csvfile.open (
"wf_pedalgormsslider.csv", std::ofstream::out | std::ofstream::trunc);
45 _csvfile <<
"n,time,wf,wf_ped_mean,wf_ped_rms" << std::endl;
65 if(!nsample) nsample = wf.size();
66 if(start > wf.size() || (start+nsample) > wf.size())
69 double sum = std::accumulate(wf.begin()+start,wf.begin()+start+nsample,0.0);
71 sum /= ((double)nsample);
80 if(!nsample) nsample = wf.size();
81 if(start > wf.size() || (start+nsample) > wf.size())
86 for(
size_t index=start; index < (start+nsample); ++index){
87 sigma += pow( (wf[index] - ped_mean), 2 );
90 sigma = sqrt(sigma/((
double)(nsample)));
109 mean_v.resize (wf.size(), 0);
110 sigma_v.resize(wf.size(), 0);
121 mean_temp_v.resize( wf.size(), 0);
123 for(
size_t i=0; i< wf.size(); ++i) {
124 mean_temp_v[i] = wf[i];
136 int last_good_index = -1;
137 double local_mean, local_rms;
138 std::vector<double> local_mean_v(wf.size(),-1.);
139 std::vector<double> local_sigma_v(wf.size(),-1.);
147 <<
" local_mean: " << local_mean
148 <<
" local_rms: " << local_rms << std::endl;
152 local_mean_v[i] = local_mean;
153 local_sigma_v[i] = local_rms;
156 std::cout <<
"\033[93mBelow threshold\033[00m: "
158 <<
" last good index was: " << last_good_index
165 last_good_index = -1;
166 std::vector<bool> ped_interapolated(wf.size(),
false);
169 if(local_mean_v[i] > -0.1) {
172 if( ( last_good_index + 1 ) < (int)i ) {
175 int start_tick = last_good_index;
179 for(
int j=start_tick; j>=start_bound; --j) {
180 if(local_mean_v[j] < 0)
continue;
183 for(
int j=end_tick; j<=end_bound; ++j) {
184 if(local_mean_v[j] < 0)
continue;
189 float slope = (local_mean_v[end_tick] - local_mean_v[start_tick]) / (
float(end_tick - start_tick));
191 for(
int j = start_tick + 1; j < end_tick; ++j) {
192 mean_temp_v[j] = slope * ( float(j - start_tick) ) + local_mean_v[start_tick];
196 sigma_v[j] = (local_sigma_v[end_tick] != 0 ? local_sigma_v[end_tick] : local_sigma_v[start_tick]);
197 ped_interapolated[j] =
true;
278 bool end_found =
false;
294 for (
size_t j = 0; j < i; j++){
295 mean_temp_v[j] = local_mean;
296 sigma_v[j] = local_rms;
297 ped_interapolated[j] =
true;
304 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal for CDF"
305 <<
"There is pulse on first sample and baseline never went back down. Returning false here.";
314 bool start_found =
false;
330 for (
size_t j = wf.size()-1; j > i; j--){
331 mean_temp_v[j] = local_mean;
332 sigma_v[j] = local_rms;
333 ped_interapolated[j] =
true;
340 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal for CDF"
341 <<
"There is pulse on last sample and baseline never went back down. Returning false here.";
360 for(
size_t i=0; i< mean_temp_v.size(); ++i) {
362 if( i < _sample_size || i >= (wf.size() -
_sample_size) )
continue;
365 if(!ped_interapolated[i]){
374 if(!ped_interapolated[i]){
380 for(
size_t i=(mean_temp_v.size() -
_sample_size); i<mean_temp_v.size(); ++i) {
382 mean_v[i] = mean_v [wf.size() - _sample_size -1];
383 if(!ped_interapolated[i]){
384 sigma_v[i] = sigma_v[wf.size() - _sample_size -1];
394 for (
size_t i = 0; i < wf.size(); i++) {
395 _csvfile <<
_wf_saved-1 <<
"," << i <<
"," << wf[i] <<
"," << mean_v[i] <<
"," << sigma_v[i] << std::endl;
415 float best_sigma = 1.1e9;
416 size_t best_sigma_index = 0;
417 size_t num_good_adc = 0;
419 for(
size_t i=0; i<sigma_v.size(); ++i) {
421 auto const&
mean = mean_v[i];
425 auto const& sigma = sigma_v[i];
426 if(sigma < best_sigma) {
428 best_sigma_index = i;
435 if( num_good_adc < 1 ) {
436 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal at all..." << std::endl;
441 if(best_sigma >
_max_sigma || num_good_adc < 3) {
444 std::cout <<
"\033[93mPedAlgoRmsSlider\033[00m: Not enough number of good mean indices."
445 <<
"Using the best guess within this waveform."
449 for(
size_t i=0; i<mean_v.size(); ++i) {
450 mean_v[i] = mean_v [ best_sigma_index ];
451 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
BEGIN_PROLOG could also be cerr
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'.
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.
BEGIN_PROLOG don t mess with this pandoraTrackGausCryoW true
BEGIN_PROLOG could also be cout