WireSchema.cxx
Go to the documentation of this file.
2 #include "WireCellUtil/Persist.h"
4 
5 
6 using namespace WireCell;
7 using namespace WireCell::WireSchema;
8 
9 
10 static std::map<std::string, StoreDBPtr> gStoreCache;
11 
12 
13 
15 {
16  // turn into absolute real path
17  std::string realpath = WireCell::Persist::resolve(filename);
18  auto maybe = gStoreCache.find(realpath);
19  if (maybe != gStoreCache.end()) {
20  return Store(maybe->second);
21  }
22 
23 
24  Json::Value jtop = WireCell::Persist::load(filename);
25  Json::Value jstore = jtop["Store"];
26 
27  StoreDB* store = new StoreDB;
28 
29 
30 
31  std::vector<Point> points;
32  {
33  Json::Value jpoints = jstore["points"];
34  const int npoints = jpoints.size();
35  points.resize(npoints);
36  for (int ipoint=0; ipoint<npoints; ++ipoint) {
37  Json::Value jp = jpoints[ipoint]["Point"];
38  points[ipoint].set(get<double>(jp,"x"), get<double>(jp,"y"), get<double>(jp,"z"));
39  }
40  }
41 
42  {// wires
43  Json::Value jwires = jstore["wires"];
44  const int nwires = jwires.size();
45  std::vector<Wire>& wires = store->wires;
46  wires.resize(nwires);
47  for (int iwire=0; iwire<nwires; ++iwire) {
48  Json::Value jwire = jwires[iwire]["Wire"];
49  Wire& wire = wires[iwire];
50  wire.ident = get<int>(jwire, "ident");
51  wire.channel = get<int>(jwire, "channel");
52  wire.segment = get<int>(jwire, "segment");
53 
54  int itail = get<int>(jwire,"tail");
55  int ihead = get<int>(jwire,"head");
56  wire.tail = points[itail];
57  wire.head = points[ihead];
58  }
59  }
60 
61 
62  {// planes
63  Json::Value jplanes = jstore["planes"];
64  const int nplanes = jplanes.size();
65  std::vector<Plane>& planes = store->planes;
66  planes.resize(nplanes);
67  for (int iplane=0; iplane<nplanes; ++iplane) {
68  Json::Value jplane = jplanes[iplane]["Plane"];
69  Plane& plane = planes[iplane];
70  plane.ident = get<int>(jplane, "ident");
71  Json::Value jwires = jplane["wires"];
72  const int nwires = jwires.size();
73  plane.wires.resize(nwires);
74  for (int iwire=0; iwire<nwires; ++iwire) {
75  plane.wires[iwire] = convert<int>(jwires[iwire]);
76  }
77  }
78  }
79 
80  {// faces
81  Json::Value jfaces = jstore["faces"];
82  const int nfaces = jfaces.size();
83  std::vector<Face>& faces = store->faces;
84  faces.resize(nfaces);
85  for (int iface=0; iface<nfaces; ++iface) {
86  Json::Value jface = jfaces[iface]["Face"];
87  Face& face = faces[iface];
88  face.ident = get<int>(jface, "ident");
89  Json::Value jplanes = jface["planes"];
90  const int nplanes = jplanes.size();
91  face.planes.resize(nplanes);
92  for (int iplane=0; iplane<nplanes; ++iplane) {
93  face.planes[iplane] = convert<int>(jplanes[iplane]);
94  }
95  }
96  }
97 
98  {// anodes
99  Json::Value janodes = jstore["anodes"];
100  const int nanodes = janodes.size();
101  std::vector<Anode>& anodes = store->anodes;
102  anodes.resize(nanodes);
103  for (int ianode=0; ianode<nanodes; ++ianode) {
104  Json::Value janode = janodes[ianode]["Anode"];
105  Anode& anode = anodes[ianode];
106  anode.ident = get<int>(janode, "ident");
107  Json::Value jfaces = janode["faces"];
108  const int nfaces = jfaces.size();
109  anode.faces.resize(nfaces);
110  for (int iface=0; iface<nfaces; ++iface) {
111  anode.faces[iface] = convert<int>(jfaces[iface]);
112  }
113  }
114  }
115 
116  {// detectors
117  Json::Value jdets = jstore["detectors"];
118  const int ndets = jdets.size();
119  std::vector<Detector>& dets = store->detectors;
120  dets.resize(ndets);
121  for (int idet=0; idet<ndets; ++idet) {
122  Json::Value jdet = jdets[idet]["Detector"];
123  Detector& det = dets[idet];
124  det.ident = get<int>(jdet, "ident");
125  Json::Value janodes = jdet["anodes"];
126  const int nanodes = janodes.size();
127  det.anodes.resize(nanodes);
128  for (int ianode=0; ianode<nanodes; ++ianode) {
129  det.anodes[ianode] = convert<int>(janodes[ianode]);
130  }
131  }
132  }
133  gStoreCache[realpath] = StoreDBPtr(store);
134  return Store(gStoreCache[realpath]);
135 }
136 
137 
138 // void WireCell::WireSchema::dump(const char* filename, const Store& store)
139 // {
140 
141 // }
142 
143 
144 Store::Store() : m_db(nullptr) {}
145 
147 
149  : m_db(other.db())
150 {
151 }
153 {
154  m_db = other.db();
155  return *this;
156 }
157 
158 
159 StoreDBPtr Store::db() const { return m_db; }
160 
161 const std::vector<Detector>& Store::detectors() const { return m_db->detectors; }
162 const std::vector<Anode>& Store::anodes() const { return m_db->anodes; }
163 const std::vector<Face>& Store::faces() const { return m_db->faces; }
164 const std::vector<Plane>& Store::planes() const { return m_db->planes; }
165 const std::vector<Wire>& Store::wires() const { return m_db->wires; }
166 
167 const Anode& Store::anode(int ident) const {
168  for (auto& a : m_db->anodes) {
169  if (a.ident == ident) {
170  return a;
171  }
172  }
173  THROW(KeyError() << errmsg{String::format("Unknown anode: %d", ident)});
174 }
175 
176 std::vector<Anode> Store::anodes(const Detector& detector) const {
177  std::vector<Anode> ret;
178  for (auto ind : detector.anodes) {
179  ret.push_back(m_db->anodes[ind]);
180  }
181  return ret;
182 }
183 
184 
185 std::vector<Face> Store::faces(const Anode& anode) const {
186  std::vector<Face> ret;
187  for (auto ind : anode.faces) {
188  ret.push_back(m_db->faces[ind]);
189  }
190  return ret;
191 }
192 
193 
194 std::vector<Plane> Store::planes(const Face& face) const
195 {
196  std::vector<Plane> ret;
197  for (auto ind : face.planes) {
198  ret.push_back(m_db->planes[ind]);
199  }
200  return ret;
201 }
202 
203 std::vector<Wire> Store::wires(const Plane& plane) const
204 {
205  std::vector<Wire> ret;
206  for (auto ind : plane.wires) {
207  ret.push_back(m_db->wires[ind]);
208  }
209  return ret;
210 }
211 
213 {
214  BoundingBox bb;
215  for (const auto& face : faces(anode)) {
216  bb(bounding_box(face).bounds());
217  }
218  return bb;
219 }
221 {
222  BoundingBox bb;
223  for (const auto& plane : planes(face)) {
224  bb(bounding_box(plane).bounds());
225  }
226  return bb;
227 }
229 {
230  BoundingBox bb;
231  for (const auto& wire : wires(plane)) {
232  Ray ray(wire.tail, wire.head);
233  bb(ray);
234  }
235  return bb;
236 
237 }
238 
239 Ray Store::wire_pitch(const Plane& plane) const
240 {
241  Vector wtot;
242  for (const auto& wire : wires(plane)) {
243  Ray ray(wire.tail, wire.head);
244  wtot += ray_vector(ray);
245  }
246  wtot = wtot.norm();
247 
248  const Wire& w1 = m_db->wires[plane.wires.front()];
249  const Wire& w2 = m_db->wires[plane.wires.back()];
250 
251  const Vector c1 = 0.5*(w1.tail + w1.head);
252  const Vector c2 = 0.5*(w2.tail + w2.head);
253 
254  // approximate pitch, in the plane
255  Vector pit = ray_vector(Ray(c1,c2));
256  Vector ecks = wtot.cross(pit); // X-axis
257  pit = ecks.cross(wtot);
258  return Ray(wtot, pit.norm());
259 }
260 
261 
262 std::vector<int> Store::channels(const Plane& plane) const
263 {
264  std::vector<int> ret;
265  for (const auto& wire : wires(plane)) {
266  ret.push_back(wire.channel);
267  }
268  return ret;
269 }
std::pair< Point, Point > Ray
A line segment running from a first (tail) to a second (head) point.
Definition: Point.h:21
const int nwires
std::shared_ptr< const StoreDB > StoreDBPtr
Definition: WireSchema.h:59
Ray wire_pitch(const Plane &plane) const
Definition: WireSchema.cxx:239
const std::vector< Detector > & detectors() const
Definition: WireSchema.cxx:161
std::string string
Definition: nybbler.cc:12
boost::error_info< struct tag_errmsg, std::string > errmsg
Definition: Exceptions.h:54
BoundingBox bounding_box(const Anode &anode) const
Definition: WireSchema.cxx:212
static std::map< std::string, StoreDBPtr > gStoreCache
Definition: WireSchema.cxx:10
const std::vector< Anode > & anodes() const
Definition: WireSchema.cxx:162
string filename
Definition: train.py:213
std::vector< Anode > anodes
Definition: WireSchema.h:51
std::vector< Face > faces
Definition: WireSchema.h:52
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2106
std::vector< int > planes
Definition: WireSchema.h:36
D3Vector norm() const
Return a normalized vector in the direction of this vector.
Definition: D3Vector.h:91
Json::Value load(const std::string &filename, const externalvars_t &extvar=externalvars_t(), const externalvars_t &extcode=externalvars_t())
Definition: Persist.cxx:121
#define THROW(e)
Definition: Exceptions.h:25
std::vector< int > wires
Definition: WireSchema.h:31
std::vector< int > faces
Definition: WireSchema.h:41
BoundingBox bounds(int x, int y, int w, int h)
Definition: main.cpp:37
std::string format(const std::string &form, TYPES...objs)
Definition: String.h:45
Store load(const char *filename)
Definition: WireSchema.cxx:14
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
const std::vector< Wire > & wires() const
Definition: WireSchema.cxx:165
Definition: Main.h:22
const std::vector< Plane > & planes() const
Definition: WireSchema.cxx:164
const std::vector< Face > & faces() const
Definition: WireSchema.cxx:163
std::vector< Wire > wires
Definition: WireSchema.h:54
int convert< int >(const Configuration &cfg, const int &def)
Definition: Configuration.h:74
std::vector< int > channels(const Plane &plane) const
Definition: WireSchema.cxx:262
std::vector< Plane > planes
Definition: WireSchema.h:53
std::vector< Detector > detectors
Definition: WireSchema.h:50
std::vector< int > anodes
Definition: WireSchema.h:46
D3Vector cross(const D3Vector &rhs) const
Return the cross product of this vector and the other.
Definition: D3Vector.h:100
StoreDBPtr db() const
Definition: WireSchema.cxx:159
Vector ray_vector(const Ray &ray)
Definition: Point.cxx:67
Store & operator=(const Store &other)
Definition: WireSchema.cxx:152
const Anode & anode(int ident) const
Definition: WireSchema.cxx:167
std::string resolve(const std::string &filename)
Definition: Persist.cxx:99
def points(jdat)
Definition: rendertvtk.py:105
Thrown when a wrong key or has been encountered.
Definition: Exceptions.h:43