StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PowhegProcs.h
1 // PowhegProcs.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2018 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 // Author: Philip Ilten, May 2015.
6 
7 #ifndef Pythia8_PowhegProcs_H
8 #define Pythia8_PowhegProcs_H
9 
10 #include "Pythia8/Pythia.h"
11 #include "Pythia8Plugins/PowhegHooks.h"
12 
13 namespace Pythia8 {
14 
15 //==========================================================================
16 
17 // A class to generate events with hard processes from POWHEGBOX
18 // matrix elements. See http://powhegbox.mib.infn.it/ for further
19 // details on POWHEGBOX.
20 
21 // WARNING: If one wishes to use LHAPDF with both POWHEGBOX and
22 // Pythia, only LHAPDF 6 configured with the option
23 // "--with-lhapdf6-plugin=LHAPDF6.h" for Pythia should be used. If
24 // not, and differing PDF sets are used between POWHEGBOX and Pythia,
25 // POWHEGBOX will not re-initialize the PDF set and consequently will
26 // use the PDF set last used by Pythia.
27 
28 class PowhegProcs {
29 
30 public:
31 
32  // Constructor.
33  PowhegProcs(Pythia *pythiaPtrIn, string procIn, string dirIn = "powhegrun",
34  string pdfIn = "", bool random = true);
35 
36  // Destructor.
37  ~PowhegProcs();
38 
39  // Read a POWHEG settings string.
40  bool readString(string line);
41 
42  // Read a POWHEG settings file.
43  bool readFile(string name);
44 
45  // Write out the input for POWHEG.
46  bool init();
47 
48  // The POWHEG LHAup pointer.
49  LHAup *lhaup;
50 
51 private:
52 
53  // Typedefs of the hooks used to access the plugin.
54  typedef LHAup* NewLHAupPowheg(Pythia*);
55  typedef void DeleteLHAupPowheg(LHAup*);
56 
57  // The POWHEG process name, run directory, and PDF file (if not LHAPDF).
58  string proc, dir, pdf;
59 
60  // The map of POWHEG settings.
61  map<string, string> settings;
62 
63  // The associated PYTHIA pointer.
64  Pythia *pythia;
65 
66  // The POWHEG plugin library.
67  void *lib;
68 
69  // The POWHEG hooks.
70  PowhegHooks hooks;
71 
72 };
73 
74 //--------------------------------------------------------------------------
75 
76 // Constructor.
77 
78 // pythiaPtrIn: The PYTHIA object the plugin will use for settings and
79 // random numbers.
80 
81 // procIn: the process name. An attempt is made to load the plugin
82 // library libpythia8powheg<procIn>.so.
83 
84 // dirIn: The directory where the POWHEG matrix element will be
85 // run. This is needed if two instances are to be run concurrently
86 // since the matrix element generates a large number of files.
87 
88 // pdfIn: The full path and name of the PDF file to use, if not using
89 // LHAPDF. This file is copied to the run directory via the init()
90 // method.
91 
92 // random: Flag to use the Pythia random number generator with
93 // POWHEGBOX. If true, the POWHEGBOX random number block is
94 // initialized for each event from the Pythia random number
95 // generator. If false, the default POWHEGBOX random number generation
96 // is performed. Note that the initialization is always performed
97 // using the POWHEGBOX random number generation (which can be modified
98 // via the POWHEGBOX configuration).
99 
100 PowhegProcs::PowhegProcs(Pythia *pythiaPtrIn, string procIn, string dirIn,
101  string pdfIn, bool random) : lhaup(0), proc(procIn), dir(dirIn), pdf(pdfIn),
102  pythia(pythiaPtrIn), lib(0) {
103 
104  if (!pythia) return;
105  NewLHAupPowheg *sym(0);
106  const char* error(0);
107 
108  // Load the library.
109  lib = dlopen(("libpythia8powheg" + proc + ".so").c_str(), RTLD_LAZY);
110  error = dlerror();
111  if (error) {
112  pythia->info.errorMsg("Error from PowhegProcs::PowhegProcs: "
113  + string(error));
114  return;
115  }
116  dlerror();
117 
118  // Load the LHAup pointer.
119  sym = (NewLHAupPowheg*)dlsym(lib, "newLHAupPowheg");
120  error = dlerror();
121  if (error) {
122  pythia->info.errorMsg("Error from PowhegProcs::PowhegProcs: "
123  + string(error));
124  }
125  dlerror();
126  pythia->settings.addWord("POWHEG:dir", dir);
127  pythia->settings.addFlag("POWHEG:pythiaRandom", random);
128  if (sym) lhaup = sym(pythia);
129 
130  // Configure PYTHIA.
131  pythia->setLHAupPtr(lhaup);
132  pythia->setUserHooksPtr(&hooks);
133 
134 }
135 
136 //--------------------------------------------------------------------------
137 
138 // Destructor.
139 
140 PowhegProcs::~PowhegProcs() {
141 
142  // Delete the LHAup pointer.
143  if (lhaup && lib) {
144  DeleteLHAupPowheg *sym(0);
145  sym = (DeleteLHAupPowheg*)dlsym(lib, "deleteLHAupPowheg");
146  if (sym) sym(lhaup);
147  }
148 
149  // Unload the library.
150  if (lib) {dlclose(lib); dlerror();}
151 
152 }
153 
154 //--------------------------------------------------------------------------
155 
156 // Read a POWHEG settings string. If a setting is repeated a warning
157 // is printed but the most recent setting is used.
158 
159 bool PowhegProcs::readString(string line) {
160 
161  // Copy string without initial and trailing blanks.
162  if (!pythia) return false;
163  if (line.find_first_not_of(" \n\t\v\b\r\f\a") == string::npos) return true;
164  int firstChar = line.find_first_not_of(" \n\t\v\b\r\f\a");
165  int lastChar = line.find_last_not_of(" \n\t\v\b\r\f\a");
166  line = line.substr(firstChar, lastChar + 1 - firstChar);
167 
168  // Find the key.
169  firstChar = line.find_first_of(" \t\f\v\n\r");
170  string key = toLower( line.substr(0, firstChar), false);
171 
172  // Add the setting.
173  if (key.size() > 0
174  && key.find_first_of("abcdedfghijklmnopqrtsuvwxyz") == 0) {
175  map<string, string>::iterator setting = settings.find(key);
176  if (setting != settings.end()) {
177  pythia->info.errorMsg("Warning from PowhegProcs::readString: replacing "
178  "previous POWHEG setting for " + key + ".");
179  setting->second = line;
180  } else settings[key] = line;
181  }
182  return true;
183 
184 }
185 
186 //--------------------------------------------------------------------------
187 
188 // Read a POWHEG settings file.
189 
190 bool PowhegProcs::readFile(string name) {
191 
192  fstream config(name.c_str(), ios::in); string line;
193  while (getline(config, line, '\n')) readString(line);
194  config.close();
195  return true;
196 
197 }
198 
199 //--------------------------------------------------------------------------
200 
201 // Write the input for POWHEG.
202 
203 bool PowhegProcs::init() {
204 
205  // Copy over the PDF file if needed.
206  if (pdf != "") {
207  fstream pdfin(pdf.c_str(), ios::in | ios::binary);
208  fstream pdfout((dir + "/" + pdf.substr(0, pdf.find_last_of("/"))).c_str(),
209  ios::out | ios::binary);
210  pdfout << pdfin.rdbuf();
211  pdfin.close();
212  pdfout.close();
213  }
214 
215  // Copy the settings to the configuration file.
216  fstream config((dir + "/" + "powheg.input").c_str(), ios::out);
217  for (map<string, string>::iterator setting = settings.begin();
218  setting != settings.end(); ++setting) config << setting->second << "\n";
219  config.close();
220  return true;
221 
222 }
223 
224 //==========================================================================
225 
226 } // end namespace Pythia8
227 
228 #endif // Pythia8_PowhegProcs_H