LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
UtilFunc.cxx
Go to the documentation of this file.
1 #include "UtilFunc.h"
2 #include "OpticalRecoException.h"
4 
5 #include <algorithm>
6 #include <cmath>
7 #include <numeric>
8 
9 #include "TH1D.h"
10 
11 namespace pmtana {
12 
13  double mean(const std::vector<short>& wf, size_t start, size_t nsample)
14  {
15  if (!nsample) nsample = wf.size();
16  if (start > wf.size() || (start + nsample) > wf.size())
17  throw OpticalRecoException("Invalid start/end index!");
18 
19  double sum =
20  std::accumulate(wf.begin() + start, wf.begin() + start + nsample, 0.0) / ((double)nsample);
21 
22  return sum;
23  }
24 
25  double edge_aware_mean(const std::vector<short>& wf, int start, int end)
26  {
27 
28  auto m = double{0.0};
29  auto n_t = unsigned{0};
30 
31  for (int k = start; k < end; ++k) {
32  if (k < 0 or k > (int)(wf.size()) - 1) continue;
33  m += wf.at(k);
34  ++n_t;
35  }
36 
37  if (n_t > 0) m /= n_t;
38  n_t = 0;
39 
40  return m;
41  }
42 
43  double std(const std::vector<short>& wf, const double ped_mean, size_t start, size_t nsample)
44  {
45  if (!nsample) nsample = wf.size();
46  if (start > wf.size() || (start + nsample) > wf.size())
47  throw OpticalRecoException("Invalid start/end index!");
48 
49  double sigma = 0;
50 
51  for (size_t index = start; index < (start + nsample); ++index)
52 
53  sigma += pow((wf[index] - ped_mean), 2);
54 
55  sigma = sqrt(sigma / ((double)(nsample)));
56 
57  return sigma;
58  }
59 
60  double BinnedMaxOccurrence(const PedestalMean_t& mean_v, const size_t nbins)
61  {
62  if (nbins < 1) throw OpticalRecoException("Cannot have 0 binning");
63 
64  auto res = std::minmax_element(std::begin(mean_v), std::end(mean_v));
65 
66  double bin_width = ((*res.second) - (*res.first)) / ((double)nbins);
67 
68  if (nbins == 1 || bin_width == 0) return ((*res.first) + bin_width / 2.);
69 
70  //std::cout<<"Min: "<<(*res.first)<<" Max: "<<(*res.second)<<" Width: "<<bin_width<<std::endl;
71 
72  // Construct array of nbins
73  static std::vector<size_t> ctr_v(nbins, 0);
74  for (auto& v : ctr_v)
75  v = 0;
76  for (auto const& v : mean_v) {
77 
78  size_t index = int((v - (*res.first)) / bin_width);
79  //std::cout<<"adc = "<<v<<" width = "<<bin_width<< " ... "
80  //<<index<<" / "<<ctr_v.size()<<std::endl;
81 
82  ctr_v[index]++;
83  }
84 
85  // Find max occurrence
86  auto max_it = std::max_element(std::begin(ctr_v), std::end(ctr_v));
87 
88  // Get the mean of max-occurrence bins
89  double mean_max_occurrence = 0;
90  double num_occurrence = 0;
91  for (size_t bin = 0; bin < ctr_v.size(); ++bin) {
92 
93  if (ctr_v[bin] != (*max_it)) continue;
94 
95  mean_max_occurrence += ((*res.first) + bin_width / 2. + bin_width * bin);
96 
97  num_occurrence += 1.0;
98  }
99 
100  return (mean_max_occurrence / num_occurrence);
101  }
102 
103  // template<typename W>
104  int sign(double val)
105  {
106 
107  if (val > 0) return 1;
108  if (val < 0) return -1;
109  return 0;
110  }
111 
112  double BinnedMaxTH1D(const std::vector<double>& v, int bins)
113  {
114 
115  auto max_it = std::max_element(std::begin(v), std::end(v));
116  auto min_it = std::min_element(std::begin(v), std::end(v));
117 
118  TH1D th("th", ";;", bins, *min_it, *max_it);
119 
120  for (const auto& m : v)
121  th.Fill(m);
122 
123  return th.GetXaxis()->GetBinCenter(th.GetMaximumBin());
124  }
125 
126 }
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:43
double BinnedMaxOccurrence(const PedestalMean_t &mean_v, const size_t nbins)
Definition: UtilFunc.cxx:60
double edge_aware_mean(const std::vector< short > &wf, int start, int end)
Definition: UtilFunc.cxx:25
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
float bin[41]
Definition: plottest35.C:14
int sign(double val)
Definition: UtilFunc.cxx:104
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Double_t sum
Definition: plot.C:31
std::vector< double > PedestalMean_t
double BinnedMaxTH1D(const std::vector< double > &v, int bins)
Definition: UtilFunc.cxx:112