Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards imaging with DNN and from artROOT #350

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions img/src/CMMModifier.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ bool CMMModifier::operator()(const input_pointer& in, output_pointer& out)

// copy a CMM from input frame
auto cmm = in->masks();

// // ------------ Debug Ewerton 2024-02-29 -----------
// //auto mycmm = in->masks();
// for(auto x : cmm) //cmm[m_cm_tag])
// {
// std::cout << "\n [CMMModifier-Ew] cmm.first=" << x.first << "\n";// " (" << x.second.first << ", " << x.second.second << ")\n";
// }
// std::cout << "\n [CMMModifier-Ew] end of inmasks loop" ;
// // ------------ End debug Ewerton 2024-02-29 -----------


if (cmm.find(m_cm_tag)==cmm.end()) {
log->warn("no ChannelMask with name \""+m_cm_tag+"\", will create one");
}
Expand Down Expand Up @@ -304,6 +315,15 @@ bool CMMModifier::operator()(const input_pointer& in, output_pointer& out)
// Basic frame stays the same.
auto sfout = new Aux::SimpleFrame(in->ident(), in->time(), out_traces, in->tick(), cmm);

// // ------------ Debug Ewerton 2024-02-29 -----------
// // ChannelMaskMap = typedef std::map<std::string, ChannelMasks>
// auto mycmm = in->masks();
// for(auto x : mycmm) //cmm[m_cm_tag])
// {
// std::cout << "\n [CMMModifier-Ew] cmm.first=" << x.first << "\n";// " (" << x.second.first << ", " << x.second.second << ")\n";
// }
// // ------------ End debug Ewerton 2024-02-29 -----------

