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