Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CsvMeasurementWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CsvMeasurementWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2021 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 
21 
22 #include <array>
23 #include <optional>
24 #include <ostream>
25 #include <stdexcept>
26 #include <variant>
27 #include <vector>
28 
29 #include <dfe/dfe_io_dsv.hpp>
30 
31 #include "CsvOutputData.hpp"
32 
36  : WriterT(config.inputMeasurements, "CsvMeasurementWriter", level),
37  m_cfg(config) {
38  // Input container for measurements is already checked by base constructor
39  if (m_cfg.inputMeasurementSimHitsMap.empty()) {
40  throw std::invalid_argument(
41  "Missing hit-to-simulated-hits map input collection");
42  }
43 
46 }
47 
49 
51  // Write the tree
52  return ProcessCode::SUCCESS;
53 }
54 
56  const AlgorithmContext& ctx, const MeasurementContainer& measurements) {
57  const auto& measurementSimHitsMap = m_inputMeasurementSimHitsMap(ctx);
58 
60 
61  // Open per-event file for all components
62  std::string pathMeasurements =
63  perEventFilepath(m_cfg.outputDir, "measurements.csv", ctx.eventNumber);
64  std::string pathMeasurementSimHitMap = perEventFilepath(
65  m_cfg.outputDir, "measurement-simhit-map.csv", ctx.eventNumber);
66 
67  dfe::NamedTupleCsvWriter<MeasurementData> writerMeasurements(
68  pathMeasurements, m_cfg.outputPrecision);
69 
70  std::optional<dfe::NamedTupleCsvWriter<CellData>> writerCells{std::nullopt};
71  if (not m_cfg.inputClusters.empty()) {
73  "Set up writing of clusters from collection: " << m_cfg.inputClusters);
74  clusters = m_inputClusters(ctx);
75  std::string pathCells =
76  perEventFilepath(m_cfg.outputDir, "cells.csv", ctx.eventNumber);
77  writerCells =
78  dfe::NamedTupleCsvWriter<CellData>{pathCells, m_cfg.outputPrecision};
79  }
80 
81  dfe::NamedTupleCsvWriter<MeasurementSimHitLink> writerMeasurementSimHitMap(
82  pathMeasurementSimHitMap, m_cfg.outputPrecision);
83 
84  MeasurementData meas;
85  CellData cell;
86 
87  // Will be reused as measurement counter
88  meas.measurement_id = 0;
89 
90  ACTS_VERBOSE("Writing " << measurements.size()
91  << " measurements in this event.");
92 
93  for (Index measIdx = 0u; measIdx < measurements.size(); ++measIdx) {
94  const auto& measurement = measurements[measIdx];
95 
96  auto simHitIndices = makeRange(measurementSimHitsMap.equal_range(measIdx));
97  for (auto [_, simHitIdx] : simHitIndices) {
98  writerMeasurementSimHitMap.append({measIdx, simHitIdx});
99  }
100 
101  std::visit(
102  [&](const auto& m) {
104  m.sourceLink().template get<IndexSourceLink>().geometryId();
105  // MEASUREMENT information ------------------------------------
106 
107  // Encoded geometry identifier. same for all hits on the module
108  meas.geometry_id = geoId.value();
109  meas.local_key = 0;
110  // Create a full set of parameters
111  auto parameters = (m.expander() * m.parameters()).eval();
117 
118  auto covariance =
119  (m.expander() * m.covariance() * m.expander().transpose()).eval();
125  for (unsigned int ipar = 0;
126  ipar < static_cast<unsigned int>(Acts::eBoundSize); ++ipar) {
127  if (m.contains(static_cast<Acts::BoundIndices>(ipar))) {
128  meas.local_key = ((1 << (ipar + 1)) | meas.local_key);
129  }
130  }
131 
132  writerMeasurements.append(meas);
133 
134  // CLUSTER / channel information ------------------------------
135  if (not clusters.empty() && writerCells) {
136  auto cluster = clusters[measIdx];
137  cell.geometry_id = meas.geometry_id;
138  cell.measurement_id = meas.measurement_id;
139  for (auto& c : cluster.channels) {
140  cell.channel0 = c.bin[0];
141  cell.channel1 = c.bin[1];
142  // TODO store digital timestamp once added to the cell definition
143  cell.timestamp = 0;
144  cell.value = c.activation;
145  writerCells->append(cell);
146  }
147  }
148  // Increase counter
149  meas.measurement_id += 1;
150  },
151  measurement);
152  }
154 }