ChannelSelector.cxx
Go to the documentation of this file.
4 
6 
7 #include <sstream>
8 
11 
12 using namespace WireCell;
13 using namespace WireCell::SigProc;
14 
16  : log(Log::logger("glue"))
17 {
18 }
19 
20 ChannelSelector::~ChannelSelector()
21 {
22 }
23 
24 
26 {
28 
29  /// Only traces with channels in this array will be in the output.
30  cfg["channels"] = Json::arrayValue;
31 
32  /// Only traces with these tags will be in the output. If no tags
33  /// are given then tags are not considered.
34  cfg["tags"] = Json::arrayValue;
35 
36  return cfg;
37 }
38 
40 {
41  // tags need some order
42  auto jtags = cfg["tags"];
43  int ntags = jtags.size();
44  m_tags.clear();
45  m_tags.resize(ntags);
46  for (int ind=0; ind<ntags; ++ind) {
47  m_tags[ind] = jtags[ind].asString();
48  }
49 
50  // channels are just a bag
51  for (auto jchan : cfg["channels"]) {
52  m_channels.insert(jchan.asInt());
53  }
54 }
55 
56 void ChannelSelector::set_channels(const std::vector<int>& channels)
57 {
58  m_channels.clear();
59  for(int ch : channels) {
60  m_channels.insert(ch);
61  }
62 }
63 
64 
66 {
67  out = nullptr;
68  if (!in) {
69  log->debug("ChannelSelector: sees EOS");
70  return true; // eos
71  }
72 
73  std::vector<ITrace::vector> tracesvin;
74 
75  size_t ntraces = 0;
76 
77  size_t ntags = m_tags.size();
78  if (!ntags) {
79  tracesvin.push_back(FrameTools::untagged_traces(in));
80  log->debug("ChannelSelector: see frame: {} no tags, whole frame ({} traces out of {})",
81  in->ident(), tracesvin.back().size(), in->traces()->size());
82  ntraces += tracesvin[0].size();
83  }
84  else {
85  tracesvin.resize(ntags);
86  std::stringstream ss;
87  ss << "ChannelSelector: see frame: "<<in->ident() << " looking for " << ntags << " tags:";
88  for (size_t ind=0; ind<ntags; ++ind) {
89  std::string tag = m_tags[ind];
90  tracesvin[ind] = FrameTools::tagged_traces(in, tag);
91  ss << " " << tag << ":[" << tracesvin[ind].size() << " traces]";
92  ntraces += tracesvin[ind].size();
93  }
94  log->debug(ss.str());
95  }
96  if (!ntraces) {
97  log->warn("ChannelSelector: see no traces from frame {}", in->ident());
98  }
99 
100 
101  ITrace::vector out_traces;
102  std::vector<IFrame::trace_list_t> tagged_trace_indices;
103 
104  for (size_t ind=0; ind<tracesvin.size(); ++ind) {
105  auto& traces = tracesvin[ind];
106 
108  for (size_t trind=0; trind < traces.size(); ++trind) {
109  auto& trace = traces[trind];
110  if (m_channels.find(trace->channel()) == m_channels.end()) {
111  continue;
112  }
113  tl.push_back(out_traces.size());
114  out_traces.push_back(trace);
115  }
116  tagged_trace_indices.push_back(tl);
117  }
118 
119  std::stringstream taginfo;
120 
121  auto sf = new SimpleFrame(in->ident(), in->time(), out_traces, in->tick());
122  if (ntags) {
123  for (size_t ind=0; ind<ntags; ++ind) {
124  std::string tag = m_tags[ind];
125  sf->tag_traces(tag, tagged_trace_indices[ind]);
126  taginfo << tag << " ";
127  }
128  }
129  for(auto ftag: in->frame_tags()){
130  sf->tag_frame(ftag);
131  taginfo << "frame tag: " << ftag;
132  }
133 
134  out = IFrame::pointer(sf);
135  log->debug("ChannelSelector: producing {} traces, tags: {}",
136  out->traces()->size(), taginfo.str());
137 
138  return true;
139 }
140 
141 
142 
ITrace::vector tagged_traces(IFrame::pointer frame, IFrame::tag_t tag)
Definition: FrameTools.cxx:111
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
std::shared_ptr< const IFrame > pointer
Definition: IData.h:19
std::string string
Definition: nybbler.cc:12
virtual bool operator()(const input_pointer &in, output_pointer &out)
IFrameFilter interface.
std::vector< std::string > m_tags
std::unordered_set< int > m_channels
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(ChannelSelector, WireCell::SigProc::ChannelSelector, WireCell::IFrameFilter, WireCell::IConfigurable) using namespace WireCell
ITrace::vector untagged_traces(IFrame::pointer frame)
Definition: FrameTools.cxx:90
std::shared_ptr< const IFrame > output_pointer
Definition: IFunctionNode.h:40
logptr_t logger(std::string name)
Definition: Logging.cxx:71
virtual void set_channels(const std::vector< int > &channels)
void log(source_loc source, level::level_enum lvl, const char *fmt, const Args &...args)
Definition: spdlog.h:165
Definition: Main.h:22
Json::Value Configuration
Definition: Configuration.h:50
std::vector< size_t > trace_list_t
Definition: IFrame.h:36
virtual void configure(const WireCell::Configuration &config)
IConfigurable interface.