All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes | List of all members
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::LArFFTWPlan
fFFTPlan
 
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 9 of file SignalShaper.cxx.

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

Definition at line 22 of file SignalShaper.cxx.

23 {}

Member Function Documentation

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

Definition at line 138 of file SignalShaper.cxx.

139 {
140  // Make sure configuration is not locked.
141 
142  if(fFilterLocked)
143  throw cet::exception("SignalShaper") << "Configuration locked.\n";
144 
145  // If this is the first filter function, just copy the filter function.
146  // Otherwise, update the overall filter function.
147 
148  if(fFilter.size() == 0) {
149  fFilter = filt;
150  fFilter.resize(fFFTSize / 2 + 1);
151  }
152  else {
153  unsigned int n = std::min(fFilter.size(), filt.size());
154  for(unsigned int i=0; i<n; ++i)
155  fFilter[i] *= filt[i];
156  for(unsigned int i=n; i<fFilter.size(); ++i)
157  fFilter[i] = 0.;
158  }
159 }
then filt
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:87
void util::SignalShaper::AddResponseFunction ( const std::vector< double > &  resp,
bool  ResetResponse = false 
)

Definition at line 43 of file SignalShaper.cxx.

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

Definition at line 213 of file SignalShaper.cxx.

214 {
215  // Make sure configuration is not locked.
216 
217  if(fFilterLocked)
218  throw cet::exception("SignalShaper") << "Configuration locked.\n";
219 
220  // Lock response configuration.
221 
222  LockResponse();
223 
224  // Make sure filter function has been configured.
225 
226  if(fFilter.size() == 0)
227  throw cet::exception("SignalShaper")
228  << "Filter function has not been configured.\n";
229 
230  // Make sure filter function has the correct size.
231  // (Should always be the case if we get here.)
232 
233  unsigned int n = fFFTSize;
234  if (2 * (fFilter.size() - 1) != n)
235  if (fFilter.size() != fConvKernel.size()) {
236  throw cet::exception("SignalShaper") << __func__ << ": inconsistent size, "
237  << fFilter.size() << " vs. " << fConvKernel.size() << "\n";
238  }
239 
240  // Calculate deconvolution kernel as the ratio of the
241  // filter function and the convolution kernel.
242 
244  for(unsigned int i=0; i<fDeconvKernel.size(); ++i) {
245  if(std::abs(fConvKernel[i].real()) <= 0.0001 && std::abs(fConvKernel[i].imag()) <= 0.0001) {
246  fDeconvKernel[i] = 0.;
247  }
248  else {
249  fDeconvKernel[i] /= fConvKernel[i];
250  }
251  }
252 
253  // Normalize the deconvolution kernel.
254 
255  // Calculate the unnormalized deconvoluted response
256  // (inverse FFT of filter function).
257 
258  std::vector<double> deconv(n, 0.);
259  fFFT->DoInvFFT(const_cast<std::vector<std::complex<double>>&>(fFilter), deconv);
260  //fFFT->DoInvFFT(fFilter, deconv);
261 
262  if (fNorm){
263  // Find the peak value of the response
264  // Should normally be at zero, but don't assume that.
265  // Use DeconvKernelPolairty to find what to normalize to
266  double peak_response = 0;
267  if ( fDeconvKernelPolarity == -1 )
268  peak_response = 4096;
269  for(unsigned int i = 0; i < fResponse.size(); ++i) {
270  if( (fResponse[i] > peak_response)
271  and (fDeconvKernelPolarity == 1))
272  peak_response = fResponse[i];
273  else if ( (fResponse[i] < peak_response)
274  and ( fDeconvKernelPolarity == -1) )
275  peak_response = fResponse[i];
276  }
277  if ( fDeconvKernelPolarity == -1 )
278  peak_response *= -1;
279  if (peak_response <= 0.) {
280  throw cet::exception("SignalShaper") << __func__
281  << ": peak should always be positive (got " << peak_response << ")\n";
282  }
283 
284  // Find the peak value of the deconvoluted response
285  // Should normally be at zero, but don't assume that.
286 
287  double peak_deconv = 0.;
288  for(unsigned int i = 0; i < deconv.size(); ++i) {
289  if(deconv[i] > peak_deconv)
290  peak_deconv = deconv[i];
291  }
292  if (peak_deconv <= 0.) {
293  throw cet::exception("SignalShaper") << __func__
294  << ": deconvolution peak should always be positive (got " << peak_deconv << ")\n";
295  }
296 
297  // Multiply the deconvolution kernel by a factor such that
298  // (Peak of response) = (Peak of deconvoluted response).
299 
300  double ratio = peak_response / peak_deconv;
301  for(unsigned int i = 0; i < fDeconvKernel.size(); ++i)
302  fDeconvKernel[i] *= ratio;
303  }
304 
305  // Set the lock flag.
306 
307  fFilterLocked = true;
308 }
void LockResponse() const
T abs(T value)
std::vector< double > fResponse
Definition: SignalShaper.h:80
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:104
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:84
std::vector< std::complex< double > > fDeconvKernel
Definition: SignalShaper.h:90
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:87
const std::vector<std::complex<double> >& util::SignalShaper::ConvKernel ( ) const
inline

Definition at line 23 of file SignalShaper.h.

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

Definition at line 25 of file SignalShaper.h.

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

Definition at line 24 of file SignalShaper.h.

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

Definition at line 180 of file SignalShaper.cxx.

