cepgen
is hosted by
Hepforge
,
IPPP Durham
CepGen
1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
EventHarvester.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
#include "
CepGen/Core/Exception.h
"
20
#include "
CepGen/Core/RunParameters.h
"
21
#include "
CepGen/Event/Event.h
"
22
#include "
CepGen/EventFilter/EventBrowser.h
"
23
#include "
CepGen/EventFilter/EventHarvester.h
"
24
#include "
CepGen/Modules/DrawerFactory.h
"
25
#include "
CepGen/Modules/ProcessFactory.h
"
26
#include "
CepGen/Utils/Drawer.h
"
27
#include "
CepGen/Utils/String.h
"
28
29
namespace
cepgen
{
30
EventHarvester::EventHarvester
(
const
ParametersList
& params)
31
:
EventExporter
(params),
32
browser_(new utils::EventBrowser),
33
show_hists_(steer<bool>(
"show"
)),
34
save_hists_(steer<bool>(
"save"
)),
35
filename_(steer<std::string>(
"filename"
)) {
36
// build the plotter object if specified
37
const
auto
& plotter = steer<std::string>(
"plotter"
);
38
if
(!plotter.empty())
39
drawer_ = DrawerFactory::get().build(plotter, params);
40
41
// extract list of variables to be plotted in histogram
42
const
auto
& hist_vars = steer<ParametersList>(
"histVariables"
);
43
for
(
const
auto
& key : hist_vars.keys()) {
44
const
auto
& vars =
utils::split
(key,
':'
);
45
if
(vars.size() < 1 || vars.size() > 2)
46
throw
CG_FATAL
(
"EventHarvester"
) <<
"Invalid number of variables to correlate for '"
<< key <<
"'!"
;
47
48
auto
hvar = hist_vars.get<
ParametersList
>(key);
49
const
auto
& log = hvar.
get
<
bool
>(
"log"
);
50
auto
name =
utils::sanitise
(key);
51
if
(vars.size() == 1) {
// 1D histogram
52
auto
hist =
utils::Hist1D
(hvar.set<std::string>(
"name"
, name));
53
hist.xAxis().setLabel(vars.at(0));
54
hist.yAxis().setLabel(
"d$\\sigma$/d"
+ vars.at(0) +
" (pb/bin)"
);
55
hists_.emplace_back(Hist1DInfo{vars.at(0), hist, log});
56
}
else
if
(vars.size() == 2) {
// 2D histogram
57
auto
hist =
utils::Hist2D
(hvar.set<std::string>(
"name"
,
utils::sanitise
(name)));
58
hist.xAxis().setLabel(vars.at(0));
59
hist.yAxis().setLabel(vars.at(1));
60
hist.zAxis().setLabel(
"d$^2$$\\sigma$/d"
+ vars.at(0) +
"/d"
+ vars.at(1) +
" (pb/bin)"
);
61
hists2d_.emplace_back(Hist2DInfo{vars.at(0), vars.at(1), hist, log});
62
}
63
}
64
if
(save_hists_ && !hists_.empty())
65
file_.open(filename_);
66
}
67
68
EventHarvester::~EventHarvester
() {
69
//--- histograms printout
70
if
(!show_hists_ && !save_hists_)
71
return
;
72
try
{
73
for
(
auto
& h_var : hists_) {
74
h_var.hist.scale(cross_section_ / (num_evts_ + 1));
75
h_var.hist.setTitle(proc_name_);
76
std::ostringstream os;
77
if
(drawer_)
78
drawer_->draw(h_var.hist, h_var.log ?
utils::Drawer::Mode::logy
:
utils::Drawer::Mode::none
);
79
if
(show_hists_)
80
CG_INFO
(
"EventHarvester"
) << os.str();
81
if
(save_hists_)
82
file_ <<
"\n"
<< os.str() <<
"\n"
;
83
}
84
for
(
auto
& h_var : hists2d_) {
85
std::ostringstream os;
86
h_var.hist.setTitle(proc_name_);
87
if
(drawer_)
88
drawer_->draw(
89
h_var.hist,
90
utils::Drawer::Mode::grid
| (h_var.log ?
utils::Drawer::Mode::logz
:
utils::Drawer::Mode::none
));
91
if
(show_hists_)
92
CG_INFO
(
"EventHarvester"
) << os.str();
93
if
(save_hists_)
94
file_ <<
"\n"
<< os.str() <<
"\n"
;
95
}
96
if
(save_hists_)
97
CG_INFO
(
"EventHarvester"
) <<
"Saved "
<<
utils::s
(
"histogram"
, hists_.size(),
true
) <<
" into \""
<< filename_
98
<<
"\"."
;
99
}
catch
(
const
Exception
& exc) {
100
CG_ERROR
(
"EventHarvester"
) <<
"Failed to save the histograms harvested in this run. Error received: "
101
<< exc.
what
();
102
}
103
}
104
105
void
EventHarvester::initialise() {
106
num_evts_ = 0ul;
107
proc_name_ = ProcessFactory::get().describe(
runParameters
().processName());
108
proc_name_ +=
109
", \\sqrt{s} = "
+
utils::format
(
"%g"
,
runParameters
().kinematics().incomingBeams().sqrtS() * 1.e-3) +
" TeV"
;
110
if
(save_hists_ && !hists_.empty())
111
file_ <<
banner
(
"#"
) <<
"\n"
;
112
}
113
114
bool
EventHarvester::operator<<
(
const
Event
& ev) {
115
// increment the corresponding histograms
116
for
(
auto
& h_var : hists_)
117
h_var.hist.fill(browser_->get(ev, h_var.var));
118
for
(
auto
& h_var : hists2d_)
119
h_var.hist.fill(browser_->get(ev, h_var.var1), browser_->get(ev, h_var.var2));
120
++num_evts_;
121
return
true
;
122
}
123
124
ParametersDescription
EventHarvester::description
() {
125
auto
desc =
EventExporter::description
();
126
desc.setDescription(
"Text-based histogramming tool"
);
127
desc.add<std::string>(
"plotter"
,
""
).setDescription(
"Plotting algorithm to use"
);
128
desc.add<std::string>(
"filename"
,
"output.hists.txt"
).setDescription(
"Output file name for histogram dump"
);
129
desc.add<
bool
>(
"show"
,
true
).setDescription(
"Show the histogram(s) at the end of the run?"
);
130
desc.add<
bool
>(
"save"
,
false
).setDescription(
"Save the histogram(s) at the end of the run?"
);
131
// per-histogram default parameters
132
ParametersDescription
hist_desc;
133
// x-axis attributes
134
hist_desc.
add
<std::vector<double> >(
"xbins"
, {}).setDescription(
"x-axis bins definition"
);
135
hist_desc.
add
<
int
>(
"nbinsX"
, 25).setDescription(
"Bins multiplicity for x-axis"
);
136
hist_desc.
add
<
Limits
>(
"xrange"
,
Limits
{0., 1.}).setDescription(
"Minimum-maximum range for x-axis"
);
137
// y-axis attributes
138
hist_desc.
add
<std::vector<double> >(
"ybins"
, {}).setDescription(
"y-axis bins definition"
);
139
hist_desc.
add
<
int
>(
"nbinsY"
, 50).setDescription(
"Bins multiplicity for y-axis"
);
140
hist_desc.
add
<
Limits
>(
"yrange"
,
Limits
{0., 1.}).setDescription(
"Minimum-maximum range for y-axis"
);
141
hist_desc.
add
<
bool
>(
"log"
,
false
).setDescription(
"Plot logarithmic axis?"
);
142
desc.addParametersDescriptionVector(
"histVariables"
, hist_desc, {})
143
.setDescription(
"Histogram definition for 1/2 variable(s)"
);
144
return
desc;
145
}
146
}
// namespace cepgen
DrawerFactory.h
Drawer.h
EventBrowser.h
EventHarvester.h
Event.h
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
CG_ERROR
#define CG_ERROR(mod)
Definition
Exception.h:60
CG_INFO
#define CG_INFO(mod)
Definition
Message.h:216
ProcessFactory.h
RunParameters.h
String.h
cepgen::EventExporter
Output format handler for events export.
Definition
EventExporter.h:30
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::EventHarvester::operator<<
bool operator<<(const Event &) override
Writer operator.
Definition
EventHarvester.cpp:114
cepgen::EventHarvester::EventHarvester
EventHarvester(const ParametersList &)
Definition
EventHarvester.cpp:30
cepgen::EventHarvester::~EventHarvester
virtual ~EventHarvester()
Definition
EventHarvester.cpp:68
cepgen::EventHarvester::description
static ParametersDescription description()
Definition
EventHarvester.cpp:124
cepgen::Event
Container for the information on the in- and outgoing particles' kinematics.
Definition
Event.h:28
cepgen::Exception
Definition
Exception.h:25
cepgen::Exception::what
const char * what() const noexcept override
Definition
Exception.cpp:36
cepgen::Limits
Validity interval for a variable.
Definition
Limits.h:28
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersDescription::add
ParametersDescription & add(const std::string &name, const T &def)
Add the description to a new parameter.
Definition
ParametersDescription.h:59
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::ParametersList::get
T get(const std::string &key, const T &def=default_arg< T >::get()) const
Get a parameter value.
Definition
ParametersList.cpp:391
cepgen::Steerable::description
static ParametersDescription description()
Description of all object parameters.
Definition
Steerable.cpp:42
cepgen::utils::Drawer::Mode::logz
@ logz
Definition
Drawer.h:47
cepgen::utils::Drawer::Mode::grid
@ grid
Definition
Drawer.h:49
cepgen::utils::Drawer::Mode::none
@ none
Definition
Drawer.h:44
cepgen::utils::Drawer::Mode::logy
@ logy
Definition
Drawer.h:46
cepgen::utils::Hist1D
1D histogram container
Definition
Histogram.h:72
cepgen::utils::Hist2D
2D histogram container
Definition
Histogram.h:146
cepgen::utils::format
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition
String.h:61
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::utils::sanitise
std::string sanitise(const std::string &str)
Replace all unsafe characters to build a computer-readable (and filename-safe) string.
Definition
String.cpp:105
cepgen::utils::split
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition
String.cpp:233
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGen
EventFilter
EventHarvester.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7