Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-18 08:20:48

0001 // HepMC3Hooks.h is a part of the PYTHIA event generator.
0002 // Copyright (C) 2025 Philip Ilten, Torbjorn Sjostrand.
0003 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
0004 // Please respect the MCnet Guidelines, see GUIDELINES for details.
0005 
0006 // Author: Christian T. Preuss.
0007 
0008 // This class implements an interface to HepMC3 that can be loaded
0009 // via the plugin structure. It can be run with PythiaParallel.
0010 
0011 #ifndef Pythia8_HepMC3Hooks_H
0012 #define Pythia8_HepMC3Hooks_H
0013 
0014 // Pythia includes.
0015 #include "Pythia8/Pythia.h"
0016 #include "Pythia8/Plugins.h"
0017 #include "Pythia8Plugins/HepMC3.h"
0018 
0019 // Directory creation for POSIX.
0020 #include <sys/stat.h>
0021 
0022 namespace Pythia8 {
0023 
0024 //==========================================================================
0025 
0026 // Create a directory, with nesting if necessary. This is not compatible
0027 // outside POSIX systems, and should be removed after migration to C++17.
0028 
0029 bool makeDir(vector<string> path, mode_t mode = 0777) {
0030 
0031   // Create the structure needed for stat.
0032   struct stat info;
0033 
0034   // Loop over the directories.
0035   string pathNow = "";
0036   bool first = true;
0037   for (string& dir : path) {
0038 
0039     // Check if the directory exists.
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     // Create the directory and check it exists.
0048     if (mkdir(pathNow.c_str(), mode) != 0) return false;
0049   }
0050   return true;
0051 
0052 }
0053 
0054 //==========================================================================
0055 
0056 // UserHook to write HepMC3 files.
0057 
0058 class HepMC3Hooks : public UserHooks {
0059 
0060 public:
0061 
0062   // Constructors and destructor.
0063   HepMC3Hooks() {}
0064   HepMC3Hooks(Pythia* pythiaPtrIn, Settings*, Logger*) :
0065     pythiaPtr(pythiaPtrIn) {}
0066   ~HepMC3Hooks() {if (hepMCPtr != nullptr) delete hepMCPtr;}
0067 
0068   //--------------------------------------------------------------------------
0069 
0070   // Print event to HepMC file.
0071   void onEndEvent(Status) override {
0072 
0073     // Create the HepMC converter.
0074     if (hepMCPtr == nullptr) {
0075 
0076       // Set the filename if running in parallel.
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       // Create a HepMC converter and directory structure if needed.
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       // Save some settings.
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     // Convert the event to HepMC.
0103     hepMCPtr->fillNextEvent(*pythiaPtr);
0104 
0105     // Write event. Currently each thread writes into its own file.
0106     hepMCPtr->writeEvent();
0107 
0108   }
0109 
0110   //--------------------------------------------------------------------------
0111 
0112   // Finalise.
0113   void onStat() override {}
0114 
0115   //--------------------------------------------------------------------------
0116 
0117   // Finalise. Currently, nothing is done here, but merging of the HepMC3
0118   // records could be done here if this is a useful feature.
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 // Register HepMC settings.
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 // Declare the plugin.
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 } // end namespace Pythia8
0154 
0155 #endif // end Pythia8_HepMC3Hooks_H