Analysis Software
Documentation for sPHENIX simulation 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 
14 
15 #include <cstdint>
16 #include <ostream>
17 #include <stdexcept>
18 
21  : m_cfg(cfg), m_logger(Acts::getDefaultLogger("EventGenerator", lvl)) {
22  if (m_cfg.outputParticles.empty()) {
23  throw std::invalid_argument("Missing output particles collection");
24  }
25  if (m_cfg.generators.empty()) {
26  throw std::invalid_argument("No generators are configured");
27  }
28  if (!m_cfg.randomNumbers) {
29  throw std::invalid_argument("Missing random numbers service");
30  }
31 
33 }
34 
36  return "EventGenerator";
37 }
38 
40  const {
41  return {0u, SIZE_MAX};
42 }
43 
45  const AlgorithmContext& ctx) {
47 
48  auto rng = m_cfg.randomNumbers->spawnGenerator(ctx);
49 
50  size_t nPrimaryVertices = 0;
51  for (size_t iGenerate = 0; iGenerate < m_cfg.generators.size(); ++iGenerate) {
52  auto& generate = m_cfg.generators[iGenerate];
53 
54  // generate the primary vertices from this generator
55  for (size_t n = (*generate.multiplicity)(rng); 0 < n; --n) {
56  nPrimaryVertices += 1;
57 
58  // generate primary vertex position
59  auto vertexPosition = (*generate.vertex)(rng);
60  // generate particles associated to this vertex
61  auto vertexParticles = (*generate.particles)(rng);
62 
63  ACTS_VERBOSE("Generate vertex at " << vertexPosition.transpose());
64 
65  auto updateParticleInPlace = [&](ActsFatras::Particle& particle) {
66  // only set the primary vertex, leave everything else as-is
67  // using the number of primary vertices as the index ensures
68  // that barcode=0 is not used, since it is used elsewhere
69  // to signify elements w/o an associated particle.
70  const auto pid = ActsFatras::Barcode(particle.particleId())
71  .setVertexPrimary(nPrimaryVertices);
72  // move particle to the vertex
73  const auto pos4 = (vertexPosition + particle.fourPosition()).eval();
74  ACTS_VERBOSE(" - particle at " << pos4.transpose());
75  // `withParticleId` returns a copy because it changes the identity
76  particle = particle.withParticleId(pid).setPosition4(pos4);
77  };
78  for (auto& vertexParticle : vertexParticles) {
79  updateParticleInPlace(vertexParticle);
80  }
81 
82  ACTS_VERBOSE("event=" << ctx.eventNumber << " generator=" << iGenerate
83  << " primary_vertex=" << nPrimaryVertices
84  << " n_particles=" << vertexParticles.size());
85 
86  particles.merge(std::move(vertexParticles));
87  }
88  }
89 
90  ACTS_DEBUG("event=" << ctx.eventNumber
91  << " n_primary_vertices=" << nPrimaryVertices
92  << " n_particles=" << particles.size());
93 
94  // move generated event to the store
95  m_outputParticles(ctx, std::move(particles));
96  return ProcessCode::SUCCESS;
97 }