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