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 #include "Pythia8ToHepMC3.h"
12 
13 #include <iostream>
14 using namespace HepMC3;
15 
16 /** Main program */
17 int main(int argc, char **argv) {
18  if (argc < 3) {
19  std::cout << "Usage: " << argv[0] << " <pythia_config_file> <output_hepmc3_file>" << std::endl;
20  exit(-1);
21  }
22 
23  Pythia8::Pythia pythia;
24  Pythia8ToHepMC3 pythiaToHepMC;
25  pythia.readFile(argv[1]);
26  pythia.init();
27  std::shared_ptr<GenRunInfo> run = std::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 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