## HepMC Interface

An interface to both the legacy HepMC2 [Dob01] and current HepMC3 [Buc19] standard event record format has been provided by M. Kirsanov and the HepMC3 collaboration. The code is stored in `include/Pythia8Plugins/HepMC2.h` and `include/Pythia8Plugins/HepMC3.h`, respectively. To use it, the relevant libraries need to be linked, as explained in the `README` file. Only version 2.06 (or later) of HepMC2 is supported, and all version of HepMC3 are supported, by agreement with the LHC experimental community. All examples use the HepMC3 interface, with the exceptions of `main144.cc` and optionally `main134.cc` which still utilise the HepMC2 interface and are provided as legacy examples.

The (simple) procedure to translate PYTHIA 8 events into HepMC ones is illustrated in the `main131.cc`, `main132.cc` and `main133.cc` main programs. At the core is a call to the

    HepMC3::Pythia8ToHepMC3::fill_next_event( pythia, hepmcevt, ievnum = -1)

which takes a reference of the generator object and uses it, on the one hand, to read out and convert the event record in `pythia.event` and, on the other hand, to extract and store parton-density (PDF), cross section and other information for the hard subprocess from `pythia.info`. There is also an alternative form that does not requires access to the full `pythia` object, but only the event record, at the expense of a reduced information storage, see below.

While PYTHIA always stores momenta in units of GeV, with c = 1, the `HepMC3::GenEvent` event record class may be created with units of MeV or GeV and mm or cm, see e.g. the `main131.cc` code. When filling the HepMC event record, PYTHIA will switch the current HepMC event record units to GeV and mm. This can be switched back by the user with the `HepMC3::GeneEvent::set_units` method. Note, the default units in HepMC3 are GeV and mm. This differs from HepMC2 where the library is built with user specified default units.

The status code is now based on the new standard introduced for HepMC 2.05, see the Event::statusHepMC(...) conversion routine for details.

The current values from `pythia.info.sigmaGen()` and `pythia.info.sigmaErr()` are stored for each event, multiplied by 10^9 to convert from mb to pb. Note that PYTHIA improves its accuracy by Monte Carlo integration in the course of the run, so the values associated with the last generated event should be the most accurate ones. If events also come with a dimensional weight, like in some Les Houches strategies, this weight is in units of pb.

### Direct output to HepMC via plugin

A PYTHIA plugin is available, `HepMC3Hooks` defined in `Pythia8Plugins/HepMC3Hooks.h`, which allows for the easy writing of PYTHIA output to HepMC3 files. The plugin works with `PythiaParallel`, and HepMC3 output is written out to a different text file for each thread. For the plugin to be available, PYTHIA must be configured with the `--with-hepmc3` flag before being built.

      ./configure --with-hepmc3

If successful, the plugin library `lib/pythia8hepmc3.so` will be built.

The example `main225` and corresponding command file `main225.cmnd` demonstrate how to use the plugin. The general usage is as follows. First, load the plugin.

      Init:plugins = {libpythia8hepmc3.so::HepMC3Hooks}

None of the options described below is available if the plugin is not successfully loaded. Once the plugin is loaded, a number of additional settings will be available. To begin, set the output HepMC3 file name.

      HepMC3:fileName = NAME

If directory structure is included in the HepMC3 file name, an attempt will be made to create that directory structure. If the `HepMC3Hooks` are run with more than one thread in `PythiaParallel`, the given file name will be appended by the thread number. Multiple threads writing into a single HepMC3 file is currently not supported. Additional options control what is written to the HepMC file, as well as warnings that are printed by HepMC.

      ! Warn if there are inconsistencies.
      HepMC:printInconsistency = true
      ! Warn if there are final partons.
      HepMC:freePartonWarnings = true
      ! Flag to store the PDF information.
      HepMC:storePDF = true
      ! Flag to store the process information.
      HepMC:storeProcess = true

### Direct output to HepMC

Provided that PYTHIA is properly linked to HepMC, implementation of the most common user case (run PYTHIA with a runcard, output HepMC) exists. The example `main144.cc` implements this. The sample command file `main144.cmnd` provides a good starting point. The line:

        Main:writeHepMC = on

is the switch needed to write a HepMC file. The example is then run with:

        ./main144 -c main144.cmnd

and a HepMC file is then written. Note that this example currently uses the legacy HepMC2 library to maintain Rivet compatability.

