EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ObjPropagationStepsWriter.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ObjPropagationStepsWriter.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 
9 #pragma once
10 
14 
15 #include <fstream>
16 
17 namespace ActsExamples {
18 
28 template <typename step_t>
30  : public WriterT<std::vector<std::vector<step_t>>> {
31  public:
32  struct Config {
33  std::string collection;
34  std::string outputDir;
35  double outputScalor = 1.0;
36  size_t outputPrecision = 6;
37  };
38 
45  : WriterT<std::vector<std::vector<step_t>>>(cfg.collection,
46  "ObjSpacePointWriter", level),
47  m_cfg(cfg) {
48  if (m_cfg.collection.empty()) {
49  throw std::invalid_argument("Missing input collection");
50  }
51  }
52 
54  ~ObjPropagationStepsWriter() override = default;
55 
57  ProcessCode endRun() final override {
59  }
60 
61  private:
63 
64  protected:
68  const AlgorithmContext& context,
69  const std::vector<std::vector<step_t>>& stepCollection) final override {
70  // open per-event file
71  std::string path = ActsExamples::perEventFilepath(
72  m_cfg.outputDir, "propagation-steps.obj", context.eventNumber);
73  std::ofstream os(path, std::ofstream::out | std::ofstream::trunc);
74  if (!os) {
75  throw std::ios_base::failure("Could not open '" + path + "' to write");
76  }
77 
78  // Initialize the vertex counter
79  unsigned int vCounter = 0;
80 
81  for (auto& steps : stepCollection) {
82  // At least three points to draw
83  if (steps.size() > 2) {
84  // We start from one
85  ++vCounter;
86  for (auto& step : steps) {
87  // Write the space point
88  os << "v " << m_cfg.outputScalor * step.position.x() << " "
89  << m_cfg.outputScalor * step.position.y() << " "
90  << m_cfg.outputScalor * step.position.z() << '\n';
91  }
92  // Write out the line - only if we have at least two points created
93  size_t vBreak = vCounter + steps.size() - 1;
94  for (; vCounter < vBreak; ++vCounter)
95  os << "l " << vCounter << " " << vCounter + 1 << '\n';
96  }
97  }
99  }
100 };
101 
102 } // namespace ActsExamples