HepMC3 event record library
pythia8_example.cc

Basic example of use for pythia8 interface

/**
* @example pythia8_example.cc
* @brief Basic example of use for pythia8 interface
*
*/
#include "HepMC3/Print.h"
#include "Pythia8/Pythia.h"
#include "Pythia8ToHepMC3.h"
#include <iostream>
using namespace HepMC3;
/** Main program */
int main(int argc, char **argv) {
if (argc < 3) {
std::cout << "Usage: " << argv[0] << " <pythia_config_file> <output_hepmc3_file>" << std::endl;
exit(-1);
}
Pythia8::Pythia pythia;
Pythia8ToHepMC3 pythiaToHepMC;
pythia.readFile(argv[1]);
pythia.init();
std::shared_ptr<GenRunInfo> run = std::make_shared<GenRunInfo>();
struct GenRunInfo::ToolInfo generator={std::string("Pythia8"),std::to_string(PYTHIA_VERSION).substr(0,5),std::string("Used generator")};
run->tools().push_back(generator);
struct GenRunInfo::ToolInfo config={std::string(argv[1]),"1.0",std::string("Control cards")};
run->tools().push_back(config);
std::vector<std::string> names;
for (int iWeight=0; iWeight < pythia.info.nWeights(); ++iWeight) {
std::string s=pythia.info.weightLabel(iWeight);
if (!s.length()) s=std::to_string((long long int)iWeight);
names.push_back(s);
}
if (!names.size()) names.push_back("default");
run->set_weight_names(names);
WriterAscii file(argv[2],run);
int nEvent = pythia.mode("Main:numberOfEvents");
for( int i = 0; i< nEvent; ++i ) {
if( !pythia.next() ) continue;
GenEvent hepmc( Units::GEV, Units::MM );
pythiaToHepMC.fill_next_event(pythia.event, &hepmc, -1, &pythia.info);
if( i==0 ) {
std::cout << "First event: " << std::endl;
}
file.write_event(hepmc);
}
file.close();
pythia.stat();
}
GenEvent.h
Definition of class GenEvent.
HepMC3::GenRunInfo::tools
const std::vector< ToolInfo > & tools() const
The vector of tools used to produce this run.
Definition: GenRunInfo.h:63
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:41
HepMC3
HepMC3 main namespace.
Definition: AnalysisExample.h:18
HepMC3::Print::listing
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
WriterAscii.h
Definition of class WriterAscii.
Print.h
Definition of static class Print.
HepMC3::GenRunInfo::set_weight_names
void set_weight_names(const std::vector< std::string > &names)
Set the names of the weights in this run.
Definition: GenRunInfo.cc:18
HepMC3::GenRunInfo::ToolInfo
Interrnal struct for keeping track of tools.
Definition: GenRunInfo.h:38
main
int main(int argc, char **argv)
Definition: rootIOTree_example_read.cc:23
HepMC3::WriterAscii::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterAscii.cc:63
HepMC3::WriterAscii::close
void close() override
Close file stream.
Definition: WriterAscii.cc:354