AddNoise.cxx
Go to the documentation of this file.
1 #include "WireCellGen/AddNoise.h"
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::AddNoise::AddNoise(const std::string& model, const std::string& rng)
20  : m_model_tn(model)
21  , m_rng_tn(rng)
22  , m_nsamples(9600)
23  , m_rep_percent(0.02) // replace 2% at a time
24  , log(Log::logger("sim"))
25 {
26 }
27 
28 
29 Gen::AddNoise::~AddNoise()
30 {
31 }
32 
34 {
36 
37  // fixme: maybe add some tag support?
38 
39  cfg["model"] = m_model_tn;
40  cfg["rng"] = m_rng_tn;
41  cfg["nsamples"] = m_nsamples;
42  cfg["replacement_percentage"] = m_rep_percent;
43  return cfg;
44 }
45 
47 {
48  m_rng_tn = get(cfg, "rng", m_rng_tn);
49  m_rng = Factory::find_tn<IRandom>(m_rng_tn);
50  m_model_tn = get(cfg, "model", m_model_tn);
51  m_model = Factory::find_tn<IChannelSpectrum>(m_model_tn);
52  m_nsamples = get<int>(cfg,"nsamples",m_nsamples);
53  m_rep_percent = get<double>(cfg,"replacement_percentage",m_rep_percent);
54 
55  log->debug("AddNoise: using IRandom: \"{}\", IChannelSpectrum: \"{}\"",
56  m_rng_tn, m_model_tn);
57 }
58 
59 
60 
62 {
63  if (!inframe) {
64  outframe = nullptr;
65  return true;
66  }
67 
68  ITrace::vector outtraces;
69  for (const auto& intrace : *inframe->traces()) {
70  int chid = intrace->channel();
71  const auto& spec = (*m_model)(chid);
73 
74  wave.resize(m_nsamples,0);
75  Waveform::increase(wave, intrace->charge());
76  auto trace = make_shared<SimpleTrace>(chid, intrace->tbin(), wave);
77  outtraces.push_back(trace);
78  }
79  outframe = make_shared<SimpleFrame>(inframe->ident(), inframe->time(),
80  outtraces, inframe->tick());
81  return true;
82 }
83 
84 
virtual void configure(const WireCell::Configuration &config)
IConfigurable.
Definition: AddNoise.cxx:46
Sequence< real_t > realseq_t
Definition: Waveform.h:31
virtual bool operator()(const input_pointer &inframe, output_pointer &outframe)
IFrameFilter.
Definition: AddNoise.cxx:61
STL namespace.
cfg
Definition: dbjson.py:29
std::vector< pointer > vector
Definition: IData.h:21
Log::logptr_t log
Definition: AddNoise.h:44
std::shared_ptr< const IFrame > input_pointer
Definition: IFunctionNode.h:39
IChannelSpectrum::pointer m_model
Definition: AddNoise.h:38
std::string m_model_tn
Definition: AddNoise.h:40
void increase(Sequence< Val > &seq, Val scalar)
Increase (shift) sequence values by scalar.
Definition: Waveform.h:129
WireCell::Waveform::realseq_t generate_waveform(const std::vector< float > &spec, IRandom::pointer rng, double replace=0.02)
Definition: Noise.cxx:7
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
Definition: AddNoise.cxx:33
Monte Carlo Simulation.
std::shared_ptr< const IFrame > output_pointer
Definition: IFunctionNode.h:40
logptr_t logger(std::string name)
Definition: Logging.cxx:71
void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &...args)
Definition: spdlog.h:165
Definition: Main.h:22
WIRECELL_FACTORY(AddNoise, WireCell::Gen::AddNoise, WireCell::IFrameFilter, WireCell::IConfigurable) using namespace std
Json::Value Configuration
Definition: Configuration.h:50
IRandom::pointer m_rng
Definition: AddNoise.h:37
std::string m_rng_tn
Definition: AddNoise.h:40