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"
35void convert_to_HepMC(
const ThePEG::Event & m_event, HepMC::GenEvent & evt,
bool nocopies,ThePEG::Energy eunit, ThePEG::Length lunit);
75 const long* seeds = engine->getSeeds();
80 int32_t combined_seed = std::abs(seeds[0] * seeds[1]);
82 std::ostringstream ss_seed;
83 ss_seed << combined_seed;
89 ATH_MSG_INFO(
"Using the random number seed " + ss_seed.str() +
" provided by athena");
90 m_api.seed(combined_seed);
95 ThePEG::Repository::exitOnError() = 1;
98 const char* datapath = getenv(
"DATAPATH" );
99 std::vector<std::string> datapaths;
101 std::string datapath_str(datapath);
102 for (
auto part : std::views::split(datapath_str,
':')) {
103 datapaths.emplace_back(part.begin(), part.end());
106 for(
const std::string& p : datapaths ) {
107 ThePEG::Repository::appendReadDir( p );
109 const char* ldpath = getenv(
"LD_LIBRARY_PATH" );
110 std::vector<std::string> ldpaths;
112 std::string ldpath_str(ldpath);
113 for (
auto part : ldpath_str | std::views::split(
':')) {
114 ldpaths.emplace_back(std::string(part.begin(), part.end()));
117 for(
const std::string& p : ldpaths ) {
118 ThePEG::DynamicLoader::appendPath( p );
121 ATH_MSG_DEBUG(
"Num of library search paths = " << ThePEG::DynamicLoader::allPaths().size());
125 ATH_MSG_DEBUG(
"Loading Herwig default repo from " << repopath);
126 ThePEG::Repository::load(std::move(repopath));
127 ATH_MSG_DEBUG(
"Successfully loaded Herwig default repository");
135 m_runinfo = std::make_shared<HepMC3::GenRunInfo>();
137 struct HepMC3::GenRunInfo::ToolInfo generator={std::string(
"Herwig7"), std::string(
"7"), std::string(
"Used generator")};
138 m_runinfo->tools().push_back(std::move(generator));
144 return StatusCode::SUCCESS;
156 return StatusCode::SUCCESS;
169 ATH_MSG_DEBUG(
"Converting ThePEG::Event to HepMC::GenEvent");
171 if (!evt->run_info()) evt->set_run_info(m_runinfo);
172 evt->set_units(HepMC3::Units::MEV, HepMC3::Units::MM);
179 evt->set_event_number(evtInfo->eventNumber());
182 const EventContext& ctx = Gaudi::Hive::currentContext();
184 std::vector<long> seeds(s, s+2);
185 ATH_MSG_DEBUG(
"Random seeds: " << seeds[0] <<
", " << seeds[1]);
189 if (evt->weights().empty()) {
190 evt->weights().push_back(
m_event->weight());
196 ThePEG::tSubProPtr sub =
m_event->primarySubProcess();
197 int id1 = sub->incoming().first ->id();
198 int id2 = sub->incoming().second->id();
200 ThePEG::tcEHPtr eh = ThePEG::dynamic_ptr_cast<ThePEG::tcEHPtr>(
m_event->handler());
202 double x1 = eh->lastX1();
203 double x2 = eh->lastX2();
205 std::pair<ThePEG::PDF,ThePEG::PDF> pdfs;
206 pdfs.first = eh->pdf<ThePEG::PDF>(sub->incoming().first);
207 pdfs.second = eh->pdf<ThePEG::PDF>(sub->incoming().second);
209 ThePEG::Energy2 scale = eh->lastScale();
210 double Q = std::sqrt(scale/ThePEG::GeV2);
212 double pdf1 = pdfs.first.xfx(sub->incoming().first ->dataPtr(), scale, x1);
213 double pdf2 = pdfs.first.xfx(sub->incoming().second->dataPtr(), scale, x2);
216 HepMC3::GenPdfInfoPtr pdfi = std::make_shared<HepMC3::GenPdfInfo>();
217 pdfi->set(id1,
id2, x1, x2, Q, pdf1, pdf2);
219 HepMC::PdfInfo pdfi(id1,
id2, x1, x2, Q, pdf1, pdf2);
221 evt->set_pdf_info(std::move(pdfi));
233 return StatusCode::SUCCESS;
244 ATH_MSG_INFO(
"MetaData: generator = Herwig7 " << HWVERSION );
245 ATH_MSG_INFO( std::scientific << std::setprecision(5) <<
"MetaData: cross-section (nb) = " <<
m_gen->eventHandler()->integratedXSec()*
m_xsscale/ThePEG::nanobarn);
249 ThePEG::Repository::cleanup();
252 if (
m_cleanup_herwig_scratch && (std::filesystem::is_directory(
"Herwig-scratch") || std::filesystem::is_directory(
"Herwig-cache"))){
254 ATH_MSG_INFO(
"removing Herwig-scratch/Herwig-cache folder from "+std::filesystem::current_path().
string());
257 std::this_thread::sleep_for(std::chrono::seconds(5));
261 (std::filesystem::remove_all(
"Herwig-scratch") || std::filesystem::remove_all(
"Herwig-cache"));
263 catch (
const std::exception& e) {
264 ATH_MSG_WARNING(
"Failed to delete the folder 'Herwig-scratch': "+std::string(e.what()));
269 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(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.
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::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.
int m_seed_from_generatetf
Random number seed from Generate_tf.py.
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.
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.
static std::string find_file_from_list(const std::string &logical_file_name, const std::string &search_list)
void set_random_states(GenEvent *e, std::vector< T > a)