Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SvgPointWriter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SvgPointWriter.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
19 
20 #include <fstream>
21 #include <mutex>
22 
23 namespace ActsExamples {
24 
27 struct AccessorXYZ {
28  template <typename T>
29  auto x(const T& o) const {
30  return o.x();
31  }
32 
33  template <typename T>
34  auto y(const T& o) const {
35  return o.y();
36  }
37 
38  template <typename T>
39  auto z(const T& o) const {
40  return o.z();
41  }
42 };
43 
47  template <typename T>
48  auto x(const T& o) const {
49  return o.position().x();
50  }
51 
52  template <typename T>
53  auto y(const T& o) const {
54  return o.position().y();
55  }
56 
57  template <typename T>
58  auto z(const T& o) const {
59  return o.position().z();
60  }
61 };
62 
76 template <typename T, typename Acc = AccessorXYZ>
77 class SvgPointWriter final : public WriterT<GeometryIdMultiset<T>> {
78  public:
79  struct Config {
80  std::string writerName = "PointWriter";
83  size_t outputPrecision = 6;
84 
87  s_pointStyle;
88 
90  Acts::Svg::Style infoBoxStyle = s_infoStyle; // The style of the info box
91 
92  bool projectionXY = true;
93  std::array<Acts::ActsScalar, 2> zRangeXY = {
94  std::numeric_limits<Acts::ActsScalar>::lowest(),
95  std::numeric_limits<Acts::ActsScalar>::max()};
96 
97  bool projectionZR = true;
98 
99  std::shared_ptr<const Acts::TrackingGeometry> trackingGeometry =
100  nullptr;
101 
102 
105  };
106 
107  SvgPointWriter(const Config& cfg,
109 
110  protected:
112  const ActsExamples::AlgorithmContext& context,
113  const GeometryIdMultiset<T>& pointCollection) final;
114 
115  private:
117 
118  std::mutex m_writeMutex;
119 };
120 
121 } // namespace ActsExamples
122 
123 template <typename T, typename Acc>
127  : WriterT<GeometryIdMultiset<T>>(cfg.inputCollection, cfg.writerName,
128  level),
129  m_cfg(cfg) {
130  if (m_cfg.inputCollection.empty()) {
131  throw std::invalid_argument("Missing input collection");
132  }
133 }
134 
135 template <typename T, typename Acc>
137  const ActsExamples::AlgorithmContext& context,
138  const GeometryIdMultiset<T>& pointCollection) {
139  // Ensure exclusive access to file writing
140  std::lock_guard<std::mutex> lock(m_writeMutex);
141 
142  Acc xyz;
143 
144  // Open per-event files
145  std::string pathXY =
146  perEventFilepath(m_cfg.outputDir, "pointsXY.svg", context.eventNumber);
147 
148  // Open per-event files
149  std::string pathZR =
150  perEventFilepath(m_cfg.outputDir, "pointsZR.svg", context.eventNumber);
151 
153  tgpOptions.trackingGeometryOptions = m_cfg.trackingGeometryOptions;
154  auto [xyView, zrView] = Acts::Svg::TrackingGeometryProjections::convert(
155  context.geoContext, *m_cfg.trackingGeometry, tgpOptions);
156 
157  // Fill the space points
158  unsigned int id = 0;
159  for (const auto& data : pointCollection) {
160  // Use the accessor to build an x/y view
161  Acts::Vector3 point3D = {xyz.x(data), xyz.y(data), xyz.z(data)};
162  // Draw the xy view
163  if (m_cfg.projectionXY and point3D.z() >= m_cfg.zRangeXY[0] and
164  point3D.z() <= m_cfg.zRangeXY[1]) {
165  auto p = Acts::Svg::EventDataConverter::pointXY(point3D, m_cfg.spSize,
166  m_cfg.spStyle, id);
167  xyView.add_object(p);
168  // Draw a connected text box
169  if (not m_cfg.infoBoxTitle.empty()) {
170  auto xyIbox = Acts::Svg::infoBox(
171  static_cast<actsvg::scalar>(point3D.x() + 10.),
172  static_cast<actsvg::scalar>(point3D.y() - 10.), m_cfg.infoBoxTitle,
173  {"Position: " + Acts::toString(point3D)}, m_cfg.infoBoxStyle, p);
174  xyView.add_object(xyIbox);
175  }
176  }
177  // Draw the zy view
178  if (m_cfg.projectionZR) {
179  auto p = Acts::Svg::EventDataConverter::pointZR(point3D, m_cfg.spSize,
180  m_cfg.spStyle, id);
181 
182  zrView.add_object(p);
183  }
184  ++id;
185  }
186 
187  Acts::Svg::toFile({xyView}, pathXY);
188  Acts::Svg::toFile({zrView}, pathZR);
189 
190  return ProcessCode::SUCCESS;
191 }