SignalShaping.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file SignalShaping.h
4 ///
5 /// \brief Generic class for shaping signals on wires.
6 ///
7 /// \author H. Greenlee
8 ///
9 /// This is a generic class for shaping signals on wires during simulation
10 /// (convolution) and reconstruction (deconvolution).
11 ///
12 /// This class acts as a repository for a consistent set of convolution
13 /// and deconvolution kernels. It also supplies an interface for
14 /// convoluting either type of kernel with a time series signal. All
15 /// FFT type calculations are done using LArFFT service.
16 ///
17 /// This class has only a default constructor. Configuration must be done
18 /// externally by calling configuration methods. The proper method for
19 /// configuring this class is as follows.
20 ///
21 /// 1. Add one or more response functions using method AddReponseFunction.
22 /// 2. Optionally call methods SetPeakResponseTime or ShiftResponseTime.
23 /// 3. Add one or more filter functions using method AddFilterFunction.
24 /// 4. Call method CalculateDeconvKernel once.
25 ///
26 /// After the deconvolution kernel is calculated, the configuration is locked.
27 ///
28 /// Notes on time and frequency series functions
29 /// ---------------------------------------------
30 ///
31 /// Times and frequencies are measured in units of ticks and cycles/tick.
32 ///
33 /// Time series are represented as vector<double> of length N, representing
34 /// sampled times on interval [0,N) ticks. (N = LArFFT::FFTSize().)
35 ///
36 /// Frequency series are represented as vector<TComplex> of length (N/2+1),
37 /// representing sampled frequencies on interval [0, 1/2] cycles/tick.
38 /// Negative frequencies (not stored) are complex conjugate of
39 /// corresponding positive frequency.
40 ///
41 /// Update notes
42 /// -------------
43 ///
44 /// * Yun-Tse Tsai (yuntse@slac.stanford.edu), July 17th, 2014<br/>
45 /// Modify
46 /// `void AddResponseFunction(const std::vector<double>& resp);`
47 /// to
48 /// `void AddResponseFunction(const std::vector<double>& resp, bool ResetResponse = false );`
49 /// If you want to reset your response, `fResponse` in this object, you can
50 /// do
51 /// `AddResponseFunction( yourResponse, true )`
52 /// The other part involving `AddResponseFunction` shouldn't be affected.
53 /// * X. Qian 2015/01/06 <br/>
54 /// Add the time offset variable<br/>
55 /// Need to add the set and extraction code
56 ////////////////////////////////////////////////////////////////////////
57 
58 #ifndef SIGNALSHAPING_H
59 #define SIGNALSHAPING_H
60 
61 #include <vector>
62 #include "TComplex.h"
63 
66 
67 namespace util {
68 
70 public:
71 
72  // Constructor, destructor.
73  SignalShaping();
74  virtual ~SignalShaping();
75 
76  // Accessors.
77  const std::vector<double>& Response() const {return fResponse;}
78  const std::vector<double>& Response_save() const {return fResponse_save;}
79  const std::vector<TComplex>& ConvKernel() const {return fConvKernel;}
80  const std::vector<TComplex>& Filter() const {return fFilter;}
81  const std::vector<TComplex>& DeconvKernel() const {return fDeconvKernel;}
82  /* const int GetTimeOffset() const {return fTimeOffset;} */
83 
84  // Signal shaping methods.
85 
86  // Convolute a time series with convolution kernel.
87  template <class T> void Convolute(std::vector<T>& func) const;
88 
89  // Convolute a time series with deconvolution kernel.
90  template <class T> void Deconvolute(std::vector<T>& func) const;
91 
92 
93  // Configuration methods.
94 
95  // Only reset deconvolution
96  //void ResetDecon();
97  // Reset this class to default-constructed state.
98  void Reset();
99 
101  void set_normflag(bool flag){fNorm = flag;}
102 
103  // Add a time domain response function.
104  // Updates overall response function and convolution kernel.
105  void AddResponseFunction(const std::vector<double>& resp, bool ResetResponse = false );
106 
107  /* //X. Qian, set time offset */
108  /* void SetTimeOffset(const int time){fTimeOffset = time;} */
109 
110  // Shift response function in time.
111  // Updates overall response function and convolution kernel.
112  void ShiftResponseTime(double ticks);
113  void SetPeakResponseTime(double tick);
114 
115  // Add a filter function.
116  void AddFilterFunction(const std::vector<TComplex>& filt);
117 
118  //Add DeconvKernel Polarity switch to decide how to normalize
119  //deconvoluted signal w.r.t. RawDigits. If +1 then normalize
120  //to Max ADC, if -1 to Min ADC
121  void SetDeconvKernelPolarity(int pol);
122 
123  // Test and lock the current response function.
124  // Does not lock filter configuration.
125  void LockResponse() const;
126 
127  // Calculate deconvolution kernel using current convolution kernel
128  // and filter function.
129  // Fully locks configuration.
130  void CalculateDeconvKernel() const;
131 
132  private:
133 
134  // Attributes.
135  // unused double fMinConvKernelFrac; ///< minimum value of convKernel/peak for deconvolution
136 
137  // Lock flags.
138  mutable bool fResponseLocked;
139  mutable bool fFilterLocked;
140 
141  // Overall response.
142  std::vector<double> fResponse;
143  std::vector<double> fResponse_save;
144 
145  // Convolution kernel (fourier transform of response function).
146  std::vector<TComplex> fConvKernel;
147 
148  // Overall filter function.
149  std::vector<TComplex> fFilter;
150 
151  // Deconvolution kernel (= fFilter / fConvKernel).
152  mutable std::vector<TComplex> fDeconvKernel;
153 
154  // Deconvolution Kernel Polarity Flag
155  // Set to +1 if deconv signal should be deconv to + ADC count
156  // Set to -1 if one wants to normalize to - ADC count
158 
159  // Xin added */
160  bool fNorm;
161 };
162 
163 }
164 
165 //----------------------------------------------------------------------
166 // Convolute a time series with current response.
167 template <class T> inline void util::SignalShaping::Convolute(std::vector<T>& func) const
168 {
169  // Make sure response configuration is locked.
170  if(!fResponseLocked)
171  LockResponse();
172 
174 
175  // Make sure that time series has the correct size.
176  if(int const n = func.size(); n != fft->FFTSize())
177  throw cet::exception("SignalShaping") << "Bad time series size = " << n << "\n";
178 
179  fft->Convolute(func, const_cast<std::vector<TComplex>&>(fConvKernel));
180 }
181 
182 //----------------------------------------------------------------------
183 // Convolute a time series with deconvolution kernel.
184 template <class T> inline void util::SignalShaping::Deconvolute(std::vector<T>& func) const
185 {
186  // Make sure deconvolution kernel is configured.
187  if(!fFilterLocked)
189 
191 
192  // Make sure that time series has the correct size.
193  if(int const n = func.size(); n != fft->FFTSize())
194  throw cet::exception("SignalShaping") << "Bad time series size = " << n << "\n";
195 
196  fft->Convolute(func, fDeconvKernel);
197 }
198 
199 #endif
void set_normflag(bool flag)
const std::vector< double > & Response_save() const
Definition: SignalShaping.h:78
void Deconvolute(std::vector< T > &func) const
Namespace for general, non-LArSoft-specific utilities.
std::vector< TComplex > fConvKernel
const std::vector< double > & Response() const
Definition: SignalShaping.h:77
void AddResponseFunction(const std::vector< double > &resp, bool ResetResponse=false)
void LockResponse() const
std::vector< TComplex > fFilter
tick ticks
Alias for common language habits.
Definition: electronics.h:78
void Convolute(std::vector< T > &func) const
int FFTSize() const
Definition: LArFFT.h:69
void CalculateDeconvKernel() const
std::void_t< T > n
void SetDeconvKernelPolarity(int pol)
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
void Convolute(std::vector< T > &input, std::vector< T > &respFunc)
Definition: LArFFT.h:173
const std::vector< TComplex > & DeconvKernel() const
Definition: SignalShaping.h:81
void ShiftResponseTime(double ticks)
std::vector< TComplex > fDeconvKernel
const std::vector< TComplex > & Filter() const
Definition: SignalShaping.h:80
void SetPeakResponseTime(double tick)
def func()
Definition: docstring.py:7
std::vector< double > fResponse
std::vector< double > fResponse_save
const std::vector< TComplex > & ConvKernel() const
Definition: SignalShaping.h:79
void AddFilterFunction(const std::vector< TComplex > &filt)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33