PYTHIA  8.312
LHAMadgraph.h
1 // LHAMadgraph.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2024 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 
6 // Author: Philip Ilten, December 2015.
7 
8 #ifndef Pythia8_LHAMadgraph_H
9 #define Pythia8_LHAMadgraph_H
10 
11 #include "Pythia8/Pythia.h"
12 #include "Pythia8Plugins/JetMatching.h"
13 #include "Pythia8Plugins/GeneratorInput.h"
14 #include <unistd.h>
15 #include <sys/stat.h>
16 
17 namespace Pythia8 {
18 
19 //==========================================================================
20 
21 // A derived class from LHAup which generates events with MadGraph 5.
22 
23 // This class automatically generates hard processes with MadGraph 5
24 // and aMC@NLO, reads in the LHEF file output, and performs matching
25 // (if requested). For tree-level generation MLM matching is performed
26 // while FxFx is used for aMC@NLO generation.
27 
28 // The user can send commands to MadGraph via the readString
29 // method. Any string begining with "configure " is used for the initial
30 // MadGraph configuration with "configure " stripped from the
31 // begining. In general, only the process and run settings need to be
32 // provided. Run settings must begin with " set" (note the leading
33 // space). The output and launch commands, random seed, and shower
34 // choice are automatically handled. For example, the following will
35 // produce di-muon events from 13 TeV proton proton collisions at NLO
36 // in QCD:
37 
38 // readString("generate p p > mu+ mu- [QCD]")
39 
40 // The number of events generated per MadGraph run is controlled by
41 // setEvents, while the random seed is controlled by setSeed. The
42 // maximum number of jets produced by MadGraph (needed for matching)
43 // is automatically determined but can be manually specified with
44 // setJets. In general these methods should not be needed; for further
45 // details see the method documentation.
46 
47 // Events are generated with MadGraph utilizing the "gridpack" method
48 // for MadGraph 5:
49 //
50 // https://cp3.irmp.ucl.ac.be/projects/madgraph/wiki/GridDevelopment
51 //
52 // and an eqivalent method for aMC@NLO:
53 //
54 // https://answers.launchpad.net/mg5amcnlo/+question/243268
55 //
56 // Consequently the run directory ("madgraphrun" by default) does not
57 // need to be deleted between independent runs with the same
58 // configuration (excluding random seeds). Indeed, keeping the
59 // directory significantly speeds the generation process, particularly
60 // for NLO generation with aMC@NLO as the grid initialization
61 // can be skipped after the initial run.
62 
63 class LHAupMadgraph : public LHAup {
64 
65 public:
66 
67  // Types of MadGraph stages.
68  enum Stage{Auto, Configure, Generate, Launch};
69 
70  // Constructor.
71  LHAupMadgraph(Pythia* pythiaIn, bool matchIn = true,
72  string dirIn = "madgraphrun", string exeIn = "mg5_aMC");
73 
74  // Destructor: Print error statistics before exiting. Printing code
75  // basically copied from Info class.
77 
78  // Read a MadGraph command string.
79  bool readString(string line, Stage stage = Auto);
80 
81  // Add a MadGraph configuration card to be used.
82  void addCard(string src, string dst);
83 
84  // Set the number of events to generate per run.
85  void setEvents(int eventsIn);
86 
87  // Set the random seed and maximum runs.
88  bool setSeed(int seedIn, int runsIn = 30081);
89 
90  // Set the maximum number of jets produced by MadGraph.
91  void setJets(int jetsIn);
92 
93  // Set the initialization information.
94  bool setInit();
95 
96  // Set the event information.
97  bool setEvent(int = 0);
98 
99  // Note: The functions below have been made public to ease the generation
100  // of Python bindings.
101  //protected:
102 
103  // Execute a system command.
104  bool execute(string line);
105 
106  // Write the MadGraph configuration.
107  bool configure();
108 
109  // Run the generate stage of MadGraph.
110  bool generate();
111 
112  // Run the launch stage of MadGraph.
113  bool launch();
114 
115  // Run MadGraph.
116  bool run(int eventsIn, int seedIn = -1);
117 
118  // Create the LHEF reader.
119  bool reader(bool init);
120 
121  // Print a message the first few times. Insert in database.
122  void errorMsg(string messageIn) {
123  // Recover number of times message occured. Also inserts new string.
124  int times = messages[messageIn];
125  ++messages[messageIn];
126  // Print message the first few times.
127  if (times < TIMESTOPRINT) cout << " PYTHIA " << messageIn << endl;
128  }
129 
130 private:
131 
132  // The PYTHIA object and LHEF file reader and matching hook.
133  Pythia *pythia;
134  LHAupLHEF *lhef;
135  shared_ptr<JetMatchingMadgraph> hook;
136 
137  // Stored members.
138  int events, seed, runs, nRuns, jets;
139  bool match, amcatnlo;
140  string dir, exe, lhegz;
141  vector< pair<string, string> > cards;
142 
143  // The MadGraph commands for the config, generate, and launch stages.
144  vector<string> configureLines, generateLines, launchLines;
145 
146  // Vector of whether a command stage has been overridden by the user.
147  vector<bool> override;
148 
149  // Map for all error messages.
150  map<string, int> messages;
151  // Number of times the same error message is repeated, unless overridden.
152  static const int TIMESTOPRINT = 1;
153 
154 };
155 
156 //--------------------------------------------------------------------------
157 
158 // Constructor.
159 
160 LHAupMadgraph::LHAupMadgraph(Pythia *pythiaIn, bool matchIn, string dirIn,
161  string exeIn) :
162  pythia(pythiaIn), lhef(0), hook(0), events(10000), seed(-1), runs(30081),
163  nRuns(0), jets(-1), match(matchIn), dir(dirIn), exe(exeIn),
164  lhegz(dirIn + "/events.lhe.gz"), override(vector<bool>(3, false)) {
165  mkdir(dir.c_str(), 0777);
166  if (pythia) pythia->readString("Beams:frameType = 5");
167 }
168 
169 //--------------------------------------------------------------------------
170 
171 // Destructor: Print error statistics before exiting. Printing code
172 // basically copied from Info class.
174 
175  if (lhef) delete lhef;
176 
177  // Header.
178  cout << "\n *------- LHAupMadgraph Error and Warning Messages Statistics"
179  << " ---------------------------------------------------* \n"
180  << " | "
181  << " | \n"
182  << " | times message "
183  << " | \n"
184  << " | "
185  << " | \n";
186 
187  // Loop over all messages
188  map<string, int>::iterator messageEntry = messages.begin();
189  if (messageEntry == messages.end())
190  cout << " | 0 no errors or warnings to report "
191  << " | \n";
192  while (messageEntry != messages.end()) {
193  // Message printout.
194  string temp = messageEntry->first;
195  int len = temp.length();
196  temp.insert( len, max(0, 102 - len), ' ');
197  cout << " | " << setw(6) << messageEntry->second << " "
198  << temp << " | \n";
199  ++messageEntry;
200  }
201 
202  // Done.
203  cout << " | "
204  << " | \n"
205  << " *------- End LHAupMadgraph Error and Warning Messages "
206  << "Statistics -----------------------------------------------* "
207  << endl;
208 }
209 
210 //--------------------------------------------------------------------------
211 
212 // Read a MadGraph command string.
213 
214 // If the stage is set to Auto, commands beginning with " set" are
215 // used in the launch stage (these must begin with a single space to
216 // differentiate from generate stage set commands), commands begining
217 // with "configure" are used in the configuration stage, and all
218 // remaining commands (excluding output and launch) are used in the
219 // generate stage. Output, launch, seed, and shower commands are
220 // automatically handled. If the user wishes to override commands,
221 // then the stage can be specified. This will prevent any
222 // automatically generated commands from being used for that
223 // stage. This should only be done if the user understands what
224 // additional commands are needed.
225 
226 // The system MadGraph configuration will be used if the configuration
227 // stage is overridden by the user and only blank commands have been
228 // passed. This is accomplished via
229 // readString("", LHAupMadgraph::Configure).
230 
231 bool LHAupMadgraph::readString(string line, Stage stage) {
232  if (stage == Auto) {
233  if (line.substr(0, 4) == " set") launchLines.push_back(line);
234  else if (line.substr(0, 10) == "configure ")
235  configureLines.push_back(line.substr(10));
236  else if (line.substr(0, 6) != "output" && line.substr(0, 6) != "launch")
237  generateLines.push_back(line);
238  else return false;
239  } else if (stage == Configure) {
240  override[Configure] = true; if (line != "") configureLines.push_back(line);
241  } else if (stage == Generate) {
242  override[Generate] = true; generateLines.push_back(line);
243  } else if (stage == Launch) {
244  override[Launch] = true; launchLines.push_back(line);
245  } else return false;
246  return true;
247 }
248 
249 //--------------------------------------------------------------------------
250 
251 // Add a MadGraph configuration card to be used.
252 
253 // In general, MadGraph should be configured via the readString
254 // method. However, there are some cases where the user might wish to
255 // provide an entire configuration card, e.g. setting BSM model
256 // space. This method allows the user to provide a source card, src,
257 // which is then copied to <MadGraph run directory>/Cards/dst. These
258 // cards are copied before any MadGraph processes are launched.
259 
260 void LHAupMadgraph::addCard(string src, string dst) {
261  cards.push_back(make_pair(src, dst));
262 }
263 
264 //--------------------------------------------------------------------------
265 
266 // Set the random seed and maximum allowed runs.
267 
268 // If the random seed is negative (default of -1), then the MadGraph
269 // seed is taken as the Pythia parameter "Random:seed", which must be
270 // greater than 0. If the maximum number of allowed runs is exceeded
271 // (default of 30081) an error is thrown. The seed for a MadGraph run
272 // is set as:
273 
274 // (random seed - 1) * (maximum runs) + (number of runs) + 1
275 
276 // MadGraph can only handle random seeds up to 30081 * 30081. So, with
277 // this strategy, one can generate Pythia jobs with seeds from 1 to
278 // 30081, with each job running MadGraph less than 30081 times, and
279 // ensure a fully statistically independent sample. If more than 30081
280 // jobs are needed, then the maximum allowed runs can be lowered
281 // accordingly, and if need be, setEvents can be used to increase the
282 // number of events generated per run.
283 
284 bool LHAupMadgraph::setSeed(int seedIn, int runsIn) {
285 
286  if (!pythia) return false;
287  seed = seedIn;
288  if (seed < 0) {
289  seed = pythia->settings.mode("Random:seed");
290  if (seed < 1) {
291  errorMsg("Error from LHAupMadgraph::setSeed: the given "
292  "Pythia seed is less than 1."); return false;}
293  }
294  runs = runsIn;
295  if (seed * runs > 30081 * 30081) {
296  errorMsg("Error from LHAupMadgraph::setSeed: the given seed "
297  "exceeds the MadGraph limit."); return false;}
298  nRuns = 0;
299  return true;
300 
301 }
302 
303 //--------------------------------------------------------------------------
304 
305 // Set the number of events to generate per MadGraph run, the default is 10000.
306 
307 void LHAupMadgraph::setEvents(int eventsIn) {events = eventsIn;}
308 
309 //--------------------------------------------------------------------------
310 
311 // Set the number maximum number of jets generated by MadGraph.
312 
313 // If negative (default of -1) then the number of jets is determined
314 // automatically. This is the maximum number of jets produced at
315 // leading order.
316 
317 void LHAupMadgraph::setJets(int jetsIn) {jets = jetsIn;}
318 
319 //--------------------------------------------------------------------------
320 
321 // Execute a system command.
322 
323 bool LHAupMadgraph::execute(string line) {return system(line.c_str()) != -1;}
324 
325 //--------------------------------------------------------------------------
326 
327 // Write the MadGraph configuration.
328 
329 // If not overridden, the MadGraph configuration is set to prevent the
330 // output from being opened in a web-browser and stop MadGraph
331 // updates.
332 
334 
335  if (override[Configure] && configureLines.size() == 0) return true;
336  mkdir((dir + "/.mg5").c_str(), 0777);
337  fstream config((dir + "/.mg5/mg5_configuration.txt").c_str(), ios::out);
338  for (int iLine = 0; iLine < (int)configureLines.size(); ++iLine)
339  config << configureLines[iLine] << "\n";
340  if (!override[Configure])
341  config << "automatic_html_opening = False\n"
342  << "auto_update = 0\n";
343  config.close();
344  return true;
345 
346 }
347 
348 //--------------------------------------------------------------------------
349 
350 // Run the generate stage of MadGraph.
351 
352 // The final command of "output <dir> -f -nojpeg\n" is automatically
353 // set, if not overridden. MadGraph is then run and the output is
354 // checked. Finally, the configuration is updated and the type of run,
355 // MadGraph or aMC@NLO, is determined.
356 
358 
359  // Write the user settings to the generate configuration file.
360  if (!pythia) return false;
361  fstream config((dir + "/generate.py").c_str(), ios::out);
362  for (int iLine = 0; iLine < (int)generateLines.size(); ++iLine)
363  config << generateLines[iLine] << "\n";
364  if (!override[Generate]) config << "output " << dir << "/tmp -f -nojpeg\n";
365  config.close();
366 
367  // Run MadGraph and check output.
368  fstream orig((dir + "/.mg5/mg5_configuration.txt").c_str(), ios::in);
369  char *home = getenv("HOME");
370  setenv("HOME", dir.c_str(), 1);
371  bool success = execute(exe + " " + dir + "/generate.py");
372  setenv("HOME", home, 1);
373  if (!success) {orig.close(); return false;}
374  else if (access((dir + "/tmp/Cards/run_card.dat").c_str(), F_OK) == -1) {
375  errorMsg("Error from LHAupMadgraph::generate: MadGraph "
376  "failed to produce run_card.dat");
377  orig.close(); return false;
378  } else execute("mv " + dir + "/tmp/* " + dir + "; rmdir " + dir + "/tmp");
379 
380  // Update configuration.
381  amcatnlo =
382  access((dir + "/Cards/amcatnlo_configuration.txt").c_str(), F_OK) != -1;
383  if (orig.good()) {
384  fstream copy((dir + "/Cards/" + (amcatnlo ? "amcatnlo" : "me5") +
385  "_configuration.txt").c_str(), ios::out);
386  copy << orig.rdbuf(); copy.close();
387  }
388  orig.close();
389 
390  // Copy over any user provided configuration cards.
391  for (int iCard = 0; iCard < (int)cards.size(); ++iCard) {
392  ifstream src((cards[iCard].first).c_str(), ios::binary);
393  ofstream dst((dir + "/Cards/" + cards[iCard].second).c_str(), ios::binary);
394  dst << src.rdbuf();
395  }
396  return true;
397 
398 }
399 
400 //--------------------------------------------------------------------------
401 
402 // Run the launch stage of MadGraph.
403 
404 // The first command "launch ..." is automatically set, and depends on
405 // whether aMC@NLO is being used.
406 
408 
409  // Open the launch configuration file and write the settings.
410  if (!pythia) return false;
411  fstream config((dir + "/launch.py").c_str(), ios::out);
412  if (!override[Launch]) {
413  config << "launch " << dir << " -n run";
414  if (amcatnlo) config << " -p\n" << "set parton_shower PYTHIA8\n"
415  << "set ickkw 3\n" << "set nevents 0\n"
416  << "set req_acc 0.001\n";
417  else config << " -s parton\n" << "set ickkw 1\n" << "set gridpack True\n";
418  }
419 
420  // Write the user settings.
421  for (int iLine = 0; iLine < (int)launchLines.size(); ++iLine)
422  config << launchLines[iLine] << "\n";
423  if (!override[Launch]) config << "done\n";
424  config.close();
425 
426  // Fix aMC@NLO linking.
427  if (amcatnlo) {
428  string line = "cd " + dir + "/MCatNLO/lib; LINKS=`ls`; for LINK in $LINKS;"
429  " do TARG=`readlink $LINK`; if [[ $TARG = ../* ]]; then "
430  "rm $LINK; ln -s ${TARG:3} $LINK; fi; done";
431  if (!execute(line)) {
432  errorMsg("Error from LHAupMadgraph::launch: failed to "
433  "link aMC@NLO libraries"); return false;}
434  }
435 
436  // Run MadGraph and create run scripts.
437  if (!execute(exe + " " + dir + "/launch.py")) return false;
438  if (amcatnlo) {
439  if (access((dir + "/SubProcesses/results.dat").c_str(), F_OK) == -1) {
440  errorMsg("Error from LHAupMadgraph::launch: aMC@NLO failed "
441  "to produce results.dat"); return false;}
442  fstream script((dir + "/run.sh").c_str(), ios::out);
443  script << "#!/usr/bin/env bash\n"
444  << "sed -i \"s/.*= *nevents/$1 = nevents/g\" ./Cards/run_card.dat\n"
445  << "sed -i \"s/.*= *iseed/$2 = iseed/g\" ./Cards/run_card.dat\n"
446  << "./bin/generate_events --parton --nocompile --only_generation "
447  "--force --name run\n" << "mv Events/run/events.lhe.gz ./\n";
448  script.close(); execute("chmod 755 " + dir + "/run.sh");
449  } else {
450  string gpk = "run_gridpack.tar.gz";
451  if (access((dir + "/" + gpk).c_str(), F_OK) == -1) {
452  errorMsg("Error from LHAupMadgraph::launch: MadEvent failed"
453  " to produce " + gpk); return false;}
454  string line = "cd " + dir + "; tar -xzf " + gpk + "; cd madevent/lib; "
455  "LINK=`readlink libLHAPDF.a`; if [[ $LINK = ../* ]]; then "
456  "rm libLHAPDF.a; ln -s ../$LINK libLHAPDF.a; fi; cd ../; "
457  "./bin/compile dynamic; ./bin/clean4grid";
458  if (!execute(line)) {
459  errorMsg("Error from LHAupMadgraph::launch: failed to "
460  "compile MadEvent code"); return false;}
461  }
462  return true;
463 
464 }
465 
466 //--------------------------------------------------------------------------
467 
468 // Run MadGraph.
469 
470 bool LHAupMadgraph::run(int eventsIn, int seedIn) {
471 
472  if (!pythia) return false;
473  if (nRuns >= runs) {
474  errorMsg("Error from LHAupMadgraph::run: maximum number "
475  "of allowed runs exceeded."); return false;}
476  if (access((dir + "/run.sh").c_str(), F_OK) == -1) return false;
477  if (seed < 0 && !setSeed(seed, runs)) return false;
478  if (seedIn < 0) seedIn = (seed - 1) * runs + nRuns + 1;
479  stringstream line;
480  line << "cd " + dir + "; ./run.sh " << eventsIn << " " << seedIn;
481  if (!amcatnlo) line << "; rm -rf ./madevent/Events/*";
482  if (!execute(line.str())) return false;
483  if (access(lhegz.c_str(), F_OK) == -1) return false;
484  ++nRuns;
485  return true;
486 
487 }
488 
489 //--------------------------------------------------------------------------
490 
491 // Create the LHEF reader.
492 
493 bool LHAupMadgraph::reader(bool init) {
494 
495  // Check valid LHE file.
496  if (!pythia) return false;
497  if (lhef) delete lhef;
498  bool setScales(pythia->settings.flag("Beams:setProductionScalesFromLHEF"));
499  lhef = new LHAupLHEF(infoPtr, lhegz.c_str(), nullptr, false, setScales);
500  if (!lhef->setInit()) {
501  errorMsg("Error from LHAupMadgraph::reader: failed to "
502  "initialize the LHEF reader"); return false;}
503  if (lhef->sizeProc() != 1) {
504  errorMsg("Error from LHAupMadgraph::reader: number of "
505  "processes is not 1"); return false;}
506 
507  if (init) {
508 
509  // Determine the cross-section (if needed).
510  double sig(lhef->xSec(0)), err(lhef->xErr(0));
511  if (!amcatnlo) {
512  fstream results((dir + "/madevent/SubProcesses/"
513  "run_results.dat").c_str(), ios::in);
514  string v; vector<double> vs;
515  while (std::getline(results, v, ' ')) vs.push_back(atof(v.c_str()));
516  if (vs.size() < 2) {
517  errorMsg("Error from LHAupMadgraph::reader: could not "
518  "extract cross-section"); return false;}
519  sig = vs[0]; err = vs[1];
520  }
521 
522  // Set the info.
523  setBeamA(lhef->idBeamA(), lhef->eBeamA(), lhef->pdfGroupBeamA(),
524  lhef->pdfSetBeamA());
525  setBeamB(lhef->idBeamB(), lhef->eBeamB(), lhef->pdfGroupBeamB(),
526  lhef->pdfSetBeamB());
527  setStrategy(lhef->strategy());
528  addProcess(lhef->idProcess(0), sig, err, lhef->xMax(0));
529  xSecSumSave = sig; xErrSumSave = err;
530  }
531  return true;
532 
533 }
534 
535 //--------------------------------------------------------------------------
536 
537 // Set the initialization information.
538 
539 // If shower matching has been requested, then the matching is also
540 // set up depending on the type of MadGraph output.
541 
543 
544  // Initialize MadGraph.
545  if (!pythia) return false;
546  if (access((dir + "/run.sh").c_str(), F_OK) == -1) {
547  if (!configure()) {
548  errorMsg("Error from LHAupMadgraph::setInit: failed to "
549  "create the MadGraph configuration"); return false;}
550  if (!generate()) {
551  errorMsg("Error from LHAupMadgraph::setInit: failed to "
552  "generate the MadGraph process"); return false;}
553  if (!launch()) {
554  errorMsg("Error from LHAupMadgraph::setInit: failed to "
555  "launch the MadGraph process"); return false;}
556  } else
557  amcatnlo =
558  access((dir + "/Cards/amcatnlo_configuration.txt").c_str(), F_OK) != -1;
559 
560  // Set up matching if requested.
561  if (match) {
562 
563  // Load the MadGraph parameters.
564  ifstream card((dir + "/Cards/run_card.dat").c_str());
565  string str((std::istreambuf_iterator<char>(card)),
566  std::istreambuf_iterator<char>());
567  MadgraphPar mad;
568  mad.parse(str);
569  mad.printParams();
570 
571  // Extract maximum number of jets.
572  int iLine = jets < 0 ? 0 : generateLines.size();
573  for (; iLine < (int)generateLines.size(); ++iLine) {
574  string line = generateLines[iLine];
575  size_t found = line.find(">");
576  if (found == string::npos) continue;
577  else line = line.substr(found);
578  stringstream sline(line); string p; int n(0);
579  while (std::getline(sline, p, ' ') && p != ",")
580  if (p == "j" || p == "g" || p == "u" || p == "d" || p == "c" ||
581  p == "s" || p == "b" || p == "u~" || p == "d~" || p == "c~" ||
582  p == "s~" || p == "b~") ++n;
583  if (n > jets) jets = n;
584  }
585 
586  // Common settings.
587  double etaj = mad.getParam("etaj");
588  Settings &set = pythia->settings;
589  set.flag("JetMatching:merge", true);
590  set.mode("JetMatching:scheme", 1);
591  set.flag("JetMatching:setMad", false);
592  set.mode("JetMatching:nQmatch", mad.getParamAsInt("maxjetflavor"));
593  set.parm("JetMatching:qCut", mad.getParam("ptj"));
594  set.parm("JetMatching:etaJetMax", etaj > 0 ? etaj : 100);
595  set.mode("JetMatching:nJetMax", jets);
596  set.parm("Check:epTolErr", 1e-2);
597 
598  // aMC@NLO settings.
599  if (amcatnlo) {
600  set.parm("JetMatching:coneRadius", mad.getParam("jetradius"));
601  set.mode("JetMatching:slowJetPower", mad.getParam("jetalgo"));
602  set.parm("JetMatching:qCutME", mad.getParam("ptj"));
603  set.mode("JetMatching:jetAlgorithm", 2);
604  set.flag("JetMatching:doFxFx", true);
605  set.flag("SpaceShower:MEcorrections", false);
606  set.parm("TimeShower:pTmaxMatch", 1);
607  set.parm("TimeShower:pTmaxFudge", 1);
608  set.flag("TimeShower:MEcorrections", false);
609  set.flag("TimeShower:globalRecoil", true);
610  set.flag("TimeShower:limitPTmaxGlobal", true);
611  set.mode("TimeShower:nMaxGlobalRecoil", 1);
612  set.mode("TimeShower:globalRecoilMode", 2);
613 
614  // MLM tree-level MadGraph settings.
615  } else set.parm("JetMatching:clFact", mad.getParam("alpsfact"));
616 
617  // Set the matching hook.
618  hook = make_shared<JetMatchingMadgraph>();
619  pythia->setUserHooksPtr((UserHooksPtr)hook);
620  }
621 
622  // Create the LHEF LHAup object and run setInit.
623  if (!run(events)) return false;
624  if (!reader(true)) return false;
625  listInit();
626  return true;
627 
628 }
629 
630 //--------------------------------------------------------------------------
631 
632 // Set the event information.
633 
635 
636  // Run setEvent from the LHEF object and launch MadGraph if failed.
637  if (!pythia) return false;
638  if (!lhef) {
639  errorMsg("Error from LHAupMadgraph::setEvent: LHAupLHEF "
640  "object not correctly initialized"); return false;}
641  if (!lhef->fileFound()) {
642  errorMsg("Error from LHAupMadgraph::setEvent: LHEF "
643  "event file was not found"); return false;}
644  if (!lhef->setEvent()) {
645  if (!run(events)) return false;
646  if (!reader(false)) return false;
647  lhef->setEvent();
648  }
649 
650  // Read the event from the LHEF object.
651  setProcess(lhef->idProcess(), lhef->weight(), lhef->scale(),
652  lhef->alphaQED(), lhef->alphaQCD());
653  for (int ip = 1; ip < lhef->sizePart(); ++ip)
654  addParticle(lhef->id(ip), lhef->status(ip), lhef->mother1(ip),
655  lhef->mother2(ip), lhef->col1(ip), lhef->col2(ip),
656  lhef->px(ip), lhef->py(ip), lhef->pz(ip), lhef->e(ip),
657  lhef->m(ip), lhef->tau(ip), lhef->spin(ip), lhef->scale(ip));
658  setIdX(lhef->id1(), lhef->id2(), lhef->x1(), lhef->x2());
659  setPdf(lhef->id1pdf(), lhef->id2pdf(), lhef->x1pdf(), lhef->x2pdf(),
660  lhef->scalePDF(), lhef->pdf1(), lhef->pdf2(), lhef->pdfIsSet());
661  return true;
662 
663 }
664 
665 //==========================================================================
666 
667 } // end namespace Pythia8
668 
669 #endif // Pythia8_LHAMadgraph_H
void setBeamA(int idIn, double eIn, int pdfGroupIn=0, int pdfSetIn=0)
Input beam info.
Definition: LesHouches.h:222
void addProcess(int idProcIn, double xSecIn=1., double xErrIn=0., double xMaxIn=1.)
Input process info.
Definition: LesHouches.h:233
void errorMsg(string messageIn)
Print a message the first few times. Insert in database.
Definition: LHAMadgraph.h:122
void setJets(int jetsIn)
Set the maximum number of jets produced by MadGraph.
Definition: LHAMadgraph.h:317
bool execute(string line)
Execute a system command.
Definition: LHAMadgraph.h:323
bool flag(string keyIn)
Give back current value, with check that key exists.
Definition: Settings.cc:1478
bool setSeed(int seedIn, int runsIn=30081)
Set the random seed and maximum runs.
Definition: LHAMadgraph.h:284
bool fileFound()
Confirm that file was found and opened as expected.
Definition: LesHouches.h:408
bool reader(bool init)
Create the LHEF reader.
Definition: LHAMadgraph.h:493
void setStrategy(int strategyIn)
Input process weight strategy.
Definition: LesHouches.h:230
void setIdX(int id1In, int id2In, double x1In, double x2In)
Input info on flavour and x values of hard-process initiators.
Definition: LesHouches.h:263
~LHAupMadgraph()
Definition: LHAMadgraph.h:173
Settings settings
Settings: databases of flags/modes/parms/words to control run.
Definition: Pythia.h:332
int id1() const
Give back info on flavour and x values of hard-process initiators.
Definition: LesHouches.h:158
Definition: LesHouches.h:78
bool setInit()
Set the initialization information.
Definition: LHAMadgraph.h:542
void addParticle(LHAParticle particleIn)
Input particle info, one particle at the time.
Definition: LesHouches.h:252
void setProcess(int idProcIn=0, double weightIn=1., double scaleIn=0., double alphaQEDIn=0.0073, double alphaQCDIn=0.12)
Input info on the selected process.
Definition: LesHouches.h:243
bool setEvent(int=0)
Set the event information.
Definition: LHAMadgraph.h:634
A derived class with information read from a Les Houches Event File.
Definition: LesHouches.h:346
void setEvents(int eventsIn)
Set the number of events to generate per run.
Definition: LHAMadgraph.h:307
void setPdf(int id1pdfIn, int id2pdfIn, double x1pdfIn, double x2pdfIn, double scalePDFIn, double pdf1In, double pdf2In, bool pdfIsSetIn)
Optionally input info on parton density values of event.
Definition: LesHouches.h:267
void addCard(string src, string dst)
Add a MadGraph configuration card to be used.
Definition: LHAMadgraph.h:260
bool launch()
Run the launch stage of MadGraph.
Definition: LHAMadgraph.h:407
bool parse(const string paramStr)
Parse an incoming Madgraph parameter file string.
Definition: GeneratorInput.h:1139
int sizePart() const
Give back info on separate particle.
Definition: LesHouches.h:141
A derived class from LHAup which generates events with MadGraph 5.
Definition: LHAMadgraph.h:63
bool readString(string line, Stage stage=Auto)
Read a MadGraph command string.
Definition: LHAMadgraph.h:231
void printParams()
Print parameters read from the &#39;.par&#39; file.
Definition: GeneratorInput.h:1190
Stage
Types of MadGraph stages.
Definition: LHAMadgraph.h:68
LHAupMadgraph(Pythia *pythiaIn, bool matchIn=true, string dirIn="madgraphrun", string exeIn="mg5_aMC")
Constructor.
Definition: LHAMadgraph.h:160
bool pdfIsSet() const
Optional: give back info on parton density values of event.
Definition: LesHouches.h:164
bool setUserHooksPtr(UserHooksPtr userHooksPtrIn)
Possibility to pass in pointer for user hooks.
Definition: Pythia.h:147
bool configure()
Write the MadGraph configuration.
Definition: LHAMadgraph.h:333
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
The Pythia class contains the top-level routines to generate an event.
Definition: Pythia.h:71
bool setEvent(int=0)
Routine for doing the job of reading and setting info on next event.
Definition: LesHouches.h:420
Info * infoPtr
Pointer to various information on the generation.
Definition: LesHouches.h:218
bool generate()
Run the generate stage of MadGraph.
Definition: LHAMadgraph.h:357
Definition: GeneratorInput.h:156
double getParam(const string &paramIn)
Definition: GeneratorInput.h:175
bool readString(string, bool warn=true, int subrun=SUBRUNDEFAULT)
Read in one update for a setting or particle data from a single line.
Definition: Pythia.cc:365
void listInit()
Print the initialization info; useful to check that setting it worked.
Definition: LesHouches.cc:33
bool run(int eventsIn, int seedIn=-1)
Run MadGraph.
Definition: LHAMadgraph.h:470
Definition: Settings.h:195