cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.3
A generic central exclusive processes event generator
Loading...
Searching...
No Matches
TextEventHandler.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2020-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#include <iostream> // for cout
21
22#include "CepGen/Event/Event.h"
25#include "CepGen/Utils/Value.h"
26
27namespace cepgen {
32 public:
33 explicit TextEventHandler(const ParametersList& params)
34 : EventExporter(params), save_banner_(steer<bool>("saveBanner")), print_every_(steer<int>("printEvery")) {
35 if (const auto& filename = steer<std::string>("filename"); !filename.empty())
36 out_ = new std::ofstream(filename);
37 else
38 out_ = &std::cout;
39 }
41 if (out_ != &std::cout) {
42 if (auto* fout = dynamic_cast<std::ofstream*>(out_); fout)
43 fout->close();
44 delete out_;
45 }
46 }
47
49 auto desc = EventExporter::description();
50 desc.setDescription("Simple text-based event dumper");
51 desc.add<bool>("saveBanner", true).setDescription("Save boilerplate in output file?");
52 desc.add<int>("printEvery", 10).setDescription("Period at which events are dumped");
53 desc.add<std::string>("filename", "").setDescription("Output filename");
54 return desc;
55 }
56
57 void setCrossSection(const Value& cross_section) override {
58 if (out_ != &std::cout)
59 *out_ << "Total cross-section: " << cross_section << " pb.\n";
60 }
61 bool operator<<(const Event& ev) override {
62 if (print_every_ < 0 || event_num_++ % print_every_ == 0)
63 *out_ << ev << "\n";
64 return true;
65 }
66
67 private:
68 void initialise() override {
69 if (save_banner_)
70 *out_ << banner("#") << "\n";
71 }
72 const bool save_banner_;
73 const int print_every_;
74 std::ostream* out_{nullptr};
75 };
76} // namespace cepgen
77REGISTER_EXPORTER("dump", TextEventHandler);
#define REGISTER_EXPORTER(name, obj)
Add a generic export module definition to the factory.
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.
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
Simple event dump module.
TextEventHandler(const ParametersList &params)
static ParametersDescription description()
void setCrossSection(const Value &cross_section) override
Specify the cross section value, in pb.
bool operator<<(const Event &ev) override
Writer operator.
A scalar value with its uncertainty.
Definition Value.h:26
Common namespace for this Monte Carlo generator.