cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
HepMC2Importer.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2022-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 <HepMC/GenEvent.h>
20#include <HepMC/IO_GenEvent.h>
21#include <HepMC/Version.h>
22
23#include <memory>
24
29
30namespace cepgen {
34 class HepMC2Importer final : public EventImporter {
35 public:
37 explicit HepMC2Importer(const ParametersList& params)
38 : EventImporter(params), reader_(new HepMC::IO_GenEvent(steer<std::string>("filename"), std::ios::in)) {
39 if (!reader_)
40 throw CG_FATAL("HepMC2Importer") << "Failed to initialise HepMCv2 reader.";
41 CG_INFO("HepMC2Importer") << "Interfacing module initialised "
42 << "for HepMC version " << HEPMC_VERSION << " and HepMC ASCII file '"
43 << steer<std::string>("filename") << "' with I/O state " << reader_->rdstate() << ".";
44 }
45
46 bool operator>>(Event& evt) override {
47 HepMC::GenEvent event;
48 if (!reader_->fill_next_event(&event))
49 return false;
50 if (!cross_section_retrieved_) {
51 if (const auto xsec = event.cross_section(); xsec)
52 setCrossSection(Value{xsec->cross_section(), xsec->cross_section_error()});
53 cross_section_retrieved_ = true;
54 }
55 CG_DEBUG("HepMC2Importer").log([&event](auto& log) { event.print(log.stream()); });
56 evt = Event(static_cast<const HepMC::CepGenEvent&>(event));
57 return true;
58 }
59
61 auto desc = EventImporter::description();
62 desc.setDescription("HepMC2 ASCII file importer module");
63 desc.add<std::string>("filename", "input.hepmc").setDescription("Input filename");
64 return desc;
65 }
66
67 private:
68 void initialise() override {}
69 const std::unique_ptr<HepMC::IO_GenEvent> reader_;
70 bool cross_section_retrieved_{false};
71 };
72} // namespace cepgen
73REGISTER_EVENT_IMPORTER("hepmc2", HepMC2Importer);
#define REGISTER_EVENT_IMPORTER(name, obj)
Add a generic import module definition to the factory.
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_DEBUG(mod)
Definition Message.h:220
#define CG_INFO(mod)
Definition Message.h:216
Interfacing between CepGen and HepMC2 event definitions.
Base event importer module.
static ParametersDescription description()
void setCrossSection(const Value &xsec)
Specify the process cross section and uncertainty, in pb.
Container for the information on the in- and outgoing particles' kinematics.
Definition Event.h:28
Handler for the HepMC file output.
static ParametersDescription description()
HepMC2Importer(const ParametersList &params)
Class constructor.
bool operator>>(Event &evt) override
Read the next event.
A description object for parameters collection.
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
Common namespace for this Monte Carlo generator.