|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <filesystem> |
| 4 | +#include <fairlogger/Logger.h> |
| 5 | + |
| 6 | +class GeneratorEPOS4 : public o2::eventgen::GeneratorHepMC |
| 7 | +{ |
| 8 | + public: |
| 9 | + GeneratorEPOS4() = default; |
| 10 | + ~GeneratorEPOS4() = default; |
| 11 | + |
| 12 | +}; |
| 13 | + |
| 14 | +// Next function takes the optns file as argument and edits the maximum number of events to be generated. |
| 15 | +// When used as an external generator it is important that the events passed with the -n flag are the same |
| 16 | +// or lower of the events set in the optns file, otherwise the generation will crash. That is why the .ini |
| 17 | +// example file contains the maximum integer available, assuming less events than that are generated in a real |
| 18 | +// life scenario. Unfortunately a larger number cannot be used at the moment since EPOS4 has a signed integer |
| 19 | +// type for the nfull parameter. Might be changed in the future. |
| 20 | +// When running locally, or on the GRID (not in hyperloop), the default parameters provided in the .ini file of the |
| 21 | +// external generation can be overwritten using the confKeyValues option (or similar depending on the tool used). |
| 22 | +FairGenerator* generateEPOS4(const std::string &name, const int& nEvents) |
| 23 | +{ |
| 24 | + // check if the file exists |
| 25 | + auto filename = gSystem->ExpandPathName(name.c_str()); |
| 26 | + if (!std::filesystem::exists(filename)) |
| 27 | + { |
| 28 | + LOG(fatal) << "Options file " << filename << " does not exist!"; |
| 29 | + exit(1); |
| 30 | + } |
| 31 | + // cache all the lines of the optns file and replace the number of events |
| 32 | + std::ifstream file(filename); |
| 33 | + std::string line; |
| 34 | + bool found = false; |
| 35 | + std::stringstream buffer; |
| 36 | + while (std::getline(file, line)) |
| 37 | + { |
| 38 | + if (line.find("nfull") != std::string::npos){ |
| 39 | + // replace the number of events |
| 40 | + found = true; |
| 41 | + line = "set nfull " + std::to_string(nEvents); |
| 42 | + } |
| 43 | + buffer << line << "\n"; |
| 44 | + } |
| 45 | + file.close(); |
| 46 | + // Write the updated content back to a file in the current directory |
| 47 | + std::ofstream outFile("cfg.optns"); |
| 48 | + outFile << buffer.str(); |
| 49 | + if (!found) |
| 50 | + { |
| 51 | + outFile << "set nfull " + std::to_string(nEvents); |
| 52 | + } |
| 53 | + outFile.close(); |
| 54 | + auto gen = new GeneratorEPOS4(); |
| 55 | + auto& param0 = o2::eventgen::GeneratorFileOrCmdParam::Instance(); |
| 56 | + auto& param = o2::eventgen::GeneratorHepMCParam::Instance(); |
| 57 | + auto& conf = o2::conf::SimConfig::Instance(); |
| 58 | + // setup the HepMC generator to run with automatic FIFOs |
| 59 | + gen->setup(param0, param, conf); |
| 60 | + return gen; |
| 61 | +} |
0 commit comments