LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util::SignalShaping Class Reference

#include "SignalShaping.h"

Public Member Functions

 SignalShaping ()
 
virtual ~SignalShaping ()
 
const std::vector< double > & Response () const
 
const std::vector< double > & Response_save () const
 
const std::vector< TComplex > & ConvKernel () const
 
const std::vector< TComplex > & Filter () const
 
const std::vector< TComplex > & DeconvKernel () const
 
template<class T >
void Convolute (std::vector< T > &func) const
 
template<class T >
void Deconvolute (std::vector< T > &func) const
 
void Reset ()
 
void save_response ()
 
void set_normflag (bool flag)
 
void AddResponseFunction (const std::vector< double > &resp, bool ResetResponse=false)
 
void ShiftResponseTime (double ticks)
 
void SetPeakResponseTime (double tick)
 
void AddFilterFunction (const std::vector< TComplex > &filt)
 
void SetDeconvKernelPolarity (int pol)
 
void LockResponse () const
 
void CalculateDeconvKernel () const
 

Private Attributes

bool fResponseLocked
 
bool fFilterLocked
 
std::vector< double > fResponse
 
std::vector< double > fResponse_save
 
std::vector< TComplex > fConvKernel
 
std::vector< TComplex > fFilter
 
std::vector< TComplex > fDeconvKernel
 
int fDeconvKernelPolarity
 
bool fNorm
 

Detailed Description

Definition at line 69 of file SignalShaping.h.

Constructor & Destructor Documentation

util::SignalShaping::SignalShaping ( )

Definition at line 18 of file SignalShaping.cxx.

util::SignalShaping::~SignalShaping ( )
virtual

Definition at line 23 of file SignalShaping.cxx.

23 {}

Member Function Documentation

void util::SignalShaping::AddFilterFunction ( const std::vector< TComplex > &  filt)

Definition at line 153 of file SignalShaping.cxx.

References fFilter, fFilterLocked, util::LArFFT::FFTSize(), filt, and n.

Referenced by set_normflag().

154 {
155  // Make sure configuration is not locked.
156 
157  if (fFilterLocked) throw cet::exception("SignalShaping") << "Configuration locked.\n";
158 
159  // Get FFT service.
160 
162 
163  // If this is the first filter function, just copy the filter function.
164  // Otherwise, update the overall filter function.
165 
166  if (fFilter.size() == 0) {
167  fFilter = filt;
168  fFilter.resize(fft->FFTSize() / 2 + 1);
169  }
170  else {
171  unsigned int n = std::min(fFilter.size(), filt.size());
172  for (unsigned int i = 0; i < n; ++i)
173  fFilter[i] *= filt[i];
174  for (unsigned int i = n; i < fFilter.size(); ++i)
175  fFilter[i] = 0.;
176  }
177 }
std::vector< TComplex > fFilter
int FFTSize() const
Definition: LArFFT.h:66
Char_t n[5]
TString filt[ntarg]
Definition: Style.C:28
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaping::AddResponseFunction ( const std::vector< double > &  resp,
bool  ResetResponse = false 
)

Definition at line 53 of file SignalShaping.cxx.

References util::LArFFT::DoFFT(), util::LArFFT::DoInvFFT(), fConvKernel, util::LArFFT::FFTSize(), fResponse, and fResponseLocked.

Referenced by set_normflag().

54 {
55  // Make sure configuration is not locked.
56 
57  if (fResponseLocked) throw cet::exception("SignalShaping") << "Configuration locked.\n";
58 
59  // Get FFT service.
60 
62  int nticks = fft->FFTSize();
63 
64  // Copy new response function into fResponse attribute, and pad or
65  // truncate to correct size.
66 
67  fResponse = resp;
68  fResponse.resize(nticks, 0.);
69 
70  // Is this the first response function?
71 
72  if (fConvKernel.size() == 0 || ResetResponse) {
73 
74  // This is the first response function.
75  // Just calculate the fourier transform.
76 
77  fConvKernel.resize(nticks / 2 + 1);
79  }
80  else {
81 
82  // Not the first response function.
83  // Calculate the fourier transform of new response function.
84 
85  std::vector<TComplex> kern(nticks / 2 + 1);
86  fft->DoFFT(fResponse, kern);
87 
88  // Update overall convolution kernel.
89 
90  if (kern.size() != fConvKernel.size()) {
91  throw cet::exception("SignalShaping") << __func__ << ": inconsistent kernel size, "
92  << kern.size() << " vs. " << fConvKernel.size();
93  }
94  for (unsigned int i = 0; i < kern.size(); ++i)
95  fConvKernel[i] *= kern[i];
96 
97  // Recalculate overall response function.
98 
100  }
101 }
std::vector< TComplex > fConvKernel
void DoFFT(std::vector< T > &input, std::vector< TComplex > &output)
Definition: LArFFT.h:95
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
Definition: LArFFT.h:117
int FFTSize() const
Definition: LArFFT.h:66
std::vector< double > fResponse
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaping::CalculateDeconvKernel ( ) const

