JsonBlobSetSink.cxx
Go to the documentation of this file.
2 
3 #include "WireCellUtil/Units.h"
4 #include "WireCellUtil/String.h"
6 
7 #include <fstream>
8 
11 
12 
13 using namespace WireCell;
14 
16  : m_drift_speed(1.6*units::mm/units::us)
17  , m_filename("blobs-%02d.json")
18  , m_face(0)
19  , l(Log::logger("io"))
20 {
21 }
22 Img::JsonBlobSetSink::~JsonBlobSetSink()
23 {
24 }
25 
26 
28 {
29  m_face = get(cfg, "face", m_face);
30  m_drift_speed = get(cfg, "drift_speed", m_drift_speed);
31  m_filename = get(cfg, "filename", m_filename);
32 }
33 
34 WireCell::Configuration Img::JsonBlobSetSink::default_configuration() const
35 {
37 
38  // File name for output files. A "%d" will be filled in with blob
39  // set ident number.
40  cfg["filename"] = m_filename;
41  // Set to -1 to not apply any face filter, otherwise only consider matching face number
42  cfg["face"] = m_face;
43  cfg["drift_speed"] = m_drift_speed;
44  return cfg;
45 }
46 
47 bool Img::JsonBlobSetSink::operator()(const IBlobSet::pointer& bs)
48 {
49  if (!bs) {
50  l->debug("JsonBlobSetSink: eos");
51  return true;
52  }
53 
54  const auto& blobs = bs->blobs();
55  if (blobs.empty()) {
56  l->info("JsonBlobSetSink: no blobs");
57  return true;
58  }
59 
60  auto slice = blobs[0]->slice();
61  auto frame = slice->frame();
62  const double start = slice->start();
63  const double time = frame->time();
64  const double x = (start-time)*m_drift_speed;
65 
66  l->debug("JsonBlobSetSink: frame:{}, slice:{} set:{} time:{} ms, start={} ms x:{} nblobs:{}",
67  frame->ident(), slice->ident(), bs->ident(),
68  time/units::ms, start/units::ms,
69  x, blobs.size());
70 
71  Json::Value jblobs = Json::arrayValue;
72 
73 
74  for (const auto& iblob: blobs) {
75  if (m_face >= 0 and m_face != iblob->face()->ident()) {
76  continue; // filter
77  }
78  const auto& coords = iblob->face()->raygrid();
79  const auto& blob = iblob->shape();
80  Json::Value jcorners = Json::arrayValue;
81  for (const auto& corner : blob.corners()) {
82  Json::Value jcorner = Json::arrayValue;
83  auto pt = coords.ray_crossing(corner.first, corner.second);
84  jcorner.append(x);
85  jcorner.append(pt.y());
86  jcorner.append(pt.z());
87  jcorners.append(jcorner);
88  }
89  Json::Value jblob;
90  jblob["points"] = jcorners;
91  jblob["values"]["charge"] = iblob->value();
92  jblob["values"]["uncert"] = iblob->uncertainty();
93  jblob["values"]["ident"] = iblob->ident();
94  jblob["values"]["inset"] = jblobs.size();
95 
96  jblobs.append(jblob);
97  }
98 
99 
101 
102  top["blobs"] = jblobs;
103 
104  std::string fname = m_filename;
105  if (m_filename.find("%") != std::string::npos) {
106  fname = String::format(m_filename, bs->ident());
107  }
108  std::ofstream fstr(fname);
109  if (!fstr) {
110  l->error("Failed to open for writing: %s", fname);
111  return false;
112  }
113  fstr << top;
114 
115 
116  return true;
117 }
118 
std::shared_ptr< const IBlobSet > pointer
Definition: IData.h:19
WIRECELL_FACTORY(JsonBlobSetSink, WireCell::Img::JsonBlobSetSink, WireCell::IBlobSetSink, WireCell::IConfigurable) using namespace WireCell
std::string string
Definition: nybbler.cc:12
cfg
Definition: dbjson.py:29
static QStrList * l
Definition: config.cpp:1044
def blobs(cm, hist)
Definition: plots.py:79
def configure(cfg)
Definition: cuda.py:34
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
logptr_t logger(std::string name)
Definition: Logging.cxx:71
static const double mm
Definition: Units.h:73
Definition: Main.h:22
static const double us
Definition: Units.h:101
static const double ms
Definition: Units.h:100
Json::Value Configuration
Definition: Configuration.h:50
list x
Definition: train.py:276