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 "ActsExamples/EventData/DataContainers.hpp"
18 
19 #include <fstream>
20 
21 namespace ActsExamples {
22 
32 template <class T>
33 class JsonSpacePointWriter : public WriterT<GeometryIdMultimap<T>> {
34  public:
35  struct Config {
36  std::string collection;
37  std::string outputDir;
38  size_t outputPrecision = 6;
39  };
40 
41  JsonSpacePointWriter(const Config& cfg,
43 
44  protected:
46  const ActsExamples::AlgorithmContext& context,
47  const GeometryIdMultimap<T>& spacePoints) final override;
48 
49  private:
50  // since class itself is templated, base class template must be fixed
52 
54 };
55 
56 } // namespace ActsExamples
57 
58 template <class T>
62  : Base(cfg.collection, "JsonSpacePointWriter", level), m_cfg(cfg) {
63  if (m_cfg.collection.empty()) {
64  throw std::invalid_argument("Missing input collection");
65  }
66 }
67 
68 template <class T>
70  const ActsExamples::AlgorithmContext& context,
71  const GeometryIdMultimap<T>& spacePoints) {
72  // open per-event file
73  std::string path = perEventFilepath(m_cfg.outputDir, "spacepoints.json",
74  context.eventNumber);
75  std::ofstream os(path, std::ofstream::out | std::ofstream::trunc);
76  if (!os) {
77  throw std::ios_base::failure("Could not open '" + path + "' to write");
78  }
79 
80  os << std::setprecision(m_cfg.outputPrecision);
81  os << "{\n";
82 
83  bool firstVolume = true;
84  for (auto& volumeData : spacePoints) {
85  geo_id_value volumeID = volumeData.first;
86 
87  if (!firstVolume)
88  os << ",\n";
89  os << " \"SpacePoints_" << volumeID << "\" : [\n";
90 
91  bool firstPoint = true;
92  for (auto& layerData : volumeData.second) {
93  for (auto& moduleData : layerData.second) {
94  for (auto& data : moduleData.second) {
95  // set the comma correctly
96  if (!firstPoint)
97  os << ",\n";
98  // write the space point
99  os << " [" << data.x() << ", " << data.y() << ", " << data.z()
100  << "]";
101  firstPoint = false;
102  }
103  }
104  }
105  os << "]";
106  firstVolume = false;
107  }
108  os << "\n}\n";
109 
110  return ProcessCode::SUCCESS;
111 }