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