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

#include "SignalShaper.h"

Public Member Functions

 SignalShaper (int fftsize, std::string fftopt)
 
virtual ~SignalShaper ()
 
const std::vector< double > & Response () const
 
const std::vector< double > & Response_save () const
 
const std::vector< std::complex< double > > & ConvKernel () const
 
const std::vector< std::complex< double > > & Filter () const
 
const std::vector< std::complex< double > > & 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< std::complex< double >> &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< std::complex< double > > fConvKernel
 
std::vector< std::complex< double > > fFilter
 
std::vector< std::complex< double > > fDeconvKernel
 
int fDeconvKernelPolarity
 
bool fNorm
 
int fFFTSize
 
const void * fPlan
 
const void * rPlan
 
std::unique_ptr< util::LArFFTWPlanfFFTPlan
 
std::unique_ptr< util::LArFFTWfFFT
 

Detailed Description

Definition at line 13 of file SignalShaper.h.

Constructor & Destructor Documentation

util::SignalShaper::SignalShaper ( int  fftsize,
std::string  fftopt 
)

Definition at line 8 of file SignalShaper.cxx.

9  : fResponseLocked(false)
10  , fFilterLocked(false)
11  , fNorm(true)
12  , fFFTSize(fftsize)
13  , fFFTPlan(new util::LArFFTWPlan(fftsize, fftopt))
14  , fFFT(new util::LArFFTW(fftsize, fFFTPlan->fPlan, fFFTPlan->rPlan, 0))
15 {}
std::unique_ptr< util::LArFFTWPlan > fFFTPlan
Definition: SignalShaper.h:106
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:107
util::SignalShaper::~SignalShaper ( )
virtual

Definition at line 20 of file SignalShaper.cxx.

20 {}

Member Function Documentation

void util::SignalShaper::AddFilterFunction ( const std::vector< std::complex< double >> &  filt)

Definition at line 127 of file SignalShaper.cxx.

References fFFTSize, fFilter, fFilterLocked, filt, and n.

Referenced by set_normflag().

128 {
129  // Make sure configuration is not locked.
130 
131  if (fFilterLocked) throw cet::exception("SignalShaper") << "Configuration locked.\n";
132 
133  // If this is the first filter function, just copy the filter function.
134  // Otherwise, update the overall filter function.
135 
136  if (fFilter.size() == 0) {
137  fFilter = filt;
138  fFilter.resize(fFFTSize / 2 + 1);
139  }
140  else {
141  unsigned int n = std::min(fFilter.size(), filt.size());
142  for (unsigned int i = 0; i < n; ++i)
143  fFilter[i] *= filt[i];
144  for (unsigned int i = n; i < fFilter.size(); ++i)
145  fFilter[i] = 0.;
146  }
147 }
Char_t n[5]
TString filt[ntarg]
Definition: Style.C:28
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:90
void util::SignalShaper::AddResponseFunction ( const std::vector< double > &  resp,
bool  ResetResponse = false 
)

Definition at line 38 of file SignalShaper.cxx.

References fConvKernel, fFFT, fFFTSize, fResponse, and fResponseLocked.

Referenced by set_normflag().

39 {
40  // Make sure configuration is not locked.
41 
42  if (fResponseLocked) throw cet::exception("SignalShaper") << "Configuration locked.\n";
43 
44  int nticks = fFFTSize;
45 
46  // Copy new response function into fResponse attribute, and pad or
47  // truncate to correct size.
48 
49  fResponse = resp;
50  fResponse.resize(nticks, 0.);
51 
52  // Is this the first response function?
53 
54  if (fConvKernel.size() == 0 || ResetResponse) {
55 
56  // This is the first response function.
57  // Just calculate the fourier transform.
58 
59  fConvKernel.resize(nticks / 2 + 1);
60  fFFT->DoFFT(fResponse, fConvKernel);
61  }
62  else {
63 
64  // Not the first response function.
65  // Calculate the fourier transform of new response function.
66 
67  std::vector<std::complex<double>> kern(nticks / 2 + 1);
68  fFFT->DoFFT(fResponse, kern);
69 
70  // Update overall convolution kernel.
71 
72  if (kern.size() != fConvKernel.size()) {
73  throw cet::exception("SignalShaper") << __func__ << ": inconsistent kernel size, "
74  << kern.size() << " vs. " << fConvKernel.size();
75  }
76  for (unsigned int i = 0; i < kern.size(); ++i)
77  fConvKernel[i] *= kern[i];
78 
79  // Recalculate overall response function.
80 
81  fFFT->DoInvFFT(fConvKernel, fResponse);
82  }
83 }
std::vector< double > fResponse
Definition: SignalShaper.h:83
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:107
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:87
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaper::CalculateDeconvKernel ( ) const

