Reframer.cxx
Go to the documentation of this file.
1 #include "WireCellGen/Reframer.h"
2 #include "WireCellUtil/Units.h"
3 
6 
8 
9 #include <map>
10 #include <unordered_set>
11 
12 
15 
16 using namespace std;
17 using namespace WireCell;
18 
19 
21  : m_toffset(0.0)
22  , m_fill(0.0)
23  , m_tbin(0)
24  , m_nticks(0)
25  , log(Log::logger("sim"))
26 {
27 }
28 
29 Gen::Reframer::~Reframer()
30 {
31 }
32 
34 {
36 
37  cfg["anode"] = "";
38 
39  cfg["tags"] = Json::arrayValue;
40  cfg["tbin"] = m_tbin;
41  cfg["nticks"] = m_nticks;
42  cfg["toffset"] = m_toffset;
43  cfg["fill"] = m_fill;
44 
45  return cfg;
46 }
47 
49 {
50  const std::string anode_tn = cfg["anode"].asString();
51  log->debug("Gen::Reframer: using anode: \"{}\"", anode_tn);
52  m_anode = Factory::find_tn<IAnodePlane>(anode_tn);
53 
54  m_input_tags.clear();
55  for (auto jtag : cfg["tags"]) {
56  m_input_tags.push_back(jtag.asString());
57  }
58  m_toffset = get(cfg, "toffset", m_toffset);
59  m_tbin = get(cfg, "tbin", m_tbin);
60  m_fill = get(cfg, "fill", m_fill);
61  m_nticks = get(cfg, "nticks", m_nticks);
62 }
63 
64 
65 
66 
68 {
69  if (!inframe) {
70  outframe = nullptr;
71  return true;
72  }
73 
74  // Storage for samples indexed by channel ident.
75  std::map<int, std::vector<float> > waves;
76 
77  // initialize a "rectangular" 2D array of samples
78  for (auto chid : m_anode->channels()) {
79  waves[chid].resize(m_nticks, m_fill);
80  }
81 
82  // Get traces to consider
83  std::vector<ITrace::pointer> traces;
84  auto all_traces = inframe->traces();
85  if (m_input_tags.empty()) { // all traces
86  traces.insert(traces.begin(), all_traces->begin(), all_traces->end());
87  }
88  else {
89  // get tagged traces, but don't double count
90  std::unordered_set<int> trace_indices;
91  for (auto tag : m_input_tags) {
92  auto indices = inframe->tagged_traces(tag);
93  trace_indices.insert(indices.begin(), indices.end());
94  }
95  for (int ind : trace_indices) {
96  traces.push_back(all_traces->at(ind));
97  }
98  }
99 
100  // Lay down input traces over output waves
101  for (auto trace : traces) {
102  const int chid = trace->channel();
103 
104  const int tbin_in = trace->tbin();
105  auto in_it = trace->charge().begin();
106 
107  auto& wave = waves[chid]; // reference
108  auto out_it = wave.begin();
109 
110  const int delta_tbin = m_tbin - tbin_in;
111  if (delta_tbin > 0) { // must truncate input
112  in_it += delta_tbin;
113  }
114  else { // must pad output
115  out_it += -delta_tbin;
116  }
117 
118  // Go as far as possible but don't walk of the end of either
119  const auto in_end = trace->charge().end();
120  const auto out_end = wave.end();
121  while (in_it < in_end and out_it < out_end) {
122  *out_it += *in_it; // accumulate
123  ++in_it;
124  ++out_it;
125  }
126 
127  }
128 
129  // Transfer waves into traces.
130  ITrace::vector out_traces;
131  for (auto& it : waves) {
132  const int chid = it.first;
133  auto& wave = it.second;
134  auto out_trace = make_shared<SimpleTrace>(chid, 0, wave);
135  out_traces.push_back(out_trace);
136  }
137 
138  outframe = make_shared<SimpleFrame>(inframe->ident(),
139  inframe->time() + m_toffset + m_tbin*inframe->tick(),
140  out_traces,
141  inframe->tick());
142  log->debug("Gen::Reframer: frame {} {} traces, {} ticks",
143  inframe->ident(), out_traces.size(), m_nticks);
144  return true;
145 }
virtual bool operator()(const input_pointer &inframe, output_pointer &outframe)
The calling signature:
Definition: Reframer.cxx:67
std::vector< std::string > m_input_tags
Definition: Reframer.h:59
std::string string
Definition: nybbler.cc:12
STL namespace.
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
Definition: Reframer.cxx:33
cfg
Definition: dbjson.py:29
std::vector< pointer > vector
Definition: IData.h:21
std::shared_ptr< const IFrame > input_pointer
Definition: IFunctionNode.h:39
WIRECELL_FACTORY(Reframer, WireCell::Gen::Reframer, WireCell::IFrameFilter, WireCell::IConfigurable) using namespace std
Log::logptr_t log
Definition: Reframer.h:63
constexpr std::array< std::size_t, geo::vect::dimension< Vector >)> indices()
Returns a sequence of indices valid for a vector of the specified type.
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
IAnodePlane::pointer m_anode
Definition: Reframer.h:56
Json::Value Configuration
Definition: Configuration.h:50
virtual void configure(const WireCell::Configuration &config)
Accept a configuration.
Definition: Reframer.cxx:48