cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
TextVariablesHandler.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 <fstream>
20
21#include "CepGen/Event/Event.h"
25
26namespace cepgen {
33 public:
34 explicit TextVariablesHandler(const ParametersList& params)
35 : EventExporter(params),
36 file_(steer<std::string>("filename")),
37 variables_(steer<std::vector<std::string> >("variables")),
38 save_banner_(steer<bool>("saveBanner")),
39 save_variables_(steer<bool>("saveVariables")),
40 separator_(steer<std::string>("separator")) {
41 //--- extract list of variables to store in output file
42 oss_vars_.clear();
43 std::string sep;
44 for (const auto& var : variables_)
45 oss_vars_ << sep << var, sep = separator_;
46 }
47
49 auto desc = EventExporter::description();
50 desc.setDescription("Text dump of variables");
51 desc.add<std::string>("filename", "output.txt").setDescription("Output filename for variables dump");
52 desc.add<std::vector<std::string> >("variables", {}).setDescription("List of variables to dump");
53 desc.add<bool>("saveBanner", true).setDescription("Also save the boilerplate in output files?");
54 desc.add<bool>("saveVariables", true).setDescription("Save the variable(s) into an output file?");
55 desc.add<std::string>("separator", "\t").setDescription("Base separator in output file");
56 return desc;
57 }
58
59 bool operator<<(const Event& ev) override {
60 if (variables_.empty())
61 return true;
62 std::string sep;
63 for (const auto& var : variables_) // write down the variables list in the file
64 file_ << sep << browser_.get(ev, var), sep = separator_;
65 file_ << "\n";
66 return true;
67 }
68
69 private:
70 void initialise() override {
71 if (save_banner_)
72 file_ << banner("#") << "\n";
73 if (save_variables_)
74 file_ << "# " << oss_vars_.str() << "\n";
75 }
76
77 std::ofstream file_;
78 //--- variables definition
79 const std::vector<std::string> variables_;
80 const bool save_banner_, save_variables_;
81 const std::string separator_;
82
83 const utils::EventBrowser browser_;
84
85 std::ostringstream oss_vars_;
86 };
87} // namespace cepgen
88REGISTER_EXPORTER("vars", TextVariablesHandler);
#define REGISTER_EXPORTER(name, obj)
Add a generic export module definition to the factory.
Output format handler for events export.
std::string banner(const std::string &prep="") const
Print a banner containing runtime parameters.
Container for the information on the in- and outgoing particles' kinematics.
Definition Event.h:28
A description object for parameters collection.
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
Handler for the generic text file output.
TextVariablesHandler(const ParametersList &params)
static ParametersDescription description()
bool operator<<(const Event &ev) override
Writer operator.
double get(const Event &ev, const std::string &var) const
Get/compute a variable value.
Common namespace for this Monte Carlo generator.