Definition at line 199 of file SignalShaper.cxx.

References util::abs(), fConvKernel, fDeconvKernel, fDeconvKernelPolarity, fFFT, fFFTSize, fFilter, fFilterLocked, fNorm, fResponse, LockResponse(), and n.

Referenced by set_normflag().

200 {
201  // Make sure configuration is not locked.
202 
203  if (fFilterLocked) throw cet::exception("SignalShaper") << "Configuration locked.\n";
204 
205  // Lock response configuration.
206 
207  LockResponse();
208 
209  // Make sure filter function has been configured.
210 
211  if (fFilter.size() == 0)
212  throw cet::exception("SignalShaper") << "Filter function has not been configured.\n";
213 
214  // Make sure filter function has the correct size.
215  // (Should always be the case if we get here.)
216 
217  unsigned int n = fFFTSize;
218  if (2 * (fFilter.size() - 1) != n)
219  if (fFilter.size() != fConvKernel.size()) {
220  throw cet::exception("SignalShaper") << __func__ << ": inconsistent size, " << fFilter.size()
221  << " vs. " << fConvKernel.size() << "\n";
222  }
223 
224  // Calculate deconvolution kernel as the ratio of the
225  // filter function and the convolution kernel.
226 
228  for (unsigned int i = 0; i < fDeconvKernel.size(); ++i) {
229  if (std::abs(fConvKernel[i].real()) <= 0.0001 && std::abs(fConvKernel[i].imag()) <= 0.0001) {
230  fDeconvKernel[i] = 0.;
231  }
232  else {
233  fDeconvKernel[i] /= fConvKernel[i];
234  }
235  }
236 
237  // Normalize the deconvolution kernel.
238 
239  // Calculate the unnormalized deconvoluted response
240  // (inverse FFT of filter function).
241 
242  std::vector<double> deconv(n, 0.);
243  fFFT->DoInvFFT(const_cast<std::vector<std::complex<double>>&>(fFilter), deconv);
244  //fFFT->DoInvFFT(fFilter, deconv);
245 
246  if (fNorm) {
247  // Find the peak value of the response
248  // Should normally be at zero, but don't assume that.
249  // Use DeconvKernelPolairty to find what to normalize to
250  double peak_response = 0;
251  if (fDeconvKernelPolarity == -1) peak_response = 4096;
252  for (unsigned int i = 0; i < fResponse.size(); ++i) {
253  if ((fResponse[i] > peak_response) and (fDeconvKernelPolarity == 1))
254  peak_response = fResponse[i];
255  else if ((fResponse[i] < peak_response) and (fDeconvKernelPolarity == -1))
256  peak_response = fResponse[i];
257  }
258  if (fDeconvKernelPolarity == -1) peak_response *= -1;
259  if (peak_response <= 0.) {
260  throw cet::exception("SignalShaper")
261  << __func__ << ": peak should always be positive (got " << peak_response << ")\n";
262  }
263 
264  // Find the peak value of the deconvoluted response
265  // Should normally be at zero, but don't assume that.
266 
267  double peak_deconv = 0.;
268  for (unsigned int i = 0; i < deconv.size(); ++i) {
269  if (deconv[i] > peak_deconv) peak_deconv = deconv[i];
270  }
271  if (peak_deconv <= 0.) {
272  throw cet::exception("SignalShaper")
273  << __func__ << ": deconvolution peak should always be positive (got " << peak_deconv
274  << ")\n";
275  }
276 
277  // Multiply the deconvolution kernel by a factor such that
278  // (Peak of response) = (Peak of deconvoluted response).
279 
280  double ratio = peak_response / peak_deconv;
281  for (unsigned int i = 0; i < fDeconvKernel.size(); ++i)
282  fDeconvKernel[i] *= ratio;
283  }
284 
285  // Set the lock flag.
286 
287  fFilterLocked = true;
288 }
constexpr auto abs(T v)
Returns the absolute value of the argument.
void LockResponse() const
std::vector< double > fResponse
Definition: SignalShaper.h:83
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:107
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:87
Char_t n[5]
std::vector< std::complex< double > > fDeconvKernel
Definition: SignalShaper.h:93
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:90
const std::vector<std::complex<double> >& util::SignalShaper::ConvKernel ( ) const
inline

