17 #include <HepMC/GenEvent.h>
18 #include <HepMC/PdfInfo.h>
19 #include <HepMC/IO_GenEvent.h>
21 #include <Herwig/API/HerwigAPI.h>
23 #include <ThePEG/Utilities/DynamicLoader.h>
24 #include <ThePEG/Repository/Repository.h>
25 #include <ThePEG/Handlers/EventHandler.h>
26 #include <ThePEG/Handlers/XComb.h>
27 #include <ThePEG/EventRecord/Event.h>
28 #include <ThePEG/EventRecord/Particle.h>
29 #include <ThePEG/EventRecord/Collision.h>
30 #include <ThePEG/EventRecord/TmpTransform.h>
31 #include <ThePEG/Config/ThePEG.h>
32 #include <ThePEG/PDF/PartonExtractor.h>
33 #include <ThePEG/PDF/PDFBase.h>
34 #include <ThePEG/Utilities/UtilityBase.h>
35 #include <ThePEG/Vectors/HepMCConverter.h>
45 #include "CLHEP/Random/RandomEngine.h"
51 : randomEngineGlueProxy_(ThePEG::RandomEngineGlue::Proxy::
create()),
53 generator_(pset.getParameter<
string>(
"generatorModule")),
54 run_(pset.getParameter<
string>(
"run")),
55 dumpConfig_(pset.getUntrackedParameter<
string>(
"dumpConfig",
"HerwigConfig.in")),
56 skipEvents_(pset.getUntrackedParameter<unsigned int>(
"skipEvents", 0)) {
59 if (!dumpEvents.empty()) {
61 edm::LogInfo(
"ThePEGSource") <<
"Event logging switched on (=> " << dumpEvents <<
")";
71 edm::LogInfo(
"Herwig7Interface") <<
"Event generator finalized";
87 std::transform(runModeTemp.begin(), runModeTemp.end(), runModeTemp.begin(), ::tolower);
89 while (!runModeTemp.empty()) {
92 size_t pos = runModeTemp.find(
',');
93 if (std::string::npos == pos)
96 choice = runModeTemp.substr(0, pos);
98 if (pos == std::string::npos)
101 runModeTemp.erase(0, pos + 1);
104 edm::LogInfo(
"Herwig7Interface") <<
"HerwigUIProvider object with run mode " <<
HwUI_->runMode() <<
" created.\n";
107 if (choice ==
"read") {
111 <<
" will be passed to Herwig for the read step.\n";
113 }
else if (choice ==
"build") {
117 <<
" will be passed to Herwig for the build step.\n";
120 }
else if (choice ==
"integrate") {
122 edm::LogInfo(
"Herwig7Interface") <<
"Run file " << runFileName
123 <<
" will be passed to Herwig for the integrate step.\n";
124 HwUI_->setRunMode(Herwig::RunMode::INTEGRATE, pset, runFileName);
127 }
else if (choice ==
"run") {
129 edm::LogInfo(
"Herwig7Interface") <<
"Run file " << runFileName <<
" will be passed to Herwig for the run step.\n";
130 HwUI_->setRunMode(Herwig::RunMode::RUN, pset, runFileName);
132 edm::LogInfo(
"Herwig7Interface") <<
"Cannot recognize \"" << choice <<
"\".\n"
133 <<
"Trying to skip step.\n";
141 edm::LogInfo(
"Herwig7Interface") <<
"callHerwigGenerator function invoked with run mode " <<
HwUI_->runMode()
145 switch (
HwUI_->runMode()) {
146 case Herwig::RunMode::INIT:
149 case Herwig::RunMode::READ:
152 case Herwig::RunMode::BUILD:
155 case Herwig::RunMode::INTEGRATE:
156 Herwig::API::integrate(*
HwUI_);
158 case Herwig::RunMode::MERGEGRIDS:
159 Herwig::API::mergegrids(*
HwUI_);
161 case Herwig::RunMode::RUN: {
163 eg_ = Herwig::API::prepareRun(*
HwUI_);
167 edm::LogError(
"Herwig7Interface") <<
"Error during read in of command line parameters.\n"
168 <<
"Program execution will stop now.";
171 HwUI_->quitWithHelp();
177 edm::LogError(
"Herwig7Interface") <<
": ThePEG::Exception caught.\n"
179 <<
"See logfile for details.\n";
182 edm::LogError(
"Herwig7Interface") <<
": " << e.what() <<
'\n';
184 }
catch (
const char *what) {
185 edm::LogError(
"Herwig7Interface") <<
": caught exception: " << what <<
"\n";
191 if (
HwUI_->runMode() == Herwig::RunMode::RUN) {
192 edm::LogInfo(
"Herwig7Interface") <<
"Starting EventGenerator initialization";
194 edm::LogInfo(
"Herwig7Interface") <<
"EventGenerator initialized";
206 edm::LogInfo(
"Herwig7Interface") <<
"Stopped EventGenerator due to missing run mode.";
231 using namespace ThePEG;
233 if (!event->primaryCollision())
236 tSubProPtr sub =
event->primaryCollision()->primarySubProcess();
237 TmpTransform<tSubProPtr>
tmp(sub, Utilities::getBoostToCM(sub->incoming()));
239 double pthat = (*sub->outgoing().begin())->momentum().perp() / ThePEG::GeV;
240 for (PVector::const_iterator it = sub->outgoing().begin(); it != sub->outgoing().end(); ++it)
241 pthat = std::min<double>(
pthat, (*it)->momentum().perp() / ThePEG::GeV);
253 stringstream logstream;
256 stringstream herwiginputconfig;
264 vector<string> configFiles = pset.
getParameter<vector<string> >(
"configFiles");
266 for (
const auto &iter : configFiles) {
268 ifstream externalConfigFile(iter);
269 if (externalConfigFile.is_open()) {
270 edm::LogInfo(
"Herwig7Interface") <<
"Reading config file (" << iter <<
")" << endl;
271 stringstream configFileStream;
272 configFileStream << externalConfigFile.rdbuf();
273 string configFileContent = configFileStream.str();
276 string searchKeyword(
"saverun");
277 if (configFileContent.find(searchKeyword) != std::string::npos) {
278 edm::LogInfo(
"Herwig7Interface") <<
"Commented out saverun command in external input config file(" << iter
280 configFileContent.insert(configFileContent.find(searchKeyword),
"#");
282 herwiginputconfig <<
"# Begin Config file input" << endl
283 << configFileContent << endl
284 <<
"# End Config file input";
285 edm::LogInfo(
"Herwig7Interface") <<
"Finished reading config file (" << iter <<
")" << endl;
287 edm::LogWarning(
"Herwig7Interface") <<
"Could not read config file (" << iter <<
")" << endl;
291 edm::LogInfo(
"Herwig7Interface") <<
"Start with processing CMSSW config" << endl;
295 iter = collector.
begin();
296 herwiginputconfig << endl <<
"# Begin Parameter set input\n" << endl;
297 for (; iter != collector.
end(); ++iter) {
298 herwiginputconfig << *iter << endl;
302 herwiginputconfig <<
"saverun " <<
run_ <<
" " <<
generator_ << endl;
309 cfgDump << herwiginputconfig.str() << endl;
T getUntrackedParameter(std::string const &, T const &) const
static double pthat(const ThePEG::EventPtr &event)
void initRepository(const edm::ParameterSet ¶ms)
void callHerwigGenerator()
void createInputFile(const edm::ParameterSet ¶ms)
std::shared_ptr< Herwig::HerwigUIProvider > HwUI_
void setPEGRandomEngine(CLHEP::HepRandomEngine *)
std::auto_ptr< HepMC::IO_BaseClass > iobc_
CLHEP::HepRandomEngine * randomEngine
~Herwig7Interface() noexcept
Log< level::Error, false > LogError
std::shared_ptr< ThePEG::RandomEngineGlue::Proxy > randomEngineGlueProxy_
static std::auto_ptr< HepMC::GenEvent > convert(const ThePEG::EventPtr &event)
Herwig7Interface(const edm::ParameterSet ¶ms)
const std::string generator_
Log< level::Info, false > LogInfo
const unsigned int skipEvents_
T getParameter(std::string const &) const
const_iterator end() const
const_iterator begin() const
void flushRandomNumberGenerator()
void setRandomEngine(CLHEP::HepRandomEngine *v)
Log< level::Warning, false > LogWarning