Analysis Software
Documentation for sPHENIX simulation 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 
13 
14 #include <algorithm>
15 #include <cmath>
16 #include <iterator>
17 #include <ostream>
18 #include <random>
19 #include <utility>
20 
21 #include <Pythia8/Pythia.h>
22 
23 namespace {
24 struct FrameworkRndmEngine : public Pythia8::RndmEngine {
26 
27  FrameworkRndmEngine(ActsExamples::RandomEngine& rng_) : rng(rng_) {}
28  double flat() override {
29  return std::uniform_real_distribution<double>(0.0, 1.0)(rng);
30  }
31 };
32 } // namespace
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 re-enable via config
40  m_pythia8->settings.flag("Print:quiet", true);
41  for (const auto& setting : m_cfg.settings) {
42  ACTS_VERBOSE("use Pythia8 setting '" << setting << "'");
43  m_pythia8->readString(setting.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 
57  RandomEngine& rng) {
58  using namespace Acts::UnitLiterals;
59 
60  SimParticleContainer::sequence_type generated;
61  std::vector<SimParticle::Vector4> vertexPositions;
62 
63  // pythia8 is not thread safe and generation needs to be protected
64  std::lock_guard<std::mutex> lock(m_pythia8Mutex);
65 // use per-thread random engine also in pythia
66 #if PYTHIA_VERSION_INTEGER >= 8310
67  m_pythia8->rndm.rndmEnginePtr(std::make_shared<FrameworkRndmEngine>(rng));
68 #else
69  FrameworkRndmEngine rndmEngine(rng);
70  m_pythia8->rndm.rndmEnginePtr(&rndmEngine);
71 #endif
72  {
73  Acts::FpeMonitor mon{0}; // disable all FPEs while we're in Pythia8
74  m_pythia8->next();
75  }
76 
77  if (m_cfg.printShortEventListing) {
78  m_pythia8->process.list();
79  }
80  if (m_cfg.printLongEventListing) {
81  m_pythia8->event.list();
82  }
83 
84  // convert generated final state particles into internal format
85  for (int ip = 0; ip < m_pythia8->event.size(); ++ip) {
86  const auto& genParticle = m_pythia8->event[ip];
87 
88  // ignore beam particles
89  if (genParticle.statusHepMC() == 4) {
90  continue;
91  }
92  // only interested in final, visible particles
93  if (not genParticle.isFinal()) {
94  continue;
95  }
96  if (not genParticle.isVisible()) {
97  continue;
98  }
99 
100  // production vertex. Pythia8 time uses units mm/c, and we use c=1
102  genParticle.xProd() * 1_mm, genParticle.yProd() * 1_mm,
103  genParticle.zProd() * 1_mm, genParticle.tProd() * 1_mm);
104 
105  // define the particle identifier including possible secondary vertices
106  ActsFatras::Barcode particleId(0u);
107  // ensure particle identifier component is non-zero
108  particleId.setParticle(1u + generated.size());
109  // only secondaries have a defined vertex position
110  if (genParticle.hasVertex()) {
111  // either add to existing secondary vertex if exists or create new one
112  // TODO can we do this w/o the manual search and position check?
113  auto it = std::find_if(
114  vertexPositions.begin(), vertexPositions.end(),
115  [=](const SimParticle::Vector4& pos) { return (pos == pos4); });
116  if (it == vertexPositions.end()) {
117  // no matching secondary vertex exists -> create new one
118  vertexPositions.emplace_back(pos4);
119  particleId.setVertexSecondary(vertexPositions.size());
120  ACTS_VERBOSE("created new secondary vertex " << pos4.transpose());
121  } else {
122  particleId.setVertexSecondary(
123  1u + std::distance(vertexPositions.begin(), it));
124  }
125  }
126 
127  // construct internal particle
128  const auto pdg = static_cast<Acts::PdgParticle>(genParticle.id());
129  const auto charge = genParticle.charge() * 1_e;
130  const auto mass = genParticle.m0() * 1_GeV;
132  particle.setPosition4(pos4);
133  // normalization/ units are not import for the direction
134  particle.setDirection(genParticle.px(), genParticle.py(), genParticle.pz());
135  particle.setAbsoluteMomentum(
136  std::hypot(genParticle.px(), genParticle.py(), genParticle.pz()) *
137  1_GeV);
138 
139  generated.push_back(std::move(particle));
140  }
141 
143  out.insert(generated.begin(), generated.end());
144  return out;
145 }