OmniChannelNoiseDB.h
Go to the documentation of this file.
1 #ifndef WIRECELLSIGPROC_OMNICHANNELNOISEDB
2 #define WIRECELLSIGPROC_OMNICHANNELNOISEDB
3 
9 
10 #include "WireCellUtil/Waveform.h"
11 #include "WireCellUtil/Units.h"
12 #include "WireCellUtil/String.h"
14 #include "WireCellUtil/Logging.h"
15 
16 #include <vector>
17 #include <tuple>
18 #include <unordered_map>
19 #include <memory>
20 
21 namespace WireCell {
22  namespace SigProc {
23 
25  public:
26 
27  /// Create a configurable channel noise DB for digitized
28  /// waveforms with the given size and number of samples.
29  /// Default is for microphone.
31  virtual ~OmniChannelNoiseDB();
32 
33  // IConfigurable
34  virtual void configure(const WireCell::Configuration& config);
36 
37  // IChannelNoiseDatabase
38  virtual double sample_time() const;
39 
40  virtual double nominal_baseline(int channel) const;
41  virtual double gain_correction(int channel) const;
42  virtual double response_offset(int channel) const;
43 
44  virtual double min_rms_cut(int channel) const;
45  virtual double max_rms_cut(int channel) const;
46 
47  virtual int pad_window_front(int channel) const;
48  virtual int pad_window_back(int channel) const;
49 
50  virtual float coherent_nf_decon_limit(int channel) const;
51  virtual float coherent_nf_decon_lf_cutoff(int channel) const;
52  virtual float coherent_nf_decon_limit1(int channel) const;
53  virtual float coherent_nf_adc_limit(int channel) const;
54  virtual float coherent_nf_protection_factor(int channel) const;
55  virtual float coherent_nf_min_adc_limit(int channel) const;
56  virtual float coherent_nf_roi_min_max_ratio(int channel) const;
57 
58  virtual const filter_t& rcrc(int channel) const;
59  virtual const filter_t& config(int channel) const;
60  virtual const filter_t& noise(int channel) const;
61  virtual const filter_t& response(int channel) const;
62 
63  // todo:
64 
65  virtual std::vector<channel_group_t> coherent_channels() const {
66  return m_channel_groups;
67  }
68  virtual channel_group_t bad_channels() const {
69  return m_bad_channels;
70  }
71  virtual channel_group_t miscfg_channels() const {
72  return m_miscfg_channels;
73  }
74 
75 
76  protected:
77  // Allow subclasses some access so that they may leverage
78  // all the configuration code this class provides while
79  // supplying some subset from an external source. These
80  // setters should be called before each use of
81  // IChannelNoiseDatabase interface and may be called
82  // multiple times
83 
84  // Override the bad channels.
85  virtual void set_bad_channels(const channel_group_t& bad) {
86  m_bad_channels = bad;
87  }
88 
89  // Override the reconfigured spectrum for a set of
90  // channels. If `reset` is true then all channels
91  // reconfig spectrum is set to the default before applying
92  // the reconfig spectrum associated with the given from/to
93  // gain/shaping. If false, then other channels are not
94  // touched.
95  virtual void set_misconfigured(const std::vector<int>& channels,
96  double from_gain, double from_shaping,
97  double to_gain, double to_shaping,
98  bool reset = false);
99 
100  private:
101  double m_tick;
106 
107  typedef std::shared_ptr<filter_t> shared_filter_t;
108  typedef std::vector<shared_filter_t> filter_vector_t;
109 
110  // Embody the "database" entry for one channel.
111  struct ChannelInfo {
112  int chid;
113 
114  // direct scalar values
117 
118  float decon_limit;
120  float adc_limit;
125 
126  // parameters
127 
128  // frequency space filters
129  shared_filter_t rcrc, config, noise, response;
130 
131  ChannelInfo();
132  };
133 
134  //std::vector<ChannelInfo> m_db;
135  //std::unordered_map<int, ChannelInfo*> m_db;
136  std::unordered_map<int, ChannelInfo> m_db;
137 
138  const ChannelInfo& dbget(int ch) const {
139  auto it = m_db.find(ch);
140  if (it == m_db.end()) {
141  //it = m_db.find(defch);
142  //return *(it->second);
143  // m_db.insert(std::make_pair(ch, new ChannelInfo));
144  // return *(m_db[ch]);
145  THROW(KeyError() << errmsg{String::format("no db info for channel %d", ch)});
146  }
147  return it->second;
148  //return m_db.at(ch);
149  }
150 
151  std::vector< channel_group_t > m_channel_groups;
154 
155  // JSON parsing. Exhausting.
156  std::vector<int> parse_channels(const Json::Value& jchannels);
157  shared_filter_t make_filter(std::complex<float> defval = std::complex<float>(1,0));
158  shared_filter_t default_filter();
159  shared_filter_t parse_freqmasks(Json::Value jfm);
160  shared_filter_t parse_rcrc(Json::Value jrcrc, int nrc);
161  double parse_gain(Json::Value jreconfig);
162  shared_filter_t parse_reconfig(Json::Value jreconfig);
163  shared_filter_t get_reconfig(double from_gain, double from_shaping,
164  double to_gain, double to_shaping);
165  shared_filter_t parse_response(Json::Value jreconfig);
166  //ChannelInfo* make_ci(int chid, Json::Value jci);
168  ChannelInfo& get_ci(int chid);
169 
170 
171  // Reuse the same filter spectra for matching input parameters.
172 
173  // lookup by truncated rcrc value
174  std::unordered_map<int, shared_filter_t> m_rcrc_cache;
175  // lookup by OR of the four truncated values
176  std::unordered_map<int, shared_filter_t> m_reconfig_cache;
177  // lookup by explicit waveform id
178  std::unordered_map<int, shared_filter_t> m_waveform_cache;
179  // lookup by WirePlaneId::ident()
180  std::unordered_map<int, shared_filter_t> m_response_cache;
181 
183  };
184  }
185 
186 }
187 
188 #endif
189 
190 // Local Variables:
191 // mode: c++
192 // c-basic-offset: 4
193 // End:
std::unordered_map< int, shared_filter_t > m_rcrc_cache
virtual float coherent_nf_adc_limit(int channel) const
virtual void configure(const WireCell::Configuration &config)
Accept a configuration.
virtual const filter_t & config(int channel) const
Return the filter to correct any wrongly configured channels.
virtual const filter_t & noise(int channel) const
Return the filter to attenuate noise.
shared_filter_t get_reconfig(double from_gain, double from_shaping, double to_gain, double to_shaping)
virtual float coherent_nf_min_adc_limit(int channel) const
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
virtual float coherent_nf_decon_limit1(int channel) const
shared_filter_t parse_rcrc(Json::Value jrcrc, int nrc)
virtual double sample_time() const
FIXME: how to handle state changes?
std::unordered_map< int, shared_filter_t > m_response_cache
virtual int pad_window_back(int channel) const
std::vector< int > parse_channels(const Json::Value &jchannels)
virtual double min_rms_cut(int channel) const
const ChannelInfo & dbget(int ch) const
cfg
Definition: dbjson.py:29
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
std::unordered_map< int, shared_filter_t > m_waveform_cache
QTextStream & reset(QTextStream &s)
virtual void set_bad_channels(const channel_group_t &bad)
double parse_gain(Json::Value jreconfig)
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
virtual float coherent_nf_roi_min_max_ratio(int channel) const
virtual float coherent_nf_protection_factor(int channel) const
std::shared_ptr< Interface > pointer
Definition: Interface.h:16
virtual channel_group_t bad_channels() const
Return channels which are considered a&#39;priori "bad".
virtual double nominal_baseline(int channel) const
Return a nominal baseline correction (additive offset)
#define THROW(e)
Definition: Exceptions.h:25
virtual float coherent_nf_decon_lf_cutoff(int channel) const
virtual const filter_t & rcrc(int channel) const
Return the filter for the RC+RC coupling response function.
virtual float coherent_nf_decon_limit(int channel) const
std::shared_ptr< filter_t > shared_filter_t
WireCell::Waveform::compseq_t filter_t
The data type for all frequency-space, multiplicative filters.
std::unordered_map< int, ChannelInfo > m_db
std::shared_ptr< spdlog::logger > logptr_t
Definition: Logging.h:24
std::string format(const std::string &form, TYPES...objs)
Definition: String.h:45
shared_filter_t parse_reconfig(Json::Value jreconfig)
Definition: Main.h:22
virtual const filter_t & response(int channel) const
A nominal detector response spectrum for a given channel.
std::unordered_map< int, shared_filter_t > m_reconfig_cache
std::vector< shared_filter_t > filter_vector_t
virtual std::vector< channel_group_t > coherent_channels() const
Return channel grouping for coherent noise subtraction.
virtual double max_rms_cut(int channel) const
virtual void set_misconfigured(const std::vector< int > &channels, double from_gain, double from_shaping, double to_gain, double to_shaping, bool reset=false)
shared_filter_t parse_response(Json::Value jreconfig)
Json::Value Configuration
Definition: Configuration.h:50
virtual double response_offset(int channel) const
Return a time offset associated with the response().
virtual double gain_correction(int channel) const
shared_filter_t parse_freqmasks(Json::Value jfm)
std::vector< channel_group_t > m_channel_groups
virtual channel_group_t miscfg_channels() const
Return channels which are considered a&#39;priori "misconfigured".
virtual int pad_window_front(int channel) const
shared_filter_t make_filter(std::complex< float > defval=std::complex< float >(1, 0))
Thrown when a wrong key or has been encountered.
Definition: Exceptions.h:43