14#include "ThePEG/Repository/EventGenerator.h"
15#include "ThePEG/Repository/Repository.h"
16#include "ThePEG/Persistency/PersistentIStream.h"
17#include "ThePEG/Utilities/DynamicLoader.h"
18#include "ThePEG/Utilities/Debug.h"
19#include "ThePEG/EventRecord/Event.h"
20#include "ThePEG/EventRecord/SubProcess.h"
21#include "ThePEG/Handlers/XComb.h"
22#include "ThePEG/Handlers/EventHandler.h"
23#include "ThePEG/PDF/PartonExtractor.h"
24#include "ThePEG/PDF/PDF.h"
26#include "Herwig/API/HerwigAPI.h"
27#include "Herwig/Utilities/HerwigStrategy.h"
35#include <system_error>
79 const long* seeds = engine->getSeeds();
84 int32_t combined_seed = std::abs(seeds[0] * seeds[1]);
86 std::ostringstream ss_seed;
87 ss_seed << combined_seed;
93 ATH_MSG_INFO(
"Using the random number seed " + ss_seed.str() +
" provided by athena");
94 m_api.seed(combined_seed);
99 ThePEG::Repository::exitOnError() = 1;
102 const char* datapath = getenv(
"DATAPATH" );
103 std::vector<std::string> datapaths;
105 std::string datapath_str(datapath);
106 for (
auto part : std::views::split(datapath_str,
':')) {
107 datapaths.emplace_back(part.begin(), part.end());
110 for(
const std::string& p : datapaths ) {
111 ThePEG::Repository::appendReadDir( p );
113 const char* ldpath = getenv(
"LD_LIBRARY_PATH" );
114 std::vector<std::string> ldpaths;
116 std::string ldpath_str(ldpath);
117 for (
auto part : ldpath_str | std::views::split(
':')) {
118 ldpaths.emplace_back(std::string(part.begin(), part.end()));
121 for(
const std::string& p : ldpaths ) {
122 ThePEG::DynamicLoader::appendPath( p );
125 ATH_MSG_DEBUG(
"Num of library search paths = " << ThePEG::DynamicLoader::allPaths().
size());
128 const std::filesystem::path repository_path(
m_repository);
129 std::error_code repository_error;
130 if (repository_path.empty() ||
131 !std::filesystem::is_regular_file(repository_path, repository_error)) {
132 ATH_MSG_ERROR(
"Configured Herwig repository does not exist: '" <<
133 repository_path.string() <<
"'");
134 return StatusCode::FAILURE;
137 ATH_MSG_DEBUG(
"Using Herwig default repo from " << repository_path);
138 m_api.repository(repository_path.string());
147 m_runinfo = std::make_shared<HepMC3::GenRunInfo>();
149 struct HepMC3::GenRunInfo::ToolInfo generator={std::string(
"Herwig7"), Herwig::HerwigStrategy::version, std::string(
"Used generator")};
150 m_runinfo->tools().push_back(std::move(generator));
155 return StatusCode::SUCCESS;
161 return StatusCode::SUCCESS;
165 const std::string runfile_suffix =
".run";
166 const auto suffix_pos = inputfile_name.rfind(runfile_suffix);
167 if (suffix_pos != std::string::npos) {
168 inputfile_name.replace(suffix_pos, runfile_suffix.size(),
".in");
170 std::filesystem::path inputfile_path(
m_runfile);
171 inputfile_path.replace_extension(
".in");
172 inputfile_name = inputfile_path.string();
175 ATH_MSG_INFO(
"Writing CA infile text to '"+inputfile_name+
"'");
176 std::ofstream infile_stream(inputfile_name);
178 infile_stream.close();
179 if (!infile_stream) {
180 ATH_MSG_ERROR(
"Failed to write CA infile text to '"+inputfile_name+
"'");
181 return StatusCode::FAILURE;
184 if (share_path.empty()) {
185 ATH_MSG_ERROR(
"Could not determine the Herwig share path for CA infile materialisation");
186 return StatusCode::FAILURE;
189 ATH_MSG_INFO(
"Preparing Herwig runfile from CA infile '"+inputfile_name+
"'");
190 m_api.prependReadDirectory(share_path);
191 m_api.inputfile(inputfile_name);
192 Herwig::API::read(
m_api);
194 return StatusCode::SUCCESS;
206 return StatusCode::SUCCESS;
219 ATH_MSG_DEBUG(
"Converting ThePEG::Event to HepMC::GenEvent");
220 if (!evt->run_info()) evt->set_run_info(
m_runinfo);
221 evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
227 evt->set_event_number(evtInfo->eventNumber());
230 const EventContext& ctx = Gaudi::Hive::currentContext();
232 std::vector<long> seeds(s, s+2);
233 ATH_MSG_DEBUG(
"Random seeds: " << seeds[0] <<
", " << seeds[1]);
237 if (evt->weights().empty()) {
238 evt->weights().push_back(
m_event->weight());
244 ThePEG::tSubProPtr sub =
m_event->primarySubProcess();
245 int id1 = sub->incoming().first ->id();
246 int id2 = sub->incoming().second->id();
248 ThePEG::tcEHPtr eh = ThePEG::dynamic_ptr_cast<ThePEG::tcEHPtr>(
m_event->handler());
250 double x1 = eh->lastX1();
251 double x2 = eh->lastX2();
253 std::pair<ThePEG::PDF,ThePEG::PDF> pdfs;
254 pdfs.first = eh->pdf<ThePEG::PDF>(sub->incoming().first);
255 pdfs.second = eh->pdf<ThePEG::PDF>(sub->incoming().second);
257 ThePEG::Energy2 scale = eh->lastScale();
258 double Q = std::sqrt(scale/ThePEG::GeV2);
260 double pdf1 = pdfs.first.xfx(sub->incoming().first ->dataPtr(), scale, x1);
261 double pdf2 = pdfs.first.xfx(sub->incoming().second->dataPtr(), scale, x2);
263 HepMC3::GenPdfInfoPtr pdfi = std::make_shared<HepMC3::GenPdfInfo>();
264 pdfi->set(id1, id2, x1, x2, Q, pdf1, pdf2);
265 evt->set_pdf_info(std::move(pdfi));
272 return StatusCode::SUCCESS;
283 ATH_MSG_INFO(
"MetaData: generator = Herwig7 " << HWVERSION );
284 ATH_MSG_INFO( std::scientific << std::setprecision(5) <<
"MetaData: cross-section (nb) = " <<
m_gen->eventHandler()->integratedXSec()*
m_xsscale/ThePEG::nanobarn);
288 ThePEG::Repository::cleanup();
291 if (
m_cleanup_herwig_scratch && (std::filesystem::is_directory(
"Herwig-scratch") || std::filesystem::is_directory(
"Herwig-cache"))){
293 ATH_MSG_INFO(
"removing Herwig-scratch/Herwig-cache folder from "+std::filesystem::current_path().
string());
296 std::this_thread::sleep_for(std::chrono::seconds(5));
300 (std::filesystem::remove_all(
"Herwig-scratch") || std::filesystem::remove_all(
"Herwig-cache"));
302 catch (
const std::exception& e) {
303 ATH_MSG_WARNING(
"Failed to delete the folder 'Herwig-scratch': "+std::string(e.what()));
308 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_INFO(x,...)
void convert_to_HepMC(const ThePEG::Event &m_event, HepMC::GenEvent &evt, bool nocopies, ThePEG::Energy eunit, ThePEG::Length lunit)
Athena interface for the Herwig7 generator.
size_t size() const
Number of registered mappings.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
GenModule(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
CLHEP::HepRandomEngine * getRandomEngine(const std::string &streamName, const EventContext &ctx) const
CLHEP::HepRandomEngine * getRandomEngineDuringInitialize(const std::string &streamName, unsigned long int randomSeedOffset, unsigned int conditionsRun=1, unsigned int lbn=1) const
IntegerProperty m_randomSeed
Seed for random number engine.
std::shared_ptr< HepMC3::GenRunInfo > m_runinfo
The run info for HepMC3.
std::string m_pdfname_mpi
PS PDF name, stored for AMI capture at finalize.
StatusCode callGenerator()
Run the generator for one event.
SG::ReadHandleKey< xAOD::EventInfo > m_evtInfoKey
StatusCode genFinalize()
Close down the generator.
ThePEG::EventPtr m_event
ThePEG event object.
std::string m_repository
Herwig/ThePEG repository used to materialise CA run settings.
int m_seed_from_generatetf
Random number seed from Generate_tf.py.
StatusCode writeRunFileFromText(const std::string &share_path)
Write the CA input text into a real Herwig runfile.
std::string m_pdfname_me
ME PDF name, stored for AMI capture at finalize.
bool m_cleanup_herwig_scratch
Possibly remove Herwig-scratch folder after finishing the event generation.
Herwig7(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
double m_xsscale
Scale integrated cross section by a factor for MetaData output.
std::string m_runSettings
In-memory run settings provided by CA fragments.
StatusCode fillEvt(HepMC::GenEvent *evt)
Convert the generated event into the HepMC format.
int m_dsid
Gen_tf.py run args needed in interface.
ThePEG::EGPtr m_gen
ThePEG generator object.
std::string m_runfile
Name of run file.
StatusCode genInitialize()
Initialize the generator.
Herwig7API m_api
Herwig7 API object.
std::string m_setupfile
Name of setup file.
bool m_use_seed_from_generatetf
Ignore random number seed provided by athena and use the one from Generate_tf.py instead.
void set_random_states(GenEvent *e, std::vector< long int > &a)
HepMC3::GenEvent GenEvent