JsonDepoSource.cxx
Go to the documentation of this file.
2 
4 
7 
8 #include "WireCellUtil/Point.h"
9 #include "WireCellUtil/Persist.h"
10 
13 
14 #include <iostream>
15 #include <string>
16 #include <locale> // for std::tolower
17 
18 
19 using namespace WireCell;
20 
21 // These adapters deal with whether the file holds just "n" number of
22 // electrons, just "q" at a point or both "q" and "s" along a step.
23 // For the first, just a units conversion is needed. In the latter
24 // two cases a real RecombinationModel is used to return amount of
25 // drifting charge for given point energy deposition "q" or as both
26 // q=dE and s=dX.
28 public:
30  virtual double operator()(Json::Value depo) const = 0;
31 };
33  double m_scale;
34 public:
35  ElectronsAdapter(double scale=1.0) : m_scale(scale) {}
36  virtual ~ElectronsAdapter() {};
37  virtual double operator()(Json::Value depo) const {
38  return m_scale*depo["n"].asInt()*(-1.0*units::eplus);
39  }
40 };
43 public:
45  virtual ~PointAdapter() {}
46  virtual double operator()(Json::Value depo) const {
47  const double dE = depo["q"].asDouble();
48  return (*m_model)(dE);
49  }
50 };
53 public:
55  virtual ~StepAdapter() {}
56  virtual double operator()(Json::Value depo) const {
57  const double dE = depo["q"].asDouble();
58  const double dX = depo["s"].asDouble();
59  return (*m_model)(dE, dX);
60  }
61 };
62 
63 
64 using namespace std;
65 using namespace WireCell;
66 
68  : m_adapter(nullptr), m_eos(false)
69 {
70 }
71 
73 {
74 }
75 
76 
78 {
79  if (m_eos) {
80  return false;
81  }
82 
83  if (m_depos.empty()) {
84  m_eos = true;
85  out = nullptr;
86  return true;
87  }
88 
89  out = m_depos.back();
90  m_depos.pop_back();
91  return true;
92 }
93 
95 {
97  cfg["filename"] = ""; // json file name
98  cfg["jsonpath"] = "depos"; // path to depo list in json data
99  cfg["model"] = "electrons"; // model for converting "q" and maybe
100  // "s" or "n" to amount of drifting
101  // charge.
102  return cfg;
103 }
104 
106 {
107  const double q = (*m_adapter)(jdepo);
108  auto idepo = std::make_shared<SimpleDepo>(
109  get(jdepo,"t",0.0),
110  Point(get(jdepo, "x", 0.0),
111  get(jdepo, "y", 0.0),
112  get(jdepo, "z", 0.0)),
113  q);
114  return idepo;
115 }
116 
117 
118 
120 {
121  m_depos.clear();
122  if (m_adapter) {
123  delete m_adapter;
124  m_adapter = nullptr;
125  }
126  std::string model_tn = cfg["model"].asString();
127  std::string model_type = String::split(model_tn)[0];
128 
129  if (model_type == "electrons") { // "n" already gives number of ionization electrons
130  double scale = get(cfg,"scale",1.0);
131  cerr << "Sio::JsonDepoSource: using electrons with scale=" << scale << endl;
132  m_adapter = new ElectronsAdapter(scale);
133  }
134  else {
135  auto model = Factory::lookup_tn<IRecombinationModel>(model_tn);
136  if (!model) {
137  cerr << "Sio::JsonDepoSource::configure: unknown recombination model: \"" << model_tn << "\"\n";
138  return;
139  }
140  if (model_type == "MipRecombination") {
142  }
143  if (model_type == "BirksRecombination" || model_type == "BoxRecombination") {
144  m_adapter = new StepAdapter(model);
145  }
146  }
147 
148  // get and load JSON file.
149  string filename = get<string>(cfg,"filename");
150  string dotpath = get<string>(cfg,"jsonpath","depos");
151  if (filename.empty()) {
152  cerr << "JsonDepoSource::configure: no JSON filename given" << endl;
153  return; // fixme: uh, error handle much?
154  }
155  Json::Value top = WireCell::Persist::load(filename.c_str());
156 
157  double qtot = 0;
158  auto jdepos = branch(top, dotpath);
159  for (auto jdepo : jdepos) {
160  auto idepo = jdepo2idepo(jdepo);
161  m_depos.push_back(idepo);
162  qtot += idepo->charge();
163  }
164  std::sort(m_depos.begin(), m_depos.end(), descending_time);
165  cerr << "Sio::JsonDepoSource::configure: "
166  << "slurped in " << m_depos.size() << " depositions, "
167  << " = " << -1*qtot/units::eplus << " electrons\n";
168 }
169 
170 
IRecombinationModel::pointer m_model
std::shared_ptr< const IDepo > pointer
Definition: IData.h:19
JsonRecombinationAdaptor * m_adapter
static const double eplus
Definition: Units.h:110
std::shared_ptr< IRecombinationModel > pointer
Access subclass facet by pointer.
Definition: IComponent.h:33
virtual ~StepAdapter()
virtual ~ElectronsAdapter()
D3Vector< double > Point
A 3D Cartesian point in double precision.
Definition: Point.h:15
virtual double operator()(Json::Value depo) const =0
std::string string
Definition: nybbler.cc:12
virtual ~PointAdapter()
IDepo::pointer jdepo2idepo(Json::Value jdepo)
virtual double operator()(Json::Value depo) const
STL namespace.
virtual double operator()(Json::Value depo) const
cfg
Definition: dbjson.py:29
string filename
Definition: train.py:213
StepAdapter(IRecombinationModel::pointer model)
ElectronsAdapter(double scale=1.0)
Configuration branch(Configuration cfg, const std::string &dotpath)
Follow a dot.separated.path and return the branch there.
virtual double operator()(Json::Value depo) const
virtual bool operator()(IDepo::pointer &out)
IDepoSource.
IRecombinationModel::pointer m_model
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
Json::Value load(const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Definition: Persist.cxx:121
WIRECELL_FACTORY(JsonDepoSource, WireCell::Sio::JsonDepoSource, WireCell::IDepoSource, WireCell::IConfigurable) using namespace WireCell
WireCell::IDepo::vector m_depos
Definition: Main.h:22
bool descending_time(const WireCell::IDepo::pointer &lhs, const WireCell::IDepo::pointer &rhs)
Compare two IDepo::pointers for by time, descending. x is used to break tie.
Definition: IDepo.cxx:29
void scale(Sequence< Val > &seq, Val scalar)
Scale (multiply) sequence values by scalar.
Definition: Waveform.h:146
Json::Value Configuration
Definition: Configuration.h:50
PointAdapter(IRecombinationModel::pointer model)
std::vector< std::string > split(const std::string &in, const std::string &delim=":")
Definition: String.cxx:5
virtual WireCell::Configuration default_configuration() const
IConfigurable.
QTextStream & endl(QTextStream &s)
virtual void configure(const WireCell::Configuration &config)
Accept a configuration.