ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JsonSpacePointWriter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JsonSpacePointWriter.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017 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 
12 
13 #pragma once
14 
15 #include <fstream>
16 
17 #include "ACTFW/EventData/DataContainers.hpp"
20 
21 namespace FW {
22 namespace Json {
23 
33 template <class T>
34 class JsonSpacePointWriter : public WriterT<GeometryIdMultimap<T>> {
35  public:
36  struct Config {
37  std::string collection;
38  std::string outputDir;
39  size_t outputPrecision = 6;
40  };
41 
42  JsonSpacePointWriter(const Config& cfg,
44 
45  protected:
48  const GeometryIdMultimap<T>& spacePoints) final override;
49 
50  private:
51  // since class itself is templated, base class template must be fixed
53 
55 };
56 
57 } // namespace Json
58 } // namespace FW
59 
60 template <class T>
64  : Base(cfg.collection, "JsonSpacePointWriter", level), m_cfg(cfg) {
65  if (m_cfg.collection.empty()) {
66  throw std::invalid_argument("Missing input collection");
67  }
68 }
69 
70 template <class T>
73  const GeometryIdMultimap<T>& spacePoints) {
74  // open per-event file
75  std::string path = perEventFilepath(m_cfg.outputDir, "spacepoints.json",
76  context.eventNumber);
77  std::ofstream os(path, std::ofstream::out | std::ofstream::trunc);
78  if (!os) {
79  throw std::ios_base::failure("Could not open '" + path + "' to write");
80  }
81 
82  os << std::setprecision(m_cfg.outputPrecision);
83  os << "{\n";
84 
85  bool firstVolume = true;
86  for (auto& volumeData : spacePoints) {
87  geo_id_value volumeID = volumeData.first;
88 
89  if (!firstVolume)
90  os << ",\n";
91  os << " \"SpacePoints_" << volumeID << "\" : [\n";
92 
93  bool firstPoint = true;
94  for (auto& layerData : volumeData.second) {
95  for (auto& moduleData : layerData.second) {
96  for (auto& data : moduleData.second) {
97  // set the comma correctly
98  if (!firstPoint)
99  os << ",\n";
100  // write the space point
101  os << " [" << data.x() << ", " << data.y() << ", " << data.z()
102  << "]";
103  firstPoint = false;
104  }
105  }
106  }
107  os << "]";
108  firstVolume = false;
109  }
110  os << "\n}\n";
111 
112  return ProcessCode::SUCCESS;
113 }