EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventGenerator.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EventGenerator.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019-2020 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 
12 
13 #include <algorithm>
14 #include <cstdint>
15 #include <stdexcept>
16 
19  : m_cfg(cfg), m_logger(Acts::getDefaultLogger("EventGenerator", lvl)) {
20  if (m_cfg.outputParticles.empty()) {
21  throw std::invalid_argument("Missing output particles collection");
22  }
23  if (m_cfg.generators.empty()) {
24  throw std::invalid_argument("No generators are configured");
25  }
26  if (!m_cfg.randomNumbers) {
27  throw std::invalid_argument("Missing random numbers service");
28  }
29 }
30 
32  return "EventGenerator";
33 }
34 
36  const {
37  return {0u, SIZE_MAX};
38 }
39 
41  const AlgorithmContext& ctx) {
42  SimParticleContainer particles;
43 
44  auto rng = m_cfg.randomNumbers->spawnGenerator(ctx);
45 
46  size_t nPrimaryVertices = 0;
47  for (size_t iGenerate = 0; iGenerate < m_cfg.generators.size(); ++iGenerate) {
48  auto& generate = m_cfg.generators[iGenerate];
49 
50  // generate the primary vertices from this generator
51  for (size_t n = generate.multiplicity(rng); 0 < n; --n) {
52  nPrimaryVertices += 1;
53 
54  // generate primary vertex position
55  auto vertexPosition = generate.vertex(rng);
56  // generate particles associated to this vertex
57  auto vertexParticles = generate.particles(rng);
58 
59  auto updateParticleInPlace = [&](ActsFatras::Particle& particle) {
60  // only set the primary vertex, leave everything else as-is
61  // using the number of primary vertices as the index ensures
62  // that barcode=0 is not used, since it is used elsewhere
63  // to signify elements w/o an associated particle.
64  const auto pid = ActsFatras::Barcode(particle.particleId())
65  .setVertexPrimary(nPrimaryVertices);
66  // move particle to the vertex
67  const auto pos4 = (vertexPosition + particle.position4()).eval();
68  // `withParticleId` returns a copy because it changes the identity
69  particle = particle.withParticleId(pid).setPosition4(pos4);
70  };
71  for (auto& vertexParticle : vertexParticles) {
72  updateParticleInPlace(vertexParticle);
73  }
74 
75  ACTS_VERBOSE("event=" << ctx.eventNumber << " generator=" << iGenerate
76  << " primary_vertex=" << nPrimaryVertices
77  << " n_particles=" << vertexParticles.size());
78 
79  particles.merge(std::move(vertexParticles));
80  }
81  }
82 
83  ACTS_DEBUG("event=" << ctx.eventNumber
84  << " n_primary_vertices=" << nPrimaryVertices
85  << " n_particles=" << particles.size());
86 
87  // move generated event to the store
88  ctx.eventStore.add(m_cfg.outputParticles, std::move(particles));
89  return ProcessCode::SUCCESS;
90 }