LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
RFFHitFitter.cxx
Go to the documentation of this file.
1 
15 #include "RFFHitFitter.h"
16 #include "cetlib_except/exception.h"
17 #include <cmath>
18 #include <iostream>
19 
20 hit::RFFHitFitter::RFFHitFitter(float step, float max) : fGEAlg(step, max) {}
21 
23  unsigned int min_multi,
24  float threshold,
25  float step,
26  float max)
27  : fGEAlg(step, max)
28 {
29  SetFitterParams(max_mean, min_multi, threshold);
30 }
31 
32 void hit::RFFHitFitter::SetFitterParams(float max_mean, unsigned int min_multi, float threshold)
33 {
34  fMeanMatchThreshold = max_mean;
35  fMinMergeMultiplicity = min_multi;
36 
38 
39  fFinalAmpThreshold = threshold;
40 
41  ClearResults();
42 }
43 
44 void hit::RFFHitFitter::RunFitter(const std::vector<float>& signal)
45 {
46  ClearResults();
49  CalculateMergedMeansAndSigmas(signal.size());
50  CalculateAmplitudes(signal);
51 }
52 
53 void hit::RFFHitFitter::CalculateAllMeansAndSigmas(const std::vector<float>& signal)
54 {
55  if (signal.size() <= 2) return;
56 
57  float prev_dev = 0, this_dev = 0;
58  ;
59  float slope = 0;
60  float sigma = 0;
61  float intercept = 0;
62  float mean = 0;
63 
64  for (size_t i_tick = 1; i_tick < signal.size() - 1; i_tick++) {
65  this_dev = 0.5 * (signal[i_tick + 1] - signal[i_tick - 1]) / signal[i_tick];
66  slope = this_dev - prev_dev;
67 
68  prev_dev = this_dev;
69 
70  if (slope >= 0) continue;
71 
72  sigma = std::sqrt(-1 / slope);
73  intercept = 0.5 * (signal[i_tick + 1] - signal[i_tick - 1]) / signal[i_tick] - slope * i_tick;
74  mean = -1 * intercept / slope;
75 
76  fSignalSet.insert(std::make_pair(mean, sigma));
77  }
78 }
79 
81 {
82  fMergeVector.clear();
83  fMergeVector.reserve(fSignalSet.size());
84 
85  float prev_mean = -9e6;
86  for (std::multiset<MeanSigmaPair>::iterator it = fSignalSet.begin(); it != fSignalSet.end();
87  it++) {
88  if (std::abs(it->first - prev_mean) > fMeanMatchThreshold || fMergeVector.size() == 0)
90  else
91  fMergeVector.back().push_back(it);
92  prev_mean = it->first;
93  }
94 }
95 
97 {
98  fMeanVector.reserve(fMergeVector.size());
99  fSigmaVector.reserve(fMergeVector.size());
100  fMeanErrorVector.reserve(fMergeVector.size());
101  fSigmaErrorVector.reserve(fMergeVector.size());
102 
103  for (size_t i_col = 0; i_col < fMergeVector.size(); i_col++) {
104  if (fMergeVector[i_col].size() < fMinMergeMultiplicity) continue;
105 
106  fMeanVector.push_back(0.0);
107  fSigmaVector.push_back(0.0);
108 
109  for (auto const& sigpair : fMergeVector[i_col]) {
110  fMeanVector.back() += sigpair->first;
111  fSigmaVector.back() += sigpair->second;
112  }
113 
114  fMeanVector.back() /= fMergeVector[i_col].size();
115  fSigmaVector.back() /= fMergeVector[i_col].size();
116 
117  if (fMeanVector.back() < 0 || fMeanVector.back() > signal_size - 1) {
118  fMeanVector.pop_back();
119  fSigmaVector.pop_back();
120  continue;
121  }
122 
123  fMeanErrorVector.push_back(0.0);
124  fSigmaErrorVector.push_back(0.0);
125 
126  for (auto const& sigpair : fMergeVector[i_col]) {
127  fMeanErrorVector.back() +=
128  (sigpair->first - fMeanVector.back()) * (sigpair->first - fMeanVector.back());
129  fSigmaErrorVector.back() +=
130  (sigpair->second - fSigmaVector.back()) * (sigpair->second - fSigmaVector.back());
131  }
132 
133  fMeanErrorVector.back() = std::sqrt(fMeanErrorVector.back()) / fMergeVector[i_col].size();
134  fSigmaErrorVector.back() = std::sqrt(fSigmaErrorVector.back()) / fMergeVector[i_col].size();
135  }
136 }
137 
138 void hit::RFFHitFitter::CalculateAmplitudes(const std::vector<float>& signal)
139 {
140  std::vector<float> heightVector(fMeanVector.size());
141  size_t bin = 0;
142 
143  for (size_t i = 0; i < fMeanVector.size(); i++) {
144  if (fMeanVector[i] < 0)
145  bin = 0;
146  else if (fMeanVector[i] + 1 > signal.size())
147  bin = signal.size() - 2;
148  else
149  bin = std::floor(fMeanVector[i]);
150 
151  if (bin >= signal.size() - 1)
152  throw cet::exception("RFFHitFitter")
153  << "Error in CalculatAmplitudes! bin is out of range!\n"
154  << "\tFor element " << i << " bin is " << bin << "(" << fMeanVector[i] << ")"
155  << " but size is " << signal.size() << ".\n";
156 
157  heightVector[i] = signal[bin] - (fMeanVector[i] - (float)bin) * (signal[bin] - signal[bin + 1]);
158  }
159 
161 
162  while (HitsBelowThreshold()) {
163  for (size_t i = 0; i < fAmpVector.size(); i++) {
164  if (fAmpVector[i] < fFinalAmpThreshold) {
165  fMeanVector.erase(fMeanVector.begin() + i);
166  fMeanErrorVector.erase(fMeanErrorVector.begin() + i);
167  fSigmaVector.erase(fSigmaVector.begin() + i);
168  fSigmaErrorVector.erase(fSigmaErrorVector.begin() + i);
169  fAmpVector.erase(fAmpVector.begin() + i);
170  heightVector.erase(heightVector.begin() + i);
171  }
172  }
174  }
175 
176  fAmpErrorVector.resize(fAmpVector.size(), 0.0);
177 }
178 
180 {
181  for (auto const& amp : fAmpVector)
182  if (amp < fFinalAmpThreshold) return true;
183  return false;
184 }
185 
187 {
188  fMeanVector.clear();
189  fSigmaVector.clear();
190  fMeanErrorVector.clear();
191  fSigmaErrorVector.clear();
192  fAmpVector.clear();
193  fAmpErrorVector.clear();
194  fSignalSet.clear();
195  fMergeVector.clear();
196 }
197 
199 {
200  std::cout << "InitialSignalSet" << std::endl;
201 
202  for (auto const& sigpair : fSignalSet)
203  std::cout << "\t" << sigpair.first << " / " << sigpair.second << std::endl;
204 
205  std::cout << "\nNHits = " << NHits() << std::endl;
206  std::cout << "\tMean / Sigma / Amp" << std::endl;
207  for (size_t i = 0; i < NHits(); i++)
208  std::cout << "\t" << fMeanVector[i] << " +- " << fMeanErrorVector[i] << " / " << fSigmaVector[i]
209  << " +- " << fSigmaErrorVector[i] << " / " << fAmpVector[i] << " +- "
210  << fAmpErrorVector[i] << std::endl;
211 }
intermediate_table::iterator iterator
std::vector< float > fAmpErrorVector
Definition: RFFHitFitter.h:71
unsigned int fMinMergeMultiplicity
Definition: RFFHitFitter.h:58
void RunFitter(const std::vector< float > &signal)
void SetFitterParams(float, unsigned int, float)
std::vector< std::vector< std::multiset< MeanSigmaPair >::iterator > > fMergeVector
Definition: RFFHitFitter.h:74
constexpr auto abs(T v)
Returns the absolute value of the argument.
void CalculateMergedMeansAndSigmas(std::size_t signal_size)
std::vector< float > fSigmaVector
Definition: RFFHitFitter.h:67
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
void CalculateAllMeansAndSigmas(const std::vector< float > &signal)
util::GaussianEliminationAlg fGEAlg
Definition: RFFHitFitter.h:64
float fFinalAmpThreshold
Definition: RFFHitFitter.h:59
const std::vector< float > & SolveEquations(const std::vector< float > &meanVector, const std::vector< float > &sigmaVector, const std::vector< float > &heightVector)
std::multiset< MeanSigmaPair, SignalSetComp > fSignalSet
Definition: RFFHitFitter.h:73
float bin[41]
Definition: plottest35.C:14
std::vector< float > fAmpVector
Definition: RFFHitFitter.h:70
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
RFFHitFitter(float, unsigned int, float, float step=0.1, float max=5.0)
void CalculateAmplitudes(const std::vector< float > &signal)
std::vector< float > fSigmaErrorVector
Definition: RFFHitFitter.h:69
float fMeanMatchThreshold
Definition: RFFHitFitter.h:57
unsigned int NHits()
Definition: RFFHitFitter.h:50
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< float > fMeanVector
Definition: RFFHitFitter.h:66
std::vector< float > fMeanErrorVector
Definition: RFFHitFitter.h:68