Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetScapeWriterFinalStateStream.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetScapeWriterFinalStateStream.cc
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 // Jetscape final state {hadrons,kartons} writer ascii class
16 // Based on JetScapeWriterStream.
17 // author: Raymond Ehlers <raymond.ehlers@cern.ch>, ORNL
18 
20 #include "JetScapeLogger.h"
21 #include "JetScapeXML.h"
22 
23 namespace Jetscape {
24 
25 // Register the modules with the base class
26 template <>
27 RegisterJetScapeModule<JetScapeWriterFinalStatePartonsStream<ofstream>>
28  JetScapeWriterFinalStatePartonsStream<ofstream>::regParton("JetScapeWriterFinalStatePartonsAscii");
29 template <>
30 RegisterJetScapeModule<JetScapeWriterFinalStateHadronsStream<ofstream>>
31  JetScapeWriterFinalStateHadronsStream<ofstream>::regHadron("JetScapeWriterFinalStateHadronsAscii");
32 template <>
33 RegisterJetScapeModule<JetScapeWriterFinalStatePartonsStream<ogzstream>>
34  JetScapeWriterFinalStatePartonsStream<ogzstream>::regPartonGZ("JetScapeWriterFinalStatePartonsAsciiGZ");
35 template <>
36 RegisterJetScapeModule<JetScapeWriterFinalStateHadronsStream<ogzstream>>
37  JetScapeWriterFinalStateHadronsStream<ogzstream>::regHadronGZ("JetScapeWriterFinalStateHadronsAsciiGZ");
38 
39 template <class T>
41  SetOutputFileName(m_file_name_out);
42 }
43 
45  VERBOSE(8);
46  if (GetActive())
47  Close();
48 }
49 
51  // Write the entire event all at once.
52 
53  // Optionally write pt-hat value to event header
54  std::string pt_hat_text = "";
55  int write_pthat = JetScapeXML::Instance()->GetElementInt({"write_pthat"});
56  if (write_pthat) {
57  pt_hat_text += "\tpt_hat\t";
58  pt_hat_text += std::to_string(GetHeader().GetPtHat());
59  }
60 
61  // First, write header
62  // NOTE: Needs consistent "\t" between all entries to simplify parsing later.
63  // NOTE: Could also add Npart, Ncoll, and TotalEntropy. See the original stream writer.
64  output_file << "#"
65  << "\t" << "Event\t" << GetCurrentEvent() + 1 // +1 to index the event count from 1
66  << "\t" << "weight\t" << std::setprecision(15) << GetHeader().GetEventWeight() << std::setprecision(6)
67  << "\t" << "EPangle\t" << (GetHeader().GetEventPlaneAngle() > -999 ? GetHeader().GetEventPlaneAngle() : 0)
68  << "\t" << "N_" << GetName() << "\t" << particles.size()
69  << pt_hat_text
70  << "\n";
71 
72  // Next, write the particles. Will contain either hadrons or partons based on the derived class.
73  unsigned int ipart = 0;
74  for (const auto & p : particles) {
75  auto particle = p.get();
76  output_file << ipart
77  << " " << particle->pid()
78  << " " << particle->pstat()
79  << " " << particle->e()
80  << " " << particle->px()
81  << " " << particle->py()
82  << " " << particle->pz()
83  << "\n";
84  ++ipart;
85  }
86 
87  // Cleanup to be ready for the next event.
88  particles.clear();
89 }
90 
91 template <class T> void JetScapeWriterFinalStateStream<T>::Init() {
92  if (GetActive()) {
93  // Capitalize name
94  std::string name = GetName();
95  name[0] = toupper(name[0]);
96  JSINFO << "JetScape Final State " << name << " Stream Writer initialized with output file = "
97  << GetOutputFileName();
98  output_file.open(GetOutputFileName().c_str());
99  // NOTE: This header will only be printed once at the beginning on the file.
100  output_file << "#"
101  // The specifics the version number. For consistency in parsing, the string
102  // will always be "v<number>"
103  << "\t" << "JETSCAPE_FINAL_STATE\t" << "v2"
104  << "\t" << "|" // As a delimiter
105  << "\t" << "N"
106  << "\t" << "pid"
107  << "\t" << "status"
108  << "\t" << "E"
109  << "\t" << "Px"
110  << "\t" << "Py"
111  << "\t" << "Pz"
112  << "\n";
113  }
114 }
115 
116 template <class T> void JetScapeWriterFinalStateStream<T>::Exec() {
117  // JSINFO<<"Run JetScapeWriterFinalStateStream<T>: Write event # "<<GetCurrentEvent()<<" ...";
118 
119  // if (GetActive())
120  // WriteEvent();
121 }
122 
123 template <class T>
124 void JetScapeWriterFinalStateStream<T>::Write(weak_ptr<PartonShower> ps) {
125  auto pShower = ps.lock();
126  if (!pShower)
127  return;
128 
129  auto finalStatePartons = pShower->GetFinalPartons();
130 
131  // Store final state partons.
132  for (const auto parton : finalStatePartons) {
133  particles.push_back(parton);
134  }
135 }
136 
137 template <class T> void JetScapeWriterFinalStateStream<T>::Write(weak_ptr<Hadron> h) {
138  auto hh = h.lock();
139  if (hh) {
140  particles.push_back(hh);
141  }
142 }
143 
144 template <class T> void JetScapeWriterFinalStateStream<T>::Close() {
145  // Write xsec output at the end.
146  // NOTE: Needs consistent "\t" between all entries to simplify parsing later.
147  output_file << "#" << "\t"
148  << "sigmaGen\t" << GetHeader().GetSigmaGen() << "\t"
149  << "sigmaErr\t" << GetHeader().GetSigmaErr() << "\n";
150  output_file.close();
151 }
152 
155 
156 #ifdef USE_GZIP
159 #endif
160 
161 } // end namespace Jetscape