// passing through other parts of the original frame
for (auto ftag : in->frame_tags()) {
sfout->tag_frame(ftag);
Expand Down
2 changes: 1 addition & 1 deletion img/src/Projection2D.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ LayerProjection2DMap WireCell::Img::Projection2D::get_projection(const WireCell:
for (const auto& chan_desc : map_b2c[bdesc]) {
const auto& chan = std::get<channel_t>(cg[chan_desc].ptr);
WirePlaneLayer_t layer = chan->planeid().layer();
int cident = chan->ident();
int cident = chan->index();
auto charge = activity[chan].value();
auto unc = activity[chan].uncertainty();
// TODO: make this configurable and robust
Expand Down
5 changes: 5 additions & 0 deletions pytorch/inc/WireCellPytorch/DNNROIFinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace WireCell {
// replaced by the anode number.
std::vector<std::string> intags;

std::string summary_tag{""};

// The tag used for the input decon charge. This is
// usually "decon_chargeN" with "N" replaced with the
// anode number.
Expand Down Expand Up @@ -127,6 +129,9 @@ namespace WireCell {
// Convert traces to a dense array
Array::array_xxf traces_to_eigen(ITrace::vector traces);

// extract trace summary to mathch the eigen format order
IFrame::trace_summary_t get_summary_e(const IFrame::pointer& inframe, const std::string &tag) const;

// Convert dense array to (dense) traces
ITrace::shared_vector eigen_to_traces(const Array::array_xxf& arr);

Expand Down
29 changes: 28 additions & 1 deletion pytorch/src/DNNROIFinding.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void Pytorch::DNNROIFinding::configure(const WireCell::Configuration& cfg)
for (auto one : cfg["intags"]) {
m_cfg.intags.push_back(one.asString());
}
m_cfg.summary_tag = get(cfg, "summary_tag", m_cfg.summary_tag);
m_cfg.tick_per_slice = get(cfg, "tick_per_slice", m_cfg.tick_per_slice);
m_cfg.decon_charge_tag = get(cfg, "decon_charge_tag", m_cfg.decon_charge_tag);
m_cfg.outtag = get(cfg, "outtag", m_cfg.outtag);
Expand Down Expand Up @@ -185,6 +186,24 @@ Array::array_xxf Pytorch::DNNROIFinding::traces_to_eigen(ITrace::vector traces)
return arr;
}

IFrame::trace_summary_t Pytorch::DNNROIFinding::get_summary_e(const IFrame::pointer& inframe, const std::string &tag) const {
IFrame::trace_summary_t summary_e(m_nrows, 0.0);
std::unordered_map<int, size_t> ch2row;
for (size_t i = 0; i < m_chlist.size(); ++i) {
ch2row[m_chlist[i]] = i;
}
auto traces = Aux::tagged_traces(inframe, tag);
auto summary = inframe->trace_summary(tag);
for (size_t i = 0; i < traces.size(); ++i) {
const auto& tr = traces[i];
const auto& ch = tr->channel();
const auto& row = ch2row.find(ch);
if (row != ch2row.end()) {
summary_e[row->second] = summary[i];
}
}
return summary_e;
}

ITrace::shared_vector Pytorch::DNNROIFinding::eigen_to_traces(const Array::array_xxf& arr)
{
Expand Down Expand Up @@ -303,8 +322,16 @@ bool Pytorch::DNNROIFinding::operator()(const IFrame::pointer& inframe, IFrame::
traces,
inframe->tick(), inframe->masks());
sframe->tag_frame("DNNROIFinding");
sframe->tag_traces(m_cfg.outtag, m_trace_indices);
log->debug("call={} getting summary for tag {}", m_save_count, m_cfg.summary_tag);
auto summary = get_summary_e(inframe, m_cfg.summary_tag);
std::string ss;
for (const auto& rms : summary) {
ss += fmt::format("{:.2f} ", rms);
}
log->trace("call={} summary: {}", m_save_count, ss);
sframe->tag_traces(m_cfg.outtag, m_trace_indices, summary);
outframe = IFrame::pointer(sframe);
log->debug("call={} output frame: {}", m_save_count, Aux::taginfo(outframe));

log->debug(tk(fmt::format("call={} finish", m_save_count)));
++m_save_count;
Expand Down
51 changes: 47 additions & 4 deletions sigproc/src/ChannelSelector.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ bool ChannelSelector::operator()(const input_pointer& in, output_pointer& out)
}

std::vector<ITrace::vector> tracesvin;
std::vector<IFrame::trace_summary_t> summariesvin; //added Ewerton 2023-10-04

// size_t ntraces = 0;
size_t ntags = m_tags.size();
Expand All @@ -86,32 +87,51 @@ bool ChannelSelector::operator()(const input_pointer& in, output_pointer& out)
}
else {
tracesvin.resize(ntags);
summariesvin.resize(ntags);
for (size_t ind = 0; ind < ntags; ++ind) {
std::string tag = m_tags[ind];
tracesvin[ind] = Aux::tagged_traces(in, tag);
summariesvin[ind] = in->trace_summary(tag);//added Ewerton 2023-10-04
// std::cerr << "\nChannelSelector: tag=" << tag << "\n";//added Ewerton 2023-10-04
// ntraces += tracesvin[ind].size();
}
}



ITrace::vector out_traces;
std::vector<IFrame::trace_list_t> tagged_trace_indices;
std::vector<IFrame::trace_summary_t> tagged_trace_summaries;//added Ewerton 2023-10-04


for (size_t ind = 0; ind < tracesvin.size(); ++ind) {
//std::cerr << "\n tracesvin[ind].size()=" << tracesvin[ind].size() << "\n";//added Ewerton 2023-10-02
auto& traces = tracesvin[ind];

auto& summary = summariesvin[ind];//added Ewerton 2023-10-04
IFrame::trace_list_t tl;
IFrame::trace_summary_t thl; //added Ewerton 2023-10-04
for (size_t trind = 0; trind < traces.size(); ++trind) {
auto& trace = traces[trind];
// DEBUG Ewerton 2024-04-15
// std::cerr << "\n [ChannelSelector] summary.size()=" << summary.size() << "\n";
// end DEBUG
auto threshold = summary.size() ? summary[trind] : -999; // added Ewerton 2023-10-04
if (m_channels.find(trace->channel()) == m_channels.end()) {
continue;
}
tl.push_back(out_traces.size());
if(summary.size()) thl.push_back(threshold); //added Ewerton 2023-10-04
out_traces.push_back(trace);
// summary[trind] => element
// trind => element index
// sl.push_back(out..) => index of trace in out_traces
}
tagged_trace_indices.push_back(tl);
tagged_trace_summaries.push_back(thl); //empty vector if there is no summary for given tag. added Ewerton 2023-10-04
}

auto sf = new Aux::SimpleFrame(in->ident(), in->time(), out_traces, in->tick());
// auto sf = new Aux::SimpleFrame(in->ident(), in->time(), out_traces, in->tick()); // original
auto sf = new Aux::SimpleFrame(in->ident(), in->time(), out_traces, in->tick(), in->masks()); // changed Ewerton 2023-10-??

if (ntags) {
for (size_t ind = 0; ind < ntags; ++ind) {
std::string tag = m_tags[ind];
Expand All @@ -121,7 +141,11 @@ bool ChannelSelector::operator()(const input_pointer& in, output_pointer& out)
}
}
else {
sf->tag_traces(tag, tagged_trace_indices[ind]);
//sf->tag_traces(tag, tagged_trace_indices[ind]); //original. commented Ewerton 2023-10-02
if(tagged_trace_summaries[ind].size())
sf->tag_traces(tag, tagged_trace_indices[ind], tagged_trace_summaries[ind]); //added Ewerton 2023-10-04
else
sf->tag_traces(tag, tagged_trace_indices[ind]); //added Ewerton 2023-10-04
}
}
}
Expand All @@ -144,5 +168,24 @@ bool ChannelSelector::operator()(const input_pointer& in, output_pointer& out)
info << "input " << Aux::taginfo(in) << " output: " << Aux::taginfo(out);
log->debug(info.str());


/*
Waveform::ChannelMaskMap deb_masks = out->masks(); //default cmm from masks() function is empty cmm -> create local CMM?
std::cerr << "\n ChannelSelector deb_masks.size()=" << deb_masks.size() << "\n\n"; std::string cmm_tag("bad"); // use single tag for cmm: "bad"
Waveform::ChannelMasks chm = deb_masks[cmm_tag]; // use single tag for now: "bad"
int deleteme2=0;
for(auto cm : chm)
{
deleteme2++;
if(deleteme2<10) {
auto ch = cm.first;
std::cerr << "\n ChannelSelector ChannelMaskMap: ch=" << ch << "\n\n";
Waveform::BinRangeList br = cm.second;
for(auto r : br) std::cerr << "\n ChannelSelector rlow=" << r.first << ", rup=" << r.second;
}
}
*/
// ------------end debug Ewerton 2023-08-22 ------------

return true;
}