LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
pmtana::PedAlgoRollingMean Class Referenceabstract

#include "PedAlgoRollingMean.h"

Inheritance diagram for pmtana::PedAlgoRollingMean:
pmtana::PMTPedestalBase

Public Member Functions

 PedAlgoRollingMean (const std::string name="PedRollingMean")
 Default constructor. More...
 
 PedAlgoRollingMean (const fhicl::ParameterSet &pset, const std::string name="PedRollingMean")
 Alternative ctor. More...
 
virtual ~PedAlgoRollingMean ()
 Default destructor. More...
 
const std::string & Name () const
 Name getter. More...
 
bool Evaluate (const pmtana::Waveform_t &wf)
 Method to compute a pedestal. More...
 
double Mean (size_t i) const
 Getter of the pedestal mean value. More...
 
const pmtana::PedestalMean_tMean () const
 Getter of the pedestal mean value. More...
 
double Sigma (size_t i) const
 Getter of the pedestal standard deviation. More...
 
const pmtana::PedestalSigma_tSigma () const
 Getter of the pedestal standard deviation. More...
 

Protected Member Functions

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. More...
 
virtual bool ComputePedestal (const ::pmtana::Waveform_t &wf, pmtana::PedestalMean_t &mean_v, pmtana::PedestalSigma_t &sigma_v)=0
 

Private Attributes

size_t _sample_size
 
float _max_sigma
 
float _ped_range_max
 
float _ped_range_min
 
double _threshold
 
double _diff_threshold
 
double _diff_adc_count
 
int _n_presamples
 

Detailed Description

A class that calculates pedestal mean & standard deviation (here and elsewhere called as "RMS").

Definition at line 30 of file PedAlgoRollingMean.h.

Constructor & Destructor Documentation

pmtana::PedAlgoRollingMean::PedAlgoRollingMean ( const std::string  name = "PedRollingMean")

Default constructor.

Definition at line 18 of file PedAlgoRollingMean.cxx.

19  : PMTPedestalBase(name)
20  //*****************************************************************
21  {
22  srand(static_cast<unsigned int>(time(0)));
23  }
PMTPedestalBase(std::string name="noname")
Default constructor.
pmtana::PedAlgoRollingMean::PedAlgoRollingMean ( const fhicl::ParameterSet pset,
const std::string  name = "PedRollingMean" 
)

Alternative ctor.

Definition at line 26 of file PedAlgoRollingMean.cxx.

References _diff_adc_count, _diff_threshold, _max_sigma, _n_presamples, _ped_range_max, _ped_range_min, _sample_size, _threshold, and fhicl::ParameterSet::get().

29  : PMTPedestalBase(name)
30  //############################################################
31  {
32 
33  _sample_size = pset.get<size_t>("SampleSize");
34  _max_sigma = pset.get<float> ("MaxSigma");
35  _ped_range_max = pset.get<float> ("PedRangeMax");
36  _ped_range_min = pset.get<float> ("PedRangeMin");
37 
38  // _range = pset.get<int> ("RandomRange");
39  // _divisions = pset.get<double>("RandomRangeDivisions");
40  _threshold = pset.get<double>("Threshold");
41  _diff_threshold = pset.get<double>("DiffBetweenGapsThreshold");
42  _diff_adc_count = pset.get<double>("DiffADCCounts");
43 
44  _n_presamples = pset.get<int>("NPrePostSamples");
45 
46  //_random_shift = pset.get<double>("RandomRangeShift");
47  // Random seed number generator
48  //srand(static_cast<unsigned int>(time(0)));
49  }
PMTPedestalBase(std::string name="noname")
Default constructor.
T get(std::string const &key) const
Definition: ParameterSet.h:231
pmtana::PedAlgoRollingMean::~PedAlgoRollingMean ( )
virtual

Default destructor.

Definition at line 52 of file PedAlgoRollingMean.cxx.

54  {}

Member Function Documentation

bool pmtana::PedAlgoRollingMean::ComputePedestal ( const pmtana::Waveform_t wf,
pmtana::PedestalMean_t mean_v,
pmtana::PedestalSigma_t sigma_v 
)
protected

Method to compute a pedestal of the input waveform using "nsample" ADC samples from "start" index.

Definition at line 57 of file PedAlgoRollingMean.cxx.

References _diff_adc_count, _diff_threshold, _max_sigma, _n_presamples, _ped_range_max, _sample_size, _threshold, pmtana::BinnedMaxOccurrence(), pmtana::edge_aware_mean(), pmtana::mean(), and pmtana::std().

