Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CsvSimHitWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CsvSimHitWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 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 
10 
20 
21 #include <stdexcept>
22 #include <vector>
23 
24 #include <dfe/dfe_io_dsv.hpp>
25 
26 #include "CsvOutputData.hpp"
27 
31  : WriterT(config.inputSimHits, "CsvSimHitWriter", level), m_cfg(config) {
32  // inputSimHits is already checked by base constructor
33  if (m_cfg.outputStem.empty()) {
34  throw std::invalid_argument("Missing output filename stem");
35  }
36 }
37 
39  const AlgorithmContext& ctx, const ActsExamples::SimHitContainer& simHits) {
40  // open per-event file for all simhit components
41  std::string pathSimHit = perEventFilepath(
42  m_cfg.outputDir, m_cfg.outputStem + ".csv", ctx.eventNumber);
43 
44  dfe::NamedTupleCsvWriter<SimHitData> writerSimHit(pathSimHit,
45  m_cfg.outputPrecision);
46 
47  // CsvOutputData struct
48  SimHitData simhit;
49  // Write data from internal impl. to output-side struct
50  for (const auto& simHit : simHits) {
51  // local simhit information in global coord.
52  const Acts::Vector4& globalPos4 = simHit.fourPosition();
53  const Acts::Vector4& momentum4Before = simHit.momentum4Before();
54 
55  simhit.geometry_id = simHit.geometryId().value();
56  simhit.particle_id = simHit.particleId().value();
57  // hit position
58  simhit.tx = globalPos4[Acts::ePos0] / Acts::UnitConstants::mm;
59  simhit.ty = globalPos4[Acts::ePos1] / Acts::UnitConstants::mm;
60  simhit.tz = globalPos4[Acts::ePos2] / Acts::UnitConstants::mm;
61  simhit.tt = globalPos4[Acts::eTime] / Acts::UnitConstants::ns;
62  // particle four-momentum before interaction
63  simhit.tpx = momentum4Before[Acts::eMom0] / Acts::UnitConstants::GeV;
64  simhit.tpy = momentum4Before[Acts::eMom1] / Acts::UnitConstants::GeV;
65  simhit.tpz = momentum4Before[Acts::eMom2] / Acts::UnitConstants::GeV;
66  simhit.te = momentum4Before[Acts::eEnergy] / Acts::UnitConstants::GeV;
67  // particle four-momentum change due to interaction
68  const auto delta4 = simHit.momentum4After() - momentum4Before;
69  simhit.deltapx = delta4[Acts::eMom0] / Acts::UnitConstants::GeV;
70  simhit.deltapy = delta4[Acts::eMom1] / Acts::UnitConstants::GeV;
71  simhit.deltapz = delta4[Acts::eMom2] / Acts::UnitConstants::GeV;
73  // TODO write hit index along the particle trajectory
74  simhit.index = simHit.index();
75  writerSimHit.append(simhit);
76  } // end simHit loop
77 
79 }