sandreco  1.0.8
A modular reconstruction software for SAND
sand.h
1 #pragma once
2 
3 #include <common/data.h>
4 
5 #include <cctype>
6 #include <cstdint>
7 #include <string>
8 #include <string_view>
9 
10 #include <Math/Rotation3D.h>
11 #include <Math/SMatrix.h>
12 #include <Math/SVector.h>
13 #include <Math/Transform3D.h>
14 #include <Math/Vector3D.h>
15 #include <Math/Vector4D.h>
16 
17 #include <ufw/utils.hpp>
18 
19 namespace sand {
20 
24  using pos_3d = ROOT::Math::PositionVector3D<ROOT::Math::Cartesian3D<double>>;
25  using dir_3d = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>>;
26  using vec_4d = ROOT::Math::XYZTVector;
27 
28  using mom_3d = dir_3d;
29  using mom_4d = ROOT::Math::PxPyPzEVector;
30 
31  using rot_3d = ROOT::Math::Rotation3D;
39  using xform_3d = ROOT::Math::Transform3D;
40 
41 } // namespace sand
42 
43 template <>
44 struct fmt::formatter<sand::pos_3d> : formatter<string_view> {
45  auto format(const sand::pos_3d& c, format_context& ctx) const -> format_context::iterator {
46  return fmt::format_to(ctx.out(), "({:.3f}, {:.3f}, {:.3f})", c.x(), c.y(), c.z());
47  }
48 };
49 
50 template <>
51 struct fmt::formatter<sand::dir_3d> : formatter<string_view> {
52  auto format(const sand::dir_3d& c, format_context& ctx) const -> format_context::iterator {
53  return fmt::format_to(ctx.out(), "({:.3f}, {:.3f}, {:.3f})", c.x(), c.y(), c.z());
54  }
55 };
56 
57 template <>
58 struct fmt::formatter<sand::vec_4d> : formatter<string_view> {
59  auto format(const sand::vec_4d& c, format_context& ctx) const -> format_context::iterator {
60  return fmt::format_to(ctx.out(), "({:.3f}, {:.3f}, {:.3f}, {:.3f})", c.x(), c.y(), c.z(), c.t());
61  }
62 };
63 
64 template <>
65 struct fmt::formatter<sand::rot_3d> : formatter<string_view> {
66  auto format(const sand::rot_3d& c, format_context& ctx) const -> format_context::iterator {
67  sand::rot_3d::Scalar r[9];
68  c.GetComponents(r);
69  return fmt::format_to(ctx.out(), "R(({:.3f}, {:.3f}, {:.3f}), ({:.3f}, {:.3f}, {:.3f}), ({:.3f}, {:.3f}, {:.3f}))",
70  r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]);
71  }
72 };
73 
74 template <>
75 struct fmt::formatter<sand::xform_3d> : formatter<string_view> {
76  auto format(const sand::xform_3d& c, format_context& ctx) const -> format_context::iterator {
77  sand::xform_3d::Scalar r[12];
78  c.GetComponents(r);
79  return fmt::format_to(
80  ctx.out(),
81  "R(({:.3f}, {:.3f}, {:.3f}), ({:.3f}, {:.3f}, {:.3f}), ({:.3f}, {:.3f}, {:.3f})), T({:.3f}, {:.3f}, {:.3f}))",
82  r[0], r[1], r[2], r[4], r[5], r[6], r[8], r[9], r[10], r[3], r[7], r[11]);
83  }
84 };
85 
86 namespace sand {
90  class geo_path : public std::string {
91  public:
92  using std::string::string;
93  using std::string::operator=;
94 
95  inline geo_path& operator/= (const std::string_view&);
96 
97  geo_path operator/ (const std::string_view& rhs) const {
98  geo_path p(*this);
99  return p /= rhs;
100  }
101 
102  inline geo_path& operator-= (const geo_path&);
103 
104  geo_path operator- (const geo_path& rhs) const {
105  geo_path p(*this);
106  return p -= rhs;
107  }
108 
109  inline std::string_view token(std::size_t) const;
110  };
111 
115  inline geo_path& geo_path::operator/= (const std::string_view& sv) {
116  if (empty()) {
117  assign(sv);
118  return *this;
119  }
120  if (sv.empty()) {
121  return *this;
122  }
123  if (back() == '/') {
124  if (sv.front() == '/') {
125  append(sv.substr(1));
126  } else {
127  append(sv);
128  }
129  } else {
130  if (sv.front() == '/') {
131  append(sv);
132  } else {
133  reserve(size() + 1 + sv.size());
134  push_back('/');
135  append(sv);
136  }
137  }
138  return *this;
139  }
140 
147  if (std::string_view(sub.data(), sub.size()) == sub) {
148  erase(0, sub.size());
149  }
150  return *this;
151  }
152 
156  inline std::string_view geo_path::token(std::size_t i) const {
157  std::size_t start = 0;
158  std::size_t stop = 0;
159  if (empty()) {
160  return std::string_view();
161  }
162  if (at(0) == '/') {
163  start = 1;
164  }
165  while (i--) {
166  start = find('/', start) + 1;
167  }
168  stop = find('/', start);
169  if (stop == std::string::npos) {
170  stop = size();
171  }
172  return std::string_view(data() + start, stop - start);
173  }
174 
178  enum subdetector_t : uint8_t {
179  kSandBegin = 0,
180 
181  ECAL = kSandBegin,
182  GRAIN = 1,
183  STT = 2,
184  DRIFT = 3,
185  MUON = 4,
186  MAGNET= 5,
187 
188  kSandEnd = 6,
189 
190  WORLD = 7,
191  OTHER = 8,
192 
193  kLoopableEnd = 9,
194 
195  GENERIC_DRIFT = 10,
196  TRACKER = STT,
197 
198  NONE = 255
199  };
200 
201  static constexpr size_t kNumComponents = static_cast<size_t>(subdetector_t::kLoopableEnd);
202 
206  inline std::map<subdetector_t, std::string> component_to_string = {
207  {subdetector_t::GRAIN, "LArHit"},
208  {subdetector_t::STT, "Straw"},
209  {subdetector_t::DRIFT, "DriftVolume"},
210  {subdetector_t::ECAL, "EMCalSci"},
211  {subdetector_t::MAGNET, "Magnet"},
212  {subdetector_t::WORLD, "World"},
213  };
214 
218  inline std::map<std::string, subdetector_t> string_to_component = {
219  {"LArHit", subdetector_t::GRAIN},
220  {"Straw", subdetector_t::STT},
221  {"DriftVolume", subdetector_t::DRIFT},
222  {"EMCalSci", subdetector_t::ECAL},
223  {"Magnet", subdetector_t::MAGNET},
224  {"World", subdetector_t::WORLD},
225  };
226 
230  inline std::initializer_list<std::string> grain_names = {"GRAIN", "GRIAN"};
231 
235  inline std::initializer_list<std::string> stt_names = {"horizontalST", "STT", "TrkMod", "CMod",
236  "C3H6Mod", "Trk", "SuperMod", "Drift"};
237 
242  inline bool is_generic_drift_name(std::string_view name) {
243  if (name.size() <= 2 || name.substr(0, 2) != "s_") return false;
244  auto digits_end = name.find_first_not_of("0123456789", 2);
245  if (digits_end == 2) return false;
246  if (digits_end == std::string_view::npos) return true;
247  return name[digits_end] == '_' && digits_end + 1 < name.size();
248  }
249 
253  inline std::initializer_list<std::string> drift_names = {"SANDtracker", "Trk", "CMod", "C3H6Mod", "SuperMod", "Drift",
254  "C3H6Target", "CTarget", "C3H6Mylar", "CMylar", "Frame"};
255 
259  inline std::initializer_list<std::string> ecal_names = {"ECAL", "kloe_calo_volume"};
260 
264  inline std::initializer_list<std::string> magnet_names = {"KLOE", "Yoke", "Mag"};
265 
269  inline std::initializer_list<std::string> world_names = {"World", "rock", "volSAND",
270  "sand_inner", "Enclosure", "volDetEnclosure"};
276  struct geo_id {
277  using supermodule_t = uint8_t;
278  using plane_t = uint8_t;
279  using tube_t = uint16_t;
280 
281  enum region_t : uint8_t { BARREL = 0, ENDCAP_A = 1, ENDCAP_B = 2, UNKNOWN = 255 };
282  enum element_t : uint8_t {
283  ENDCAP_VERTICAL = 1,
284  ENDCAP_CURVE_TOP = 2,
285  ENDCAP_CURVE_BOT = 3,
286  ENDCAP_HOR_TOP = 4,
287  ENDCAP_HOR_BOT = 5,
288  NONE = 255
289  };
290  // ROOT reports an internal error when generating a dictionary for this anonymous union.
291  // Since we don't care too much, we can just pretend it is just an int.
292 #ifndef __CLING__
293  union {
294  struct {
295  uint8_t reserved___0;
296  subdetector_t subdetector;
297  uint8_t padding___1[6];
298  } /*any*/;
299  struct {
300  uint8_t reserved___0;
301  subdetector_t subdetector;
302  supermodule_t supermodule;
303  plane_t plane;
304  uint8_t padding___1[4];
305  } drift;
306  struct {
307  uint8_t reserved___0;
308  subdetector_t subdetector;
309  region_t region;
310  supermodule_t supermodule;
311  element_t element;
312  plane_t plane;
313  uint8_t padding___1[2];
314  } ecal;
315  struct {
316  uint8_t reserved___0;
317  subdetector_t subdetector;
318  uint8_t padding___1[6];
319  } grain;
320  struct {
321  uint8_t reserved___0;
322  subdetector_t subdetector;
323  supermodule_t supermodule;
324  plane_t plane;
325  tube_t tube;
326  uint8_t padding___1[2];
327  } stt;
328  uint64_t raw = -1;
329  };
330 #else //__CLING__
331  uint64_t raw = -1;
332 #endif //__CLING__
333  };
334 
335  static_assert(sizeof(geo_id) == sizeof(uint64_t), "geo_id size is not 8 bytes");
336 
337  // Equality operator
338  inline bool operator== (geo_id lhs, geo_id rhs) { return lhs.raw == rhs.raw; }
339 
340  // Less-than operator for ordering
341  inline bool operator< (geo_id lhs, geo_id rhs) { return lhs.raw < rhs.raw; }
342 
347  struct channel_id {
348  using link_t = uint8_t;
349  using channel_t = uint32_t;
350  // ROOT reports an internal error when generating a dictionary for this anonymous union.
351  // Since we don't care too much, we can just pretend it is just an int.
352 #ifndef __CLING__
353  union {
354  struct {
355  uint8_t reserved___0;
356  subdetector_t subdetector;
357  link_t link;
358  uint8_t padding___1;
359  channel_t channel;
360  } /*any*/;
361  uint64_t raw = -1;
362  };
363 #else //__CLING__
364  uint64_t raw = -1;
365 #endif //__CLING__
366  };
367 
368  // Equality operator
369  inline bool operator== (channel_id lhs, channel_id rhs) { return lhs.raw == rhs.raw; }
370 
371  // Equality operator
372  inline bool operator!= (channel_id lhs, channel_id rhs) { return lhs.raw != rhs.raw; }
373 
374  // Less-than operator for ordering
375  inline bool operator< (channel_id lhs, channel_id rhs) { return lhs.raw < rhs.raw; }
376 
377 } // namespace sand
378 
379 #ifndef __CLING__
380 
381 template <>
382 struct fmt::formatter<sand::subdetector_t> : formatter<string_view> {
383  auto format(sand::subdetector_t s, format_context& ctx) const -> format_context::iterator {
384  switch (s) {
385  case sand::DRIFT:
386  return fmt::format_to(ctx.out(), "DRIFT");
387  case sand::ECAL:
388  return fmt::format_to(ctx.out(), "ECAL");
389  case sand::GRAIN:
390  return fmt::format_to(ctx.out(), "GRAIN");
391  case sand::STT:
392  return fmt::format_to(ctx.out(), "STT");
393  case sand::MUON:
394  return fmt::format_to(ctx.out(), "MUON");
395  default:
396  return fmt::format_to(ctx.out(), "NONE");
397  }
398  }
399 };
400 
401 template <>
402 struct fmt::formatter<sand::geo_id> : formatter<string_view> {
403  auto format(const sand::geo_id& gid, format_context& ctx) const -> format_context::iterator {
404  switch (gid.subdetector) {
405  case sand::DRIFT:
406  return fmt::format_to(ctx.out(), "[{}: module {}, plane {}]", gid.subdetector, gid.drift.supermodule,
407  gid.drift.plane);
408  case sand::ECAL:
409  return fmt::format_to(ctx.out(), "[{}: region {}, module {}, element {}, plane {}]", gid.subdetector,
410  gid.ecal.region, gid.ecal.supermodule, gid.ecal.element, gid.ecal.plane);
411  case sand::GRAIN:
412  return fmt::format_to(ctx.out(), "[{}: inner vessel]", gid.subdetector); // grain has no substructure to report
413  case sand::STT:
414  return fmt::format_to(ctx.out(), "[{}: module {}, plane {}, tube {}]", gid.subdetector, gid.stt.supermodule,
415  gid.stt.plane, gid.stt.tube);
416  default:
417  return fmt::format_to(ctx.out(), "[{}: not a valid subdetector]", gid.subdetector);
418  }
419  }
420 };
421 
422 template <>
423 struct fmt::formatter<sand::channel_id> : formatter<string_view> {
424  auto format(const sand::channel_id& chid, format_context& ctx) const -> format_context::iterator {
425  return fmt::format_to(ctx.out(), "[{}: link {}, channel {}]", chid.subdetector, chid.link, chid.channel);
426  }
427 };
428 
429 #endif //__CLING__
Definition: sand.h:90
geo_path & operator/=(const std::string_view &)
Definition: sand.h:115
std::string_view token(std::size_t) const
Definition: sand.h:156
geo_path & operator-=(const geo_path &)
Definition: sand.h:146
Definition: sand.h:347
Definition: sand.h:276