61  {
62 
63  // parameters
64  if(wf.size()<=(_sample_size * 2))
65  return false;
66 
67  const size_t window_size = _sample_size*2;
68 
69  // middle mean
70  for(size_t i=0; i< wf.size(); ++i) {
71 
72  mean_v[i] = 0;
73  sigma_v[i] = 0;
74 
75  if( i < _sample_size || i >= (wf.size() - _sample_size) ) continue;
76 
77  mean_v[i] = mean (wf,i - _sample_size,window_size);
78  sigma_v[i] = std (wf,mean_v[i],i - _sample_size,window_size);
79  }
80 
81  // front mean
82  for(size_t i=0; i<_sample_size; ++i) {
83 
84  mean_v[i] = mean_v [_sample_size];
85  sigma_v[i] = sigma_v[_sample_size];
86 
87  }
88  // tail mean
89  for(size_t i=(wf.size() - _sample_size); i<wf.size(); ++i) {
90 
91  mean_v[i] = mean_v [wf.size() - _sample_size -1];
92  sigma_v[i] = sigma_v[wf.size() - _sample_size -1];
93 
94  }
95 
96  float best_sigma = 1.1e9;
97  size_t best_sigma_index = 0;
98  size_t num_good_adc = 0;
99 
100  for(size_t i=0; i<sigma_v.size(); ++i) {
101  // Only consider adcs which mean is in the allowed range
102  auto const& mean = mean_v[i];
103 
104  if( mean < _ped_range_min || mean > _ped_range_max ) continue;
105 
106  auto const& sigma = sigma_v[i];
107  if(sigma < best_sigma) {
108  best_sigma = sigma;
109  best_sigma_index = i;
110  }
111 
112  if(sigma < _max_sigma) num_good_adc += 1;
113  }
114 
115 
116  if( num_good_adc < 1 ) {
117  std::cerr << "\033[93m<<" << __FUNCTION__ << ">>\033[00m Could not find good pedestal at all..." << std::endl;
118  return false;
119  }
120 
121  // If not enough # of good mean indices, use the best guess within this waveform
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 );
126  }
127 
128  return true;
129  }
130 
131 
132  // Else do extrapolation, or random seed depending on what we find...
133 
134  unsigned nbins = 1000;
135 
137  const auto mode_mean = BinnedMaxOccurrence(mean_v ,nbins);
138  const auto mode_sigma = BinnedMaxOccurrence(sigma_v,nbins);
139 
140  //auto mode_mean = BinnedMaxTH1D(mean_v ,nbins);
141  //auto mode_sigma = BinnedMaxTH1D(sigma_v,nbins);
142 
143  //std::cout<<mode_mean<<" +/- "<<mode_sigma<<std::endl;
144 
145  _diff_threshold *= mode_sigma;
146 
148 
149  int last_good_index = -1;
150 
151  for(size_t i=0; i < wf.size(); ++i) {
152 
153  auto const mean = mean_v[i];
154  auto const sigma = sigma_v[i];
155 
156  // if(sigma <= _max_sigma && mean < _ped_range_max && mean > _ped_range_min) {
157  // not sure if this works well for basline that is "linear" seen by David K
158 
159  if(sigma <= _threshold * mode_sigma && fabs(mean - mode_mean) <= _threshold * mode_sigma) {
160 
161  if(last_good_index<0) {
162  last_good_index = (int)i;
163  continue;
164  }
165 
166  if( ( last_good_index + 1 ) < (int) i ) {
167 
168  auto diff = fabs(mean_v.at(last_good_index) - mean);
169 
170  if ( diff > diff_cutoff) {
171  //this should become generic interpolation function, for now lets leave.
172  float slope = (mean - mean_v.at(last_good_index)) / (float(i - last_good_index));
173 
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;
177  }
178  }
179  else {
180  //difference roughly the same lets fill with constant baseline
181 
182  auto presample_mean = edge_aware_mean(wf,last_good_index - _n_presamples, last_good_index);
183  auto postsample_mean = edge_aware_mean(wf,i, _n_presamples);
184 
185  auto diff_pre = fabs(presample_mean - mode_mean);
186  auto diff_post = fabs(postsample_mean - mode_mean);
187 
188  auto constant_mean = diff_pre <= diff_post ? presample_mean : postsample_mean;
189 
190  for(size_t j = last_good_index + 1; j < i; ++j) {
191  //mean_v.at(j) = floor( mean_v.at(last_good_index) ) + _random_shift + (double) ( rand() % _range) / _divisions;
192  mean_v.at(j) = constant_mean;
193  sigma_v.at(j) = mode_sigma;
194  }
195  }
196  }
197  last_good_index = (int)i;
198  }
199  }
200 
201 
202  // Next do extrapolation to the first and end (if needed)
203  // vic: for now we leave this i'm not sure if this really needs
204  // to be tuned until I can make unit test
205  // update: yes this needs work...
206 
207  if(sigma_v.front() > mode_sigma) {
208 
209  int first_index = -1;
210  int second_index = -1;
211 
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;
217  break;
218  }
219  }
220  }
221 
222  if(first_index < 0 || second_index < 0) {
223  std::cerr <<"\033[93m<<" << __FUNCTION__ << ">>\033[00m Could not find good pedestal for CDF"
224  << "\n"
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.";
229  return false;
230  }
231 
232 
233  auto diff = fabs(mean_v.at(first_index) - mean_v.at(second_index));
234 
235  if ( diff > diff_cutoff) {
236 
237  float slope = (mean_v.at(second_index) - mean_v.at(first_index)) / (float(second_index - first_index));
238 
239  for(int j=0; j < first_index; ++j) {
240  mean_v.at(j) = mean_v.at(first_index) - slope * (first_index - j);
241  sigma_v.at(j) = _max_sigma;
242  }
243 
244  } else {
245 
246  auto postsample_mean = edge_aware_mean(wf,first_index, first_index + _n_presamples);
247 
248  for(int j=0; j < first_index; ++j) {
249  //mean_v.at(j) = floor( mean_v.at(second_index) ) + _random_shift + (double) ( rand() % _range) / _divisions;
250  mean_v.at(j) = postsample_mean;
251  sigma_v.at(j) = mode_sigma;
252  }
253  }
254 
255  }
256 
257 
258  if(sigma_v.back() > mode_sigma) {
259 
260  int first_index = -1;
261  int second_index = -1;
262 
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;
268  break;
269  }
270  }
271  }
272 
273  if(first_index < 0 || second_index < 0) {
274  std::cerr <<"\033[93m<<" << __FUNCTION__ << ">>\033[00m Could not find good pedestal for CDF"
275  << "\n"
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.";
280  return false;
281  }
282 
283 
284  auto diff = fabs(mean_v.at(second_index) - mean_v.at(first_index) );
285 
286  if ( diff > diff_cutoff) {
287 
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);
291  sigma_v.at(j) = _max_sigma;
292  }
293 
294  }
295  else {
296 
297  auto presample_mean = edge_aware_mean(wf,first_index - _n_presamples, second_index);
298 
299  for(int j = second_index+1; j < int(wf.size()); ++j) {
300  //mean_v.at(j) = floor( mean_v.at(first_index) ) + _random_shift + (double) ( rand() % _range) / _divisions;
301  mean_v.at(j) = presample_mean;
302  sigma_v.at(j) = mode_sigma;
303  }
304  }
305  }
306 
307 
308  return true;
309 
310  }
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:44
double BinnedMaxOccurrence(const PedestalMean_t &mean_v, const size_t nbins)
Definition: UtilFunc.cxx:61
double edge_aware_mean(const std::vector< short > &wf, int start, int end)
Definition: UtilFunc.cxx:27
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:15
virtual bool pmtana::PMTPedestalBase::ComputePedestal ( const ::pmtana::Waveform_t wf,
pmtana::PedestalMean_t mean_v,
pmtana::PedestalSigma_t sigma_v 
)
protectedpure virtualinherited

