7 #ifndef larana_OPTICALDETECTOR_PEDALGORMSSLIDER_CXX 8 #define larana_OPTICALDETECTOR_PEDALGORMSSLIDER_CXX 25 srand(static_cast<unsigned int>(time(0)));
30 const std::string name)
44 if (_n_wf_to_csvfile > 0) {
45 _csvfile.open (
"wf_pedalgormsslider.csv", std::ofstream::out | std::ofstream::trunc);
46 _csvfile <<
"n,time,wf,wf_ped_mean,wf_ped_rms" << std::endl;
60 std::cout <<
"PedAlgoRmsSlider setting:" 71 if(!nsample) nsample = wf.size();
72 if(start > wf.size() || (start+nsample) > wf.size())
75 double sum = std::accumulate(wf.begin()+start,wf.begin()+start+nsample,0.0);
77 sum /= ((double)nsample);
86 if(!nsample) nsample = wf.size();
87 if(start > wf.size() || (start+nsample) > wf.size())
92 for(
size_t index=start; index < (start+nsample); ++index){
93 sigma += pow( (wf[index] - ped_mean), 2 );
96 sigma = sqrt(sigma/((
double)(nsample)));
115 mean_v.resize (wf.size(), 0);
116 sigma_v.resize(wf.size(), 0);
127 mean_temp_v.resize( wf.size(), 0);
129 for(
size_t i=0; i< wf.size(); ++i) {
130 mean_temp_v[i] = wf[i];
143 double local_mean, local_rms;
145 int last_good_index = -1;
146 double last_local_mean = -1;
147 double last_local_rms = -1;
149 std::vector<bool> ped_interapolated;
150 ped_interapolated.resize(wf.size());
151 for (
size_t i = 0; i < wf.size(); i++) ped_interapolated.at(i) =
false;
158 if(
_verbose) std::cout <<
"\033[93mPedAlgoRmsSlider\033[00m: i " << i <<
" local_mean: " << local_mean <<
" local_rms: " << local_rms << std::endl;
163 std::cout <<
"\033[93mBelow threshold\033[00m: " 165 <<
" last good index was: " << last_good_index
168 if(last_good_index<0) {
169 last_good_index = (int)i;
170 last_local_mean = local_mean;
171 last_local_rms = local_rms;
176 if( ( last_good_index + 1 ) < (int)i ) {
179 float slope = (local_mean - last_local_mean) / (
float(i - last_good_index));
181 for(
size_t j = last_good_index + 1; j < i && j < wf.size(); ++j) {
182 mean_temp_v.at(j) = slope * ( float(j - last_good_index) ) + mean_temp_v.at(last_good_index);
186 sigma_v.at(j) = (local_rms != 0 ? local_rms : last_local_rms);
187 ped_interapolated.at(j) =
true;
191 last_good_index = (int)i;
192 last_local_mean = local_mean;
193 last_local_rms = local_rms;
208 bool end_found =
false;
224 for (
size_t j = 0; j < i; j++){
225 mean_temp_v.at(j) = local_mean;
226 sigma_v.at(j) = local_rms;
227 ped_interapolated.at(j) =
true;
234 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal for CDF" 235 <<
"There is pulse on first sample and baseline never went back down. Returning false here.";
244 bool start_found =
false;
260 for (
size_t j = wf.size()-1; j > i; j--){
261 mean_temp_v.at(j) = local_mean;
262 sigma_v.at(j) = local_rms;
263 ped_interapolated.at(j) =
true;
270 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal for CDF" 271 <<
"There is pulse on last sample and baseline never went back down. Returning false here.";
290 for(
size_t i=0; i< mean_temp_v.size(); ++i) {
292 if( i < _sample_size || i >= (wf.size() -
_sample_size) )
continue;
295 if(!ped_interapolated.at(i)){
304 if(!ped_interapolated.at(i)){
310 for(
size_t i=(mean_temp_v.size() -
_sample_size); i<mean_temp_v.size(); ++i) {
312 mean_v[i] = mean_v [wf.size() - _sample_size -1];
313 if(!ped_interapolated.at(i)){
314 sigma_v[i] = sigma_v[wf.size() - _sample_size -1];
324 for (
size_t i = 0; i < wf.size(); i++) {
325 _csvfile <<
_wf_saved-1 <<
"," << i <<
"," << wf[i] <<
"," << mean_v.at(i) <<
"," << sigma_v[i] << std::endl;
345 float best_sigma = 1.1e9;
346 size_t best_sigma_index = 0;
347 size_t num_good_adc = 0;
349 for(
size_t i=0; i<sigma_v.size(); ++i) {
351 auto const&
mean = mean_v[i];
355 auto const& sigma = sigma_v[i];
356 if(sigma < best_sigma) {
358 best_sigma_index = i;
365 if( num_good_adc < 1 ) {
366 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal at all..." << std::endl;
371 if(best_sigma >
_max_sigma || num_good_adc < 3) {
374 std::cout <<
"\033[93mPedAlgoRmsSlider\033[00m: Not enough number of good mean indices." 375 <<
"Using the best guess within this waveform." 379 for(
size_t i=0; i<mean_v.size(); ++i) {
380 mean_v[i] = mean_v.at ( best_sigma_index );
381 sigma_v[i] = sigma_v.at ( 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'.
virtual ~PedAlgoRmsSlider()
Default destructor.
float _max_sigma
Max sigma to consider adc as 'sane'.
PedAlgoRmsSlider(const std::string name="PedRmsSlider")
Default constructor.
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.