Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:19:19

0001 /*******************************************************************************
0002  * Copyright (c) The JETSCAPE Collaboration, 2018
0003  *
0004  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
0005  *
0006  * For the list of contributors see AUTHORS.
0007  *
0008  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
0009  *
0010  * or via email to bugs.jetscape@gmail.com
0011  *
0012  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
0013  * See COPYING for details.
0014  ******************************************************************************/
0015 // Jetscape final state {hadrons,kartons} writer ascii class
0016 // Based on JetScapeWriterStream.
0017 // author: Raymond Ehlers <raymond.ehlers@cern.ch>, ORNL
0018 
0019 #include "JetScapeWriterFinalStateStream.h"
0020 #include "JetScapeLogger.h"
0021 #include "JetScapeXML.h"
0022 
0023 namespace Jetscape {
0024 
0025 // Register the modules with the base class
0026 template <>
0027 RegisterJetScapeModule<JetScapeWriterFinalStatePartonsStream<ofstream>>
0028     JetScapeWriterFinalStatePartonsStream<ofstream>::regParton("JetScapeWriterFinalStatePartonsAscii");
0029 template <>
0030 RegisterJetScapeModule<JetScapeWriterFinalStateHadronsStream<ofstream>>
0031     JetScapeWriterFinalStateHadronsStream<ofstream>::regHadron("JetScapeWriterFinalStateHadronsAscii");
0032 template <>
0033 RegisterJetScapeModule<JetScapeWriterFinalStatePartonsStream<ogzstream>>
0034     JetScapeWriterFinalStatePartonsStream<ogzstream>::regPartonGZ("JetScapeWriterFinalStatePartonsAsciiGZ");
0035 template <>
0036 RegisterJetScapeModule<JetScapeWriterFinalStateHadronsStream<ogzstream>>
0037     JetScapeWriterFinalStateHadronsStream<ogzstream>::regHadronGZ("JetScapeWriterFinalStateHadronsAsciiGZ");
0038 
0039 template <class T>
0040 JetScapeWriterFinalStateStream<T>::JetScapeWriterFinalStateStream(string m_file_name_out) {
0041   SetOutputFileName(m_file_name_out);
0042 }
0043 
0044 template <class T> JetScapeWriterFinalStateStream<T>::~JetScapeWriterFinalStateStream() {
0045   VERBOSE(8);
0046   if (GetActive())
0047     Close();
0048 }
0049 
0050 template <class T> void JetScapeWriterFinalStateStream<T>::WriteEvent() {
0051   // Write the entire event all at once.
0052 
0053   // Optionally write pt-hat value to event header
0054   std::string pt_hat_text = "";
0055   int write_pthat = JetScapeXML::Instance()->GetElementInt({"write_pthat"});
0056   if (write_pthat) {
0057     pt_hat_text += "\tpt_hat\t";
0058     pt_hat_text += std::to_string(GetHeader().GetPtHat());
0059   }
0060 
0061   // First, write header
0062   // NOTE: Needs consistent "\t" between all entries to simplify parsing later.
0063   // NOTE: Could also add Npart, Ncoll, and TotalEntropy. See the original stream writer.
0064   output_file << "#"
0065       << "\t" << "Event\t" << GetCurrentEvent() + 1  // +1 to index the event count from 1
0066       << "\t" << "weight\t" << std::setprecision(15) << GetHeader().GetEventWeight() << std::setprecision(6)
0067       << "\t" << "EPangle\t" << (GetHeader().GetEventPlaneAngle() > -999 ? GetHeader().GetEventPlaneAngle() : 0)
0068       << "\t" << "N_" << GetName() << "\t" << particles.size()
0069       << pt_hat_text
0070       <<  "\n";
0071 
0072   // Next, write the particles. Will contain either hadrons or partons based on the derived class.
0073   unsigned int ipart = 0;
0074   for (const auto & p : particles) {
0075     auto particle = p.get();
0076     output_file << ipart
0077         << " " << particle->pid()
0078         << " " << particle->pstat()
0079         << " " << particle->e()
0080         << " " << particle->px()
0081         << " " << particle->py()
0082         << " " << particle->pz()
0083         << "\n";
0084     ++ipart;
0085   }
0086 
0087   // Cleanup to be ready for the next event.
0088   particles.clear();
0089 }
0090 
0091 template <class T> void JetScapeWriterFinalStateStream<T>::Init() {
0092   if (GetActive()) {
0093     // Capitalize name
0094     std::string name = GetName();
0095     name[0] = toupper(name[0]);
0096     JSINFO << "JetScape Final State " << name << " Stream Writer initialized with output file = "
0097            << GetOutputFileName();
0098     output_file.open(GetOutputFileName().c_str());
0099     // NOTE: This header will only be printed once at the beginning on the file.
0100     output_file << "#"
0101         // The specifics the version number. For consistency in parsing, the string
0102         // will always be "v<number>"
0103         << "\t" << "JETSCAPE_FINAL_STATE\t" << "v2"
0104         << "\t" << "|"  // As a delimiter
0105         << "\t" << "N"
0106         << "\t" << "pid"
0107         << "\t" << "status"
0108         << "\t" << "E"
0109         << "\t" << "Px"
0110         << "\t" << "Py"
0111         << "\t" << "Pz"
0112         << "\n";
0113   }
0114 }
0115 
0116 template <class T> void JetScapeWriterFinalStateStream<T>::Exec() {
0117   // JSINFO<<"Run JetScapeWriterFinalStateStream<T>: Write event # "<<GetCurrentEvent()<<" ...";
0118 
0119   // if (GetActive())
0120   //   WriteEvent();
0121 }
0122 
0123 template <class T>
0124 void JetScapeWriterFinalStateStream<T>::Write(weak_ptr<PartonShower> ps) {
0125   auto pShower = ps.lock();
0126   if (!pShower)
0127     return;
0128 
0129   auto finalStatePartons = pShower->GetFinalPartons();
0130 
0131   // Store final state partons.
0132   for (const auto parton : finalStatePartons) {
0133       particles.push_back(parton);
0134   }
0135 }
0136 
0137 template <class T> void JetScapeWriterFinalStateStream<T>::Write(weak_ptr<Hadron> h) {
0138   auto hh = h.lock();
0139   if (hh) {
0140     particles.push_back(hh);
0141   }
0142 }
0143 
0144 template <class T> void JetScapeWriterFinalStateStream<T>::Close() {
0145     // Write xsec output at the end.
0146     // NOTE: Needs consistent "\t" between all entries to simplify parsing later.
0147     output_file << "#" << "\t"
0148         << "sigmaGen\t" << GetHeader().GetSigmaGen() << "\t"
0149         << "sigmaErr\t" << GetHeader().GetSigmaErr() << "\n";
0150     output_file.close();
0151 }
0152 
0153 template class JetScapeWriterFinalStatePartonsStream<ofstream>;
0154 template class JetScapeWriterFinalStateHadronsStream<ofstream>;
0155 
0156 #ifdef USE_GZIP
0157 template class JetScapeWriterFinalStatePartonsStream<ogzstream>;
0158 template class JetScapeWriterFinalStateHadronsStream<ogzstream>;
0159 #endif
0160 
0161 } // end namespace Jetscape