Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:03:58

0001 // Pythia8Yoda.h is a part of the PYTHIA event generator.
0002 // Copyright (C) 2025 Torbjorn Sjostrand.
0003 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
0004 // Please respect the MCnet Guidelines, see GUIDELINES for details.
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 // Simplified interface to booking and writing of YODA histograms.
0018 // Remember to configure Pythia with YODA; --with-yoda.
0019 // YODA version 2 or greater is required.
0020 //
0021 // Usage: 1) Create a Pythia8Yoda object in your main program. 2) Use
0022 // it to book histograms, receive shared pointers to the histograms. 3) Fill
0023 // the histograms while you run, and 4) At the end of the analysis, write out
0024 // the histograms to a file.
0025 
0026 // Typedef of shared pointers to analysis objects.
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   // The constructor needs a prefix name, which will be put on the histogram
0039   // path, as well as an output file name.
0040   Pythia8Yoda(const string& anaNameIn = "/PYTHIA8/", const string& outputIn =
0041     "pythia") : anaName("/"+anaNameIn+"/"), outName(outputIn),
0042     finalized(false) { }
0043 
0044   // The destructor will write the histogram file if this has not
0045   // already been done.
0046   ~Pythia8Yoda() {
0047     if (!finalized) write();
0048   }
0049 
0050   // Write histograms.
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   // Read and return a named YODA object from a file.
0063   // Input arguments filename.yoda and full object path.
0064   template<typename T>
0065   static T read(const string& fName, const string& objPath) {
0066     // Read the file content.
0067     vector<YODA::AnalysisObject* > anaObjs;
0068     YODA::ReaderYODA::read(fName, anaObjs);
0069 
0070     // Find the object and convert if possible.
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     // Warning if object not found/converted.
0080     if (!(result.path() == objPath))
0081       cout << "Warning: " << objPath << " not found/incorrect type," << endl;
0082 
0083     // Clean up
0084     for (auto* obj : anaObjs) delete obj;
0085 
0086     return result;
0087   }
0088 
0089   // Book any YODA object and return a shared ptr.
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   // Write the most common use cases to give better compiler warnings and
0098   // easier use.
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   // Book a 2D histogram.
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   // Book a 3D histogram.
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   // Book a 1D profile.
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   // Book a 2D profile.
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