Definition at line 233 of file SignalShaping.cxx.

References util::abs(), util::LArFFT::DoInvFFT(), fConvKernel, fDeconvKernel, fDeconvKernelPolarity, fFilter, fFilterLocked, util::LArFFT::FFTSize(), fNorm, fResponse, LockResponse(), and n.

Referenced by Deconvolute(), and set_normflag().

234 {
235  // Make sure configuration is not locked.
236 
237  if (fFilterLocked) throw cet::exception("SignalShaping") << "Configuration locked.\n";
238 
239  // Lock response configuration.
240 
241  LockResponse();
242 
243  // Get FFT service.
244 
246 
247  // Make sure filter function has been configured.
248 
249  if (fFilter.size() == 0)
250  throw cet::exception("SignalShaping") << "Filter function has not been configured.\n";
251 
252  // Make sure filter function has the correct size.
253  // (Should always be the case if we get here.)
254 
255  unsigned int n = fft->FFTSize();
256  if (2 * (fFilter.size() - 1) != n)
257  if (fFilter.size() != fConvKernel.size()) {
258  throw cet::exception("SignalShaping") << __func__ << ": inconsistent size, " << fFilter.size()
259  << " vs. " << fConvKernel.size() << "\n";
260  }
261 
262  // Calculate deconvolution kernel as the ratio of the
263  // filter function and the convolution kernel.
264 
266  for (unsigned int i = 0; i < fDeconvKernel.size(); ++i) {
267  if (std::abs(fConvKernel[i].Re()) <= 0.0001 && std::abs(fConvKernel[i].Im()) <= 0.0001) {
268  fDeconvKernel[i] = 0.;
269  }
270  else {
271  fDeconvKernel[i] /= fConvKernel[i];
272  }
273  }
274 
275  // Normalize the deconvolution kernel.
276 
277  // Calculate the unnormalized deconvoluted response
278  // (inverse FFT of filter function).
279 
280  std::vector<double> deconv(n, 0.);
281  fft->DoInvFFT(const_cast<std::vector<TComplex>&>(fFilter), deconv);
282 
283  if (fNorm) {
284  // Find the peak value of the response
285  // Should normally be at zero, but don't assume that.
286  // Use DeconvKernelPolairty to find what to normalize to
287  double peak_response = 0;
288  if (fDeconvKernelPolarity == -1) peak_response = 4096;
289  for (unsigned int i = 0; i < fResponse.size(); ++i) {
290  if ((fResponse[i] > peak_response) and (fDeconvKernelPolarity == 1))
291  peak_response = fResponse[i];
292  else if ((fResponse[i] < peak_response) and (fDeconvKernelPolarity == -1))
293  peak_response = fResponse[i];
294  }
295  if (fDeconvKernelPolarity == -1) peak_response *= -1;
296  if (peak_response <= 0.) {
297  throw cet::exception("SignalShaping")
298  << __func__ << ": peak should always be positive (got " << peak_response << ")\n";
299  }
300 
301  // Find the peak value of the deconvoluted response
302  // Should normally be at zero, but don't assume that.
303 
304  double peak_deconv = 0.;
305  for (unsigned int i = 0; i < deconv.size(); ++i) {
306  if (deconv[i] > peak_deconv) peak_deconv = deconv[i];
307  }
308  if (peak_deconv <= 0.) {
309  throw cet::exception("SignalShaping")
310  << __func__ << ": deconvolution peak should always be positive (got " << peak_deconv
311  << ")\n";
312  }
313 
314  // Multiply the deconvolution kernel by a factor such that
315  // (Peak of response) = (Peak of deconvoluted response).
316 
317  double ratio = peak_response / peak_deconv;
318  for (unsigned int i = 0; i < fDeconvKernel.size(); ++i)
319  fDeconvKernel[i] *= ratio;
320  }
321  // Set the lock flag.
322 
323  fFilterLocked = true;
324 }
std::vector< TComplex > fConvKernel
constexpr auto abs(T v)
Returns the absolute value of the argument.
void LockResponse() const
std::vector< TComplex > fFilter
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
Definition: LArFFT.h:117
int FFTSize() const
Definition: LArFFT.h:66
std::vector< TComplex > fDeconvKernel
std::vector< double > fResponse
Char_t n[5]
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
const std::vector<TComplex>& util::SignalShaping::ConvKernel ( ) const
inline

Definition at line 78 of file SignalShaping.h.

References fConvKernel.

