All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SharedWaveformBaseline.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/PMT/Algorithms/SharedWaveformBaseline.h
3  * @brief Extracts and writes PMT waveform baselines.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date May 5, 2022
6  * @see icaruscode/PMT/Algorithms/SharedWaveformBaseline.cxx
7  */
8 
9 #ifndef ICARUSCODE_PMT_ALGORITHMS_SHAREDWAVEFORMBASELINE_H
10 #define ICARUSCODE_PMT_ALGORITHMS_SHAREDWAVEFORMBASELINE_H
11 
12 
13 // LArSoft libraries
15 
16 // C/C++ standard libraries
17 #include <vector>
18 #include <utility> // std::move()
19 #include <string>
20 #include <ostream>
21 #include <cstdint> // std::size_t
22 
23 
24 // -----------------------------------------------------------------------------
25 namespace opdet { class SharedWaveformBaseline; }
26 /**
27  * @class opdet::SharedWaveformBaseline
28  * @brief Extracts a common baseline from waveforms.
29  *
30  * This algorithm processes a group of waveforms at a time, and returns a common
31  * baseline for them.
32  * The baseline is learned by looking at a fixed size of the beginning of each
33  * of the waveforms, as follows:
34  *
35  * 1. the RMS of the first portion of each baseline is computed;
36  * 2. the median of the samples on the same samples is also computed;
37  * 3. an acceptance range is constructed, using the median of the sample as the
38  * center and `nRMS` times the median of the RMS as maximum distance from
39  * that center in either direction;
40  * 4. as a second pass, if in the first portion of a waveform there are at least
41  * `nExcessSamples` samples in a row that are outside of the acceptance
42  * range, that waveform is excluded;
43  * 5. all the samples in the first portion of the remaining waveforms are
44  * averaged to obtain the final estimation of the baseline; this last step
45  * should increase the resolution of the baseline beyond the median that was
46  * obtained at step 2.
47  *
48  * The parameters are specified at algorithm construction time and are contained
49  * in the `Params_t` object.
50  *
51  */
53  public:
54 
55  /// Algorithm configuration parameters.
56  struct Params_t {
57 
58  std::size_t nSample; ///< Number of samples to use from each waveform.
59 
60  double nRMS; ///< Number of RMS from the baseline to discard a waveform.
61 
62  /// Number of samples out of range to discard a waveform.
63  unsigned int nExcessSamples;
64 
65  /// Dumps this configuration into the output stream `out`.
66  template <typename Stream>
67  void dump(
68  Stream& out,
69  std::string const& indent, std::string const& firstIndent
70  ) const;
71  template <typename Stream>
72  void dump(Stream& out, std::string const& indent = "") const
73  { dump(out, indent, indent); }
74 
75  }; // Params_t
76 
77 
78  /// Type for algorithm result.
79  struct BaselineInfo_t {
80 
81  /// Magic value used to denote the lack of a (`double`) data item.
82  static constexpr double NoInfo = std::numeric_limits<double>::max();
83 
84  /// Value of the baseline [ADC#]
85  double baseline = NoInfo;
86 
87  /// The RMS found during the extraction.
88  double RMS = NoInfo;
89 
90  /// Number of waveforms used for the extraction.
91  unsigned int nWaveforms = 0U;
92 
93  /// Number of samples used for the extraction.
94  unsigned int nSamples = 0U;
95 
96  }; // BaselineInfo_t
97 
98 
99  SharedWaveformBaseline(Params_t params, std::string logCategory):
100  fParams{ std::move(params) }
101  , fLogCategory{ std::move(logCategory) }
102  {}
103 
104  /// Returns a common baseline from all the specified waveforms.
105  BaselineInfo_t operator()
106  (std::vector<raw::OpDetWaveform const*> const& waveforms) const;
107 
108  /// Returns the set of configuration parameters of this algorithm.
109  Params_t const& parameters() const { return fParams; }
110 
111  private:
112  Params_t fParams; ///< Algorithm parameters.
113 
114  std::string fLogCategory; ///< Name of stream category for console messages.
115 
116 }; // opdet::SharedWaveformBaseline
117 
118 
119 //------------------------------------------------------------------------------
120 namespace opdet {
121 
122  inline std::ostream& operator<<
123  (std::ostream& out, SharedWaveformBaseline::Params_t const& params)
124  { params.dump(out); return out; }
125 
126 } // namespace opdet
127 
128 
129 //------------------------------------------------------------------------------
130 //--- Template implementation
131 //------------------------------------------------------------------------------
132 template <typename Stream>
134  Stream& out,
135  std::string const& indent, std::string const& firstIndent
136  ) const
137 {
138  out << firstIndent << "samples from each waveforms: " << nSample
139  << "\n" << indent << "pedestal range: +/- " << nRMS << " x RMS"
140  << "\n" << indent << "use only waveforms with less than "
141  << nExcessSamples << " samples out of pedestal range"
142  ;
143 } // opdet::SharedWaveformBaseline::Params_t::dump()
144 
145 
146 //------------------------------------------------------------------------------
147 
148 #endif // ICARUSCODE_PMT_ALGORITHMS_SHAREDWAVEFORMBASELINE_H
Params_t const & parameters() const
Returns the set of configuration parameters of this algorithm.
double RMS
The RMS found during the extraction.
std::size_t nSample
Number of samples to use from each waveform.
unsigned int nSamples
Number of samples used for the extraction.
SharedWaveformBaseline(Params_t params, std::string logCategory)
void dump(Stream &out, std::string const &indent="") const
unsigned int nExcessSamples
Number of samples out of range to discard a waveform.
Algorithm configuration parameters.
std::string fLogCategory
Name of stream category for console messages.
Extracts a common baseline from waveforms.
static constexpr double NoInfo
Magic value used to denote the lack of a (double) data item.
void dump(Stream &out, std::string const &indent, std::string const &firstIndent) const
Dumps this configuration into the output stream out.
double nRMS
Number of RMS from the baseline to discard a waveform.
Params_t fParams
Algorithm parameters.
unsigned int nWaveforms
Number of waveforms used for the extraction.
double baseline
Value of the baseline [ADC#].
bnb BNB Stream