PYTHIA  8.312
Pythia8Rivet.h
1 // Pythia8Rivet.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: Christian Bierlich, September 2020.
7 
8 #ifndef PYTHIA8RIVET_H
9 #define PYTHIA8RIVET_H
10 
11 #include "Rivet/AnalysisHandler.hh"
12 #include "Rivet/Tools/RivetPaths.hh"
13 #include "Rivet/Config/RivetConfig.hh"
14 
15 // Check that a proper version code is available (wrong for 4.0.0).
16 #if RIVET_VERSION_CODE - 1 == -1
17 #define RIVET_VERSION_CODE 40000
18 #endif
19 
20 // Deterine which Rivet version is used. If beyond 4, require HepMC3.
21 #if RIVET_VERSION_CODE >= 40000
22 #define RIVET_ENABLE_HEPMC_3
23 #define PYTHIA_USING_RIVET_4
24 #endif
25 
26 // Set the version of HepMC being used (either from Rivet or set above).
27 #ifndef RIVET_ENABLE_HEPMC_3
28 #include "Pythia8Plugins/HepMC2.h"
29 #else
30 #include "Pythia8Plugins/HepMC3.h"
31 #endif
32 #include "Pythia8/HIInfo.h"
33 #include <set>
34 #include <vector>
35 #include <string>
36 
37 namespace Pythia8 {
38 
39 // Simplified interface to the Rivet program. Remember to link with
40 // pythia and -lhepmcinterface -lHepMC -lRivet
41 //
42 // Usage: (1) Create an object giving the pythia object and a filename
43 // as arguments. (2) Repeatedly specify (the name of an) analysis with
44 // the addAnalysis() function, possibly with analysis parameters.
45 // (3) initialize the underlying Rivet object with the init() function.
46 // (4) Analyze an event with the operator() function. (5) Dump the
47 // histograms to a file with the done() function.
48 class Pythia8Rivet {
49 
50 public:
51 
52  // The constructor needs to have the main Pythia object, and the
53  // name of the file where the histograms are dumped.
54  Pythia8Rivet(Pythia & pytin, string fname)
55  : pythia(&pytin), filename(fname), rivet(0), igBeam(false), nDump(-1),
56  dumpFile("") {}
57 
58  // The destructor will write the histogram file if this has not
59  // already been done.
61 
62  // Add the name of an analysis to be performed, with a list
63  // of analysis parameters.
64  void addAnalysis(string ana) {analyses.insert(ana);}
65 
66  // Add a YODA file pre-load pre-filled histogram from.
67  void addPreload(string prel) {preloads.push_back(prel);}
68 
69  // Set "ignore beams" flag.
70  void ignoreBeams(bool flagIn) {igBeam = flagIn;}
71 
72  // Set Rivet dump period and file name for intermittent histograms.
73  // If no dumpfile given, the default output file is used.
74  void dump(int period, string fname = "") {
75  nDump = period;
76  dumpFile = fname;
77  }
78 
79  // Add an attribute to the current event.
80  void addAttribute(const string& name, double val) {
81  eventAttributes[name] = val;}
82 
83  // Add an (optional) run name for Rivet internal use.
84  void addRunName(const string runname) {rname = runname;}
85 
86  // Initialize Rivet. Will do nothing if Rivet was already initialized
87  void init() {
88  if ( rivet ) return;
89  rivet = new Rivet::AnalysisHandler(rname);
90 #ifndef PYTHIA_USING_RIVET_4
91  rivet->setIgnoreBeams(igBeam);
92  if (nDump > 0) {
93  if (dumpFile == "")
94  rivet->dump(filename, nDump);
95  else
96  rivet->dump(dumpFile, nDump);
97  }
98 #else
99  rivet->setCheckBeams(!igBeam);
100  if (nDump > 0) {
101  if (dumpFile == "")
102  rivet->setFinalizePeriod(filename, nDump);
103  else
104  rivet->setFinalizePeriod(dumpFile, nDump);
105  }
106 #endif
107  initpath();
108  for(int i = 0, N = preloads.size(); i < N; ++i)
109  rivet->readData(preloads[i]);
110  for (set<string>::iterator it = analyses.begin();
111  it != analyses.end(); ++it) {
112  rivet->addAnalysis(*it);
113  }
114  rivet->init(converter.event());
115  }
116 
117  // Helper function to set analysis path.
118  void initpath() const {
119  for ( const string & path : Rivet::getAnalysisLibPaths() )
120  if ( path == "." ) return;
121  Rivet::addAnalysisLibPath(".");
122  }
123 
124  // Analyze the default Pythia event. Will do nothing if Rivet has
125  // not been intialized.
126  void operator()() {this->operator()(pythia->event);}
127 
128  // Analyze the given event.
129  void operator()(Event & event) {
130  converter.fillNextEvent(*pythia);
131  if ( !rivet ) init();
132 #ifdef RIVET_ENABLE_HEPMC_3
133  for (auto att : eventAttributes)
134  converter.addAttribute(att.first, att.second);
135 #endif
136  rivet->analyze(converter.event());
137  eventAttributes.clear();
138  }
139 
140  // Writes histograms to file and deletes the Rivet object. Does
141  // nothing if Rivet was not initialized.
142  void done() {
143  if ( !rivet ) return;
144  rivet->finalize();
145  rivet->writeData(filename);
146  delete rivet;
147  rivet = 0;
148  }
149 
150 private:
151 
152  // The main pythia object.
153  Pythia * pythia;
154 
155  // The name of the file where the histograms are dumped.
156  string filename;
157 
158  // Analyses with optional analysis parameters.
159  set<string> analyses;
160 
161  // The names of YODA files to preload.
162  vector<string> preloads;
163 
164  // The Rivet object.
165  Rivet::AnalysisHandler * rivet;
166 
167  // The HepMC converter.
168  Pythia8ToHepMC converter;
169 
170  // The Rivet run name.
171  string rname;
172 
173  // Ignore beams flag.
174  bool igBeam;
175 
176  // Dump period.
177  int nDump;
178 
179  // Optional alternative dumpfile name.
180  string dumpFile;
181 
182  // Additional event attributes of double type to send to Rivet.
183  map<const string, double> eventAttributes;
184 
185 };
186 
187 }
188 
189 #endif
void addAttribute(const string &name, double &attribute)
Add an attribute of double type.
Definition: HepMC3.h:416
GenEvent & event()
Get a reference to the current GenEvent.
Definition: HepMC2.h:464
The Event class holds all info on the generated event.
Definition: Event.h:453
void init()
Initialize Rivet. Will do nothing if Rivet was already initialized.
Definition: Pythia8Rivet.h:87
Event event
The event record for the complete event history.
Definition: Pythia.h:323
void addAnalysis(string ana)
Definition: Pythia8Rivet.h:64
~Pythia8Rivet()
Definition: Pythia8Rivet.h:60
void dump(int period, string fname="")
Definition: Pythia8Rivet.h:74
void done()
Definition: Pythia8Rivet.h:142
Definition: HepMC2.h:408
void addAttribute(const string &name, double val)
Add an attribute to the current event.
Definition: Pythia8Rivet.h:80
void operator()(Event &event)
Analyze the given event.
Definition: Pythia8Rivet.h:129
void operator()()
Definition: Pythia8Rivet.h:126
void ignoreBeams(bool flagIn)
Set "ignore beams" flag.
Definition: Pythia8Rivet.h:70
Pythia8Rivet(Pythia &pytin, string fname)
Definition: Pythia8Rivet.h:54
void addRunName(const string runname)
Add an (optional) run name for Rivet internal use.
Definition: Pythia8Rivet.h:84
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 fillNextEvent(Pythia &pythia)
Definition: HepMC2.h:444
void addPreload(string prel)
Add a YODA file pre-load pre-filled histogram from.
Definition: Pythia8Rivet.h:67
Definition: Pythia8Rivet.h:48
void initpath() const
Helper function to set analysis path.
Definition: Pythia8Rivet.h:118