Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:39

0001 // LHAPowheg.h is a part of the PYTHIA event generator.
0002 // Copyright (C) 2024 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 // Author: Philip Ilten, May 2015.
0006 
0007 #ifndef Pythia8_LHAPowheg_H
0008 #define Pythia8_LHAPowheg_H
0009 
0010 #include "Pythia8/Plugins.h"
0011 #include "Pythia8Plugins/LHAFortran.h"
0012 #include <sys/stat.h>
0013 #include <unistd.h>
0014 
0015 namespace Pythia8 {
0016 
0017 //==========================================================================
0018 
0019 // Give access to the POWHEG commonblocks and subroutines.
0020 
0021 extern "C" {
0022 
0023   // The random number common block.
0024   extern struct {
0025     int rnd_numseeds, rnd_initialseed, rnd_iwhichseed;
0026     char rnd_cwhichseed[4];
0027     int rnd_i1, rnd_i2;
0028   } pwhg_rnd_;
0029 
0030   // The RANMAR (R48 modification) common block.
0031   extern struct {
0032     double u[97];
0033     double c;
0034     int i97, j97;
0035   } r48st1_;
0036 
0037   // Initialize Powheg.
0038   void pwhginit_();
0039 
0040   // Reset the counters.
0041   void resetcnt_(const char *string, int length);
0042 
0043   // Generate an event.
0044   void pwhgevent_();
0045 
0046   // Access Powheg input data.
0047   double powheginput_(const char *string, int length);
0048 
0049 }
0050 
0051 //==========================================================================
0052 
0053 // A plugin class to generate events with hard processes from
0054 // POWHEGBOX matrix elements. See http://powhegbox.mib.infn.it/ for
0055 // further details on POWHEGBOX.
0056 
0057 // WARNING: If one wishes to use LHAPDF with both POWHEGBOX and
0058 // Pythia, only LHAPDF 6 should be used. If not, and differing PDF
0059 // sets are used between POWHEGBOX and Pythia, POWHEGBOX will not
0060 // re-initialize the PDF set and consequently will use the PDF set
0061 // last used by Pythia.
0062 
0063 class LHAupPowheg : public LHAupFortran {
0064 
0065 public:
0066 
0067   // Constructor.
0068   LHAupPowheg(Pythia* pythiaPtr, Settings* settingsPtr, Logger* loggerPtr);
0069 
0070   // Call pwhginit and fill the HEPRUP commonblock.
0071   bool fillHepRup();
0072 
0073   // Call pwhgevent and fill the HEEUP commonblock.
0074   bool fillHepEup();
0075 
0076 private:
0077 
0078   // Read a POWHEG settings string.
0079   bool readString(string line);
0080 
0081   // Read a POWHEG settings file.
0082   bool readFile(string name);
0083 
0084   // Write out the input for POWHEG.
0085   bool init();
0086 
0087   // The external random number generator.
0088   Rndm* rndmPtr{};
0089 
0090   // Logger.
0091   Logger* loggerPtr{};
0092 
0093   // The POWHEG run directory and PDF file (if not LHAPDF).
0094   string dir, pdf;
0095 
0096   // The map of POWHEG settings.
0097   map<string, string> settings;
0098 
0099   // The current working directory.
0100   char cwd[FILENAME_MAX];
0101 
0102 };
0103 
0104 //--------------------------------------------------------------------------
0105 
0106 // Constructor.
0107 
0108 LHAupPowheg::LHAupPowheg(Pythia *pythiaPtr, Settings *settingsPtr,
0109   Logger* loggerPtrIn) : loggerPtr(loggerPtrIn), dir("powhegrun"), pdf("") {
0110 
0111   // Load the settings.
0112   if (settingsPtr != nullptr) {
0113     dir = settingsPtr->word("POWHEG:dir");
0114     pdf = settingsPtr->word("POWHEG:pdf");
0115 
0116     // Read the command files and strings.
0117     for (string cmndFile : settingsPtr->wvec("POWHEG:cmndFiles"))
0118       readFile(cmndFile);
0119     for (string cmnd : settingsPtr->wvec("POWHEG:cmnds"))
0120       readString(cmnd);
0121   }
0122 
0123   // Set up the random number generator.
0124   if (pythiaPtr != nullptr) {
0125     if (settingsPtr->isFlag("POWHEG:pythiaRandom")) rndmPtr = &pythiaPtr->rndm;
0126   }
0127   mkdir(dir.c_str(), 0777);
0128   init();
0129 
0130 }
0131 
0132 //--------------------------------------------------------------------------
0133 
0134 // Call pwhginit and fill the HEPRUP commonblock.
0135 
0136 bool LHAupPowheg::fillHepRup() {
0137 
0138   // Set multiple random seeds to none.
0139   getcwd(cwd, sizeof(cwd));
0140   chdir(dir.c_str());
0141   strcpy(pwhg_rnd_.rnd_cwhichseed, "none");
0142 
0143   // Initialize Powheg.
0144   pwhginit_();
0145 
0146   // Reset all the counters.
0147   resetcnt_("upper bound failure in inclusive cross section", 46);
0148   resetcnt_("vetoed calls in inclusive cross section", 39);
0149   resetcnt_("upper bound failures in generation of radiation", 47);
0150   resetcnt_("vetoed radiation", 16);
0151   chdir(cwd);
0152   return fillHepEup();
0153 
0154 }
0155 
0156 //--------------------------------------------------------------------------
0157 
0158 // Set the random numbers, call pwhgevent, and fill the HEPEUP commonblock.
0159 
0160 bool LHAupPowheg::fillHepEup() {
0161 
0162   // Change directory.
0163   getcwd(cwd, sizeof(cwd));
0164   chdir(dir.c_str());
0165 
0166   // Reset the random block if requested.
0167   if (rndmPtr != nullptr) {
0168     r48st1_.i97 = 97;
0169     r48st1_.j97 = 33;
0170     r48st1_.c = rndmPtr->flat();
0171     for (int i = 0; i < 97; ++i) r48st1_.u[i] = rndmPtr->flat();
0172   }
0173 
0174   // Generate the event.
0175   pwhgevent_();
0176   chdir(cwd);
0177   return true;
0178 
0179 }
0180 
0181 //--------------------------------------------------------------------------
0182 
0183 // Read a POWHEG settings string. If a setting is repeated a warning
0184 // is printed but the most recent setting is used.
0185 
0186 bool LHAupPowheg::readString(string line) {
0187 
0188   // Copy string without initial and trailing blanks.
0189   if (line.find_first_not_of(" \n\t\v\b\r\f\a") == string::npos) return true;
0190   int firstChar = line.find_first_not_of(" \n\t\v\b\r\f\a");
0191   int lastChar  = line.find_last_not_of(" \n\t\v\b\r\f\a");
0192   line = line.substr(firstChar, lastChar + 1 - firstChar);
0193 
0194   // Find the key.
0195   firstChar = line.find_first_of("  \t\f\v\n\r");
0196   string key = toLower( line.substr(0, firstChar), false);
0197 
0198   // Add the setting.
0199   if (key.size() > 0
0200     && key.find_first_of("abcdedfghijklmnopqrtsuvwxyz") == 0) {
0201     map<string, string>::iterator setting = settings.find(key);
0202     if (setting != settings.end()) {
0203       if (loggerPtr) loggerPtr->WARNING_MSG(
0204         "replacing previous POWHEG setting for " + key);
0205       setting->second = line;
0206     } else settings[key] = line;
0207   }
0208   return true;
0209 
0210 }
0211 
0212 //--------------------------------------------------------------------------
0213 
0214 // Read a POWHEG settings file.
0215 
0216 bool LHAupPowheg::readFile(string name) {
0217 
0218   fstream config(name.c_str(), ios::in); string line;
0219   while (getline(config, line, '\n')) readString(line);
0220   config.close();
0221   return true;
0222 
0223 }
0224 
0225 //--------------------------------------------------------------------------
0226 
0227 // Write the input for POWHEG.
0228 
0229 bool LHAupPowheg::init() {
0230 
0231   // Copy over the PDF file if needed.
0232   if (pdf != "") {
0233     fstream pdfin(pdf.c_str(), ios::in | ios::binary);
0234     fstream pdfout((dir + "/" + pdf.substr(0, pdf.find_last_of("/"))).c_str(),
0235       ios::out | ios::binary);
0236     pdfout << pdfin.rdbuf();
0237     pdfin.close();
0238     pdfout.close();
0239   }
0240 
0241   // Copy the settings to the configuration file.
0242   fstream config((dir + "/" + "powheg.input").c_str(), ios::out);
0243   for (map<string, string>::iterator setting = settings.begin();
0244     setting != settings.end(); ++setting) config << setting->second << "\n";
0245   config.close();
0246   return true;
0247 
0248 }
0249 
0250 //--------------------------------------------------------------------------
0251 
0252 // Register POWHEG settings.
0253 
0254 void powhegSettings(Settings *settingsPtr) {
0255   settingsPtr->addWord("POWHEG:dir", "powhegrun");
0256   settingsPtr->addWord("POWHEG:pdf", "");
0257   settingsPtr->addWVec("POWHEG:cmnds", {});
0258   settingsPtr->addWVec("POWHEG:cmndFiles", {});
0259   settingsPtr->addFlag("POWHEG:pythiaRandom", true);
0260 }
0261 
0262 //--------------------------------------------------------------------------
0263 
0264 // Declare the plugin.
0265 
0266 PYTHIA8_PLUGIN_CLASS(LHAup, LHAupPowheg, true, true, true)
0267 PYTHIA8_PLUGIN_SETTINGS(powhegSettings)
0268 PYTHIA8_PLUGIN_VERSIONS(PYTHIA_VERSION_INTEGER)
0269 
0270 //==========================================================================
0271 
0272 } // end namespace Pythia8
0273 
0274 #endif // Pythia8_LHAPowheg_H