From 241ee99bf5d49cd125cfa712609d42fe575bc035 Mon Sep 17 00:00:00 2001 From: Marco Giacalone Date: Sun, 2 Feb 2025 19:51:37 +0100 Subject: [PATCH] Fix for dpl-eventgen usage, added nEvents protection and executable selection --- .../generator/generator_pythia8_powheg.C | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/MC/config/PWGGAJE/external/generator/generator_pythia8_powheg.C b/MC/config/PWGGAJE/external/generator/generator_pythia8_powheg.C index 989f33971..6ca884dda 100644 --- a/MC/config/PWGGAJE/external/generator/generator_pythia8_powheg.C +++ b/MC/config/PWGGAJE/external/generator/generator_pythia8_powheg.C @@ -1,6 +1,6 @@ R__ADD_INCLUDE_PATH($O2DPG_MC_CONFIG_ROOT) ///#include "FairGenerator.h" -//#include "Generators/GeneratorPythia8.h" +#include "Generators/GeneratorPythia8.h" #include "Pythia8/Pythia.h" #include "Generators/GeneratorPythia8Param.h" #include "CommonUtils/FileSystemUtils.h" @@ -24,13 +24,31 @@ using namespace Pythia8; // Pythia8 generator using POWHEG data that are generated during the initialization // of the external generator. The POWHEG configuration file is copied to the current -// directory with the right name and the POWHEG events are generated using the pwhg_main_hvq executable. +// directory with the right name and the POWHEG events are generated using the executable +// specified via the type parameter, namely: +// 0: pwhg_main_hvq +// 1: pwhg_main_W +// 2: pwhg_main_Z +// 3: pwhg_main_dijet +// 4: pwhg_main_directphoton class GeneratorJEPythia8POWHEG : public o2::eventgen::GeneratorPythia8 { public: /// default constructor - GeneratorJEPythia8POWHEG(std::string confpath = "pwgpath") + GeneratorJEPythia8POWHEG(std::string confpath = "pwgpath", short int type = 0) { + // Assign events to generate with POWHEG + unsigned int nPowhegEvents = getTotalNEvents(); + if (nPowhegEvents == 0) { + LOG(fatal) << "Number of events not set or set to 0."; + exit(1); + } + // Check on nEvents to generate with POWHEG + // due to integer limit hardcoded in the generator + if (nPowhegEvents > std::numeric_limits::max()) { + LOG(fatal) << "Number of events for POWHEG exceeds the maximum allowed value"; + exit(1); + } // Check if file exist and is not empty if (std::filesystem::exists(confpath) && std::filesystem::file_size(confpath) > 0) { // Copy the file to the current directory @@ -51,7 +69,7 @@ public: if (line.find("numevts") != std::string::npos) { // Set the number of events to the number of events defined in the configuration - line = "numevts " + std::to_string(mSimConfig.getNEvents()); + line = "numevts " + std::to_string(nPowhegEvents); // replace it in the file isnumevts = true; } @@ -61,7 +79,7 @@ public: dst << "iseed " << seed << std::endl; } if (!isnumevts) { - dst << "numevts " << mSimConfig.getNEvents() << std::endl; + dst << "numevts " << nPowhegEvents << std::endl; } src.close(); dst.close(); @@ -69,13 +87,24 @@ public: LOG(fatal) << "POWHEG configuration file not found or empty" << std::endl; exit(1); } - // Generate the POWHEG events - std::string cmd = "pwhg_main_hvq"; - system(cmd.c_str()); + // Get POWHEG executable to use + if (type >= mPowhegGen.size()) { + LOG(warn) << "Available POWHEG generators are:"; + for (int k = 0; k < mPowhegGen.size(); k++) + { + LOG(warn) << "\t" << k << ": " << mPowhegGen[k]; + } + LOG(fatal) << "POWHEG generator type " << type << " not found"; + exit(1); + } else { + LOG(info) << "Running POWHEG using the " << mPowhegGen[type] << " executable"; + // Generate the POWHEG events + system(mPowhegGen[type].c_str()); + } }; private: - o2::conf::SimConfig mSimConfig = o2::conf::SimConfig::Instance(); // local sim config object + const std::vector mPowhegGen = {"pwhg_main_hvq", "pwhg_main_W", "pwhg_main_Z", "pwhg_main_dijet", "pwhg_main_directphoton"}; // POWHEG executables }; } // namespace eventgen @@ -83,13 +112,13 @@ private: /** generator instance and settings **/ -FairGenerator *getGeneratorJEPythia8POWHEG(std::string powhegconf = "pwgpath", std::string pythia8conf = "") +FairGenerator *getGeneratorJEPythia8POWHEG(std::string powhegconf = "pwgpath", std::string pythia8conf = "", short int type = 0) { using namespace o2::eventgen; // Expand paths for the POWHEG configuration file powhegconf = o2::utils::expandShellVarsInFileName(powhegconf); LOG(info) << "Using POWHEG configuration file: " << powhegconf; - auto myGen = new GeneratorJEPythia8POWHEG(powhegconf); + auto myGen = new GeneratorJEPythia8POWHEG(powhegconf, type); if(GeneratorPythia8Param::Instance().config.empty() && pythia8conf.empty()) { LOG(fatal) << "No configuration provided for Pythia8"; }