NumpyDepoSaver.cxx
Go to the documentation of this file.
2 
5 #include "WireCellUtil/cnpy.h"
6 
7 #include <string>
8 #include <vector>
9 #include <algorithm>
10 #include <iostream>
11 #include <tuple>
12 
15 
16 using namespace WireCell;
17 
19  : m_save_count(0)
20 {
21 }
22 
23 Sio::NumpyDepoSaver::~NumpyDepoSaver()
24 {
25 }
26 
27 
28 WireCell::Configuration Sio::NumpyDepoSaver::default_configuration() const
29 {
31 
32  // The output file name to write. Only compressed (zipped) Numpy
33  // files are supported. Writing is always in "append" mode. It's
34  // up to the user to delete a previous instance of the file if
35  // it's old contents are not wanted.
36  cfg["filename"] = "wct-frame.npz";
37 
38  return cfg;
39 }
40 
42 {
43  m_cfg = config;
44 }
45 
46 
47 typedef std::tuple<IDepo::pointer, size_t, size_t> depo_gen_child;
48 typedef std::vector< depo_gen_child > depos_with_prior;
49 
50 static void push_depo(depos_with_prior& dp, WireCell::IDepo::pointer depo, size_t gen=0, size_t childid=0)
51 {
52  dp.push_back(depo_gen_child(depo, gen, childid));
53  auto prior = depo->prior();
54  if (!prior) {
55  return;
56  }
57  push_depo(dp, prior, gen+1, dp.size());
58 }
59 static depos_with_prior flatten_depos(std::vector<WireCell::IDepo::pointer> depos)
60 {
61  depos_with_prior ret;
62  for (auto depo : depos) {
63  push_depo(ret, depo);
64  }
65  return ret;
66 }
67 
68 
69 bool Sio::NumpyDepoSaver::operator()(const WireCell::IDepo::pointer& indepo,
70  WireCell::IDepo::pointer& outdepo)
71 {
72  if (indepo) {
73  outdepo = indepo;
74  m_depos.push_back(indepo);
75  return true;
76  }
77  outdepo = nullptr;
78 
79  const size_t ndepos = m_depos.size();
80  if (!ndepos) {
81  std::cerr << "NumpyDepoSaver: warning: EOS and no depos seen.\n";
82  return true;
83  }
84 
85  auto fdepos = flatten_depos(m_depos);
86  const size_t nfdepos = fdepos.size();
87 
88  // time, charge, x, y, z, dlong, dtran
89  const size_t ndata=7;
90  Array::array_xxf data(nfdepos, ndata);
91  // ID, pdg, gen, child
92  const size_t ninfo = 4;
93  Array::array_xxi info(nfdepos, ninfo);
94  for (size_t idepo=0; idepo != nfdepos; ++idepo) {
95  auto depogc = fdepos[idepo];
96  auto depo = std::get<0>(depogc);
97  auto gen = std::get<1>(depogc);
98  auto child = std::get<2>(depogc);
99  data(idepo, 0) = depo->time();
100  data(idepo, 1) = depo->charge();
101  data(idepo, 2) = depo->pos().x();
102  data(idepo, 3) = depo->pos().y();
103  data(idepo, 4) = depo->pos().z();
104  data(idepo, 5) = depo->extent_long();
105  data(idepo, 6) = depo->extent_tran();
106  info(idepo, 0) = depo->id();
107  info(idepo, 1) = depo->pdg();
108  info(idepo, 2) = gen;
109  info(idepo, 3) = child;
110  }
111  const std::string data_name = String::format("depo_data_%d", m_save_count);
112  const std::string info_name = String::format("depo_info_%d", m_save_count);
113 
114  const std::string fname = m_cfg["filename"].asString();
115  const std::string mode = "a";
116  cnpy::npz_save(fname, data_name, data.data(), {ndata, ndepos}, mode);
117  cnpy::npz_save(fname, info_name, info.data(), {ninfo, ndepos}, mode);
118  m_depos.clear();
119 
120  ++m_save_count;
121  return true;
122 }
123 
124 
125 
126 
127 
128 
129 
130 
131 
static depos_with_prior flatten_depos(std::vector< WireCell::IDepo::pointer > depos)
std::shared_ptr< const IDepo > pointer
Definition: IData.h:19
static void push_depo(depos_with_prior &dp, WireCell::IDepo::pointer depo, size_t gen=0, size_t childid=0)
std::string string
Definition: nybbler.cc:12
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
Definition: cnpy.h:138
cfg
Definition: dbjson.py:29
basic_data data
Definition: format.h:764
def configure(cfg)
Definition: cuda.py:34
static Config * config
Definition: config.cpp:1054
const int ndepos
Definition: Main.h:22
std::vector< depo_gen_child > depos_with_prior
std::tuple< IDepo::pointer, size_t, size_t > depo_gen_child
Json::Value Configuration
Definition: Configuration.h:50
Eigen::Array< int, Eigen::Dynamic, Eigen::Dynamic > array_xxi
Integer.
Definition: Array.h:48
Eigen::ArrayXXf array_xxf
A real, 2D array.
Definition: Array.h:54
WIRECELL_FACTORY(NumpyDepoSaver, WireCell::Sio::NumpyDepoSaver, WireCell::IDepoFilter, WireCell::IConfigurable) using namespace WireCell