File indexing completed on 2026-04-18 08:20:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef Pythia8_HepMC3Hooks_H
0012 #define Pythia8_HepMC3Hooks_H
0013
0014
0015 #include "Pythia8/Pythia.h"
0016 #include "Pythia8/Plugins.h"
0017 #include "Pythia8Plugins/HepMC3.h"
0018
0019
0020 #include <sys/stat.h>
0021
0022 namespace Pythia8 {
0023
0024
0025
0026
0027
0028
0029 bool makeDir(vector<string> path, mode_t mode = 0777) {
0030
0031
0032 struct stat info;
0033
0034
0035 string pathNow = "";
0036 bool first = true;
0037 for (string& dir : path) {
0038
0039
0040 pathNow += (first ? "" : "/") + dir;
0041 first = false;
0042 if (stat(pathNow.c_str(), &info) == 0) {
0043 if ((info.st_mode & S_IFDIR) == 0) return false;
0044 else continue;
0045 }
0046
0047
0048 if (mkdir(pathNow.c_str(), mode) != 0) return false;
0049 }
0050 return true;
0051
0052 }
0053
0054
0055
0056
0057
0058 class HepMC3Hooks : public UserHooks {
0059
0060 public:
0061
0062
0063 HepMC3Hooks() {}
0064 HepMC3Hooks(Pythia* pythiaPtrIn, Settings*, Logger*) :
0065 pythiaPtr(pythiaPtrIn) {}
0066 ~HepMC3Hooks() {if (hepMCPtr != nullptr) delete hepMCPtr;}
0067
0068
0069
0070
0071 void onEndEvent(Status) override {
0072
0073
0074 if (hepMCPtr == nullptr) {
0075
0076
0077 string filename = word("HepMC:filename");
0078 int idx = mode("Parallelism:index");
0079 if (idx >= 0) {
0080 size_t iSuffix = filename.find(".hepmc");
0081 if (iSuffix != string::npos)
0082 filename = filename.substr(0,iSuffix);
0083 filename = filename + "_" + to_string(idx) + ".hepmc";
0084 }
0085
0086
0087 vector<string> path = splitString(filename, "/");
0088 if (path.size() > 1) {
0089 mutexPtr->lock();
0090 makeDir(vector<string>(path.begin(), path.end() - 1));
0091 mutexPtr->unlock();
0092 }
0093 hepMCPtr = new Pythia8ToHepMC(filename);
0094
0095
0096 hepMCPtr->set_print_inconsistency(flag("HepMC:printInconsistency"));
0097 hepMCPtr->set_free_parton_warnings(flag("HepMC:freePartonWarnings"));
0098 hepMCPtr->set_store_pdf(flag("HepMC:storePDF"));
0099 hepMCPtr->set_store_proc(flag("HepMC:storeProcess"));
0100 }
0101
0102
0103 hepMCPtr->fillNextEvent(*pythiaPtr);
0104
0105
0106 hepMCPtr->writeEvent();
0107
0108 }
0109
0110
0111
0112
0113 void onStat() override {}
0114
0115
0116
0117
0118
0119 void onStat(vector<PhysicsBase*>, Pythia*) override {
0120 onStat();
0121 }
0122
0123 private:
0124
0125 Pythia* pythiaPtr{};
0126 Pythia8ToHepMC* hepMCPtr{};
0127
0128 };
0129
0130
0131
0132
0133
0134 void hepmcSettings(Settings *settingsPtr) {
0135 settingsPtr->addWord("HepMC:fileName", "events.hepmc");
0136 settingsPtr->addFlag("HepMC:printInconsistency", "true");
0137 settingsPtr->addFlag("HepMC:freePartonWarnings", "true");
0138 settingsPtr->addFlag("HepMC:storePDF", "true");
0139 settingsPtr->addFlag("HepMC:storeProcess", "true");
0140 }
0141
0142
0143
0144
0145
0146 PYTHIA8_PLUGIN_CLASS(UserHooks, HepMC3Hooks, true, false, false)
0147 PYTHIA8_PLUGIN_SETTINGS(hepmcSettings)
0148 PYTHIA8_PLUGIN_PARALLEL(true)
0149 PYTHIA8_PLUGIN_VERSIONS(PYTHIA_VERSION_INTEGER)
0150
0151
0152
0153 }
0154
0155 #endif