File indexing completed on 2025-09-16 09:03:58
0001
0002
0003
0004
0005 #include "Pythia8/PythiaStdlib.h"
0006
0007 #ifndef PYTHIA8YODA_H
0008 #define PYTHIA8YODA_H
0009
0010 #include "YODA/Histo.h"
0011 #include "YODA/Profile.h"
0012 #include "YODA/WriterYODA.h"
0013 #include "YODA/ReaderYODA.h"
0014
0015 namespace Pythia8 {
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 using AnaObjectPtr = shared_ptr<YODA::AnalysisObject>;
0028 using Histo1DPtr = shared_ptr<YODA::Histo1D>;
0029 using Histo2DPtr = shared_ptr<YODA::Histo2D>;
0030 using Histo3DPtr = shared_ptr<YODA::Histo3D>;
0031 using Profile1DPtr = shared_ptr<YODA::Profile1D>;
0032 using Profile2DPtr = shared_ptr<YODA::Profile2D>;
0033
0034 class Pythia8Yoda {
0035
0036 public:
0037
0038
0039
0040 Pythia8Yoda(const string& anaNameIn = "/PYTHIA8/", const string& outputIn =
0041 "pythia") : anaName("/"+anaNameIn+"/"), outName(outputIn),
0042 finalized(false) { }
0043
0044
0045
0046 ~Pythia8Yoda() {
0047 if (!finalized) write();
0048 }
0049
0050
0051 void write() {
0052 string purge = ".yoda";
0053 size_t pos = outName.find(purge);
0054 if (pos != std::string::npos)
0055 outName.replace(pos, purge.length(), "");
0056 std::cout << "Writing histograms to: " << outName << ".yoda" << std::endl;
0057 YODA::WriterYODA::write(outName+".yoda", anaObjects.begin(),
0058 anaObjects.end());
0059 finalized = true;
0060 }
0061
0062
0063
0064 template<typename T>
0065 static T read(const string& fName, const string& objPath) {
0066
0067 vector<YODA::AnalysisObject* > anaObjs;
0068 YODA::ReaderYODA::read(fName, anaObjs);
0069
0070
0071 T result;
0072 for (auto ao : anaObjs)
0073 if (ao->path() == objPath)
0074 if (T* castObj = dynamic_cast<T*>(ao)) {
0075 result = *castObj;
0076 break;
0077 }
0078
0079
0080 if (!(result.path() == objPath))
0081 cout << "Warning: " << objPath << " not found/incorrect type," << endl;
0082
0083
0084 for (auto* obj : anaObjs) delete obj;
0085
0086 return result;
0087 }
0088
0089
0090 template<typename T, typename... Args>
0091 shared_ptr<T> book(Args&&... args) {
0092 auto h = make_shared<T>(std::forward<Args>(args)...);
0093 anaObjects.push_back(h);
0094 return h;
0095 }
0096
0097
0098
0099 Histo1DPtr bookHisto1D(int nBins, double xMin, double xMax,
0100 const string& title) {
0101 return book<YODA::Histo1D>(nBins, xMin, xMax, anaName + title, title);
0102 }
0103
0104
0105 Histo2DPtr bookHisto2D(int nBinsX, double xMin, double xMax, int nBinsY,
0106 double yMin, double yMax, const std::string& title) {
0107 return book<YODA::Histo2D>(nBinsX, xMin, xMax, nBinsY, yMin, yMax,
0108 anaName + title, title);
0109 }
0110
0111
0112 Histo3DPtr bookHisto3D(int nBinsX, double xMin, double xMax, int nBinsY,
0113 double yMin, double yMax, int nBinsZ, double zMin, double zMax,
0114 const std::string& title) {
0115 return book<YODA::Histo3D>(nBinsX, xMin, xMax, nBinsY, yMin, yMax,
0116 nBinsZ, zMin, zMax, anaName + title, title);
0117 }
0118
0119
0120 Profile1DPtr bookProfile1D(int nBins, double xMin, double xMax,
0121 const std::string& title) {
0122 return book<YODA::Profile1D>(nBins, xMin, xMax, anaName + title, title);
0123 }
0124
0125
0126 Profile2DPtr bookProfile2D(int nBinsX, double xMin, double xMax, int nBinsY,
0127 double yMin, double yMax, const std::string& title) {
0128 return book<YODA::Profile2D>(nBinsX, xMin, xMax, nBinsY, yMin, yMax,
0129 anaName + title, title);
0130 }
0131
0132 private:
0133 string anaName;
0134 string outName;
0135 bool finalized;
0136 std::vector<AnaObjectPtr> anaObjects;
0137
0138 };
0139
0140 }
0141
0142 #endif