78 { return fConvKernel; }
std::vector< TComplex > fConvKernel
template<class T >
void util::SignalShaping::Convolute ( std::vector< T > &  func) const
inline

Definition at line 171 of file SignalShaping.h.

References util::LArFFT::Convolute(), fConvKernel, util::LArFFT::FFTSize(), fResponseLocked, LockResponse(), and n.

Referenced by DeconvKernel().

172 {
173  // Make sure response configuration is locked.
175 
177 
178  // Make sure that time series has the correct size.
179  if (int const n = func.size(); n != fft->FFTSize())
180  throw cet::exception("SignalShaping") << "Bad time series size = " << n << "\n";
181 
182  fft->Convolute(func, const_cast<std::vector<TComplex>&>(fConvKernel));
183 }
std::vector< TComplex > fConvKernel
void LockResponse() const
int FFTSize() const
Definition: LArFFT.h:66
void Convolute(std::vector< T > &input, std::vector< T > &respFunc)
Definition: LArFFT.h:170
Char_t n[5]
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
const std::vector<TComplex>& util::SignalShaping::DeconvKernel ( ) const
inline

Definition at line 80 of file SignalShaping.h.

References Convolute(), Deconvolute(), fDeconvKernel, and Reset().

80 { return fDeconvKernel; }
std::vector< TComplex > fDeconvKernel
template<class T >
void util::SignalShaping::Deconvolute ( std::vector< T > &  func) const
inline

Definition at line 188 of file SignalShaping.h.

References CalculateDeconvKernel(), util::LArFFT::Convolute(), fDeconvKernel, fFilterLocked, util::LArFFT::FFTSize(), and n.

Referenced by DeconvKernel().

189 {
190  // Make sure deconvolution kernel is configured.
192 
194 
195  // Make sure that time series has the correct size.
196  if (int const n = func.size(); n != fft->FFTSize())
197  throw cet::exception("SignalShaping") << "Bad time series size = " << n << "\n";
198 
199  fft->Convolute(func, fDeconvKernel);
200 }
int FFTSize() const
Definition: LArFFT.h:66
void CalculateDeconvKernel() const
void Convolute(std::vector< T > &input, std::vector< T > &respFunc)
Definition: LArFFT.h:170
std::vector< TComplex > fDeconvKernel
Char_t n[5]
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
const std::vector<TComplex>& util::SignalShaping::Filter ( ) const
inline

Definition at line 79 of file SignalShaping.h.

References fFilter.

79 { return fFilter; }
std::vector< TComplex > fFilter
void util::SignalShaping::LockResponse ( ) const

Definition at line 197 of file SignalShaping.cxx.

References fConvKernel, util::LArFFT::FFTSize(), fResponse, fResponseLocked, and n.

Referenced by CalculateDeconvKernel(), Convolute(), and set_normflag().

198 {
199  // Do nothing if the response is already locked.
200 
201  if (!fResponseLocked) {
202 
203  // Get FFT service.
204 
206 
207  // Make sure response has been configured.
208 
209  if (fResponse.size() == 0)
210  throw cet::exception("SignalShaping") << "Response has not been configured.\n";
211 
212  // Make sure response and convolution kernel have the correct
213  // size (should always be the case if we get here).
214 
215  unsigned int n = fft->FFTSize();
216  if (fResponse.size() != n)
217  throw cet::exception("SignalShaping")
218  << __func__ << ": inconsistent kernel size, " << fResponse.size() << " vs. " << n << "\n";
219  if (2 * (fConvKernel.size() - 1) != n)
220  throw cet::exception("SignalShaping")
221  << __func__ << ": unexpected FFT size, " << n << " vs. expected "
222  << (2 * (fConvKernel.size() - 1)) << "\n";
223 
224  // Set the lock flag.
225 
226  fResponseLocked = true;
227  }
228 }
std::vector< TComplex > fConvKernel
int FFTSize() const
Definition: LArFFT.h:66
std::vector< double > fResponse
Char_t n[5]
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaping::Reset ( )

Definition at line 39 of file SignalShaping.cxx.

References fConvKernel, fDeconvKernel, fDeconvKernelPolarity, fFilter, fFilterLocked, fResponse, and fResponseLocked.

Referenced by DeconvKernel().

40 {
41  fResponseLocked = false;
42  fFilterLocked = false;
43  fResponse.clear();
44  fConvKernel.clear();
45  fFilter.clear();
46  fDeconvKernel.clear();
47  //Set deconvolution polarity to + as default
49 }
std::vector< TComplex > fConvKernel
std::vector< TComplex > fFilter
std::vector< TComplex > fDeconvKernel
std::vector< double > fResponse
const std::vector<double>& util::SignalShaping::Response ( ) const
inline

Definition at line 76 of file SignalShaping.h.

References fResponse.

