StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LesHouches.h
1 // LesHouches.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 
6 // Header file for Les Houches Accord user process information.
7 // LHAProcess: stores a single process; used by the other classes.
8 // LHAParticle: stores a single particle; used by the other classes.
9 // LHAup: base class for initialization and event information.
10 // LHAupLHEF: derived class for reading from an Les Houches Event File.
11 // Code for interfacing with Fortran commonblocks is found in LHAFortran.h.
12 
13 #ifndef Pythia8_LesHouches_H
14 #define Pythia8_LesHouches_H
15 
16 #include "Pythia8/Event.h"
17 #include "Pythia8/Info.h"
18 #include "Pythia8/PythiaStdlib.h"
19 #include "Pythia8/Settings.h"
20 #include "Pythia8/Streams.h"
21 
22 namespace Pythia8 {
23 
24 //==========================================================================
25 
26 // A class for the processes stored in LHAup.
27 
28 class LHAProcess {
29 
30 public:
31 
32  // Constructors.
33  LHAProcess() : idProc(0), xSecProc(0.), xErrProc(0.), xMaxProc(0.) { }
34  LHAProcess(int idProcIn, double xSecIn, double xErrIn, double xMaxIn) :
35  idProc(idProcIn), xSecProc(xSecIn), xErrProc(xErrIn),
36  xMaxProc(xMaxIn) { }
37 
38  // Process properties.
39  int idProc;
40  double xSecProc, xErrProc, xMaxProc;
41 
42 } ;
43 
44 //==========================================================================
45 
46 // A class for the particles stored in LHAup.
47 
48 class LHAParticle {
49 
50 public:
51 
52  // Constructors.
53  LHAParticle() : idPart(0), statusPart(0), mother1Part(0),
54  mother2Part(0), col1Part(0), col2Part(0), pxPart(0.), pyPart(0.),
55  pzPart(0.), ePart(0.), mPart(0.), tauPart(0.), spinPart(9.),
56  scalePart(-1.) { }
57  LHAParticle(int idIn, int statusIn, int mother1In, int mother2In,
58  int col1In, int col2In, double pxIn, double pyIn, double pzIn,
59  double eIn, double mIn, double tauIn, double spinIn,
60  double scaleIn) :
61  idPart(idIn), statusPart(statusIn), mother1Part(mother1In),
62  mother2Part(mother2In), col1Part(col1In), col2Part(col2In),
63  pxPart(pxIn), pyPart(pyIn), pzPart(pzIn), ePart(eIn), mPart(mIn),
64  tauPart(tauIn), spinPart(spinIn), scalePart(scaleIn) { }
65 
66  // Particle properties.
67  int idPart, statusPart, mother1Part, mother2Part, col1Part, col2Part;
68  double pxPart, pyPart, pzPart, ePart, mPart, tauPart, spinPart,
69  scalePart;
70 
71 } ;
72 
73 //==========================================================================
74 
75 // LHAup is base class for initialization and event information
76 // from an external parton-level generator.
77 
78 class LHAup {
79 
80 public:
81 
82  // Destructor.
83  virtual ~LHAup() {}
84 
85  // Set info pointer.
86  void setPtr(Info* infoPtrIn) {infoPtr = infoPtrIn;}
87 
88  // Method to be used for LHAupLHEF derived class.
89  virtual void newEventFile(const char*) {}
90  virtual bool fileFound() {return true;}
91  virtual bool useExternal() {return false;}
92 
93  // A pure virtual method setInit, wherein all initialization information
94  // is supposed to be set in the derived class. Can do this by reading a
95  // file or some other way, as desired. Returns false if it did not work.
96  virtual bool setInit() = 0;
97 
98  // Give back info on beams.
99  int idBeamA() const {return idBeamASave;}
100  int idBeamB() const {return idBeamBSave;}
101  double eBeamA() const {return eBeamASave;}
102  double eBeamB() const {return eBeamBSave;}
103  int pdfGroupBeamA() const {return pdfGroupBeamASave;}
104  int pdfGroupBeamB() const {return pdfGroupBeamBSave;}
105  int pdfSetBeamA() const {return pdfSetBeamASave;}
106  int pdfSetBeamB() const {return pdfSetBeamBSave;}
107 
108  // Give back weight strategy.
109  int strategy() const {return strategySave;}
110 
111  // Give back info on processes.
112  int sizeProc() const {return processes.size();}
113  int idProcess(int proc) const {return processes[proc].idProc;}
114  double xSec(int proc) const {return processes[proc].xSecProc;}
115  double xErr(int proc) const {return processes[proc].xErrProc;}
116  double xMax(int proc) const {return processes[proc].xMaxProc;}
117  double xSecSum() const {return xSecSumSave;}
118  double xErrSum() const {return xErrSumSave;}
119 
120  // Print the initialization info; useful to check that setting it worked.
121  void listInit();
122 
123  // A pure virtual method setEvent, wherein information on the next event
124  // is supposed to be set in the derived class.
125  // Strategies +-1 and +-2: idProcIn is the process type, selected by PYTHIA.
126  // Strategies +-3 and +-4: idProcIn is dummy; process choice is made locally.
127  // The method can find the next event by a runtime interface to another
128  // program, or by reading a file, as desired.
129  // The method should return false if it did not work.
130  virtual bool setEvent(int idProcIn = 0) = 0;
131 
132  // Give back process number, weight, scale, alpha_em, alpha_s.
133  int idProcess() const {return idProc;}
134  double weight() const {return weightProc;}
135  double scale() const {return scaleProc;}
136  double alphaQED() const {return alphaQEDProc;}
137  double alphaQCD() const {return alphaQCDProc;}
138 
139  // Give back info on separate particle.
140  int sizePart() const {return particles.size();}
141  int id(int part) const {return particles[part].idPart;}
142  int status(int part) const {return particles[part].statusPart;}
143  int mother1(int part) const {return particles[part].mother1Part;}
144  int mother2(int part) const {return particles[part].mother2Part;}
145  int col1(int part) const {return particles[part].col1Part;}
146  int col2(int part) const {return particles[part].col2Part;}
147  double px(int part) const {return particles[part].pxPart;}
148  double py(int part) const {return particles[part].pyPart;}
149  double pz(int part) const {return particles[part].pzPart;}
150  double e(int part) const {return particles[part].ePart;}
151  double m(int part) const {return particles[part].mPart;}
152  double tau(int part) const {return particles[part].tauPart;}
153  double spin(int part) const {return particles[part].spinPart;}
154  double scale(int part) const {return particles[part].scalePart;}
155 
156  // Give back info on flavour and x values of hard-process initiators.
157  int id1() const {return id1Save;}
158  int id2() const {return id2Save;}
159  double x1() const {return x1Save;}
160  double x2() const {return x2Save;}
161 
162  // Optional: give back info on parton density values of event.
163  bool pdfIsSet() const {return pdfIsSetSave;}
164  int id1pdf() const {return id1pdfSave;}
165  int id2pdf() const {return id2pdfSave;}
166  double x1pdf() const {return x1pdfSave;}
167  double x2pdf() const {return x2pdfSave;}
168  double scalePDF() const {return scalePDFSave;}
169  double pdf1() const {return pdf1Save;}
170  double pdf2() const {return pdf2Save;}
171 
172  // Print the info; useful to check that reading an event worked.
173  void listEvent();
174 
175  // Skip ahead a number of events, which are not considered further.
176  // Mainly intended for debug when using the LHAupLHEF class.
177  virtual bool skipEvent(int nSkip) {
178  for (int iSkip = 0; iSkip < nSkip; ++iSkip) if (!setEvent()) return false;
179  return true;}
180 
181  // Four routines to write a Les Houches Event file in steps.
182  virtual bool openLHEF(string fileNameIn);
183  virtual bool closeLHEF(bool updateInit = false);
184  bool initLHEF();
185  bool eventLHEF(bool verbose = true);
186 
187  // Get access to the Les Houches Event file name.
188  string getFileName() const {return fileName;}
189 
190 protected:
191 
192  // Constructor. Sets default to be that events come with unit weight.
193  LHAup(int strategyIn = 3) : fileName("void"), strategySave(strategyIn)
194  { processes.reserve(10); particles.reserve(20);
195  setBeamA( 0, 0., 0, 0); setBeamB( 0, 0., 0, 0); }
196 
197  // Allow conversion from mb to pb.
198  static const double CONVERTMB2PB;
199 
200  // Pointer to various information on the generation.
201  Info* infoPtr;
202 
203  // Input beam info.
204  void setBeamA(int idIn, double eIn, int pdfGroupIn = 0, int pdfSetIn = 0)
205  { idBeamASave = idIn; eBeamASave = eIn; pdfGroupBeamASave = pdfGroupIn;
206  pdfSetBeamASave = pdfSetIn;}
207  void setBeamB(int idIn, double eIn, int pdfGroupIn = 0, int pdfSetIn = 0)
208  { idBeamBSave = idIn; eBeamBSave = eIn; pdfGroupBeamBSave = pdfGroupIn;
209  pdfSetBeamBSave = pdfSetIn;}
210 
211  // Input process weight strategy.
212  void setStrategy(int strategyIn) {strategySave = strategyIn;}
213 
214  // Input process info.
215  void addProcess(int idProcIn, double xSecIn = 1., double xErrIn = 0.,
216  double xMaxIn = 1.) { processes.push_back( LHAProcess( idProcIn,
217  xSecIn, xErrIn, xMaxIn)); }
218 
219  // Possibility to update some cross section info at end of run.
220  void setXSec(int iP, double xSecIn) {processes[iP].xSecProc = xSecIn;}
221  void setXErr(int iP, double xErrIn) {processes[iP].xErrProc = xErrIn;}
222  void setXMax(int iP, double xMaxIn) {processes[iP].xMaxProc = xMaxIn;}
223 
224  // Input info on the selected process.
225  void setProcess(int idProcIn = 0, double weightIn = 1., double
226  scaleIn = 0., double alphaQEDIn = 0.0073, double alphaQCDIn = 0.12) {
227  idProc = idProcIn; weightProc = weightIn; scaleProc = scaleIn;
228  alphaQEDProc = alphaQEDIn; alphaQCDProc = alphaQCDIn;
229  // Clear particle list. Add empty zeroth particle for correct indices.
230  particles.clear(); addParticle(0); pdfIsSetSave = false;}
231 
232  // Input particle info, one particle at the time.
233  void addParticle(LHAParticle particleIn) {
234  particles.push_back(particleIn);}
235  void addParticle(int idIn, int statusIn = 0, int mother1In = 0,
236  int mother2In = 0, int col1In = 0, int col2In = 0, double pxIn = 0.,
237  double pyIn = 0., double pzIn = 0., double eIn = 0., double mIn = 0.,
238  double tauIn = 0., double spinIn = 9., double scaleIn = -1.) {
239  particles.push_back( LHAParticle( idIn, statusIn, mother1In, mother2In,
240  col1In, col2In, pxIn, pyIn, pzIn, eIn, mIn, tauIn, spinIn,
241  scaleIn) ); }
242 
243  // Input info on flavour and x values of hard-process initiators.
244  void setIdX(int id1In, int id2In, double x1In, double x2In)
245  { id1Save = id1In; id2Save = id2In; x1Save = x1In; x2Save = x2In;}
246 
247  // Optionally input info on parton density values of event.
248  void setPdf(int id1pdfIn, int id2pdfIn, double x1pdfIn, double x2pdfIn,
249  double scalePDFIn, double pdf1In, double pdf2In, bool pdfIsSetIn)
250  { id1pdfSave = id1pdfIn; id2pdfSave = id2pdfIn; x1pdfSave = x1pdfIn;
251  x2pdfSave = x2pdfIn; scalePDFSave = scalePDFIn; pdf1Save = pdf1In;
252  pdf2Save = pdf2In; pdfIsSetSave = pdfIsSetIn;}
253 
254  // Three routines for LHEF files, but put here for flexibility.
255  bool setInitLHEF(istream& is, bool readHeaders = false);
256  bool setNewEventLHEF(istream& is);
257  bool setOldEventLHEF();
258 
259  // Helper routines to open and close a file handling GZIPSUPPORT:
260  // ifstream ifs;
261  // istream *is = openFile("myFile.txt", ifs);
262  // -- Process file using is --
263  // closeFile(is, ifs);
264  istream* openFile(const char *fn, ifstream &ifs);
265  void closeFile(istream *&is, ifstream &ifs);
266 
267  // LHAup is a friend class to infoPtr, but derived classes
268  // are not. This wrapper function can be used by derived classes
269  // to set headers in the Info class.
270  void setInfoHeader(const string &key, const string &val) {
271  infoPtr->setHeader(key, val); }
272 
273  // Event properties from LHEF files, for repeated use.
274  int nupSave, idprupSave;
275  double xwgtupSave, scalupSave, aqedupSave, aqcdupSave, xSecSumSave,
276  xErrSumSave;
277  vector<LHAParticle> particlesSave;
278  bool getPDFSave, getScale;
279  int id1InSave, id2InSave, id1pdfInSave, id2pdfInSave;
280  double x1InSave, x2InSave, x1pdfInSave, x2pdfInSave, scalePDFInSave,
281  pdf1InSave, pdf2InSave;
282 
283  // File to which to write Les Houches Event File information.
284  string fileName;
285  fstream osLHEF;
286  char dateNow[12];
287  char timeNow[9];
288 
289 private:
290 
291  // Event weighting and mixing strategy.
292  int strategySave;
293 
294  // Beam particle properties.
295  int idBeamASave, idBeamBSave;
296  double eBeamASave, eBeamBSave;
297  int pdfGroupBeamASave, pdfGroupBeamBSave, pdfSetBeamASave,
298  pdfSetBeamBSave;
299 
300  // The process list, stored as a vector of processes.
301  vector<LHAProcess> processes;
302 
303  // Store info on the selected process.
304  int idProc;
305  double weightProc, scaleProc, alphaQEDProc, alphaQCDProc;
306 
307  // The particle list, stored as a vector of particles.
308  vector<LHAParticle> particles;
309 
310  // Info on initiators and optionally on parton density values of event.
311  bool pdfIsSetSave;
312  int id1Save, id2Save, id1pdfSave, id2pdfSave;
313  double x1Save, x2Save, x1pdfSave, x2pdfSave, scalePDFSave, pdf1Save,
314  pdf2Save;
315 
316 };
317 
318 //==========================================================================
319 
320 // A derived class with information read from a Les Houches Event File.
321 
322 class LHAupLHEF : public LHAup {
323 
324 public:
325 
326  // Constructor.
327  LHAupLHEF(Pythia8::Info* infoPtrIn, istream* isIn, istream* isHeadIn,
328  bool readHeadersIn = false, bool setScalesFromLHEFIn = false ) :
329  infoPtr(infoPtrIn), filename(""), headerfile(""),
330  is(isIn), is_gz(NULL), isHead(isHeadIn), isHead_gz(NULL),
331  readHeaders(readHeadersIn), reader(is),
332  setScalesFromLHEF(setScalesFromLHEFIn), hasExtFileStream(true),
333  hasExtHeaderStream(true) {}
334 
335  LHAupLHEF(Pythia8::Info* infoPtrIn, const char* filenameIn,
336  const char* headerIn = NULL, bool readHeadersIn = false,
337  bool setScalesFromLHEFIn = false ) :
338  infoPtr(infoPtrIn), filename(filenameIn), headerfile(headerIn),
339  is(NULL), is_gz(NULL), isHead(NULL), isHead_gz(NULL),
340  readHeaders(readHeadersIn), reader(filenameIn),
341  setScalesFromLHEF(setScalesFromLHEFIn), hasExtFileStream(false),
342  hasExtHeaderStream(false) {
343  is = (openFile(filenameIn, ifs));
344  isHead = (headerfile == NULL) ? is : openFile(headerfile, ifsHead);
345  is_gz = new igzstream(filename);
346  isHead_gz = (headerfile == NULL) ? is_gz : new igzstream(headerfile);
347  }
348 
349 
350  // Destructor.
351  ~LHAupLHEF() {
352  // Close files
353  closeAllFiles();
354  }
355 
356  // Helper routine to correctly close files.
357  void closeAllFiles() {
358 
359  if (!hasExtHeaderStream && isHead_gz != is_gz) isHead_gz->close();
360  if (isHead_gz != is_gz) delete isHead_gz;
361  if (is_gz) is_gz->close();
362  if (is_gz) delete is_gz;
363 
364  // Close header file if separate, and close main file.
365  if (!hasExtHeaderStream && isHead != is) closeFile(isHead, ifsHead);
366  if (!hasExtFileStream) closeFile(is, ifs);
367  }
368 
369  // Want to use new file with events, but without reinitialization.
370  void newEventFile(const char* filenameIn) {
371  // Close files and then open new file.
372  closeAllFiles();
373  is = (openFile(filenameIn, ifs));
374  is_gz = new igzstream(filenameIn);
375  // Re-initialise Les Houches file reader.
376  reader.setup(filenameIn);
377  // Set isHead to is to keep expected behaviour in
378  // fileFound() and closeAllFiles().
379  isHead = is;
380  isHead_gz = is_gz;
381  }
382 
383  // Confirm that file was found and opened as expected.
384  bool fileFound() {return (useExternal() || (isHead->good() && is->good()));}
385  bool useExternal() {return (hasExtHeaderStream && hasExtFileStream);}
386 
387  // Routine for doing the job of reading and setting initialization info.
388  bool setInit() {
389  return setInitLHEF(*isHead, readHeaders);
390  }
391 
392  // Routine for doing the job of reading and setting initialization info.
393  bool setInitLHEF( istream & isIn, bool readHead);
394 
395  // Routine for doing the job of reading and setting info on next event.
396  bool setEvent(int = 0) {
397  if (!setNewEventLHEF()) return false;
398  return setOldEventLHEF();
399  }
400 
401  // Skip ahead a number of events, which are not considered further.
402  bool skipEvent(int nSkip) {
403  for (int iSkip = 0; iSkip < nSkip; ++iSkip)
404  if (!setNewEventLHEF()) return false;
405  return true;
406  }
407 
408  // Routine for doing the job of reading and setting info on next event.
409  bool setNewEventLHEF();
410 
411  // Update cross-section information at the end of the run.
412  bool updateSigma() {return true;}
413 
414 protected:
415 
416  // Used internally to read a single line from the stream.
417  bool getLine(string & line, bool header = true) {
418 #ifdef GZIPSUPPORT
419  if ( isHead_gz && header && !getline(*isHead_gz, line)) return false;
420  else if ( is_gz && !header && !getline(*is_gz, line)) return false;
421  if (header && !getline(*isHead, line)) return false;
422  else if (!header && !getline(*is, line)) return false;
423 #else
424  if (header && !getline(*isHead, line)) return false;
425  else if (!header && !getline(*is, line)) return false;
426 #endif
427  // Replace single by double quotes
428  replace(line.begin(),line.end(),'\'','\"');
429  return true;
430  }
431 
432 private:
433 
434  Info* infoPtr;
435  const char* filename;
436  const char* headerfile;
437 
438  // File from which to read (or a stringstream).
439  // Optionally also a file from which to read the LHEF header.
440  istream *is;
441  igzstream *is_gz;
442  ifstream ifs;
443  istream *isHead;
444  igzstream *isHead_gz;
445  ifstream ifsHead;
446 
447  // Flag to read headers or not
448  bool readHeaders;
449 
450  Reader reader;
451 
452  // Flag to set particle production scales or not.
453  bool setScalesFromLHEF, hasExtFileStream, hasExtHeaderStream;
454 
455 };
456 
457 //==========================================================================
458 
459 // A derived class with information read from PYTHIA 8 itself, for output.
460 
461 class LHAupFromPYTHIA8 : public LHAup {
462 
463 public:
464 
465  // Constructor.
466  LHAupFromPYTHIA8(Event* processPtrIn, Info* infoPtrIn) {
467  processPtr = processPtrIn; infoPtr = infoPtrIn;}
468 
469  // Destructor.
470  ~LHAupFromPYTHIA8() {}
471 
472  // Routine for doing the job of reading and setting initialization info.
473  bool setInit();
474 
475  // Routine for doing the job of reading and setting info on next event.
476  bool setEvent(int = 0);
477 
478  // Update cross-section information at the end of the run.
479  bool updateSigma();
480 
481 private:
482 
483  // Pointers to process event record and further information.
484  Event* processPtr;
485  Info* infoPtr;
486 
487 };
488 
489 //==========================================================================
490 
491 // A derived class with LHEF 3.0 information read from PYTHIA 8 itself, for
492 // output.
493 
494 class LHEF3FromPythia8 : public LHAup {
495 
496 public:
497 
498  // Constructor.
499  LHEF3FromPythia8(Event* eventPtrIn, Settings* settingsPtrIn,
500  Info* infoPtrIn, ParticleData* particleDataPtrIn, int pDigitsIn = 15,
501  bool writeToFileIn = true) :
502  eventPtr(eventPtrIn),settingsPtr(settingsPtrIn), infoPtr(infoPtrIn),
503  particleDataPtr(particleDataPtrIn), writer(osLHEF), pDigits(pDigitsIn),
504  writeToFile(writeToFileIn) {}
505 
506  // Routine for reading, setting and printing the initialisation info.
507  bool setInit();
508 
509  // Routine for reading, setting and printing the next event.
510  void setEventPtr(Event* evPtr) { eventPtr = evPtr; }
511  bool setEvent(int = 0);
512  string getEventString() { return writer.getEventString(&hepeup); }
513 
514  // Function to open the output file.
515  bool openLHEF(string fileNameIn);
516 
517  // Function to close (and possibly update) the output file.
518  bool closeLHEF(bool updateInit = false);
519 
520 private:
521 
522  // Pointer to event that should be printed.
523  Event* eventPtr;
524 
525  // Pointer to settings and info objects.
526  Settings* settingsPtr;
527  Info* infoPtr;
528  ParticleData* particleDataPtr;
529 
530  // LHEF3 writer
531  Writer writer;
532 
533  // Number of digits to set width of double write out
534  int pDigits;
535  bool writeToFile;
536 
537  // Some internal init and event block objects for convenience.
538  HEPRUP heprup;
539  HEPEUP hepeup;
540 
541 };
542 
543 //==========================================================================
544 
545 } // end namespace Pythia8
546 
547 #endif // Pythia8_LesHouches_H
Definition: AgUStep.h:26