PYTHIA  8.316
HepMC3Hooks.h
1 // HepMC3Hooks.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2025 Philip Ilten, Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // Author: Christian T. Preuss.
7 
8 // This class implements an interface to HepMC3 that can be loaded
9 // via the plugin structure. It can be run with PythiaParallel.
10 
11 #ifndef Pythia8_HepMC3Hooks_H
12 #define Pythia8_HepMC3Hooks_H
13 
14 // Pythia includes.
15 #include "Pythia8/Pythia.h"
16 #include "Pythia8/Plugins.h"
17 #include "Pythia8Plugins/HepMC3.h"
18 
19 // Directory creation for POSIX.
20 #include <sys/stat.h>
21 
22 namespace Pythia8 {
23 
24 //==========================================================================
25 
26 // Create a directory, with nesting if necessary. This is not compatible
27 // outside POSIX systems, and should be removed after migration to C++17.
28 
29 bool makeDir(vector<string> path, mode_t mode = 0777) {
30 
31  // Create the structure needed for stat.
32  struct stat info;
33 
34  // Loop over the directories.
35  string pathNow = "";
36  bool first = true;
37  for (string& dir : path) {
38 
39  // Check if the directory exists.
40  pathNow += (first ? "" : "/") + dir;
41  first = false;
42  if (stat(pathNow.c_str(), &info) == 0) {
43  if ((info.st_mode & S_IFDIR) == 0) return false;
44  else continue;
45  }
46 
47  // Create the directory and check it exists.
48  if (mkdir(pathNow.c_str(), mode) != 0) return false;
49  }
50  return true;
51 
52 }
53 
54 //==========================================================================
55 
56 // UserHook to write HepMC3 files.
57 
58 class HepMC3Hooks : public UserHooks {
59 
60 public:
61 
62  // Constructors and destructor.
64  HepMC3Hooks(Pythia* pythiaPtrIn, Settings*, Logger*) :
65  pythiaPtr(pythiaPtrIn) {}
66  ~HepMC3Hooks() {if (hepMCPtr != nullptr) delete hepMCPtr;}
67 
68  //--------------------------------------------------------------------------
69 
70  // Print event to HepMC file.
71  void onEndEvent(Status) override {
72 
73  // Create the HepMC converter.
74  if (hepMCPtr == nullptr) {
75 
76  // Set the filename if running in parallel.
77  string filename = word("HepMC:filename");
78  int idx = mode("Parallelism:index");
79  if (idx >= 0) {
80  size_t iSuffix = filename.find(".hepmc");
81  if (iSuffix != string::npos)
82  filename = filename.substr(0,iSuffix);
83  filename = filename + "_" + to_string(idx) + ".hepmc";
84  }
85 
86  // Create a HepMC converter and directory structure if needed.
87  vector<string> path = splitString(filename, "/");
88  if (path.size() > 1) {
89  mutexPtr->lock();
90  makeDir(vector<string>(path.begin(), path.end() - 1));
91  mutexPtr->unlock();
92  }
93  hepMCPtr = new Pythia8ToHepMC(filename);
94 
95  // Save some settings.
96  hepMCPtr->set_print_inconsistency(flag("HepMC:printInconsistency"));
97  hepMCPtr->set_free_parton_warnings(flag("HepMC:freePartonWarnings"));
98  hepMCPtr->set_store_pdf(flag("HepMC:storePDF"));
99  hepMCPtr->set_store_proc(flag("HepMC:storeProcess"));
100  }
101 
102  // Convert the event to HepMC.
103  hepMCPtr->fillNextEvent(*pythiaPtr);
104 
105  // Write event. Currently each thread writes into its own file.
106  hepMCPtr->writeEvent();
107 
108  }
109 
110  //--------------------------------------------------------------------------
111 
112  // Finalise.
113  void onStat() override {}
114 
115  //--------------------------------------------------------------------------
116 
117  // Finalise. Currently, nothing is done here, but merging of the HepMC3
118  // records could be done here if this is a useful feature.
119  void onStat(vector<PhysicsBase*>, Pythia*) override {
120  onStat();
121  }
122 
123 private:
124 
125  Pythia* pythiaPtr{};
126  Pythia8ToHepMC* hepMCPtr{};
127 
128 };
129 
130 //--------------------------------------------------------------------------
131 
132 // Register HepMC settings.
133 
135  settingsPtr->addWord("HepMC:fileName", "events.hepmc");
136  settingsPtr->addFlag("HepMC:printInconsistency", "true");
137  settingsPtr->addFlag("HepMC:freePartonWarnings", "true");
138  settingsPtr->addFlag("HepMC:storePDF", "true");
139  settingsPtr->addFlag("HepMC:storeProcess", "true");
140 }
141 
142 //--------------------------------------------------------------------------
143 
144 // Declare the plugin.
145 
146 PYTHIA8_PLUGIN_CLASS(UserHooks, HepMC3Hooks, true, false, false)
147 PYTHIA8_PLUGIN_SETTINGS(hepmcSettings)
149 PYTHIA8_PLUGIN_VERSIONS(PYTHIA_VERSION_INTEGER)
150 
151 //==========================================================================
152 
153 } // end namespace Pythia8
154 
155 #endif // end Pythia8_HepMC3Hooks_H
vector< string > splitString(string val, string delim)
Split a string by a delimiter.
Definition: PythiaStdlib.cc:72
Settings * settingsPtr
Pointer to the settings database.
Definition: PhysicsBase.h:85
mutex * mutexPtr
Mutex that should be locked for thread-unsafe code.
Definition: PhysicsBase.h:131
PYTHIA8_PLUGIN_PARALLEL(true)
Declare the plugin.
void onStat(vector< PhysicsBase * >, Pythia *) override
Definition: HepMC3Hooks.h:119
Definition: Logger.h:23
HepMC3Hooks()
Constructors and destructor.
Definition: HepMC3Hooks.h:63
void writeEvent()
Write out the current GenEvent to the internal stream.
Definition: HepMC2.h:451
void set_print_inconsistency(bool b=true)
Set values for some switches.
Definition: HepMC3.h:258
void hepmcSettings(Settings *settingsPtr)
Register HepMC settings.
Definition: HepMC3Hooks.h:134
Status
Enumerate the different status codes the event generation can have.
Definition: PhysicsBase.h:31
Definition: HepMC2.h:409
void onStat() override
Finalise.
Definition: HepMC3Hooks.h:113
bool makeDir(vector< string > path, mode_t mode=0777)
Definition: HepMC3Hooks.h:29
UserHook to write HepMC3 files.
Definition: HepMC3Hooks.h:58
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
The Pythia class contains the top-level routines to generate an event.
Definition: Pythia.h:72
bool fillNextEvent(Pythia &pythia)
Definition: HepMC2.h:445
UserHooks is base class for user access to program execution.
Definition: UserHooks.h:32
bool flag(string key) const
Shorthand to read settings values.
Definition: PhysicsBase.h:44
void onEndEvent(Status) override
Print event to HepMC file.
Definition: HepMC3Hooks.h:71
Definition: Settings.h:196
void addFlag(string keyIn, bool defaultIn)
Add new entry.
Definition: Settings.h:280