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