LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
PedAlgoRmsSlider.cxx
Go to the documentation of this file.
1 //
3 // PedAlgoRmsSlider source
4 //
6 
7 #ifndef larana_OPTICALDETECTOR_PEDALGORMSSLIDER_CXX
8 #define larana_OPTICALDETECTOR_PEDALGORMSSLIDER_CXX
9 
10 #include "PedAlgoRmsSlider.h"
11 #include "OpticalRecoException.h"
12 #include "UtilFunc.h"
13 
14 #include <iostream>
15 #include <fstream>
16 #include <numeric>
17 
18 namespace pmtana{
19 
20  //*****************************************************************
21  PedAlgoRmsSlider::PedAlgoRmsSlider(const std::string name)
22  : PMTPedestalBase(name)
23  //*****************************************************************
24  {
25  srand(static_cast<unsigned int>(time(0)));
26  }
27 
28  //**************************************************************************
30  const std::string name)
31  : PMTPedestalBase(name)
32  //############################################################
33  {
34 
35  _sample_size = pset.get<size_t>("SampleSize", 7 );
36  _threshold = pset.get<double>("Threshold", 0.6 );
37  _max_sigma = pset.get<float> ("MaxSigma", 0.5 );
38  _ped_range_max = pset.get<float> ("PedRangeMax", 2150 );
39  _ped_range_min = pset.get<float> ("PedRangeMin", 100 );
40 
41  _verbose = pset.get<bool> ("Verbose", true );
42  _n_wf_to_csvfile = pset.get<int> ("NWaveformsToFile", 12 );
43 
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;
47  }
48 
49  }
50 
51  //*******************************************
53  //*******************************************
54  {}
55 
56  //*******************************************
58  //*******************************************
59  {
60  std::cout << "PedAlgoRmsSlider setting:"
61  << "\n\t SampleSize: " << _sample_size
62  << "\n\t Threshold: " << _threshold
63  << "\n\t Verbose: " << _verbose
64  << "\n\t NWaveformsToFile: " << _n_wf_to_csvfile << std::endl;
65  }
66 
67  //****************************************************************************
68  double PedAlgoRmsSlider::CalcMean(const std::vector<double>& wf, size_t start, size_t nsample)
69  //****************************************************************************
70  {
71  if(!nsample) nsample = wf.size();
72  if(start > wf.size() || (start+nsample) > wf.size())
73  throw OpticalRecoException("Invalid start/end index!");
74 
75  double sum = std::accumulate(wf.begin()+start,wf.begin()+start+nsample,0.0);
76 
77  sum /= ((double)nsample);
78 
79  return sum;
80  }
81 
82  //****************************************************************************
83  double PedAlgoRmsSlider::CalcStd(const std::vector<double>& wf, const double ped_mean, size_t start, size_t nsample)
84  //****************************************************************************
85  {
86  if(!nsample) nsample = wf.size();
87  if(start > wf.size() || (start+nsample) > wf.size())
88  throw OpticalRecoException("Invalid start/end index!");
89 
90  double sigma = 0;
91 
92  for(size_t index=start; index < (start+nsample); ++index){
93  sigma += pow( (wf[index] - ped_mean), 2 );
94  }
95 
96  sigma = sqrt(sigma/((double)(nsample)));
97 
98  return sigma;
99  }
100 
101  //****************************************************************************
103  pmtana::PedestalMean_t& mean_v,
104  pmtana::PedestalSigma_t& sigma_v)
105  //****************************************************************************
106  {
107 
108  if (_verbose)
109  this->PrintInfo();
110 
111  if (wf.size() <= (_sample_size * 2))
112  return false;
113 
114  // Prepare output
115  mean_v.resize (wf.size(), 0);
116  sigma_v.resize(wf.size(), 0);
117 
118 
119 
120 
121  // **********
122  // To start, set the pedestal equal to
123  // the wf itself
124  // **********
125 
126  pmtana::PedestalMean_t mean_temp_v;
127  mean_temp_v.resize( wf.size(), 0);
128 
129  for(size_t i=0; i< wf.size(); ++i) {
130  mean_temp_v[i] = wf[i];
131  sigma_v[i] = 0;
132  }
133 
134 
135 
136 
137 
138  // **********
139  // Now look for rms variations
140  // and change the mean and rms accordingly
141  // **********
142 
143  double local_mean, local_rms;
144 
145  int last_good_index = -1;
146  double last_local_mean = -1;
147  double last_local_rms = -1;
148 
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;
152 
153  for (size_t i = 0; i < wf.size() - _sample_size; i++) {
154 
155  local_mean = mean(wf, i, _sample_size);
156  local_rms = std(wf, local_mean, i, _sample_size);
157 
158  if(_verbose) std::cout << "\033[93mPedAlgoRmsSlider\033[00m: i " << i << " local_mean: " << local_mean << " local_rms: " << local_rms << std::endl;
159 
160  if (local_rms < _threshold) {
161 
162  if(_verbose)
163  std::cout << "\033[93mBelow threshold\033[00m: "
164  << "at i " << i
165  << " last good index was: " << last_good_index
166  << std::endl;
167 
168  if(last_good_index<0) {
169  last_good_index = (int)i;
170  last_local_mean = local_mean;
171  last_local_rms = local_rms;
172  continue;
173  }
174 
175 
176  if( ( last_good_index + 1 ) < (int)i ) {
177 
178  //this should become generic interpolation function, for now lets leave.
179  float slope = (local_mean - last_local_mean) / (float(i - last_good_index));
180 
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);
183  // for sigma, put either the sigma in the region before the pulse or
184  // after the pulse, depending on which one if != 0. If both are !=0 put the one after
185  // the pulse (just default), if both are zero then put zero
186  sigma_v.at(j) = (local_rms != 0 ? local_rms : last_local_rms); // todo: fluctuate baseline
187  ped_interapolated.at(j) = true;
188  }
189  }
190 
191  last_good_index = (int)i;
192  last_local_mean = local_mean;
193  last_local_rms = local_rms;
194  }
195 
196 
197  }
198 
199 
200 
201  // **********
202  // Now look at special cases, if wf starts or
203  // ends with a pulse
204  // **********
205 
206  // At start
207 
208  bool end_found = false;
209 
210  local_mean = mean(wf, 0, _sample_size);
211  local_rms = std(wf, local_mean, 0, _sample_size);
212 
213  if (local_rms >= _threshold) {
214 
215  for (size_t i = 1; i < wf.size() - _sample_size; i++) {
216 
217  local_mean = mean(wf, i, _sample_size);
218  local_rms = std(wf, local_mean, i, _sample_size);
219 
220  if (local_rms < _threshold) {
221 
222  end_found = true;
223 
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;
228  }
229  break;
230  }
231  }
232 
233  if (!end_found) {
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.";
236  return false;
237  }
238 
239  }
240 
241 
242  // At end
243 
244  bool start_found = false;
245 
246  local_mean = mean(wf, wf.size()-1-_sample_size, _sample_size);
247  local_rms = std(wf, local_mean, wf.size()-1-_sample_size, _sample_size);
248 
249  if (local_rms >= _threshold) {
250 
251  size_t i = wf.size() - 1 - _sample_size;
252  while (i-- > 0) {
253  local_mean = mean(wf, i, _sample_size);
254  local_rms = std(wf, local_mean, i, _sample_size);
255 
256  if (local_rms < _threshold) {
257 
258  start_found = true;
259 
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;
264  }
265  break;
266  }
267  }
268 
269  if (!start_found) {
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.";
272  return false;
273  }
274 
275  }
276 
277 
278 
279 
280 
281 
282 
283  // **********
284  // Now smooth it to estimate the final pedestal
285  // **********
286 
287  const size_t window_size = _sample_size*2;
288 
289  // middle mean
290  for(size_t i=0; i< mean_temp_v.size(); ++i) {
291 
292  if( i < _sample_size || i >= (wf.size() - _sample_size) ) continue;
293 
294  mean_v[i] = this->CalcMean (mean_temp_v,i - _sample_size,window_size);
295  if(!ped_interapolated.at(i)){
296  sigma_v[i] = this->CalcStd (mean_temp_v,mean_v[i],i - _sample_size,window_size);
297  }
298  }
299 
300  // front mean
301  for(size_t i=0; i<_sample_size; ++i) {
302 
303  mean_v[i] = mean_v [_sample_size];
304  if(!ped_interapolated.at(i)){
305  sigma_v[i] = sigma_v[_sample_size];
306  }
307  }
308 
309  // tail mean
310  for(size_t i=(mean_temp_v.size() - _sample_size); i<mean_temp_v.size(); ++i) {
311 
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];
315  }
316  }
317 
318 
319 
320 
321  // Save to file
322  if (_wf_saved + 1 <= _n_wf_to_csvfile) {
323  _wf_saved ++;
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;
326  }
327  }
328 
329 
330  bool is_sane = this->CheckSanity(mean_v, sigma_v);
331 
332  return is_sane;
333 
334  }
335 
336 
337 
338 
339 
340  //*******************************************
342  //*******************************************
343  {
344 
345  float best_sigma = 1.1e9;
346  size_t best_sigma_index = 0;
347  size_t num_good_adc = 0;
348 
349  for(size_t i=0; i<sigma_v.size(); ++i) {
350  // Only consider adcs which mean is in the allowed range
351  auto const& mean = mean_v[i];
352 
353  if( mean < _ped_range_min || mean > _ped_range_max ) continue;
354 
355  auto const& sigma = sigma_v[i];
356  if(sigma < best_sigma) {
357  best_sigma = sigma;
358  best_sigma_index = i;
359  }
360 
361  if(sigma < _max_sigma) num_good_adc += 1;
362  }
363 
364 
365  if( num_good_adc < 1 ) {
366  std::cerr << "\033[93m<<" << __FUNCTION__ << ">>\033[00m Could not find good pedestal at all..." << std::endl;
367  return false;
368  }
369 
370  // If not enough # of good mean indices, use the best guess within this waveform
371  if(best_sigma > _max_sigma || num_good_adc < 3) {
372 
373  if(_verbose) {
374  std::cout << "\033[93mPedAlgoRmsSlider\033[00m: Not enough number of good mean indices."
375  << "Using the best guess within this waveform."
376  << std::endl;
377  }
378 
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 );
382  }
383  }
384 
385  return true;
386 
387  }
388 }
389 
390 #endif
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)
Definition: UtilFunc.cxx:44
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 &#39;sane&#39;.
virtual ~PedAlgoRmsSlider()
Default destructor.
float _max_sigma
Max sigma to consider adc as &#39;sane&#39;.
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 &#39;sane&#39;.
T get(std::string const &key) const
Definition: ParameterSet.h:231
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)
Definition: UtilFunc.cxx:15
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.