Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for dpl-eventgen usage, added nEvents protection and executable selection #1895

Merged
merged 1 commit into from
Feb 4, 2025
Merged
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
51 changes: 40 additions & 11 deletions MC/config/PWGGAJE/external/generator/generator_pythia8_powheg.C
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<int>::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
Expand All @@ -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;
}
Expand All @@ -61,35 +79,46 @@ public:
dst << "iseed " << seed << std::endl;
}
if (!isnumevts) {
dst << "numevts " << mSimConfig.getNEvents() << std::endl;
dst << "numevts " << nPowhegEvents << std::endl;
}
src.close();
dst.close();
} else {
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<std::string> mPowhegGen = {"pwhg_main_hvq", "pwhg_main_W", "pwhg_main_Z", "pwhg_main_dijet", "pwhg_main_directphoton"}; // POWHEG executables
};

} // namespace eventgen
} // namespace o2

/** 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";
}
Expand Down