Definition at line 22 of file SignalShaper.h.

References fConvKernel.

22 { return fConvKernel; }
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:87
template<class T >
void util::SignalShaper::Convolute ( std::vector< T > &  func) const

Referenced by DeconvKernel().

const std::vector<std::complex<double> >& util::SignalShaper::DeconvKernel ( ) const
inline

Definition at line 24 of file SignalShaper.h.

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

24 { return fDeconvKernel; }
std::vector< std::complex< double > > fDeconvKernel
Definition: SignalShaper.h:93
template<class T >
void util::SignalShaper::Deconvolute ( std::vector< T > &  func) const

Referenced by DeconvKernel().

const std::vector<std::complex<double> >& util::SignalShaper::Filter ( ) const
inline

Definition at line 23 of file SignalShaper.h.

References fFilter.

23 { return fFilter; }
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:90
void util::SignalShaper::LockResponse ( ) const

Definition at line 167 of file SignalShaper.cxx.

References fConvKernel, fFFTSize, fResponse, fResponseLocked, and n.

Referenced by CalculateDeconvKernel(), and set_normflag().

168 {
169  // Do nothing if the response is already locked.
170 
171  if (!fResponseLocked) {
172 
173  // Make sure response has been configured.
174 
175  if (fResponse.size() == 0)
176  throw cet::exception("SignalShaper") << "Response has not been configured.\n";
177 
178  // Make sure response and convolution kernel have the correct
179  // size (should always be the case if we get here).
180 
181  unsigned int n = fFFTSize;
182  if (fResponse.size() != n)
183  throw cet::exception("SignalShaper")
184  << __func__ << ": inconsistent kernel size, " << fResponse.size() << " vs. " << n << "\n";
185  if (2 * (fConvKernel.size() - 1) != n)
186  throw cet::exception("SignalShaper")
187  << __func__ << ": unexpected FFT size, " << n << " vs. expected "
188  << (2 * (fConvKernel.size() - 1)) << "\n";
189 
190  // Set the lock flag.
191 
192  fResponseLocked = true;
193  }
194 }
std::vector< double > fResponse
Definition: SignalShaper.h:83
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:87
Char_t n[5]
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaper::Reset ( )

Definition at line 24 of file SignalShaper.cxx.

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

Referenced by DeconvKernel().

25 {
26  fResponseLocked = false;
27  fFilterLocked = false;
28  fResponse.clear();
29  fConvKernel.clear();
30  fFilter.clear();
31  fDeconvKernel.clear();
32  //Set deconvolution polarity to + as default
34 }
std::vector< double > fResponse
Definition: SignalShaper.h:83
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:87
std::vector< std::complex< double > > fDeconvKernel
Definition: SignalShaper.h:93
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:90
const std::vector<double>& util::SignalShaper::Response ( ) const
inline

Definition at line 20 of file SignalShaper.h.

References fResponse.

20 { return fResponse; }
std::vector< double > fResponse
Definition: SignalShaper.h:83
const std::vector<double>& util::SignalShaper::Response_save ( ) const
inline

Definition at line 21 of file SignalShaper.h.

References fResponse_save.

21 { return fResponse_save; }
std::vector< double > fResponse_save
Definition: SignalShaper.h:84
void util::SignalShaper::save_response ( )
inline

