EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CsvPlanarClusterWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CsvPlanarClusterWriter.cpp
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 
10 
12 #include "Acts/Utilities/Units.hpp"
18 
19 #include <stdexcept>
20 
21 #include <dfe/dfe_io_dsv.hpp>
22 
23 #include "TrackMlData.hpp"
24 
28  : WriterT(cfg.inputClusters, "CsvPlanarClusterWriter", lvl), m_cfg(cfg) {
29  // inputClusters is already checked by base constructor
30  if (m_cfg.inputSimulatedHits.empty()) {
31  throw std::invalid_argument("Missing simulated hits input collection");
32  }
33 }
34 
36  const AlgorithmContext& ctx,
38  clusters) {
39  // retrieve simulated hits
40  const auto& simHits =
41  ctx.eventStore.get<SimHitContainer>(m_cfg.inputSimulatedHits);
42 
43  // open per-event file for all components
44  std::string pathHits =
45  perEventFilepath(m_cfg.outputDir, "hits.csv", ctx.eventNumber);
46  std::string pathCells =
47  perEventFilepath(m_cfg.outputDir, "cells.csv", ctx.eventNumber);
48  std::string pathTruth =
49  perEventFilepath(m_cfg.outputDir, "truth.csv", ctx.eventNumber);
50 
51  dfe::NamedTupleCsvWriter<HitData> writerHits(pathHits, m_cfg.outputPrecision);
52  dfe::NamedTupleCsvWriter<CellData> writerCells(pathCells,
53  m_cfg.outputPrecision);
54  dfe::NamedTupleCsvWriter<TruthHitData> writerTruth(pathTruth,
55  m_cfg.outputPrecision);
56 
57  HitData hit;
58  CellData cell;
59  TruthHitData truth;
60  // will be reused as hit counter
61  hit.hit_id = 0;
62 
63  for (const auto& entry : clusters) {
64  Acts::GeometryIdentifier geoId = entry.first;
65  const Acts::PlanarModuleCluster& cluster = entry.second;
66  // local cluster information
67  const auto& parameters = cluster.parameters();
68  Acts::Vector2D localPos(parameters[0], parameters[1]);
69  Acts::Vector3D globalFakeMom(1, 1, 1);
70  Acts::Vector3D globalPos = cluster.referenceObject().localToGlobal(
71  ctx.geoContext, localPos, globalFakeMom);
72 
73  // encoded geometry identifier
74  hit.geometry_id = geoId.value();
75  // (partially) decoded geometry identifier
76  hit.volume_id = geoId.volume();
77  hit.layer_id = geoId.layer();
78  hit.module_id = geoId.sensitive();
79  // write global hit information
80  hit.x = globalPos.x() / Acts::UnitConstants::mm;
81  hit.y = globalPos.y() / Acts::UnitConstants::mm;
82  hit.z = globalPos.z() / Acts::UnitConstants::mm;
84  writerHits.append(hit);
85 
86  // write local cell information
87  cell.hit_id = hit.hit_id;
88  for (auto& c : cluster.digitizationCells()) {
89  cell.ch0 = c.channel0;
90  cell.ch1 = c.channel1;
91  // TODO store digitial timestamp once added to the cell definition
92  cell.timestamp = 0;
93  cell.value = c.data;
94  writerCells.append(cell);
95  }
96 
97  // write hit-particle truth association
98  // each hit can have multiple particles, e.g. in a dense environment
99  truth.hit_id = hit.hit_id;
100  truth.geometry_id = hit.geometry_id;
101  for (auto idx : cluster.sourceLink().indices()) {
102  auto it = simHits.nth(idx);
103  if (it == simHits.end()) {
104  ACTS_FATAL("Simulation hit with index " << idx << " does not exist");
105  return ProcessCode::ABORT;
106  }
107 
108  const auto& simHit = *it;
109  truth.particle_id = simHit.particleId().value();
110  // hit position
111  truth.tx = simHit.position().x() / Acts::UnitConstants::mm;
112  truth.ty = simHit.position().y() / Acts::UnitConstants::mm;
113  truth.tz = simHit.position().z() / Acts::UnitConstants::mm;
114  truth.tt = simHit.time() / Acts::UnitConstants::ns;
115  // particle four-momentum before interaction
116  truth.tpx = simHit.momentum4Before().x() / Acts::UnitConstants::GeV;
117  truth.tpy = simHit.momentum4Before().y() / Acts::UnitConstants::GeV;
118  truth.tpz = simHit.momentum4Before().z() / Acts::UnitConstants::GeV;
119  truth.te = simHit.momentum4Before().w() / Acts::UnitConstants::GeV;
120  // particle four-momentum change due to interaction
121  const auto delta4 = simHit.momentum4After() - simHit.momentum4Before();
122  truth.deltapx = delta4.x() / Acts::UnitConstants::GeV;
123  truth.deltapy = delta4.y() / Acts::UnitConstants::GeV;
124  truth.deltapz = delta4.z() / Acts::UnitConstants::GeV;
125  truth.deltae = delta4.w() / Acts::UnitConstants::GeV;
126  // TODO write hit index along the particle trajectory
127  truth.index = simHit.index();
128  writerTruth.append(truth);
129  }
130 
131  // increase hit id for next iteration
132  hit.hit_id += 1;
133  }
134 
136 }