HepMC3 event record library
pythia8_example.cc
1/**
2 * @example pythia8_example.cc
3 * @brief Basic example of use for pythia8 interface
4 *
5 */
6#include "HepMC3/GenEvent.h"
8#include "HepMC3/Print.h"
9
10#include "Pythia8/Pythia.h"
11#include "Pythia8Plugins/HepMC3.h"
12#include <iostream>
13using namespace HepMC3;
14
15/** Main program */
16int main(int argc, char **argv) {
17 if (argc < 3) {
18 std::cout << "Usage: " << argv[0] << " <pythia_config_file> <output_hepmc3_file>" << std::endl;
19 exit(-1);
20 }
21
22 Pythia8::Pythia pythia;
23 Pythia8ToHepMC3 pythiaToHepMC;
24 pythia.readFile(argv[1]);
25 pythia.init();
26 std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();
27 struct GenRunInfo::ToolInfo generator = {std::string("Pythia8"),std::to_string(PYTHIA_VERSION).substr(0,5),std::string("Used generator")};
28 run->tools().push_back(generator);
29 struct GenRunInfo::ToolInfo config = {std::string(argv[1]),"1.0",std::string("Control cards")};
30 run->tools().push_back(config);
31 std::vector<std::string> names;
32 for (int iWeight = 0; iWeight < pythia.info.nWeights(); ++iWeight) {
33 std::string s = pythia.info.weightLabel(iWeight);
34 if (!s.length()) s = std::to_string(static_cast<long long int>(iWeight));
35 names.push_back(s);
36 }
37 if (names.empty()) names.emplace_back("default");
38 run->set_weight_names(names);
39 WriterAscii file(argv[2],run);
40
41 int nEvent = pythia.mode("Main:numberOfEvents");
42
43 for( int i = 0; i < nEvent; ++i ) {
44 if( !pythia.next() ) continue;
45
46 GenEvent hepmc( Units::GEV, Units::MM );
47
48 pythiaToHepMC.fill_next_event(pythia.event, &hepmc, -1, &pythia.info);
49
50 if( i == 0 ) {
51 std::cout << "First event: " << std::endl;
52 Print::listing(hepmc);
53 }
54
55 file.write_event(hepmc);
56 }
57
58 file.close();
59 pythia.stat();
60}
Definition of class GenEvent.
Definition of static class Print.
Definition of class WriterAscii.
Stores event-related information.
Definition GenEvent.h:47
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition Print.cc:52
GenEvent I/O serialization for structured text files.
Definition WriterAscii.h:25
HepMC3 main namespace.
Interrnal struct for keeping track of tools.
Definition GenRunInfo.h:38