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