ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Pythia8ProcessGenerator.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Pythia8ProcessGenerator.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2017-2019 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 
11 #include <Pythia8/Pythia.h>
12 #include <algorithm>
13 #include <iterator>
14 #include <random>
15 
16 namespace {
17 struct FrameworkRndmEngine : public Pythia8::RndmEngine {
18  FW::RandomEngine& rng;
19 
20  FrameworkRndmEngine(FW::RandomEngine& rng_) : rng(rng_) {}
21  double flat() {
22  return std::uniform_real_distribution<double>(0.0, 1.0)(rng);
23  }
24 };
25 } // namespace
26 
27 std::function<std::vector<FW::SimVertex>(FW::RandomEngine&)>
30  auto gen = std::make_shared<Pythia8Generator>(cfg, lvl);
31  return [=](RandomEngine& rng) { return (*gen)(rng); };
32 }
33 
36  : m_cfg(cfg),
37  m_logger(Acts::getDefaultLogger("Pythia8Generator", lvl)),
38  m_pythia8(std::make_unique<Pythia8::Pythia>("", false)) {
39  // disable all output by default but allow reenable via config
40  m_pythia8->settings.flag("Print:quiet", true);
41  for (const auto& str : m_cfg.settings) {
42  ACTS_VERBOSE("use Pythia8 setting '" << str << "'");
43  m_pythia8->readString(str.c_str());
44  }
45  m_pythia8->settings.mode("Beams:idA", m_cfg.pdgBeam0);
46  m_pythia8->settings.mode("Beams:idB", m_cfg.pdgBeam1);
47  m_pythia8->settings.mode("Beams:frameType", 1);
48  m_pythia8->settings.parm("Beams:eCM",
50  m_pythia8->init();
51 }
52 
53 // needed to allow unique_ptr of forward-declared Pythia class
55 
56 std::vector<FW::SimVertex> FW::Pythia8Generator::operator()(
57  FW::RandomEngine& rng) {
58  using namespace Acts::UnitLiterals;
59 
60  // first process vertex is the primary one at origin with time=0
61  std::vector<SimVertex> vertices = {
62  SimVertex(SimVertex::Vector4::Zero()),
63  };
64 
65  // pythia8 is not thread safe and generation needs to be protected
66  std::lock_guard<std::mutex> lock(m_pythia8Mutex);
67  // use per-thread random engine also in pythia
68  FrameworkRndmEngine rndmEngine(rng);
69  m_pythia8->rndm.rndmEnginePtr(&rndmEngine);
70  m_pythia8->next();
71 
72  // convert generated final state particles into internal format
73  for (int ip = 0; ip < m_pythia8->event.size(); ++ip) {
74  const auto& genParticle = m_pythia8->event[ip];
75 
76  // ignore beam particles
77  if (genParticle.statusHepMC() == 4) {
78  continue;
79  }
80  // only interested in final, visible particles
81  if (not genParticle.isFinal()) {
82  continue;
83  }
84  if (not genParticle.isVisible()) {
85  continue;
86  }
87 
88  // production vertex. Pythia8 time uses units mm/c, and we use c=1
89  SimVertex::Vector4 pos4(
90  genParticle.xProd() * 1_mm, genParticle.yProd() * 1_mm,
91  genParticle.zProd() * 1_mm, genParticle.tProd() * 1_mm);
92  // identify vertex
93  std::vector<SimVertex>::iterator vertex;
94  if (not genParticle.hasVertex()) {
95  // w/o defined vertex, must belong to the first (primary) process vertex
96  vertex = vertices.begin();
97  } else {
98  // either add to existing secondary vertex if exists or create new one
99  // TODO can we do this w/o the manual search and position check?
100  vertex = std::find_if(
101  vertices.begin(), vertices.end(),
102  [=](const SimVertex& v) { return (v.position4 == pos4); });
103  if (vertex == vertices.end()) {
104  // no matching secondary vertex exists -> create new one
105  vertices.emplace_back(pos4);
106  vertex = std::prev(vertices.end());
107 
108  ACTS_VERBOSE("created new secondary vertex " << pos4.transpose());
109  }
110  }
111 
112  // Ensure particle identifier components are defaulted to zero.
113  ActsFatras::Barcode particleId(0u);
114  // first vertex w/ distance=0 contains all direct particles
115  particleId.setVertexSecondary(std::distance(vertices.begin(), vertex));
116  // ensure particle identifier component is non-zero
117  particleId.setParticle(1u + vertex->outgoing.size());
118  // reuse PDG id from generator
119  const auto pdg = static_cast<Acts::PdgParticle>(genParticle.id());
120 
121  // construct internal particle
122  ActsFatras::Particle particle(particleId, pdg, genParticle.charge() * 1_e,
123  genParticle.m0() * 1_GeV);
124  particle.setPosition4(pos4);
125  // normalization/ units are not import for the direction
126  particle.setDirection(genParticle.px(), genParticle.py(), genParticle.pz());
127  particle.setAbsMomentum(
128  std::hypot(genParticle.px(), genParticle.py(), genParticle.pz()) *
129  1_GeV);
130 
131  vertex->outgoing.push_back(std::move(particle));
132  }
133  return vertices;
134 }