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