Method to compute pedestal: mean and sigma array should be filled per ADC. The length of each array is guaranteed to be same.

Referenced by pmtana::PMTPedestalBase::Evaluate().

bool pmtana::PMTPedestalBase::Evaluate ( const pmtana::Waveform_t wf)
inherited

Method to compute a pedestal.

Definition at line 33 of file PMTPedestalBase.cxx.

References pmtana::PMTPedestalBase::_mean_v, pmtana::PMTPedestalBase::_sigma_v, and pmtana::PMTPedestalBase::ComputePedestal().

Referenced by pmtana::PedAlgoUB::ComputePedestal(), and pmtana::PulseRecoManager::Reconstruct().

35  {
36  _mean_v.resize(wf.size(),0);
37  _sigma_v.resize(wf.size(),0);
38 
39  for(size_t i=0; i<wf.size(); ++i)
40  _mean_v[i] = _sigma_v[i] = 0;
41 
42  const bool res = ComputePedestal(wf, _mean_v, _sigma_v);
43 
44  if(wf.size() != _mean_v.size())
45  throw OpticalRecoException("Internal error: computed pedestal mean array length changed!");
46  if(wf.size() != _sigma_v.size())
47  throw OpticalRecoException("Internal error: computed pedestal sigma array length changed!");
48 
49  return res;
50  }
pmtana::PedestalMean_t _mean_v
A variable holder for pedestal mean value.
virtual bool ComputePedestal(const ::pmtana::Waveform_t &wf, pmtana::PedestalMean_t &mean_v, pmtana::PedestalSigma_t &sigma_v)=0
pmtana::PedestalSigma_t _sigma_v
A variable holder for pedestal standard deviation.
double pmtana::PMTPedestalBase::Mean ( size_t  i) const
inherited

