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"
30 #include "boost/thread/thread.hpp"
32 #include "boost/algorithm/string.hpp"
40 m_use_seed_from_generatetf(false),
41 m_seed_from_generatetf(0),
42 m_pdfname_me(
"UNKNOWN"), m_pdfname_mpi(
"UNKNOWN")
74 const long* seeds = engine->getSeeds();
79 int32_t combined_seed = std::abs(seeds[0] * seeds[1]);
81 std::ostringstream ss_seed;
82 ss_seed << combined_seed;
88 ATH_MSG_INFO(
"Using the random number seed " + ss_seed.str() +
" provided by athena");
94 ThePEG::Repository::exitOnError() = 1;
97 char* env1 =
getenv(
"CMTPATH");
98 char* env2 =
getenv(
"CMTCONFIG");
99 std::string reposearchpaths;
100 if (env1 ==
nullptr || env2 ==
nullptr) {
104 std::vector< std::string > datapaths;
106 boost::is_any_of( std::string(
":" ) ) );
107 for(
const std::string&
p : datapaths ) {
108 ThePEG::Repository::appendReadDir(
p );
110 const char* ldpath =
getenv(
"LD_LIBRARY_PATH" );
111 std::vector< std::string > ldpaths;
112 boost::split( ldpaths, ldpath, boost::is_any_of( std::string(
":" ) ) );
113 for(
const std::string&
p : ldpaths ) {
114 ThePEG::DynamicLoader::appendPath(
p );
117 std::vector<std::string> cmtpaths;
118 boost::split(cmtpaths, env1, boost::is_any_of(std::string(
":")));
119 const std::string cmtconfig = env2;
120 const std::string sharepath =
"/InstallArea/" + cmtconfig +
"/share";
121 const std::string libpath =
"/InstallArea/" + cmtconfig +
"/lib";
123 for(
const std::string&
p: cmtpaths) {
124 const std::string cmtsharepath =
p + sharepath;
125 ATH_MSG_DEBUG(
"Appending " + cmtsharepath +
" to ThePEG repository and command file search paths");
126 reposearchpaths = reposearchpaths + (reposearchpaths.length() == 0 ?
"" :
":") + cmtsharepath;
127 ThePEG::Repository::appendReadDir(cmtsharepath);
128 const std::string cmtlibpath =
p + libpath;
129 ATH_MSG_DEBUG(
"Appending " + cmtlibpath +
" to ThePEG loader search path");
130 ThePEG::DynamicLoader::appendPath(cmtlibpath);
133 ATH_MSG_DEBUG(
"Num of library search paths = " << ThePEG::DynamicLoader::allPaths().
size());
137 ATH_MSG_DEBUG(
"Loading Herwig default repo from " << repopath);
139 ATH_MSG_DEBUG(
"Successfully loaded Herwig default repository");
147 m_runinfo = std::make_shared<HepMC3::GenRunInfo>();
149 struct HepMC3::GenRunInfo::ToolInfo
generator={std::string(
"Herwig7"), std::string(
"7"), std::string(
"Used generator")};
156 return StatusCode::SUCCESS;
168 return StatusCode::SUCCESS;
181 ATH_MSG_DEBUG(
"Converting ThePEG::Event to HepMC::GenEvent");
183 if (!
evt->run_info())
evt->set_run_info(m_runinfo);
194 const EventContext& ctx = Gaudi::Hive::currentContext();
196 std::vector<long> seeds(
s,
s+2);
197 ATH_MSG_DEBUG(
"Random seeds: " << seeds[0] <<
", " << seeds[1]);
201 if (
evt->weights().empty()) {
208 ThePEG::tSubProPtr sub =
m_event->primarySubProcess();
209 int id1 = sub->incoming().first ->id();
210 int id2 = sub->incoming().second->id();
212 ThePEG::tcEHPtr eh = ThePEG::dynamic_ptr_cast<ThePEG::tcEHPtr>(
m_event->handler());
214 double x1 = eh->lastX1();
215 double x2 = eh->lastX2();
217 std::pair<ThePEG::PDF,ThePEG::PDF> pdfs;
218 pdfs.first = eh->pdf<
ThePEG::PDF>(sub->incoming().first);
219 pdfs.second = eh->pdf<
ThePEG::PDF>(sub->incoming().second);
221 ThePEG::Energy2
scale = eh->lastScale();
222 double Q = std::sqrt(
scale/ThePEG::GeV2);
224 double pdf1 = pdfs.first.xfx(sub->incoming().first ->dataPtr(),
scale,
x1);
225 double pdf2 = pdfs.first.xfx(sub->incoming().second->dataPtr(),
scale,
x2);
229 pdfi->set(id1,
id2,
x1,
x2, Q, pdf1, pdf2);
231 HepMC::PdfInfo pdfi(id1,
id2,
x1,
x2, Q, pdf1, pdf2);
233 evt->set_pdf_info(pdfi);
245 return StatusCode::SUCCESS;
256 ATH_MSG_INFO(
"MetaData: generator = Herwig7 " << HWVERSION );
261 ThePEG::Repository::cleanup();
264 if (
m_cleanup_herwig_scratch && (std::filesystem::is_directory(
"Herwig-scratch") || std::filesystem::is_directory(
"Herwig-cache"))){
266 ATH_MSG_INFO(
"removing Herwig-scratch/Herwig-cache folder from "+std::filesystem::current_path().
string());
273 (std::filesystem::remove_all(
"Herwig-scratch") || std::filesystem::remove_all(
"Herwig-cache"));
276 ATH_MSG_WARNING(
"Failed to delete the folder 'Herwig-scratch': "+std::string(
e.what()));
281 return StatusCode::SUCCESS;