Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EDM4hepSimHitReader.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EDM4hepSimHitReader.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 <edm4hep/MCParticleCollection.h>
18 #include <edm4hep/SimTrackerHitCollection.h>
19 #include <podio/Frame.h>
20 
21 namespace ActsExamples {
22 
25  : m_cfg(config),
26  m_logger(Acts::getDefaultLogger("EDM4hepSimHitReader", level)) {
27  m_reader.openFile(m_cfg.inputPath);
28 
31 
32  m_eventsRange = std::make_pair(0, m_reader.getEntries("events"));
33 }
34 
36  return "EDM4hepSimHitReader";
37 }
38 
39 std::pair<size_t, size_t> EDM4hepSimHitReader::availableEvents() const {
40  return m_eventsRange;
41 }
42 
44  podio::Frame frame = m_reader.readEntry("events", ctx.eventNumber);
45 
46  const auto& mcParticleCollection =
47  frame.get<edm4hep::MCParticleCollection>(m_cfg.inputParticles);
48 
49  if (!m_cfg.outputParticles.empty()) {
50  SimParticleContainer::sequence_type unordered;
51 
52  for (const auto& mcParticle : mcParticleCollection) {
54  mcParticle, [](const edm4hep::MCParticle& p) {
55  ActsFatras::Barcode result;
56  // TODO dont use podio internal id
57  result.setParticle(p.id());
58  return result;
59  });
60  unordered.push_back(particle);
61  }
62 
63  // Write ordered particles container to the EventStore
65  particles.insert(unordered.begin(), unordered.end());
66  m_outputParticles(ctx, std::move(particles));
67  }
68 
69  SimHitContainer::sequence_type unordered;
70 
71  const auto& simTrackerHitCollection =
72  frame.get<edm4hep::SimTrackerHitCollection>(m_cfg.inputSimTrackerHits);
73 
74  for (const auto& simTrackerHit : simTrackerHitCollection) {
75  try {
76  auto hit = EDM4hepUtil::readSimHit(
77  simTrackerHit,
78  [](const edm4hep::MCParticle& particle) {
79  ActsFatras::Barcode result;
80  // TODO dont use podio internal id
81  result.setParticle(particle.id());
82  return result;
83  },
84  [&](std::uint64_t cellId) {
85  auto detElement = m_cfg.dd4hepDetector->geometryService->detector()
86  .volumeManager()
87  .lookupDetElement(cellId);
88  Acts::GeometryIdentifier result = detElement.volumeID();
89  return result;
90  });
91  unordered.push_back(std::move(hit));
92  } catch (...) {
93  ACTS_ERROR("EDM4hepSimHitReader: failed to convert SimTrackerHit");
94  continue;
95  }
96  }
97 
98  SimHitContainer simHits;
99  simHits.insert(unordered.begin(), unordered.end());
100  m_outputSimHits(ctx, std::move(simHits));
101 
102  return ProcessCode::SUCCESS;
103 }
104 
105 } // namespace ActsExamples