Version: SMASH-2.0
hepmcoutput.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #include "smash/hepmcoutput.h"
11 
12 #include "HepMC3/Print.h"
13 
14 namespace smash {
15 
50 
51 HepMcOutput::HepMcOutput(const bf::path &path, std::string name,
52  const OutputParameters & /*out_par*/,
53  const int total_N, const int proj_N)
54  : OutputInterface(name),
55  filename_(path / (name + ".asciiv3")),
56  total_N_(total_N),
57  proj_N_(proj_N) {
59  filename_unfinished_ += +".unfinished";
60  output_file_ =
61  make_unique<HepMC3::WriterAscii>(filename_unfinished_.string());
62 }
63 
65 
66 int HepMcOutput::construct_nuclear_pdg_code(int na, int nz) const {
67  const int pdg_nuclear_code_prefix = 10 * 1E8;
68  // Hypernuclei not supported here
69  const int pdg_nuclear_code_lambda = 0 * 1E7;
70  const int pdg_nuclear_code_charge = nz * 1E4;
71  const int pdg_nuclear_code_baryon = na * 1E1;
72  // SMASH does not do isomers
73  const int pdg_nuclear_code_isomer = 0 * 1E0;
74 
75  return pdg_nuclear_code_prefix + pdg_nuclear_code_lambda +
76  pdg_nuclear_code_charge + pdg_nuclear_code_baryon +
77  pdg_nuclear_code_isomer;
78 }
79 
80 void HepMcOutput::at_eventstart(const Particles &particles,
81  const int event_number, const EventInfo &) {
83  make_unique<HepMC3::GenEvent>(HepMC3::Units::GEV, HepMC3::Units::MM);
84 
85  /* Rivet needs a value for the cross section, but SMASH only knows about
86  * nucleon cross sections, not about cross sections between nuclei,
87  * so a dummy is used. */
88  std::shared_ptr<HepMC3::GenCrossSection> cross_section =
89  std::make_shared<HepMC3::GenCrossSection>();
90  current_event_->add_attribute("GenCrossSection", cross_section);
91  const double dummy_xs = 1.0;
92  cross_section->set_cross_section(dummy_xs, dummy_xs);
93 
94  current_event_->set_event_number(event_number);
95  vertex_ = std::make_shared<HepMC3::GenVertex>();
96  current_event_->add_vertex(vertex_);
97 
98  if (proj_N_ > 0) {
99  // Collider modus: Construct and write projectile and target as two intial
100  // particles
101  int targ_N = total_N_ - proj_N_;
102  int proj_Z = 0;
103  int targ_Z = 0;
104  FourVector total_mom_proj;
105  FourVector total_mom_targ;
106  for (const ParticleData &data : particles) {
107  if (data.id() < proj_N_) {
108  total_mom_proj += data.momentum();
109  if (data.is_proton()) {
110  proj_Z += 1;
111  } else if (!data.is_neutron()) {
112  throw std::invalid_argument(
113  "HepMC output in SMASH only supports colliding nuclei consisting "
114  "of p and n.");
115  }
116  } else {
117  total_mom_targ += data.momentum();
118  if (data.is_proton()) {
119  targ_Z += 1;
120  } else if (!data.is_neutron()) {
121  throw std::invalid_argument(
122  "HepMC output in SMASH only supports colliding nuclei consisting "
123  "of p and n.");
124  }
125  }
126  }
127 
128  const int proj_nuclear_pdg_code =
130  const int targ_nuclear_pdg_code =
131  construct_nuclear_pdg_code(targ_N, targ_Z);
132 
133  HepMC3::GenParticlePtr projectile_p = std::make_shared<HepMC3::GenParticle>(
134  HepMC3::FourVector(total_mom_proj.x1(), total_mom_proj.x2(),
135  total_mom_proj.x3(), total_mom_proj.x0()),
136  proj_nuclear_pdg_code, status_code_for_beam_particles);
137  vertex_->add_particle_in(projectile_p);
138  HepMC3::GenParticlePtr target_p = std::make_shared<HepMC3::GenParticle>(
139  HepMC3::FourVector(total_mom_targ.x1(), total_mom_targ.x2(),
140  total_mom_targ.x3(), total_mom_targ.x0()),
141  targ_nuclear_pdg_code, status_code_for_beam_particles);
142  vertex_->add_particle_in(target_p);
143 
144  } else {
145  // Other modi (not collider): Write all inital particles into output
146  for (const ParticleData &data : particles) {
147  const FourVector mom = data.momentum();
148  HepMC3::GenParticlePtr p = std::make_shared<HepMC3::GenParticle>(
149  HepMC3::FourVector(mom.x1(), mom.x2(), mom.x3(), mom.x0()),
150  data.pdgcode().get_decimal(), status_code_for_beam_particles);
151  vertex_->add_particle_in(p);
152  }
153  }
154 }
155 
156 void HepMcOutput::at_eventend(const Particles &particles,
157  const int32_t /*event_number*/,
158  const EventInfo &event) {
159  // Set heavy ion attribute, only the impact parameter is known
160  std::shared_ptr<HepMC3::GenHeavyIon> heavy_ion =
161  std::make_shared<HepMC3::GenHeavyIon>();
162  current_event_->add_attribute("GenHeavyIon", heavy_ion);
163  // Impact paramter has to converted to mm (x1E-12), since fm not a supported
164  // unit in HepMC, -1(.0) are placeholders
165  heavy_ion->set(-1, -1, -1, -1, -1, -1, -1, -1, -1,
166  event.impact_parameter * 1E-12, -1.0, -1.0, -1.0, -1.0, -1.0);
167 
168  for (const ParticleData &data : particles) {
169  const FourVector mom = data.momentum();
170  HepMC3::GenParticlePtr p = std::make_shared<HepMC3::GenParticle>(
171  HepMC3::FourVector(mom.x1(), mom.x2(), mom.x3(), mom.x0()),
172  data.pdgcode().get_decimal(), status_code_for_final_particles);
173  vertex_->add_particle_out(p);
174  }
175 
176  output_file_->write_event(*current_event_);
177 }
178 
179 } // namespace smash
smash
Definition: action.h:24
smash::HepMcOutput::proj_N_
const int proj_N_
Total number of nucleons in projectile, needed for converting nuclei to single particles.
Definition: hepmcoutput.h:102
smash::HepMcOutput::total_N_
const int total_N_
Total number of nucleons in projectile and target, needed for converting nuclei to single particles.
Definition: hepmcoutput.h:97
smash::ParticleData
Definition: particledata.h:52
smash::HepMcOutput::filename_unfinished_
bf::path filename_unfinished_
Filename of output as long as simulation is still running.
Definition: hepmcoutput.h:91
smash::HepMcOutput::HepMcOutput
HepMcOutput(const bf::path &path, std::string name, const OutputParameters &out_par, const int total_N, const int proj_N)
Create HepMC particle output.
Definition: hepmcoutput.cc:51
smash::FourVector::x3
double x3() const
Definition: fourvector.h:315
smash::HepMcOutput::at_eventstart
void at_eventstart(const Particles &particles, const int event_number, const EventInfo &) override
Add the initial particles information of an event to the central vertex.
Definition: hepmcoutput.cc:80
smash::FourVector::x1
double x1() const
Definition: fourvector.h:307
smash::EventInfo
Structure to contain custom data for output.
Definition: outputinterface.h:35
smash::HepMcOutput::output_file_
std::unique_ptr< HepMC3::WriterAscii > output_file_
Pointer to Ascii HepMC3 output file.
Definition: hepmcoutput.h:122
smash::HepMcOutput::status_code_for_beam_particles
static const int status_code_for_beam_particles
HepMC convention: status code for target and projecticle particles.
Definition: hepmcoutput.h:84
smash::HepMcOutput::~HepMcOutput
~HepMcOutput()
Destructor renames file.
Definition: hepmcoutput.cc:64
smash::OutputParameters
Helper structure for Experiment to hold output options and parameters.
Definition: outputparameters.h:25
smash::FourVector::x0
double x0() const
Definition: fourvector.h:303
smash::HepMcOutput::current_event_
std::unique_ptr< HepMC3::GenEvent > current_event_
HepMC3::GenEvent pointer for current event.
Definition: hepmcoutput.h:125
smash::FourVector::x2
double x2() const
Definition: fourvector.h:311
smash::OutputInterface
Abstraction of generic output.
Definition: outputinterface.h:65
smash::HepMcOutput::status_code_for_final_particles
static const int status_code_for_final_particles
HepMC convention: status code for final particles.
Definition: hepmcoutput.h:86
smash::Particles
Definition: particles.h:33
smash::HepMcOutput::vertex_
HepMC3::GenVertexPtr vertex_
HepMC3::GenVertex pointer for central vertex in event.
Definition: hepmcoutput.h:128
smash::FourVector
Definition: fourvector.h:33
smash::EventInfo::impact_parameter
double impact_parameter
Impact parameter for collider modus, otherwise dummy.
Definition: outputinterface.h:37
smash::pdg::p
constexpr int p
Proton.
Definition: pdgcode_constants.h:28
hepmcoutput.h
smash::HepMcOutput::construct_nuclear_pdg_code
int construct_nuclear_pdg_code(int na, int nz) const
Construct nulcear pdg code for porjectile and target, see PDG chapter "Monte Carlo particle numbering...
Definition: hepmcoutput.cc:66
smash::HepMcOutput::at_eventend
void at_eventend(const Particles &particles, const int32_t event_number, const EventInfo &event) override
Add the final particles information of an event to the central vertex.
Definition: hepmcoutput.cc:156
smash::HepMcOutput::filename_
const bf::path filename_
Filename of output.
Definition: hepmcoutput.h:89