Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:50

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "ActsExamples/Io/Csv/CsvMeasurementWriter.hpp"
0010 
0011 #include "Acts/Definitions/TrackParametrization.hpp"
0012 #include "Acts/Definitions/Units.hpp"
0013 #include "Acts/Geometry/GeometryIdentifier.hpp"
0014 #include "ActsExamples/EventData/Cluster.hpp"
0015 #include "ActsExamples/EventData/Index.hpp"
0016 #include "ActsExamples/EventData/Measurement.hpp"
0017 #include "ActsExamples/Framework/AlgorithmContext.hpp"
0018 #include "ActsExamples/Io/Csv/CsvInputOutput.hpp"
0019 #include "ActsExamples/Utilities/Paths.hpp"
0020 #include "ActsExamples/Utilities/Range.hpp"
0021 #include "ActsFatras/Digitization/Channelizer.hpp"
0022 
0023 #include <array>
0024 #include <optional>
0025 #include <ostream>
0026 #include <stdexcept>
0027 #include <variant>
0028 #include <vector>
0029 
0030 #include "CsvOutputData.hpp"
0031 
0032 ActsExamples::CsvMeasurementWriter::CsvMeasurementWriter(
0033     const ActsExamples::CsvMeasurementWriter::Config& config,
0034     Acts::Logging::Level level)
0035     : WriterT(config.inputMeasurements, "CsvMeasurementWriter", level),
0036       m_cfg(config) {
0037   // Input container for measurements is already checked by base constructor
0038   if (m_cfg.inputMeasurementSimHitsMap.empty()) {
0039     throw std::invalid_argument(
0040         "Missing hit-to-simulated-hits map input collection");
0041   }
0042 
0043   m_inputMeasurementSimHitsMap.initialize(m_cfg.inputMeasurementSimHitsMap);
0044   m_inputClusters.maybeInitialize(m_cfg.inputClusters);
0045 }
0046 
0047 ActsExamples::CsvMeasurementWriter::~CsvMeasurementWriter() = default;
0048 
0049 ActsExamples::ProcessCode ActsExamples::CsvMeasurementWriter::finalize() {
0050   // Write the tree
0051   return ProcessCode::SUCCESS;
0052 }
0053 
0054 ActsExamples::ProcessCode ActsExamples::CsvMeasurementWriter::writeT(
0055     const AlgorithmContext& ctx, const MeasurementContainer& measurements) {
0056   const auto& measurementSimHitsMap = m_inputMeasurementSimHitsMap(ctx);
0057 
0058   ClusterContainer clusters;
0059 
0060   // Open per-event file for all components
0061   std::string pathMeasurements =
0062       perEventFilepath(m_cfg.outputDir, "measurements.csv", ctx.eventNumber);
0063   std::string pathMeasurementSimHitMap = perEventFilepath(
0064       m_cfg.outputDir, "measurement-simhit-map.csv", ctx.eventNumber);
0065 
0066   ActsExamples::NamedTupleCsvWriter<MeasurementData> writerMeasurements(
0067       pathMeasurements, m_cfg.outputPrecision);
0068 
0069   std::optional<ActsExamples::NamedTupleCsvWriter<CellData>> writerCells{
0070       std::nullopt};
0071   if (!m_cfg.inputClusters.empty()) {
0072     ACTS_VERBOSE(
0073         "Set up writing of clusters from collection: " << m_cfg.inputClusters);
0074     clusters = m_inputClusters(ctx);
0075     std::string pathCells =
0076         perEventFilepath(m_cfg.outputDir, "cells.csv", ctx.eventNumber);
0077     writerCells = ActsExamples::NamedTupleCsvWriter<CellData>{
0078         pathCells, m_cfg.outputPrecision};
0079   }
0080 
0081   ActsExamples::NamedTupleCsvWriter<MeasurementSimHitLink>
0082       writerMeasurementSimHitMap(pathMeasurementSimHitMap,
0083                                  m_cfg.outputPrecision);
0084 
0085   MeasurementData meas;
0086   CellData cell;
0087 
0088   // Will be reused as measurement counter
0089   meas.measurement_id = 0;
0090 
0091   ACTS_VERBOSE("Writing " << measurements.size()
0092                           << " measurements in this event.");
0093 
0094   for (Index measIdx = 0u; measIdx < measurements.size(); ++measIdx) {
0095     const ConstVariableBoundMeasurementProxy measurement =
0096         measurements.getMeasurement(measIdx);
0097 
0098     auto simHitIndices = makeRange(measurementSimHitsMap.equal_range(measIdx));
0099     for (auto [_, simHitIdx] : simHitIndices) {
0100       writerMeasurementSimHitMap.append({measIdx, simHitIdx});
0101     }
0102 
0103     Acts::GeometryIdentifier geoId = measurement.geometryId();
0104     // MEASUREMENT information ------------------------------------
0105 
0106     // Encoded geometry identifier. same for all hits on the module
0107     meas.geometry_id = geoId.value();
0108     meas.local_key = 0;
0109     // Create a full set of parameters
0110     auto parameters = measurement.fullParameters();
0111     meas.local0 = parameters[Acts::eBoundLoc0] / Acts::UnitConstants::mm;
0112     meas.local1 = parameters[Acts::eBoundLoc1] / Acts::UnitConstants::mm;
0113     meas.phi = parameters[Acts::eBoundPhi] / Acts::UnitConstants::rad;
0114     meas.theta = parameters[Acts::eBoundTheta] / Acts::UnitConstants::rad;
0115     meas.time = parameters[Acts::eBoundTime] / Acts::UnitConstants::mm;
0116 
0117     auto covariance = measurement.fullCovariance();
0118     meas.var_local0 = covariance(Acts::eBoundLoc0, Acts::eBoundLoc0);
0119     meas.var_local1 = covariance(Acts::eBoundLoc1, Acts::eBoundLoc1);
0120     meas.var_phi = covariance(Acts::eBoundPhi, Acts::eBoundPhi);
0121     meas.var_theta = covariance(Acts::eBoundTheta, Acts::eBoundTheta);
0122     meas.var_time = covariance(Acts::eBoundTime, Acts::eBoundTime);
0123     for (unsigned int ipar = 0;
0124          ipar < static_cast<unsigned int>(Acts::eBoundSize); ++ipar) {
0125       if (measurement.contains(static_cast<Acts::BoundIndices>(ipar))) {
0126         meas.local_key = ((1 << (ipar + 1)) | meas.local_key);
0127       }
0128     }
0129 
0130     writerMeasurements.append(meas);
0131 
0132     // CLUSTER / channel information ------------------------------
0133     if (!clusters.empty() && writerCells) {
0134       auto cluster = clusters[measIdx];
0135       cell.geometry_id = meas.geometry_id;
0136       cell.measurement_id = meas.measurement_id;
0137       for (auto& c : cluster.channels) {
0138         cell.channel0 = c.bin[0];
0139         cell.channel1 = c.bin[1];
0140         // TODO store digital timestamp once added to the cell definition
0141         cell.timestamp = 0;
0142         cell.value = c.activation;
0143         writerCells->append(cell);
0144       }
0145     }
0146     // Increase counter
0147     meas.measurement_id += 1;
0148   }
0149   return ActsExamples::ProcessCode::SUCCESS;
0150 }