cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ProMCHandler.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2019-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma GCC diagnostic push
20#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
21#include <ProMCBook.h>
22#pragma GCC diagnostic pop
23
24#include <cstdio>
25
27#include "CepGen/Event/Event.h"
30#include "CepGen/Physics/PDG.h"
33#include "CepGen/Utils/String.h"
34#include "CepGen/Utils/Value.h"
35#include "CepGen/Version.h"
36
37namespace cepgen {
41 class ProMCHandler final : public EventExporter {
42 public:
43 explicit ProMCHandler(const ParametersList& params)
44 : EventExporter(params),
45 compress_evt_(steer<bool>("compress")),
46 log_file_path_(steer<std::string>("logFile")),
47 log_file_(log_file_path_) {}
48
50 if (file_) {
51 ProMCStat stat;
52 stat.set_cross_section_accumulated(cross_section_);
53 stat.set_cross_section_error_accumulated(cross_section_.uncertainty());
54 stat.set_luminosity_accumulated(event_num_ / (double)cross_section_);
55 stat.set_ntried(event_num_);
56 stat.set_nselected(event_num_);
57 stat.set_naccepted(event_num_);
58 file_->setStatistics(stat);
59 file_->close();
60 }
61 const auto num_removed_files = fs::remove_all(log_file_path_); // delete the log file once attached
62 CG_DEBUG("ProMCHandler") << utils::s("file", num_removed_files, true) << " removed.";
63 }
64
66 auto desc = EventExporter::description();
67 desc.setDescription("ProMC file output module");
68 desc.add<std::string>("filename", "output.promc");
69 desc.add<bool>("compress", false);
70 desc.add<std::string>("logFile", "logfile.txt");
71 return desc;
72 }
73
74 void setCrossSection(const Value& cross_section) override { cross_section_ = cross_section; }
75 bool operator<<(const Event&) override;
76
77 private:
78 void initialise() override {
79 file_.reset(new ProMCBook(steer<std::string>("filename").c_str(), "w"));
80 file_->setDescription(runParameters().generation().maxGen(), "Sample generated using CepGen v" + version::tag);
81 log_file_ << banner() << "\n";
82 ProMCHeader hdr;
83 hdr.set_momentumunit(GEV_UNIT);
84 hdr.set_lengthunit(M_UNIT); // unused as for now
85 for (const auto& pdg : PDG::get().particles()) {
86 auto data = hdr.add_particledata();
87 const auto& desc = PDG::get()(pdg);
88 data->set_id((int)pdg);
89 data->set_mass(desc.mass);
90 data->set_name(desc.name);
91 data->set_width(desc.width);
92 data->set_charge(desc.charge * 1. / 3.);
93 }
96 hdr.set_pdf1(0);
97 hdr.set_pdf2(0);
98 hdr.set_x1(0);
99 hdr.set_x2(0);
100 hdr.set_ecm(runParameters().kinematics().incomingBeams().sqrtS());
101 file_->setHeader(hdr);
102 }
103
104 static constexpr double GEV_UNIT = 1.e6; // base unit in GEV_UNIT^-1 GeV = keV
105 static constexpr double M_UNIT = 1.e3; // base unit in M^-1 m = mm
106 static int inGeV(double val) { return int(val * GEV_UNIT); }
107
108 std::unique_ptr<ProMCBook> file_;
109 const bool compress_evt_;
110 const std::string log_file_path_;
111 std::ofstream log_file_;
112 Value cross_section_{0., 1.};
113 };
114
116 if (!file_)
117 return false;
118 ProMCEvent event;
119 auto evt = event.mutable_event();
120 evt->set_number(event_num_++);
121 evt->set_process_id(0);
123 evt->set_alpha_qed(ev.metadata("alphaEM"));
124 evt->set_alpha_qcd(ev.metadata("alphaS"));
125 evt->set_weight(1.);
126
127 unsigned short i = 0;
128 const auto& parts = compress_evt_ ? ev.compress().particles() : ev.particles();
129 for (const auto& par : parts) {
130 auto part = event.mutable_particles();
131 part->add_id(i++);
132 part->add_pdg_id(par.integerPdgId());
133 part->add_status((unsigned int)par.status());
134 //--- kinematics
135 part->add_px(inGeV(par.momentum().px()));
136 part->add_py(inGeV(par.momentum().py()));
137 part->add_pz(inGeV(par.momentum().pz()));
138 part->add_energy(inGeV(par.momentum().energy()));
139 part->add_mass(inGeV(par.momentum().mass()));
140 part->add_barcode(0);
141 //--- parentage
142 const auto &daughter = par.daughters(), &moth = par.mothers();
143 part->add_daughter1(daughter.empty() ? 0 : *daughter.begin() + 1);
144 part->add_daughter2(daughter.size() > 1 ? *daughter.rbegin() + 1 : 0);
145 part->add_mother1(moth.empty() ? 0 : *moth.begin() + 1);
146 part->add_mother2(moth.size() > 1 ? *moth.rbegin() + 1 : 0);
147 //--- vertex
148 part->add_x(0);
149 part->add_y(0);
150 part->add_z(0);
151 part->add_t(0);
152 }
153 return file_->write(event);
154 }
155} // namespace cepgen
156REGISTER_EXPORTER("promc", ProMCHandler);
#define REGISTER_EXPORTER(name, obj)
Add a generic export module definition to the factory.
#define CG_DEBUG(mod)
Definition Message.h:220
spdgid_t integerPdgId() const
Beam particle PDG id Set the beam particle PDG id.
Definition Beam.h:47
Output format handler for events export.
unsigned long long event_num_
Event index.
std::string banner(const std::string &prep="") const
Print a banner containing runtime parameters.
const RunParameters & runParameters() const
List of run parameters.
Container for the information on the in- and outgoing particles' kinematics.
Definition Event.h:28
Event compress() const
Compress the event record.
Definition Event.cpp:115
Particle & oneWithRole(Particle::Role role)
First Particle object with a given role in the event.
Definition Event.cpp:190
Particles particles() const
Vector of all particles in the event.
Definition Event.cpp:353
EventMetadata metadata
List of auxiliary information.
Definition Event.h:136
const Beam & positive() const
Reference to the positive-z beam information.
double sqrtS() const
Incoming beams centre of mass energy (in GeV)
const Beam & negative() const
Reference to the negative-z beam information.
IncomingBeams & incomingBeams()
Beam/primary particle kinematics.
Definition Kinematics.h:36
double mass() const
Mass (in GeV) as computed from its energy and momentum.
Definition Momentum.cpp:222
A singleton holding all physics constants associated to particles.
Definition PDG.h:28
static PDG & get()
Retrieve a unique instance of this particles info collection.
Definition PDG.cpp:41
A description object for parameters collection.
Momentum & momentum()
Retrieve the momentum object associated with this particle Retrieve the momentum object associated wi...
Definition Particle.h:123
@ Intermediate
Intermediate two-parton system.
Definition Particle.h:57
Handler for the ProMC file output.
bool operator<<(const Event &) override
Writer operator.
static ParametersDescription description()
void setCrossSection(const Value &cross_section) override
Specify the cross section value, in pb.
ProMCHandler(const ParametersList &params)
const Kinematics & kinematics() const
Events kinematics for phase space definition.
static ParametersDescription description()
Description of all object parameters.
Definition Steerable.cpp:42
T steer(const std::string &key) const
Retrieve a parameters as previously steered.
Definition Steerable.h:39
A scalar value with its uncertainty.
Definition Value.h:26
double uncertainty() const
Absolute uncertainty around the central value.
Definition Value.h:35
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
Common namespace for this Monte Carlo generator.
static const std::string tag
CepGen version.
Definition Version.h:28