ATLAS Offline Software
Loading...
Searching...
No Matches
Epos4.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5// ----------------------------------------------------------------------
6// Generators/Epos4.cxx
7//
8//
9// AuthorList:
10// Andrii Verbytskyi
11// Paulina Majchrzak
12// ----------------------------------------------------------------------
13#include "Epos4.h"
15#include "CLHEP/Random/RandFlat.h"
16#include "GaudiKernel/MsgStream.h"
17
18#include "AtlasHepMC/GenEvent.h"
19#include "AtlasHepMC/HeavyIon.h"
21
22#include "HepMC3/GenEvent.h"
23#include "HepMC3/Writer.h"
24#include "HepMC3/Print.h"
25#include "HepMC3/GenEvent.h"
26
27#include <cstdlib> //for std::getenv
28#include <cstring> //for strlen, memcpy
29#include <filesystem>
30#include <fstream> //for ofstream, ifstream
31#include <iostream>
32#include <memory> //for shared_ptr, dynamic_pointer_cast
33
34// Match the Fortran COMMON block layout
35
37 char fnjob[1000]; // Fortran CHARACTER*1000
38 int nfnjob; // Fortran INTEGER
39};
40
41// Declare the Fortran COMMON block symbol
42extern struct jobfnametype jobfname_;
43// C function to set values in the COMMON block
44void set_job_common(const char *filename) {
45 size_t len = std::strlen(filename);
46 if (len > 1000)
47 len = 1000;
48 // Copy filename and pad with spaces
49 std::memcpy(jobfname_.fnjob, filename, len);
50 for (size_t i = len; i < 1000; ++i) {
51 jobfname_.fnjob[i] = ' ';
52 }
53 jobfname_.nfnjob = (int)len;
54}
55
56namespace {
57static std::string epos_rndm_stream = "EPOS4_INIT";
58static CLHEP::HepRandomEngine *p_rndmEngine{};
59} // namespace
60
61extern std::shared_ptr<HepMC3::Writer> writer;
62namespace HepMC3 {
63class WriterEPOS : public Writer {
64public:
65 WriterEPOS([[maybe_unused]] const std::string &filename,
66 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
67 set_run_info(std::move(run));
68 }
69 WriterEPOS([[maybe_unused]] std::ostream &stream,
70 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
71 set_run_info(std::move(run));
72 }
73 WriterEPOS([[maybe_unused]] std::shared_ptr<std::ostream> s_stream,
74 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>()) {
75 set_run_info(std::move(run));
76 }
78 void write_event(const GenEvent &evt) override { m_event = evt; };
79 const GenEvent &current_event() const { return m_event; }
80 bool failed() override { return false; };
81 void close() override {}
82
83private:
84 GenEvent m_event;
85};
86} // namespace HepMC3
87
88void checkTime();
91void eposEnd();
92void eposStart();
95void listParticles(int &);
103int setEnergyIndex(int &);
107
108// ----------------------------------------------------------------------
109Epos4::Epos4(const std::string &name, ISvcLocator *pSvcLocator)
110 : GenModule(name, pSvcLocator) {
111
112 epos_rndm_stream = "EPOS4_INIT";
113 declareProperty("BeamMomentum", m_beamMomentum = -6500.0); // GeV
114 declareProperty("TargetMomentum", m_targetMomentum = 6500.0);
115 declareProperty("InputCard", m_inputcard = "foo.optns");
116 m_events = 0; // current event number (counted by interface)
117}
118
119namespace fs = std::filesystem;
120std::string Epos4::create_file(const std::string &filein) {
121
122 if (filein.size() < 6 ||
123 filein.compare(filein.size() - 6, 6, ".optns") != 0) {
124 ATH_MSG_ERROR(" Input file name: " << filein
125 << " does not end with \".optns\"\n");
126 return "";
127 }
128
129 fs::path source = filein;
130 fs::path destination =
131 fs::current_path() /
132 fs::path(std::string("z-") + source.filename().string());
133
134 std::ifstream src(source); // Open in text mode
135 std::ofstream dst(destination); // Open in text mode
136
137 if (!src) {
138 ATH_MSG_ERROR(" Cannot open source file: " << source << "\n");
139 return "";
140 }
141
142 if (!dst) {
143 ATH_MSG_ERROR(" Cannot create destination file: " << destination << "\n");
144 return "";
145 }
146
147 std::string line;
148 while (std::getline(src, line)) {
149 dst << line << '\n';
150 }
151 dst << "set ihepmc 1" << '\n';
152
153 std::string file = source.filename().string().size() > 6
154 ? source.filename().string().substr(
155 0, source.filename().string().size() - 6)
156 : "";
157
158 std::string num = "0";
159 std::string one = file;
160 if (num != "0") {
161 one = file + "-" + num;
162 }
163
164 std::string clinput = "z-" + one + ".clinput";
165 std::ofstream ofile(clinput);
166
167 std::string CHK = std::getenv("CHK")
168 ? std::getenv("CHK")
169 : (ATH_MSG_WARNING(" CHK not set\n"), "");
170 if (m_seeds.size() < 2)
171 ATH_MSG_WARNING(" m_seeds should contain at least 2 elements\n");
172 std::string seedi = m_seeds.size() < 1
173 ? "111111111"
174 : std::to_string(m_seeds.at(
175 0)); // Should be something like "222222222";
176 // //WARNING: SEEDS SHOUD EXIST HERE!
177 std::string seedj = m_seeds.size() < 2
178 ? "222222222"
179 : std::to_string(m_seeds.at(
180 1)); // Should be something like "111111111";
181 std::string rootcproot = "nono";
182 std::string system = "i";
183 std::string ext1 = "-";
184 std::string ext3 = "-";
185 std::string ext4 = "-";
186 std::string gefac = "1";
187 auto safeGetenv = [this](const char *envName) -> std::string {
188 const char *env = std::getenv(envName);
189 if (env)
190 return env;
191 ATH_MSG_WARNING(envName << " not set.");
192 return {};
193 };
194 std::string EPO = safeGetenv("EPO");
195 std::string SRCEXT = safeGetenv("SRCEXT");
196 std::string HTO = safeGetenv("HTO");
197 std::string SRC = safeGetenv("SRC");
198 std::string CONF = safeGetenv("CONF");
199 std::string OPT = safeGetenv("OPT");
200 std::string OPX = "";
201 if (std::string(OPT) == "./") {
202 OPX = std::filesystem::current_path().string() + "/";
203 } else {
204 OPX = std::move(OPT);
205 }
206
207 ofile << "!fname mtr " << CHK << "z-" << one << ".mtr\n";
208 ofile << "set seedj " << seedj << " set seedi " << seedi << "\n";
209 ofile << "echo off\n";
210 ofile << "rootcproot " << rootcproot << "\n";
211 ofile << "system " << system << "\n";
212 ofile << "ext1 " << ext1 << "\n";
213 ofile << "ext3 " << ext3 << "\n";
214 ofile << "ext4 " << ext4 << "\n";
215 ofile << "set gefac " << gefac << "\n";
216 ofile << "!!!beginoptns spherio !No longer used.\n";
217 ofile << "!!!... !See version < 3118 \n";
218 ofile << "!!!endoptns spherio !if needed again \n";
219 ofile << "fname pathep " << EPO << "\n";
220 ofile << "!-------HQ--------\n";
221 ofile << "fname user1 " << SRCEXT << "/HQt/\n";
222 ofile << "fname user2 " << HTO << "z-" << one << ".hq\n";
223 ofile << "fname user3 " << SRCEXT << "/URt/\n";
224 ofile << "!-------HQ END--------\n";
225 ofile << "fname pathpdf " << SRC << "/TPt/\n";
226 ofile << "fname histo " << HTO << "z-" << one << ".histo\n";
227 ofile << "fname check " << CHK << "z-" << one << ".check\n";
228 ofile << "fname copy " << CHK << "z-" << one << ".copy\n";
229 ofile << "fname log " << CHK << "z-" << one << ".log\n";
230 ofile << "fname data " << CHK << "z-" << one << ".data\n";
231 ofile << "fname initl " << SRC << "/KWt/aa.i\n";
232 ofile << "fname inidi " << SRC << "/TPt/di.i\n";
233 ofile << "fname inidr " << SRC << "/KWt/dr.i\n";
234 ofile << "fname iniev " << SRC << "/KWt/ev.i\n";
235 ofile << "fname inirj " << SRC << "/KWt/rj.i\n";
236 ofile << "fname inics " << SRC << "/TPt/cs.i\n";
237 ofile << "fname inigrv " << SRC << "/grv.i\n";
238 ofile << "fname partab " << SRCEXT << "/YK/ptl6.data\n";
239 ofile << "fname dectab " << SRCEXT << "/YK/dky6.data\n";
240 ofile << "fname hpf " << SRCEXT << "/UR/tables.dat \n";
241 ofile << "!fqgsjet dat " << EPO << SRC
242 << "qgsjet/qgsjet.dat !No longer used.\n";
243 ofile << "!fqgsjet ncs " << EPO << SRC
244 << "qgsjet/qgsjet.ncs !No longer used.\n";
245 ofile << "!fqgsjetII dat " << EPO << SRC
246 << "qgsjetII/qgsdat-II-03 !No longer used.\n";
247 ofile << "!fqgsjetII ncs " << EPO << SRC
248 << "qgsjetII/sectnu-II-03 !No longer used.\n";
249 ofile << "nodecay 1220\n";
250 ofile << "nodecay -1220\n";
251 ofile << "nodecay 120\n";
252 ofile << "nodecay -120\n";
253 ofile << "nodecay 130\n";
254 ofile << "nodecay -130\n";
255 ofile << "nodecay -20\n";
256 ofile << "nodecay 14\n";
257 ofile << "nodecay -14\n";
258 ofile << "nodecay 16\n";
259 ofile << "nodecay -16\n";
260 ofile << "echo on\n";
261 ofile << "input " << CONF << "/parbf.i\n";
262 ofile << "input " << OPX << "z-" << one << ".optns\n";
263 ofile << "input " << SRC << "/KWn/paraf.i\n";
264 ofile << "input " << CONF << "/partx.i\n";
265 ofile << "runprogram\n";
266 ofile << "stopprogram\n";
267
268 ofile.close();
269 return clinput;
270}
271
272// ----------------------------------------------------------------------
274 p_rndmEngine = getRandomEngineDuringInitialize(epos_rndm_stream, m_randomSeed,
275 m_dsid); // NOT THREAD-SAFE
276 epos_rndm_stream = "EPOS4";
277 m_events = 0;
278
279 writer = std::make_shared<HepMC3::WriterEPOS>("foo");
280 const std::string & x = this->create_file(m_inputcard);
281 set_job_common(x.c_str());
283 checkTime();
284 eposStart();
288 int currentnumberOfEnergyValues = numberOfEnergyValues();
290 for (int k = 1; k <= currentnumberOfEnergyValues; k++) {
295 }
296
297 return StatusCode::SUCCESS;
298}
299
300// ----------------------------------------------------------------------
302 // Re-seed the random number stream
303 long seeds[7];
304 const EventContext &ctx = Gaudi::Hive::currentContext();
305 ATHRNG::calculateSeedsMC21(seeds, epos_rndm_stream,
306 ctx.eventID().event_number(), m_dsid,
308 p_rndmEngine->setSeeds(seeds, 0); // NOT THREAD-SAFE
309 // save the random number seeds in the event
310 const long *s = p_rndmEngine->getSeeds();
311 m_seeds.assign({s[0], s[1]});
312 ++m_events;
315 // auto e =
316 // std::dynamic_pointer_cast<HepMC3::WriterEPOS>(writer)->current_event();
317 return StatusCode::SUCCESS;
318}
319
320// ----------------------------------------------------------------------
321StatusCode Epos4::genFinalize() {
322 ATH_MSG_INFO("EPOS4 finalizing.");
323 ATH_MSG_INFO("MetaData: generator = Epos4 .");
328 eposEnd();
330 return StatusCode::SUCCESS;
331}
332
333// ----------------------------------------------------------------------
334StatusCode Epos4::fillEvt(HepMC::GenEvent *evt) {
335 auto e =
336 std::dynamic_pointer_cast<HepMC3::WriterEPOS>(writer)->current_event();
338
339 e.set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
340 HepMC3::Print::content(e);
341
342 *evt = e;
343 return StatusCode::SUCCESS;
344}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
void checkTime()
void eposEnd()
int numberOfEnergyValues()
void finalizeSimulation()
std::shared_ptr< HepMC3::Writer > writer
int numberOfEvents()
void initializeElectronProtonPart()
void showMemoryAtStart()
int setEnergyIndex(int &)
void set_job_common(const char *filename)
Definition Epos4.cxx:44
void readInputFile()
void listParticles(int &)
void initializeEventCounters()
struct jobfnametype jobfname_
void generateEposEvent(int &)
void defineStorageSettings()
void initializeEpos()
void initializeHeavyQuarkPart()
void showMemoryAtEnd()
void rewindInputFile()
void eposStart()
void writeStatistics()
static Double_t fs
#define x
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
virtual StatusCode genFinalize() override
For finalising the generator, if required.
Definition Epos4.cxx:321
virtual StatusCode genInitialize() override
For initializing the generator, if required.
Definition Epos4.cxx:273
virtual StatusCode callGenerator() override
For calling the generator on each iteration of the event loop.
Definition Epos4.cxx:301
double m_beamMomentum
Definition Epos4.h:39
std::string m_inputcard
Definition Epos4.h:33
double m_targetMomentum
Definition Epos4.h:40
std::string create_file(const std::string &filein)
Definition Epos4.cxx:120
IntegerProperty m_dsid
Definition Epos4.h:43
Epos4(const std::string &name, ISvcLocator *pSvcLocator)
Definition Epos4.cxx:109
int m_events
Definition Epos4.h:36
std::vector< long int > m_seeds
Definition Epos4.h:45
virtual StatusCode fillEvt(HepMC::GenEvent *evt) override
For filling the HepMC event object.
Definition Epos4.cxx:334
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenModule.cxx:14
CLHEP::HepRandomEngine * getRandomEngineDuringInitialize(const std::string &streamName, unsigned long int randomSeedOffset, unsigned int conditionsRun=1, unsigned int lbn=1) const
Definition GenModule.cxx:53
IntegerProperty m_randomSeed
Seed for random number engine.
Definition GenModule.h:84
WriterEPOS(std::shared_ptr< std::ostream > s_stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Definition Epos4.cxx:73
const GenEvent & current_event() const
Definition Epos4.cxx:79
void write_event(const GenEvent &evt) override
Definition Epos4.cxx:78
void close() override
Definition Epos4.cxx:81
WriterEPOS(std::ostream &stream, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Definition Epos4.cxx:69
WriterEPOS(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Definition Epos4.cxx:65
bool failed() override
Definition Epos4.cxx:80
GenEvent m_event
Definition Epos4.cxx:84
void calculateSeedsMC21(long *seeds, const std::string &algName, uint64_t ev, uint64_t run, uint64_t offset=0)
Set the random seed using a string (e.g.
char fnjob[1000]
Definition Epos4.cxx:37
TFile * file
int run(int argc, char *argv[])