76 { return fResponse; }
std::vector< double > fResponse
const std::vector<double>& util::SignalShaping::Response_save ( ) const
inline

Definition at line 77 of file SignalShaping.h.

References fResponse_save.

77 { return fResponse_save; }
std::vector< double > fResponse_save
void util::SignalShaping::save_response ( )
inline

Definition at line 100 of file SignalShaping.h.

References fResponse, and fResponse_save.

101  {
102  fResponse_save.clear();
104  }
std::vector< double > fResponse
std::vector< double > fResponse_save
void util::SignalShaping::set_normflag ( bool  flag)
inline
void util::SignalShaping::SetDeconvKernelPolarity ( int  pol)

Definition at line 181 of file SignalShaping.cxx.

References fDeconvKernelPolarity.

Referenced by set_normflag().

182 {
183 
184  if ((pol != 1) and (pol != -1)) {
185  throw cet::exception("SignalShaping")
186  << __func__ << ": DeconvKernelPolarity should be +1 or -1 (got " << pol
187  << "). Setting to +1\n";
189  }
190 
191  else
192  fDeconvKernelPolarity = pol;
193 }
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaping::SetPeakResponseTime ( double  tick)

Definition at line 127 of file SignalShaping.cxx.

References util::LArFFT::FFTSize(), fResponse, fResponseLocked, util::LArFFT::PeakCorrelation(), and ShiftResponseTime().

Referenced by set_normflag().

128 {
129  // Make sure configuration is not locked.
130 
131  if (fResponseLocked) throw cet::exception("SignalShaping") << "Configuration locked.\n";
132 
133  // Get FFT service.
134 
136 
137  // Construct a delta-function response centered at tick zero.
138 
139  std::vector<double> delta(fft->FFTSize(), 0.);
140  delta[0] = 1.;
141 
142  // Figure out peak of current overall response.
143 
144  double peak = fft->PeakCorrelation(delta, fResponse);
145 
146  // Shift peak response to desired tick.
147 
148  ShiftResponseTime(tick - peak);
149 }
T PeakCorrelation(std::vector< T > &shape1, std::vector< T > &shape2)
Definition: LArFFT.h:270
int FFTSize() const
Definition: LArFFT.h:66
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:73
void ShiftResponseTime(double ticks)
std::vector< double > fResponse
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaping::ShiftResponseTime ( double  ticks)

Definition at line 106 of file SignalShaping.cxx.

References util::LArFFT::DoInvFFT(), fConvKernel, fResponse, fResponseLocked, and util::LArFFT::ShiftData().

Referenced by set_normflag(), and SetPeakResponseTime().

107 {
108  // Make sure configuration is not locked.
109 
110  if (fResponseLocked) throw cet::exception("SignalShaping") << "Configuration locked.\n";
111 
112  // Get FFT service.
113 
115 
116  // Update convolution kernel.
117 
118  fft->ShiftData(fConvKernel, ticks);
119 
120  // Recalculate overall response functiion.
121 
123 }
void ShiftData(std::vector< TComplex > &input, double shift)
Definition: LArFFT.cc:116
std::vector< TComplex > fConvKernel
tick ticks
Alias for common language habits.
Definition: electronics.h:76
void DoInvFFT(std::vector< TComplex > &input, std::vector< T > &output)
Definition: LArFFT.h:117
std::vector< double > fResponse
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33

Member Data Documentation

std::vector<TComplex> util::SignalShaping::fConvKernel
private
std::vector<TComplex> util::SignalShaping::fDeconvKernel
mutableprivate

Definition at line 155 of file SignalShaping.h.

Referenced by CalculateDeconvKernel(), DeconvKernel(), Deconvolute(), and Reset().

int util::SignalShaping::fDeconvKernelPolarity
private

Definition at line 160 of file SignalShaping.h.

Referenced by CalculateDeconvKernel(), Reset(), and SetDeconvKernelPolarity().

std::vector<TComplex> util::SignalShaping::fFilter
private

Definition at line 152 of file SignalShaping.h.

Referenced by AddFilterFunction(), CalculateDeconvKernel(), Filter(), and Reset().

bool util::SignalShaping::fFilterLocked
mutableprivate

Definition at line 142 of file SignalShaping.h.

Referenced by AddFilterFunction(), CalculateDeconvKernel(), Deconvolute(), and Reset().

bool util::SignalShaping::fNorm
private

Definition at line 163 of file SignalShaping.h.

Referenced by CalculateDeconvKernel(), and set_normflag().

std::vector<double> util::SignalShaping::fResponse
private
std::vector<double> util::SignalShaping::fResponse_save
private

Definition at line 146 of file SignalShaping.h.

Referenced by Response_save(), and save_response().

bool util::SignalShaping::fResponseLocked
mutableprivate

The documentation for this class was generated from the following files: