Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions run/SimExamples/Pythia8_HepMC_Wrapper/Pythia8HepMC3.C
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 10 in run/SimExamples/Pythia8_HepMC_Wrapper/Pythia8HepMC3.C

View workflow job for this annotation

GitHub Actions / PR formatting / copyright headers

Missing or malformed copyright notice

This source file is missing the correct copyright notice.
{
public:
HepMC3_Pythia8Wrapper(std::string filename = "pythia8.hepmc") : GeneratorPythia8(), mFileName(filename)
{
// HepMC conversion object.
mToHepMC = std::make_unique<Pythia8::Pythia8ToHepMC>();
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<Pythia8::Pythia8ToHepMC> 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;
}
16 changes: 16 additions & 0 deletions run/SimExamples/Pythia8_HepMC_Wrapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- doxy
\page refrunSimExamplesPythiaHepMCWrapper Example showing easy HepMC extraction using GeneratorPythia8
/doxy -->

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


19 changes: 19 additions & 0 deletions run/SimExamples/Pythia8_HepMC_Wrapper/run.sh
Original file line number Diff line number Diff line change
@@ -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}
1 change: 1 addition & 0 deletions run/SimExamples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<!-- doxy
* \subpage refrunSimExamplesPythia8
* \subpage refrunSimExamplesPythiaHepMCWrapper
* \subpage refrunSimExamplesHF_Embedding_Pythia8
* \subpage refrunSimExamplesSignal_ImpactB
* \subpage refrunSimExamplesTrigger_ImpactB_Pythia8
Expand Down
Loading