Definition at line 41 of file SignalShaper.h.

References fResponse, and fResponse_save.

42  {
43  fResponse_save.clear();
45  }
std::vector< double > fResponse_save
Definition: SignalShaper.h:84
std::vector< double > fResponse
Definition: SignalShaper.h:83
void util::SignalShaper::set_normflag ( bool  flag)
inline
void util::SignalShaper::SetDeconvKernelPolarity ( int  pol)

Definition at line 151 of file SignalShaper.cxx.

References fDeconvKernelPolarity.

Referenced by set_normflag().

152 {
153 
154  if ((pol != 1) and (pol != -1)) {
155  throw cet::exception("SignalShaper")
156  << __func__ << ": DeconvKernelPolarity should be +1 or -1 (got " << pol
157  << "). Setting to +1\n";
159  }
160 
161  else
162  fDeconvKernelPolarity = pol;
163 }
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaper::SetPeakResponseTime ( double  tick)

Definition at line 105 of file SignalShaper.cxx.

References fFFT, fFFTSize, fResponse, fResponseLocked, and ShiftResponseTime().

Referenced by set_normflag().

106 {
107  // Make sure configuration is not locked.
108 
109  if (fResponseLocked) throw cet::exception("SignalShaper") << "Configuration locked.\n";
110 
111  // Construct a delta-function response centered at tick zero.
112 
113  std::vector<double> delta(fFFTSize, 0.);
114  delta[0] = 1.;
115 
116  // Figure out peak of current overall response.
117 
118  double peak = fFFT->PeakCorrelation(delta, fResponse);
119 
120  // Shift peak response to desired tick.
121 
122  ShiftResponseTime(tick - peak);
123 }
void ShiftResponseTime(double ticks)
std::vector< double > fResponse
Definition: SignalShaper.h:83
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:107
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:73
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void util::SignalShaper::ShiftResponseTime ( double  ticks)

Definition at line 88 of file SignalShaper.cxx.

References fConvKernel, fFFT, fResponse, and fResponseLocked.

Referenced by set_normflag(), and SetPeakResponseTime().

89 {
90  // Make sure configuration is not locked.
91 
92  if (fResponseLocked) throw cet::exception("SignalShaper") << "Configuration locked.\n";
93 
94  // Update convolution kernel.
95 
96  fFFT->ShiftData(fConvKernel, ticks);
97 
98  // Recalculate overall response functiion.
99 
100  fFFT->DoInvFFT(fConvKernel, fResponse);
101 }
tick ticks
Alias for common language habits.
Definition: electronics.h:76
std::vector< double > fResponse
Definition: SignalShaper.h:83
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:107
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:87
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33

Member Data Documentation

std::vector<std::complex<double> > util::SignalShaper::fConvKernel
private
std::vector<std::complex<double> > util::SignalShaper::fDeconvKernel
mutableprivate

Definition at line 93 of file SignalShaper.h.

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

int util::SignalShaper::fDeconvKernelPolarity
private

Definition at line 98 of file SignalShaper.h.

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

std::unique_ptr<util::LArFFTW> util::SignalShaper::fFFT
private
std::unique_ptr<util::LArFFTWPlan> util::SignalShaper::fFFTPlan
private

Definition at line 106 of file SignalShaper.h.

int util::SignalShaper::fFFTSize
private
std::vector<std::complex<double> > util::SignalShaper::fFilter
private

Definition at line 90 of file SignalShaper.h.

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

bool util::SignalShaper::fFilterLocked
mutableprivate

Definition at line 80 of file SignalShaper.h.

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

bool util::SignalShaper::fNorm
private

Definition at line 101 of file SignalShaper.h.

Referenced by CalculateDeconvKernel(), and set_normflag().

const void* util::SignalShaper::fPlan
private

Definition at line 104 of file SignalShaper.h.

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

Definition at line 84 of file SignalShaper.h.

Referenced by Response_save(), and save_response().

bool util::SignalShaper::fResponseLocked
mutableprivate
const void* util::SignalShaper::rPlan
private

Definition at line 105 of file SignalShaper.h.


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