7 #ifndef larana_OPTICALDETECTOR_PEDALGOROLLINGMEAN_CXX 8 #define larana_OPTICALDETECTOR_PEDALGOROLLINGMEAN_CXX 22 srand(static_cast<unsigned int>(time(0)));
28 const std::string name)
70 for(
size_t i=0; i< wf.size(); ++i) {
75 if( i < _sample_size || i >= (wf.size() -
_sample_size) )
continue;
89 for(
size_t i=(wf.size() -
_sample_size); i<wf.size(); ++i) {
91 mean_v[i] = mean_v [wf.size() - _sample_size -1];
92 sigma_v[i] = sigma_v[wf.size() - _sample_size -1];
96 float best_sigma = 1.1e9;
97 size_t best_sigma_index = 0;
98 size_t num_good_adc = 0;
100 for(
size_t i=0; i<sigma_v.size(); ++i) {
102 auto const&
mean = mean_v[i];
106 auto const& sigma = sigma_v[i];
107 if(sigma < best_sigma) {
109 best_sigma_index = i;
116 if( num_good_adc < 1 ) {
117 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal at all..." << std::endl;
122 if(best_sigma >
_max_sigma || num_good_adc < 3) {
123 for(
size_t i=0; i<mean_v.size(); ++i) {
124 mean_v[i] = mean_v.at ( best_sigma_index );
125 sigma_v[i] = sigma_v.at ( best_sigma_index );
134 unsigned nbins = 1000;
149 int last_good_index = -1;
151 for(
size_t i=0; i < wf.size(); ++i) {
153 auto const mean = mean_v[i];
154 auto const sigma = sigma_v[i];
161 if(last_good_index<0) {
162 last_good_index = (int)i;
166 if( ( last_good_index + 1 ) < (int) i ) {
168 auto diff = fabs(mean_v.at(last_good_index) -
mean);
170 if ( diff > diff_cutoff) {
172 float slope = (
mean - mean_v.at(last_good_index)) / (
float(i - last_good_index));
174 for(
size_t j = last_good_index + 1; j < i; ++j) {
175 mean_v.at(j) = slope * ( float(j - last_good_index) ) + mean_v.at(last_good_index);
176 sigma_v.at(j) = mode_sigma;
185 auto diff_pre = fabs(presample_mean - mode_mean);
186 auto diff_post = fabs(postsample_mean - mode_mean);
188 auto constant_mean = diff_pre <= diff_post ? presample_mean : postsample_mean;
190 for(
size_t j = last_good_index + 1; j < i; ++j) {
192 mean_v.at(j) = constant_mean;
193 sigma_v.at(j) = mode_sigma;
197 last_good_index = (int)i;
207 if(sigma_v.front() > mode_sigma) {
209 int first_index = -1;
210 int second_index = -1;
212 for(
size_t i=0; i < wf.size(); ++i) {
213 if( sigma_v.at(i) < mode_sigma ) {
214 if( first_index < 0 ) first_index = (int)i;
215 else if( second_index < 0 ) {
216 second_index = (int)i;
222 if(first_index < 0 || second_index < 0) {
223 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal for CDF" 225 <<
"first_index: " << first_index <<
"\n" 226 <<
"second_index: " << second_index <<
"\n" 227 <<
"If one of these is less than zero, this means there is pulse\n" 228 <<
"on first sample and baseline never went back down. Returning false here.";
233 auto diff = fabs(mean_v.at(first_index) - mean_v.at(second_index));
235 if ( diff > diff_cutoff) {
237 float slope = (mean_v.at(second_index) - mean_v.at(first_index)) / (
float(second_index - first_index));
239 for(
int j=0; j < first_index; ++j) {
240 mean_v.at(j) = mean_v.at(first_index) - slope * (first_index - j);
248 for(
int j=0; j < first_index; ++j) {
250 mean_v.at(j) = postsample_mean;
251 sigma_v.at(j) = mode_sigma;
258 if(sigma_v.back() > mode_sigma) {
260 int first_index = -1;
261 int second_index = -1;
263 for(
int i = wf.size()-1; i >= 0; --i) {
264 if(sigma_v.at(i) < mode_sigma) {
265 if( second_index < 0 ) second_index = (int)i;
266 else if( first_index < 0 ) {
267 first_index = (int)i;
273 if(first_index < 0 || second_index < 0) {
274 std::cerr <<
"\033[93m<<" << __FUNCTION__ <<
">>\033[00m Could not find good pedestal for CDF" 276 <<
"first_index: " << first_index <<
"\n" 277 <<
"second_index: " << second_index <<
"\n" 278 <<
"If one of these is less than zero, this means there is pulse\n" 279 <<
"on the last sample and baseline never went back down. Returning false here.";
284 auto diff = fabs(mean_v.at(second_index) - mean_v.at(first_index) );
286 if ( diff > diff_cutoff) {
288 float slope = (mean_v.at(second_index) - mean_v.at(first_index)) / (
float(second_index - first_index));
289 for(
int j = second_index+1; j < int(wf.size()); ++j) {
290 mean_v.at(j) = mean_v.at(second_index) + slope * (j-second_index);
299 for(
int j = second_index+1; j < int(wf.size()); ++j) {
301 mean_v.at(j) = presample_mean;
302 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
virtual ~PedAlgoRollingMean()
Default destructor.