All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WaveformOperations.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/WaveformOperations.h
3  * @brief Operations on waveform samples.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date June 27, 2019
6  *
7  * This is a header-only library.
8  */
9 
10 #ifndef ICARUSALG_UTILITIES_WAVEFORMOPERATIONS_H
11 #define ICARUSALG_UTILITIES_WAVEFORMOPERATIONS_H
12 
13 
14 // C/C++ standard library
15 #include <type_traits> // std::enable_if_t
16 
17 
18 /**
19  * @brief Functions to manipulate waveform sample values.
20  *
21  * This namespace provides trivial functions to manage operations on waveform
22  * samples given a certain polarity.
23  *
24  *
25  */
26 namespace icarus::waveform_operations {
27 
28  namespace details {
29 
30  // -------------------------------------------------------------------------
31  template <typename Sample, int Polarity, typename = void>
32  struct FlipImpl;
33 
34  template <typename Sample, int Polarity>
35  struct FlipImpl<Sample, Polarity, std::enable_if_t<(Polarity > 0)>> {
36  static constexpr Sample transform(Sample sample) { return sample; }
37  };
38 
39  template <typename Sample, int Polarity>
40  struct FlipImpl<Sample, Polarity, std::enable_if_t<(Polarity < 0)>> {
41  static constexpr Sample transform(Sample sample) { return -sample; }
42  };
43 
44  template <int Polarity, typename Sample>
45  constexpr Sample flip(Sample sample)
46  { return FlipImpl<Sample, Polarity>::transform(sample); }
47 
48 
49  // -------------------------------------------------------------------------
50  /**
51  * @brief Operations on a waveform with a fixed baseline.
52  * @tparam Sample type of ADC counts in the waveform
53  * @tparam Transform transformation to apply on the samples
54  *
55  * This object provides several functions to operate on waveforms
56  * independently of their "polarity".
57  *
58  * It can be used as an object that stores the waveform baseline, in which
59  * case a few member functions provide operations with respect to that
60  * baseline. Static functions independent of the baseline are also offered.
61  *
62  * The type of operations offered here are:
63  * * shifts with respect to a baseline
64  * * distance from the baseline
65  * * relative comparisons
66  *
67  */
68  template <typename Sample, Sample Transform(Sample)>
69  struct WaveformTransformedOperations {
70 
71  using Sample_t = Sample; ///< Type of ADC samples.
72 
73  // --- BEGIN --- Constructors --------------------------------------------
74  /// Constructor: sets the baseline to `0`.
75  WaveformTransformedOperations() = default;
76 
77  /// Constructor: sets the `baseline`.
78  constexpr WaveformTransformedOperations(Sample_t baseline)
79  : fBaseline(baseline) {}
80 
81  // --- END --- Constructors ----------------------------------------------
82 
83 
84  // --- BEGIN --- Operations relative to the baseline ---------------------
85  /// @name Operations relative to the baseline
86  /// @{
87  /// Returns the current baseline.
88  Sample_t baseline() const { return fBaseline; }
89 
90  /// Shift (addition) of an offset `shift` to the baseline.
91  constexpr Sample_t shiftFromBaseline(Sample_t shift) const
92  { return shiftBy(fBaseline, shift); }
93 
94  /// Distance of `sample` from the baseline.
95  constexpr Sample_t subtractBaseline(Sample_t sample) const
96  { return subtractBaseline(sample, fBaseline); }
97 
98  /// @}
99  // --- END --- Operations relative to the baseline -----------------------
100 
101 
102  // --- BEGIN --- Operations ----------------------------------------------
103  /// @name Operations involving two samples.
104  /// @{
105 
106  /// Difference between two samples (`to - from`).
107  static constexpr Sample_t distance(Sample_t from, Sample_t to)
108  { return transform(to - from); }
109 
110  /// Shift (addition) of an offset `shift` to a baseline.
111  static constexpr Sample_t shiftBy(Sample_t baseline, Sample_t shift)
112  { return baseline + transform(shift); }
113 
114  /// Distance of `sample` from the baseline: just mnemonic for `distance`.
115  static constexpr Sample_t subtractBaseline
116  (Sample_t sample, Sample_t baseline)
117  { return distance(baseline, sample); }
118 
119  /// @}
120  // --- END --- Operations ------------------------------------------------
121 
122 
123  // --- BEGIN --- Comparisons ---------------------------------------------
124  /// @name Comparisons
125  /// @{
126 
127  static constexpr bool lessThan(Sample_t a, Sample_t b)
128  { return transform(a) < transform(b); }
129  static constexpr bool greaterThan(Sample_t a, Sample_t b)
130  { return transform(a) > transform(b); }
131  static constexpr bool noLessThan(Sample_t a, Sample_t b)
132  { return transform(a) >= transform(b); }
133  static constexpr bool noGreaterThan(Sample_t a, Sample_t b)
134  { return transform(a) <= transform(b); }
135 
136  /// @}
137  // --- END --- Comparisons -----------------------------------------------
138 
139 
140  private:
141 
142  Sample_t fBaseline { 0 }; ///< Waveform baseline [ADC counts]
143 
144  // this is just to make it explicit that this is constexpr.
145  static constexpr Sample_t transform(Sample_t sample)
146  { return Transform(sample); }
147 
148  }; // WaveformTransformedOperations
149 
150 
151  // -------------------------------------------------------------------------
152 
153 
154  } // namespace details
155 
156 
157  // ---------------------------------------------------------------------------
158  /**
159  * @brief Waveform operations of waveforms with specified polarity.
160  * @tparam Sample type of ADC count in the waveform
161  * @tparam Polarity whether the positive signal develops above the baseline
162  * (positive polarity, +1) or below it (negative polarity,
163  * -1)
164  * @see details::WaveformTransformedOperations
165  *
166  * This is an alias of `details::WaveformTransformedOperations`.
167  */
168  template <typename Sample, int Polarity>
169  using Operations = details::WaveformTransformedOperations
170  <Sample, details::flip<Polarity, Sample>>;
171 
172  /// Waveform operations for positive polarity waveforms.
173  template <typename Sample>
175 
176  /// Waveform operations for negative polarity waveforms.
177  template <typename Sample>
179 
180 
181  // ---------------------------------------------------------------------------
182 
183 
184 } // namespace icarus::waveform_operations
185 
186 
187 #endif // ICARUSALG_UTILITIES_WAVEFORMOPERATIONS_H
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
static constexpr Sample_t transform(Sample_t sample)
shift
Definition: fcl_checks.sh:26
process_name gaushit a
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
Operations< Sample,-1 > NegativePolarityOperations
Waveform operations for negative polarity waveforms.
typename std::enable_if< B, T >::type enable_if_t
Definition: json.hpp:2191
BEGIN_PROLOG baseline
details::WaveformTransformedOperations< Sample, details::flip< Polarity, Sample >> Operations
Waveform operations of waveforms with specified polarity.
static constexpr bool noGreaterThan(Sample_t a, Sample_t b)
bool greaterThan(CluLen c1, CluLen c2)
bool lessThan(CluLen c1, CluLen c2)
Sample_t fBaseline
Waveform baseline [ADC counts].
Operations< Sample,+1 > PositivePolarityOperations
Waveform operations for positive polarity waveforms.