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