181 {
182  // Do nothing if the response is already locked.
183 
184  if(!fResponseLocked) {
185 
186  // Make sure response has been configured.
187 
188  if(fResponse.size() == 0)
189  throw cet::exception("SignalShaper")
190  << "Response has not been configured.\n";
191 
192  // Make sure response and convolution kernel have the correct
193  // size (should always be the case if we get here).
194 
195  unsigned int n = fFFTSize;
196  if (fResponse.size() != n)
197  throw cet::exception("SignalShaper") << __func__ << ": inconsistent kernel size, "
198  << fResponse.size() << " vs. " << n << "\n";
199  if (2 * (fConvKernel.size() - 1) != n)
200  throw cet::exception("SignalShaper") << __func__ << ": unexpected FFT size, "
201  << n << " vs. expected " << (2 * (fConvKernel.size() - 1)) << "\n";
202 
203  // Set the lock flag.
204 
205  fResponseLocked = true;
206  }
207 }
std::vector< double > fResponse
Definition: SignalShaper.h:80
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:84
void util::SignalShaper::Reset ( )

Definition at line 28 of file SignalShaper.cxx.

29 {
30  fResponseLocked = false;
31  fFilterLocked = false;
32  fResponse.clear();
33  fConvKernel.clear();
34  fFilter.clear();
35  fDeconvKernel.clear();
36  //Set deconvolution polarity to + as default
38 }
std::vector< double > fResponse
Definition: SignalShaper.h:80
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:84
std::vector< std::complex< double > > fDeconvKernel
Definition: SignalShaper.h:90
std::vector< std::complex< double > > fFilter
Definition: SignalShaper.h:87
const std::vector<double>& util::SignalShaper::Response ( ) const
inline

Definition at line 21 of file SignalShaper.h.

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

Definition at line 22 of file SignalShaper.h.

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

Definition at line 41 of file SignalShaper.h.

std::vector< double > fResponse_save
Definition: SignalShaper.h:81
std::vector< double > fResponse
Definition: SignalShaper.h:80
void util::SignalShaper::set_normflag ( bool  flag)
inline

Definition at line 42 of file SignalShaper.h.

42 {fNorm = flag;}
then echo unknown compiler flag
void util::SignalShaper::SetDeconvKernelPolarity ( int  pol)

Definition at line 163 of file SignalShaper.cxx.

164 {
165 
166  if ( (pol != 1) and (pol != -1) ) {
167  throw cet::exception("SignalShaper") << __func__
168  << ": DeconvKernelPolarity should be +1 or -1 (got " << pol << "). Setting to +1\n";
170  }
171 
172  else
173  fDeconvKernelPolarity = pol;
174 
175 }
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
void util::SignalShaper::SetPeakResponseTime ( double  tick)

Definition at line 114 of file SignalShaper.cxx.

115 {
116  // Make sure configuration is not locked.
117 
118  if(fResponseLocked)
119  throw cet::exception("SignalShaper") << "Configuration locked.\n";
120 
121  // Construct a delta-function response centered at tick zero.
122 
123  std::vector<double> delta(fFFTSize, 0.);
124  delta[0] = 1.;
125 
126  // Figure out peak of current overall response.
127 
128  double peak = fFFT->PeakCorrelation(delta, fResponse);
129 
130  // Shift peak response to desired tick.
131 
132  ShiftResponseTime(tick - peak);
133 }
void ShiftResponseTime(double ticks)
std::vector< double > fResponse
Definition: SignalShaper.h:80
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:104
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
void util::SignalShaper::ShiftResponseTime ( double  ticks)

Definition at line 95 of file SignalShaper.cxx.

96 {
97  // Make sure configuration is not locked.
98 
99  if(fResponseLocked)
100  throw cet::exception("SignalShaper") << "Configuration locked.\n";
101 
102  // Update convolution kernel.
103 
104  fFFT->ShiftData(fConvKernel, ticks);
105 
106  // Recalculate overall response functiion.
107 
108  fFFT->DoInvFFT(fConvKernel, fResponse);
109 }
tick ticks
Alias for common language habits.
Definition: electronics.h:78
std::vector< double > fResponse
Definition: SignalShaper.h:80
std::unique_ptr< util::LArFFTW > fFFT
Definition: SignalShaper.h:104
std::vector< std::complex< double > > fConvKernel
Definition: SignalShaper.h:84

Member Data Documentation

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

Definition at line 84 of file SignalShaper.h.

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

Definition at line 90 of file SignalShaper.h.

int util::SignalShaper::fDeconvKernelPolarity
private

Definition at line 95 of file SignalShaper.h.

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

Definition at line 104 of file SignalShaper.h.

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

Definition at line 103 of file SignalShaper.h.

int util::SignalShaper::fFFTSize
private

Definition at line 100 of file SignalShaper.h.

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

Definition at line 87 of file SignalShaper.h.

bool util::SignalShaper::fFilterLocked
mutableprivate

Definition at line 77 of file SignalShaper.h.

bool util::SignalShaper::fNorm
private

Definition at line 98 of file SignalShaper.h.

const void* util::SignalShaper::fPlan
private

Definition at line 101 of file SignalShaper.h.

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

Definition at line 80 of file SignalShaper.h.

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

Definition at line 81 of file SignalShaper.h.

bool util::SignalShaper::fResponseLocked
mutableprivate

Definition at line 76 of file SignalShaper.h.

const void* util::SignalShaper::rPlan
private

Definition at line 102 of file SignalShaper.h.


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