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 declareProperty( "InputCard", m_inputcard = "foo.optns" );
115 m_events = 0; // current event number (counted by interface)
116}
117
118
119namespace fs = std::filesystem;
120std::string Epos4::create_file(const std::string& filein) {
121
122 if (filein.size() < 6 || filein.compare(filein.size() - 6, 6, ".optns") != 0) {
123 std::cerr << "Error: Input file name: " << filein << " does not end with \".optns\"\n";
124 return "";
125 }
126
127 fs::path source = filein;
128 fs::path destination = fs::current_path() / fs::path(std::string("z-") + source.filename().string());
129
130
131 std::ifstream src(source); // Open in text mode
132 std::ofstream dst(destination); // Open in text mode
133
134 if (!src) {
135 std::cerr << "Error: Cannot open source file: " << source << "\n";
136 return "";
137 }
138
139 if (!dst) {
140 std::cerr << "Error: Cannot create destination file: " << destination << "\n";
141 return "";
142 }
143
144 std::string line;
145 while (std::getline(src, line)) {
146 dst << line << '\n';
147 }
148 dst << "set ihepmc 1" << '\n';
149
150 std::string file = source.filename().string().size() > 6 ? source.filename().string().substr(0, source.filename().string().size() - 6 ) : "";
151
152 std::string num = "0";
153 std::string one = file;
154 if (num != "0") {
155 one = file + "-" + num;
156 }
157
158 std::string clinput = "z-" + one + ".clinput";
159 std::ofstream ofile(clinput);
160
161 std::string CHK = std::getenv("CHK") ? std::getenv("CHK") : (std::cerr << "Warning: CHK not set\n", "");
162 if (m_seeds.size() < 2) std::cerr << "Warning: m_seeds should contain at least 2 elements\n";
163 std::string seedi = m_seeds.size() < 1 ? "111111111" : std::to_string(m_seeds.at(0));// Should be something like "222222222"; //WARNING: SEEDS SHOUD EXIST HERE!
164 std::string seedj = m_seeds.size() < 2 ? "222222222" : std::to_string(m_seeds.at(1));// Should be something like "111111111";
165 std::string rootcproot = "nono";
166 std::string system = "i";
167 std::string ext1 = "-";
168 std::string ext3 = "-";
169 std::string ext4 = "-";
170 std::string gefac = "1";
171 std::string EPO = std::getenv("EPO") ? std::getenv("EPO") : (std::cerr << "Warning: EPO not set\n", "");
172 std::string SRCEXT = std::getenv("SRCEXT") ? std::getenv("SRCEXT") : (std::cerr << "Warning: SRCEXT not set\n", "");
173 std::string HTO = std::getenv("HTO") ? std::getenv("HTO") : (std::cerr << "Warning: HTO not set\n", "");
174 std::string SRC = std::getenv("SRC") ? std::getenv("SRC") : (std::cerr << "Warning: SRC not set\n", "");
175 std::string CONF = std::getenv("CONF") ? std::getenv("CONF") : (std::cerr << "Warning: CONF not set\n", "");
176 std::string OPT = std::getenv("OPT") ? std::getenv("OPT") : (std::cerr << "Warning: OPT not set\n", "");
177 std::string OPX = "";
178 if (std::string(OPT) == "./") {
179 OPX = std::filesystem::current_path().string() + "/";
180 } else {
181 OPX = std::move(OPT);
182 }
183
184 ofile << "!fname mtr " << CHK << "z-" << one << ".mtr\n";
185 ofile << "set seedj " << seedj << " set seedi " << seedi << "\n";
186 ofile << "echo off\n";
187 ofile << "rootcproot " << rootcproot << "\n";
188 ofile << "system " << system << "\n";
189 ofile << "ext1 " << ext1 << "\n";
190 ofile << "ext3 " << ext3 << "\n";
191 ofile << "ext4 " << ext4 << "\n";
192 ofile << "set gefac " << gefac << "\n";
193 ofile << "!!!beginoptns spherio !No longer used.\n";
194 ofile << "!!!... !See version < 3118 \n";
195 ofile << "!!!endoptns spherio !if needed again \n";
196 ofile << "fname pathep " << EPO << "\n";
197 ofile << "!-------HQ--------\n";
198 ofile << "fname user1 " << SRCEXT << "/HQt/\n";
199 ofile << "fname user2 " << HTO << "z-" << one << ".hq\n";
200 ofile << "fname user3 " << SRCEXT << "/URt/\n";
201 ofile << "!-------HQ END--------\n";
202 ofile << "fname pathpdf " << SRC << "/TPt/\n";
203 ofile << "fname histo " << HTO << "z-" << one << ".histo\n";
204 ofile << "fname check " << CHK << "z-" << one << ".check\n";
205 ofile << "fname copy " << CHK << "z-" << one << ".copy\n";
206 ofile << "fname log " << CHK << "z-" << one << ".log\n";
207 ofile << "fname data " << CHK << "z-" << one << ".data\n";
208 ofile << "fname initl " << SRC << "/KWt/aa.i\n";
209 ofile << "fname inidi " << SRC << "/TPt/di.i\n";
210 ofile << "fname inidr " << SRC << "/KWt/dr.i\n";
211 ofile << "fname iniev " << SRC << "/KWt/ev.i\n";
212 ofile << "fname inirj " << SRC << "/KWt/rj.i\n";
213 ofile << "fname inics " << SRC << "/TPt/cs.i\n";
214 ofile << "fname inigrv " << SRC << "/grv.i\n";
215 ofile << "fname partab " << SRCEXT << "/YK/ptl6.data\n";
216 ofile << "fname dectab " << SRCEXT << "/YK/dky6.data\n";
217 ofile << "fname hpf " << SRCEXT << "/UR/tables.dat \n";
218 ofile << "!fqgsjet dat " << EPO << SRC << "qgsjet/qgsjet.dat !No longer used.\n";
219 ofile << "!fqgsjet ncs " << EPO << SRC << "qgsjet/qgsjet.ncs !No longer used.\n";
220 ofile << "!fqgsjetII dat " << EPO << SRC << "qgsjetII/qgsdat-II-03 !No longer used.\n";
221 ofile << "!fqgsjetII ncs " << EPO << SRC << "qgsjetII/sectnu-II-03 !No longer used.\n";
222 ofile << "nodecay 1220\n";
223 ofile << "nodecay -1220\n";
224 ofile << "nodecay 120\n";
225 ofile << "nodecay -120\n";
226 ofile << "nodecay 130\n";
227 ofile << "nodecay -130\n";
228 ofile << "nodecay -20\n";
229 ofile << "nodecay 14\n";
230 ofile << "nodecay -14\n";
231 ofile << "nodecay 16\n";
232 ofile << "nodecay -16\n";
233 ofile << "echo on\n";
234 ofile << "input " << CONF << "/parbf.i\n";
235 ofile << "input " << OPX << "z-" << one << ".optns\n";
236 ofile << "input " << SRC << "/KWn/paraf.i\n";
237 ofile << "input " << CONF << "/partx.i\n";
238 ofile << "runprogram\n";
239 ofile << "stopprogram\n";
240
241 ofile.close();
242 return clinput;
243}
244
245// ----------------------------------------------------------------------
247{
248 p_rndmEngine = getRandomEngineDuringInitialize(epos_rndm_stream, m_randomSeed, m_dsid); // NOT THREAD-SAFE
249 epos_rndm_stream = "EPOS4";
250 m_events = 0;
251
252 writer = std::make_shared<HepMC3::WriterEPOS>("foo");
253 auto x = this->create_file(m_inputcard);
254 set_job_common(x.c_str());
256 checkTime();
257 eposStart();
261 int currentnumberOfEnergyValues = numberOfEnergyValues();
263 for (int k=1; k<=currentnumberOfEnergyValues; k++) {
268 }
269
270 return StatusCode::SUCCESS;
271}
272
273// ----------------------------------------------------------------------
275{
276 //Re-seed the random number stream
277 long seeds[7];
278 const EventContext& ctx = Gaudi::Hive::currentContext();
279 ATHRNG::calculateSeedsMC21(seeds, epos_rndm_stream, ctx.eventID().event_number(), m_dsid, m_randomSeed);
280 p_rndmEngine->setSeeds(seeds, 0); // NOT THREAD-SAFE
281 // save the random number seeds in the event
282 const long *s = p_rndmEngine->getSeeds();
283 m_seeds.assign({s[0], s[1]});
284 ++m_events;
287// auto e = std::dynamic_pointer_cast<HepMC3::WriterEPOS>(writer)->current_event();
288 return StatusCode::SUCCESS;
289}
290
291// ----------------------------------------------------------------------
293{
294 ATH_MSG_INFO("EPOS4 finalizing.");
295 ATH_MSG_INFO("MetaData: generator = Epos4 .");
300 eposEnd();
302 return StatusCode::SUCCESS;
303}
304
305// ----------------------------------------------------------------------
306StatusCode Epos4::fillEvt( HepMC::GenEvent* evt )
307{
308 auto e = std::dynamic_pointer_cast<HepMC3::WriterEPOS>(writer)->current_event();
310 *evt = e;
311 return StatusCode::SUCCESS;
312}
313
#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 genFinalize() override
For finalising the generator, if required.
Definition Epos4.cxx:292
virtual StatusCode genInitialize() override
For initializing the generator, if required.
Definition Epos4.cxx:246
virtual StatusCode callGenerator() override
For calling the generator on each iteration of the event loop.
Definition Epos4.cxx:274
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:306
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[])