There are several other useful command line options to `main144`. They are all displayed by running `./main144 -h`.

### The HepMC3 public methods

Here comes a complete list of all public methods of the `Pythia8ToHepMC3` class in the `HepMC3` (not `Pythia8`!) namespace.

Pythia8ToHepMC3::Pythia8ToHepMC()
virtual Pythia8ToHepMC3::~Pythia8ToHepMC3()
the constructor and destructor take no arguments.

bool Pythia8ToHepMC3::fill_next_event( Pythia8::Pythia& pythia, GenEvent* evt, int ievnum = -1)
convert a `Pythia` event to a `HepMC` one. Will return true if succeeded.
`argument` pythia : the input `Pythia` generator object, from which both the event and other information can be obtained.
`argument` evt : the output `GenEvt` event, in its standard form.
`argument` iev : set the event number of the current event. If negative then the internal event number is used, which is incremented by one for each new event.

bool Pythia8ToHepMC3::fill_next_event( Pythia8::Event& pyev, GenEvent* evt, int ievnum = -1, Pythia8::Info* pyinfo = 0, Pythia8::Settings* pyset = 0)
convert a `Pythia` event to a `HepMC` one. Will return true if succeeded.
`argument` pyev : the input `Pythia` event that is to be converted to HepMC format.
`argument` evt : the output `GenEvt` event, in its standard form.
`argument` iev : set the event number of the current event. If negative then the internal event number is used, which is incremented by one for each new event.
`argument` pyinfo : pointer to the `Pythia Info` object, which is used to extract PFD values, and process and cross section information. Without such a pointer this information therefore cannot be stored, i.e. it is equivalent to the three `set_store` methods below being set false.
`argument` pyset : pointer to the `Pythia Settings` object, which is used to decide whether hadronization is carried out, and therefore whether to warn about unhadronized partons. Without such a pointer the `set_free_parton_warnings` method below uniquely controls the behaviour.

The following paired methods can be used to set and get the status of some switches that modify the behaviour of the conversion routine. The `set` methods have the same default input values as the internal initialization ones, so that a call without an argument (re)stores the default.

void Pythia8ToHepMC3::set_print_inconsistency(bool b = true)
bool Pythia8ToHepMC3::print_inconsistency()
print a warning line, on `cerr`, when inconsistent mother and daughter information is encountered.

void Pythia8ToHepMC3::set_free_parton_warnings(bool b = true)
bool Pythia8ToHepMC3::free_parton_warnings()
check for when unhadronized gluons or quarks are encountered in the event record, and issue a warning when encountered. Does not apply when Pythia hadronization is switched off. Default is to do this check. If `set_crash_on_problem()` is true, then the program will abort.

void Pythia8ToHepMC3::set_crash_on_problem(bool b = false)
bool Pythia8ToHepMC3::crash_on_problem()
when set to true, `free_parton_warnings()` is true, and unhadronized gluons or quarks are encountered in the event record then the program will abort.

void Pythia8ToHepMC3::set_convert_gluon_to_0(bool b = false)
bool Pythia8ToHepMC3::convert_gluon_to_0()
the normal gluon identity code 21 is used also when parton density information is stored, unless this optional argument is set true to have gluons represented by a 0. This choice does not affect the normal event record, where a gluon is always 21.

void Pythia8ToHepMC3::set_store_pdf(bool b = true)
bool Pythia8ToHepMC3::store_pdf()
for each event store information on the two incoming flavours, their x values and common factorization scale, and the values of the two parton distributions, xf(x,Q).

void Pythia8ToHepMC3::set_store_proc(bool b = true)
bool Pythia8ToHepMC::store_proc()
for each event store information on the Pythia process code, the renormalization scale, and alpha_em and alpha_s values used for the hard process.

void Pythia8ToHepMC3::set_store_xsec(bool b = true)
bool Pythia8ToHepMC3::store_xsec()
for each event store information on the Pythia cross section and its error, in pb, and the event weight. If events also come with a dimensional weight, like in some Les Houches strategies, this weight is in units of pb.

### The HepMC2 public methods

These methods are very similar to the HepMC3 methods but now all public methods of the `Pythia8ToHepMC` class in the `HepMC` namespace are given.

Pythia8ToHepMC::Pythia8ToHepMC()
virtual Pythia8ToHepMC::~Pythia8ToHepMC()
the constructor and destructor take no arguments.

bool Pythia8ToHepMC::fill_next_event( Pythia8::Pythia& pythia, GenEvent* evt, int ievnum = -1, bool append = false, GenParticle* rootParticle = 0, int iBarcode = -1)
convert a `Pythia` event to a `HepMC` one. Will return true if succeeded.
`argument` pythia : the input `Pythia` generator object, from which both the event and other information can be obtained.
`argument` evt : the output `GenEvt` event, in its standard form.
`argument` iev : set the event number of the current event. If negative then the internal event number is used, which is incremented by one for each new event.
`argument` append : if `true` then the input event is appended to the current `HepMC` event record, rather than starting a new one.
`argument` rootParticle : the root particle that defines the new production vertex for the particles to be added in the `append = true` option.
`argument` iBarcode : used to set the bar code when `append = true`. If positive then start from `iBarcode` itself, if negative then start from the current size of the `HepMC` event record, and if 0 then set all bar codes to vanish.

bool Pythia8ToHepMC::fill_next_event( Pythia8::Event& pyev, GenEvent* evt, int ievnum = -1, Pythia8::Info* pyinfo = 0, Pythia8::Settings* pyset = 0, bool append = false, GenParticle* rootParticle = 0, int iBarcode = -1)
convert a `Pythia` event to a `HepMC` one. Will return true if succeeded.
`argument` pyev : the input `Pythia` event that is to be converted to HepMC format.
`argument` evt : the output `GenEvt` event, in its standard form.
`argument` iev : set the event number of the current event. If negative then the internal event number is used, which is incremented by one for each new event.
`argument` pyinfo : pointer to the `Pythia Info` object, which is used to extract PFD values, and process and cross section information. Without such a pointer this information therefore cannot be stored, i.e. it is equivalent to the three `set_store` methods below being set false.
`argument` pyset : pointer to the `Pythia Settings` object, which is used to decide whether hadronization is carried out, and therefore whether to warn about unhadronized partons. Without such a pointer the `set_free_parton_warnings` method below uniquely controls the behaviour.
`argument` append : if `true` then the input event is appended to the current `HepMC` event record, rather than starting a new one.
`argument` rootParticle : the root particle that defines the new production vertex for the particles to be added in the `append = true` option.
`argument` iBarcode : used to set the bar code when `append = true`. If positive then start from `iBarcode` itself, if negative then start from the current size of the `HepMC` event record, and if 0 then set all bar codes to vanish.

The following paired methods can be used to set and get the status of some switches that modify the behaviour of the conversion routine. The `set` methods have the same default input values as the internal initialization ones, so that a call without an argument (re)stores the default.

void Pythia8ToHepMC::set_print_inconsistency(bool b = true)
bool Pythia8ToHepMC::print_inconsistency()
print a warning line, on `cerr`, when inconsistent mother and daughter information is encountered.

void Pythia8ToHepMC::set_free_parton_exception(bool b = true)
bool Pythia8ToHepMC::free_parton_exception()
check and throw an exception when unhadronized gluons or quarks are encountered in the event record. Does not apply when Pythia hadronization is switched off. Default is to do this check. If an exception is thrown the `PartonEndVertexException` class will return a warning message. The calling code can choose action to take, also having access to the location (`index()`) and species (`pdg_id()`) of a bad parton.

void Pythia8ToHepMC::set_convert_gluon_to_0(bool b = false)
bool Pythia8ToHepMC::convert_gluon_to_0()
the normal gluon identity code 21 is used also when parton density information is stored, unless this optional argument is set true to have gluons represented by a 0. This choice does not affect the normal event record, where a gluon is always 21.

void Pythia8ToHepMC::set_store_pdf(bool b = true)
bool Pythia8ToHepMC::store_pdf()
for each event store information on the two incoming flavours, their x values and common factorization scale, and the values of the two parton distributions, xf(x,Q).

void Pythia8ToHepMC::set_store_proc(bool b = true)
bool Pythia8ToHepMC::store_proc()
for each event store information on the Pythia process code, the renormalization scale, and alpha_em and alpha_s values used for the hard process.

void Pythia8ToHepMC::set_store_xsec(bool b = true)
bool Pythia8ToHepMC::store_xsec()
for each event store information on the Pythia cross section and its error, in pb, and the event weight. If events also come with a dimensional weight, like in some Les Houches strategies, this weight is in units of pb.