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
26
#include "
CepGen/Core/RunParameters.h
"
27
#include "
CepGen/Event/Event.h
"
28
#include "
CepGen/EventFilter/EventExporter.h
"
29
#include "
CepGen/Modules/EventExporterFactory.h
"
30
#include "
CepGen/Physics/PDG.h
"
31
#include "
CepGen/Utils/Filesystem.h
"
32
#include "
CepGen/Utils/Message.h
"
33
#include "
CepGen/Utils/String.h
"
34
#include "
CepGen/Utils/Value.h
"
35
#include "
CepGen/Version.h
"
36
37
namespace
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
49
~ProMCHandler
() {
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
65
static
ParametersDescription
description
() {
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
}
94
hdr.set_id1(
runParameters
().
kinematics
().
incomingBeams
().
positive
().
integerPdgId
());
95
hdr.set_id2(
runParameters
().
kinematics
().
incomingBeams
().
negative
().
integerPdgId
());
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
115
bool
ProMCHandler::operator<<
(
const
Event
& ev) {
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);
122
evt->set_scale(ev.
oneWithRole
(
Particle::Role::Intermediate
).
momentum
().
mass
());
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
156
REGISTER_EXPORTER
(
"promc"
, ProMCHandler);
EventExporterFactory.h
REGISTER_EXPORTER
#define REGISTER_EXPORTER(name, obj)
Add a generic export module definition to the factory.
Definition
EventExporterFactory.h:25
EventExporter.h
Event.h
Filesystem.h
Message.h
CG_DEBUG
#define CG_DEBUG(mod)
Definition
Message.h:220
PDG.h
RunParameters.h
String.h
Value.h
Version.h
cepgen::Beam::integerPdgId
spdgid_t integerPdgId() const
Beam particle PDG id Set the beam particle PDG id.
Definition
Beam.h:47
cepgen::EventExporter
Output format handler for events export.
Definition
EventExporter.h:30
cepgen::EventExporter::event_num_
unsigned long long event_num_
Event index.
Definition
EventExporter.h:41
cepgen::EventExporter::banner
std::string banner(const std::string &prep="") const
Print a banner containing runtime parameters.
Definition
EventExporter.cpp:32
cepgen::EventHandler::runParameters
const RunParameters & runParameters() const
List of run parameters.
Definition
EventHandler.cpp:44
cepgen::Event
Container for the information on the in- and outgoing particles' kinematics.
Definition
Event.h:28
cepgen::Event::compress
Event compress() const
Compress the event record.
Definition
Event.cpp:115
cepgen::Event::oneWithRole
Particle & oneWithRole(Particle::Role role)
First Particle object with a given role in the event.
Definition
Event.cpp:190
cepgen::Event::particles
Particles particles() const
Vector of all particles in the event.
Definition
Event.cpp:353
cepgen::Event::metadata
EventMetadata metadata
List of auxiliary information.
Definition
Event.h:136
cepgen::IncomingBeams::positive
const Beam & positive() const
Reference to the positive-z beam information.
Definition
IncomingBeams.h:38
cepgen::IncomingBeams::sqrtS
double sqrtS() const
Incoming beams centre of mass energy (in GeV)
Definition
IncomingBeams.cpp:202
cepgen::IncomingBeams::negative
const Beam & negative() const
Reference to the negative-z beam information.
Definition
IncomingBeams.h:40
cepgen::Kinematics::incomingBeams
IncomingBeams & incomingBeams()
Beam/primary particle kinematics.
Definition
Kinematics.h:36
cepgen::Momentum::mass
double mass() const
Mass (in GeV) as computed from its energy and momentum.
Definition
Momentum.cpp:222
cepgen::PDG
A singleton holding all physics constants associated to particles.
Definition
PDG.h:28
cepgen::PDG::get
static PDG & get()
Retrieve a unique instance of this particles info collection.
Definition
PDG.cpp:41
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::Particle::momentum
Momentum & momentum()
Retrieve the momentum object associated with this particle Retrieve the momentum object associated wi...
Definition
Particle.h:123
cepgen::Particle::Intermediate
@ Intermediate
Intermediate two-parton system.
Definition
Particle.h:57
cepgen::ProMCHandler
Handler for the ProMC file output.
Definition
ProMCHandler.cpp:41
cepgen::ProMCHandler::operator<<
bool operator<<(const Event &) override
Writer operator.
Definition
ProMCHandler.cpp:115
cepgen::ProMCHandler::~ProMCHandler
~ProMCHandler()
Definition
ProMCHandler.cpp:49
cepgen::ProMCHandler::description
static ParametersDescription description()
Definition
ProMCHandler.cpp:65
cepgen::ProMCHandler::setCrossSection
void setCrossSection(const Value &cross_section) override
Specify the cross section value, in pb.
Definition
ProMCHandler.cpp:74
cepgen::ProMCHandler::ProMCHandler
ProMCHandler(const ParametersList ¶ms)
Definition
ProMCHandler.cpp:43
cepgen::RunParameters::kinematics
const Kinematics & kinematics() const
Events kinematics for phase space definition.
Definition
RunParameters.cpp:125
cepgen::Steerable::description
static ParametersDescription description()
Description of all object parameters.
Definition
Steerable.cpp:42
cepgen::Steerable::steer
T steer(const std::string &key) const
Retrieve a parameters as previously steered.
Definition
Steerable.h:39
cepgen::Value
A scalar value with its uncertainty.
Definition
Value.h:26
cepgen::Value::uncertainty
double uncertainty() const
Absolute uncertainty around the central value.
Definition
Value.h:35
cepgen::utils::s
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition
String.cpp:228
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
pdg
Definition
MCDFileParser.cpp:28
cepgen::version::tag
static const std::string tag
CepGen version.
Definition
Version.h:28
CepGenAddOns
ProMCWrapper
ProMCHandler.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7