Getter of the pedestal mean value.

Definition at line 53 of file PMTPedestalBase.cxx.

References pmtana::PMTPedestalBase::_mean_v, and ss.

Referenced by pmtana::PedAlgoUB::ComputePedestal(), and pmtana::PulseRecoManager::Reconstruct().

55  {
56  if(i > _mean_v.size()) {
57  std::stringstream ss;
58  ss << "Invalid index: no pedestal mean exist @ " << i;
59  throw OpticalRecoException(ss.str());
60  }
61  return _mean_v[i];
62  }
Float_t ss
Definition: plot.C:23
pmtana::PedestalMean_t _mean_v
A variable holder for pedestal mean value.
const PedestalMean_t & pmtana::PMTPedestalBase::Mean ( ) const
inherited

Getter of the pedestal mean value.

Definition at line 77 of file PMTPedestalBase.cxx.

References pmtana::PMTPedestalBase::_mean_v.

79  { return _mean_v; }
pmtana::PedestalMean_t _mean_v
A variable holder for pedestal mean value.
const std::string & pmtana::PMTPedestalBase::Name ( ) const
inherited

Name getter.

Definition at line 28 of file PMTPedestalBase.cxx.

References pmtana::PMTPedestalBase::_name.

30  { return _name;}
std::string _name
Name.
double pmtana::PMTPedestalBase::Sigma ( size_t  i) const
inherited

Getter of the pedestal standard deviation.

Definition at line 65 of file PMTPedestalBase.cxx.

References pmtana::PMTPedestalBase::_sigma_v, and ss.

Referenced by pmtana::PedAlgoUB::ComputePedestal(), and pmtana::PulseRecoManager::Reconstruct().

67  {
68  if(i > _sigma_v.size()) {
69  std::stringstream ss;
70  ss << "Invalid index: no pedestal sigma exist @ " << i;
71  throw OpticalRecoException(ss.str());
72  }
73  return _sigma_v[i];
74  }
Float_t ss
Definition: plot.C:23
pmtana::PedestalSigma_t _sigma_v
A variable holder for pedestal standard deviation.
const PedestalSigma_t & pmtana::PMTPedestalBase::Sigma ( ) const
inherited

Getter of the pedestal standard deviation.

Definition at line 82 of file PMTPedestalBase.cxx.

References pmtana::PMTPedestalBase::_sigma_v.

84  { return _sigma_v; }
pmtana::PedestalSigma_t _sigma_v
A variable holder for pedestal standard deviation.

Member Data Documentation

double pmtana::PedAlgoRollingMean::_diff_adc_count
private

Definition at line 62 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().

double pmtana::PedAlgoRollingMean::_diff_threshold
private

Definition at line 61 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().

float pmtana::PedAlgoRollingMean::_max_sigma
private

Definition at line 54 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().

int pmtana::PedAlgoRollingMean::_n_presamples
private

Definition at line 64 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().

float pmtana::PedAlgoRollingMean::_ped_range_max
private

Definition at line 55 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().

float pmtana::PedAlgoRollingMean::_ped_range_min
private

Definition at line 56 of file PedAlgoRollingMean.h.

Referenced by PedAlgoRollingMean().

size_t pmtana::PedAlgoRollingMean::_sample_size
private

Definition at line 53 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().

double pmtana::PedAlgoRollingMean::_threshold
private

Definition at line 60 of file PedAlgoRollingMean.h.

Referenced by ComputePedestal(), and PedAlgoRollingMean().


The documentation for this class was generated from the following files: