diff --git a/run/SimExamples/Pythia8_HepMC_Wrapper/Pythia8HepMC3.C b/run/SimExamples/Pythia8_HepMC_Wrapper/Pythia8HepMC3.C new file mode 100644 index 0000000000000..9e60bc0a5d851 --- /dev/null +++ b/run/SimExamples/Pythia8_HepMC_Wrapper/Pythia8HepMC3.C @@ -0,0 +1,44 @@ +/// \author Marco Giacalone - March 2025 + +// A simple wrapper and demonstrator around Pythia8 for extracting HepMC3 files. + +#include "Pythia8/Pythia.h" +#include "Pythia8Plugins/HepMC3.h" + +using namespace o2::eventgen; + +class HepMC3_Pythia8Wrapper : public GeneratorPythia8 +{ + public: + HepMC3_Pythia8Wrapper(std::string filename = "pythia8.hepmc") : GeneratorPythia8(), mFileName(filename) + { + // HepMC conversion object. + mToHepMC = std::make_unique(); + mToHepMC->setNewFile((filename == "" ? "pythia.hepmc" : filename)); + }; + ~HepMC3_Pythia8Wrapper() = default; + + bool importParticles() override + { + // events are written after the importParticles step + // since some filtering is happening there + auto ret = GeneratorPythia8::importParticles(); + if (ret) { + LOG(info) << "Writing event to HepMC3 format"; + mToHepMC->writeNextEvent(mPythia); + } + return ret; + }; + + private: + std::string mFileName = "pythia8.hepmc"; + std::unique_ptr mToHepMC; +}; + +FairGenerator* + hepmc_pythia8(std::string filename = "pythia8.hepmc") +{ + std::cout << "HepMC3_Pythia8Wrapper initialising with filename: " << filename << std::endl; + auto py8 = new HepMC3_Pythia8Wrapper(filename); + return py8; +} diff --git a/run/SimExamples/Pythia8_HepMC_Wrapper/README.md b/run/SimExamples/Pythia8_HepMC_Wrapper/README.md new file mode 100644 index 0000000000000..a334b7b3ef81e --- /dev/null +++ b/run/SimExamples/Pythia8_HepMC_Wrapper/README.md @@ -0,0 +1,16 @@ + + +This example demonstrates how we can extend GeneratorPythia8 in a user-defined macro (or external generator), +to achieve additional HepMC3 export of generated Pythia8 events. + +The example provides a small utility for poeple in need to obtain HepMC files from Pythia8. +Note that many other methods to achieve this are possible (See original Pythia8 example). + +The example provides: + +- The external generator implementation `Pythia8HepMC3.C` +- a `run.sh` script demonstrating it's usage and a check feeding back the generated hepmc into the simulation + + diff --git a/run/SimExamples/Pythia8_HepMC_Wrapper/run.sh b/run/SimExamples/Pythia8_HepMC_Wrapper/run.sh new file mode 100755 index 0000000000000..7e4501d0d659f --- /dev/null +++ b/run/SimExamples/Pythia8_HepMC_Wrapper/run.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# +# Script doing Pythia8 event generation and writing these events into HepMC3 files +# (next to generating the usual MCTrack kinematics output). +# +# The script also performs a second event generation based on the generated HepMC3 files. +# In principle it should yield identical kinematics files. +# + +NEVENTS=1000 +SEED=11 + +o2-sim -j 1 -g external --configKeyValues 'GeneratorExternal.fileName=Pythia8HepMC3.C;GeneratorExternal.funcName=hepmc_pythia8("skimmed.hepmc");GeneratorPythia8.config=${O2_ROOT}/share/Generators/egconfig/pythia8_inel.cfg' --seed ${SEED} --noGeant -o pythia8_skimmed -n ${NEVENTS} +o2-sim -j 1 -g external --configKeyValues 'GeneratorExternal.fileName=Pythia8HepMC3.C;GeneratorExternal.funcName=hepmc_pythia8("unskimmed.hepmc");GeneratorPythia8.config=${O2_ROOT}/share/Generators/egconfig/pythia8_inel.cfg;GeneratorPythia8.includePartonEvent=true' --seed ${SEED} --noGeant -o pythia8_unskimmed -n ${NEVENTS} + +# propagate generated hepmc file; it should produce the same kinematics as the original Pythia8 +o2-sim -j 1 -g hepmc --configKeyValues="GeneratorFileOrCmd.fileNames=skimmed.hepmc" --vertexMode kNoVertex --noGeant -o fromhepmc_skimmed -n ${NEVENTS} --seed ${SEED} +o2-sim -j 1 -g hepmc --configKeyValues="GeneratorFileOrCmd.fileNames=unskimmed.hepmc" --vertexMode kNoVertex --noGeant -o fromhepmc_unskimmed -n ${NEVENTS} --seed ${SEED} diff --git a/run/SimExamples/README.md b/run/SimExamples/README.md index 725d60c4854ca..3a54625acf413 100644 --- a/run/SimExamples/README.md +++ b/run/SimExamples/README.md @@ -6,6 +6,7 @@