Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EDM4hepSimHitWriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EDM4hepSimHitWriter.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 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 
16 
17 #include <stdexcept>
18 
19 #include <edm4hep/MCParticle.h>
20 #include <edm4hep/SimTrackerHit.h>
21 #include <podio/Frame.h>
22 
23 namespace ActsExamples {
24 
27  : WriterT(config.inputSimHits, "CsvSimHitWriter", level),
28  m_cfg(config),
29  m_writer(config.outputPath) {
30  ACTS_VERBOSE("Created output file " << config.outputPath);
31 
32  if (m_cfg.inputSimHits.empty()) {
33  throw std::invalid_argument("Missing simulated hits input collection");
34  }
35 
36  if (m_cfg.outputParticles.empty()) {
37  throw std::invalid_argument("Missing output particle name");
38  }
39 
40  if (m_cfg.outputSimTrackerHits.empty()) {
41  throw std::invalid_argument("Missing output sim tracker hit name");
42  }
43 
45 }
46 
48  m_writer.finish();
49 
50  return ProcessCode::SUCCESS;
51 }
52 
54  const SimHitContainer& simHits) {
55  podio::Frame frame;
56 
57  EDM4hepUtil::MapParticleIdTo particleMapper;
58  std::unordered_map<ActsFatras::Barcode, edm4hep::MutableMCParticle>
59  particleMap;
60 
61  edm4hep::MCParticleCollection mcParticles;
62  edm4hep::SimTrackerHitCollection simTrackerHitCollection;
63 
64  if (!m_cfg.inputParticles.empty()) {
65  auto particles = m_inputParticles(ctx);
66 
67  for (const auto& particle : particles) {
68  auto p = mcParticles->create();
69  particleMap[particle.particleId()] = p;
71  }
72 
73  particleMapper = [&](ActsFatras::Barcode particleId) {
74  auto it = particleMap.find(particleId);
75  if (it == particleMap.end()) {
76  throw std::runtime_error("Particle not found in map");
77  }
78  return it->second;
79  };
80  }
81 
82  for (const auto& simHit : simHits) {
83  auto simTrackerHit = simTrackerHitCollection->create();
85  simHit, simTrackerHit, particleMapper,
86  [](Acts::GeometryIdentifier id) { return id.value(); });
87  }
88 
89  frame.put(std::move(mcParticles), m_cfg.outputParticles);
90  frame.put(std::move(simTrackerHitCollection), m_cfg.outputSimTrackerHits);
91 
92  m_writer.writeFrame(frame, "events");
93 
94  return ProcessCode::SUCCESS;
95 }
96 
97 } // namespace ActsExamples