PYTHIA  8.313
Pythia8Yoda.h
1 // Pythia8Yoda.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2025 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 #include "Pythia8/PythiaStdlib.h"
6 
7 #ifndef PYTHIA8YODA_H
8 #define PYTHIA8YODA_H
9 
10 #include "YODA/Histo.h"
11 #include "YODA/Profile.h"
12 #include "YODA/WriterYODA.h"
13 #include "YODA/ReaderYODA.h"
14 
15 namespace Pythia8 {
16 
17 // Simplified interface to booking and writing of YODA histograms.
18 // Remember to configure Pythia with YODA; --with-yoda.
19 // YODA version 2 or greater is required.
20 //
21 // Usage: 1) Create a Pythia8Yoda object in your main program. 2) Use
22 // it to book histograms, receive shared pointers to the histograms. 3) Fill
23 // the histograms while you run, and 4) At the end of the analysis, write out
24 // the histograms to a file.
25 
26 // Typedef of shared pointers to analysis objects.
27 using AnaObjectPtr = shared_ptr<YODA::AnalysisObject>;
28 using Histo1DPtr = shared_ptr<YODA::Histo1D>;
29 using Histo2DPtr = shared_ptr<YODA::Histo2D>;
30 using Histo3DPtr = shared_ptr<YODA::Histo3D>;
31 using Profile1DPtr = shared_ptr<YODA::Profile1D>;
32 using Profile2DPtr = shared_ptr<YODA::Profile2D>;
33 
34 class Pythia8Yoda {
35 
36 public:
37 
38  // The constructor needs a prefix name, which will be put on the histogram
39  // path, as well as an output file name.
40  Pythia8Yoda(const string& anaNameIn = "/PYTHIA8/", const string& outputIn =
41  "pythia") : anaName("/"+anaNameIn+"/"), outName(outputIn),
42  finalized(false) { }
43 
44  // The destructor will write the histogram file if this has not
45  // already been done.
47  if (!finalized) write();
48  }
49 
50  // Write histograms.
51  void write() {
52  string purge = ".yoda";
53  size_t pos = outName.find(purge);
54  if (pos != std::string::npos)
55  outName.replace(pos, purge.length(), "");
56  std::cout << "Writing histograms to: " << outName << ".yoda" << std::endl;
57  YODA::WriterYODA::write(outName+".yoda", anaObjects.begin(),
58  anaObjects.end());
59  finalized = true;
60  }
61 
62  // Read and return a named YODA object from a file.
63  // Input arguments filename.yoda and full object path.
64  template<typename T>
65  static T read(const string& fName, const string& objPath) {
66  // Read the file content.
67  vector<YODA::AnalysisObject* > anaObjs;
68  YODA::ReaderYODA::read(fName, anaObjs);
69 
70  // Find the object and convert if possible.
71  T result;
72  for (auto ao : anaObjs)
73  if (ao->path() == objPath)
74  if (T* castObj = dynamic_cast<T*>(ao)) {
75  result = *castObj;
76  break;
77  }
78 
79  // Warning if object not found/converted.
80  if (!(result.path() == objPath))
81  cout << "Warning: " << objPath << " not found/incorrect type," << endl;
82 
83  // Clean up
84  for (auto* obj : anaObjs) delete obj;
85 
86  return result;
87  }
88 
89  // Book any YODA object and return a shared ptr.
90  template<typename T, typename... Args>
91  shared_ptr<T> book(Args&&... args) {
92  auto h = make_shared<T>(std::forward<Args>(args)...);
93  anaObjects.push_back(h);
94  return h;
95  }
96 
97  // Write the most common use cases to give better compiler warnings and
98  // easier use.
99  Histo1DPtr bookHisto1D(int nBins, double xMin, double xMax,
100  const string& title) {
101  return book<YODA::Histo1D>(nBins, xMin, xMax, anaName + title, title);
102  }
103 
104  // Book a 2D histogram.
105  Histo2DPtr bookHisto2D(int nBinsX, double xMin, double xMax, int nBinsY,
106  double yMin, double yMax, const std::string& title) {
107  return book<YODA::Histo2D>(nBinsX, xMin, xMax, nBinsY, yMin, yMax,
108  anaName + title, title);
109  }
110 
111  // Book a 3D histogram.
112  Histo3DPtr bookHisto3D(int nBinsX, double xMin, double xMax, int nBinsY,
113  double yMin, double yMax, int nBinsZ, double zMin, double zMax,
114  const std::string& title) {
115  return book<YODA::Histo3D>(nBinsX, xMin, xMax, nBinsY, yMin, yMax,
116  nBinsZ, zMin, zMax, anaName + title, title);
117  }
118 
119  // Book a 1D profile.
120  Profile1DPtr bookProfile1D(int nBins, double xMin, double xMax,
121  const std::string& title) {
122  return book<YODA::Profile1D>(nBins, xMin, xMax, anaName + title, title);
123  }
124 
125  // Book a 2D profile.
126  Profile2DPtr bookProfile2D(int nBinsX, double xMin, double xMax, int nBinsY,
127  double yMin, double yMax, const std::string& title) {
128  return book<YODA::Profile2D>(nBinsX, xMin, xMax, nBinsY, yMin, yMax,
129  anaName + title, title);
130  }
131 
132 private:
133  string anaName;
134  string outName;
135  bool finalized;
136  std::vector<AnaObjectPtr> anaObjects;
137 
138 };
139 
140 }
141 
142 #endif
void write()
Write histograms.
Definition: Pythia8Yoda.h:51
Profile2DPtr bookProfile2D(int nBinsX, double xMin, double xMax, int nBinsY, double yMin, double yMax, const std::string &title)
Book a 2D profile.
Definition: Pythia8Yoda.h:126
Histo3DPtr bookHisto3D(int nBinsX, double xMin, double xMax, int nBinsY, double yMin, double yMax, int nBinsZ, double zMin, double zMax, const std::string &title)
Book a 3D histogram.
Definition: Pythia8Yoda.h:112
~Pythia8Yoda()
Definition: Pythia8Yoda.h:46
Histo2DPtr bookHisto2D(int nBinsX, double xMin, double xMax, int nBinsY, double yMin, double yMax, const std::string &title)
Book a 2D histogram.
Definition: Pythia8Yoda.h:105
static T read(const string &fName, const string &objPath)
Definition: Pythia8Yoda.h:65
shared_ptr< T > book(Args &&...args)
Book any YODA object and return a shared ptr.
Definition: Pythia8Yoda.h:91
Histo1DPtr bookHisto1D(int nBins, double xMin, double xMax, const string &title)
Definition: Pythia8Yoda.h:99
Definition: Pythia8Yoda.h:34
Profile1DPtr bookProfile1D(int nBins, double xMin, double xMax, const std::string &title)
Book a 1D profile.
Definition: Pythia8Yoda.h:120
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
Pythia8Yoda(const string &anaNameIn="/PYTHIA8/", const string &outputIn="pythia")
Definition: Pythia8Yoda.h:40
shared_ptr< YODA::AnalysisObject > AnaObjectPtr
Typedef of shared pointers to analysis objects.
Definition: Pythia8Yoda.h:27