NoiseSource.cxx
Go to the documentation of this file.
2 
5 
6 #include "WireCellUtil/Persist.h"
8 
9 #include "Noise.h"
10 
11 #include <iostream>
12 
15 
16 using namespace std;
17 using namespace WireCell;
18 
19 Gen::NoiseSource::NoiseSource(const std::string& model, const std::string& anode, const std::string& rng)
20  : m_time(0.0*units::ns)
21  , m_stop(1.0*units::ms)
22  , m_readout(5.0*units::ms)
23  , m_tick(0.5*units::us)
24  , m_frame_count(0)
25  , m_anode_tn(anode)
26  , m_model_tn(model)
27  , m_rng_tn(rng)
28  , m_nsamples(9600)
29  , m_rep_percent(0.02) // replace 2% at a time
30  , m_eos(false)
31 {
32  // initialize the random number ...
33  //auto& spec = (*m_model)(0);
34 
35  // end initialization ..
36 
37 }
38 
39 
40 Gen::NoiseSource::~NoiseSource()
41 {
42 }
43 
45 {
47  cfg["start_time"] = m_time;
48  cfg["stop_time"] = m_stop;
49  cfg["readout_time"] = m_readout;
50  cfg["sample_period"] = m_tick;
51  cfg["first_frame_number"] = m_frame_count;
52 
53  cfg["anode"] = m_anode_tn;
54  cfg["model"] = m_model_tn;
55  cfg["rng"] = m_rng_tn;
56  cfg["nsamples"] = m_nsamples;
57  cfg["replacement_percentage"] = m_rep_percent;
58  return cfg;
59 }
60 
62 {
63  m_rng_tn = get(cfg, "rng", m_rng_tn);
64  m_rng = Factory::find_tn<IRandom>(m_rng_tn);
65  if (!m_rng) {
66  THROW(KeyError() << errmsg{"failed to get IRandom: " + m_rng_tn});
67  }
68 
69  m_anode_tn = get(cfg, "anode", m_anode_tn);
70  m_anode = Factory::find_tn<IAnodePlane>(m_anode_tn);
71  if (!m_anode) {
72  THROW(KeyError() << errmsg{"failed to get IAnodePlane: " + m_anode_tn});
73  }
74 
75  m_model_tn = get(cfg, "model", m_model_tn);
76  m_model = Factory::find_tn<IChannelSpectrum>(m_model_tn);
77  if (!m_model) {
78  THROW(KeyError() << errmsg{"failed to get IChannelSpectrum: " + m_model_tn});
79  }
80 
81  m_readout = get<double>(cfg, "readout_time", m_readout);
82  m_time = get<double>(cfg, "start_time", m_time);
83  m_stop = get<double>(cfg, "stop_time", m_stop);
84  m_tick = get<double>(cfg, "sample_period", m_tick);
85  m_frame_count = get<int>(cfg, "first_frame_number", m_frame_count);
86  m_nsamples = get<int>(cfg,"m_nsamples",m_nsamples);
87  m_rep_percent = get<double>(cfg,"replacement_percentage",m_rep_percent);
88 
89  cerr << "Gen::NoiseSource: using IRandom: \"" << m_rng_tn << "\""
90  << " IAnodePlane: \"" << m_anode_tn << "\""
91  << " IChannelSpectrum: \"" << m_model_tn << "\""
92  << " readout time: " << m_readout/units::us << "us\n";
93 
94 
95 
96 }
97 
98 
99 
101 {
102  if (m_eos) { // This source does not restart.
103  return false;
104  }
105 
106  if (m_time >= m_stop) {
107  frame = nullptr;
108  m_eos = true;
109  return true;
110  }
111  ITrace::vector traces;
112  const int tbin = 0;
113  int nsamples = 0;
114  for (auto chid : m_anode->channels()) {
115  const auto& spec = (*m_model)(chid);
116 
118  // std::cout << noise.size() << " " << nsamples << std::endl;
119  noise.resize(m_nsamples,0);
120  auto trace = make_shared<SimpleTrace>(chid, tbin, noise);
121  traces.push_back(trace);
122  nsamples += noise.size();
123  }
124  cerr << "Gen::NoiseSource: made " << traces.size() << " traces, "
125  << nsamples << " samples\n";
126  frame = make_shared<SimpleFrame>(m_frame_count, m_time, traces, m_tick);
127  m_time += m_readout;
128  ++m_frame_count;
129  return true;
130 }
131 
132 
std::shared_ptr< const IFrame > pointer
Definition: IData.h:19
Sequence< real_t > realseq_t
Definition: Waveform.h:31
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
virtual bool operator()(IFrame::pointer &frame)
IFrameSource.
virtual void configure(const WireCell::Configuration &config)
IConfigurable.
Definition: NoiseSource.cxx:61
STL namespace.
cfg
Definition: dbjson.py:29
std::vector< pointer > vector
Definition: IData.h:21
IAnodePlane::pointer m_anode
Definition: NoiseSource.h:40
IChannelSpectrum::pointer m_model
Definition: NoiseSource.h:41
WIRECELL_FACTORY(NoiseSource, WireCell::Gen::NoiseSource, WireCell::IFrameSource, WireCell::IConfigurable) using namespace std
#define THROW(e)
Definition: Exceptions.h:25
WireCell::Waveform::realseq_t generate_waveform(const std::vector< float > &spec, IRandom::pointer rng, double replace=0.02)
Definition: Noise.cxx:7
Definition: Main.h:22
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
Definition: NoiseSource.cxx:44
static const double us
Definition: Units.h:101
static const double ms
Definition: Units.h:100
IRandom::pointer m_rng
Definition: NoiseSource.h:39
Json::Value Configuration
Definition: Configuration.h:50
static const double us
Definition: Units.h:105
QAsciiDict< Entry > ns
Thrown when a wrong key or has been encountered.
Definition: Exceptions.h:43