Retagger.cxx
Go to the documentation of this file.
1 #include "WireCellGen/Retagger.h"
4 
7 
8 using namespace WireCell;
9 
11 {
12 }
13 
14 Gen::Retagger::~Retagger()
15 {
16 }
17 
19 {
20  // frame/trace/merge tagging.
21  m_trctx.configure(cfg["tag_rules"]);
22 }
23 
24 WireCell::Configuration Gen::Retagger::default_configuration() const
25 {
27  cfg["tag_rules"] = Json::arrayValue;
28  return cfg;
29 }
30 
31 bool Gen::Retagger::operator()(const input_pointer& inframe, output_pointer& outframe)
32 {
33  outframe = nullptr;
34  if (!inframe) {
35  return true; // eos
36  }
37 
38 
39  // Basic frame data is just shunted across.
40  auto sfout = new SimpleFrame(inframe->ident(), inframe->time(), *inframe->traces(), inframe->tick());
41 
42  //
43  // frame
44  auto fintags = inframe->frame_tags();
45  // Add empty tag to allow rules to create an output tag even if none exist.
46  // No equivalent should be made for trace tags.
47  fintags.push_back("");
48  auto fouttags = m_trctx.transform(0, "frame", fintags);
49  for (auto ftag : fouttags) {
50  sfout->tag_frame(ftag);
51  }
52 
53  //
54  // trace
55  for (auto inttag : inframe->trace_tags()) {
56  tagrules::tagset_t touttags = m_trctx.transform(0, "trace", inttag);
57  if (touttags.empty()) {
58  continue;
59  }
60  const auto& traces = inframe->tagged_traces(inttag);
61  const auto& summary = inframe->trace_summary(inttag);
62  for (auto otag : touttags) {
63  sfout->tag_traces(otag, traces, summary);
64  }
65  }
66 
67  //
68  // merge
69  typedef std::tuple<tagrules::tag_t, IFrame::trace_list_t, IFrame::trace_summary_t> tag_trace_list_summary_t;
70  typedef std::vector< tag_trace_list_summary_t > ttls_stash_t;
71  typedef std::unordered_map<std::string, ttls_stash_t> ttls_stash_map_t;
72  ttls_stash_map_t stashmap;
73  for (auto inttag : inframe->trace_tags()) {
74  tagrules::tagset_t touttags = m_trctx.transform(0, "merge", inttag);
75  // std::cerr << "Retagger: " << inttag << " -> " << touttags.size() << " outtags\n";
76  if (touttags.empty()) {
77  continue;
78  }
79  const auto& traces = inframe->tagged_traces(inttag);
80  const auto& summary = inframe->trace_summary(inttag);
81  for (auto otag : touttags) {
82  stashmap[otag].push_back(std::make_tuple(inttag, traces, summary));
83  }
84  }
85  for (auto it : stashmap) {
86  auto& otag = it.first;
87  auto& stashv = it.second;
88  size_t ntraces = 0, nsummary=0;
89  // If at least one summary is not empty we must assure output
90  // summary is aligned with output trace index vector.
91  // Otherwise, we assume input plays by the rules and keeps
92  // size of traces and summary equal.
93  for (auto& ttls : stashv) {
94  ntraces += get<1>(ttls).size();
95  nsummary += get<2>(ttls).size();
96  }
97  IFrame::trace_list_t otraces;
98  otraces.reserve(ntraces);
99  IFrame::trace_summary_t osummary;
100  if (nsummary > 0) {
101  osummary.reserve(ntraces);
102  }
103  for (auto& ttls : stashv) { // one more time
104  auto& traces = get<1>(ttls);
105  otraces.insert(otraces.end(), traces.begin(), traces.end());
106 
107  // std::cerr << "Retagger: merge: " << get<0>(ttls) << " -> " << otag
108  // << " with " << otraces.size() << " / " << ntraces << std::endl;
109  if (!nsummary) {
110  continue;
111  }
112  auto& summary = get<2>(ttls);
113  if (summary.empty()) {
114  for (size_t ind=0; ind<traces.size(); ++ind) {
115  summary.push_back(0); // zero pad to cover missing summary and keep alignment with traces.
116  }
117  }
118  else {
119  osummary.insert(osummary.end(), summary.begin(), summary.end());
120  }
121  }
122  std::cerr << "Retagger: tagging trace set: " << otag
123  << " with " << otraces.size() << " traces, " << osummary.size() << " summary\n";
124 
125  sfout->tag_traces(otag, otraces, osummary);
126  }
127 
128 
129  outframe = IFrame::pointer(sfout);
130  return true;
131 }
132 
std::unordered_set< tag_t > tagset_t
Definition: TagRules.h:48
cfg
Definition: dbjson.py:29
virtual const tag_list_t & frame_tags() const
Definition: SimpleFrame.cxx:52
void tag_frame(const tag_t &tag)
Definition: SimpleFrame.cxx:71
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:87
std::shared_ptr< const IFrame > input_pointer
Definition: IFunctionNode.h:39
WIRECELL_FACTORY(Retagger, WireCell::Gen::Retagger, WireCell::IFrameFilter, WireCell::IConfigurable) using namespace WireCell
def configure(cfg)
Definition: cuda.py:34
std::vector< double > trace_summary_t
Definition: IFrame.h:39
std::shared_ptr< const IFrame > output_pointer
Definition: IFunctionNode.h:40
Definition: Main.h:22
Json::Value Configuration
Definition: Configuration.h:50
def summary(store)
Definition: info.py:119
const GenericPointer< typename T::ValueType > & pointer
Definition: pointer.h:1124
std::vector< size_t > trace_list_t
Definition: IFrame.h:36