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) 2020 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 *pythia, string procIn, string dirIn = "powhegrun",
34  string pdfIn = "", bool random = true);
35 
36  // Read a POWHEG settings string.
37  bool readString(string line);
38 
39  // Read a POWHEG settings file.
40  bool readFile(string name);
41 
42  // Write out the input for POWHEG.
43  bool init();
44 
45 private:
46 
47  // The POWHEG process name, run directory, and PDF file (if not LHAPDF).
48  string proc, dir, pdf;
49 
50  // The map of POWHEG settings.
51  map<string, string> settings;
52 
53 };
54 
55 //--------------------------------------------------------------------------
56 
57 // Constructor.
58 
59 // pythia: The PYTHIA object the plugin will use for settings and
60 // random numbers.
61 
62 // procIn: the process name. An attempt is made to load the plugin
63 // library libpythia8powheg<procIn>.so.
64 
65 // dirIn: The directory where the POWHEG matrix element will be
66 // run. This is needed if two instances are to be run concurrently
67 // since the matrix element generates a large number of files.
68 
69 // pdfIn: The full path and name of the PDF file to use, if not using
70 // LHAPDF. This file is copied to the run directory via the init()
71 // method.
72 
73 // random: Flag to use the Pythia random number generator with
74 // POWHEGBOX. If true, the POWHEGBOX random number block is
75 // initialized for each event from the Pythia random number
76 // generator. If false, the default POWHEGBOX random number generation
77 // is performed. Note that the initialization is always performed
78 // using the POWHEGBOX random number generation (which can be modified
79 // via the POWHEGBOX configuration).
80 
81 PowhegProcs::PowhegProcs(Pythia *pythia, string procIn, string dirIn,
82  string pdfIn, bool random) : proc(procIn), dir(dirIn), pdf(pdfIn) {
83 
84  // Load the LHAup pointer.
85  pythia->settings.addWord("POWHEG:dir", dir);
86  pythia->settings.addFlag("POWHEG:pythiaRandom", random);
87  pythia->setLHAupPtr(make_shared<LHAupPlugin>
88  ("libpythia8powheg" + proc + ".so", pythia));
89  pythia->setUserHooksPtr(make_shared<PowhegHooks>());
90 
91 }
92 
93 //--------------------------------------------------------------------------
94 
95 // Read a POWHEG settings string. If a setting is repeated a warning
96 // is printed but the most recent setting is used.
97 
98 bool PowhegProcs::readString(string line) {
99 
100  // Copy string without initial and trailing blanks.
101  if (line.find_first_not_of(" \n\t\v\b\r\f\a") == string::npos) return true;
102  int firstChar = line.find_first_not_of(" \n\t\v\b\r\f\a");
103  int lastChar = line.find_last_not_of(" \n\t\v\b\r\f\a");
104  line = line.substr(firstChar, lastChar + 1 - firstChar);
105 
106  // Find the key.
107  firstChar = line.find_first_of(" \t\f\v\n\r");
108  string key = toLower( line.substr(0, firstChar), false);
109 
110  // Add the setting.
111  if (key.size() > 0
112  && key.find_first_of("abcdedfghijklmnopqrtsuvwxyz") == 0) {
113  map<string, string>::iterator setting = settings.find(key);
114  if (setting != settings.end()) {
115  cout << "Warning from PowhegProcs::readString: replacing "
116  << "previous POWHEG setting for " << key << "." << endl;
117  setting->second = line;
118  } else settings[key] = line;
119  }
120  return true;
121 
122 }
123 
124 //--------------------------------------------------------------------------
125 
126 // Read a POWHEG settings file.
127 
128 bool PowhegProcs::readFile(string name) {
129 
130  fstream config(name.c_str(), ios::in); string line;
131  while (getline(config, line, '\n')) readString(line);
132  config.close();
133  return true;
134 
135 }
136 
137 //--------------------------------------------------------------------------
138 
139 // Write the input for POWHEG.
140 
141 bool PowhegProcs::init() {
142 
143  // Copy over the PDF file if needed.
144  if (pdf != "") {
145  fstream pdfin(pdf.c_str(), ios::in | ios::binary);
146  fstream pdfout((dir + "/" + pdf.substr(0, pdf.find_last_of("/"))).c_str(),
147  ios::out | ios::binary);
148  pdfout << pdfin.rdbuf();
149  pdfin.close();
150  pdfout.close();
151  }
152 
153  // Copy the settings to the configuration file.
154  fstream config((dir + "/" + "powheg.input").c_str(), ios::out);
155  for (map<string, string>::iterator setting = settings.begin();
156  setting != settings.end(); ++setting) config << setting->second << "\n";
157  config.close();
158  return true;
159 
160 }
161 
162 //==========================================================================
163 
164 } // end namespace Pythia8
165 
166 #endif // Pythia8_PowhegProcs_H